diff --git a/examples/2048/2048/input.go b/examples/2048/2048/input.go index fb4fafb0b..686b77feb 100644 --- a/examples/2048/2048/input.go +++ b/examples/2048/2048/input.go @@ -149,10 +149,10 @@ func (i *Input) Update() { } switch i.touchState { case touchStateNone: - ts := ebiten.Touches() + ts := ebiten.TouchIDs() if len(ts) == 1 { - i.touchID = ts[0].ID() - x, y := ts[0].Position() + i.touchID = ts[0] + x, y := ebiten.TouchPosition(ts[0]) i.touchInitPosX = x i.touchInitPosY = y i.touchLastPosX = x @@ -160,15 +160,15 @@ func (i *Input) Update() { i.touchState = touchStatePressing } case touchStatePressing: - ts := ebiten.Touches() + ts := ebiten.TouchIDs() if len(ts) >= 2 { break } if len(ts) == 1 { - if ts[0].ID() != i.touchID { + if ts[0] != i.touchID { i.touchState = touchStateInvalid } else { - x, y := ts[0].Position() + x, y := ebiten.TouchPosition(ts[0]) i.touchLastPosX = x i.touchLastPosY = y } @@ -188,7 +188,7 @@ func (i *Input) Update() { case touchStateSettled: i.touchState = touchStateNone case touchStateInvalid: - if len(ebiten.Touches()) == 0 { + if len(ebiten.TouchIDs()) == 0 { i.touchState = touchStateNone } } diff --git a/examples/paint/main.go b/examples/paint/main.go index f17ad6a9d..1fbc51a90 100644 --- a/examples/paint/main.go +++ b/examples/paint/main.go @@ -82,8 +82,8 @@ func update(screen *ebiten.Image) error { } // Paint the brush by touches - for _, t := range ebiten.Touches() { - x, y := t.Position() + for _, t := range ebiten.TouchIDs() { + x, y := ebiten.TouchPosition(t) paint(canvasImage, x, y) drawn = true } @@ -98,9 +98,9 @@ func update(screen *ebiten.Image) error { screen.DrawImage(canvasImage, nil) msg := fmt.Sprintf("(%d, %d)", mx, my) - for _, t := range ebiten.Touches() { - x, y := t.Position() - msg += fmt.Sprintf("\n(%d, %d) touch %d", x, y, t.ID()) + for _, t := range ebiten.TouchIDs() { + x, y := ebiten.TouchPosition(t) + msg += fmt.Sprintf("\n(%d, %d) touch %d", x, y, t) } ebitenutil.DebugPrint(screen, msg) return nil diff --git a/input.go b/input.go index f79acf9ef..7c561fc2d 100644 --- a/input.go +++ b/input.go @@ -110,7 +110,35 @@ func IsGamepadButtonPressed(id int, button GamepadButton) bool { return input.Get().IsGamepadButtonPressed(id, input.GamepadButton(button)) } -// Touch represents a touch state. +// TouchIDs returns the current touch states. +// +// TouchIDs returns nil when there are no touches. +// TouchIDs always returns nil on desktops. +// +// TouchIDs is concurrent-safe. +func TouchIDs() []int { + var ids []int + for _, t := range ui.AdjustedTouches() { + ids = append(ids, t.ID()) + } + return ids +} + +// TouchPosition returns the position for the touch of the specified ID. +// +// If the touch of the specified ID is not present, TouchPosition returns (0, 0). +// +// TouchPosition is cuncurrent-safe. +func TouchPosition(id int) (int, int) { + for _, t := range ui.AdjustedTouches() { + if t.ID() == id { + return t.Position() + } + } + return 0, 0 +} + +// Touch is deprecated as of 1.7.0. Use TouchPosition instead. type Touch interface { // ID returns an identifier for one stroke. ID() int @@ -119,10 +147,7 @@ type Touch interface { Position() (x, y int) } -// Touches returns the current touch states. -// -// Touches returns nil when there are no touches. -// Touches always returns nil on desktops. +// Touches is deprecated as of 1.7.0. Use TouchIDs instead. func Touches() []Touch { touches := ui.AdjustedTouches() var copies []Touch diff --git a/inpututil/inpututil.go b/inpututil/inpututil.go index 1d4682945..7ca172b72 100644 --- a/inpututil/inpututil.go +++ b/inpututil/inpututil.go @@ -150,10 +150,10 @@ func (i *inputState) update() { i.prevTouchDurations[id] = 0 } - for _, t := range ebiten.Touches() { - ids[t.ID()] = struct{}{} - i.prevTouchDurations[t.ID()] = i.touchDurations[t.ID()] - i.touchDurations[t.ID()]++ + for _, id := range ebiten.TouchIDs() { + ids[id] = struct{}{} + i.prevTouchDurations[id] = i.touchDurations[id] + i.touchDurations[id]++ } idsToDelete = []int{} for id := range i.touchDurations {