From a62b8a00e760d279db42309b0fa01b9e6d6bbc98 Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Sun, 17 Sep 2023 03:43:18 +0900 Subject: [PATCH] internal/ui: use float64 for cursor positions internally --- input.go | 5 +++-- internal/ui/input.go | 4 ++-- internal/ui/input_glfw.go | 2 +- internal/ui/input_js.go | 9 ++++----- internal/ui/ui_js.go | 4 ++-- 5 files changed, 12 insertions(+), 12 deletions(-) diff --git a/input.go b/input.go index fa9d9ff71..eca33733b 100644 --- a/input.go +++ b/input.go @@ -88,7 +88,8 @@ func KeyName(key Key) string { // // CursorPosition is concurrent-safe. func CursorPosition() (x, y int) { - return theInputState.cursorPosition() + cx, cy := theInputState.cursorPosition() + return int(cx), int(cy) } // Wheel returns x and y offsets of the mouse wheel or touchpad scroll. @@ -427,7 +428,7 @@ func (i *inputState) isKeyPressed(key Key) bool { } } -func (i *inputState) cursorPosition() (int, int) { +func (i *inputState) cursorPosition() (float64, float64) { i.m.Lock() defer i.m.Unlock() return i.state.CursorX, i.state.CursorY diff --git a/internal/ui/input.go b/internal/ui/input.go index fcdbae3c5..5f0202006 100644 --- a/internal/ui/input.go +++ b/internal/ui/input.go @@ -41,8 +41,8 @@ type Touch struct { type InputState struct { KeyPressed [KeyMax + 1]bool MouseButtonPressed [MouseButtonMax + 1]bool - CursorX int - CursorY int + CursorX float64 + CursorY float64 WheelX float64 WheelY float64 Touches []Touch diff --git a/internal/ui/input_glfw.go b/internal/ui/input_glfw.go index c5294a327..8e97424a4 100644 --- a/internal/ui/input_glfw.go +++ b/internal/ui/input_glfw.go @@ -68,7 +68,7 @@ func (u *userInterfaceImpl) updateInputState() error { // AdjustPosition can return NaN at the initialization. if !math.IsNaN(cx) && !math.IsNaN(cy) { - u.inputState.CursorX, u.inputState.CursorY = int(cx), int(cy) + u.inputState.CursorX, u.inputState.CursorY = cx, cy } if err := gamepad.Update(); err != nil { diff --git a/internal/ui/input_js.go b/internal/ui/input_js.go index b909a9903..a6b5eb901 100644 --- a/internal/ui/input_js.go +++ b/internal/ui/input_js.go @@ -114,17 +114,16 @@ func (u *userInterfaceImpl) setMouseCursorFromEvent(e js.Value) { s := u.DeviceScaleFactor() x, y := u.context.clientPositionToLogicalPosition(e.Get("clientX").Float(), e.Get("clientY").Float(), s) - u.origCursorX, u.origCursorY = int(x), int(y) + u.origCursorX, u.origCursorY = x, y if u.cursorMode == CursorModeCaptured { dx, dy := e.Get("movementX").Float()/s, e.Get("movementY").Float()/s - // TODO: Keep float64 values. - u.inputState.CursorX += int(dx) - u.inputState.CursorY += int(dy) + u.inputState.CursorX += dx + u.inputState.CursorY += dy return } - u.inputState.CursorX, u.inputState.CursorY = int(x), int(y) + u.inputState.CursorX, u.inputState.CursorY = x, y } func (u *userInterfaceImpl) recoverCursorPosition() { diff --git a/internal/ui/ui_js.go b/internal/ui/ui_js.go index 6f16b6cdf..c20ce4675 100644 --- a/internal/ui/ui_js.go +++ b/internal/ui/ui_js.go @@ -100,8 +100,8 @@ type userInterfaceImpl struct { context *context inputState InputState - origCursorX int - origCursorY int + origCursorX float64 + origCursorY float64 keyboardLayoutMap js.Value