diff --git a/_docs/index.tmpl.html b/_docs/index.tmpl.html
index d7292c062..42fa76d06 100644
--- a/_docs/index.tmpl.html
+++ b/_docs/index.tmpl.html
@@ -81,6 +81,7 @@ pre {
v1.2.0-rc1 released.
- Support for gamepads
+ - Support for touch events
- Added new functions for image rendering:
- Image.DrawFilledRect
diff --git a/_docs/public/index.html b/_docs/public/index.html
index ad3a89749..c114d4156 100644
--- a/_docs/public/index.html
+++ b/_docs/public/index.html
@@ -107,6 +107,7 @@ pre {
- v1.2.0-rc1 released.
- Support for gamepads
+ - Support for touch events
- Added new functions for image rendering:
- Image.DrawFilledRect
diff --git a/internal/ui/input_js.go b/internal/ui/input_js.go
index bdcdf111b..1589241e4 100644
--- a/internal/ui/input_js.go
+++ b/internal/ui/input_js.go
@@ -60,7 +60,7 @@ func (i *input) mouseUp(button int) {
}
}
-func (i *input) mouseMove(x, y int) {
+func (i *input) setMouseCursor(x, y int) {
i.cursorX, i.cursorY = x, y
}
diff --git a/internal/ui/ui_js.go b/internal/ui/ui_js.go
index d4151d4fa..8af9d97a6 100644
--- a/internal/ui/ui_js.go
+++ b/internal/ui/ui_js.go
@@ -135,21 +135,60 @@ func init() {
e.Call("preventDefault")
button := e.Get("button").Int()
currentInput.mouseDown(button)
+ setMouseCursorFromEvent(e)
})
canvas.Call("addEventListener", "mouseup", func(e js.Object) {
e.Call("preventDefault")
button := e.Get("button").Int()
currentInput.mouseUp(button)
+ setMouseCursorFromEvent(e)
+ })
+ canvas.Call("addEventListener", "mousemove", func(e js.Object) {
+ e.Call("preventDefault")
+ setMouseCursorFromEvent(e)
})
canvas.Call("addEventListener", "contextmenu", func(e js.Object) {
e.Call("preventDefault")
})
+ // Touch (emulating mouse events)
+ // TODO: Need to create indimendent touch functions?
+ canvas.Call("addEventListener", "touchstart", func(e js.Object) {
+ e.Call("preventDefault")
+ currentInput.mouseDown(0)
+ touches := e.Get("changedTouches")
+ touch := touches.Index(0)
+ setMouseCursorFromEvent(touch)
+ })
+ canvas.Call("addEventListener", "touchend", func(e js.Object) {
+ e.Call("preventDefault")
+ currentInput.mouseUp(0)
+ touches := e.Get("changedTouches")
+ touch := touches.Index(0)
+ setMouseCursorFromEvent(touch)
+ })
+ canvas.Call("addEventListener", "touchmove", func(e js.Object) {
+ e.Call("preventDefault")
+ touches := e.Get("changedTouches")
+ touch := touches.Index(0)
+ setMouseCursorFromEvent(touch)
+ })
+
// Gamepad
window.Call("addEventListener", "gamepadconnected", func(e js.Object) {
+ // Do nothing.
})
}
+func setMouseCursorFromEvent(e js.Object) {
+ scale := canvas.Get("dataset").Get("ebitenScale").Int() // TODO: Float?
+ rect := canvas.Call("getBoundingClientRect")
+ x, y := e.Get("clientX").Int(), e.Get("clientY").Int()
+ x -= rect.Get("left").Int()
+ y -= rect.Get("top").Int()
+ currentInput.setMouseCursor(x/scale, y/scale)
+}
+
func devicePixelRatio() int {
// TODO: What if ratio is not an integer but a float?
ratio := js.Global.Get("window").Get("devicePixelRatio").Int()
@@ -165,6 +204,7 @@ func Start(width, height, scale int, title string) (actualScale int, err error)
actualScale = scale * devicePixelRatio()
canvas.Set("width", width*actualScale)
canvas.Set("height", height*actualScale)
+ canvas.Get("dataset").Set("ebitenScale", scale)
canvasStyle := canvas.Get("style")
cssWidth := width * scale
@@ -175,13 +215,6 @@ func Start(width, height, scale int, title string) (actualScale int, err error)
canvasStyle.Set("left", "calc(50% - "+strconv.Itoa(cssWidth/2)+"px)")
canvasStyle.Set("top", "calc(50% - "+strconv.Itoa(cssHeight/2)+"px)")
- canvas.Call("addEventListener", "mousemove", func(e js.Object) {
- rect := canvas.Call("getBoundingClientRect")
- x, y := e.Get("clientX").Int(), e.Get("clientY").Int()
- x -= rect.Get("left").Int()
- y -= rect.Get("top").Int()
- currentInput.mouseMove(x/scale, y/scale)
- })
canvas.Call("focus")
return actualScale, nil