input: Add TouchIDs and TouchPosition (#497)

This change deprecates Touch interface and Touches function.
This commit is contained in:
Hajime Hoshi 2018-05-07 00:31:43 +09:00
parent 167d21ec46
commit fbeed96bb5
4 changed files with 46 additions and 21 deletions

View File

@ -149,10 +149,10 @@ func (i *Input) Update() {
} }
switch i.touchState { switch i.touchState {
case touchStateNone: case touchStateNone:
ts := ebiten.Touches() ts := ebiten.TouchIDs()
if len(ts) == 1 { if len(ts) == 1 {
i.touchID = ts[0].ID() i.touchID = ts[0]
x, y := ts[0].Position() x, y := ebiten.TouchPosition(ts[0])
i.touchInitPosX = x i.touchInitPosX = x
i.touchInitPosY = y i.touchInitPosY = y
i.touchLastPosX = x i.touchLastPosX = x
@ -160,15 +160,15 @@ func (i *Input) Update() {
i.touchState = touchStatePressing i.touchState = touchStatePressing
} }
case touchStatePressing: case touchStatePressing:
ts := ebiten.Touches() ts := ebiten.TouchIDs()
if len(ts) >= 2 { if len(ts) >= 2 {
break break
} }
if len(ts) == 1 { if len(ts) == 1 {
if ts[0].ID() != i.touchID { if ts[0] != i.touchID {
i.touchState = touchStateInvalid i.touchState = touchStateInvalid
} else { } else {
x, y := ts[0].Position() x, y := ebiten.TouchPosition(ts[0])
i.touchLastPosX = x i.touchLastPosX = x
i.touchLastPosY = y i.touchLastPosY = y
} }
@ -188,7 +188,7 @@ func (i *Input) Update() {
case touchStateSettled: case touchStateSettled:
i.touchState = touchStateNone i.touchState = touchStateNone
case touchStateInvalid: case touchStateInvalid:
if len(ebiten.Touches()) == 0 { if len(ebiten.TouchIDs()) == 0 {
i.touchState = touchStateNone i.touchState = touchStateNone
} }
} }

View File

@ -82,8 +82,8 @@ func update(screen *ebiten.Image) error {
} }
// Paint the brush by touches // Paint the brush by touches
for _, t := range ebiten.Touches() { for _, t := range ebiten.TouchIDs() {
x, y := t.Position() x, y := ebiten.TouchPosition(t)
paint(canvasImage, x, y) paint(canvasImage, x, y)
drawn = true drawn = true
} }
@ -98,9 +98,9 @@ func update(screen *ebiten.Image) error {
screen.DrawImage(canvasImage, nil) screen.DrawImage(canvasImage, nil)
msg := fmt.Sprintf("(%d, %d)", mx, my) msg := fmt.Sprintf("(%d, %d)", mx, my)
for _, t := range ebiten.Touches() { for _, t := range ebiten.TouchIDs() {
x, y := t.Position() x, y := ebiten.TouchPosition(t)
msg += fmt.Sprintf("\n(%d, %d) touch %d", x, y, t.ID()) msg += fmt.Sprintf("\n(%d, %d) touch %d", x, y, t)
} }
ebitenutil.DebugPrint(screen, msg) ebitenutil.DebugPrint(screen, msg)
return nil return nil

View File

@ -110,7 +110,35 @@ func IsGamepadButtonPressed(id int, button GamepadButton) bool {
return input.Get().IsGamepadButtonPressed(id, input.GamepadButton(button)) 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 { type Touch interface {
// ID returns an identifier for one stroke. // ID returns an identifier for one stroke.
ID() int ID() int
@ -119,10 +147,7 @@ type Touch interface {
Position() (x, y int) Position() (x, y int)
} }
// Touches returns the current touch states. // Touches is deprecated as of 1.7.0. Use TouchIDs instead.
//
// Touches returns nil when there are no touches.
// Touches always returns nil on desktops.
func Touches() []Touch { func Touches() []Touch {
touches := ui.AdjustedTouches() touches := ui.AdjustedTouches()
var copies []Touch var copies []Touch

View File

@ -150,10 +150,10 @@ func (i *inputState) update() {
i.prevTouchDurations[id] = 0 i.prevTouchDurations[id] = 0
} }
for _, t := range ebiten.Touches() { for _, id := range ebiten.TouchIDs() {
ids[t.ID()] = struct{}{} ids[id] = struct{}{}
i.prevTouchDurations[t.ID()] = i.touchDurations[t.ID()] i.prevTouchDurations[id] = i.touchDurations[id]
i.touchDurations[t.ID()]++ i.touchDurations[id]++
} }
idsToDelete = []int{} idsToDelete = []int{}
for id := range i.touchDurations { for id := range i.touchDurations {