2023-02-08 19:05:36 -07:00
|
|
|
package fun
|
|
|
|
|
|
|
|
import "image"
|
|
|
|
import "git.tebibyte.media/sashakoshka/tomo/input"
|
|
|
|
import "git.tebibyte.media/sashakoshka/tomo/theme"
|
|
|
|
import "git.tebibyte.media/sashakoshka/tomo/config"
|
|
|
|
import "git.tebibyte.media/sashakoshka/tomo/artist"
|
|
|
|
import "git.tebibyte.media/sashakoshka/tomo/elements/core"
|
2023-02-08 21:41:31 -07:00
|
|
|
import "git.tebibyte.media/sashakoshka/tomo/elements/fun/music"
|
2023-02-08 19:05:36 -07:00
|
|
|
|
|
|
|
const pianoKeyWidth = 18
|
|
|
|
|
|
|
|
type pianoKey struct {
|
|
|
|
image.Rectangle
|
2023-02-08 21:41:31 -07:00
|
|
|
music.Note
|
2023-02-08 19:05:36 -07:00
|
|
|
}
|
|
|
|
|
2023-02-09 12:50:24 -07:00
|
|
|
// Piano is an element that can be used to input midi notes.
|
2023-02-08 19:05:36 -07:00
|
|
|
type Piano struct {
|
|
|
|
*core.Core
|
2023-02-09 14:15:02 -07:00
|
|
|
*core.FocusableCore
|
2023-02-08 19:05:36 -07:00
|
|
|
core core.CoreControl
|
2023-02-09 14:15:02 -07:00
|
|
|
focusableControl core.FocusableCoreControl
|
2023-02-08 21:41:31 -07:00
|
|
|
low, high music.Octave
|
2023-02-08 19:05:36 -07:00
|
|
|
|
|
|
|
config config.Wrapped
|
|
|
|
theme theme.Wrapped
|
|
|
|
|
|
|
|
flatKeys []pianoKey
|
|
|
|
sharpKeys []pianoKey
|
2023-02-14 15:05:13 -07:00
|
|
|
contentBounds image.Rectangle
|
2023-02-08 19:05:36 -07:00
|
|
|
|
|
|
|
pressed *pianoKey
|
2023-02-09 14:15:02 -07:00
|
|
|
keynavPressed map[music.Note] bool
|
2023-02-08 19:05:36 -07:00
|
|
|
|
2023-02-08 21:41:31 -07:00
|
|
|
onPress func (music.Note)
|
|
|
|
onRelease func (music.Note)
|
2023-02-08 19:05:36 -07:00
|
|
|
}
|
|
|
|
|
2023-02-09 12:50:24 -07:00
|
|
|
// NewPiano returns a new piano element with a lowest and highest octave,
|
|
|
|
// inclusive. If low is greater than high, they will be swapped.
|
2023-02-08 21:41:31 -07:00
|
|
|
func NewPiano (low, high music.Octave) (element *Piano) {
|
2023-02-09 12:50:24 -07:00
|
|
|
if low > high {
|
|
|
|
temp := low
|
|
|
|
low = high
|
|
|
|
high = temp
|
|
|
|
}
|
|
|
|
|
2023-02-08 19:05:36 -07:00
|
|
|
element = &Piano {
|
|
|
|
low: low,
|
|
|
|
high: high,
|
2023-02-09 14:15:02 -07:00
|
|
|
keynavPressed: make(map[music.Note] bool),
|
2023-02-08 19:05:36 -07:00
|
|
|
}
|
2023-02-09 14:15:02 -07:00
|
|
|
|
2023-02-08 19:05:36 -07:00
|
|
|
element.theme.Case = theme.C("fun", "piano")
|
2023-03-14 23:42:07 -06:00
|
|
|
element.Core, element.core = core.NewCore (element, func () {
|
2023-02-08 19:05:36 -07:00
|
|
|
element.recalculate()
|
|
|
|
element.draw()
|
|
|
|
})
|
2023-02-09 14:15:02 -07:00
|
|
|
element.FocusableCore,
|
2023-03-14 23:42:07 -06:00
|
|
|
element.focusableControl = core.NewFocusableCore(element.core, element.redo)
|
2023-02-08 19:05:36 -07:00
|
|
|
element.updateMinimumSize()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// OnPress sets a function to be called when a key is pressed.
|
2023-02-08 21:41:31 -07:00
|
|
|
func (element *Piano) OnPress (callback func (note music.Note)) {
|
2023-02-08 19:05:36 -07:00
|
|
|
element.onPress = callback
|
|
|
|
}
|
|
|
|
|
|
|
|
// OnRelease sets a function to be called when a key is released.
|
2023-02-08 21:41:31 -07:00
|
|
|
func (element *Piano) OnRelease (callback func (note music.Note)) {
|
2023-02-08 19:05:36 -07:00
|
|
|
element.onRelease = callback
|
|
|
|
}
|
|
|
|
|
|
|
|
func (element *Piano) HandleMouseDown (x, y int, button input.Button) {
|
2023-02-09 14:15:02 -07:00
|
|
|
element.Focus()
|
2023-02-08 19:05:36 -07:00
|
|
|
if button != input.ButtonLeft { return }
|
|
|
|
element.pressUnderMouseCursor(image.Pt(x, y))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (element *Piano) HandleMouseUp (x, y int, button input.Button) {
|
|
|
|
if button != input.ButtonLeft { return }
|
2023-02-09 14:15:02 -07:00
|
|
|
if element.onRelease != nil && element.pressed != nil {
|
2023-02-08 19:05:36 -07:00
|
|
|
element.onRelease((*element.pressed).Note)
|
|
|
|
}
|
|
|
|
element.pressed = nil
|
|
|
|
element.redo()
|
|
|
|
}
|
|
|
|
|
2023-03-15 22:25:36 -06:00
|
|
|
func (element *Piano) HandleMotion (x, y int) {
|
2023-02-08 19:05:36 -07:00
|
|
|
if element.pressed == nil { return }
|
|
|
|
element.pressUnderMouseCursor(image.Pt(x, y))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (element *Piano) pressUnderMouseCursor (point image.Point) {
|
|
|
|
// find out which note is being pressed
|
|
|
|
newKey := (*pianoKey)(nil)
|
|
|
|
for index, key := range element.flatKeys {
|
|
|
|
if point.In(key.Rectangle) {
|
|
|
|
newKey = &element.flatKeys[index]
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for index, key := range element.sharpKeys {
|
|
|
|
if point.In(key.Rectangle) {
|
|
|
|
newKey = &element.sharpKeys[index]
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if newKey == nil { return }
|
|
|
|
|
|
|
|
if newKey != element.pressed {
|
2023-02-08 21:41:31 -07:00
|
|
|
// release previous note
|
|
|
|
if element.pressed != nil && element.onRelease != nil {
|
|
|
|
element.onRelease((*element.pressed).Note)
|
|
|
|
}
|
|
|
|
|
2023-02-08 19:05:36 -07:00
|
|
|
// press new note
|
|
|
|
element.pressed = newKey
|
|
|
|
if element.onPress != nil {
|
|
|
|
element.onPress((*element.pressed).Note)
|
|
|
|
}
|
|
|
|
element.redo()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-09 14:15:02 -07:00
|
|
|
var noteForKey = map[input.Key] music.Note {
|
2023-02-09 14:36:38 -07:00
|
|
|
'a': 46,
|
|
|
|
'z': 47,
|
|
|
|
|
|
|
|
'x': 48,
|
|
|
|
'd': 49,
|
|
|
|
'c': 50,
|
|
|
|
'f': 51,
|
|
|
|
'v': 52,
|
|
|
|
'b': 53,
|
|
|
|
'h': 54,
|
|
|
|
'n': 55,
|
|
|
|
'j': 56,
|
|
|
|
'm': 57,
|
|
|
|
'k': 58,
|
|
|
|
',': 59,
|
|
|
|
'.': 60,
|
|
|
|
';': 61,
|
|
|
|
'/': 62,
|
2023-02-09 15:26:36 -07:00
|
|
|
'\'': 63,
|
2023-02-09 14:36:38 -07:00
|
|
|
|
|
|
|
'1': 56,
|
|
|
|
'q': 57,
|
|
|
|
'2': 58,
|
|
|
|
'w': 59,
|
|
|
|
|
|
|
|
'e': 60,
|
|
|
|
'4': 61,
|
|
|
|
'r': 62,
|
|
|
|
'5': 63,
|
|
|
|
't': 64,
|
|
|
|
'y': 65,
|
|
|
|
'7': 66,
|
|
|
|
'u': 67,
|
|
|
|
'8': 68,
|
|
|
|
'i': 69,
|
|
|
|
'9': 70,
|
|
|
|
'o': 71,
|
|
|
|
|
|
|
|
'p': 72,
|
|
|
|
'-': 73,
|
|
|
|
'[': 74,
|
|
|
|
'=': 75,
|
|
|
|
']': 76,
|
|
|
|
'\\': 77,
|
2023-02-09 14:15:02 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func (element *Piano) HandleKeyDown (key input.Key, modifiers input.Modifiers) {
|
|
|
|
if !element.Enabled() { return }
|
|
|
|
note, exists := noteForKey[key]
|
|
|
|
if !exists { return }
|
|
|
|
if !element.keynavPressed[note] {
|
|
|
|
element.keynavPressed[note] = true
|
|
|
|
if element.onPress != nil {
|
|
|
|
element.onPress(note)
|
|
|
|
}
|
|
|
|
element.redo()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (element *Piano) HandleKeyUp (key input.Key, modifiers input.Modifiers) {
|
|
|
|
note, exists := noteForKey[key]
|
|
|
|
if !exists { return }
|
|
|
|
_, pressed := element.keynavPressed[note]
|
|
|
|
if !pressed { return }
|
|
|
|
delete(element.keynavPressed, note)
|
|
|
|
if element.onRelease != nil {
|
|
|
|
element.onRelease(note)
|
|
|
|
}
|
|
|
|
element.redo()
|
|
|
|
}
|
|
|
|
|
2023-02-08 19:05:36 -07:00
|
|
|
// SetTheme sets the element's theme.
|
|
|
|
func (element *Piano) SetTheme (new theme.Theme) {
|
|
|
|
if new == element.theme.Theme { return }
|
|
|
|
element.theme.Theme = new
|
|
|
|
element.updateMinimumSize()
|
|
|
|
element.recalculate()
|
|
|
|
element.redo()
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetConfig sets the element's configuration.
|
|
|
|
func (element *Piano) SetConfig (new config.Config) {
|
|
|
|
if new == element.config.Config { return }
|
|
|
|
element.config.Config = new
|
|
|
|
element.updateMinimumSize()
|
|
|
|
element.recalculate()
|
|
|
|
element.redo()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (element *Piano) updateMinimumSize () {
|
2023-02-27 22:17:05 -07:00
|
|
|
padding := element.theme.Padding(theme.PatternPinboard)
|
2023-02-08 19:05:36 -07:00
|
|
|
element.core.SetMinimumSize (
|
2023-02-26 20:20:17 -07:00
|
|
|
pianoKeyWidth * 7 * element.countOctaves() +
|
2023-02-27 22:17:05 -07:00
|
|
|
padding.Horizontal(),
|
|
|
|
64 + padding.Vertical())
|
2023-02-08 19:05:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func (element *Piano) countOctaves () int {
|
|
|
|
return int(element.high - element.low + 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (element *Piano) countFlats () int {
|
|
|
|
return element.countOctaves() * 8
|
|
|
|
}
|
|
|
|
|
|
|
|
func (element *Piano) countSharps () int {
|
|
|
|
return element.countOctaves() * 5
|
|
|
|
}
|
|
|
|
|
|
|
|
func (element *Piano) redo () {
|
|
|
|
if element.core.HasImage() {
|
|
|
|
element.draw()
|
|
|
|
element.core.DamageAll()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (element *Piano) recalculate () {
|
|
|
|
element.flatKeys = make([]pianoKey, element.countFlats())
|
|
|
|
element.sharpKeys = make([]pianoKey, element.countSharps())
|
|
|
|
|
2023-02-26 20:20:17 -07:00
|
|
|
padding := element.theme.Padding(theme.PatternPinboard)
|
|
|
|
bounds := padding.Apply(element.Bounds())
|
2023-02-09 09:38:01 -07:00
|
|
|
|
2023-02-08 19:05:36 -07:00
|
|
|
dot := bounds.Min
|
|
|
|
note := element.low.Note(0)
|
|
|
|
limit := element.high.Note(12)
|
|
|
|
flatIndex := 0
|
|
|
|
sharpIndex := 0
|
|
|
|
for note < limit {
|
|
|
|
if note.IsSharp() {
|
|
|
|
element.sharpKeys[sharpIndex].Rectangle = image.Rect (
|
|
|
|
-(pianoKeyWidth * 3) / 7, 0,
|
|
|
|
(pianoKeyWidth * 3) / 7,
|
2023-02-08 21:41:31 -07:00
|
|
|
(bounds.Dy() * 5) / 8).Add(dot)
|
2023-02-08 19:05:36 -07:00
|
|
|
element.sharpKeys[sharpIndex].Note = note
|
|
|
|
sharpIndex ++
|
|
|
|
} else {
|
|
|
|
element.flatKeys[flatIndex].Rectangle = image.Rect (
|
|
|
|
0, 0, pianoKeyWidth, bounds.Dy()).Add(dot)
|
|
|
|
dot.X += pianoKeyWidth
|
|
|
|
element.flatKeys[flatIndex].Note = note
|
|
|
|
flatIndex ++
|
|
|
|
}
|
|
|
|
note ++
|
|
|
|
}
|
2023-02-14 15:05:13 -07:00
|
|
|
|
|
|
|
element.contentBounds = image.Rectangle {
|
|
|
|
bounds.Min,
|
|
|
|
image.Pt(dot.X, bounds.Max.Y),
|
|
|
|
}
|
2023-02-08 19:05:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func (element *Piano) draw () {
|
2023-02-26 20:20:17 -07:00
|
|
|
state := theme.State {
|
2023-02-09 14:15:02 -07:00
|
|
|
Focused: element.Focused(),
|
|
|
|
Disabled: !element.Enabled(),
|
|
|
|
}
|
2023-02-09 09:38:01 -07:00
|
|
|
|
2023-02-08 19:05:36 -07:00
|
|
|
for _, key := range element.flatKeys {
|
2023-02-09 14:15:02 -07:00
|
|
|
_, keynavPressed := element.keynavPressed[key.Note]
|
2023-02-08 19:05:36 -07:00
|
|
|
element.drawFlat (
|
|
|
|
key.Rectangle,
|
|
|
|
element.pressed != nil &&
|
2023-02-09 14:15:02 -07:00
|
|
|
(*element.pressed).Note == key.Note || keynavPressed,
|
|
|
|
state)
|
2023-02-08 19:05:36 -07:00
|
|
|
}
|
|
|
|
for _, key := range element.sharpKeys {
|
2023-02-09 14:15:02 -07:00
|
|
|
_, keynavPressed := element.keynavPressed[key.Note]
|
2023-02-08 19:05:36 -07:00
|
|
|
element.drawSharp (
|
|
|
|
key.Rectangle,
|
|
|
|
element.pressed != nil &&
|
2023-02-09 14:15:02 -07:00
|
|
|
(*element.pressed).Note == key.Note || keynavPressed,
|
|
|
|
state)
|
2023-02-08 19:05:36 -07:00
|
|
|
}
|
2023-02-14 15:05:13 -07:00
|
|
|
|
|
|
|
pattern := element.theme.Pattern(theme.PatternPinboard, state)
|
2023-02-26 20:20:17 -07:00
|
|
|
artist.DrawShatter (
|
2023-03-11 23:47:58 -07:00
|
|
|
element.core, pattern, element.Bounds(), element.contentBounds)
|
2023-02-08 19:05:36 -07:00
|
|
|
}
|
|
|
|
|
2023-02-09 14:15:02 -07:00
|
|
|
func (element *Piano) drawFlat (
|
|
|
|
bounds image.Rectangle,
|
|
|
|
pressed bool,
|
2023-02-26 20:20:17 -07:00
|
|
|
state theme.State,
|
2023-02-09 14:15:02 -07:00
|
|
|
) {
|
|
|
|
state.Pressed = pressed
|
2023-02-09 09:38:01 -07:00
|
|
|
pattern := element.theme.Theme.Pattern (
|
2023-02-12 08:58:23 -07:00
|
|
|
theme.PatternButton, state, theme.C("fun", "flatKey"))
|
2023-03-11 23:15:36 -07:00
|
|
|
pattern.Draw(element.core, bounds)
|
2023-02-08 19:05:36 -07:00
|
|
|
}
|
|
|
|
|
2023-02-09 14:15:02 -07:00
|
|
|
func (element *Piano) drawSharp (
|
|
|
|
bounds image.Rectangle,
|
|
|
|
pressed bool,
|
2023-02-26 20:20:17 -07:00
|
|
|
state theme.State,
|
2023-02-09 14:15:02 -07:00
|
|
|
) {
|
|
|
|
state.Pressed = pressed
|
2023-02-09 09:38:01 -07:00
|
|
|
pattern := element.theme.Theme.Pattern (
|
2023-02-12 08:58:23 -07:00
|
|
|
theme.PatternButton, state, theme.C("fun", "sharpKey"))
|
2023-03-11 23:15:36 -07:00
|
|
|
pattern.Draw(element.core, bounds)
|
2023-02-08 19:05:36 -07:00
|
|
|
}
|