input: Adjust mouse cursor position in ui package

This commit is contained in:
Hajime Hoshi 2018-04-01 23:53:26 +09:00
parent 4de2dc0240
commit 7d2fd9654a
2 changed files with 17 additions and 23 deletions

View File

@ -227,23 +227,23 @@ func OnKeyUp(e *js.Object) {
theInput.keyUp(code)
}
func OnMouseDown(e *js.Object, scale float64, left, top int) {
func OnMouseDown(e *js.Object) {
e.Call("preventDefault")
button := e.Get("button").Int()
theInput.mouseDown(button)
setMouseCursorFromEvent(e, scale, left, top)
setMouseCursorFromEvent(e)
}
func OnMouseUp(e *js.Object, scale float64, left, top int) {
func OnMouseUp(e *js.Object) {
e.Call("preventDefault")
button := e.Get("button").Int()
theInput.mouseUp(button)
setMouseCursorFromEvent(e, scale, left, top)
setMouseCursorFromEvent(e)
}
func OnMouseMove(e *js.Object, scale float64, left, top int) {
func OnMouseMove(e *js.Object) {
e.Call("preventDefault")
setMouseCursorFromEvent(e, scale, left, top)
setMouseCursorFromEvent(e)
}
func OnTouchStart(e *js.Object, scale float64, left, top int) {
@ -261,11 +261,9 @@ func OnTouchMove(e *js.Object, scale float64, left, top int) {
theInput.updateTouches(touchEventToTouches(e, scale, left, top))
}
func setMouseCursorFromEvent(e *js.Object, scale float64, left, top int) {
func setMouseCursorFromEvent(e *js.Object) {
x, y := e.Get("clientX").Int(), e.Get("clientY").Int()
x -= left
y -= top
theInput.setMouseCursor(int(float64(x)/scale), int(float64(y)/scale))
theInput.setMouseCursor(x, y)
}
func touchEventToTouches(e *js.Object, scale float64, left, top int) []*Touch {

View File

@ -78,7 +78,12 @@ func ScreenPadding() (x0, y0, x1, y1 float64) {
}
func AdjustedCursorPosition() (x, y int) {
return input.Get().CursorPosition()
x, y = input.Get().CursorPosition()
rect := canvas.Call("getBoundingClientRect")
x -= rect.Get("left").Int()
y -= rect.Get("top").Int()
scale := currentUI.getScale()
return int(float64(x) / scale), int(float64(y) / scale)
}
func IsCursorVisible() bool {
@ -253,18 +258,9 @@ func initialize() error {
canvas.Call("addEventListener", "keyup", input.OnKeyUp)
// Mouse
canvas.Call("addEventListener", "mousedown", func(e *js.Object) {
rect := canvas.Call("getBoundingClientRect")
input.OnMouseDown(e, currentUI.getScale(), rect.Get("left").Int(), rect.Get("top").Int())
})
canvas.Call("addEventListener", "mouseup", func(e *js.Object) {
rect := canvas.Call("getBoundingClientRect")
input.OnMouseUp(e, currentUI.getScale(), rect.Get("left").Int(), rect.Get("top").Int())
})
canvas.Call("addEventListener", "mousemove", func(e *js.Object) {
rect := canvas.Call("getBoundingClientRect")
input.OnMouseMove(e, currentUI.getScale(), rect.Get("left").Int(), rect.Get("top").Int())
})
canvas.Call("addEventListener", "mousedown", input.OnMouseDown)
canvas.Call("addEventListener", "mouseup", input.OnMouseUp)
canvas.Call("addEventListener", "mousemove", input.OnMouseMove)
canvas.Call("addEventListener", "contextmenu", func(e *js.Object) {
e.Call("preventDefault")
})