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.
|
|
|
|
|
2019-04-07 11:55:56 +02:00
|
|
|
package js
|
2015-01-02 07:20:05 +01:00
|
|
|
|
2015-01-12 06:36:13 +01:00
|
|
|
import (
|
2020-02-04 15:59:57 +01:00
|
|
|
"encoding/hex"
|
2019-04-30 19:15:28 +02:00
|
|
|
"syscall/js"
|
2018-04-01 16:20:45 +02:00
|
|
|
"unicode"
|
|
|
|
|
2020-10-03 19:35:13 +02:00
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/driver"
|
2020-12-16 04:41:49 +01:00
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/jsutil"
|
|
|
|
)
|
|
|
|
|
|
|
|
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() {
|
|
|
|
for _, k := range driverKeyToJSKey {
|
|
|
|
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 {
|
|
|
|
if jsutil.Equal(k, key) {
|
|
|
|
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
|
|
|
|
2020-12-27 09:54:00 +01:00
|
|
|
type gamepad struct {
|
2020-01-09 14:11:32 +01:00
|
|
|
name string
|
2019-04-07 11:55:56 +02:00
|
|
|
axisNum int
|
|
|
|
axes [16]float64
|
|
|
|
buttonNum int
|
|
|
|
buttonPressed [256]bool
|
|
|
|
}
|
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
|
2018-09-30 10:08:16 +02:00
|
|
|
wheelX float64
|
|
|
|
wheelY float64
|
2020-12-27 09:54:00 +01:00
|
|
|
gamepads map[driver.GamepadID]gamepad
|
2020-10-09 20:36:14 +02:00
|
|
|
touches map[driver.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) {
|
2020-10-16 22:16:02 +02: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
|
|
|
}
|
|
|
|
|
2020-10-07 18:08:30 +02:00
|
|
|
func (i *Input) GamepadSDLID(id driver.GamepadID) string {
|
2020-02-04 15:59:57 +01:00
|
|
|
// This emulates the implementation of EMSCRIPTEN_JoystickGetDeviceGUID.
|
|
|
|
// https://hg.libsdl.org/SDL/file/bc90ce38f1e2/src/joystick/emscripten/SDL_sysjoystick.c#l385
|
2020-10-07 18:08:30 +02:00
|
|
|
if len(i.gamepads) <= int(id) {
|
2020-02-04 15:59:57 +01:00
|
|
|
return ""
|
|
|
|
}
|
|
|
|
var sdlid [16]byte
|
|
|
|
copy(sdlid[:], []byte(i.gamepads[id].name))
|
|
|
|
return hex.EncodeToString(sdlid[:])
|
2020-01-09 14:11:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// GamepadName returns a string containing some information about the controller.
|
|
|
|
// A PS2 controller returned "810-3-USB Gamepad" on Firefox
|
|
|
|
// A Xbox 360 controller returned "xinput" on Firefox and "Xbox 360 Controller (XInput STANDARD GAMEPAD)" on Chrome
|
2020-10-07 18:08:30 +02:00
|
|
|
func (i *Input) GamepadName(id driver.GamepadID) string {
|
|
|
|
if len(i.gamepads) <= int(id) {
|
2020-01-09 14:11:32 +01:00
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return i.gamepads[id].name
|
|
|
|
}
|
|
|
|
|
2020-10-07 18:08:30 +02:00
|
|
|
func (i *Input) GamepadIDs() []driver.GamepadID {
|
2019-04-07 11:55:56 +02:00
|
|
|
if len(i.gamepads) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
2020-12-27 09:54:00 +01:00
|
|
|
|
2020-10-07 18:08:30 +02:00
|
|
|
var r []driver.GamepadID
|
2020-12-27 09:54:00 +01:00
|
|
|
for id := range i.gamepads {
|
|
|
|
r = append(r, id)
|
2019-04-07 11:55:56 +02:00
|
|
|
}
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
2020-10-07 18:08:30 +02:00
|
|
|
func (i *Input) GamepadAxisNum(id driver.GamepadID) int {
|
2020-12-27 09:54:00 +01:00
|
|
|
g, ok := i.gamepads[id]
|
|
|
|
if !ok {
|
2019-04-07 11:55:56 +02:00
|
|
|
return 0
|
|
|
|
}
|
2020-12-27 09:54:00 +01:00
|
|
|
return g.axisNum
|
2019-04-07 11:55:56 +02:00
|
|
|
}
|
|
|
|
|
2020-10-07 18:08:30 +02:00
|
|
|
func (i *Input) GamepadAxis(id driver.GamepadID, axis int) float64 {
|
2020-12-27 09:54:00 +01:00
|
|
|
g, ok := i.gamepads[id]
|
|
|
|
if !ok {
|
2019-04-07 11:55:56 +02:00
|
|
|
return 0
|
|
|
|
}
|
2020-12-27 09:54:00 +01:00
|
|
|
if g.axisNum <= axis {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
return g.axes[axis]
|
2019-04-07 11:55:56 +02:00
|
|
|
}
|
|
|
|
|
2020-10-07 18:08:30 +02:00
|
|
|
func (i *Input) GamepadButtonNum(id driver.GamepadID) int {
|
2020-12-27 09:54:00 +01:00
|
|
|
g, ok := i.gamepads[id]
|
|
|
|
if !ok {
|
2019-04-07 11:55:56 +02:00
|
|
|
return 0
|
|
|
|
}
|
2020-12-27 09:54:00 +01:00
|
|
|
return g.buttonNum
|
2019-04-07 11:55:56 +02:00
|
|
|
}
|
|
|
|
|
2020-10-07 18:08:30 +02:00
|
|
|
func (i *Input) IsGamepadButtonPressed(id driver.GamepadID, button driver.GamepadButton) bool {
|
2020-12-27 09:54:00 +01:00
|
|
|
g, ok := i.gamepads[id]
|
|
|
|
if !ok {
|
2019-04-07 11:55:56 +02:00
|
|
|
return false
|
|
|
|
}
|
2020-12-27 09:54:00 +01:00
|
|
|
if g.buttonNum <= int(button) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return g.buttonPressed[button]
|
2019-04-07 11:55:56 +02:00
|
|
|
}
|
|
|
|
|
2020-10-09 20:36:14 +02:00
|
|
|
func (i *Input) TouchIDs() []driver.TouchID {
|
2019-04-07 11:55:56 +02:00
|
|
|
if len(i.touches) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-10-09 20:36:14 +02:00
|
|
|
var ids []driver.TouchID
|
2019-04-07 11:55:56 +02:00
|
|
|
for id := range i.touches {
|
|
|
|
ids = append(ids, id)
|
|
|
|
}
|
|
|
|
return ids
|
|
|
|
}
|
|
|
|
|
2020-10-09 20:36:14 +02:00
|
|
|
func (i *Input) TouchPosition(id driver.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 {
|
2020-10-16 22:16:02 +02: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
|
|
|
}
|
|
|
|
|
2017-08-14 21:11:51 +02:00
|
|
|
func (i *Input) RuneBuffer() []rune {
|
2020-11-18 11:37:12 +01:00
|
|
|
rs := make([]rune, len(i.runeBuffer))
|
|
|
|
copy(rs, i.runeBuffer)
|
|
|
|
return rs
|
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
|
|
|
|
}
|
|
|
|
|
2019-03-30 15:53:27 +01:00
|
|
|
func (i *Input) IsKeyPressed(key driver.Key) bool {
|
2017-04-13 20:02:38 +02:00
|
|
|
if i.keyPressed != nil {
|
2020-12-16 05:29:32 +01:00
|
|
|
if i.keyPressed[jsKeyToID(driverKeyToJSKey[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 {
|
2020-02-19 02:42:42 +01:00
|
|
|
for c, k := range edgeKeyCodeToDriverKey {
|
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
|
|
|
|
}
|
|
|
|
|
2019-03-30 15:53:27 +01:00
|
|
|
var codeToMouseButton = map[int]driver.MouseButton{
|
|
|
|
0: driver.MouseButtonLeft,
|
|
|
|
1: driver.MouseButtonMiddle,
|
|
|
|
2: driver.MouseButtonRight,
|
2017-04-11 04:56:05 +02:00
|
|
|
}
|
|
|
|
|
2019-03-30 15:53:27 +01:00
|
|
|
func (i *Input) IsMouseButtonPressed(button driver.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
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2020-12-20 10:17:08 +01:00
|
|
|
func (i *Input) updateGamepads() {
|
2018-06-29 17:02:15 +02:00
|
|
|
nav := js.Global().Get("navigator")
|
2020-11-22 10:22:52 +01:00
|
|
|
if !nav.Truthy() {
|
|
|
|
return
|
|
|
|
}
|
2020-12-27 09:54:00 +01:00
|
|
|
|
2020-11-21 06:25:17 +01:00
|
|
|
if !nav.Get("getGamepads").Truthy() {
|
2015-01-12 06:55:28 +01:00
|
|
|
return
|
|
|
|
}
|
2020-12-27 09:54:00 +01:00
|
|
|
|
|
|
|
i.gamepads = map[driver.GamepadID]gamepad{}
|
|
|
|
|
2015-01-12 06:55:28 +01:00
|
|
|
gamepads := nav.Call("getGamepads")
|
2020-12-27 09:54:00 +01:00
|
|
|
l := gamepads.Length()
|
|
|
|
for idx := 0; idx < l; idx++ {
|
|
|
|
gp := gamepads.Index(idx)
|
|
|
|
if !gp.Truthy() {
|
2015-01-12 06:36:13 +01:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2020-12-27 09:54:00 +01:00
|
|
|
id := driver.GamepadID(gp.Get("index").Int())
|
|
|
|
g := gamepad{}
|
|
|
|
g.name = gp.Get("id").String()
|
|
|
|
|
|
|
|
axes := gp.Get("axes")
|
2015-01-12 06:36:13 +01:00
|
|
|
axesNum := axes.Get("length").Int()
|
2020-12-27 09:54:00 +01:00
|
|
|
g.axisNum = axesNum
|
|
|
|
for a := 0; a < len(g.axes); a++ {
|
2015-01-12 06:36:13 +01:00
|
|
|
if axesNum <= a {
|
2020-12-27 09:54:00 +01:00
|
|
|
break
|
2015-01-12 06:36:13 +01:00
|
|
|
}
|
2020-12-27 09:54:00 +01:00
|
|
|
g.axes[a] = axes.Index(a).Float()
|
2015-01-12 06:36:13 +01:00
|
|
|
}
|
|
|
|
|
2020-12-27 09:54:00 +01:00
|
|
|
buttons := gp.Get("buttons")
|
2015-01-12 06:36:13 +01:00
|
|
|
buttonsNum := buttons.Get("length").Int()
|
2020-12-27 09:54:00 +01:00
|
|
|
g.buttonNum = buttonsNum
|
|
|
|
for b := 0; b < len(g.buttonPressed); b++ {
|
2015-01-12 06:36:13 +01:00
|
|
|
if buttonsNum <= b {
|
2020-12-27 09:54:00 +01:00
|
|
|
break
|
2015-01-12 06:36:13 +01:00
|
|
|
}
|
2020-12-27 09:54:00 +01:00
|
|
|
g.buttonPressed[b] = buttons.Index(b).Get("pressed").Bool()
|
2015-01-12 06:36:13 +01:00
|
|
|
}
|
2020-12-27 09:54:00 +01:00
|
|
|
|
|
|
|
i.gamepads[id] = g
|
2015-01-12 06:36:13 +01:00
|
|
|
}
|
|
|
|
}
|
2016-05-26 18:31:30 +02: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"); {
|
|
|
|
case jsutil.Equal(t, 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()
|
2020-02-19 02:42:42 +01:00
|
|
|
if edgeKeyCodeToDriverKey[code] == driver.KeyUp ||
|
|
|
|
edgeKeyCodeToDriverKey[code] == driver.KeyDown ||
|
|
|
|
edgeKeyCodeToDriverKey[code] == driver.KeyLeft ||
|
|
|
|
edgeKeyCodeToDriverKey[code] == driver.KeyRight ||
|
|
|
|
edgeKeyCodeToDriverKey[code] == driver.KeyBackspace ||
|
|
|
|
edgeKeyCodeToDriverKey[code] == driver.KeyTab {
|
2019-04-06 16:32:19 +02:00
|
|
|
e.Call("preventDefault")
|
|
|
|
}
|
|
|
|
i.keyDownEdge(code)
|
|
|
|
return
|
|
|
|
}
|
2020-12-16 04:51:52 +01:00
|
|
|
if jsutil.Equal(c, driverKeyToJSKey[driver.KeyUp]) ||
|
|
|
|
jsutil.Equal(c, driverKeyToJSKey[driver.KeyDown]) ||
|
|
|
|
jsutil.Equal(c, driverKeyToJSKey[driver.KeyLeft]) ||
|
|
|
|
jsutil.Equal(c, driverKeyToJSKey[driver.KeyRight]) ||
|
|
|
|
jsutil.Equal(c, driverKeyToJSKey[driver.KeyBackspace]) ||
|
|
|
|
jsutil.Equal(c, driverKeyToJSKey[driver.KeyTab]) {
|
2018-04-01 16:20:45 +02:00
|
|
|
e.Call("preventDefault")
|
|
|
|
}
|
2020-12-16 04:51:52 +01:00
|
|
|
i.keyDown(c)
|
2020-12-16 04:41:49 +01:00
|
|
|
case jsutil.Equal(t, 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)
|
|
|
|
}
|
2020-12-16 04:41:49 +01:00
|
|
|
case jsutil.Equal(t, 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"))
|
2020-12-16 04:41:49 +01:00
|
|
|
case jsutil.Equal(t, stringMousedown):
|
2019-04-06 16:32:19 +02:00
|
|
|
button := e.Get("button").Int()
|
|
|
|
i.mouseDown(button)
|
|
|
|
i.setMouseCursorFromEvent(e)
|
2020-12-16 04:41:49 +01:00
|
|
|
case jsutil.Equal(t, stringMouseup):
|
2019-04-06 16:32:19 +02:00
|
|
|
button := e.Get("button").Int()
|
|
|
|
i.mouseUp(button)
|
|
|
|
i.setMouseCursorFromEvent(e)
|
2020-12-16 04:41:49 +01:00
|
|
|
case jsutil.Equal(t, stringMousemove):
|
2019-04-06 16:32:19 +02:00
|
|
|
i.setMouseCursorFromEvent(e)
|
2020-12-16 04:41:49 +01:00
|
|
|
case jsutil.Equal(t, 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()
|
2020-12-16 04:41:49 +01:00
|
|
|
case jsutil.Equal(t, stringTouchstart) || jsutil.Equal(t, stringTouchend) || jsutil.Equal(t, stringTouchmove):
|
2020-12-20 10:19:58 +01:00
|
|
|
i.updateTouchesFromEvent(e)
|
2018-04-01 16:20:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-06 16:32:19 +02:00
|
|
|
func (i *Input) setMouseCursorFromEvent(e js.Value) {
|
2018-04-01 16:20:45 +02:00
|
|
|
x, y := e.Get("clientX").Int(), e.Get("clientY").Int()
|
2019-04-06 16:32:19 +02:00
|
|
|
i.setMouseCursor(x, y)
|
2018-04-01 16:20:45 +02:00
|
|
|
}
|
|
|
|
|
2020-12-20 10:19:58 +01:00
|
|
|
func (i *Input) updateTouchesFromEvent(e js.Value) {
|
2018-04-01 16:20:45 +02:00
|
|
|
j := e.Get("targetTouches")
|
2020-10-09 20:36:14 +02:00
|
|
|
ts := map[driver.TouchID]pos{}
|
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)
|
2020-10-09 20:36:14 +02:00
|
|
|
id := driver.TouchID(jj.Get("identifier").Int())
|
2019-03-31 09:28:50 +02:00
|
|
|
ts[id] = pos{
|
|
|
|
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
|
|
|
}
|
2020-12-20 12:25:18 +01:00
|
|
|
|
|
|
|
func (i *Input) updateForGo2Cpp() {
|
|
|
|
if !go2cpp.Truthy() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
i.touches = map[driver.TouchID]pos{}
|
2020-12-27 09:54:00 +01:00
|
|
|
touchCount := go2cpp.Get("touchCount").Int()
|
|
|
|
for idx := 0; idx < touchCount; idx++ {
|
2020-12-21 15:01:12 +01:00
|
|
|
id := go2cpp.Call("getTouchPositionId", idx)
|
|
|
|
x := go2cpp.Call("getTouchPositionX", idx)
|
|
|
|
y := go2cpp.Call("getTouchPositionY", idx)
|
|
|
|
i.touches[driver.TouchID(id.Int())] = pos{
|
2020-12-20 12:25:18 +01:00
|
|
|
X: x.Int(),
|
|
|
|
Y: y.Int(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|