mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-12-24 10:48:53 +01:00
mobile: Multitouches (#101)
This commit is contained in:
parent
405ad9a136
commit
e4ca01db31
@ -131,9 +131,9 @@ func Update(screen *ebiten.Image) error {
|
||||
return err
|
||||
}
|
||||
msg := ""
|
||||
if ebiten.IsMouseButtonPressed(ebiten.MouseButtonLeft) {
|
||||
x, y := ebiten.CursorPosition()
|
||||
msg = fmt.Sprintf("(%d, %d)", x, y)
|
||||
for _, t := range ebiten.Touches() {
|
||||
x, y := t.Position()
|
||||
msg += fmt.Sprintf("ID: %d, (%d, %d)\n", t.ID(), x, y)
|
||||
}
|
||||
ebitenutil.DebugPrint(screen, msg)
|
||||
return nil
|
||||
|
@ -16,22 +16,14 @@
|
||||
|
||||
package ui
|
||||
|
||||
func (i *input) touchDown(x, y int) {
|
||||
func (i *input) updateTouches(touches []Touch) {
|
||||
i.m.Lock()
|
||||
defer i.m.Unlock()
|
||||
i.mouseButtonPressed[MouseButtonLeft] = true
|
||||
i.cursorX, i.cursorY = x, y
|
||||
ts := make([]touch, len(touches))
|
||||
for i := 0; i < len(ts); i++ {
|
||||
ts[i].id = touches[i].ID()
|
||||
x, y := touches[i].Position()
|
||||
ts[i].x, ts[i].y = 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
|
||||
i.touches = ts
|
||||
}
|
||||
|
@ -149,17 +149,6 @@ func Resume() {
|
||||
}()
|
||||
}
|
||||
|
||||
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)
|
||||
func UpdateTouches(touches []Touch) {
|
||||
currentInput.updateTouches(touches)
|
||||
}
|
||||
|
@ -32,9 +32,9 @@ type EventDispatcher interface {
|
||||
Render() error
|
||||
Pause()
|
||||
Resume()
|
||||
TouchDown(x, y int)
|
||||
TouchUp(x, y int)
|
||||
TouchMove(x, y int)
|
||||
TouchDown(id int, x, y int)
|
||||
TouchMove(id int, x, y int)
|
||||
TouchUp(id int)
|
||||
}
|
||||
|
||||
// Start starts the game and returns immediately.
|
||||
@ -42,10 +42,18 @@ type EventDispatcher interface {
|
||||
// Different from ebiten.Run, this invokes only the game loop and not the main (UI) loop.
|
||||
func Start(f func(*ebiten.Image) error, width, height, scale int, title string) (EventDispatcher, error) {
|
||||
chError = ebiten.RunWithoutMainLoop(f, width, height, scale, title)
|
||||
return &eventDispatcher{}, nil
|
||||
return &eventDispatcher{
|
||||
touches: map[int]position{},
|
||||
}, nil
|
||||
}
|
||||
|
||||
type position struct {
|
||||
x int
|
||||
y int
|
||||
}
|
||||
|
||||
type eventDispatcher struct {
|
||||
touches map[int]position
|
||||
}
|
||||
|
||||
func (e *eventDispatcher) SetScreenSize(width, height int) {
|
||||
@ -74,14 +82,41 @@ func (e *eventDispatcher) Resume() {
|
||||
ui.Resume()
|
||||
}
|
||||
|
||||
func (e *eventDispatcher) TouchDown(x, y int) {
|
||||
ui.TouchDown(x, y)
|
||||
// touch implements ui.Touch.
|
||||
type touch struct {
|
||||
id int
|
||||
position position
|
||||
}
|
||||
|
||||
func (e *eventDispatcher) TouchUp(x, y int) {
|
||||
ui.TouchUp(x, y)
|
||||
func (t touch) ID() int {
|
||||
return t.id
|
||||
}
|
||||
|
||||
func (e *eventDispatcher) TouchMove(x, y int) {
|
||||
ui.TouchMove(x, y)
|
||||
func (t touch) Position() (int, int) {
|
||||
// TODO: Is this OK to adjust the position here?
|
||||
return t.position.x / ui.CurrentUI().ScreenScale(),
|
||||
t.position.y / ui.CurrentUI().ScreenScale()
|
||||
}
|
||||
|
||||
func (e *eventDispatcher) TouchDown(id int, x, y int) {
|
||||
e.touches[id] = position{x, y}
|
||||
e.updateTouches()
|
||||
}
|
||||
|
||||
func (e *eventDispatcher) TouchMove(id int, x, y int) {
|
||||
e.touches[id] = position{x, y}
|
||||
e.updateTouches()
|
||||
}
|
||||
|
||||
func (e *eventDispatcher) TouchUp(id int) {
|
||||
delete(e.touches, id)
|
||||
e.updateTouches()
|
||||
}
|
||||
|
||||
func (e *eventDispatcher) updateTouches() {
|
||||
ts := []ui.Touch{}
|
||||
for id, position := range e.touches {
|
||||
ts = append(ts, touch{id, position})
|
||||
}
|
||||
ui.UpdateTouches(ts)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user