2020-01-25 23:28:21 -05:00
|
|
|
package d2input
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sort"
|
|
|
|
|
2020-07-03 15:09:16 -04:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2interface"
|
2020-07-01 14:03:40 -04:00
|
|
|
)
|
2020-01-25 23:28:21 -05:00
|
|
|
|
|
|
|
type inputManager struct {
|
2020-07-03 15:09:16 -04:00
|
|
|
inputService d2interface.InputService
|
2020-06-23 18:12:08 -04:00
|
|
|
cursorX int
|
|
|
|
cursorY int
|
2020-01-25 23:28:21 -05:00
|
|
|
|
2020-07-03 15:09:16 -04:00
|
|
|
buttonMod d2interface.MouseButtonMod
|
|
|
|
keyMod d2interface.KeyMod
|
2020-01-25 23:28:21 -05:00
|
|
|
|
|
|
|
entries handlerEntryList
|
|
|
|
}
|
|
|
|
|
2020-07-03 15:09:16 -04:00
|
|
|
// Advance advances the inputManager
|
|
|
|
func (im *inputManager) Advance(_, _ float64) error {
|
|
|
|
im.updateKeyMod()
|
|
|
|
im.updateButtonMod()
|
|
|
|
|
2020-06-23 18:12:08 -04:00
|
|
|
cursorX, cursorY := im.inputService.CursorPosition()
|
2020-07-03 15:09:16 -04:00
|
|
|
eventBase := HandlerEvent{
|
|
|
|
im.keyMod,
|
|
|
|
im.buttonMod,
|
|
|
|
cursorX,
|
|
|
|
cursorY,
|
|
|
|
}
|
2020-01-25 23:28:21 -05:00
|
|
|
|
2020-07-03 15:09:16 -04:00
|
|
|
for key := d2interface.KeyMin; key <= d2interface.KeyMax; key++ {
|
|
|
|
im.updateJustPressedKey(key, eventBase)
|
|
|
|
im.updateJustReleasedKey(key, eventBase)
|
|
|
|
im.updatePressedKey(key, eventBase)
|
|
|
|
}
|
|
|
|
|
|
|
|
im.updateInputChars(eventBase)
|
|
|
|
|
|
|
|
for button := d2interface.MouseButtonMin; button <= d2interface.MouseButtonMax; button++ {
|
|
|
|
im.updateJustPressedButton(button, eventBase)
|
|
|
|
im.updateJustReleasedButton(button, eventBase)
|
|
|
|
im.updatePressedButton(button, eventBase)
|
|
|
|
}
|
|
|
|
|
|
|
|
im.updateCursor(cursorX, cursorY, eventBase)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (im *inputManager) updateKeyMod() {
|
2020-01-25 23:28:21 -05:00
|
|
|
im.keyMod = 0
|
2020-07-03 15:09:16 -04:00
|
|
|
if im.inputService.IsKeyPressed(d2interface.KeyAlt) {
|
|
|
|
im.keyMod |= d2interface.KeyModAlt
|
2020-01-25 23:28:21 -05:00
|
|
|
}
|
2020-07-03 15:09:16 -04:00
|
|
|
|
|
|
|
if im.inputService.IsKeyPressed(d2interface.KeyControl) {
|
|
|
|
im.keyMod |= d2interface.KeyModControl
|
2020-01-25 23:28:21 -05:00
|
|
|
}
|
2020-07-03 15:09:16 -04:00
|
|
|
|
|
|
|
if im.inputService.IsKeyPressed(d2interface.KeyShift) {
|
|
|
|
im.keyMod |= d2interface.KeyModShift
|
2020-01-25 23:28:21 -05:00
|
|
|
}
|
2020-07-03 15:09:16 -04:00
|
|
|
}
|
2020-01-25 23:28:21 -05:00
|
|
|
|
2020-07-03 15:09:16 -04:00
|
|
|
func (im *inputManager) updateButtonMod() {
|
2020-01-25 23:28:21 -05:00
|
|
|
im.buttonMod = 0
|
2020-07-03 15:09:16 -04:00
|
|
|
if im.inputService.IsMouseButtonPressed(d2interface.MouseButtonLeft) {
|
|
|
|
im.buttonMod |= d2interface.MouseButtonModLeft
|
2020-01-25 23:28:21 -05:00
|
|
|
}
|
2020-07-03 15:09:16 -04:00
|
|
|
|
|
|
|
if im.inputService.IsMouseButtonPressed(d2interface.MouseButtonMiddle) {
|
|
|
|
im.buttonMod |= d2interface.MouseButtonModMiddle
|
2020-01-25 23:28:21 -05:00
|
|
|
}
|
|
|
|
|
2020-07-03 15:09:16 -04:00
|
|
|
if im.inputService.IsMouseButtonPressed(d2interface.MouseButtonRight) {
|
|
|
|
im.buttonMod |= d2interface.MouseButtonModRight
|
2020-01-25 23:28:21 -05:00
|
|
|
}
|
2020-07-03 15:09:16 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (im *inputManager) updateJustPressedKey(k d2interface.Key, e HandlerEvent) {
|
|
|
|
if im.inputService.IsKeyJustPressed(k) {
|
|
|
|
event := KeyEvent{HandlerEvent: e, key: k}
|
2020-01-25 23:28:21 -05:00
|
|
|
|
2020-07-03 15:09:16 -04:00
|
|
|
fn := func(handler d2interface.InputEventHandler) bool {
|
|
|
|
if l, ok := handler.(d2interface.KeyDownHandler); ok {
|
|
|
|
return l.OnKeyDown(&event)
|
|
|
|
}
|
2020-01-25 23:28:21 -05:00
|
|
|
|
2020-07-03 15:09:16 -04:00
|
|
|
return false
|
2020-01-25 23:28:21 -05:00
|
|
|
}
|
|
|
|
|
2020-07-03 15:09:16 -04:00
|
|
|
im.propagate(fn)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (im *inputManager) updateJustReleasedKey(k d2interface.Key, e HandlerEvent) {
|
|
|
|
if im.inputService.IsKeyJustReleased(k) {
|
|
|
|
event := KeyEvent{HandlerEvent: e, key: k}
|
|
|
|
|
|
|
|
fn := func(handler d2interface.InputEventHandler) bool {
|
|
|
|
if l, ok := handler.(d2interface.KeyUpHandler); ok {
|
|
|
|
return l.OnKeyUp(&event)
|
|
|
|
}
|
2020-01-25 23:28:21 -05:00
|
|
|
|
2020-07-03 15:09:16 -04:00
|
|
|
return false
|
2020-01-25 23:28:21 -05:00
|
|
|
}
|
2020-07-03 15:09:16 -04:00
|
|
|
im.propagate(fn)
|
|
|
|
}
|
|
|
|
}
|
2020-01-25 23:28:21 -05:00
|
|
|
|
2020-07-03 15:09:16 -04:00
|
|
|
func (im *inputManager) updatePressedKey(k d2interface.Key, e HandlerEvent) {
|
|
|
|
if im.inputService.IsKeyPressed(k) {
|
|
|
|
event := KeyEvent{
|
|
|
|
HandlerEvent: e,
|
|
|
|
key: k,
|
|
|
|
duration: im.inputService.KeyPressDuration(k),
|
|
|
|
}
|
2020-01-25 23:28:21 -05:00
|
|
|
|
2020-07-03 15:09:16 -04:00
|
|
|
fn := func(handler d2interface.InputEventHandler) bool {
|
|
|
|
if l, ok := handler.(d2interface.KeyRepeatHandler); ok {
|
|
|
|
return l.OnKeyRepeat(&event)
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
2020-01-25 23:28:21 -05:00
|
|
|
}
|
2020-07-03 15:09:16 -04:00
|
|
|
im.propagate(fn)
|
2020-01-25 23:28:21 -05:00
|
|
|
}
|
2020-07-03 15:09:16 -04:00
|
|
|
}
|
2020-01-25 23:28:21 -05:00
|
|
|
|
2020-07-03 15:09:16 -04:00
|
|
|
func (im *inputManager) updateInputChars(eventBase HandlerEvent) {
|
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}
|
2020-07-03 15:09:16 -04:00
|
|
|
|
|
|
|
fn := func(handler d2interface.InputEventHandler) bool {
|
|
|
|
if l, ok := handler.(d2interface.KeyCharsHandler); ok {
|
|
|
|
l.OnKeyChars(&event)
|
2020-01-25 23:28:21 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
2020-07-03 15:09:16 -04:00
|
|
|
}
|
|
|
|
im.propagate(fn)
|
2020-01-25 23:28:21 -05:00
|
|
|
}
|
2020-07-03 15:09:16 -04:00
|
|
|
}
|
2020-01-25 23:28:21 -05:00
|
|
|
|
2020-07-03 15:09:16 -04:00
|
|
|
func (im *inputManager) updateJustPressedButton(b d2interface.MouseButton, e HandlerEvent) {
|
|
|
|
if im.inputService.IsMouseButtonJustPressed(b) {
|
|
|
|
event := MouseEvent{e, b}
|
2020-01-25 23:28:21 -05:00
|
|
|
|
2020-07-03 15:09:16 -04:00
|
|
|
fn := func(handler d2interface.InputEventHandler) bool {
|
|
|
|
if l, ok := handler.(d2interface.MouseButtonDownHandler); ok {
|
|
|
|
return l.OnMouseButtonDown(&event)
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
2020-01-25 23:28:21 -05:00
|
|
|
}
|
2020-07-03 15:09:16 -04:00
|
|
|
im.propagate(fn)
|
|
|
|
}
|
|
|
|
}
|
2020-01-25 23:28:21 -05:00
|
|
|
|
2020-07-03 15:09:16 -04:00
|
|
|
func (im *inputManager) updateJustReleasedButton(b d2interface.MouseButton, e HandlerEvent) {
|
|
|
|
if im.inputService.IsMouseButtonJustReleased(b) {
|
|
|
|
event := MouseEvent{e, b}
|
|
|
|
|
|
|
|
fn := func(handler d2interface.InputEventHandler) bool {
|
|
|
|
if l, ok := handler.(d2interface.MouseButtonUpHandler); ok {
|
|
|
|
return l.OnMouseButtonUp(&event)
|
|
|
|
}
|
2020-06-22 15:55:32 -04:00
|
|
|
|
2020-07-03 15:09:16 -04:00
|
|
|
return false
|
2020-06-22 15:55:32 -04:00
|
|
|
}
|
2020-07-03 15:09:16 -04:00
|
|
|
im.propagate(fn)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (im *inputManager) updatePressedButton(b d2interface.MouseButton, e HandlerEvent) {
|
|
|
|
if im.inputService.IsMouseButtonPressed(b) {
|
|
|
|
event := MouseEvent{e, b}
|
|
|
|
|
|
|
|
fn := func(handler d2interface.InputEventHandler) bool {
|
|
|
|
if l, ok := handler.(d2interface.MouseButtonRepeatHandler); ok {
|
|
|
|
return l.OnMouseButtonRepeat(&event)
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
2020-01-25 23:28:21 -05:00
|
|
|
}
|
2020-07-03 15:09:16 -04:00
|
|
|
im.propagate(fn)
|
2020-01-25 23:28:21 -05:00
|
|
|
}
|
2020-07-03 15:09:16 -04:00
|
|
|
}
|
2020-01-25 23:28:21 -05:00
|
|
|
|
2020-07-03 15:09:16 -04:00
|
|
|
func (im *inputManager) updateCursor(cursorX, cursorY int, e HandlerEvent) {
|
2020-01-25 23:28:21 -05:00
|
|
|
if im.cursorX != cursorX || im.cursorY != cursorY {
|
2020-07-03 15:09:16 -04:00
|
|
|
event := MouseMoveEvent{e}
|
|
|
|
|
|
|
|
fn := func(handler d2interface.InputEventHandler) bool {
|
|
|
|
if l, ok := handler.(d2interface.MouseMoveHandler); ok {
|
|
|
|
return l.OnMouseMove(&event)
|
2020-01-25 23:28:21 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
2020-07-03 15:09:16 -04:00
|
|
|
}
|
|
|
|
im.propagate(fn)
|
2020-01-25 23:28:21 -05:00
|
|
|
|
|
|
|
im.cursorX, im.cursorY = cursorX, cursorY
|
|
|
|
}
|
2020-07-03 15:09:16 -04:00
|
|
|
}
|
2020-01-25 23:28:21 -05:00
|
|
|
|
2020-07-03 15:09:16 -04:00
|
|
|
// BindHandlerWithPriority adds an event handler with a specific call priority
|
|
|
|
func (im *inputManager) BindHandlerWithPriority(
|
|
|
|
h d2interface.InputEventHandler,
|
|
|
|
p d2interface.Priority) error {
|
|
|
|
return singleton.bindHandler(h, p)
|
2020-01-25 23:28:21 -05:00
|
|
|
}
|
|
|
|
|
2020-07-03 15:09:16 -04:00
|
|
|
// BindHandler adds an event handler
|
|
|
|
func (im *inputManager) BindHandler(h d2interface.InputEventHandler) error {
|
|
|
|
return im.bindHandler(h, d2interface.PriorityDefault)
|
|
|
|
}
|
|
|
|
|
|
|
|
// BindHandler adds an event handler
|
|
|
|
func (im *inputManager) bindHandler(h d2interface.InputEventHandler, p d2interface.Priority) error {
|
2020-01-25 23:28:21 -05:00
|
|
|
for _, entry := range im.entries {
|
2020-07-03 15:09:16 -04:00
|
|
|
if entry.handler == h {
|
2020-01-25 23:28:21 -05:00
|
|
|
return ErrHasReg
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-03 15:09:16 -04:00
|
|
|
entry := handlerEntry{h, p}
|
|
|
|
im.entries = append(im.entries, entry)
|
2020-01-25 23:28:21 -05:00
|
|
|
sort.Sort(im.entries)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-07-03 15:09:16 -04:00
|
|
|
// UnbindHandler removes a previously bound event handler
|
|
|
|
func (im *inputManager) UnbindHandler(handler d2interface.InputEventHandler) error {
|
2020-01-25 23:28:21 -05:00
|
|
|
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]
|
2020-07-03 15:09:16 -04:00
|
|
|
|
2020-01-25 23:28:21 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ErrNotReg
|
|
|
|
}
|
|
|
|
|
2020-07-03 15:09:16 -04:00
|
|
|
func (im *inputManager) propagate(callback func(d2interface.InputEventHandler) bool) {
|
|
|
|
var priority d2interface.Priority
|
|
|
|
|
2020-01-25 23:28:21 -05:00
|
|
|
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 {
|
2020-07-03 15:09:16 -04:00
|
|
|
handler d2interface.InputEventHandler
|
|
|
|
priority d2interface.Priority
|
2020-07-01 14:03:40 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|