feat(tracker): adding and deleting order rows, also backward and forward deletes

This commit is contained in:
vsariola 2021-02-18 22:48:48 +02:00
parent a27494e17d
commit fd1d018e82
4 changed files with 76 additions and 10 deletions

View File

@ -181,7 +181,7 @@ func (t *Tracker) layoutInstrumentNames(gtx C) D {
} }
func (t *Tracker) layoutInstrumentEditor(gtx C) D { func (t *Tracker) layoutInstrumentEditor(gtx C) D {
for t.AddUnitBtn.Clicked() { for t.AddUnitBtn.Clicked() {
t.AddUnit() t.AddUnit(true)
} }
addUnitBtnStyle := material.IconButton(t.Theme, t.AddUnitBtn, widgetForIcon(icons.ContentAdd)) addUnitBtnStyle := material.IconButton(t.Theme, t.AddUnitBtn, widgetForIcon(icons.ContentAdd))
addUnitBtnStyle.Color = t.Theme.ContrastFg addUnitBtnStyle.Color = t.Theme.ContrastFg

View File

@ -164,13 +164,28 @@ func (t *Tracker) KeyEvent(w *app.Window, e key.Event) bool {
case "F8": case "F8":
t.SetPlaying(false) t.SetPlaying(false)
return true return true
case key.NameDeleteBackward, key.NameDeleteForward: case key.NameDeleteForward:
switch t.EditMode { switch t.EditMode {
case EditPatterns:
t.DeleteOrderRow(true)
return true
case EditTracks: case EditTracks:
t.DeleteSelection() t.DeleteSelection()
return true return true
case EditUnits: 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 return true
} }
case "Space": case "Space":
@ -200,6 +215,13 @@ func (t *Tracker) KeyEvent(w *app.Window, e key.Event) bool {
t.EditMode = (t.EditMode + 1) % 4 t.EditMode = (t.EditMode + 1) % 4
} }
return true 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: case key.NameUpArrow:
switch t.EditMode { switch t.EditMode {
case EditPatterns: case EditPatterns:

View File

@ -440,7 +440,7 @@ func (t *Tracker) SetUnit(typ string) {
t.sequencer.SetPatch(t.song.Patch) t.sequencer.SetPatch(t.song.Patch)
} }
func (t *Tracker) AddUnit() { func (t *Tracker) AddUnit(after bool) {
t.SaveUndo() t.SaveUndo()
start := t.song.FirstInstrumentVoice(t.CurrentInstrument) start := t.song.FirstInstrumentVoice(t.CurrentInstrument)
end := start + t.song.Patch.Instruments[t.CurrentInstrument].NumVoices 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) 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]) newUnitIndex := t.CurrentUnit
copy(units[t.CurrentUnit+2:], t.song.Patch.Instruments[t.CurrentInstrument].Units[t.CurrentUnit+1:]) 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.song.Patch.Instruments[t.CurrentInstrument].Units = units
t.CurrentUnit++ t.CurrentUnit = newUnitIndex
t.CurrentParam = 0 t.CurrentParam = 0
t.ClampPositions() t.ClampPositions()
t.sequencer.SetPatch(t.song.Patch) 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() { func (t *Tracker) ClearUnit() {
t.SaveUndo() t.SaveUndo()
t.song.Patch.Instruments[t.CurrentInstrument].Units[t.CurrentUnit].Type = "" t.song.Patch.Instruments[t.CurrentInstrument].Units[t.CurrentUnit].Type = ""
@ -474,7 +518,7 @@ func (t *Tracker) ClearUnit() {
t.sequencer.SetPatch(t.song.Patch) 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 { if len(t.song.Patch.Instruments[t.CurrentInstrument].Units) <= 1 {
return return
} }
@ -498,7 +542,7 @@ func (t *Tracker) DeleteUnit() {
copy(units, t.song.Patch.Instruments[t.CurrentInstrument].Units[:t.CurrentUnit]) 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:]) copy(units[t.CurrentUnit:], t.song.Patch.Instruments[t.CurrentInstrument].Units[t.CurrentUnit+1:])
t.song.Patch.Instruments[t.CurrentInstrument].Units = units t.song.Patch.Instruments[t.CurrentInstrument].Units = units
if t.CurrentUnit > 0 { if !forward && t.CurrentUnit > 0 {
t.CurrentUnit-- t.CurrentUnit--
} }
t.CurrentParam = 0 t.CurrentParam = 0

View File

@ -168,7 +168,7 @@ func (t *Tracker) layoutUnitFooter() layout.Widget {
op.InvalidateOp{}.Add(gtx.Ops) op.InvalidateOp{}.Add(gtx.Ops)
} }
for t.DeleteUnitBtn.Clicked() { for t.DeleteUnitBtn.Clicked() {
t.DeleteUnit() t.DeleteUnit(false)
op.InvalidateOp{}.Add(gtx.Ops) op.InvalidateOp{}.Add(gtx.Ops)
} }
deleteUnitBtnStyle := material.IconButton(t.Theme, t.DeleteUnitBtn, widgetForIcon(icons.ActionDelete)) deleteUnitBtnStyle := material.IconButton(t.Theme, t.DeleteUnitBtn, widgetForIcon(icons.ActionDelete))