feat: edit arrangement selections atomically with grouped undo
This commit is contained in:
parent
c5c7df1d9d
commit
7379545ced
10 changed files with 139 additions and 25 deletions
|
|
@ -320,6 +320,8 @@ MainWindow::MainWindow(bool prompt_unsaved,const char *configuration)
|
|||
(void)static_cast<MainWindow *>(c)->sequencer_->grid(xtc_ui::snap_ticks[value]);
|
||||
},this);
|
||||
Fl_Group::current(parent);
|
||||
menu->add("Edit/Nudge selection left",0,[](Fl_Widget *,void *c){static_cast<SequencerWidget *>(c)->nudge_selection(-1);},sequencer_);
|
||||
menu->add("Edit/Nudge selection right",0,[](Fl_Widget *,void *c){static_cast<SequencerWidget *>(c)->nudge_selection(1);},sequencer_);
|
||||
menu->add("Edit/Select all clips",0,[](Fl_Widget *,void *c){static_cast<SequencerWidget *>(c)->select_all_clips();},sequencer_);
|
||||
menu->add("Edit/Duplicate clip",FL_CTRL+'d',[](Fl_Widget *, void *c) { (void)static_cast<SequencerWidget *>(c)->duplicate_selection(); },sequencer_);
|
||||
menu->add("Edit/Split audio at cursor", 0, [](Fl_Widget *, void *c) { (void)static_cast<SequencerWidget *>(c)->split_selection(); }, sequencer_);
|
||||
|
|
|
|||
|
|
@ -209,19 +209,11 @@ xtc_result SequencerWidget::add_midi_clip()
|
|||
*/
|
||||
xtc_result SequencerWidget::delete_selection()
|
||||
{
|
||||
if (before_edit) {
|
||||
xtc_result r = before_edit(context_);
|
||||
if (r != XTC_OK) {
|
||||
changed(r);
|
||||
return r;
|
||||
}
|
||||
}
|
||||
xtc_result r =
|
||||
clip_ != UINT64_MAX ? xtc_project_remove_clip(project_, clip_) : xtc_project_remove_track(project_, track_);
|
||||
if (r == XTC_OK)
|
||||
clip_ = track_ = UINT64_MAX;
|
||||
changed(r);
|
||||
return r;
|
||||
if(!selected_clips().empty())return transform_selection(XTC_CLIPS_DELETE,0);
|
||||
xtc_result r=before_edit ? before_edit(context_) : XTC_OK;
|
||||
if(r==XTC_OK)r=xtc_project_remove_track(project_,track_);
|
||||
if(r==XTC_OK)track_=clip_=UINT64_MAX;
|
||||
changed(r);return r;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -497,6 +489,7 @@ int SequencerWidget::handle(int event)
|
|||
if (Fl::event_key() == 's') { split_selection(); return 1; }
|
||||
if (Fl::event_key() == 'a') { audio_properties_dialog(); return 1; }
|
||||
}
|
||||
if(event==FL_KEYDOWN && !Fl::event_state(FL_CTRL|FL_ALT|FL_SHIFT) && (Fl::event_key()==FL_Left || Fl::event_key()==FL_Right)) {nudge_selection(Fl::event_key()==FL_Left ? -1 : 1);return 1;}
|
||||
if (event == FL_KEYDOWN && Fl::event_state(FL_CTRL) && Fl::event_key() == 'a') {select_all_clips();return 1;}
|
||||
if (event == FL_KEYDOWN && Fl::event_state(FL_CTRL) && Fl::event_key() == 'd') { duplicate_selection(); return 1; }
|
||||
if (event == FL_KEYDOWN && Fl::event_key() == FL_F+2) {
|
||||
|
|
@ -548,14 +541,27 @@ xtc_result SequencerWidget::grid(uint64_t ticks)
|
|||
if (xtc_ui::snap_index(ticks) < 0) return XTC_ERR_INVALID_ARGUMENT;
|
||||
grid_ = ticks; redraw(); return XTC_OK;
|
||||
}
|
||||
xtc_result SequencerWidget::transform_selection(xtc_clip_operation op,int64_t amount)
|
||||
{
|
||||
auto ids=selected_clips();std::vector<uint64_t> output(ids.size());
|
||||
xtc_result r=ids.empty() ? XTC_ERR_NOT_FOUND : XTC_OK;
|
||||
if(r==XTC_OK && before_edit)r=before_edit(context_);
|
||||
if(r==XTC_OK)r=xtc_project_transform_clips(project_,ids.data(),ids.size(),op,amount,output.data());
|
||||
if(r==XTC_OK) {
|
||||
selection_=op==XTC_CLIPS_DELETE ? std::vector<uint64_t>{} : output;
|
||||
clip_=selection_.empty() ? UINT64_MAX : selection_.back();
|
||||
}
|
||||
changed(r);return r;
|
||||
}
|
||||
xtc_result SequencerWidget::nudge_selection(int direction)
|
||||
{
|
||||
return transform_selection(XTC_CLIPS_MOVE,direction<0 ? -static_cast<int64_t>(grid_) : static_cast<int64_t>(grid_));
|
||||
}
|
||||
xtc_result SequencerWidget::duplicate_selection()
|
||||
{
|
||||
xtc_clip_info c{}; uint64_t id;
|
||||
xtc_result r = xtc_project_find_clip(project_,clip_,&c);
|
||||
if (r == XTC_OK && before_edit) r = before_edit(context_);
|
||||
if (r == XTC_OK) r = xtc_project_duplicate_clip(project_,clip_,c.track_id,c.start_tick+c.length_tick,&id);
|
||||
if (r == XTC_OK) clip_ = id;
|
||||
changed(r); return r;
|
||||
auto ids=selected_clips();uint64_t begin=XTC_PROJECT_MAX_TICK,end=0;
|
||||
for(auto id:ids){xtc_clip_info c{};xtc_project_find_clip(project_,id,&c);begin=std::min(begin,c.start_tick);end=std::max(end,c.start_tick+c.length_tick);}
|
||||
return transform_selection(XTC_CLIPS_DUPLICATE,static_cast<int64_t>(end)-static_cast<int64_t>(begin));
|
||||
}
|
||||
xtc_result SequencerWidget::rename_selection(const char *name)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@
|
|||
#include <FL/Fl_Group.H>
|
||||
#include <FL/Fl_Choice.H>
|
||||
#include "xtc/xtc_plugin.h"
|
||||
#include "xtc/xtc_editing.h"
|
||||
#include <array>
|
||||
#include "xtc/xtc_audio.h"
|
||||
#include <memory>
|
||||
|
|
@ -36,6 +37,8 @@ class SequencerWidget : public Fl_Group {
|
|||
void select_all_clips();
|
||||
bool clip_selected(uint64_t clip) const;
|
||||
std::vector<uint64_t> selected_clips() const;
|
||||
xtc_result transform_selection(xtc_clip_operation operation,int64_t amount);
|
||||
xtc_result nudge_selection(int direction);
|
||||
xtc_result duplicate_selection();
|
||||
xtc_result split_selection();
|
||||
xtc_result audio_properties(float gain, uint64_t fade_in, uint64_t fade_out);
|
||||
|
|
|
|||
40
src/project/editing.c
Normal file
40
src/project/editing.c
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
#include "project_internal.h"
|
||||
#include "xtc/xtc_editing.h"
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
static xtc_result selection_valid(const xtc_project *p,const uint64_t *ids,size_t count)
|
||||
{
|
||||
size_t i,j;xtc_clip_info c;
|
||||
if(!p || !ids || !count || count>XTC_PROJECT_MAX_CLIPS)return XTC_ERR_INVALID_ARGUMENT;
|
||||
for(i=0;i<count;++i) {
|
||||
if(xtc_project_find_clip(p,ids[i],&c)!=XTC_OK)return XTC_ERR_NOT_FOUND;
|
||||
for(j=0;j<i;++j)if(ids[i]==ids[j])return XTC_ERR_INVALID_ARGUMENT;
|
||||
}
|
||||
return XTC_OK;
|
||||
}
|
||||
static void publish(xtc_project *p,xtc_project *copy)
|
||||
{
|
||||
xtc_graph *old=p->graph;*p=*copy;copy->graph=old;
|
||||
}
|
||||
xtc_result xtc_project_transform_clips(xtc_project *p,const uint64_t *ids,size_t count,
|
||||
xtc_clip_operation operation,int64_t amount,uint64_t *out_ids)
|
||||
{
|
||||
xtc_project *copy=NULL;size_t i;uint64_t output[XTC_PROJECT_MAX_CLIPS];
|
||||
xtc_result r=selection_valid(p,ids,count);
|
||||
if(r!=XTC_OK)return r;
|
||||
if(operation<XTC_CLIPS_MOVE || operation>XTC_CLIPS_DELETE ||
|
||||
amount<-(int64_t)XTC_PROJECT_MAX_TICK || amount>(int64_t)XTC_PROJECT_MAX_TICK)return XTC_ERR_INVALID_ARGUMENT;
|
||||
r=xtc_project_clone(p,©);
|
||||
for(i=0;r==XTC_OK && i<count;++i) {
|
||||
xtc_clip_info c;int64_t start;
|
||||
r=xtc_project_find_clip(copy,ids[i],&c);if(r!=XTC_OK)break;
|
||||
start=(int64_t)c.start_tick+amount;output[i]=ids[i];
|
||||
if(operation==XTC_CLIPS_DELETE)r=xtc_project_remove_clip(copy,ids[i]);
|
||||
else if(start<0 || start>(int64_t)XTC_PROJECT_MAX_TICK)r=XTC_ERR_INVALID_ARGUMENT;
|
||||
else if(operation==XTC_CLIPS_DUPLICATE)r=xtc_project_duplicate_clip(copy,ids[i],c.track_id,(uint64_t)start,output+i);
|
||||
else r=xtc_project_edit_clip(copy,ids[i],c.track_id,(uint64_t)start,c.length_tick,c.offset_tick);
|
||||
}
|
||||
if(r==XTC_OK){publish(p,copy);if(out_ids)memcpy(out_ids,output,count*sizeof(*output));}
|
||||
xtc_project_destroy(copy);return r;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue