diff --git a/backends/opengl/input.go b/backends/opengl/input.go index 250a250..474e5fe 100644 --- a/backends/opengl/input.go +++ b/backends/opengl/input.go @@ -71,6 +71,12 @@ func (w *Window) Typed() string { return w.currInp.typed } +var actionMapping = map[glfw.Action]pixel.Action{ + glfw.Release: pixel.Release, + glfw.Press: pixel.Press, + glfw.Repeat: pixel.Repeat, +} + var mouseButtonMapping = map[glfw.MouseButton]pixel.Button{ glfw.MouseButton1: pixel.MouseButton1, glfw.MouseButton2: pixel.MouseButton2, @@ -83,7 +89,7 @@ var mouseButtonMapping = map[glfw.MouseButton]pixel.Button{ } var keyButtonMapping = map[glfw.Key]pixel.Button{ - glfw.KeyUnknown: pixel.ButtonUnknown, + glfw.KeyUnknown: pixel.UnknownButton, glfw.KeySpace: pixel.KeySpace, glfw.KeyApostrophe: pixel.KeyApostrophe, glfw.KeyComma: pixel.KeyComma, diff --git a/backends/opengl/joystick.go b/backends/opengl/joystick.go index fbcce9c..8a00ee2 100644 --- a/backends/opengl/joystick.go +++ b/backends/opengl/joystick.go @@ -34,7 +34,7 @@ var _ = map[glfw.GamepadAxis]pixel.GamepadAxis{ glfw.AxisRightTrigger: pixel.AxisRightTrigger, } -var _ = map[glfw.GamepadButton]pixel.GamepadButton{ +var gamepadButtonMapping = map[glfw.GamepadButton]pixel.GamepadButton{ glfw.ButtonA: pixel.GamepadA, glfw.ButtonB: pixel.GamepadB, glfw.ButtonX: pixel.GamepadX, @@ -128,10 +128,10 @@ func (w *Window) updateJoystickInput() { if joystick.IsGamepad() { gamepadInputs := joystick.GetGamepadState() - w.tempJoy.buttons[js] = gamepadInputs.Buttons[:] + w.tempJoy.buttons[js] = convertGamepadButtons(gamepadInputs.Buttons) w.tempJoy.axis[js] = gamepadInputs.Axes[:] } else { - w.tempJoy.buttons[js] = joystick.GetButtons() + w.tempJoy.buttons[js] = convertJoystickButtons(joystick.GetButtons()) w.tempJoy.axis[js] = joystick.GetAxes() } @@ -143,7 +143,7 @@ func (w *Window) updateJoystickInput() { w.tempJoy.name[js] = w.currJoy.name[js] } } else { - w.tempJoy.buttons[js] = []glfw.Action{} + w.tempJoy.buttons[js] = []pixel.Action{} w.tempJoy.axis[js] = []float32{} w.tempJoy.name[js] = "" } @@ -156,7 +156,7 @@ func (w *Window) updateJoystickInput() { type joystickState struct { connected [pixel.NumJoysticks]bool name [pixel.NumJoysticks]string - buttons [pixel.NumJoysticks][]glfw.Action + buttons [pixel.NumJoysticks][]pixel.Action axis [pixel.NumJoysticks][]float32 } @@ -166,7 +166,7 @@ func (js *joystickState) getButton(joystick pixel.Joystick, button int) bool { if js.buttons[joystick] == nil || button >= len(js.buttons[joystick]) || button < 0 { return false } - return js.buttons[joystick][byte(button)] == glfw.Press + return js.buttons[joystick][byte(button)] == pixel.Press } // Returns the value of a joystick axis, returning 0 if the button or joystick is invalid. @@ -177,3 +177,39 @@ func (js *joystickState) getAxis(joystick pixel.Joystick, axis int) float64 { } return float64(js.axis[joystick][axis]) } + +// Convert buttons from a GLFW gamepad mapping to pixel format +func convertGamepadButtons(buttons [glfw.ButtonLast + 1]glfw.Action) []pixel.Action { + pixelButtons := make([]pixel.Action, pixel.NumGamepadButtons) + for i, a := range buttons { + var action pixel.Action + var button pixel.GamepadButton + var ok bool + if action, ok = actionMapping[a]; !ok { + // Unknown action + continue + } + if button, ok = gamepadButtonMapping[glfw.GamepadButton(i)]; !ok { + // Unknown gamepad button + continue + } + pixelButtons[button] = action + } + return pixelButtons +} + +// Convert buttons of unknown length and arrangement to pixel format +// Used when a joystick has an unknown mapping in GLFW +func convertJoystickButtons(buttons []glfw.Action) []pixel.Action { + pixelButtons := make([]pixel.Action, len(buttons)) + for i, a := range buttons { + var action pixel.Action + var ok bool + if action, ok = actionMapping[a]; !ok { + // Unknown action + continue + } + pixelButtons[pixel.GamepadButton(i)] = action + } + return pixelButtons +} diff --git a/input.go b/input.go index 08c7c0d..1f0cff6 100644 --- a/input.go +++ b/input.go @@ -1,18 +1,42 @@ package pixel +type Action int + +// String returns a human-readable string describing the Button. +func (a Action) String() string { + name, ok := actionNames[a] + if !ok { + return actionNames[UnknownAction] + } + return name +} + +const UnknownAction Action = -1 +const ( + Release Action = iota + Press + Repeat +) + +var actionNames = map[Action]string{ + Release: "Release", + Press: "Press", + Repeat: "Repeat", + UnknownAction: "UnknownAction", +} + 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 buttonNames[UnknownButton] } return name } -const ButtonUnknown Button = -1 - +const UnknownButton Button = -1 const ( // List of all mouse buttons. MouseButton1 Button = iota @@ -147,7 +171,6 @@ const ( KeyMenu // Last iota - // NOTE: These will be unexported in the future when Window is move to the pixel package. NumButtons int = iota // Aliases @@ -157,7 +180,6 @@ const ( ) var buttonNames = map[Button]string{ - ButtonUnknown: "Unknown", MouseButton4: "MouseButton4", MouseButton5: "MouseButton5", MouseButton6: "MouseButton6", @@ -286,6 +308,7 @@ var buttonNames = map[Button]string{ KeyRightAlt: "RightAlt", KeyRightSuper: "RightSuper", KeyMenu: "Menu", + UnknownButton: "UnknownButton", } // Joystick is a joystick or controller (gamepad). @@ -295,12 +318,13 @@ type Joystick int func (j Joystick) String() string { name, ok := joystickNames[j] if !ok { - return "Invalid" + return joystickNames[UnknownJoystick] } return name } // List all of the joysticks. +const UnknownJoystick Joystick = -1 const ( Joystick1 Joystick = iota Joystick2 @@ -320,27 +344,27 @@ const ( Joystick16 // Last iota - // NOTE: These will be unexported in the future when Window is move to the pixel package. NumJoysticks int = iota ) 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", + 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", + UnknownJoystick: "UnknownJoystick", } // GamepadAxis corresponds to a gamepad axis. @@ -350,12 +374,13 @@ type GamepadAxis int func (ga GamepadAxis) String() string { name, ok := gamepadAxisNames[ga] if !ok { - return "Invalid" + return gamepadAxisNames[UnknownGamepadAxis] } return name } // Gamepad axis IDs. +const UnknownGamepadAxis GamepadAxis = -1 const ( AxisLeftX GamepadAxis = iota AxisLeftY @@ -365,17 +390,17 @@ const ( AxisRightTrigger // Last iota. - // NOTE: These will be unexported in the future when Window is move to the pixel package. NumAxes int = iota ) var gamepadAxisNames = map[GamepadAxis]string{ - AxisLeftX: "AxisLeftX", - AxisLeftY: "AxisLeftY", - AxisRightX: "AxisRightX", - AxisRightY: "AxisRightY", - AxisLeftTrigger: "AxisLeftTrigger", - AxisRightTrigger: "AxisRightTrigger", + AxisLeftX: "AxisLeftX", + AxisLeftY: "AxisLeftY", + AxisRightX: "AxisRightX", + AxisRightY: "AxisRightY", + AxisLeftTrigger: "AxisLeftTrigger", + AxisRightTrigger: "AxisRightTrigger", + UnknownGamepadAxis: "UnknownGamepadAxis", } // GamepadButton corresponds to a gamepad button. @@ -385,12 +410,13 @@ type GamepadButton int func (gb GamepadButton) String() string { name, ok := gamepadButtonNames[gb] if !ok { - return "Invalid" + return gamepadButtonNames[UnknownGampadButton] } return name } // Gamepad button IDs. +const UnknownGampadButton GamepadButton = -1 const ( GamepadA GamepadButton = iota GamepadB @@ -409,8 +435,7 @@ const ( GamepadDpadLeft // Last iota - numGamepadButtons - NumGamepadButtons = int(numGamepadButtons) + NumGamepadButtons int = iota // Aliases GamepadCross = GamepadA @@ -420,19 +445,20 @@ const ( ) 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", + 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", + UnknownGampadButton: "UnknownGampadButton", }