ui: Change Input to be an interface

This commit is contained in:
Hajime Hoshi 2016-05-20 00:15:05 +09:00
parent d09bb63f71
commit c36dd3df52
4 changed files with 36 additions and 26 deletions

View File

@ -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 {

View File

@ -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()

View File

@ -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")

View File

@ -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 {