2015-01-02 07:20:05 +01:00
|
|
|
// Copyright 2015 Hajime Hoshi
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
// +build js
|
|
|
|
|
|
|
|
package ui
|
|
|
|
|
2015-01-12 06:36:13 +01:00
|
|
|
import (
|
2017-04-11 04:56:05 +02:00
|
|
|
"sync"
|
|
|
|
|
2015-01-12 06:36:13 +01:00
|
|
|
"github.com/gopherjs/gopherjs/js"
|
|
|
|
)
|
|
|
|
|
2017-04-11 04:56:05 +02:00
|
|
|
type input struct {
|
2017-04-13 20:02:38 +02:00
|
|
|
keyPressed map[string]bool
|
|
|
|
keyPressedSafari map[int]bool
|
2017-04-11 04:56:05 +02:00
|
|
|
mouseButtonPressed map[int]bool
|
|
|
|
cursorX int
|
|
|
|
cursorY int
|
|
|
|
gamepads [16]gamePad
|
|
|
|
touches []touch
|
|
|
|
m sync.RWMutex
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *input) IsKeyPressed(key Key) bool {
|
|
|
|
i.m.RLock()
|
|
|
|
defer i.m.RUnlock()
|
2017-04-13 20:02:38 +02:00
|
|
|
if i.keyPressed != nil {
|
|
|
|
for c, k := range codeToKey {
|
|
|
|
if k != key {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if i.keyPressed[c] {
|
|
|
|
return true
|
|
|
|
}
|
2017-04-11 04:56:05 +02:00
|
|
|
}
|
2017-04-13 20:02:38 +02:00
|
|
|
}
|
|
|
|
if i.keyPressedSafari != nil {
|
|
|
|
for c, k := range keyCodeToKeySafari {
|
|
|
|
if k != key {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if i.keyPressedSafari[c] {
|
|
|
|
return true
|
|
|
|
}
|
2017-04-11 04:56:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
var codeToMouseButton = map[int]MouseButton{
|
|
|
|
0: MouseButtonLeft,
|
|
|
|
1: MouseButtonMiddle,
|
|
|
|
2: MouseButtonRight,
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *input) IsMouseButtonPressed(button MouseButton) bool {
|
|
|
|
i.m.RLock()
|
|
|
|
defer i.m.RUnlock()
|
|
|
|
if i.mouseButtonPressed == nil {
|
|
|
|
i.mouseButtonPressed = map[int]bool{}
|
|
|
|
}
|
|
|
|
for c, b := range codeToMouseButton {
|
|
|
|
if b != button {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if i.mouseButtonPressed[c] {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2017-04-13 20:02:38 +02:00
|
|
|
func (i *input) keyDown(code string) {
|
2016-03-24 15:51:20 +01:00
|
|
|
i.m.Lock()
|
|
|
|
defer i.m.Unlock()
|
2017-04-11 04:56:05 +02:00
|
|
|
if i.keyPressed == nil {
|
2017-04-13 20:02:38 +02:00
|
|
|
i.keyPressed = map[string]bool{}
|
2015-01-07 15:02:58 +01:00
|
|
|
}
|
2017-04-11 04:56:05 +02:00
|
|
|
i.keyPressed[code] = true
|
2015-01-02 16:52:49 +01:00
|
|
|
}
|
|
|
|
|
2017-04-13 20:02:38 +02:00
|
|
|
func (i *input) keyUp(code string) {
|
2016-03-24 15:51:20 +01:00
|
|
|
i.m.Lock()
|
|
|
|
defer i.m.Unlock()
|
2017-04-11 04:56:05 +02:00
|
|
|
if i.keyPressed == nil {
|
2017-04-13 20:02:38 +02:00
|
|
|
i.keyPressed = map[string]bool{}
|
2015-01-07 15:02:58 +01:00
|
|
|
}
|
2017-04-11 04:56:05 +02:00
|
|
|
i.keyPressed[code] = false
|
2015-01-02 07:20:05 +01:00
|
|
|
}
|
2015-01-06 15:41:03 +01:00
|
|
|
|
2017-04-13 20:02:38 +02:00
|
|
|
func (i *input) keyDownSafari(code int) {
|
|
|
|
i.m.Lock()
|
|
|
|
defer i.m.Unlock()
|
|
|
|
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()
|
|
|
|
if i.keyPressedSafari == nil {
|
|
|
|
i.keyPressedSafari = map[int]bool{}
|
|
|
|
}
|
|
|
|
i.keyPressedSafari[code] = false
|
|
|
|
}
|
|
|
|
|
2017-04-11 04:56:05 +02:00
|
|
|
func (i *input) mouseDown(code int) {
|
2016-03-24 15:51:20 +01:00
|
|
|
i.m.Lock()
|
|
|
|
defer i.m.Unlock()
|
2017-04-11 04:56:05 +02:00
|
|
|
if i.mouseButtonPressed == nil {
|
|
|
|
i.mouseButtonPressed = map[int]bool{}
|
2015-01-06 15:41:03 +01:00
|
|
|
}
|
2017-04-11 04:56:05 +02:00
|
|
|
i.mouseButtonPressed[code] = true
|
2015-01-06 15:41:03 +01:00
|
|
|
}
|
|
|
|
|
2017-04-11 04:56:05 +02:00
|
|
|
func (i *input) mouseUp(code int) {
|
2016-03-24 15:51:20 +01:00
|
|
|
i.m.Lock()
|
|
|
|
defer i.m.Unlock()
|
2017-04-11 04:56:05 +02:00
|
|
|
if i.mouseButtonPressed == nil {
|
|
|
|
i.mouseButtonPressed = map[int]bool{}
|
2015-01-06 15:41:03 +01:00
|
|
|
}
|
2017-04-11 04:56:05 +02:00
|
|
|
i.mouseButtonPressed[code] = false
|
2015-01-06 15:41:03 +01:00
|
|
|
}
|
|
|
|
|
2016-05-19 17:15:05 +02:00
|
|
|
func (i *input) setMouseCursor(x, y int) {
|
2016-03-24 15:51:20 +01:00
|
|
|
i.m.Lock()
|
|
|
|
defer i.m.Unlock()
|
2015-01-06 15:41:03 +01:00
|
|
|
i.cursorX, i.cursorY = x, y
|
|
|
|
}
|
2015-01-12 06:36:13 +01:00
|
|
|
|
2016-05-19 17:15:05 +02:00
|
|
|
func (i *input) updateGamepads() {
|
2016-03-24 15:51:20 +01:00
|
|
|
i.m.Lock()
|
|
|
|
defer i.m.Unlock()
|
2015-01-12 06:55:28 +01:00
|
|
|
nav := js.Global.Get("navigator")
|
|
|
|
if nav.Get("getGamepads") == js.Undefined {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
gamepads := nav.Call("getGamepads")
|
2015-01-12 06:36:13 +01:00
|
|
|
l := gamepads.Get("length").Int()
|
|
|
|
for id := 0; id < l; id++ {
|
|
|
|
gamepad := gamepads.Index(id)
|
|
|
|
if gamepad == js.Undefined || gamepad == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
axes := gamepad.Get("axes")
|
|
|
|
axesNum := axes.Get("length").Int()
|
|
|
|
i.gamepads[id].axisNum = axesNum
|
|
|
|
for a := 0; a < len(i.gamepads[id].axes); a++ {
|
|
|
|
if axesNum <= a {
|
|
|
|
i.gamepads[id].axes[a] = 0
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
i.gamepads[id].axes[a] = axes.Index(a).Float()
|
|
|
|
}
|
|
|
|
|
|
|
|
buttons := gamepad.Get("buttons")
|
|
|
|
buttonsNum := buttons.Get("length").Int()
|
|
|
|
i.gamepads[id].buttonNum = buttonsNum
|
|
|
|
for b := 0; b < len(i.gamepads[id].buttonPressed); b++ {
|
|
|
|
if buttonsNum <= b {
|
|
|
|
i.gamepads[id].buttonPressed[b] = false
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
i.gamepads[id].buttonPressed[b] = buttons.Index(b).Get("pressed").Bool()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-05-26 18:31:30 +02:00
|
|
|
|
|
|
|
func (i *input) updateTouches(t []touch) {
|
|
|
|
i.m.Lock()
|
|
|
|
defer i.m.Unlock()
|
|
|
|
i.touches = make([]touch, len(t))
|
|
|
|
copy(i.touches, t)
|
|
|
|
}
|