mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2025-01-26 02:42:02 +01:00
Support touch events (esp. for mobile browsers) (#76)
This commit is contained in:
parent
3aed28b4a7
commit
5b41374ac7
@ -81,6 +81,7 @@ pre {
|
|||||||
<li>v1.2.0-rc1 released.
|
<li>v1.2.0-rc1 released.
|
||||||
<ul>
|
<ul>
|
||||||
<li>Support for gamepads</li>
|
<li>Support for gamepads</li>
|
||||||
|
<li>Support for touch events</li>
|
||||||
<li>Added new functions for image rendering:
|
<li>Added new functions for image rendering:
|
||||||
<ul>
|
<ul>
|
||||||
<li>Image.DrawFilledRect</li>
|
<li>Image.DrawFilledRect</li>
|
||||||
|
@ -107,6 +107,7 @@ pre {
|
|||||||
<li>v1.2.0-rc1 released.
|
<li>v1.2.0-rc1 released.
|
||||||
<ul>
|
<ul>
|
||||||
<li>Support for gamepads</li>
|
<li>Support for gamepads</li>
|
||||||
|
<li>Support for touch events</li>
|
||||||
<li>Added new functions for image rendering:
|
<li>Added new functions for image rendering:
|
||||||
<ul>
|
<ul>
|
||||||
<li>Image.DrawFilledRect</li>
|
<li>Image.DrawFilledRect</li>
|
||||||
|
@ -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
|
i.cursorX, i.cursorY = x, y
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -135,21 +135,60 @@ func init() {
|
|||||||
e.Call("preventDefault")
|
e.Call("preventDefault")
|
||||||
button := e.Get("button").Int()
|
button := e.Get("button").Int()
|
||||||
currentInput.mouseDown(button)
|
currentInput.mouseDown(button)
|
||||||
|
setMouseCursorFromEvent(e)
|
||||||
})
|
})
|
||||||
canvas.Call("addEventListener", "mouseup", func(e js.Object) {
|
canvas.Call("addEventListener", "mouseup", func(e js.Object) {
|
||||||
e.Call("preventDefault")
|
e.Call("preventDefault")
|
||||||
button := e.Get("button").Int()
|
button := e.Get("button").Int()
|
||||||
currentInput.mouseUp(button)
|
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) {
|
canvas.Call("addEventListener", "contextmenu", func(e js.Object) {
|
||||||
e.Call("preventDefault")
|
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
|
// Gamepad
|
||||||
window.Call("addEventListener", "gamepadconnected", func(e js.Object) {
|
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 {
|
func devicePixelRatio() int {
|
||||||
// TODO: What if ratio is not an integer but a float?
|
// TODO: What if ratio is not an integer but a float?
|
||||||
ratio := js.Global.Get("window").Get("devicePixelRatio").Int()
|
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()
|
actualScale = scale * devicePixelRatio()
|
||||||
canvas.Set("width", width*actualScale)
|
canvas.Set("width", width*actualScale)
|
||||||
canvas.Set("height", height*actualScale)
|
canvas.Set("height", height*actualScale)
|
||||||
|
canvas.Get("dataset").Set("ebitenScale", scale)
|
||||||
canvasStyle := canvas.Get("style")
|
canvasStyle := canvas.Get("style")
|
||||||
|
|
||||||
cssWidth := width * scale
|
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("left", "calc(50% - "+strconv.Itoa(cssWidth/2)+"px)")
|
||||||
canvasStyle.Set("top", "calc(50% - "+strconv.Itoa(cssHeight/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")
|
canvas.Call("focus")
|
||||||
|
|
||||||
return actualScale, nil
|
return actualScale, nil
|
||||||
|
Loading…
Reference in New Issue
Block a user