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
|
|
|
|
|
2018-04-01 16:20:45 +02:00
|
|
|
package input
|
2015-01-02 07:20:05 +01:00
|
|
|
|
2015-01-12 06:36:13 +01:00
|
|
|
import (
|
2018-04-01 16:20:45 +02:00
|
|
|
"unicode"
|
|
|
|
|
2015-01-12 06:36:13 +01:00
|
|
|
"github.com/gopherjs/gopherjs/js"
|
|
|
|
)
|
|
|
|
|
2017-04-28 16:45:01 +02:00
|
|
|
type mockRWLock struct{}
|
|
|
|
|
|
|
|
func (m mockRWLock) Lock() {}
|
|
|
|
func (m mockRWLock) Unlock() {}
|
|
|
|
func (m mockRWLock) RLock() {}
|
|
|
|
func (m mockRWLock) RUnlock() {}
|
|
|
|
|
|
|
|
type Input struct {
|
2017-04-13 20:02:38 +02:00
|
|
|
keyPressed map[string]bool
|
2017-11-09 16:35:46 +01:00
|
|
|
keyPressedEdge map[int]bool
|
2017-04-11 04:56:05 +02:00
|
|
|
mouseButtonPressed map[int]bool
|
|
|
|
cursorX int
|
|
|
|
cursorY int
|
|
|
|
gamepads [16]gamePad
|
2018-04-01 16:20:45 +02:00
|
|
|
touches []*Touch
|
2017-08-14 21:11:51 +02:00
|
|
|
runeBuffer []rune
|
2017-04-28 16:45:01 +02:00
|
|
|
m mockRWLock
|
2017-04-11 04:56:05 +02:00
|
|
|
}
|
|
|
|
|
2017-08-14 21:11:51 +02:00
|
|
|
func (i *Input) RuneBuffer() []rune {
|
|
|
|
return i.runeBuffer
|
|
|
|
}
|
|
|
|
|
2018-04-01 16:20:45 +02:00
|
|
|
func (i *Input) ClearRuneBuffer() {
|
|
|
|
i.runeBuffer = nil
|
|
|
|
}
|
|
|
|
|
2017-04-28 16:45:01 +02:00
|
|
|
func (i *Input) IsKeyPressed(key Key) bool {
|
2017-04-13 20:02:38 +02:00
|
|
|
if i.keyPressed != nil {
|
2017-04-27 04:40:21 +02:00
|
|
|
for _, c := range keyToCodes[key] {
|
2017-04-13 20:02:38 +02:00
|
|
|
if i.keyPressed[c] {
|
|
|
|
return true
|
|
|
|
}
|
2017-04-11 04:56:05 +02:00
|
|
|
}
|
2017-04-13 20:02:38 +02:00
|
|
|
}
|
2017-11-09 16:35:46 +01:00
|
|
|
if i.keyPressedEdge != nil {
|
|
|
|
for c, k := range keyCodeToKeyEdge {
|
2017-04-13 20:02:38 +02:00
|
|
|
if k != key {
|
|
|
|
continue
|
|
|
|
}
|
2017-11-09 16:35:46 +01:00
|
|
|
if i.keyPressedEdge[c] {
|
2017-04-13 20:02:38 +02:00
|
|
|
return true
|
|
|
|
}
|
2017-04-11 04:56:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
var codeToMouseButton = map[int]MouseButton{
|
|
|
|
0: MouseButtonLeft,
|
|
|
|
1: MouseButtonMiddle,
|
|
|
|
2: MouseButtonRight,
|
|
|
|
}
|
|
|
|
|
2017-04-28 16:45:01 +02:00
|
|
|
func (i *Input) IsMouseButtonPressed(button MouseButton) bool {
|
2017-04-11 04:56:05 +02:00
|
|
|
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-28 16:45:01 +02:00
|
|
|
func (i *Input) keyDown(code string) {
|
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-28 16:45:01 +02:00
|
|
|
func (i *Input) keyUp(code string) {
|
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-11-09 16:35:46 +01:00
|
|
|
func (i *Input) keyDownEdge(code int) {
|
|
|
|
if i.keyPressedEdge == nil {
|
|
|
|
i.keyPressedEdge = map[int]bool{}
|
2017-04-13 20:02:38 +02:00
|
|
|
}
|
2017-11-09 16:35:46 +01:00
|
|
|
i.keyPressedEdge[code] = true
|
2017-04-13 20:02:38 +02:00
|
|
|
}
|
|
|
|
|
2017-11-09 16:35:46 +01:00
|
|
|
func (i *Input) keyUpEdge(code int) {
|
|
|
|
if i.keyPressedEdge == nil {
|
|
|
|
i.keyPressedEdge = map[int]bool{}
|
2017-04-13 20:02:38 +02:00
|
|
|
}
|
2017-11-09 16:35:46 +01:00
|
|
|
i.keyPressedEdge[code] = false
|
2017-04-13 20:02:38 +02:00
|
|
|
}
|
|
|
|
|
2017-04-28 16:45:01 +02:00
|
|
|
func (i *Input) mouseDown(code int) {
|
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-28 16:45:01 +02:00
|
|
|
func (i *Input) mouseUp(code int) {
|
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
|
|
|
}
|
|
|
|
|
2017-04-28 16:45:01 +02:00
|
|
|
func (i *Input) setMouseCursor(x, y int) {
|
2015-01-06 15:41:03 +01:00
|
|
|
i.cursorX, i.cursorY = x, y
|
|
|
|
}
|
2015-01-12 06:36:13 +01:00
|
|
|
|
2018-04-01 16:20:45 +02:00
|
|
|
func (i *Input) UpdateGamepads() {
|
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++ {
|
2017-10-25 19:59:06 +02:00
|
|
|
i.gamepads[id].valid = false
|
2015-01-12 06:36:13 +01:00
|
|
|
gamepad := gamepads.Index(id)
|
|
|
|
if gamepad == js.Undefined || gamepad == nil {
|
|
|
|
continue
|
|
|
|
}
|
2017-10-25 19:59:06 +02:00
|
|
|
i.gamepads[id].valid = true
|
2015-01-12 06:36:13 +01:00
|
|
|
|
|
|
|
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
|
|
|
|
2018-04-01 16:20:45 +02:00
|
|
|
func OnKeyDown(e *js.Object) {
|
|
|
|
c := e.Get("code")
|
|
|
|
if c == js.Undefined {
|
|
|
|
code := e.Get("keyCode").Int()
|
|
|
|
if keyCodeToKeyEdge[code] == KeyUp ||
|
|
|
|
keyCodeToKeyEdge[code] == KeyDown ||
|
|
|
|
keyCodeToKeyEdge[code] == KeyLeft ||
|
|
|
|
keyCodeToKeyEdge[code] == KeyRight ||
|
|
|
|
keyCodeToKeyEdge[code] == KeyBackspace ||
|
|
|
|
keyCodeToKeyEdge[code] == KeyTab {
|
|
|
|
e.Call("preventDefault")
|
|
|
|
}
|
|
|
|
theInput.keyDownEdge(code)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
cs := c.String()
|
|
|
|
if cs == keyToCodes[KeyUp][0] ||
|
|
|
|
cs == keyToCodes[KeyDown][0] ||
|
|
|
|
cs == keyToCodes[KeyLeft][0] ||
|
|
|
|
cs == keyToCodes[KeyRight][0] ||
|
|
|
|
cs == keyToCodes[KeyBackspace][0] ||
|
|
|
|
cs == keyToCodes[KeyTab][0] {
|
|
|
|
e.Call("preventDefault")
|
|
|
|
}
|
|
|
|
theInput.keyDown(cs)
|
|
|
|
}
|
|
|
|
|
|
|
|
func OnKeyPress(e *js.Object) {
|
|
|
|
e.Call("preventDefault")
|
|
|
|
if r := rune(e.Get("charCode").Int()); unicode.IsPrint(r) {
|
|
|
|
theInput.runeBuffer = append(theInput.runeBuffer, r)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func OnKeyUp(e *js.Object) {
|
|
|
|
e.Call("preventDefault")
|
|
|
|
if e.Get("code") == js.Undefined {
|
|
|
|
// Assume that UA is Edge.
|
|
|
|
code := e.Get("keyCode").Int()
|
|
|
|
theInput.keyUpEdge(code)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
code := e.Get("code").String()
|
|
|
|
theInput.keyUp(code)
|
|
|
|
}
|
|
|
|
|
2018-04-01 16:53:26 +02:00
|
|
|
func OnMouseDown(e *js.Object) {
|
2018-04-01 16:20:45 +02:00
|
|
|
e.Call("preventDefault")
|
|
|
|
button := e.Get("button").Int()
|
|
|
|
theInput.mouseDown(button)
|
2018-04-01 16:53:26 +02:00
|
|
|
setMouseCursorFromEvent(e)
|
2018-04-01 16:20:45 +02:00
|
|
|
}
|
|
|
|
|
2018-04-01 16:53:26 +02:00
|
|
|
func OnMouseUp(e *js.Object) {
|
2018-04-01 16:20:45 +02:00
|
|
|
e.Call("preventDefault")
|
|
|
|
button := e.Get("button").Int()
|
|
|
|
theInput.mouseUp(button)
|
2018-04-01 16:53:26 +02:00
|
|
|
setMouseCursorFromEvent(e)
|
2018-04-01 16:20:45 +02:00
|
|
|
}
|
|
|
|
|
2018-04-01 16:53:26 +02:00
|
|
|
func OnMouseMove(e *js.Object) {
|
2018-04-01 16:20:45 +02:00
|
|
|
e.Call("preventDefault")
|
2018-04-01 16:53:26 +02:00
|
|
|
setMouseCursorFromEvent(e)
|
2018-04-01 16:20:45 +02:00
|
|
|
}
|
|
|
|
|
2018-04-02 18:21:52 +02:00
|
|
|
func OnTouchStart(e *js.Object) {
|
2018-04-01 16:20:45 +02:00
|
|
|
e.Call("preventDefault")
|
2018-04-02 18:21:52 +02:00
|
|
|
theInput.updateTouches(e)
|
2018-04-01 16:20:45 +02:00
|
|
|
}
|
|
|
|
|
2018-04-02 18:21:52 +02:00
|
|
|
func OnTouchEnd(e *js.Object) {
|
2018-04-01 16:20:45 +02:00
|
|
|
e.Call("preventDefault")
|
2018-04-02 18:21:52 +02:00
|
|
|
theInput.updateTouches(e)
|
2018-04-01 16:20:45 +02:00
|
|
|
}
|
|
|
|
|
2018-04-02 18:21:52 +02:00
|
|
|
func OnTouchMove(e *js.Object) {
|
2018-04-01 16:20:45 +02:00
|
|
|
e.Call("preventDefault")
|
2018-04-02 18:21:52 +02:00
|
|
|
theInput.updateTouches(e)
|
2018-04-01 16:20:45 +02:00
|
|
|
}
|
|
|
|
|
2018-04-01 16:53:26 +02:00
|
|
|
func setMouseCursorFromEvent(e *js.Object) {
|
2018-04-01 16:20:45 +02:00
|
|
|
x, y := e.Get("clientX").Int(), e.Get("clientY").Int()
|
2018-04-01 16:53:26 +02:00
|
|
|
theInput.setMouseCursor(x, y)
|
2018-04-01 16:20:45 +02:00
|
|
|
}
|
|
|
|
|
2018-04-02 18:21:52 +02:00
|
|
|
func (i *Input) updateTouches(e *js.Object) {
|
2018-04-01 16:20:45 +02:00
|
|
|
j := e.Get("targetTouches")
|
2018-04-01 18:19:42 +02:00
|
|
|
ts := make([]*Touch, j.Get("length").Int())
|
|
|
|
for i := 0; i < len(ts); i++ {
|
2018-04-01 16:20:45 +02:00
|
|
|
jj := j.Call("item", i)
|
|
|
|
id := jj.Get("identifier").Int()
|
2018-04-01 18:19:42 +02:00
|
|
|
ts[i] = &Touch{
|
2018-04-01 16:20:45 +02:00
|
|
|
id: id,
|
2018-04-02 17:53:06 +02:00
|
|
|
x: jj.Get("clientX").Int(),
|
|
|
|
y: jj.Get("clientY").Int(),
|
2018-04-01 16:20:45 +02:00
|
|
|
}
|
|
|
|
}
|
2018-04-01 18:19:42 +02:00
|
|
|
i.touches = ts
|
2018-04-01 16:20:45 +02:00
|
|
|
}
|