ui: Remove Input interface and remove locks on browsers to improve performance (#346)

This commit is contained in:
Hajime Hoshi 2017-04-28 23:45:01 +09:00
parent 0dafbfe99d
commit 4e22bd770f
4 changed files with 36 additions and 64 deletions

View File

@ -14,35 +14,24 @@
package ui package ui
var currentInput = &input{} var currentInput = &Input{}
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
Touches() []Touch
}
type Touch interface { type Touch interface {
ID() int ID() int
Position() (x, y int) Position() (x, y int)
} }
func CurrentInput() Input { func CurrentInput() *Input {
return currentInput return currentInput
} }
func (i *input) CursorPosition() (x, y int) { func (i *Input) CursorPosition() (x, y int) {
i.m.RLock() i.m.RLock()
defer i.m.RUnlock() defer i.m.RUnlock()
return i.cursorX, i.cursorY return i.cursorX, i.cursorY
} }
func (i *input) GamepadAxisNum(id int) int { func (i *Input) GamepadAxisNum(id int) int {
i.m.RLock() i.m.RLock()
defer i.m.RUnlock() defer i.m.RUnlock()
if len(i.gamepads) <= id { if len(i.gamepads) <= id {
@ -51,7 +40,7 @@ func (i *input) GamepadAxisNum(id int) int {
return i.gamepads[id].axisNum 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() i.m.RLock()
defer i.m.RUnlock() defer i.m.RUnlock()
if len(i.gamepads) <= id { if len(i.gamepads) <= id {
@ -60,7 +49,7 @@ func (i *input) GamepadAxis(id int, axis int) float64 {
return i.gamepads[id].axes[axis] return i.gamepads[id].axes[axis]
} }
func (i *input) GamepadButtonNum(id int) int { func (i *Input) GamepadButtonNum(id int) int {
i.m.RLock() i.m.RLock()
defer i.m.RUnlock() defer i.m.RUnlock()
if len(i.gamepads) <= id { if len(i.gamepads) <= id {
@ -69,7 +58,7 @@ func (i *input) GamepadButtonNum(id int) int {
return i.gamepads[id].buttonNum 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() i.m.RLock()
defer i.m.RUnlock() defer i.m.RUnlock()
if len(i.gamepads) <= id { if len(i.gamepads) <= id {
@ -78,7 +67,7 @@ func (i *input) IsGamepadButtonPressed(id int, button GamepadButton) bool {
return i.gamepads[id].buttonPressed[button] return i.gamepads[id].buttonPressed[button]
} }
func (in *input) Touches() []Touch { func (in *Input) Touches() []Touch {
in.m.RLock() in.m.RLock()
defer in.m.RUnlock() defer in.m.RUnlock()
t := make([]Touch, len(in.touches)) t := make([]Touch, len(in.touches))

View File

@ -25,7 +25,7 @@ import (
glfw "github.com/go-gl/glfw/v3.2/glfw" glfw "github.com/go-gl/glfw/v3.2/glfw"
) )
type input struct { type Input struct {
keyPressed map[glfw.Key]bool keyPressed map[glfw.Key]bool
mouseButtonPressed map[glfw.MouseButton]bool mouseButtonPressed map[glfw.MouseButton]bool
cursorX int cursorX int
@ -35,7 +35,7 @@ type input struct {
m sync.RWMutex m sync.RWMutex
} }
func (i *input) IsKeyPressed(key Key) bool { func (i *Input) IsKeyPressed(key Key) bool {
i.m.RLock() i.m.RLock()
defer i.m.RUnlock() defer i.m.RUnlock()
if i.keyPressed == nil { if i.keyPressed == nil {
@ -52,7 +52,7 @@ func (i *input) IsKeyPressed(key Key) bool {
return false return false
} }
func (i *input) IsMouseButtonPressed(button MouseButton) bool { func (i *Input) IsMouseButtonPressed(button MouseButton) bool {
i.m.RLock() i.m.RLock()
defer i.m.RUnlock() defer i.m.RUnlock()
if i.mouseButtonPressed == nil { if i.mouseButtonPressed == nil {
@ -75,7 +75,7 @@ var glfwMouseButtonToMouseButton = map[glfw.MouseButton]MouseButton{
glfw.MouseButtonMiddle: MouseButtonMiddle, glfw.MouseButtonMiddle: MouseButtonMiddle,
} }
func (i *input) update(window *glfw.Window, scale float64) { func (i *Input) update(window *glfw.Window, scale float64) {
i.m.Lock() i.m.Lock()
defer i.m.Unlock() defer i.m.Unlock()

View File

@ -17,12 +17,17 @@
package ui package ui
import ( import (
"sync"
"github.com/gopherjs/gopherjs/js" "github.com/gopherjs/gopherjs/js"
) )
type input struct { type mockRWLock struct{}
func (m mockRWLock) Lock() {}
func (m mockRWLock) Unlock() {}
func (m mockRWLock) RLock() {}
func (m mockRWLock) RUnlock() {}
type Input struct {
keyPressed map[string]bool keyPressed map[string]bool
keyPressedSafari map[int]bool keyPressedSafari map[int]bool
mouseButtonPressed map[int]bool mouseButtonPressed map[int]bool
@ -30,12 +35,10 @@ type input struct {
cursorY int cursorY int
gamepads [16]gamePad gamepads [16]gamePad
touches []touch touches []touch
m sync.RWMutex m mockRWLock
} }
func (i *input) IsKeyPressed(key Key) bool { func (i *Input) IsKeyPressed(key Key) bool {
i.m.RLock()
defer i.m.RUnlock()
if i.keyPressed != nil { if i.keyPressed != nil {
for _, c := range keyToCodes[key] { for _, c := range keyToCodes[key] {
if i.keyPressed[c] { if i.keyPressed[c] {
@ -62,9 +65,7 @@ var codeToMouseButton = map[int]MouseButton{
2: MouseButtonRight, 2: MouseButtonRight,
} }
func (i *input) IsMouseButtonPressed(button MouseButton) bool { func (i *Input) IsMouseButtonPressed(button MouseButton) bool {
i.m.RLock()
defer i.m.RUnlock()
if i.mouseButtonPressed == nil { if i.mouseButtonPressed == nil {
i.mouseButtonPressed = map[int]bool{} i.mouseButtonPressed = map[int]bool{}
} }
@ -79,69 +80,53 @@ func (i *input) IsMouseButtonPressed(button MouseButton) bool {
return false return false
} }
func (i *input) keyDown(code string) { func (i *Input) keyDown(code string) {
i.m.Lock()
defer i.m.Unlock()
if i.keyPressed == nil { if i.keyPressed == nil {
i.keyPressed = map[string]bool{} i.keyPressed = map[string]bool{}
} }
i.keyPressed[code] = true i.keyPressed[code] = true
} }
func (i *input) keyUp(code string) { func (i *Input) keyUp(code string) {
i.m.Lock()
defer i.m.Unlock()
if i.keyPressed == nil { if i.keyPressed == nil {
i.keyPressed = map[string]bool{} i.keyPressed = map[string]bool{}
} }
i.keyPressed[code] = false i.keyPressed[code] = false
} }
func (i *input) keyDownSafari(code int) { func (i *Input) keyDownSafari(code int) {
i.m.Lock()
defer i.m.Unlock()
if i.keyPressedSafari == nil { if i.keyPressedSafari == nil {
i.keyPressedSafari = map[int]bool{} i.keyPressedSafari = map[int]bool{}
} }
i.keyPressedSafari[code] = true i.keyPressedSafari[code] = true
} }
func (i *input) keyUpSafari(code int) { func (i *Input) keyUpSafari(code int) {
i.m.Lock()
defer i.m.Unlock()
if i.keyPressedSafari == nil { if i.keyPressedSafari == nil {
i.keyPressedSafari = map[int]bool{} i.keyPressedSafari = map[int]bool{}
} }
i.keyPressedSafari[code] = false i.keyPressedSafari[code] = false
} }
func (i *input) mouseDown(code int) { func (i *Input) mouseDown(code int) {
i.m.Lock()
defer i.m.Unlock()
if i.mouseButtonPressed == nil { if i.mouseButtonPressed == nil {
i.mouseButtonPressed = map[int]bool{} i.mouseButtonPressed = map[int]bool{}
} }
i.mouseButtonPressed[code] = true i.mouseButtonPressed[code] = true
} }
func (i *input) mouseUp(code int) { func (i *Input) mouseUp(code int) {
i.m.Lock()
defer i.m.Unlock()
if i.mouseButtonPressed == nil { if i.mouseButtonPressed == nil {
i.mouseButtonPressed = map[int]bool{} i.mouseButtonPressed = map[int]bool{}
} }
i.mouseButtonPressed[code] = false i.mouseButtonPressed[code] = false
} }
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
@ -178,9 +163,7 @@ func (i *input) updateGamepads() {
} }
} }
func (i *input) updateTouches(t []touch) { func (i *Input) updateTouches(t []touch) {
i.m.Lock()
defer i.m.Unlock()
i.touches = make([]touch, len(t)) i.touches = make([]touch, len(t))
copy(i.touches, t) copy(i.touches, t)
} }

View File

@ -20,7 +20,7 @@ import (
"sync" "sync"
) )
type input struct { type Input struct {
cursorX int cursorX int
cursorY int cursorY int
gamepads [16]gamePad gamepads [16]gamePad
@ -28,15 +28,15 @@ type input struct {
m sync.RWMutex m sync.RWMutex
} }
func (i *input) IsKeyPressed(key Key) bool { func (i *Input) IsKeyPressed(key Key) bool {
return false return false
} }
func (i *input) IsMouseButtonPressed(key MouseButton) bool { func (i *Input) IsMouseButtonPressed(key MouseButton) bool {
return false return false
} }
func (i *input) updateTouches(touches []Touch) { func (i *Input) updateTouches(touches []Touch) {
i.m.Lock() i.m.Lock()
defer i.m.Unlock() defer i.m.Unlock()
ts := make([]touch, len(touches)) ts := make([]touch, len(touches))