mobile: Handle touch events

This commit is contained in:
Hajime Hoshi 2016-05-23 02:06:01 +09:00
parent c5163e89f4
commit f251ae8b49
6 changed files with 71 additions and 1 deletions

View File

@ -37,3 +37,17 @@ func Start() error {
func Render() error {
return mobile.Render()
}
// TODO: So many glue codes: Can I reduce those?
func TouchDown(x, y int) {
mobile.TouchDown(x, y)
}
func TouchUp(x, y int) {
mobile.TouchUp(x, y)
}
func TouchMove(x, y int) {
mobile.TouchMove(x, y)
}

View File

@ -15,10 +15,12 @@
package mobile
import (
"fmt"
"log"
"math"
"github.com/hajimehoshi/ebiten"
"github.com/hajimehoshi/ebiten/ebitenutil"
"github.com/hajimehoshi/ebiten/examples/common"
)
@ -50,5 +52,11 @@ func Update(screen *ebiten.Image) error {
if err := screen.DrawImage(gophersImage, op); err != nil {
return err
}
msg := ""
if ebiten.IsMouseButtonPressed(ebiten.MouseButtonLeft) {
x, y := ebiten.CursorPosition()
msg = fmt.Sprintf("(%d, %d)", x, y)
}
ebitenutil.DebugPrint(screen, msg)
return nil
}

View File

@ -18,8 +18,9 @@
package ui
import (
glfw "github.com/go-gl/glfw/v3.1/glfw"
"math"
glfw "github.com/go-gl/glfw/v3.1/glfw"
)
var glfwMouseButtonToMouseButton = map[glfw.MouseButton]MouseButton{

View File

@ -15,3 +15,23 @@
// +build android
package ui
func (i *input) touchDown(x, y int) {
i.m.Lock()
defer i.m.Unlock()
i.mouseButtonPressed[MouseButtonLeft] = true
i.cursorX, i.cursorY = x, y
}
func (i *input) touchUp(x, y int) {
i.m.Lock()
defer i.m.Unlock()
i.mouseButtonPressed[MouseButtonLeft] = false
i.cursorX, i.cursorY = x, y
}
func (i *input) touchMove(x, y int) {
i.m.Lock()
defer i.m.Unlock()
i.cursorX, i.cursorY = x, y
}

View File

@ -130,3 +130,18 @@ func (u *userInterface) ScreenScale() int {
func (u *userInterface) actualScreenScale() int {
return u.scale
}
func TouchDown(x, y int) {
s := currentUI.actualScreenScale()
currentInput.touchDown(x/s, y/s)
}
func TouchUp(x, y int) {
s := currentUI.actualScreenScale()
currentInput.touchUp(x/s, y/s)
}
func TouchMove(x, y int) {
s := currentUI.actualScreenScale()
currentInput.touchMove(x/s, y/s)
}

View File

@ -50,3 +50,15 @@ func Render() error {
}
return ui.Render(chError)
}
func TouchDown(x, y int) {
ui.TouchDown(x, y)
}
func TouchUp(x, y int) {
ui.TouchUp(x, y)
}
func TouchMove(x, y int) {
ui.TouchMove(x, y)
}