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.
|
|
|
|
|
2022-02-06 08:08:22 +01:00
|
|
|
package ui
|
2015-01-02 07:20:05 +01:00
|
|
|
|
2015-01-12 06:36:13 +01:00
|
|
|
import (
|
2019-04-30 19:15:28 +02:00
|
|
|
"syscall/js"
|
2018-04-01 16:20:45 +02:00
|
|
|
"unicode"
|
2020-12-16 04:41:49 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
stringKeydown = js.ValueOf("keydown")
|
|
|
|
stringKeypress = js.ValueOf("keypress")
|
|
|
|
stringKeyup = js.ValueOf("keyup")
|
|
|
|
stringMousedown = js.ValueOf("mousedown")
|
|
|
|
stringMouseup = js.ValueOf("mouseup")
|
|
|
|
stringMousemove = js.ValueOf("mousemove")
|
|
|
|
stringWheel = js.ValueOf("wheel")
|
|
|
|
stringTouchstart = js.ValueOf("touchstart")
|
|
|
|
stringTouchend = js.ValueOf("touchend")
|
|
|
|
stringTouchmove = js.ValueOf("touchmove")
|
2015-01-12 06:36:13 +01:00
|
|
|
)
|
|
|
|
|
2020-12-16 05:29:32 +01:00
|
|
|
var jsKeys []js.Value
|
|
|
|
|
|
|
|
func init() {
|
2022-02-06 10:13:10 +01:00
|
|
|
for _, k := range uiKeyToJSKey {
|
2020-12-16 05:29:32 +01:00
|
|
|
jsKeys = append(jsKeys, k)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func jsKeyToID(key js.Value) int {
|
|
|
|
// js.Value cannot be used as a map key.
|
|
|
|
// As the number of keys is around 100, just a dumb loop should work.
|
|
|
|
for i, k := range jsKeys {
|
2021-06-07 14:43:25 +02:00
|
|
|
if k.Equal(key) {
|
2020-12-16 05:29:32 +01:00
|
|
|
return i
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
|
2019-04-07 11:55:56 +02:00
|
|
|
type pos struct {
|
|
|
|
X int
|
|
|
|
Y int
|
|
|
|
}
|
2017-04-28 16:45:01 +02:00
|
|
|
|
|
|
|
type Input struct {
|
2020-12-16 05:29:32 +01:00
|
|
|
keyPressed map[int]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
|
2021-04-15 19:26:10 +02:00
|
|
|
origCursorX int
|
|
|
|
origCursorY int
|
2018-09-30 10:08:16 +02:00
|
|
|
wheelX float64
|
|
|
|
wheelY float64
|
2022-02-06 10:30:31 +01:00
|
|
|
touches map[TouchID]pos
|
2017-08-14 21:11:51 +02:00
|
|
|
runeBuffer []rune
|
2019-04-07 12:27:30 +02:00
|
|
|
ui *UserInterface
|
2019-04-07 11:55:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (i *Input) CursorPosition() (x, y int) {
|
2021-04-06 20:05:57 +02:00
|
|
|
if i.ui.context == nil {
|
|
|
|
return 0, 0
|
|
|
|
}
|
2022-02-13 08:30:33 +01:00
|
|
|
xf, yf := i.ui.context.adjustPosition(float64(i.cursorX), float64(i.cursorY), i.ui.DeviceScaleFactor())
|
2019-12-09 18:37:10 +01:00
|
|
|
return int(xf), int(yf)
|
2019-04-07 11:55:56 +02:00
|
|
|
}
|
|
|
|
|
2022-02-06 10:30:31 +01:00
|
|
|
func (i *Input) AppendTouchIDs(touchIDs []TouchID) []TouchID {
|
2019-04-07 11:55:56 +02:00
|
|
|
for id := range i.touches {
|
2021-07-09 18:15:47 +02:00
|
|
|
touchIDs = append(touchIDs, id)
|
2019-04-07 11:55:56 +02:00
|
|
|
}
|
2021-07-09 18:15:47 +02:00
|
|
|
return touchIDs
|
2019-04-07 11:55:56 +02:00
|
|
|
}
|
|
|
|
|
2022-02-06 10:30:31 +01:00
|
|
|
func (i *Input) TouchPosition(id TouchID) (x, y int) {
|
2020-10-16 22:16:02 +02:00
|
|
|
d := i.ui.DeviceScaleFactor()
|
2019-04-07 11:55:56 +02:00
|
|
|
for tid, pos := range i.touches {
|
|
|
|
if id == tid {
|
2022-02-13 08:30:33 +01:00
|
|
|
x, y := i.ui.context.adjustPosition(float64(pos.X), float64(pos.Y), d)
|
2019-12-09 18:37:10 +01:00
|
|
|
return int(x), int(y)
|
2019-04-07 11:55:56 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0, 0
|
2017-04-11 04:56:05 +02:00
|
|
|
}
|
|
|
|
|
2021-07-09 18:15:47 +02:00
|
|
|
func (i *Input) AppendInputChars(runes []rune) []rune {
|
|
|
|
return append(runes, i.runeBuffer...)
|
2017-08-14 21:11:51 +02:00
|
|
|
}
|
|
|
|
|
2020-04-02 17:06:42 +02:00
|
|
|
func (i *Input) resetForFrame() {
|
2018-04-01 16:20:45 +02:00
|
|
|
i.runeBuffer = nil
|
2018-09-30 10:08:16 +02:00
|
|
|
i.wheelX = 0
|
|
|
|
i.wheelY = 0
|
|
|
|
}
|
|
|
|
|
2022-02-06 10:13:10 +01:00
|
|
|
func (i *Input) IsKeyPressed(key Key) bool {
|
2017-04-13 20:02:38 +02:00
|
|
|
if i.keyPressed != nil {
|
2022-02-06 10:13:10 +01:00
|
|
|
if i.keyPressed[jsKeyToID(uiKeyToJSKey[key])] {
|
2019-09-01 17:25:39 +02:00
|
|
|
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 {
|
2022-02-06 10:13:10 +01:00
|
|
|
for c, k := range edgeKeyCodeToUIKey {
|
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
|
|
|
|
}
|
|
|
|
|
2022-02-06 10:30:31 +01:00
|
|
|
var codeToMouseButton = map[int]MouseButton{
|
|
|
|
0: MouseButtonLeft,
|
|
|
|
1: MouseButtonMiddle,
|
|
|
|
2: MouseButtonRight,
|
2017-04-11 04:56:05 +02:00
|
|
|
}
|
|
|
|
|
2022-02-06 10:30:31 +01: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
|
|
|
|
}
|
|
|
|
|
2018-09-30 10:01:45 +02:00
|
|
|
func (i *Input) Wheel() (xoff, yoff float64) {
|
2018-09-30 10:08:16 +02:00
|
|
|
return i.wheelX, i.wheelY
|
2018-06-17 17:38:30 +02:00
|
|
|
}
|
|
|
|
|
2020-12-16 04:51:52 +01:00
|
|
|
func (i *Input) keyDown(code js.Value) {
|
2017-04-11 04:56:05 +02:00
|
|
|
if i.keyPressed == nil {
|
2020-12-16 05:29:32 +01:00
|
|
|
i.keyPressed = map[int]bool{}
|
2015-01-07 15:02:58 +01:00
|
|
|
}
|
2020-12-16 05:29:32 +01:00
|
|
|
i.keyPressed[jsKeyToID(code)] = true
|
2015-01-02 16:52:49 +01:00
|
|
|
}
|
|
|
|
|
2020-12-16 04:51:52 +01:00
|
|
|
func (i *Input) keyUp(code js.Value) {
|
2017-04-11 04:56:05 +02:00
|
|
|
if i.keyPressed == nil {
|
2020-12-16 05:29:32 +01:00
|
|
|
i.keyPressed = map[int]bool{}
|
2015-01-07 15:02:58 +01:00
|
|
|
}
|
2020-12-16 05:29:32 +01:00
|
|
|
i.keyPressed[jsKeyToID(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
|
|
|
}
|
|
|
|
|
2020-12-20 10:17:08 +01:00
|
|
|
func (i *Input) updateFromEvent(e js.Value) {
|
2020-12-16 04:41:49 +01:00
|
|
|
// Avoid using js.Value.String() as String creates a Uint8Array via a TextEncoder and causes a heavy
|
|
|
|
// overhead (#1437).
|
|
|
|
switch t := e.Get("type"); {
|
2021-06-07 14:43:25 +02:00
|
|
|
case t.Equal(stringKeydown):
|
2019-04-06 16:32:19 +02:00
|
|
|
c := e.Get("code")
|
2020-11-21 06:25:17 +01:00
|
|
|
if c.Type() != js.TypeString {
|
2019-04-06 16:32:19 +02:00
|
|
|
code := e.Get("keyCode").Int()
|
2022-02-06 10:13:10 +01:00
|
|
|
if edgeKeyCodeToUIKey[code] == KeyArrowUp ||
|
|
|
|
edgeKeyCodeToUIKey[code] == KeyArrowDown ||
|
|
|
|
edgeKeyCodeToUIKey[code] == KeyArrowLeft ||
|
|
|
|
edgeKeyCodeToUIKey[code] == KeyArrowRight ||
|
|
|
|
edgeKeyCodeToUIKey[code] == KeyBackspace ||
|
|
|
|
edgeKeyCodeToUIKey[code] == KeyTab {
|
2019-04-06 16:32:19 +02:00
|
|
|
e.Call("preventDefault")
|
|
|
|
}
|
|
|
|
i.keyDownEdge(code)
|
|
|
|
return
|
|
|
|
}
|
2022-02-06 10:13:10 +01:00
|
|
|
if c.Equal(uiKeyToJSKey[KeyArrowUp]) ||
|
|
|
|
c.Equal(uiKeyToJSKey[KeyArrowDown]) ||
|
|
|
|
c.Equal(uiKeyToJSKey[KeyArrowLeft]) ||
|
|
|
|
c.Equal(uiKeyToJSKey[KeyArrowRight]) ||
|
|
|
|
c.Equal(uiKeyToJSKey[KeyBackspace]) ||
|
|
|
|
c.Equal(uiKeyToJSKey[KeyTab]) {
|
2018-04-01 16:20:45 +02:00
|
|
|
e.Call("preventDefault")
|
|
|
|
}
|
2020-12-16 04:51:52 +01:00
|
|
|
i.keyDown(c)
|
2021-06-07 14:43:25 +02:00
|
|
|
case t.Equal(stringKeypress):
|
2019-04-06 16:32:19 +02:00
|
|
|
if r := rune(e.Get("charCode").Int()); unicode.IsPrint(r) {
|
|
|
|
i.runeBuffer = append(i.runeBuffer, r)
|
|
|
|
}
|
2021-06-07 14:43:25 +02:00
|
|
|
case t.Equal(stringKeyup):
|
2020-11-21 06:25:17 +01:00
|
|
|
if e.Get("code").Type() != js.TypeString {
|
2019-04-06 16:32:19 +02:00
|
|
|
// Assume that UA is Edge.
|
|
|
|
code := e.Get("keyCode").Int()
|
|
|
|
i.keyUpEdge(code)
|
|
|
|
return
|
|
|
|
}
|
2020-12-16 04:51:52 +01:00
|
|
|
i.keyUp(e.Get("code"))
|
2021-06-07 14:43:25 +02:00
|
|
|
case t.Equal(stringMousedown):
|
2019-04-06 16:32:19 +02:00
|
|
|
button := e.Get("button").Int()
|
|
|
|
i.mouseDown(button)
|
|
|
|
i.setMouseCursorFromEvent(e)
|
2021-06-07 14:43:25 +02:00
|
|
|
case t.Equal(stringMouseup):
|
2019-04-06 16:32:19 +02:00
|
|
|
button := e.Get("button").Int()
|
|
|
|
i.mouseUp(button)
|
|
|
|
i.setMouseCursorFromEvent(e)
|
2021-06-07 14:43:25 +02:00
|
|
|
case t.Equal(stringMousemove):
|
2019-04-06 16:32:19 +02:00
|
|
|
i.setMouseCursorFromEvent(e)
|
2021-06-07 14:43:25 +02:00
|
|
|
case t.Equal(stringWheel):
|
2019-04-06 16:32:19 +02:00
|
|
|
// TODO: What if e.deltaMode is not DOM_DELTA_PIXEL?
|
|
|
|
i.wheelX = -e.Get("deltaX").Float()
|
|
|
|
i.wheelY = -e.Get("deltaY").Float()
|
2021-06-07 14:43:25 +02:00
|
|
|
case t.Equal(stringTouchstart) || t.Equal(stringTouchend) || t.Equal(stringTouchmove):
|
2020-12-20 10:19:58 +01:00
|
|
|
i.updateTouchesFromEvent(e)
|
2018-04-01 16:20:45 +02:00
|
|
|
}
|
2021-07-22 18:07:28 +02:00
|
|
|
|
|
|
|
i.ui.forceUpdateOnMinimumFPSMode()
|
2018-04-01 16:20:45 +02:00
|
|
|
}
|
|
|
|
|
2019-04-06 16:32:19 +02:00
|
|
|
func (i *Input) setMouseCursorFromEvent(e js.Value) {
|
2022-02-06 09:43:52 +01:00
|
|
|
if i.ui.cursorMode == CursorModeCaptured {
|
2021-04-15 19:26:10 +02:00
|
|
|
x, y := e.Get("clientX").Int(), e.Get("clientY").Int()
|
|
|
|
i.origCursorX, i.origCursorY = x, y
|
|
|
|
dx, dy := e.Get("movementX").Int(), e.Get("movementY").Int()
|
|
|
|
i.cursorX += dx
|
|
|
|
i.cursorY += dy
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-04-01 16:20:45 +02:00
|
|
|
x, y := e.Get("clientX").Int(), e.Get("clientY").Int()
|
2021-04-15 19:26:10 +02:00
|
|
|
i.cursorX, i.cursorY = x, y
|
|
|
|
i.origCursorX, i.origCursorY = x, y
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *Input) recoverCursorPosition() {
|
|
|
|
i.cursorX, i.cursorY = i.origCursorX, i.origCursorY
|
2018-04-01 16:20:45 +02:00
|
|
|
}
|
|
|
|
|
2021-01-25 16:12:12 +01:00
|
|
|
func (in *Input) updateTouchesFromEvent(e js.Value) {
|
2018-04-01 16:20:45 +02:00
|
|
|
j := e.Get("targetTouches")
|
2021-01-25 16:12:12 +01:00
|
|
|
for k := range in.touches {
|
|
|
|
delete(in.touches, k)
|
|
|
|
}
|
2019-04-14 19:56:46 +02:00
|
|
|
for i := 0; i < j.Length(); i++ {
|
2018-04-01 16:20:45 +02:00
|
|
|
jj := j.Call("item", i)
|
2022-02-06 10:30:31 +01:00
|
|
|
id := TouchID(jj.Get("identifier").Int())
|
2021-01-26 02:26:52 +01:00
|
|
|
if in.touches == nil {
|
2022-02-06 10:30:31 +01:00
|
|
|
in.touches = map[TouchID]pos{}
|
2021-01-26 02:26:52 +01:00
|
|
|
}
|
2021-01-25 16:12:12 +01:00
|
|
|
in.touches[id] = pos{
|
2019-03-31 09:28:50 +02:00
|
|
|
X: jj.Get("clientX").Int(),
|
|
|
|
Y: jj.Get("clientY").Int(),
|
2018-04-01 16:20:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-12-20 12:25:18 +01:00
|
|
|
|
|
|
|
func (i *Input) updateForGo2Cpp() {
|
|
|
|
if !go2cpp.Truthy() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-01-25 16:12:12 +01:00
|
|
|
for k := range i.touches {
|
|
|
|
delete(i.touches, k)
|
|
|
|
}
|
2020-12-27 09:54:00 +01:00
|
|
|
touchCount := go2cpp.Get("touchCount").Int()
|
|
|
|
for idx := 0; idx < touchCount; idx++ {
|
2020-12-27 11:56:24 +01:00
|
|
|
id := go2cpp.Call("getTouchId", idx)
|
|
|
|
x := go2cpp.Call("getTouchX", idx)
|
|
|
|
y := go2cpp.Call("getTouchY", idx)
|
2021-01-26 02:26:52 +01:00
|
|
|
if i.touches == nil {
|
2022-02-06 10:30:31 +01:00
|
|
|
i.touches = map[TouchID]pos{}
|
2021-01-26 02:26:52 +01:00
|
|
|
}
|
2022-02-06 10:30:31 +01:00
|
|
|
i.touches[TouchID(id.Int())] = pos{
|
2020-12-20 12:25:18 +01:00
|
|
|
X: x.Int(),
|
|
|
|
Y: y.Int(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|