mirror of
https://github.com/vsariola/sointu.git
synced 2025-05-27 10:50:23 -04:00
refactor(tracker): use built-in min & max instead of intMin & intMax
This commit is contained in:
parent
0ba6557f65
commit
1c42a51cc6
@ -50,7 +50,7 @@ func (m *Model) AddTrack() Action {
|
||||
} else {
|
||||
m.d.Cursor.Track++
|
||||
}
|
||||
m.d.Cursor.Track = intMax(intMin(m.d.Cursor.Track, len(m.d.Song.Score.Tracks)), 0)
|
||||
m.d.Cursor.Track = max(min(m.d.Cursor.Track, len(m.d.Song.Score.Tracks)), 0)
|
||||
newTracks := make([]sointu.Track, len(m.d.Song.Score.Tracks)+1)
|
||||
copy(newTracks, m.d.Song.Score.Tracks[:m.d.Cursor.Track])
|
||||
copy(newTracks[m.d.Cursor.Track+1:], m.d.Song.Score.Tracks[m.d.Cursor.Track:])
|
||||
@ -112,7 +112,7 @@ func (m *Model) AddUnit(before bool) Action {
|
||||
m.d.UnitIndex++
|
||||
}
|
||||
}
|
||||
m.d.InstrIndex = intMax(intMin(m.d.InstrIndex, len(m.d.Song.Patch)-1), 0)
|
||||
m.d.InstrIndex = max(min(m.d.InstrIndex, len(m.d.Song.Patch)-1), 0)
|
||||
instr := m.d.Song.Patch[m.d.InstrIndex]
|
||||
newUnits := make([]sointu.Unit, len(instr.Units)+1)
|
||||
m.d.UnitIndex = clamp(m.d.UnitIndex, 0, len(newUnits)-1)
|
||||
@ -141,7 +141,7 @@ func (m *Model) ClearUnit() Action {
|
||||
return Action{
|
||||
do: func() {
|
||||
defer (*Model)(m).change("DeleteUnitAction", PatchChange, MajorChange)()
|
||||
m.d.UnitIndex = intMax(intMin(m.d.UnitIndex, len(m.d.Song.Patch[m.d.InstrIndex].Units)-1), 0)
|
||||
m.d.UnitIndex = max(min(m.d.UnitIndex, len(m.d.Song.Patch[m.d.InstrIndex].Units)-1), 0)
|
||||
m.d.Song.Patch[m.d.InstrIndex].Units[m.d.UnitIndex] = sointu.Unit{}
|
||||
m.d.Song.Patch[m.d.InstrIndex].Units[m.d.UnitIndex].ID = m.maxID() + 1
|
||||
},
|
||||
|
@ -213,7 +213,7 @@ func (m *Mute) setValue(val bool) {
|
||||
return
|
||||
}
|
||||
defer (*Model)(m).change("Mute", PatchChange, MinorChange)()
|
||||
a, b := intMin(m.d.InstrIndex, m.d.InstrIndex2), intMax(m.d.InstrIndex, m.d.InstrIndex2)
|
||||
a, b := min(m.d.InstrIndex, m.d.InstrIndex2), max(m.d.InstrIndex, m.d.InstrIndex2)
|
||||
for i := a; i <= b; i++ {
|
||||
if i < 0 || i >= len(m.d.Song.Patch) {
|
||||
continue
|
||||
@ -227,7 +227,7 @@ func (m *Mute) Enabled() bool { return m.d.InstrIndex >= 0 && m.d.InstrIndex < l
|
||||
|
||||
func (m *Solo) Bool() Bool { return Bool{m} }
|
||||
func (m *Solo) Value() bool {
|
||||
a, b := intMin(m.d.InstrIndex, m.d.InstrIndex2), intMax(m.d.InstrIndex, m.d.InstrIndex2)
|
||||
a, b := min(m.d.InstrIndex, m.d.InstrIndex2), max(m.d.InstrIndex, m.d.InstrIndex2)
|
||||
for i := range m.d.Song.Patch {
|
||||
if i < 0 || i >= len(m.d.Song.Patch) {
|
||||
continue
|
||||
@ -240,7 +240,7 @@ func (m *Solo) Value() bool {
|
||||
}
|
||||
func (m *Solo) setValue(val bool) {
|
||||
defer (*Model)(m).change("Solo", PatchChange, MinorChange)()
|
||||
a, b := intMin(m.d.InstrIndex, m.d.InstrIndex2), intMax(m.d.InstrIndex, m.d.InstrIndex2)
|
||||
a, b := min(m.d.InstrIndex, m.d.InstrIndex2), max(m.d.InstrIndex, m.d.InstrIndex2)
|
||||
for i := range m.d.Song.Patch {
|
||||
if i < 0 || i >= len(m.d.Song.Patch) {
|
||||
continue
|
||||
|
@ -358,14 +358,14 @@ func between(a, b, c int) bool {
|
||||
return (a <= b && b <= c) || (c <= b && b <= a)
|
||||
}
|
||||
|
||||
func intMax(a, b int) int {
|
||||
func max(a, b int) int {
|
||||
if a > b {
|
||||
return a
|
||||
}
|
||||
return b
|
||||
}
|
||||
|
||||
func intMin(a, b int) int {
|
||||
func min(a, b int) int {
|
||||
if a < b {
|
||||
return a
|
||||
}
|
||||
|
@ -365,7 +365,7 @@ func (ie *InstrumentEditor) layoutUnitList(gtx C, t *Tracker) D {
|
||||
}
|
||||
units[i] = item
|
||||
}
|
||||
count := intMin(ie.unitDragList.TrackerList.Count(), 256)
|
||||
count := min(ie.unitDragList.TrackerList.Count(), 256)
|
||||
|
||||
element := func(gtx C, i int) D {
|
||||
gtx.Constraints.Max.Y = gtx.Dp(20)
|
||||
|
@ -227,7 +227,7 @@ func (te *NoteEditor) layoutTracks(gtx C, t *Tracker) D {
|
||||
}
|
||||
|
||||
rowTitle := func(gtx C, j int) D {
|
||||
rpp := intMax(t.RowsPerPattern().Value(), 1)
|
||||
rpp := max(t.RowsPerPattern().Value(), 1)
|
||||
pat := j / rpp
|
||||
row := j % rpp
|
||||
w := pxPatMarkWidth + pxRowMarkWidth
|
||||
@ -277,7 +277,7 @@ func (te *NoteEditor) layoutTracks(gtx C, t *Tracker) D {
|
||||
paint.FillShape(gtx.Ops, c, clip.Rect{Min: image.Pt(cx, 0), Max: image.Pt(cx+cw, gtx.Constraints.Min.Y)}.Op())
|
||||
}
|
||||
// draw the pattern marker
|
||||
rpp := intMax(t.RowsPerPattern().Value(), 1)
|
||||
rpp := max(t.RowsPerPattern().Value(), 1)
|
||||
pat := y / rpp
|
||||
row := y % rpp
|
||||
defer op.Offset(image.Pt(0, -2)).Push(gtx.Ops).Pop()
|
||||
|
@ -254,8 +254,8 @@ func (s *ScrollTable) command(gtx C, e key.Event) {
|
||||
stepX := 1
|
||||
stepY := 1
|
||||
if e.Modifiers.Contain(key.ModAlt) {
|
||||
stepX = intMax(s.ColTitleList.List.Position.Count-3, 8)
|
||||
stepY = intMax(s.RowTitleList.List.Position.Count-3, 8)
|
||||
stepX = max(s.ColTitleList.List.Position.Count-3, 8)
|
||||
stepY = max(s.RowTitleList.List.Position.Count-3, 8)
|
||||
} else if e.Modifiers.Contain(key.ModCtrl) {
|
||||
stepX = 1e6
|
||||
stepY = 1e6
|
||||
@ -277,9 +277,9 @@ func (s *ScrollTable) command(gtx C, e key.Event) {
|
||||
case key.NameRightArrow:
|
||||
s.Table.MoveCursor(stepX, 0)
|
||||
case key.NamePageUp:
|
||||
s.Table.MoveCursor(0, -intMax(s.RowTitleList.List.Position.Count-3, 8))
|
||||
s.Table.MoveCursor(0, -max(s.RowTitleList.List.Position.Count-3, 8))
|
||||
case key.NamePageDown:
|
||||
s.Table.MoveCursor(0, intMax(s.RowTitleList.List.Position.Count-3, 8))
|
||||
s.Table.MoveCursor(0, max(s.RowTitleList.List.Position.Count-3, 8))
|
||||
case key.NameHome:
|
||||
s.Table.SetCursorX(0)
|
||||
case key.NameEnd:
|
||||
|
@ -56,7 +56,7 @@ func (v Int) Set(value int) (ok bool) {
|
||||
}
|
||||
|
||||
func (r intRange) Clamp(value int) int {
|
||||
return intMax(intMin(value, r.Max), r.Min)
|
||||
return max(min(value, r.Max), r.Min)
|
||||
}
|
||||
|
||||
// Model methods
|
||||
@ -138,7 +138,7 @@ func (v *InstrumentVoices) Value() int {
|
||||
if v.d.InstrIndex < 0 || v.d.InstrIndex >= len(v.d.Song.Patch) {
|
||||
return 1
|
||||
}
|
||||
return intMax(v.d.Song.Patch[v.d.InstrIndex].NumVoices, 1)
|
||||
return max(v.d.Song.Patch[v.d.InstrIndex].NumVoices, 1)
|
||||
}
|
||||
|
||||
func (v *InstrumentVoices) setValue(value int) {
|
||||
@ -167,7 +167,7 @@ func (v *TrackVoices) Value() int {
|
||||
if t < 0 || t >= len(v.d.Song.Score.Tracks) {
|
||||
return 1
|
||||
}
|
||||
return intMax(v.d.Song.Score.Tracks[t].NumVoices, 1)
|
||||
return max(v.d.Song.Score.Tracks[t].NumVoices, 1)
|
||||
}
|
||||
|
||||
func (v *TrackVoices) setValue(value int) {
|
||||
|
@ -157,8 +157,8 @@ func (v List) PasteElements(data []byte) (ok bool) {
|
||||
}
|
||||
|
||||
func (v *List) listRange() (lower, higher int) {
|
||||
lower = intMin(v.Selected(), v.Selected2())
|
||||
higher = intMax(v.Selected(), v.Selected2())
|
||||
lower = min(v.Selected(), v.Selected2())
|
||||
higher = max(v.Selected(), v.Selected2())
|
||||
return
|
||||
}
|
||||
|
||||
@ -200,15 +200,15 @@ func (v *Instruments) FirstID(i int) (id int, ok bool) {
|
||||
}
|
||||
|
||||
func (v *Instruments) Selected() int {
|
||||
return intMax(intMin(v.d.InstrIndex, v.Count()-1), 0)
|
||||
return max(min(v.d.InstrIndex, v.Count()-1), 0)
|
||||
}
|
||||
|
||||
func (v *Instruments) Selected2() int {
|
||||
return intMax(intMin(v.d.InstrIndex2, v.Count()-1), 0)
|
||||
return max(min(v.d.InstrIndex2, v.Count()-1), 0)
|
||||
}
|
||||
|
||||
func (v *Instruments) SetSelected(value int) {
|
||||
v.d.InstrIndex = intMax(intMin(value, v.Count()-1), 0)
|
||||
v.d.InstrIndex = max(min(value, v.Count()-1), 0)
|
||||
v.d.UnitIndex = 0
|
||||
v.d.UnitIndex2 = 0
|
||||
v.d.UnitSearching = false
|
||||
@ -216,7 +216,7 @@ func (v *Instruments) SetSelected(value int) {
|
||||
}
|
||||
|
||||
func (v *Instruments) SetSelected2(value int) {
|
||||
v.d.InstrIndex2 = intMax(intMin(value, v.Count()-1), 0)
|
||||
v.d.InstrIndex2 = max(min(value, v.Count()-1), 0)
|
||||
}
|
||||
|
||||
func (v *Instruments) swap(i, j int) (ok bool) {
|
||||
@ -346,23 +346,23 @@ func (v *Units) Iterate(yield UnitYieldFunc) {
|
||||
}
|
||||
|
||||
func (v *Units) Selected() int {
|
||||
return intMax(intMin(v.d.UnitIndex, v.Count()-1), 0)
|
||||
return max(min(v.d.UnitIndex, v.Count()-1), 0)
|
||||
}
|
||||
|
||||
func (v *Units) Selected2() int {
|
||||
return intMax(intMin(v.d.UnitIndex2, v.Count()-1), 0)
|
||||
return max(min(v.d.UnitIndex2, v.Count()-1), 0)
|
||||
}
|
||||
|
||||
func (v *Units) SetSelected(value int) {
|
||||
m := (*Model)(v)
|
||||
m.d.UnitIndex = intMax(intMin(value, v.Count()-1), 0)
|
||||
m.d.UnitIndex = max(min(value, v.Count()-1), 0)
|
||||
m.d.ParamIndex = 0
|
||||
m.d.UnitSearching = false
|
||||
m.d.UnitSearchString = ""
|
||||
}
|
||||
|
||||
func (v *Units) SetSelected2(value int) {
|
||||
(*Model)(v).d.UnitIndex2 = intMax(intMin(value, v.Count()-1), 0)
|
||||
(*Model)(v).d.UnitIndex2 = max(min(value, v.Count()-1), 0)
|
||||
}
|
||||
|
||||
func (v *Units) Count() int {
|
||||
@ -453,19 +453,19 @@ func (v *Tracks) List() List {
|
||||
}
|
||||
|
||||
func (v *Tracks) Selected() int {
|
||||
return intMax(intMin(v.d.Cursor.Track, v.Count()-1), 0)
|
||||
return max(min(v.d.Cursor.Track, v.Count()-1), 0)
|
||||
}
|
||||
|
||||
func (v *Tracks) Selected2() int {
|
||||
return intMax(intMin(v.d.Cursor2.Track, v.Count()-1), 0)
|
||||
return max(min(v.d.Cursor2.Track, v.Count()-1), 0)
|
||||
}
|
||||
|
||||
func (v *Tracks) SetSelected(value int) {
|
||||
v.d.Cursor.Track = intMax(intMin(value, v.Count()-1), 0)
|
||||
v.d.Cursor.Track = max(min(value, v.Count()-1), 0)
|
||||
}
|
||||
|
||||
func (v *Tracks) SetSelected2(value int) {
|
||||
v.d.Cursor2.Track = intMax(intMin(value, v.Count()-1), 0)
|
||||
v.d.Cursor2.Track = max(min(value, v.Count()-1), 0)
|
||||
}
|
||||
|
||||
func (v *Tracks) swap(i, j int) (ok bool) {
|
||||
@ -540,18 +540,18 @@ func (v *OrderRows) List() List {
|
||||
|
||||
func (v *OrderRows) Selected() int {
|
||||
p := v.d.Cursor.OrderRow
|
||||
p = intMax(intMin(p, v.Count()-1), 0)
|
||||
p = max(min(p, v.Count()-1), 0)
|
||||
return p
|
||||
}
|
||||
|
||||
func (v *OrderRows) Selected2() int {
|
||||
p := v.d.Cursor2.OrderRow
|
||||
p = intMax(intMin(p, v.Count()-1), 0)
|
||||
p = max(min(p, v.Count()-1), 0)
|
||||
return p
|
||||
}
|
||||
|
||||
func (v *OrderRows) SetSelected(value int) {
|
||||
y := intMax(intMin(value, v.Count()-1), 0)
|
||||
y := max(min(value, v.Count()-1), 0)
|
||||
if y != v.d.Cursor.OrderRow {
|
||||
v.follow = false
|
||||
}
|
||||
@ -559,7 +559,7 @@ func (v *OrderRows) SetSelected(value int) {
|
||||
}
|
||||
|
||||
func (v *OrderRows) SetSelected2(value int) {
|
||||
v.d.Cursor2.OrderRow = intMax(intMin(value, v.Count()-1), 0)
|
||||
v.d.Cursor2.OrderRow = max(min(value, v.Count()-1), 0)
|
||||
}
|
||||
|
||||
func (v *OrderRows) swap(x, y int) (ok bool) {
|
||||
@ -758,15 +758,15 @@ func (l *SearchResults) Iterate(yield UnitSearchYieldFunc) {
|
||||
}
|
||||
|
||||
func (l *SearchResults) Selected() int {
|
||||
return intMax(intMin(l.d.UnitSearchIndex, l.Count()-1), 0)
|
||||
return max(min(l.d.UnitSearchIndex, l.Count()-1), 0)
|
||||
}
|
||||
|
||||
func (l *SearchResults) Selected2() int {
|
||||
return intMax(intMin(l.d.UnitSearchIndex, l.Count()-1), 0)
|
||||
return max(min(l.d.UnitSearchIndex, l.Count()-1), 0)
|
||||
}
|
||||
|
||||
func (l *SearchResults) SetSelected(value int) {
|
||||
l.d.UnitSearchIndex = intMax(intMin(value, l.Count()-1), 0)
|
||||
l.d.UnitSearchIndex = max(min(value, l.Count()-1), 0)
|
||||
}
|
||||
|
||||
func (l *SearchResults) SetSelected2(value int) {
|
||||
|
@ -559,17 +559,3 @@ func clamp(a, min, max int) int {
|
||||
}
|
||||
return a
|
||||
}
|
||||
|
||||
func intMax(a, b int) int {
|
||||
if a > b {
|
||||
return a
|
||||
}
|
||||
return b
|
||||
}
|
||||
|
||||
func intMin(a, b int) int {
|
||||
if a < b {
|
||||
return a
|
||||
}
|
||||
return b
|
||||
}
|
||||
|
@ -67,7 +67,7 @@ func (p parameter) change(kind string) func() {
|
||||
func (pl *Params) List() List { return List{pl} }
|
||||
func (pl *Params) Selected() int { return pl.d.ParamIndex }
|
||||
func (pl *Params) Selected2() int { return pl.Selected() }
|
||||
func (pl *Params) SetSelected(value int) { pl.d.ParamIndex = intMax(intMin(value, pl.Count()-1), 0) }
|
||||
func (pl *Params) SetSelected(value int) { pl.d.ParamIndex = max(min(value, pl.Count()-1), 0) }
|
||||
func (pl *Params) SetSelected2(value int) {}
|
||||
func (pl *Params) cancel() { (*Model)(pl).changeCancel = true }
|
||||
|
||||
|
@ -82,10 +82,10 @@ func (r *Rect) Limit(width, height int) {
|
||||
// Table methods
|
||||
|
||||
func (v Table) Range() (rect Rect) {
|
||||
rect.TopLeft.X = intMin(v.Cursor().X, v.Cursor2().X)
|
||||
rect.TopLeft.Y = intMin(v.Cursor().Y, v.Cursor2().Y)
|
||||
rect.BottomRight.X = intMax(v.Cursor().X, v.Cursor2().X)
|
||||
rect.BottomRight.Y = intMax(v.Cursor().Y, v.Cursor2().Y)
|
||||
rect.TopLeft.X = min(v.Cursor().X, v.Cursor2().X)
|
||||
rect.TopLeft.Y = min(v.Cursor().Y, v.Cursor2().Y)
|
||||
rect.BottomRight.X = max(v.Cursor().X, v.Cursor2().X)
|
||||
rect.BottomRight.Y = max(v.Cursor().Y, v.Cursor2().Y)
|
||||
return
|
||||
}
|
||||
|
||||
@ -154,20 +154,20 @@ func (v *Order) Table() Table {
|
||||
}
|
||||
|
||||
func (m *Order) Cursor() Point {
|
||||
t := intMax(intMin(m.d.Cursor.Track, len(m.d.Song.Score.Tracks)-1), 0)
|
||||
p := intMax(intMin(m.d.Cursor.OrderRow, m.d.Song.Score.Length-1), 0)
|
||||
t := max(min(m.d.Cursor.Track, len(m.d.Song.Score.Tracks)-1), 0)
|
||||
p := max(min(m.d.Cursor.OrderRow, m.d.Song.Score.Length-1), 0)
|
||||
return Point{t, p}
|
||||
}
|
||||
|
||||
func (m *Order) Cursor2() Point {
|
||||
t := intMax(intMin(m.d.Cursor2.Track, len(m.d.Song.Score.Tracks)-1), 0)
|
||||
p := intMax(intMin(m.d.Cursor2.OrderRow, m.d.Song.Score.Length-1), 0)
|
||||
t := max(min(m.d.Cursor2.Track, len(m.d.Song.Score.Tracks)-1), 0)
|
||||
p := max(min(m.d.Cursor2.OrderRow, m.d.Song.Score.Length-1), 0)
|
||||
return Point{t, p}
|
||||
}
|
||||
|
||||
func (m *Order) SetCursor(p Point) {
|
||||
m.d.Cursor.Track = intMax(intMin(p.X, len(m.d.Song.Score.Tracks)-1), 0)
|
||||
y := intMax(intMin(p.Y, m.d.Song.Score.Length-1), 0)
|
||||
m.d.Cursor.Track = max(min(p.X, len(m.d.Song.Score.Tracks)-1), 0)
|
||||
y := max(min(p.Y, m.d.Song.Score.Length-1), 0)
|
||||
if y != m.d.Cursor.OrderRow {
|
||||
m.follow = false
|
||||
}
|
||||
@ -176,8 +176,8 @@ func (m *Order) SetCursor(p Point) {
|
||||
}
|
||||
|
||||
func (m *Order) SetCursor2(p Point) {
|
||||
m.d.Cursor2.Track = intMax(intMin(p.X, len(m.d.Song.Score.Tracks)-1), 0)
|
||||
m.d.Cursor2.OrderRow = intMax(intMin(p.Y, m.d.Song.Score.Length-1), 0)
|
||||
m.d.Cursor2.Track = max(min(p.X, len(m.d.Song.Score.Tracks)-1), 0)
|
||||
m.d.Cursor2.OrderRow = max(min(p.Y, m.d.Song.Score.Length-1), 0)
|
||||
m.updateCursorRows()
|
||||
}
|
||||
|
||||
@ -404,19 +404,19 @@ func (v *Notes) Table() Table {
|
||||
}
|
||||
|
||||
func (m *Notes) Cursor() Point {
|
||||
t := intMax(intMin(m.d.Cursor.Track, len(m.d.Song.Score.Tracks)-1), 0)
|
||||
p := intMax(intMin(m.d.Song.Score.SongRow(m.d.Cursor.SongPos), m.d.Song.Score.LengthInRows()-1), 0)
|
||||
t := max(min(m.d.Cursor.Track, len(m.d.Song.Score.Tracks)-1), 0)
|
||||
p := max(min(m.d.Song.Score.SongRow(m.d.Cursor.SongPos), m.d.Song.Score.LengthInRows()-1), 0)
|
||||
return Point{t, p}
|
||||
}
|
||||
|
||||
func (m *Notes) Cursor2() Point {
|
||||
t := intMax(intMin(m.d.Cursor2.Track, len(m.d.Song.Score.Tracks)-1), 0)
|
||||
p := intMax(intMin(m.d.Song.Score.SongRow(m.d.Cursor2.SongPos), m.d.Song.Score.LengthInRows()-1), 0)
|
||||
t := max(min(m.d.Cursor2.Track, len(m.d.Song.Score.Tracks)-1), 0)
|
||||
p := max(min(m.d.Song.Score.SongRow(m.d.Cursor2.SongPos), m.d.Song.Score.LengthInRows()-1), 0)
|
||||
return Point{t, p}
|
||||
}
|
||||
|
||||
func (v *Notes) SetCursor(p Point) {
|
||||
v.d.Cursor.Track = intMax(intMin(p.X, len(v.d.Song.Score.Tracks)-1), 0)
|
||||
v.d.Cursor.Track = max(min(p.X, len(v.d.Song.Score.Tracks)-1), 0)
|
||||
newPos := v.d.Song.Score.Clamp(sointu.SongPos{PatternRow: p.Y})
|
||||
if newPos != v.d.Cursor.SongPos {
|
||||
v.follow = false
|
||||
@ -425,7 +425,7 @@ func (v *Notes) SetCursor(p Point) {
|
||||
}
|
||||
|
||||
func (v *Notes) SetCursor2(p Point) {
|
||||
v.d.Cursor2.Track = intMax(intMin(p.X, len(v.d.Song.Score.Tracks)-1), 0)
|
||||
v.d.Cursor2.Track = max(min(p.X, len(v.d.Song.Score.Tracks)-1), 0)
|
||||
v.d.Cursor2.SongPos = v.d.Song.Score.Clamp(sointu.SongPos{PatternRow: p.Y})
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user