From c36dd3df52cf29f5a59f344ee2068365f7cf3d3e Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Fri, 20 May 2016 00:15:05 +0900 Subject: [PATCH] ui: Change Input to be an interface --- internal/ui/input.go | 32 +++++++++++++++++++++----------- internal/ui/input_glfw.go | 2 +- internal/ui/input_js.go | 12 ++++++------ internal/ui/ui_js.go | 16 ++++++++-------- 4 files changed, 36 insertions(+), 26 deletions(-) diff --git a/internal/ui/input.go b/internal/ui/input.go index cbb263ffc..d96905de7 100644 --- a/internal/ui/input.go +++ b/internal/ui/input.go @@ -18,9 +18,19 @@ import ( "sync" ) -var currentInput = &Input{} +var currentInput = &input{} -type Input struct { +type Input interface { + IsKeyPressed(key Key) bool + IsMouseButtonPressed(button MouseButton) bool + CursorPosition() (x, y int) + GamepadAxis(id int, axis int) float64 + GamepadAxisNum(id int) int + GamepadButtonNum(id int) int + IsGamepadButtonPressed(id int, button GamepadButton) bool +} + +type input struct { keyPressed [256]bool mouseButtonPressed [256]bool cursorX int @@ -36,29 +46,29 @@ type gamePad struct { buttonPressed [256]bool } -func CurrentInput() *Input { +func CurrentInput() Input { return currentInput } -func (i *Input) IsKeyPressed(key Key) bool { +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) { +func (i *input) CursorPosition() (x, y int) { i.m.RLock() defer i.m.RUnlock() - return i.cursorX, currentInput.cursorY + return i.cursorX, i.cursorY } -func (i *Input) IsMouseButtonPressed(button MouseButton) bool { +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 { +func (i *input) GamepadAxisNum(id int) int { i.m.RLock() defer i.m.RUnlock() if len(i.gamepads) <= id { @@ -67,7 +77,7 @@ func (i *Input) GamepadAxisNum(id int) int { return i.gamepads[id].axisNum } -func (i *Input) GamepadAxis(id int, axis int) float64 { +func (i *input) GamepadAxis(id int, axis int) float64 { i.m.RLock() defer i.m.RUnlock() if len(i.gamepads) <= id { @@ -76,7 +86,7 @@ func (i *Input) GamepadAxis(id int, axis int) float64 { return i.gamepads[id].axes[axis] } -func (i *Input) GamepadButtonNum(id int) int { +func (i *input) GamepadButtonNum(id int) int { i.m.RLock() defer i.m.RUnlock() if len(i.gamepads) <= id { @@ -85,7 +95,7 @@ func (i *Input) GamepadButtonNum(id int) int { return i.gamepads[id].buttonNum } -func (i *Input) IsGamepadButtonPressed(id int, button GamepadButton) bool { +func (i *input) IsGamepadButtonPressed(id int, button GamepadButton) bool { i.m.RLock() defer i.m.RUnlock() if len(i.gamepads) <= id { diff --git a/internal/ui/input_glfw.go b/internal/ui/input_glfw.go index 7ff0c1907..2a7f92f86 100644 --- a/internal/ui/input_glfw.go +++ b/internal/ui/input_glfw.go @@ -28,7 +28,7 @@ var glfwMouseButtonToMouseButton = map[glfw.MouseButton]MouseButton{ 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() diff --git a/internal/ui/input_js.go b/internal/ui/input_js.go index 7d7a4604d..36f5b1dee 100644 --- a/internal/ui/input_js.go +++ b/internal/ui/input_js.go @@ -20,7 +20,7 @@ import ( "github.com/gopherjs/gopherjs/js" ) -func (i *Input) KeyDown(key int) { +func (i *input) keyDown(key int) { i.m.Lock() defer i.m.Unlock() k, ok := keyCodeToKey[key] @@ -30,7 +30,7 @@ func (i *Input) KeyDown(key int) { 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] @@ -40,7 +40,7 @@ func (i *Input) KeyUp(key int) { 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 @@ -54,7 +54,7 @@ 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 @@ -68,13 +68,13 @@ 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 } -func (i *Input) UpdateGamepads() { +func (i *input) updateGamepads() { i.m.Lock() defer i.m.Unlock() nav := js.Global.Get("navigator") diff --git a/internal/ui/ui_js.go b/internal/ui/ui_js.go index 74a0740ae..073d4cc8a 100644 --- a/internal/ui/ui_js.go +++ b/internal/ui/ui_js.go @@ -81,7 +81,7 @@ func (u *userInterface) ActualScreenScale() int { } func (u *userInterface) Update() (interface{}, error) { - currentInput.UpdateGamepads() + currentInput.updateGamepads() if u.sizeChanged { u.sizeChanged = false w, h := u.size() @@ -165,25 +165,25 @@ func initialize() (*opengl.Context, error) { canvas.Call("addEventListener", "keydown", func(e *js.Object) { e.Call("preventDefault") code := e.Get("keyCode").Int() - currentInput.KeyDown(code) + currentInput.keyDown(code) }) canvas.Call("addEventListener", "keyup", func(e *js.Object) { e.Call("preventDefault") code := e.Get("keyCode").Int() - currentInput.KeyUp(code) + currentInput.keyUp(code) }) // Mouse canvas.Call("addEventListener", "mousedown", func(e *js.Object) { e.Call("preventDefault") button := e.Get("button").Int() - currentInput.MouseDown(button) + currentInput.mouseDown(button) setMouseCursorFromEvent(e) }) canvas.Call("addEventListener", "mouseup", func(e *js.Object) { e.Call("preventDefault") button := e.Get("button").Int() - currentInput.MouseUp(button) + currentInput.mouseUp(button) setMouseCursorFromEvent(e) }) canvas.Call("addEventListener", "mousemove", func(e *js.Object) { @@ -198,14 +198,14 @@ func initialize() (*opengl.Context, error) { // TODO: Create indimendent touch functions canvas.Call("addEventListener", "touchstart", func(e *js.Object) { e.Call("preventDefault") - currentInput.MouseDown(0) + currentInput.mouseDown(0) touches := e.Get("changedTouches") touch := touches.Index(0) setMouseCursorFromEvent(touch) }) canvas.Call("addEventListener", "touchend", func(e *js.Object) { e.Call("preventDefault") - currentInput.MouseUp(0) + currentInput.mouseUp(0) touches := e.Get("changedTouches") touch := touches.Index(0) setMouseCursorFromEvent(touch) @@ -238,7 +238,7 @@ func setMouseCursorFromEvent(e *js.Object) { x, y := e.Get("clientX").Int(), e.Get("clientY").Int() x -= rect.Get("left").Int() y -= rect.Get("top").Int() - currentInput.SetMouseCursor(x/scale, y/scale) + currentInput.setMouseCursor(x/scale, y/scale) } func devicePixelRatio() float64 {