Skip to content

Commit

Permalink
Merge pull request gopxl#76 from bhperry/pixel-actions
Browse files Browse the repository at this point in the history
Pixel actions
  • Loading branch information
bhperry authored Oct 16, 2023
2 parents 1dbf2aa + 9c8ed87 commit 12fc9f5
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 56 deletions.
8 changes: 7 additions & 1 deletion 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 All @@ -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,
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
}
124 changes: 75 additions & 49 deletions input.go
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand All @@ -157,7 +180,6 @@ const (
)

var buttonNames = map[Button]string{
ButtonUnknown: "Unknown",
MouseButton4: "MouseButton4",
MouseButton5: "MouseButton5",
MouseButton6: "MouseButton6",
Expand Down Expand Up @@ -286,6 +308,7 @@ var buttonNames = map[Button]string{
KeyRightAlt: "RightAlt",
KeyRightSuper: "RightSuper",
KeyMenu: "Menu",
UnknownButton: "UnknownButton",
}

// Joystick is a joystick or controller (gamepad).
Expand All @@ -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
Expand All @@ -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.
Expand All @@ -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
Expand All @@ -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.
Expand All @@ -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
Expand All @@ -409,8 +435,7 @@ const (
GamepadDpadLeft

// Last iota
numGamepadButtons
NumGamepadButtons = int(numGamepadButtons)
NumGamepadButtons int = iota

// Aliases
GamepadCross = GamepadA
Expand All @@ -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",
}

0 comments on commit 12fc9f5

Please sign in to comment.