-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #86 from bhperry/bhpery/input-handler-internal
Input handler internal
- Loading branch information
Showing
6 changed files
with
139 additions
and
182 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package internal | ||
|
||
import "github.com/gopxl/pixel/v2" | ||
|
||
type InputState struct { | ||
Mouse pixel.Vec | ||
Buttons [pixel.NumButtons]bool | ||
Repeat [pixel.NumButtons]bool | ||
Scroll pixel.Vec | ||
Typed string | ||
} | ||
|
||
type InputHandler struct { | ||
Prev, Curr, temp InputState | ||
|
||
PressEvents, tempPressEvents [pixel.NumButtons]bool | ||
ReleaseEvents, tempReleaseEvents [pixel.NumButtons]bool | ||
|
||
MouseInsideWindow bool | ||
} | ||
|
||
// SetMousePosition overrides the mouse position | ||
// Called when the mouse is set to a point in the backend Window | ||
func (ih *InputHandler) SetMousePosition(pos pixel.Vec) { | ||
ih.Prev.Mouse = pos | ||
ih.Curr.Mouse = pos | ||
ih.temp.Mouse = pos | ||
} | ||
|
||
// ButtonEvent sets the action state of a button for the next update | ||
func (ih *InputHandler) ButtonEvent(button pixel.Button, action pixel.Action) { | ||
switch action { | ||
case pixel.Press: | ||
ih.tempPressEvents[button] = true | ||
ih.temp.Buttons[button] = true | ||
case pixel.Release: | ||
ih.tempReleaseEvents[button] = true | ||
ih.temp.Buttons[button] = false | ||
case pixel.Repeat: | ||
ih.temp.Repeat[button] = true | ||
} | ||
} | ||
|
||
// MouseMoveEvent sets the mouse position for the next update | ||
func (ih *InputHandler) MouseMoveEvent(pos pixel.Vec) { | ||
ih.temp.Mouse = pos | ||
} | ||
|
||
// MouseScrollEvent adds to the scroll offset for the next update | ||
func (ih *InputHandler) MouseScrollEvent(x, y float64) { | ||
ih.temp.Scroll.X += x | ||
ih.temp.Scroll.Y += y | ||
} | ||
|
||
// MouseEnteredEvent is called when the mouse enters or leaves the window | ||
func (ih *InputHandler) MouseEnteredEvent(entered bool) { | ||
ih.MouseInsideWindow = entered | ||
} | ||
|
||
// CharEvent adds to the typed string for the next update | ||
func (ih *InputHandler) CharEvent(r rune) { | ||
ih.temp.Typed += string(r) | ||
} | ||
|
||
func (ih *InputHandler) Update() { | ||
ih.Prev = ih.Curr | ||
ih.Curr = ih.temp | ||
|
||
ih.PressEvents = ih.tempPressEvents | ||
ih.ReleaseEvents = ih.tempReleaseEvents | ||
|
||
// Clear last frame's temporary status | ||
ih.tempPressEvents = [pixel.NumButtons]bool{} | ||
ih.tempReleaseEvents = [pixel.NumButtons]bool{} | ||
ih.temp.Repeat = [pixel.NumButtons]bool{} | ||
ih.temp.Scroll = pixel.ZV | ||
ih.temp.Typed = "" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package internal | ||
|
||
import "github.com/gopxl/pixel/v2" | ||
|
||
type JoystickState struct { | ||
Connected [pixel.NumJoysticks]bool | ||
Name [pixel.NumJoysticks]string | ||
Buttons [pixel.NumJoysticks][]pixel.Action | ||
Axis [pixel.NumJoysticks][]float32 | ||
} | ||
|
||
// Returns if a button on a joystick is down, returning false if the button or joystick is invalid. | ||
func (js *JoystickState) GetButton(joystick pixel.Joystick, button pixel.GamepadButton) bool { | ||
// Check that the joystick and button is valid, return false by default | ||
if js.Buttons[joystick] == nil || int(button) >= len(js.Buttons[joystick]) || button < 0 { | ||
return false | ||
} | ||
return js.Buttons[joystick][button] == pixel.Press | ||
} | ||
|
||
// Returns the value of a joystick axis, returning 0 if the button or joystick is invalid. | ||
func (js *JoystickState) GetAxis(joystick pixel.Joystick, axis pixel.GamepadAxis) float64 { | ||
// Check that the joystick and axis is valid, return 0 by default. | ||
if js.Axis[joystick] == nil || int(axis) >= len(js.Axis[joystick]) || axis < 0 { | ||
return 0 | ||
} | ||
return float64(js.Axis[joystick][axis]) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.