mirror of
https://github.com/vsariola/sointu.git
synced 2025-07-27 09:24:48 -04:00
feat: UI work to facilitate future improvements in midi-into-track-input
This commit is contained in:
42
tracker/optional_int.go
Normal file
42
tracker/optional_int.go
Normal file
@ -0,0 +1,42 @@
|
||||
package tracker
|
||||
|
||||
type (
|
||||
// OptionalInt tries to follow the same convention as e.g. Int{...} or Bool{...}
|
||||
// Do not confuse with types.OptionalInteger, which you might use as a model,
|
||||
// but don't necessarily have to.
|
||||
OptionalInt struct {
|
||||
optionalIntData
|
||||
}
|
||||
|
||||
optionalIntData interface {
|
||||
Unpack() (int, bool)
|
||||
Value() int
|
||||
Range() intRange
|
||||
|
||||
setValue(int)
|
||||
unsetValue()
|
||||
change(kind string) func()
|
||||
}
|
||||
|
||||
TrackMidiVelIn Model
|
||||
)
|
||||
|
||||
func (v OptionalInt) Set(value int, present bool) (ok bool) {
|
||||
if !present {
|
||||
v.unsetValue()
|
||||
return true
|
||||
}
|
||||
// TODO: can we deduplicate this by referencing Int{...}.Set(value) ?
|
||||
r := v.Range()
|
||||
if v.Equals(value, present) || value < r.Min || value > r.Max {
|
||||
return false
|
||||
}
|
||||
defer v.change("Set")()
|
||||
v.setValue(value)
|
||||
return true
|
||||
}
|
||||
|
||||
func (v OptionalInt) Equals(value int, present bool) bool {
|
||||
oldValue, oldPresent := v.Unpack()
|
||||
return value == oldValue && present == oldPresent
|
||||
}
|
Reference in New Issue
Block a user