2020-01-25 23:28:21 -05:00
|
|
|
package d2input
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sort"
|
|
|
|
)
|
|
|
|
|
2020-07-01 14:03:40 -04:00
|
|
|
// Priority of the event handler
|
|
|
|
type Priority int
|
|
|
|
|
|
|
|
//noinspection GoUnusedConst
|
|
|
|
const (
|
|
|
|
// PriorityLow is a low priority handler
|
|
|
|
PriorityLow Priority = iota
|
|
|
|
// PriorityDefault is a default priority handler
|
|
|
|
PriorityDefault
|
|
|
|
// PriorityHigh is a high priority handler
|
|
|
|
PriorityHigh
|
|
|
|
)
|
2020-01-25 23:28:21 -05:00
|
|
|
|
2020-07-01 14:03:40 -04:00
|
|
|
// Handler is an event handler
|
|
|
|
type Handler interface{}
|
2020-06-23 18:12:08 -04:00
|
|
|
|
2020-01-25 23:28:21 -05:00
|
|
|
type inputManager struct {
|
2020-06-23 18:12:08 -04:00
|
|
|
inputService InputService
|
|
|
|
cursorX int
|
|
|
|
cursorY int
|
2020-01-25 23:28:21 -05:00
|
|
|
|
|
|
|
buttonMod MouseButtonMod
|
|
|
|
keyMod KeyMod
|
|
|
|
|
|
|
|
entries handlerEntryList
|
|
|
|
}
|
|
|
|
|
2020-06-23 18:12:08 -04:00
|
|
|
func (im *inputManager) advance(_ float64) error {
|
|
|
|
cursorX, cursorY := im.inputService.CursorPosition()
|
2020-01-25 23:28:21 -05:00
|
|
|
|
|
|
|
im.keyMod = 0
|
2020-06-23 18:12:08 -04:00
|
|
|
if im.inputService.IsKeyPressed(KeyAlt) {
|
2020-01-25 23:28:21 -05:00
|
|
|
im.keyMod |= KeyModAlt
|
|
|
|
}
|
2020-06-23 18:12:08 -04:00
|
|
|
if im.inputService.IsKeyPressed(KeyControl) {
|
2020-01-25 23:28:21 -05:00
|
|
|
im.keyMod |= KeyModControl
|
|
|
|
}
|
2020-06-23 18:12:08 -04:00
|
|
|
if im.inputService.IsKeyPressed(KeyShift) {
|
2020-01-25 23:28:21 -05:00
|
|
|
im.keyMod |= KeyModShift
|
|
|
|
}
|
|
|
|
|
|
|
|
im.buttonMod = 0
|
2020-06-23 18:12:08 -04:00
|
|
|
if im.inputService.IsMouseButtonPressed(MouseButtonLeft) {
|
2020-01-25 23:28:21 -05:00
|
|
|
im.buttonMod |= MouseButtonModLeft
|
|
|
|
}
|
2020-06-23 18:12:08 -04:00
|
|
|
if im.inputService.IsMouseButtonPressed(MouseButtonMiddle) {
|
2020-01-25 23:28:21 -05:00
|
|
|
im.buttonMod |= MouseButtonModMiddle
|
|
|
|
}
|
2020-06-23 18:12:08 -04:00
|
|
|
if im.inputService.IsMouseButtonPressed(MouseButtonRight) {
|
2020-01-25 23:28:21 -05:00
|
|
|
im.buttonMod |= MouseButtonModRight
|
|
|
|
}
|
|
|
|
|
|
|
|
eventBase := HandlerEvent{
|
|
|
|
im.keyMod,
|
|
|
|
im.buttonMod,
|
|
|
|
cursorX,
|
|
|
|
cursorY,
|
|
|
|
}
|
|
|
|
|
2020-06-25 00:36:27 -04:00
|
|
|
for key := keyMin; key <= keyMax; key++ {
|
2020-06-23 18:12:08 -04:00
|
|
|
if im.inputService.IsKeyJustPressed(key) {
|
|
|
|
event := KeyEvent{HandlerEvent: eventBase, Key: key}
|
2020-01-25 23:28:21 -05:00
|
|
|
im.propagate(func(handler Handler) bool {
|
|
|
|
if l, ok := handler.(KeyDownHandler); ok {
|
|
|
|
return l.OnKeyDown(event)
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-06-23 18:12:08 -04:00
|
|
|
if im.inputService.IsKeyPressed(key) {
|
|
|
|
event := KeyEvent{HandlerEvent: eventBase, Key: key, Duration: im.inputService.KeyPressDuration(key)}
|
2020-01-25 23:28:21 -05:00
|
|
|
im.propagate(func(handler Handler) bool {
|
|
|
|
if l, ok := handler.(KeyRepeatHandler); ok {
|
|
|
|
return l.OnKeyRepeat(event)
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-06-23 18:12:08 -04:00
|
|
|
if im.inputService.IsKeyJustReleased(key) {
|
|
|
|
event := KeyEvent{HandlerEvent: eventBase, Key: key}
|
2020-01-25 23:28:21 -05:00
|
|
|
im.propagate(func(handler Handler) bool {
|
|
|
|
if l, ok := handler.(KeyUpHandler); ok {
|
|
|
|
return l.OnKeyUp(event)
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-23 18:12:08 -04:00
|
|
|
if chars := im.inputService.InputChars(); len(chars) > 0 {
|
2020-01-25 23:28:21 -05:00
|
|
|
event := KeyCharsEvent{eventBase, chars}
|
|
|
|
im.propagate(func(handler Handler) bool {
|
|
|
|
if l, ok := handler.(KeyCharsHandler); ok {
|
|
|
|
l.OnKeyChars(event)
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-06-25 00:36:27 -04:00
|
|
|
for button := mouseButtonMin; button <= mouseButtonMax; button++ {
|
2020-06-23 18:12:08 -04:00
|
|
|
if im.inputService.IsMouseButtonJustPressed(button) {
|
2020-07-01 14:03:40 -04:00
|
|
|
event := MouseEvent{eventBase, button}
|
2020-01-25 23:28:21 -05:00
|
|
|
im.propagate(func(handler Handler) bool {
|
|
|
|
if l, ok := handler.(MouseButtonDownHandler); ok {
|
|
|
|
return l.OnMouseButtonDown(event)
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-06-23 18:12:08 -04:00
|
|
|
if im.inputService.IsMouseButtonJustReleased(button) {
|
2020-07-01 14:03:40 -04:00
|
|
|
event := MouseEvent{eventBase, button}
|
2020-06-22 15:55:32 -04:00
|
|
|
im.propagate(func(handler Handler) bool {
|
2020-06-23 18:12:08 -04:00
|
|
|
if l, ok := handler.(MouseButtonUpHandler); ok {
|
|
|
|
return l.OnMouseButtonUp(event)
|
2020-06-22 15:55:32 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
})
|
|
|
|
}
|
2020-06-23 18:12:08 -04:00
|
|
|
if im.inputService.IsMouseButtonPressed(button) {
|
2020-07-01 14:03:40 -04:00
|
|
|
event := MouseEvent{eventBase, button}
|
2020-01-25 23:28:21 -05:00
|
|
|
im.propagate(func(handler Handler) bool {
|
2020-06-23 18:12:08 -04:00
|
|
|
if l, ok := handler.(MouseButtonRepeatHandler); ok {
|
|
|
|
return l.OnMouseButtonRepeat(event)
|
2020-01-25 23:28:21 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if im.cursorX != cursorX || im.cursorY != cursorY {
|
|
|
|
event := MouseMoveEvent{eventBase}
|
|
|
|
im.propagate(func(handler Handler) bool {
|
|
|
|
if l, ok := handler.(MouseMoveHandler); ok {
|
|
|
|
return l.OnMouseMove(event)
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
})
|
|
|
|
|
|
|
|
im.cursorX, im.cursorY = cursorX, cursorY
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (im *inputManager) bindHandler(handler Handler, priority Priority) error {
|
|
|
|
for _, entry := range im.entries {
|
|
|
|
if entry.handler == handler {
|
|
|
|
return ErrHasReg
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
im.entries = append(im.entries, handlerEntry{handler, priority})
|
|
|
|
sort.Sort(im.entries)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (im *inputManager) unbindHandler(handler Handler) error {
|
|
|
|
for i, entry := range im.entries {
|
|
|
|
if entry.handler == handler {
|
|
|
|
copy(im.entries[i:], im.entries[i+1:])
|
|
|
|
im.entries = im.entries[:len(im.entries)-1]
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ErrNotReg
|
|
|
|
}
|
|
|
|
|
|
|
|
func (im *inputManager) propagate(callback func(Handler) bool) {
|
|
|
|
var priority Priority
|
|
|
|
var handled bool
|
|
|
|
|
|
|
|
for _, entry := range im.entries {
|
|
|
|
if priority > entry.priority && handled {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
if callback(entry.handler) {
|
|
|
|
handled = true
|
|
|
|
}
|
|
|
|
|
|
|
|
priority = entry.priority
|
|
|
|
}
|
|
|
|
}
|
2020-07-01 14:03:40 -04:00
|
|
|
|
|
|
|
type handlerEntry struct {
|
|
|
|
handler Handler
|
|
|
|
priority Priority
|
|
|
|
}
|
|
|
|
|
|
|
|
type handlerEntryList []handlerEntry
|
|
|
|
|
|
|
|
func (lel handlerEntryList) Len() int {
|
|
|
|
return len(lel)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (lel handlerEntryList) Swap(i, j int) {
|
|
|
|
lel[i], lel[j] = lel[j], lel[i]
|
|
|
|
}
|
|
|
|
|
|
|
|
func (lel handlerEntryList) Less(i, j int) bool {
|
|
|
|
return lel[i].priority > lel[j].priority
|
|
|
|
}
|