From fd1d018e82a3fa6fa0875a01c5bdcd19d3fc4f58 Mon Sep 17 00:00:00 2001 From: vsariola <5684185+vsariola@users.noreply.github.com> Date: Thu, 18 Feb 2021 22:48:48 +0200 Subject: [PATCH] feat(tracker): adding and deleting order rows, also backward and forward deletes --- tracker/instruments.go | 2 +- tracker/keyevent.go | 26 ++++++++++++++++++-- tracker/tracker.go | 56 +++++++++++++++++++++++++++++++++++++----- tracker/uniteditor.go | 2 +- 4 files changed, 76 insertions(+), 10 deletions(-) diff --git a/tracker/instruments.go b/tracker/instruments.go index 2daa8f0..27e9366 100644 --- a/tracker/instruments.go +++ b/tracker/instruments.go @@ -181,7 +181,7 @@ func (t *Tracker) layoutInstrumentNames(gtx C) D { } func (t *Tracker) layoutInstrumentEditor(gtx C) D { for t.AddUnitBtn.Clicked() { - t.AddUnit() + t.AddUnit(true) } addUnitBtnStyle := material.IconButton(t.Theme, t.AddUnitBtn, widgetForIcon(icons.ContentAdd)) addUnitBtnStyle.Color = t.Theme.ContrastFg diff --git a/tracker/keyevent.go b/tracker/keyevent.go index 48b71cc..f767212 100644 --- a/tracker/keyevent.go +++ b/tracker/keyevent.go @@ -164,13 +164,28 @@ func (t *Tracker) KeyEvent(w *app.Window, e key.Event) bool { case "F8": t.SetPlaying(false) return true - case key.NameDeleteBackward, key.NameDeleteForward: + case key.NameDeleteForward: switch t.EditMode { + case EditPatterns: + t.DeleteOrderRow(true) + return true case EditTracks: t.DeleteSelection() return true case EditUnits: - t.DeleteUnit() + t.DeleteUnit(true) + return true + } + case key.NameDeleteBackward: + switch t.EditMode { + case EditPatterns: + t.DeleteOrderRow(false) + return true + case EditTracks: + t.DeleteSelection() + return true + case EditUnits: + t.DeleteUnit(false) return true } case "Space": @@ -200,6 +215,13 @@ func (t *Tracker) KeyEvent(w *app.Window, e key.Event) bool { t.EditMode = (t.EditMode + 1) % 4 } return true + case key.NameReturn: + switch t.EditMode { + case EditPatterns: + t.AddOrderRow(!e.Modifiers.Contain(key.ModShortcut)) + case EditUnits: + t.AddUnit(!e.Modifiers.Contain(key.ModShortcut)) + } case key.NameUpArrow: switch t.EditMode { case EditPatterns: diff --git a/tracker/tracker.go b/tracker/tracker.go index 6711929..b2d42cf 100644 --- a/tracker/tracker.go +++ b/tracker/tracker.go @@ -440,7 +440,7 @@ func (t *Tracker) SetUnit(typ string) { t.sequencer.SetPatch(t.song.Patch) } -func (t *Tracker) AddUnit() { +func (t *Tracker) AddUnit(after bool) { t.SaveUndo() start := t.song.FirstInstrumentVoice(t.CurrentInstrument) end := start + t.song.Patch.Instruments[t.CurrentInstrument].NumVoices @@ -456,15 +456,59 @@ func (t *Tracker) AddUnit() { } } units := make([]sointu.Unit, len(t.song.Patch.Instruments[t.CurrentInstrument].Units)+1) - copy(units, t.song.Patch.Instruments[t.CurrentInstrument].Units[:t.CurrentUnit+1]) - copy(units[t.CurrentUnit+2:], t.song.Patch.Instruments[t.CurrentInstrument].Units[t.CurrentUnit+1:]) + newUnitIndex := t.CurrentUnit + if after { + newUnitIndex++ + } + copy(units, t.song.Patch.Instruments[t.CurrentInstrument].Units[:newUnitIndex]) + copy(units[newUnitIndex+1:], t.song.Patch.Instruments[t.CurrentInstrument].Units[newUnitIndex:]) t.song.Patch.Instruments[t.CurrentInstrument].Units = units - t.CurrentUnit++ + t.CurrentUnit = newUnitIndex t.CurrentParam = 0 t.ClampPositions() t.sequencer.SetPatch(t.song.Patch) } +func (t *Tracker) AddOrderRow(after bool) { + t.SaveUndo() + l := t.song.SequenceLength() + newRowIndex := t.Cursor.Pattern + if after { + newRowIndex++ + } + for i, trk := range t.song.Tracks { + seq := make([]byte, l+1) + copy(seq, trk.Sequence[:newRowIndex]) + copy(seq[newRowIndex+1:], trk.Sequence[newRowIndex:]) + t.song.Tracks[i].Sequence = seq + } + t.Cursor.Pattern = newRowIndex + t.SelectionCorner = t.Cursor + t.ClampPositions() +} + +func (t *Tracker) DeleteOrderRow(forward bool) { + l := t.song.SequenceLength() + if l <= 1 { + return + } + t.SaveUndo() + c := t.Cursor.Pattern + for i, trk := range t.song.Tracks { + seq := make([]byte, l-1) + copy(seq, trk.Sequence[:c]) + copy(seq[c:], trk.Sequence[c+1:]) + t.song.Tracks[i].Sequence = seq + } + if !forward { + if t.Cursor.Pattern > 0 { + t.Cursor.Pattern-- + } + } + t.SelectionCorner = t.Cursor + t.ClampPositions() +} + func (t *Tracker) ClearUnit() { t.SaveUndo() t.song.Patch.Instruments[t.CurrentInstrument].Units[t.CurrentUnit].Type = "" @@ -474,7 +518,7 @@ func (t *Tracker) ClearUnit() { t.sequencer.SetPatch(t.song.Patch) } -func (t *Tracker) DeleteUnit() { +func (t *Tracker) DeleteUnit(forward bool) { if len(t.song.Patch.Instruments[t.CurrentInstrument].Units) <= 1 { return } @@ -498,7 +542,7 @@ func (t *Tracker) DeleteUnit() { copy(units, t.song.Patch.Instruments[t.CurrentInstrument].Units[:t.CurrentUnit]) copy(units[t.CurrentUnit:], t.song.Patch.Instruments[t.CurrentInstrument].Units[t.CurrentUnit+1:]) t.song.Patch.Instruments[t.CurrentInstrument].Units = units - if t.CurrentUnit > 0 { + if !forward && t.CurrentUnit > 0 { t.CurrentUnit-- } t.CurrentParam = 0 diff --git a/tracker/uniteditor.go b/tracker/uniteditor.go index 7c9df55..c315991 100644 --- a/tracker/uniteditor.go +++ b/tracker/uniteditor.go @@ -168,7 +168,7 @@ func (t *Tracker) layoutUnitFooter() layout.Widget { op.InvalidateOp{}.Add(gtx.Ops) } for t.DeleteUnitBtn.Clicked() { - t.DeleteUnit() + t.DeleteUnit(false) op.InvalidateOp{}.Add(gtx.Ops) } deleteUnitBtnStyle := material.IconButton(t.Theme, t.DeleteUnitBtn, widgetForIcon(icons.ActionDelete))