From 2feb51cc9e23156b532e6d927baedb3985c04582 Mon Sep 17 00:00:00 2001 From: Ben Perry Date: Fri, 13 Oct 2023 19:14:00 -0500 Subject: [PATCH 1/4] More descriptive strings for invalid input type values --- input.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/input.go b/input.go index 08c7c0d..002b369 100644 --- a/input.go +++ b/input.go @@ -6,7 +6,7 @@ type Button int func (b Button) String() string { name, ok := buttonNames[b] if !ok { - return "Invalid" + return "InvalidButton" } return name } @@ -295,7 +295,7 @@ type Joystick int func (j Joystick) String() string { name, ok := joystickNames[j] if !ok { - return "Invalid" + return "InvalidJoystick" } return name } @@ -350,7 +350,7 @@ type GamepadAxis int func (ga GamepadAxis) String() string { name, ok := gamepadAxisNames[ga] if !ok { - return "Invalid" + return "InvalidGamepadAxis" } return name } @@ -385,7 +385,7 @@ type GamepadButton int func (gb GamepadButton) String() string { name, ok := gamepadButtonNames[gb] if !ok { - return "Invalid" + return "InvalidGamepadButton" } return name } From 9e5a77990c996ebe7a0e084b82f3c66c75f41452 Mon Sep 17 00:00:00 2001 From: Ben Perry Date: Fri, 13 Oct 2023 20:31:27 -0500 Subject: [PATCH 2/4] map pixel.Action from glfw.Action --- backends/opengl/input.go | 6 +++++ backends/opengl/joystick.go | 48 ++++++++++++++++++++++++++++++++----- input.go | 23 ++++++++++++++++++ 3 files changed, 71 insertions(+), 6 deletions(-) diff --git a/backends/opengl/input.go b/backends/opengl/input.go index 250a250..7290bb1 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, 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 002b369..3a656d2 100644 --- a/input.go +++ b/input.go @@ -1,5 +1,28 @@ 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 "InvalidAction" + } + return name +} + +const ( + Release Action = iota + Press + Repeat +) + +var actionNames = map[Action]string{ + Release: "Release", + Press: "Press", + Repeat: "Repeat", +} + type Button int // String returns a human-readable string describing the Button. From 605f1adef63d4fc91ec2cc48710398a728610860 Mon Sep 17 00:00:00 2001 From: Ben Perry Date: Sat, 14 Oct 2023 00:06:51 -0500 Subject: [PATCH 3/4] missed one --- input.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/input.go b/input.go index 3a656d2..cdb5b4b 100644 --- a/input.go +++ b/input.go @@ -432,8 +432,7 @@ const ( GamepadDpadLeft // Last iota - numGamepadButtons - NumGamepadButtons = int(numGamepadButtons) + NumGamepadButtons int = iota // Aliases GamepadCross = GamepadA From 9c8ed872745b58becfaaddd0c081f73ba54d5324 Mon Sep 17 00:00:00 2001 From: Ben Perry Date: Sat, 14 Oct 2023 00:09:03 -0500 Subject: [PATCH 4/4] Unknown* defs for all input types --- backends/opengl/input.go | 2 +- input.go | 106 ++++++++++++++++++++------------------- 2 files changed, 56 insertions(+), 52 deletions(-) diff --git a/backends/opengl/input.go b/backends/opengl/input.go index 7290bb1..474e5fe 100644 --- a/backends/opengl/input.go +++ b/backends/opengl/input.go @@ -89,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/input.go b/input.go index cdb5b4b..1f0cff6 100644 --- a/input.go +++ b/input.go @@ -6,11 +6,12 @@ type Action int func (a Action) String() string { name, ok := actionNames[a] if !ok { - return "InvalidAction" + return actionNames[UnknownAction] } return name } +const UnknownAction Action = -1 const ( Release Action = iota Press @@ -18,9 +19,10 @@ const ( ) var actionNames = map[Action]string{ - Release: "Release", - Press: "Press", - Repeat: "Repeat", + Release: "Release", + Press: "Press", + Repeat: "Repeat", + UnknownAction: "UnknownAction", } type Button int @@ -29,13 +31,12 @@ type Button int func (b Button) String() string { name, ok := buttonNames[b] if !ok { - return "InvalidButton" + return buttonNames[UnknownButton] } return name } -const ButtonUnknown Button = -1 - +const UnknownButton Button = -1 const ( // List of all mouse buttons. MouseButton1 Button = iota @@ -170,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 @@ -180,7 +180,6 @@ const ( ) var buttonNames = map[Button]string{ - ButtonUnknown: "Unknown", MouseButton4: "MouseButton4", MouseButton5: "MouseButton5", MouseButton6: "MouseButton6", @@ -309,6 +308,7 @@ var buttonNames = map[Button]string{ KeyRightAlt: "RightAlt", KeyRightSuper: "RightSuper", KeyMenu: "Menu", + UnknownButton: "UnknownButton", } // Joystick is a joystick or controller (gamepad). @@ -318,12 +318,13 @@ type Joystick int func (j Joystick) String() string { name, ok := joystickNames[j] if !ok { - return "InvalidJoystick" + return joystickNames[UnknownJoystick] } return name } // List all of the joysticks. +const UnknownJoystick Joystick = -1 const ( Joystick1 Joystick = iota Joystick2 @@ -343,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. @@ -373,12 +374,13 @@ type GamepadAxis int func (ga GamepadAxis) String() string { name, ok := gamepadAxisNames[ga] if !ok { - return "InvalidGamepadAxis" + return gamepadAxisNames[UnknownGamepadAxis] } return name } // Gamepad axis IDs. +const UnknownGamepadAxis GamepadAxis = -1 const ( AxisLeftX GamepadAxis = iota AxisLeftY @@ -388,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. @@ -408,12 +410,13 @@ type GamepadButton int func (gb GamepadButton) String() string { name, ok := gamepadButtonNames[gb] if !ok { - return "InvalidGamepadButton" + return gamepadButtonNames[UnknownGampadButton] } return name } // Gamepad button IDs. +const UnknownGampadButton GamepadButton = -1 const ( GamepadA GamepadButton = iota GamepadB @@ -442,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", }