input: Replace Touches with TouchIDs and TouchPosition

This commit is contained in:
Hajime Hoshi 2019-03-31 02:57:44 +09:00
parent 0536efd95f
commit 06219db2cc
2 changed files with 45 additions and 39 deletions

View File

@ -126,11 +126,7 @@ func IsGamepadButtonPressed(id int, button GamepadButton) bool {
// //
// TouchIDs is concurrent-safe. // TouchIDs is concurrent-safe.
func TouchIDs() []int { func TouchIDs() []int {
var ids []int return input.Get().TouchIDs()
for _, t := range adjustedTouches() {
ids = append(ids, t.ID)
}
return ids
} }
// TouchPosition returns the position for the touch of the specified ID. // TouchPosition returns the position for the touch of the specified ID.
@ -139,12 +135,18 @@ func TouchIDs() []int {
// //
// TouchPosition is cuncurrent-safe. // TouchPosition is cuncurrent-safe.
func TouchPosition(id int) (int, int) { func TouchPosition(id int) (int, int) {
for _, t := range adjustedTouches() { found := false
if t.ID == id { for _, i := range input.Get().TouchIDs() {
return t.X, t.Y if id == i {
found = true
break
} }
} }
if !found {
return 0, 0 return 0, 0
}
return ui.AdjustPosition(input.Get().TouchPosition(id))
} }
// Touch is deprecated as of 1.7.0. Use TouchPosition instead. // Touch is deprecated as of 1.7.0. Use TouchPosition instead.
@ -157,37 +159,29 @@ type Touch interface {
} }
type touch struct { type touch struct {
t *input.Touch id int
x int
y int
} }
func (t *touch) ID() int { func (t *touch) ID() int {
return t.t.ID return t.id
} }
func (t *touch) Position() (x, y int) { func (t *touch) Position() (x, y int) {
return t.t.X, t.t.Y return t.x, t.y
} }
// Touches is deprecated as of 1.7.0. Use TouchIDs instead. // Touches is deprecated as of 1.7.0. Use TouchIDs instead.
func Touches() []Touch { func Touches() []Touch {
touches := adjustedTouches() var ts []Touch
var copies []Touch for _, id := range TouchIDs() {
for _, t := range touches { x, y := TouchPosition(id)
copies = append(copies, &touch{t}) ts = append(ts, &touch{
id: id,
x: x,
y: y,
})
} }
return copies return ts
}
func adjustedTouches() []*input.Touch {
ts := input.Get().Touches()
adjusted := make([]*input.Touch, len(ts))
for i, t := range ts {
x, y := ui.AdjustPosition(t.X, t.Y)
adjusted[i] = &input.Touch{
ID: t.ID,
X: x,
Y: y,
}
}
return adjusted
} }

View File

@ -81,19 +81,31 @@ func (i *Input) IsGamepadButtonPressed(id int, button driver.GamepadButton) bool
return i.gamepads[id].buttonPressed[button] return i.gamepads[id].buttonPressed[button]
} }
func (in *Input) Touches() []*Touch { func (i *Input) TouchIDs() []int {
in.m.RLock() i.m.RLock()
defer in.m.RUnlock() defer i.m.RUnlock()
if len(in.touches) == 0 { if len(i.touches) == 0 {
return nil return nil
} }
t := make([]*Touch, len(in.touches)) var ids []int
for i := 0; i < len(t); i++ { for _, t := range i.touches {
t[i] = in.touches[i] ids = append(ids, t.ID)
} }
return t return ids
}
func (i *Input) TouchPosition(id int) (x, y int) {
i.m.RLock()
defer i.m.RUnlock()
for _, t := range i.touches {
if id == t.ID {
return t.X, t.Y
}
}
return 0, 0
} }
type gamePad struct { type gamePad struct {