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 (
|
2023-09-17 09:52:21 +02:00
|
|
|
"math"
|
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")
|
|
|
|
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
|
|
|
)
|
|
|
|
|
2023-09-17 09:17:01 +02:00
|
|
|
type touchInClient struct {
|
|
|
|
id TouchID
|
|
|
|
x float64
|
|
|
|
y float64
|
|
|
|
}
|
|
|
|
|
2022-12-16 10:34:14 +01:00
|
|
|
func jsKeyToID(key js.Value) Key {
|
2020-12-16 05:29:32 +01:00
|
|
|
// js.Value cannot be used as a map key.
|
|
|
|
// As the number of keys is around 100, just a dumb loop should work.
|
2022-12-16 10:34:14 +01:00
|
|
|
for uiKey, jsKey := range uiKeyToJSKey {
|
|
|
|
if jsKey.Equal(key) {
|
|
|
|
return uiKey
|
2020-12-16 05:29:32 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
|
2022-02-06 10:30:31 +01:00
|
|
|
var codeToMouseButton = map[int]MouseButton{
|
2022-11-22 20:40:31 +01:00
|
|
|
0: MouseButton0, // Left
|
2022-11-27 05:51:07 +01:00
|
|
|
1: MouseButton1, // Middle
|
|
|
|
2: MouseButton2, // Right
|
2022-11-22 20:28:34 +01:00
|
|
|
3: MouseButton3,
|
|
|
|
4: MouseButton4,
|
2017-04-11 04:56:05 +02:00
|
|
|
}
|
|
|
|
|
2023-10-14 19:51:23 +02:00
|
|
|
func (u *UserInterface) keyDown(code js.Value) {
|
2023-01-23 16:32:08 +01:00
|
|
|
id := jsKeyToID(code)
|
|
|
|
if id < 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
u.inputState.KeyPressed[id] = true
|
2018-06-17 17:38:30 +02:00
|
|
|
}
|
|
|
|
|
2023-10-14 19:51:23 +02:00
|
|
|
func (u *UserInterface) keyUp(code js.Value) {
|
2023-01-23 16:32:08 +01:00
|
|
|
id := jsKeyToID(code)
|
|
|
|
if id < 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
u.inputState.KeyPressed[id] = false
|
2015-01-02 07:20:05 +01:00
|
|
|
}
|
2015-01-06 15:41:03 +01:00
|
|
|
|
2023-10-14 19:51:23 +02:00
|
|
|
func (u *UserInterface) mouseDown(code int) {
|
2022-12-16 10:34:14 +01:00
|
|
|
u.inputState.MouseButtonPressed[codeToMouseButton[code]] = true
|
2015-01-06 15:41:03 +01:00
|
|
|
}
|
|
|
|
|
2023-10-14 19:51:23 +02:00
|
|
|
func (u *UserInterface) mouseUp(code int) {
|
2022-12-16 10:34:14 +01:00
|
|
|
u.inputState.MouseButtonPressed[codeToMouseButton[code]] = false
|
2015-01-06 15:41:03 +01:00
|
|
|
}
|
|
|
|
|
2023-10-14 19:51:23 +02:00
|
|
|
func (u *UserInterface) updateInputFromEvent(e js.Value) error {
|
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):
|
2022-07-12 16:09:34 +02:00
|
|
|
if str := e.Get("key").String(); isKeyString(str) {
|
|
|
|
for _, r := range str {
|
2022-12-16 10:34:14 +01:00
|
|
|
u.inputState.appendRune(r)
|
2022-07-12 16:09:34 +02:00
|
|
|
}
|
|
|
|
}
|
2022-12-16 10:34:14 +01:00
|
|
|
u.keyDown(e.Get("code"))
|
2021-06-07 14:43:25 +02:00
|
|
|
case t.Equal(stringKeyup):
|
2022-12-16 10:34:14 +01:00
|
|
|
u.keyUp(e.Get("code"))
|
2021-06-07 14:43:25 +02:00
|
|
|
case t.Equal(stringMousedown):
|
2022-12-16 10:34:14 +01:00
|
|
|
u.mouseDown(e.Get("button").Int())
|
|
|
|
u.setMouseCursorFromEvent(e)
|
2021-06-07 14:43:25 +02:00
|
|
|
case t.Equal(stringMouseup):
|
2022-12-16 10:34:14 +01:00
|
|
|
u.mouseUp(e.Get("button").Int())
|
|
|
|
u.setMouseCursorFromEvent(e)
|
2021-06-07 14:43:25 +02:00
|
|
|
case t.Equal(stringMousemove):
|
2022-12-16 10:34:14 +01:00
|
|
|
u.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?
|
2022-12-16 10:34:14 +01:00
|
|
|
u.inputState.WheelX = -e.Get("deltaX").Float()
|
|
|
|
u.inputState.WheelY = -e.Get("deltaY").Float()
|
2021-06-07 14:43:25 +02:00
|
|
|
case t.Equal(stringTouchstart) || t.Equal(stringTouchend) || t.Equal(stringTouchmove):
|
2022-12-16 10:34:14 +01:00
|
|
|
u.updateTouchesFromEvent(e)
|
2018-04-01 16:20:45 +02:00
|
|
|
}
|
2021-07-22 18:07:28 +02:00
|
|
|
|
2022-12-16 10:34:14 +01:00
|
|
|
u.forceUpdateOnMinimumFPSMode()
|
2022-09-20 06:19:40 +02:00
|
|
|
return nil
|
2018-04-01 16:20:45 +02:00
|
|
|
}
|
|
|
|
|
2023-10-14 19:51:23 +02:00
|
|
|
func (u *UserInterface) setMouseCursorFromEvent(e js.Value) {
|
2022-12-16 10:34:14 +01:00
|
|
|
if u.context == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-09-17 09:17:01 +02:00
|
|
|
u.origCursorXInClient = e.Get("clientX").Float()
|
|
|
|
u.origCursorYInClient = e.Get("clientY").Float()
|
2023-07-05 02:24:26 +02:00
|
|
|
|
2022-12-16 10:34:14 +01:00
|
|
|
if u.cursorMode == CursorModeCaptured {
|
2023-09-17 09:17:01 +02:00
|
|
|
u.cursorXInClient += e.Get("movementX").Float()
|
|
|
|
u.cursorYInClient += e.Get("movementY").Float()
|
2021-04-15 19:26:10 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-09-17 09:17:01 +02:00
|
|
|
u.cursorXInClient = u.origCursorXInClient
|
|
|
|
u.cursorYInClient = u.origCursorYInClient
|
2021-04-15 19:26:10 +02:00
|
|
|
}
|
|
|
|
|
2023-10-14 19:51:23 +02:00
|
|
|
func (u *UserInterface) recoverCursorPosition() {
|
2023-09-17 09:17:01 +02:00
|
|
|
u.cursorXInClient = u.origCursorXInClient
|
|
|
|
u.cursorYInClient = u.origCursorYInClient
|
2018-04-01 16:20:45 +02:00
|
|
|
}
|
|
|
|
|
2023-10-14 19:51:23 +02:00
|
|
|
func (u *UserInterface) updateTouchesFromEvent(e js.Value) {
|
2023-09-17 09:17:01 +02:00
|
|
|
u.touchesInClient = u.touchesInClient[:0]
|
2022-12-16 10:34:14 +01:00
|
|
|
|
|
|
|
touches := e.Get("targetTouches")
|
|
|
|
for i := 0; i < touches.Length(); i++ {
|
|
|
|
t := touches.Call("item", i)
|
2023-09-17 09:17:01 +02:00
|
|
|
u.touchesInClient = append(u.touchesInClient, touchInClient{
|
|
|
|
id: TouchID(t.Get("identifier").Int()),
|
|
|
|
x: t.Get("clientX").Float(),
|
|
|
|
y: t.Get("clientY").Float(),
|
2023-01-21 16:04:30 +01:00
|
|
|
})
|
2018-04-01 16:20:45 +02:00
|
|
|
}
|
|
|
|
}
|
2020-12-20 12:25:18 +01:00
|
|
|
|
2022-07-12 16:09:34 +02:00
|
|
|
func isKeyString(str string) bool {
|
|
|
|
// From https://www.w3.org/TR/uievents-key/#keys-unicode,
|
|
|
|
//
|
|
|
|
// A key string is a string containing a 0 or 1 non-control characters
|
|
|
|
// ("base" characters) followed by 0 or more combining characters. The
|
|
|
|
// string MUST be in Normalized Form C (NFC) as described in
|
|
|
|
// [UnicodeNormalizationForms].
|
|
|
|
//
|
|
|
|
// A non-control character is any valid Unicode character except those
|
|
|
|
// that are part of the "Other, Control" ("Cc") General Category.
|
|
|
|
//
|
|
|
|
// A combining character is any valid Unicode character in the "Mark,
|
|
|
|
// Spacing Combining" ("Mc") General Category or with a non-zero
|
|
|
|
// Combining Class.
|
|
|
|
for i, r := range str {
|
|
|
|
if i == 0 {
|
|
|
|
if unicode.Is(unicode.Cc, r) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if !unicode.Is(unicode.Mc, r) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
2022-08-13 06:33:52 +02:00
|
|
|
|
|
|
|
var (
|
|
|
|
jsKeyboard = js.Global().Get("navigator").Get("keyboard")
|
|
|
|
jsKeyboardGetLayoutMap js.Value
|
|
|
|
jsKeyboardGetLayoutMapCh chan js.Value
|
|
|
|
jsKeyboardGetLayoutMapCallback js.Func
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
if !jsKeyboard.Truthy() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
jsKeyboardGetLayoutMap = jsKeyboard.Get("getLayoutMap").Call("bind", jsKeyboard)
|
|
|
|
jsKeyboardGetLayoutMapCh = make(chan js.Value, 1)
|
|
|
|
jsKeyboardGetLayoutMapCallback = js.FuncOf(func(this js.Value, args []js.Value) any {
|
|
|
|
jsKeyboardGetLayoutMapCh <- args[0]
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-10-14 17:06:07 +02:00
|
|
|
func (u *UserInterface) KeyName(key Key) string {
|
2022-08-13 06:33:52 +02:00
|
|
|
if !u.running {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
// keyboardLayoutMap is reset every tick.
|
|
|
|
if u.keyboardLayoutMap.IsUndefined() {
|
|
|
|
if !jsKeyboard.Truthy() {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
// Invoke getLayoutMap every tick to detect the keyboard change.
|
|
|
|
// TODO: Calling this every tick might be inefficient. Is there a way to detect a keyboard change?
|
|
|
|
jsKeyboardGetLayoutMap.Invoke().Call("then", jsKeyboardGetLayoutMapCallback)
|
|
|
|
u.keyboardLayoutMap = <-jsKeyboardGetLayoutMapCh
|
|
|
|
}
|
|
|
|
|
|
|
|
n := u.keyboardLayoutMap.Call("get", uiKeyToJSKey[key])
|
|
|
|
if n.IsUndefined() {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return n.String()
|
|
|
|
}
|
2022-12-25 07:29:04 +01:00
|
|
|
|
2023-10-14 19:23:27 +02:00
|
|
|
func UpdateInputFromEvent(e js.Value) {
|
|
|
|
theUI.updateInputFromEvent(e)
|
|
|
|
}
|
|
|
|
|
2023-10-14 19:51:23 +02:00
|
|
|
func (u *UserInterface) saveCursorPosition() {
|
2023-09-17 09:52:21 +02:00
|
|
|
u.savedCursorX = u.inputState.CursorX
|
|
|
|
u.savedCursorY = u.inputState.CursorY
|
|
|
|
w, h := u.outsideSize()
|
|
|
|
u.savedOutsideWidth = w
|
|
|
|
u.savedOutsideHeight = h
|
|
|
|
}
|
|
|
|
|
2023-10-14 19:51:23 +02:00
|
|
|
func (u *UserInterface) updateInputState() error {
|
2023-09-17 09:17:01 +02:00
|
|
|
s := u.DeviceScaleFactor()
|
2023-09-17 09:52:21 +02:00
|
|
|
|
|
|
|
if !math.IsNaN(u.savedCursorX) && !math.IsNaN(u.savedCursorY) {
|
|
|
|
// If savedCursorX and savedCursorY are valid values, the cursor is saved just before entering or exiting from fullscreen.
|
|
|
|
// Even after entering or exiting from fullscreening, the outside (body) size is not updated for a while.
|
|
|
|
// Wait for the outside size updated.
|
|
|
|
if w, h := u.outsideSize(); u.savedOutsideWidth != w || u.savedOutsideHeight != h {
|
|
|
|
u.inputState.CursorX = u.savedCursorX
|
|
|
|
u.inputState.CursorY = u.savedCursorY
|
|
|
|
cx, cy := u.context.logicalPositionToClientPosition(u.inputState.CursorX, u.inputState.CursorY, s)
|
|
|
|
u.cursorXInClient = cx
|
|
|
|
u.cursorYInClient = cy
|
|
|
|
u.savedCursorX = math.NaN()
|
|
|
|
u.savedCursorY = math.NaN()
|
|
|
|
u.savedOutsideWidth = 0
|
|
|
|
u.savedOutsideHeight = 0
|
|
|
|
u.outsideSizeUnchangedCount = 0
|
|
|
|
} else {
|
|
|
|
u.outsideSizeUnchangedCount++
|
|
|
|
|
|
|
|
// If the outside size is not changed for a while, probably the screen size is not actually changed.
|
|
|
|
// Reset the state.
|
|
|
|
if u.outsideSizeUnchangedCount > 60 {
|
|
|
|
u.savedCursorX = math.NaN()
|
|
|
|
u.savedCursorY = math.NaN()
|
|
|
|
u.savedOutsideWidth = 0
|
|
|
|
u.savedOutsideHeight = 0
|
|
|
|
u.outsideSizeUnchangedCount = 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
cx, cy := u.context.clientPositionToLogicalPosition(u.cursorXInClient, u.cursorYInClient, s)
|
|
|
|
u.inputState.CursorX = cx
|
|
|
|
u.inputState.CursorY = cy
|
|
|
|
}
|
2023-09-17 09:17:01 +02:00
|
|
|
|
|
|
|
u.inputState.Touches = u.inputState.Touches[:0]
|
|
|
|
for _, t := range u.touchesInClient {
|
|
|
|
x, y := u.context.clientPositionToLogicalPosition(t.x, t.y, s)
|
|
|
|
u.inputState.Touches = append(u.inputState.Touches, Touch{
|
|
|
|
ID: t.id,
|
|
|
|
X: int(x),
|
|
|
|
Y: int(y),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-09-17 07:22:58 +02:00
|
|
|
return nil
|
|
|
|
}
|