Skip to content

Commit

Permalink
map pixel.Action from glfw.Action
Browse files Browse the repository at this point in the history
  • Loading branch information
bhperry committed Oct 14, 2023
1 parent 2feb51c commit 9e5a779
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 6 deletions.
6 changes: 6 additions & 0 deletions backends/opengl/input.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
48 changes: 42 additions & 6 deletions backends/opengl/joystick.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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()
}

Expand All @@ -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] = ""
}
Expand All @@ -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
}

Expand All @@ -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.
Expand All @@ -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
}
23 changes: 23 additions & 0 deletions input.go
Original file line number Diff line number Diff line change
@@ -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.
Expand Down

0 comments on commit 9e5a779

Please sign in to comment.