From ffc85964461dea1a4c8769f7901f07b0f3810b0d Mon Sep 17 00:00:00 2001 From: Ben Perry Date: Fri, 13 Oct 2023 12:04:51 -0500 Subject: [PATCH] Move pixel input defs to core, map to them from backend --- backends/opengl/input.go | 455 +++++++++++++----------------------- backends/opengl/joystick.go | 154 ++++++------ backends/opengl/window.go | 8 +- input.go | 434 ++++++++++++++++++++++++++++++++++ 4 files changed, 665 insertions(+), 386 deletions(-) create mode 100644 input.go diff --git a/backends/opengl/input.go b/backends/opengl/input.go index 73c1eb7..7ec4bcc 100644 --- a/backends/opengl/input.go +++ b/backends/opengl/input.go @@ -9,24 +9,24 @@ import ( ) // Pressed returns whether the Button is currently pressed down. -func (w *Window) Pressed(button Button) bool { +func (w *Window) Pressed(button pixel.Button) bool { return w.currInp.buttons[button] } // JustPressed returns whether the Button has been pressed in the last frame. -func (w *Window) JustPressed(button Button) bool { +func (w *Window) JustPressed(button pixel.Button) bool { return w.pressEvents[button] } // JustReleased returns whether the Button has been released in the last frame. -func (w *Window) JustReleased(button Button) bool { +func (w *Window) JustReleased(button pixel.Button) bool { return w.releaseEvents[button] } // Repeated returns whether a repeat event has been triggered on button. // // Repeat event occurs repeatedly when a button is held down for some time. -func (w *Window) Repeated(button Button) bool { +func (w *Window) Repeated(button pixel.Button) bool { return w.currInp.repeat[button] } @@ -71,302 +71,156 @@ func (w *Window) Typed() string { return w.currInp.typed } -// Button is a keyboard or mouse button. Why distinguish? -type Button int - -// List of all mouse buttons. -const ( - MouseButton1 = Button(glfw.MouseButton1) - MouseButton2 = Button(glfw.MouseButton2) - MouseButton3 = Button(glfw.MouseButton3) - MouseButton4 = Button(glfw.MouseButton4) - MouseButton5 = Button(glfw.MouseButton5) - MouseButton6 = Button(glfw.MouseButton6) - MouseButton7 = Button(glfw.MouseButton7) - MouseButton8 = Button(glfw.MouseButton8) - MouseButtonLast = Button(glfw.MouseButtonLast) - MouseButtonLeft = Button(glfw.MouseButtonLeft) - MouseButtonRight = Button(glfw.MouseButtonRight) - MouseButtonMiddle = Button(glfw.MouseButtonMiddle) -) - -// List of all keyboard buttons. -const ( - KeyUnknown = Button(glfw.KeyUnknown) - KeySpace = Button(glfw.KeySpace) - KeyApostrophe = Button(glfw.KeyApostrophe) - KeyComma = Button(glfw.KeyComma) - KeyMinus = Button(glfw.KeyMinus) - KeyPeriod = Button(glfw.KeyPeriod) - KeySlash = Button(glfw.KeySlash) - Key0 = Button(glfw.Key0) - Key1 = Button(glfw.Key1) - Key2 = Button(glfw.Key2) - Key3 = Button(glfw.Key3) - Key4 = Button(glfw.Key4) - Key5 = Button(glfw.Key5) - Key6 = Button(glfw.Key6) - Key7 = Button(glfw.Key7) - Key8 = Button(glfw.Key8) - Key9 = Button(glfw.Key9) - KeySemicolon = Button(glfw.KeySemicolon) - KeyEqual = Button(glfw.KeyEqual) - KeyA = Button(glfw.KeyA) - KeyB = Button(glfw.KeyB) - KeyC = Button(glfw.KeyC) - KeyD = Button(glfw.KeyD) - KeyE = Button(glfw.KeyE) - KeyF = Button(glfw.KeyF) - KeyG = Button(glfw.KeyG) - KeyH = Button(glfw.KeyH) - KeyI = Button(glfw.KeyI) - KeyJ = Button(glfw.KeyJ) - KeyK = Button(glfw.KeyK) - KeyL = Button(glfw.KeyL) - KeyM = Button(glfw.KeyM) - KeyN = Button(glfw.KeyN) - KeyO = Button(glfw.KeyO) - KeyP = Button(glfw.KeyP) - KeyQ = Button(glfw.KeyQ) - KeyR = Button(glfw.KeyR) - KeyS = Button(glfw.KeyS) - KeyT = Button(glfw.KeyT) - KeyU = Button(glfw.KeyU) - KeyV = Button(glfw.KeyV) - KeyW = Button(glfw.KeyW) - KeyX = Button(glfw.KeyX) - KeyY = Button(glfw.KeyY) - KeyZ = Button(glfw.KeyZ) - KeyLeftBracket = Button(glfw.KeyLeftBracket) - KeyBackslash = Button(glfw.KeyBackslash) - KeyRightBracket = Button(glfw.KeyRightBracket) - KeyGraveAccent = Button(glfw.KeyGraveAccent) - KeyWorld1 = Button(glfw.KeyWorld1) - KeyWorld2 = Button(glfw.KeyWorld2) - KeyEscape = Button(glfw.KeyEscape) - KeyEnter = Button(glfw.KeyEnter) - KeyTab = Button(glfw.KeyTab) - KeyBackspace = Button(glfw.KeyBackspace) - KeyInsert = Button(glfw.KeyInsert) - KeyDelete = Button(glfw.KeyDelete) - KeyRight = Button(glfw.KeyRight) - KeyLeft = Button(glfw.KeyLeft) - KeyDown = Button(glfw.KeyDown) - KeyUp = Button(glfw.KeyUp) - KeyPageUp = Button(glfw.KeyPageUp) - KeyPageDown = Button(glfw.KeyPageDown) - KeyHome = Button(glfw.KeyHome) - KeyEnd = Button(glfw.KeyEnd) - KeyCapsLock = Button(glfw.KeyCapsLock) - KeyScrollLock = Button(glfw.KeyScrollLock) - KeyNumLock = Button(glfw.KeyNumLock) - KeyPrintScreen = Button(glfw.KeyPrintScreen) - KeyPause = Button(glfw.KeyPause) - KeyF1 = Button(glfw.KeyF1) - KeyF2 = Button(glfw.KeyF2) - KeyF3 = Button(glfw.KeyF3) - KeyF4 = Button(glfw.KeyF4) - KeyF5 = Button(glfw.KeyF5) - KeyF6 = Button(glfw.KeyF6) - KeyF7 = Button(glfw.KeyF7) - KeyF8 = Button(glfw.KeyF8) - KeyF9 = Button(glfw.KeyF9) - KeyF10 = Button(glfw.KeyF10) - KeyF11 = Button(glfw.KeyF11) - KeyF12 = Button(glfw.KeyF12) - KeyF13 = Button(glfw.KeyF13) - KeyF14 = Button(glfw.KeyF14) - KeyF15 = Button(glfw.KeyF15) - KeyF16 = Button(glfw.KeyF16) - KeyF17 = Button(glfw.KeyF17) - KeyF18 = Button(glfw.KeyF18) - KeyF19 = Button(glfw.KeyF19) - KeyF20 = Button(glfw.KeyF20) - KeyF21 = Button(glfw.KeyF21) - KeyF22 = Button(glfw.KeyF22) - KeyF23 = Button(glfw.KeyF23) - KeyF24 = Button(glfw.KeyF24) - KeyF25 = Button(glfw.KeyF25) - KeyKP0 = Button(glfw.KeyKP0) - KeyKP1 = Button(glfw.KeyKP1) - KeyKP2 = Button(glfw.KeyKP2) - KeyKP3 = Button(glfw.KeyKP3) - KeyKP4 = Button(glfw.KeyKP4) - KeyKP5 = Button(glfw.KeyKP5) - KeyKP6 = Button(glfw.KeyKP6) - KeyKP7 = Button(glfw.KeyKP7) - KeyKP8 = Button(glfw.KeyKP8) - KeyKP9 = Button(glfw.KeyKP9) - KeyKPDecimal = Button(glfw.KeyKPDecimal) - KeyKPDivide = Button(glfw.KeyKPDivide) - KeyKPMultiply = Button(glfw.KeyKPMultiply) - KeyKPSubtract = Button(glfw.KeyKPSubtract) - KeyKPAdd = Button(glfw.KeyKPAdd) - KeyKPEnter = Button(glfw.KeyKPEnter) - KeyKPEqual = Button(glfw.KeyKPEqual) - KeyLeftShift = Button(glfw.KeyLeftShift) - KeyLeftControl = Button(glfw.KeyLeftControl) - KeyLeftAlt = Button(glfw.KeyLeftAlt) - KeyLeftSuper = Button(glfw.KeyLeftSuper) - KeyRightShift = Button(glfw.KeyRightShift) - KeyRightControl = Button(glfw.KeyRightControl) - KeyRightAlt = Button(glfw.KeyRightAlt) - KeyRightSuper = Button(glfw.KeyRightSuper) - KeyMenu = Button(glfw.KeyMenu) - KeyLast = Button(glfw.KeyLast) -) - -// String returns a human-readable string describing the Button. -func (b Button) String() string { - name, ok := buttonNames[b] - if !ok { - return "Invalid" - } - return name +var mouseButtonMapping = map[glfw.MouseButton]pixel.Button{ + glfw.MouseButton1: pixel.MouseButton1, + glfw.MouseButton2: pixel.MouseButton2, + glfw.MouseButton3: pixel.MouseButton3, + glfw.MouseButton4: pixel.MouseButton4, + glfw.MouseButton5: pixel.MouseButton5, + glfw.MouseButton6: pixel.MouseButton6, + glfw.MouseButton7: pixel.MouseButton7, + glfw.MouseButton8: pixel.MouseButton8, } -var buttonNames = map[Button]string{ - MouseButton4: "MouseButton4", - MouseButton5: "MouseButton5", - MouseButton6: "MouseButton6", - MouseButton7: "MouseButton7", - MouseButton8: "MouseButton8", - MouseButtonLeft: "MouseButtonLeft", - MouseButtonRight: "MouseButtonRight", - MouseButtonMiddle: "MouseButtonMiddle", - KeyUnknown: "Unknown", - KeySpace: "Space", - KeyApostrophe: "Apostrophe", - KeyComma: "Comma", - KeyMinus: "Minus", - KeyPeriod: "Period", - KeySlash: "Slash", - Key0: "0", - Key1: "1", - Key2: "2", - Key3: "3", - Key4: "4", - Key5: "5", - Key6: "6", - Key7: "7", - Key8: "8", - Key9: "9", - KeySemicolon: "Semicolon", - KeyEqual: "Equal", - KeyA: "A", - KeyB: "B", - KeyC: "C", - KeyD: "D", - KeyE: "E", - KeyF: "F", - KeyG: "G", - KeyH: "H", - KeyI: "I", - KeyJ: "J", - KeyK: "K", - KeyL: "L", - KeyM: "M", - KeyN: "N", - KeyO: "O", - KeyP: "P", - KeyQ: "Q", - KeyR: "R", - KeyS: "S", - KeyT: "T", - KeyU: "U", - KeyV: "V", - KeyW: "W", - KeyX: "X", - KeyY: "Y", - KeyZ: "Z", - KeyLeftBracket: "LeftBracket", - KeyBackslash: "Backslash", - KeyRightBracket: "RightBracket", - KeyGraveAccent: "GraveAccent", - KeyWorld1: "World1", - KeyWorld2: "World2", - KeyEscape: "Escape", - KeyEnter: "Enter", - KeyTab: "Tab", - KeyBackspace: "Backspace", - KeyInsert: "Insert", - KeyDelete: "Delete", - KeyRight: "Right", - KeyLeft: "Left", - KeyDown: "Down", - KeyUp: "Up", - KeyPageUp: "PageUp", - KeyPageDown: "PageDown", - KeyHome: "Home", - KeyEnd: "End", - KeyCapsLock: "CapsLock", - KeyScrollLock: "ScrollLock", - KeyNumLock: "NumLock", - KeyPrintScreen: "PrintScreen", - KeyPause: "Pause", - KeyF1: "F1", - KeyF2: "F2", - KeyF3: "F3", - KeyF4: "F4", - KeyF5: "F5", - KeyF6: "F6", - KeyF7: "F7", - KeyF8: "F8", - KeyF9: "F9", - KeyF10: "F10", - KeyF11: "F11", - KeyF12: "F12", - KeyF13: "F13", - KeyF14: "F14", - KeyF15: "F15", - KeyF16: "F16", - KeyF17: "F17", - KeyF18: "F18", - KeyF19: "F19", - KeyF20: "F20", - KeyF21: "F21", - KeyF22: "F22", - KeyF23: "F23", - KeyF24: "F24", - KeyF25: "F25", - KeyKP0: "KP0", - KeyKP1: "KP1", - KeyKP2: "KP2", - KeyKP3: "KP3", - KeyKP4: "KP4", - KeyKP5: "KP5", - KeyKP6: "KP6", - KeyKP7: "KP7", - KeyKP8: "KP8", - KeyKP9: "KP9", - KeyKPDecimal: "KPDecimal", - KeyKPDivide: "KPDivide", - KeyKPMultiply: "KPMultiply", - KeyKPSubtract: "KPSubtract", - KeyKPAdd: "KPAdd", - KeyKPEnter: "KPEnter", - KeyKPEqual: "KPEqual", - KeyLeftShift: "LeftShift", - KeyLeftControl: "LeftControl", - KeyLeftAlt: "LeftAlt", - KeyLeftSuper: "LeftSuper", - KeyRightShift: "RightShift", - KeyRightControl: "RightControl", - KeyRightAlt: "RightAlt", - KeyRightSuper: "RightSuper", - KeyMenu: "Menu", +var keyButtonMapping = map[glfw.Key]pixel.Button{ + glfw.KeyUnknown: pixel.KeyUnknown, + glfw.KeySpace: pixel.KeySpace, + glfw.KeyApostrophe: pixel.KeyApostrophe, + glfw.KeyComma: pixel.KeyComma, + glfw.KeyMinus: pixel.KeyMinus, + glfw.KeyPeriod: pixel.KeyPeriod, + glfw.KeySlash: pixel.KeySlash, + glfw.Key0: pixel.Key0, + glfw.Key1: pixel.Key1, + glfw.Key2: pixel.Key2, + glfw.Key3: pixel.Key3, + glfw.Key4: pixel.Key4, + glfw.Key5: pixel.Key5, + glfw.Key6: pixel.Key6, + glfw.Key7: pixel.Key7, + glfw.Key8: pixel.Key8, + glfw.Key9: pixel.Key9, + glfw.KeySemicolon: pixel.KeySemicolon, + glfw.KeyEqual: pixel.KeyEqual, + glfw.KeyA: pixel.KeyA, + glfw.KeyB: pixel.KeyB, + glfw.KeyC: pixel.KeyC, + glfw.KeyD: pixel.KeyD, + glfw.KeyE: pixel.KeyE, + glfw.KeyF: pixel.KeyF, + glfw.KeyG: pixel.KeyG, + glfw.KeyH: pixel.KeyH, + glfw.KeyI: pixel.KeyI, + glfw.KeyJ: pixel.KeyJ, + glfw.KeyK: pixel.KeyK, + glfw.KeyL: pixel.KeyL, + glfw.KeyM: pixel.KeyM, + glfw.KeyN: pixel.KeyN, + glfw.KeyO: pixel.KeyO, + glfw.KeyP: pixel.KeyP, + glfw.KeyQ: pixel.KeyQ, + glfw.KeyR: pixel.KeyR, + glfw.KeyS: pixel.KeyS, + glfw.KeyT: pixel.KeyT, + glfw.KeyU: pixel.KeyU, + glfw.KeyV: pixel.KeyV, + glfw.KeyW: pixel.KeyW, + glfw.KeyX: pixel.KeyX, + glfw.KeyY: pixel.KeyY, + glfw.KeyZ: pixel.KeyZ, + glfw.KeyLeftBracket: pixel.KeyLeftBracket, + glfw.KeyBackslash: pixel.KeyBackslash, + glfw.KeyRightBracket: pixel.KeyRightBracket, + glfw.KeyGraveAccent: pixel.KeyGraveAccent, + glfw.KeyWorld1: pixel.KeyWorld1, + glfw.KeyWorld2: pixel.KeyWorld2, + glfw.KeyEscape: pixel.KeyEscape, + glfw.KeyEnter: pixel.KeyEnter, + glfw.KeyTab: pixel.KeyTab, + glfw.KeyBackspace: pixel.KeyBackspace, + glfw.KeyInsert: pixel.KeyInsert, + glfw.KeyDelete: pixel.KeyDelete, + glfw.KeyRight: pixel.KeyRight, + glfw.KeyLeft: pixel.KeyLeft, + glfw.KeyDown: pixel.KeyDown, + glfw.KeyUp: pixel.KeyUp, + glfw.KeyPageUp: pixel.KeyPageUp, + glfw.KeyPageDown: pixel.KeyPageDown, + glfw.KeyHome: pixel.KeyHome, + glfw.KeyEnd: pixel.KeyEnd, + glfw.KeyCapsLock: pixel.KeyCapsLock, + glfw.KeyScrollLock: pixel.KeyScrollLock, + glfw.KeyNumLock: pixel.KeyNumLock, + glfw.KeyPrintScreen: pixel.KeyPrintScreen, + glfw.KeyPause: pixel.KeyPause, + glfw.KeyF1: pixel.KeyF1, + glfw.KeyF2: pixel.KeyF2, + glfw.KeyF3: pixel.KeyF3, + glfw.KeyF4: pixel.KeyF4, + glfw.KeyF5: pixel.KeyF5, + glfw.KeyF6: pixel.KeyF6, + glfw.KeyF7: pixel.KeyF7, + glfw.KeyF8: pixel.KeyF8, + glfw.KeyF9: pixel.KeyF9, + glfw.KeyF10: pixel.KeyF10, + glfw.KeyF11: pixel.KeyF11, + glfw.KeyF12: pixel.KeyF12, + glfw.KeyF13: pixel.KeyF13, + glfw.KeyF14: pixel.KeyF14, + glfw.KeyF15: pixel.KeyF15, + glfw.KeyF16: pixel.KeyF16, + glfw.KeyF17: pixel.KeyF17, + glfw.KeyF18: pixel.KeyF18, + glfw.KeyF19: pixel.KeyF19, + glfw.KeyF20: pixel.KeyF20, + glfw.KeyF21: pixel.KeyF21, + glfw.KeyF22: pixel.KeyF22, + glfw.KeyF23: pixel.KeyF23, + glfw.KeyF24: pixel.KeyF24, + glfw.KeyF25: pixel.KeyF25, + glfw.KeyKP0: pixel.KeyKP0, + glfw.KeyKP1: pixel.KeyKP1, + glfw.KeyKP2: pixel.KeyKP2, + glfw.KeyKP3: pixel.KeyKP3, + glfw.KeyKP4: pixel.KeyKP4, + glfw.KeyKP5: pixel.KeyKP5, + glfw.KeyKP6: pixel.KeyKP6, + glfw.KeyKP7: pixel.KeyKP7, + glfw.KeyKP8: pixel.KeyKP8, + glfw.KeyKP9: pixel.KeyKP9, + glfw.KeyKPDecimal: pixel.KeyKPDecimal, + glfw.KeyKPDivide: pixel.KeyKPDivide, + glfw.KeyKPMultiply: pixel.KeyKPMultiply, + glfw.KeyKPSubtract: pixel.KeyKPSubtract, + glfw.KeyKPAdd: pixel.KeyKPAdd, + glfw.KeyKPEnter: pixel.KeyKPEnter, + glfw.KeyKPEqual: pixel.KeyKPEqual, + glfw.KeyLeftShift: pixel.KeyLeftShift, + glfw.KeyLeftControl: pixel.KeyLeftControl, + glfw.KeyLeftAlt: pixel.KeyLeftAlt, + glfw.KeyLeftSuper: pixel.KeyLeftSuper, + glfw.KeyRightShift: pixel.KeyRightShift, + glfw.KeyRightControl: pixel.KeyRightControl, + glfw.KeyRightAlt: pixel.KeyRightAlt, + glfw.KeyRightSuper: pixel.KeyRightSuper, + glfw.KeyMenu: pixel.KeyMenu, } func (w *Window) initInput() { mainthread.Call(func() { w.window.SetMouseButtonCallback(func(_ *glfw.Window, button glfw.MouseButton, action glfw.Action, mod glfw.ModifierKey) { + mb, ok := mouseButtonMapping[button] + if !ok { + return + } + switch action { case glfw.Press: - w.tempPressEvents[Button(button)] = true - w.tempInp.buttons[Button(button)] = true + w.tempPressEvents[mb] = true + w.tempInp.buttons[mb] = true case glfw.Release: - w.tempReleaseEvents[Button(button)] = true - w.tempInp.buttons[Button(button)] = false + w.tempReleaseEvents[mb] = true + w.tempInp.buttons[mb] = false } }) @@ -374,15 +228,20 @@ func (w *Window) initInput() { if key == glfw.KeyUnknown { return } + kb, ok := keyButtonMapping[key] + if !ok { + return + } + switch action { case glfw.Press: - w.tempPressEvents[Button(key)] = true - w.tempInp.buttons[Button(key)] = true + w.tempPressEvents[kb] = true + w.tempInp.buttons[kb] = true case glfw.Release: - w.tempReleaseEvents[Button(key)] = true - w.tempInp.buttons[Button(key)] = false + w.tempReleaseEvents[kb] = true + w.tempInp.buttons[kb] = false case glfw.Repeat: - w.tempInp.repeat[Button(key)] = true + w.tempInp.repeat[kb] = true } }) @@ -439,9 +298,9 @@ func (w *Window) doUpdateInput() { w.releaseEvents = w.tempReleaseEvents // Clear last frame's temporary status - w.tempPressEvents = [KeyLast + 1]bool{} - w.tempReleaseEvents = [KeyLast + 1]bool{} - w.tempInp.repeat = [KeyLast + 1]bool{} + w.tempPressEvents = [pixel.KeyLast + 1]bool{} + w.tempReleaseEvents = [pixel.KeyLast + 1]bool{} + w.tempInp.repeat = [pixel.KeyLast + 1]bool{} w.tempInp.scroll = pixel.ZV w.tempInp.typed = "" diff --git a/backends/opengl/joystick.go b/backends/opengl/joystick.go index 6a7d905..a8a2daa 100644 --- a/backends/opengl/joystick.go +++ b/backends/opengl/joystick.go @@ -2,78 +2,60 @@ package opengl import ( "github.com/go-gl/glfw/v3.3/glfw" + "github.com/gopxl/pixel/v2" ) -// Joystick is a joystick or controller (gamepad). -type Joystick int - -// List all of the joysticks. -const ( - Joystick1 = Joystick(glfw.Joystick1) - Joystick2 = Joystick(glfw.Joystick2) - Joystick3 = Joystick(glfw.Joystick3) - Joystick4 = Joystick(glfw.Joystick4) - Joystick5 = Joystick(glfw.Joystick5) - Joystick6 = Joystick(glfw.Joystick6) - Joystick7 = Joystick(glfw.Joystick7) - Joystick8 = Joystick(glfw.Joystick8) - Joystick9 = Joystick(glfw.Joystick9) - Joystick10 = Joystick(glfw.Joystick10) - Joystick11 = Joystick(glfw.Joystick11) - Joystick12 = Joystick(glfw.Joystick12) - Joystick13 = Joystick(glfw.Joystick13) - Joystick14 = Joystick(glfw.Joystick14) - Joystick15 = Joystick(glfw.Joystick15) - Joystick16 = Joystick(glfw.Joystick16) - - JoystickLast = Joystick(glfw.JoystickLast) -) +var joystickMapping = map[glfw.Joystick]pixel.Joystick{ + glfw.Joystick1: pixel.Joystick1, + glfw.Joystick2: pixel.Joystick2, + glfw.Joystick3: pixel.Joystick3, + glfw.Joystick4: pixel.Joystick4, + glfw.Joystick5: pixel.Joystick5, + glfw.Joystick6: pixel.Joystick6, + glfw.Joystick7: pixel.Joystick7, + glfw.Joystick8: pixel.Joystick8, + glfw.Joystick9: pixel.Joystick9, + glfw.Joystick10: pixel.Joystick10, + glfw.Joystick11: pixel.Joystick11, + glfw.Joystick12: pixel.Joystick12, + glfw.Joystick13: pixel.Joystick13, + glfw.Joystick14: pixel.Joystick14, + glfw.Joystick15: pixel.Joystick15, + glfw.Joystick16: pixel.Joystick16, +} -// GamepadAxis corresponds to a gamepad axis. -type GamepadAxis int - -// Gamepad axis IDs. -const ( - AxisLeftX = GamepadAxis(glfw.AxisLeftX) - AxisLeftY = GamepadAxis(glfw.AxisLeftY) - AxisRightX = GamepadAxis(glfw.AxisRightX) - AxisRightY = GamepadAxis(glfw.AxisRightY) - AxisLeftTrigger = GamepadAxis(glfw.AxisLeftTrigger) - AxisRightTrigger = GamepadAxis(glfw.AxisRightTrigger) - AxisLast = GamepadAxis(glfw.AxisLast) -) +// Not currently used because Gamepad Axis/Button input works a bit different than others +var _ = map[glfw.GamepadAxis]pixel.GamepadAxis{ + glfw.AxisLeftX: pixel.AxisLeftX, + glfw.AxisLeftY: pixel.AxisLeftY, + glfw.AxisRightX: pixel.AxisRightX, + glfw.AxisRightY: pixel.AxisRightY, + glfw.AxisLeftTrigger: pixel.AxisLeftTrigger, + glfw.AxisRightTrigger: pixel.AxisRightTrigger, +} -// GamepadButton corresponds to a gamepad button. -type GamepadButton int - -// Gamepad button IDs. -const ( - ButtonA = GamepadButton(glfw.ButtonA) - ButtonB = GamepadButton(glfw.ButtonB) - ButtonX = GamepadButton(glfw.ButtonX) - ButtonY = GamepadButton(glfw.ButtonY) - ButtonLeftBumper = GamepadButton(glfw.ButtonLeftBumper) - ButtonRightBumper = GamepadButton(glfw.ButtonRightBumper) - ButtonBack = GamepadButton(glfw.ButtonBack) - ButtonStart = GamepadButton(glfw.ButtonStart) - ButtonGuide = GamepadButton(glfw.ButtonGuide) - ButtonLeftThumb = GamepadButton(glfw.ButtonLeftThumb) - ButtonRightThumb = GamepadButton(glfw.ButtonRightThumb) - ButtonDpadUp = GamepadButton(glfw.ButtonDpadUp) - ButtonDpadRight = GamepadButton(glfw.ButtonDpadRight) - ButtonDpadDown = GamepadButton(glfw.ButtonDpadDown) - ButtonDpadLeft = GamepadButton(glfw.ButtonDpadLeft) - ButtonLast = GamepadButton(glfw.ButtonLast) - ButtonCross = GamepadButton(glfw.ButtonCross) - ButtonCircle = GamepadButton(glfw.ButtonCircle) - ButtonSquare = GamepadButton(glfw.ButtonSquare) - ButtonTriangle = GamepadButton(glfw.ButtonTriangle) -) +var _ = map[glfw.GamepadButton]pixel.GamepadButton{ + glfw.ButtonA: pixel.GamepadA, + glfw.ButtonB: pixel.GamepadB, + glfw.ButtonX: pixel.GamepadX, + glfw.ButtonY: pixel.GamepadY, + glfw.ButtonLeftBumper: pixel.GamepadLeftBumper, + glfw.ButtonRightBumper: pixel.GamepadRightBumper, + glfw.ButtonBack: pixel.GamepadBack, + glfw.ButtonStart: pixel.GamepadStart, + glfw.ButtonGuide: pixel.GamepadGuide, + glfw.ButtonLeftThumb: pixel.GamepadLeftThumb, + glfw.ButtonRightThumb: pixel.GamepadRightThumb, + glfw.ButtonDpadUp: pixel.GamepadDpadUp, + glfw.ButtonDpadRight: pixel.GamepadDpadRight, + glfw.ButtonDpadDown: pixel.GamepadDpadDown, + glfw.ButtonDpadLeft: pixel.GamepadDpadLeft, +} // JoystickPresent returns if the joystick is currently connected. // // This API is experimental. -func (w *Window) JoystickPresent(js Joystick) bool { +func (w *Window) JoystickPresent(js pixel.Joystick) bool { return w.currJoy.connected[js] } @@ -81,21 +63,21 @@ func (w *Window) JoystickPresent(js Joystick) bool { // empty string. // // This API is experimental. -func (w *Window) JoystickName(js Joystick) string { +func (w *Window) JoystickName(js pixel.Joystick) string { return w.currJoy.name[js] } // JoystickButtonCount returns the number of buttons a connected joystick has. // // This API is experimental. -func (w *Window) JoystickButtonCount(js Joystick) int { +func (w *Window) JoystickButtonCount(js pixel.Joystick) int { return len(w.currJoy.buttons[js]) } // JoystickAxisCount returns the number of axes a connected joystick has. // // This API is experimental. -func (w *Window) JoystickAxisCount(js Joystick) int { +func (w *Window) JoystickAxisCount(js pixel.Joystick) int { return len(w.currJoy.axis[js]) } @@ -103,7 +85,7 @@ func (w *Window) JoystickAxisCount(js Joystick) int { // If the button index is out of range, this will return false. // // This API is experimental. -func (w *Window) JoystickPressed(js Joystick, button GamepadButton) bool { +func (w *Window) JoystickPressed(js pixel.Joystick, button pixel.GamepadButton) bool { return w.currJoy.getButton(js, int(button)) } @@ -111,7 +93,7 @@ func (w *Window) JoystickPressed(js Joystick, button GamepadButton) bool { // If the button index is out of range, this will return false. // // This API is experimental. -func (w *Window) JoystickJustPressed(js Joystick, button GamepadButton) bool { +func (w *Window) JoystickJustPressed(js pixel.Joystick, button pixel.GamepadButton) bool { return w.currJoy.getButton(js, int(button)) && !w.prevJoy.getButton(js, int(button)) } @@ -119,7 +101,7 @@ func (w *Window) JoystickJustPressed(js Joystick, button GamepadButton) bool { // If the button index is out of range, this will return false. // // This API is experimental. -func (w *Window) JoystickJustReleased(js Joystick, button GamepadButton) bool { +func (w *Window) JoystickJustReleased(js pixel.Joystick, button pixel.GamepadButton) bool { return !w.currJoy.getButton(js, int(button)) && w.prevJoy.getButton(js, int(button)) } @@ -127,31 +109,35 @@ func (w *Window) JoystickJustReleased(js Joystick, button GamepadButton) bool { // If the axis index is out of range, this will return 0. // // This API is experimental. -func (w *Window) JoystickAxis(js Joystick, axis GamepadAxis) float64 { +func (w *Window) JoystickAxis(js pixel.Joystick, axis pixel.GamepadAxis) float64 { return w.currJoy.getAxis(js, int(axis)) } // Used internally during Window.UpdateInput to update the state of the joysticks. func (w *Window) updateJoystickInput() { - for js := Joystick1; js <= JoystickLast; js++ { + for joystick := glfw.Joystick1; joystick <= glfw.JoystickLast; joystick++ { + js, ok := joystickMapping[joystick] + if !ok { + return + } // Determine and store if the joystick was connected - joystickPresent := glfw.Joystick(js).Present() + joystickPresent := joystick.Present() w.tempJoy.connected[js] = joystickPresent if joystickPresent { - if glfw.Joystick(js).IsGamepad() { - gamepadInputs := glfw.Joystick(js).GetGamepadState() + if joystick.IsGamepad() { + gamepadInputs := joystick.GetGamepadState() w.tempJoy.buttons[js] = gamepadInputs.Buttons[:] w.tempJoy.axis[js] = gamepadInputs.Axes[:] } else { - w.tempJoy.buttons[js] = glfw.Joystick(js).GetButtons() - w.tempJoy.axis[js] = glfw.Joystick(js).GetAxes() + w.tempJoy.buttons[js] = joystick.GetButtons() + w.tempJoy.axis[js] = joystick.GetAxes() } if !w.currJoy.connected[js] { // The joystick was recently connected, we get the name - w.tempJoy.name[js] = glfw.Joystick(js).GetName() + w.tempJoy.name[js] = joystick.GetName() } else { // Use the name from the previous one w.tempJoy.name[js] = w.currJoy.name[js] @@ -168,14 +154,14 @@ func (w *Window) updateJoystickInput() { } type joystickState struct { - connected [JoystickLast + 1]bool - name [JoystickLast + 1]string - buttons [JoystickLast + 1][]glfw.Action - axis [JoystickLast + 1][]float32 + connected [pixel.JoystickLast + 1]bool + name [pixel.JoystickLast + 1]string + buttons [pixel.JoystickLast + 1][]glfw.Action + axis [pixel.JoystickLast + 1][]float32 } // Returns if a button on a joystick is down, returning false if the button or joystick is invalid. -func (js *joystickState) getButton(joystick Joystick, button int) bool { +func (js *joystickState) getButton(joystick pixel.Joystick, button int) bool { // Check that the joystick and button is valid, return false by default if js.buttons[joystick] == nil || button >= len(js.buttons[joystick]) || button < 0 { return false @@ -184,7 +170,7 @@ func (js *joystickState) getButton(joystick Joystick, button int) bool { } // Returns the value of a joystick axis, returning 0 if the button or joystick is invalid. -func (js *joystickState) getAxis(joystick Joystick, axis int) float64 { +func (js *joystickState) getAxis(joystick pixel.Joystick, axis int) float64 { // Check that the joystick and axis is valid, return 0 by default. if js.axis[joystick] == nil || axis >= len(js.axis[joystick]) || axis < 0 { return 0 diff --git a/backends/opengl/window.go b/backends/opengl/window.go index 1ae636e..4caea24 100644 --- a/backends/opengl/window.go +++ b/backends/opengl/window.go @@ -99,14 +99,14 @@ type Window struct { prevInp, currInp, tempInp struct { mouse pixel.Vec - buttons [KeyLast + 1]bool - repeat [KeyLast + 1]bool + buttons [pixel.KeyLast + 1]bool + repeat [pixel.KeyLast + 1]bool scroll pixel.Vec typed string } - pressEvents, tempPressEvents [KeyLast + 1]bool - releaseEvents, tempReleaseEvents [KeyLast + 1]bool + pressEvents, tempPressEvents [pixel.KeyLast + 1]bool + releaseEvents, tempReleaseEvents [pixel.KeyLast + 1]bool prevJoy, currJoy, tempJoy joystickState } diff --git a/input.go b/input.go new file mode 100644 index 0000000..1961581 --- /dev/null +++ b/input.go @@ -0,0 +1,434 @@ +package pixel + +type Button int + +// String returns a human-readable string describing the Button. +func (b Button) String() string { + name, ok := buttonNames[b] + if !ok { + return "Invalid" + } + return name +} + +const ( + // List of all mouse buttons. + MouseButton1 Button = iota + MouseButton2 + MouseButton3 + MouseButton4 + MouseButton5 + MouseButton6 + MouseButton7 + MouseButton8 + + // Aliases + MouseButtonLeft = MouseButton1 + MouseButtonRight = MouseButton2 + MouseButtonMiddle = MouseButton3 + MouseButtonLast = MouseButton8 +) + +const ( + // List of all keyboard buttons. + KeyUnknown Button = iota + Button(MouseButtonLast) + 1 + KeySpace + KeyApostrophe + KeyComma + KeyMinus + KeyPeriod + KeySlash + Key0 + Key1 + Key2 + Key3 + Key4 + Key5 + Key6 + Key7 + Key8 + Key9 + KeySemicolon + KeyEqual + KeyA + KeyB + KeyC + KeyD + KeyE + KeyF + KeyG + KeyH + KeyI + KeyJ + KeyK + KeyL + KeyM + KeyN + KeyO + KeyP + KeyQ + KeyR + KeyS + KeyT + KeyU + KeyV + KeyW + KeyX + KeyY + KeyZ + KeyLeftBracket + KeyBackslash + KeyRightBracket + KeyGraveAccent + KeyWorld1 + KeyWorld2 + KeyEscape + KeyEnter + KeyTab + KeyBackspace + KeyInsert + KeyDelete + KeyRight + KeyLeft + KeyDown + KeyUp + KeyPageUp + KeyPageDown + KeyHome + KeyEnd + KeyCapsLock + KeyScrollLock + KeyNumLock + KeyPrintScreen + KeyPause + KeyF1 + KeyF2 + KeyF3 + KeyF4 + KeyF5 + KeyF6 + KeyF7 + KeyF8 + KeyF9 + KeyF10 + KeyF11 + KeyF12 + KeyF13 + KeyF14 + KeyF15 + KeyF16 + KeyF17 + KeyF18 + KeyF19 + KeyF20 + KeyF21 + KeyF22 + KeyF23 + KeyF24 + KeyF25 + KeyKP0 + KeyKP1 + KeyKP2 + KeyKP3 + KeyKP4 + KeyKP5 + KeyKP6 + KeyKP7 + KeyKP8 + KeyKP9 + KeyKPDecimal + KeyKPDivide + KeyKPMultiply + KeyKPSubtract + KeyKPAdd + KeyKPEnter + KeyKPEqual + KeyLeftShift + KeyLeftControl + KeyLeftAlt + KeyLeftSuper + KeyRightShift + KeyRightControl + KeyRightAlt + KeyRightSuper + KeyMenu + + // Aliases + KeyLast = KeyMenu +) + +var buttonNames = map[Button]string{ + MouseButton4: "MouseButton4", + MouseButton5: "MouseButton5", + MouseButton6: "MouseButton6", + MouseButton7: "MouseButton7", + MouseButton8: "MouseButton8", + MouseButtonLeft: "MouseButtonLeft", + MouseButtonRight: "MouseButtonRight", + MouseButtonMiddle: "MouseButtonMiddle", + KeyUnknown: "Unknown", + KeySpace: "Space", + KeyApostrophe: "Apostrophe", + KeyComma: "Comma", + KeyMinus: "Minus", + KeyPeriod: "Period", + KeySlash: "Slash", + Key0: "0", + Key1: "1", + Key2: "2", + Key3: "3", + Key4: "4", + Key5: "5", + Key6: "6", + Key7: "7", + Key8: "8", + Key9: "9", + KeySemicolon: "Semicolon", + KeyEqual: "Equal", + KeyA: "A", + KeyB: "B", + KeyC: "C", + KeyD: "D", + KeyE: "E", + KeyF: "F", + KeyG: "G", + KeyH: "H", + KeyI: "I", + KeyJ: "J", + KeyK: "K", + KeyL: "L", + KeyM: "M", + KeyN: "N", + KeyO: "O", + KeyP: "P", + KeyQ: "Q", + KeyR: "R", + KeyS: "S", + KeyT: "T", + KeyU: "U", + KeyV: "V", + KeyW: "W", + KeyX: "X", + KeyY: "Y", + KeyZ: "Z", + KeyLeftBracket: "LeftBracket", + KeyBackslash: "Backslash", + KeyRightBracket: "RightBracket", + KeyGraveAccent: "GraveAccent", + KeyWorld1: "World1", + KeyWorld2: "World2", + KeyEscape: "Escape", + KeyEnter: "Enter", + KeyTab: "Tab", + KeyBackspace: "Backspace", + KeyInsert: "Insert", + KeyDelete: "Delete", + KeyRight: "Right", + KeyLeft: "Left", + KeyDown: "Down", + KeyUp: "Up", + KeyPageUp: "PageUp", + KeyPageDown: "PageDown", + KeyHome: "Home", + KeyEnd: "End", + KeyCapsLock: "CapsLock", + KeyScrollLock: "ScrollLock", + KeyNumLock: "NumLock", + KeyPrintScreen: "PrintScreen", + KeyPause: "Pause", + KeyF1: "F1", + KeyF2: "F2", + KeyF3: "F3", + KeyF4: "F4", + KeyF5: "F5", + KeyF6: "F6", + KeyF7: "F7", + KeyF8: "F8", + KeyF9: "F9", + KeyF10: "F10", + KeyF11: "F11", + KeyF12: "F12", + KeyF13: "F13", + KeyF14: "F14", + KeyF15: "F15", + KeyF16: "F16", + KeyF17: "F17", + KeyF18: "F18", + KeyF19: "F19", + KeyF20: "F20", + KeyF21: "F21", + KeyF22: "F22", + KeyF23: "F23", + KeyF24: "F24", + KeyF25: "F25", + KeyKP0: "KP0", + KeyKP1: "KP1", + KeyKP2: "KP2", + KeyKP3: "KP3", + KeyKP4: "KP4", + KeyKP5: "KP5", + KeyKP6: "KP6", + KeyKP7: "KP7", + KeyKP8: "KP8", + KeyKP9: "KP9", + KeyKPDecimal: "KPDecimal", + KeyKPDivide: "KPDivide", + KeyKPMultiply: "KPMultiply", + KeyKPSubtract: "KPSubtract", + KeyKPAdd: "KPAdd", + KeyKPEnter: "KPEnter", + KeyKPEqual: "KPEqual", + KeyLeftShift: "LeftShift", + KeyLeftControl: "LeftControl", + KeyLeftAlt: "LeftAlt", + KeyLeftSuper: "LeftSuper", + KeyRightShift: "RightShift", + KeyRightControl: "RightControl", + KeyRightAlt: "RightAlt", + KeyRightSuper: "RightSuper", + KeyMenu: "Menu", +} + +// Joystick is a joystick or controller (gamepad). +type Joystick int + +// String returns a human-readable string describing the Joystick. +func (j Joystick) String() string { + name, ok := joystickNames[j] + if !ok { + return "Invalid" + } + return name +} + +// List all of the joysticks. +const ( + Joystick1 Joystick = iota + Joystick2 + Joystick3 + Joystick4 + Joystick5 + Joystick6 + Joystick7 + Joystick8 + Joystick9 + Joystick10 + Joystick11 + Joystick12 + Joystick13 + Joystick14 + Joystick15 + Joystick16 + + // Aliases + JoystickLast = Joystick16 +) + +var joystickNames = map[Joystick]string{ + Joystick1: "Joystick1", + Joystick2: "Joystick2", + Joystick3: "Joystick3", + Joystick4: "Joystick4", + Joystick5: "Joystick5", + Joystick6: "Joystick6", + Joystick7: "Joystick7", + Joystick8: "Joystick8", + Joystick9: "Joystick9", + Joystick10: "Joystick10", + Joystick11: "Joystick11", + Joystick12: "Joystick12", + Joystick13: "Joystick13", + Joystick14: "Joystick14", + Joystick15: "Joystick15", + Joystick16: "Joystick16", +} + +// GamepadAxis corresponds to a gamepad axis. +type GamepadAxis int + +// String returns a human-readable string describing the GamepadAxis. +func (ga GamepadAxis) String() string { + name, ok := gamepadAxisNames[ga] + if !ok { + return "Invalid" + } + return name +} + +// Gamepad axis IDs. +const ( + AxisLeftX GamepadAxis = iota + AxisLeftY + AxisRightX + AxisRightY + AxisLeftTrigger + AxisRightTrigger + + // Aliases + AxisLast = AxisRightTrigger +) + +var gamepadAxisNames = map[GamepadAxis]string{ + AxisLeftX: "AxisLeftX", + AxisLeftY: "AxisLeftY", + AxisRightX: "AxisRightX", + AxisRightY: "AxisRightY", + AxisLeftTrigger: "AxisLeftTrigger", + AxisRightTrigger: "AxisRightTrigger", +} + +// GamepadButton corresponds to a gamepad button. +type GamepadButton int + +// String returns a human-readable string describing the GamepadButton. +func (gb GamepadButton) String() string { + name, ok := gamepadButtonNames[gb] + if !ok { + return "Invalid" + } + return name +} + +// Gamepad button IDs. +const ( + GamepadA GamepadButton = iota + GamepadB + GamepadX + GamepadY + GamepadLeftBumper + GamepadRightBumper + GamepadBack + GamepadStart + GamepadGuide + GamepadLeftThumb + GamepadRightThumb + GamepadDpadUp + GamepadDpadRight + GamepadDpadDown + GamepadDpadLeft + + // Aliases + GamepadCross = GamepadA + GamepadCircle = GamepadB + GamepadSquare = GamepadX + GamepadTriangle = GamepadY + GamepadLast = GamepadDpadLeft +) + +var gamepadButtonNames = map[GamepadButton]string{ + GamepadA: "GamepadA", + GamepadB: "GamepadB", + GamepadX: "GamepadX", + GamepadY: "GamepadY", + GamepadLeftBumper: "GamepadLeftBumper", + GamepadRightBumper: "GamepadRightBumper", + GamepadBack: "GamepadBack", + GamepadStart: "GamepadStart", + GamepadGuide: "GamepadGuide", + GamepadLeftThumb: "GamepadLeftThumb", + GamepadRightThumb: "GamepadRightThumb", + GamepadDpadUp: "GamepadDpadUp", + GamepadDpadRight: "GamepadDpadRight", + GamepadDpadDown: "GamepadDpadDown", + GamepadDpadLeft: "GamepadDpadLeft", +}