mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-11-10 04:57:26 +01:00
input: Make functions goroutine-safe (#192)
This commit is contained in:
parent
6efead974f
commit
6ac6b8e7c0
28
input.go
28
input.go
@ -19,48 +19,62 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
// IsKeyPressed returns a boolean indicating whether key is pressed.
|
// IsKeyPressed returns a boolean indicating whether key is pressed.
|
||||||
|
//
|
||||||
|
// This function is goroutine-safe.
|
||||||
func IsKeyPressed(key Key) bool {
|
func IsKeyPressed(key Key) bool {
|
||||||
return ui.IsKeyPressed(ui.Key(key))
|
return ui.CurrentInput().IsKeyPressed(ui.Key(key))
|
||||||
}
|
}
|
||||||
|
|
||||||
// CursorPosition returns a position of a mouse cursor.
|
// CursorPosition returns a position of a mouse cursor.
|
||||||
|
//
|
||||||
|
// This function is goroutine-safe.
|
||||||
func CursorPosition() (x, y int) {
|
func CursorPosition() (x, y int) {
|
||||||
return ui.CursorPosition()
|
return ui.CurrentInput().CursorPosition()
|
||||||
}
|
}
|
||||||
|
|
||||||
// IsMouseButtonPressed returns a boolean indicating whether mouseButton is pressed.
|
// IsMouseButtonPressed returns a boolean indicating whether mouseButton is pressed.
|
||||||
|
//
|
||||||
|
// This function is goroutine-safe.
|
||||||
func IsMouseButtonPressed(mouseButton MouseButton) bool {
|
func IsMouseButtonPressed(mouseButton MouseButton) bool {
|
||||||
return ui.IsMouseButtonPressed(ui.MouseButton(mouseButton))
|
return ui.CurrentInput().IsMouseButtonPressed(ui.MouseButton(mouseButton))
|
||||||
}
|
}
|
||||||
|
|
||||||
// GamepadAxisNum returns the number of axes of the gamepad.
|
// GamepadAxisNum returns the number of axes of the gamepad.
|
||||||
//
|
//
|
||||||
|
// This function is goroutine-safe.
|
||||||
|
//
|
||||||
// NOTE: Gamepad API is available only on desktops, Chrome and Firefox.
|
// NOTE: Gamepad API is available only on desktops, Chrome and Firefox.
|
||||||
// To use this API, browsers might require rebooting the browser.
|
// To use this API, browsers might require rebooting the browser.
|
||||||
func GamepadAxisNum(id int) int {
|
func GamepadAxisNum(id int) int {
|
||||||
return ui.GamepadAxisNum(id)
|
return ui.CurrentInput().GamepadAxisNum(id)
|
||||||
}
|
}
|
||||||
|
|
||||||
// GamepadAxis returns the float value [-1.0 - 1.0] of the axis.
|
// GamepadAxis returns the float value [-1.0 - 1.0] of the axis.
|
||||||
//
|
//
|
||||||
|
// This function is goroutine-safe.
|
||||||
|
//
|
||||||
// NOTE: Gamepad API is available only on desktops, Chrome and Firefox.
|
// NOTE: Gamepad API is available only on desktops, Chrome and Firefox.
|
||||||
// To use this API, browsers might require rebooting the browser.
|
// To use this API, browsers might require rebooting the browser.
|
||||||
func GamepadAxis(id int, axis int) float64 {
|
func GamepadAxis(id int, axis int) float64 {
|
||||||
return ui.GamepadAxis(id, axis)
|
return ui.CurrentInput().GamepadAxis(id, axis)
|
||||||
}
|
}
|
||||||
|
|
||||||
// GamepadButtonNum returns the number of the buttons of the gamepad.
|
// GamepadButtonNum returns the number of the buttons of the gamepad.
|
||||||
//
|
//
|
||||||
|
// This function is goroutine-safe.
|
||||||
|
//
|
||||||
// NOTE: Gamepad API is available only on desktops, Chrome and Firefox.
|
// NOTE: Gamepad API is available only on desktops, Chrome and Firefox.
|
||||||
// To use this API, browsers might require rebooting the browser.
|
// To use this API, browsers might require rebooting the browser.
|
||||||
func GamepadButtonNum(id int) int {
|
func GamepadButtonNum(id int) int {
|
||||||
return ui.GamepadButtonNum(id)
|
return ui.CurrentInput().GamepadButtonNum(id)
|
||||||
}
|
}
|
||||||
|
|
||||||
// IsGamepadButtonPressed returns the boolean indicating the buttons is pressed or not.
|
// IsGamepadButtonPressed returns the boolean indicating the buttons is pressed or not.
|
||||||
//
|
//
|
||||||
|
// This function is goroutine-safe.
|
||||||
|
//
|
||||||
// NOTE: Gamepad API is available only on desktops, Chrome and Firefox.
|
// NOTE: Gamepad API is available only on desktops, Chrome and Firefox.
|
||||||
// To use this API, browsers might require rebooting the browser.
|
// To use this API, browsers might require rebooting the browser.
|
||||||
func IsGamepadButtonPressed(id int, button GamepadButton) bool {
|
func IsGamepadButtonPressed(id int, button GamepadButton) bool {
|
||||||
return ui.IsGamepadButtonPressed(id, ui.GamepadButton(button))
|
return ui.CurrentInput().IsGamepadButtonPressed(id, ui.GamepadButton(button))
|
||||||
}
|
}
|
||||||
|
@ -14,54 +14,19 @@
|
|||||||
|
|
||||||
package ui
|
package ui
|
||||||
|
|
||||||
func IsKeyPressed(key Key) bool {
|
import (
|
||||||
return currentInput.keyPressed[key]
|
"sync"
|
||||||
}
|
)
|
||||||
|
|
||||||
func CursorPosition() (x, y int) {
|
var currentInput = &Input{}
|
||||||
return currentInput.cursorX, currentInput.cursorY
|
|
||||||
}
|
|
||||||
|
|
||||||
func IsMouseButtonPressed(button MouseButton) bool {
|
type Input struct {
|
||||||
return currentInput.mouseButtonPressed[button]
|
|
||||||
}
|
|
||||||
|
|
||||||
func GamepadAxisNum(id int) int {
|
|
||||||
if len(currentInput.gamepads) <= id {
|
|
||||||
return 0
|
|
||||||
}
|
|
||||||
return currentInput.gamepads[id].axisNum
|
|
||||||
}
|
|
||||||
|
|
||||||
func GamepadAxis(id int, axis int) float64 {
|
|
||||||
if len(currentInput.gamepads) <= id {
|
|
||||||
return 0
|
|
||||||
}
|
|
||||||
return currentInput.gamepads[id].axes[axis]
|
|
||||||
}
|
|
||||||
|
|
||||||
func GamepadButtonNum(id int) int {
|
|
||||||
if len(currentInput.gamepads) <= id {
|
|
||||||
return 0
|
|
||||||
}
|
|
||||||
return currentInput.gamepads[id].buttonNum
|
|
||||||
}
|
|
||||||
|
|
||||||
func IsGamepadButtonPressed(id int, button GamepadButton) bool {
|
|
||||||
if len(currentInput.gamepads) <= id {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
return currentInput.gamepads[id].buttonPressed[button]
|
|
||||||
}
|
|
||||||
|
|
||||||
var currentInput input
|
|
||||||
|
|
||||||
type input struct {
|
|
||||||
keyPressed [256]bool
|
keyPressed [256]bool
|
||||||
mouseButtonPressed [256]bool
|
mouseButtonPressed [256]bool
|
||||||
cursorX int
|
cursorX int
|
||||||
cursorY int
|
cursorY int
|
||||||
gamepads [16]gamePad
|
gamepads [16]gamePad
|
||||||
|
m sync.RWMutex
|
||||||
}
|
}
|
||||||
|
|
||||||
type gamePad struct {
|
type gamePad struct {
|
||||||
@ -70,3 +35,61 @@ type gamePad struct {
|
|||||||
buttonNum int
|
buttonNum int
|
||||||
buttonPressed [256]bool
|
buttonPressed [256]bool
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func CurrentInput() *Input {
|
||||||
|
return currentInput
|
||||||
|
}
|
||||||
|
|
||||||
|
func (i *Input) IsKeyPressed(key Key) bool {
|
||||||
|
i.m.RLock()
|
||||||
|
defer i.m.RUnlock()
|
||||||
|
return i.keyPressed[key]
|
||||||
|
}
|
||||||
|
|
||||||
|
func (i *Input) CursorPosition() (x, y int) {
|
||||||
|
i.m.RLock()
|
||||||
|
defer i.m.RUnlock()
|
||||||
|
return i.cursorX, currentInput.cursorY
|
||||||
|
}
|
||||||
|
|
||||||
|
func (i *Input) IsMouseButtonPressed(button MouseButton) bool {
|
||||||
|
i.m.RLock()
|
||||||
|
defer i.m.RUnlock()
|
||||||
|
return i.mouseButtonPressed[button]
|
||||||
|
}
|
||||||
|
|
||||||
|
func (i *Input) GamepadAxisNum(id int) int {
|
||||||
|
i.m.RLock()
|
||||||
|
defer i.m.RUnlock()
|
||||||
|
if len(i.gamepads) <= id {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
return i.gamepads[id].axisNum
|
||||||
|
}
|
||||||
|
|
||||||
|
func (i *Input) GamepadAxis(id int, axis int) float64 {
|
||||||
|
i.m.RLock()
|
||||||
|
defer i.m.RUnlock()
|
||||||
|
if len(i.gamepads) <= id {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
return i.gamepads[id].axes[axis]
|
||||||
|
}
|
||||||
|
|
||||||
|
func (i *Input) GamepadButtonNum(id int) int {
|
||||||
|
i.m.RLock()
|
||||||
|
defer i.m.RUnlock()
|
||||||
|
if len(i.gamepads) <= id {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
return i.gamepads[id].buttonNum
|
||||||
|
}
|
||||||
|
|
||||||
|
func (i *Input) IsGamepadButtonPressed(id int, button GamepadButton) bool {
|
||||||
|
i.m.RLock()
|
||||||
|
defer i.m.RUnlock()
|
||||||
|
if len(i.gamepads) <= id {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return i.gamepads[id].buttonPressed[button]
|
||||||
|
}
|
||||||
|
@ -21,17 +21,16 @@ import (
|
|||||||
"math"
|
"math"
|
||||||
)
|
)
|
||||||
|
|
||||||
func updateInput(window *glfw.Window, scale int) error {
|
|
||||||
return currentInput.update(window, scale)
|
|
||||||
}
|
|
||||||
|
|
||||||
var glfwMouseButtonToMouseButton = map[glfw.MouseButton]MouseButton{
|
var glfwMouseButtonToMouseButton = map[glfw.MouseButton]MouseButton{
|
||||||
glfw.MouseButtonLeft: MouseButtonLeft,
|
glfw.MouseButtonLeft: MouseButtonLeft,
|
||||||
glfw.MouseButtonRight: MouseButtonRight,
|
glfw.MouseButtonRight: MouseButtonRight,
|
||||||
glfw.MouseButtonMiddle: MouseButtonMiddle,
|
glfw.MouseButtonMiddle: MouseButtonMiddle,
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *input) update(window *glfw.Window, scale int) error {
|
func (i *Input) update(window *glfw.Window, scale int) error {
|
||||||
|
i.m.Lock()
|
||||||
|
defer i.m.Unlock()
|
||||||
|
|
||||||
for g, e := range glfwKeyCodeToKey {
|
for g, e := range glfwKeyCodeToKey {
|
||||||
i.keyPressed[e] = window.GetKey(g) == glfw.Press
|
i.keyPressed[e] = window.GetKey(g) == glfw.Press
|
||||||
}
|
}
|
||||||
|
@ -20,11 +20,9 @@ import (
|
|||||||
"github.com/gopherjs/gopherjs/js"
|
"github.com/gopherjs/gopherjs/js"
|
||||||
)
|
)
|
||||||
|
|
||||||
func CurrentInput() *input {
|
func (i *Input) KeyDown(key int) {
|
||||||
return ¤tInput
|
i.m.Lock()
|
||||||
}
|
defer i.m.Unlock()
|
||||||
|
|
||||||
func (i *input) KeyDown(key int) {
|
|
||||||
k, ok := keyCodeToKey[key]
|
k, ok := keyCodeToKey[key]
|
||||||
if !ok {
|
if !ok {
|
||||||
return
|
return
|
||||||
@ -32,7 +30,9 @@ func (i *input) KeyDown(key int) {
|
|||||||
i.keyPressed[k] = true
|
i.keyPressed[k] = true
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *input) KeyUp(key int) {
|
func (i *Input) KeyUp(key int) {
|
||||||
|
i.m.Lock()
|
||||||
|
defer i.m.Unlock()
|
||||||
k, ok := keyCodeToKey[key]
|
k, ok := keyCodeToKey[key]
|
||||||
if !ok {
|
if !ok {
|
||||||
return
|
return
|
||||||
@ -40,7 +40,9 @@ func (i *input) KeyUp(key int) {
|
|||||||
i.keyPressed[k] = false
|
i.keyPressed[k] = false
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *input) MouseDown(button int) {
|
func (i *Input) MouseDown(button int) {
|
||||||
|
i.m.Lock()
|
||||||
|
defer i.m.Unlock()
|
||||||
p := &i.mouseButtonPressed
|
p := &i.mouseButtonPressed
|
||||||
switch button {
|
switch button {
|
||||||
case 0:
|
case 0:
|
||||||
@ -52,7 +54,9 @@ func (i *input) MouseDown(button int) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *input) MouseUp(button int) {
|
func (i *Input) MouseUp(button int) {
|
||||||
|
i.m.Lock()
|
||||||
|
defer i.m.Unlock()
|
||||||
p := &i.mouseButtonPressed
|
p := &i.mouseButtonPressed
|
||||||
switch button {
|
switch button {
|
||||||
case 0:
|
case 0:
|
||||||
@ -64,11 +68,15 @@ func (i *input) MouseUp(button int) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *input) SetMouseCursor(x, y int) {
|
func (i *Input) SetMouseCursor(x, y int) {
|
||||||
|
i.m.Lock()
|
||||||
|
defer i.m.Unlock()
|
||||||
i.cursorX, i.cursorY = x, y
|
i.cursorX, i.cursorY = x, y
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *input) UpdateGamepads() {
|
func (i *Input) UpdateGamepads() {
|
||||||
|
i.m.Lock()
|
||||||
|
defer i.m.Unlock()
|
||||||
nav := js.Global.Get("navigator")
|
nav := js.Global.Get("navigator")
|
||||||
if nav.Get("getGamepads") == js.Undefined {
|
if nav.Get("getGamepads") == js.Undefined {
|
||||||
return
|
return
|
||||||
|
@ -153,7 +153,7 @@ func (u *userInterface) actualScreenScale() int {
|
|||||||
|
|
||||||
func (u *userInterface) pollEvents() error {
|
func (u *userInterface) pollEvents() error {
|
||||||
glfw.PollEvents()
|
glfw.PollEvents()
|
||||||
return updateInput(u.window, u.windowScale())
|
return currentInput.update(u.window, u.windowScale())
|
||||||
}
|
}
|
||||||
|
|
||||||
func (u *userInterface) doEvents() error {
|
func (u *userInterface) doEvents() error {
|
||||||
|
Loading…
Reference in New Issue
Block a user