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 {
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
}
}

View File

@ -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

View File

@ -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

View File

@ -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 {