mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2025-01-11 19:48:54 +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
|
return err
|
||||||
}
|
}
|
||||||
msg := ""
|
msg := ""
|
||||||
if ebiten.IsMouseButtonPressed(ebiten.MouseButtonLeft) {
|
for _, t := range ebiten.Touches() {
|
||||||
x, y := ebiten.CursorPosition()
|
x, y := t.Position()
|
||||||
msg = fmt.Sprintf("(%d, %d)", x, y)
|
msg += fmt.Sprintf("ID: %d, (%d, %d)\n", t.ID(), x, y)
|
||||||
}
|
}
|
||||||
ebitenutil.DebugPrint(screen, msg)
|
ebitenutil.DebugPrint(screen, msg)
|
||||||
return nil
|
return nil
|
||||||
|
@ -16,22 +16,14 @@
|
|||||||
|
|
||||||
package ui
|
package ui
|
||||||
|
|
||||||
func (i *input) touchDown(x, y int) {
|
func (i *input) updateTouches(touches []Touch) {
|
||||||
i.m.Lock()
|
i.m.Lock()
|
||||||
defer i.m.Unlock()
|
defer i.m.Unlock()
|
||||||
i.mouseButtonPressed[MouseButtonLeft] = true
|
ts := make([]touch, len(touches))
|
||||||
i.cursorX, i.cursorY = x, y
|
for i := 0; i < len(ts); i++ {
|
||||||
}
|
ts[i].id = touches[i].ID()
|
||||||
|
x, y := touches[i].Position()
|
||||||
func (i *input) touchUp(x, y int) {
|
ts[i].x, ts[i].y = x, y
|
||||||
i.m.Lock()
|
}
|
||||||
defer i.m.Unlock()
|
i.touches = ts
|
||||||
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
|
|
||||||
}
|
}
|
||||||
|
@ -149,17 +149,6 @@ func Resume() {
|
|||||||
}()
|
}()
|
||||||
}
|
}
|
||||||
|
|
||||||
func TouchDown(x, y int) {
|
func UpdateTouches(touches []Touch) {
|
||||||
s := currentUI.actualScreenScale()
|
currentInput.updateTouches(touches)
|
||||||
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)
|
|
||||||
}
|
}
|
||||||
|
@ -32,9 +32,9 @@ type EventDispatcher interface {
|
|||||||
Render() error
|
Render() error
|
||||||
Pause()
|
Pause()
|
||||||
Resume()
|
Resume()
|
||||||
TouchDown(x, y int)
|
TouchDown(id int, x, y int)
|
||||||
TouchUp(x, y int)
|
TouchMove(id int, x, y int)
|
||||||
TouchMove(x, y int)
|
TouchUp(id int)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Start starts the game and returns immediately.
|
// 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.
|
// 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) {
|
func Start(f func(*ebiten.Image) error, width, height, scale int, title string) (EventDispatcher, error) {
|
||||||
chError = ebiten.RunWithoutMainLoop(f, width, height, scale, title)
|
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 {
|
type eventDispatcher struct {
|
||||||
|
touches map[int]position
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *eventDispatcher) SetScreenSize(width, height int) {
|
func (e *eventDispatcher) SetScreenSize(width, height int) {
|
||||||
@ -74,14 +82,41 @@ func (e *eventDispatcher) Resume() {
|
|||||||
ui.Resume()
|
ui.Resume()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *eventDispatcher) TouchDown(x, y int) {
|
// touch implements ui.Touch.
|
||||||
ui.TouchDown(x, y)
|
type touch struct {
|
||||||
|
id int
|
||||||
|
position position
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *eventDispatcher) TouchUp(x, y int) {
|
func (t touch) ID() int {
|
||||||
ui.TouchUp(x, y)
|
return t.id
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *eventDispatcher) TouchMove(x, y int) {
|
func (t touch) Position() (int, int) {
|
||||||
ui.TouchMove(x, y)
|
// 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