mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-12-25 03:08:54 +01:00
input: Adjust mouse cursor position in ui package
This commit is contained in:
parent
4de2dc0240
commit
7d2fd9654a
@ -227,23 +227,23 @@ func OnKeyUp(e *js.Object) {
|
|||||||
theInput.keyUp(code)
|
theInput.keyUp(code)
|
||||||
}
|
}
|
||||||
|
|
||||||
func OnMouseDown(e *js.Object, scale float64, left, top int) {
|
func OnMouseDown(e *js.Object) {
|
||||||
e.Call("preventDefault")
|
e.Call("preventDefault")
|
||||||
button := e.Get("button").Int()
|
button := e.Get("button").Int()
|
||||||
theInput.mouseDown(button)
|
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")
|
e.Call("preventDefault")
|
||||||
button := e.Get("button").Int()
|
button := e.Get("button").Int()
|
||||||
theInput.mouseUp(button)
|
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")
|
e.Call("preventDefault")
|
||||||
setMouseCursorFromEvent(e, scale, left, top)
|
setMouseCursorFromEvent(e)
|
||||||
}
|
}
|
||||||
|
|
||||||
func OnTouchStart(e *js.Object, scale float64, left, top int) {
|
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))
|
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, y := e.Get("clientX").Int(), e.Get("clientY").Int()
|
||||||
x -= left
|
theInput.setMouseCursor(x, y)
|
||||||
y -= top
|
|
||||||
theInput.setMouseCursor(int(float64(x)/scale), int(float64(y)/scale))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func touchEventToTouches(e *js.Object, scale float64, left, top int) []*Touch {
|
func touchEventToTouches(e *js.Object, scale float64, left, top int) []*Touch {
|
||||||
|
@ -78,7 +78,12 @@ func ScreenPadding() (x0, y0, x1, y1 float64) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func AdjustedCursorPosition() (x, y int) {
|
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 {
|
func IsCursorVisible() bool {
|
||||||
@ -253,18 +258,9 @@ func initialize() error {
|
|||||||
canvas.Call("addEventListener", "keyup", input.OnKeyUp)
|
canvas.Call("addEventListener", "keyup", input.OnKeyUp)
|
||||||
|
|
||||||
// Mouse
|
// Mouse
|
||||||
canvas.Call("addEventListener", "mousedown", func(e *js.Object) {
|
canvas.Call("addEventListener", "mousedown", input.OnMouseDown)
|
||||||
rect := canvas.Call("getBoundingClientRect")
|
canvas.Call("addEventListener", "mouseup", input.OnMouseUp)
|
||||||
input.OnMouseDown(e, currentUI.getScale(), rect.Get("left").Int(), rect.Get("top").Int())
|
canvas.Call("addEventListener", "mousemove", input.OnMouseMove)
|
||||||
})
|
|
||||||
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", "contextmenu", func(e *js.Object) {
|
canvas.Call("addEventListener", "contextmenu", func(e *js.Object) {
|
||||||
e.Call("preventDefault")
|
e.Call("preventDefault")
|
||||||
})
|
})
|
||||||
|
Loading…
Reference in New Issue
Block a user