feat(tracker, gioui): add ability & button to delete tracks.

Refer #46.
This commit is contained in:
vsariola 2021-03-11 22:26:42 +02:00
parent 87f373370e
commit 01226a2910
3 changed files with 36 additions and 0 deletions

View File

@ -32,6 +32,10 @@ func (t *Tracker) layoutTracker(gtx layout.Context) layout.Dimensions {
t.AddTrack(true)
}
for t.DeleteTrackBtn.Clicked() {
t.DeleteTrack(false)
}
//t.TrackHexCheckBoxes[i2].Value = t.TrackShowHex[i2]
//cbStyle := material.CheckBox(t.Theme, t.TrackHexCheckBoxes[i2], "hex")
//cbStyle.Color = white
@ -70,6 +74,14 @@ func (t *Tracker) layoutTracker(gtx layout.Context) layout.Dimensions {
subtractOctaveBtnStyle.Color = primaryColor
subtractOctaveBtnStyle.Background = transparent
subtractOctaveBtnStyle.Inset = layout.UniformInset(unit.Dp(6))
deleteTrackBtnStyle := material.IconButton(t.Theme, t.DeleteTrackBtn, widgetForIcon(icons.ActionDelete))
deleteTrackBtnStyle.Background = transparent
deleteTrackBtnStyle.Inset = layout.UniformInset(unit.Dp(6))
if t.CanDeleteTrack() {
deleteTrackBtnStyle.Color = primaryColor
} else {
deleteTrackBtnStyle.Color = disabledTextColor
}
newTrackBtnStyle := material.IconButton(t.Theme, t.NewTrackBtn, widgetForIcon(icons.ContentAdd))
newTrackBtnStyle.Background = transparent
newTrackBtnStyle.Inset = layout.UniformInset(unit.Dp(6))
@ -110,6 +122,7 @@ func (t *Tracker) layoutTracker(gtx layout.Context) layout.Dimensions {
layout.Rigid(Label(" Voices:", white)),
layout.Rigid(voiceUpDown),
layout.Flexed(1, func(gtx C) D { return layout.Dimensions{Size: gtx.Constraints.Min} }),
layout.Rigid(deleteTrackBtnStyle.Layout),
layout.Rigid(newTrackBtnStyle.Layout))
t.Song().Score.Tracks[t.Cursor().Track].Effect = t.TrackHexCheckBox.Value // TODO: we should not modify the model, but how should this be done
t.SetTrackVoices(t.TrackVoices.Value)

View File

@ -28,6 +28,7 @@ type Tracker struct {
TrackVoices *NumberInput
InstrumentNameEditor *widget.Editor
NewTrackBtn *widget.Clickable
DeleteTrackBtn *widget.Clickable
NewInstrumentBtn *widget.Clickable
DeleteInstrumentBtn *widget.Clickable
AddSemitoneBtn *widget.Clickable
@ -113,6 +114,7 @@ func New(audioContext sointu.AudioContext, synthService sointu.SynthService, syn
TrackVoices: new(NumberInput),
InstrumentNameEditor: &widget.Editor{SingleLine: true, Submit: true, Alignment: text.Middle},
NewTrackBtn: new(widget.Clickable),
DeleteTrackBtn: new(widget.Clickable),
NewInstrumentBtn: new(widget.Clickable),
DeleteInstrumentBtn: new(widget.Clickable),
AddSemitoneBtn: new(widget.Clickable),

View File

@ -203,6 +203,27 @@ func (m *Model) CanAddTrack() bool {
return m.song.Score.NumVoices() < 32
}
func (m *Model) DeleteTrack(forward bool) {
if !m.CanDeleteTrack() {
return
}
m.saveUndo("DeleteTrack", 0)
newTracks := make([]sointu.Track, len(m.song.Score.Tracks)-1)
copy(newTracks, m.song.Score.Tracks[:m.cursor.Track])
copy(newTracks[m.cursor.Track:], m.song.Score.Tracks[m.cursor.Track+1:])
m.song.Score.Tracks = newTracks
if !forward {
m.cursor.Track--
}
m.selectionCorner = m.cursor
m.clampPositions()
m.notifyScoreChange()
}
func (m *Model) CanDeleteTrack() bool {
return len(m.song.Score.Tracks) > 1
}
func (m *Model) SetTrackVoices(value int) {
if value < 1 {
value = 1