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

View File

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

View File

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

View File

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