diff --git a/input.go b/input.go index 341dcb681..8337bc67e 100644 --- a/input.go +++ b/input.go @@ -126,11 +126,7 @@ func IsGamepadButtonPressed(id int, button GamepadButton) bool { // // TouchIDs is concurrent-safe. func TouchIDs() []int { - var ids []int - for _, t := range adjustedTouches() { - ids = append(ids, t.ID) - } - return ids + return input.Get().TouchIDs() } // TouchPosition returns the position for the touch of the specified ID. @@ -139,12 +135,18 @@ func TouchIDs() []int { // // TouchPosition is cuncurrent-safe. func TouchPosition(id int) (int, int) { - for _, t := range adjustedTouches() { - if t.ID == id { - return t.X, t.Y + found := false + for _, i := range input.Get().TouchIDs() { + if id == i { + found = true + break } } - return 0, 0 + if !found { + return 0, 0 + } + + return ui.AdjustPosition(input.Get().TouchPosition(id)) } // Touch is deprecated as of 1.7.0. Use TouchPosition instead. @@ -157,37 +159,29 @@ type Touch interface { } type touch struct { - t *input.Touch + id int + x int + y int } func (t *touch) ID() int { - return t.t.ID + return t.id } 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. func Touches() []Touch { - touches := adjustedTouches() - var copies []Touch - for _, t := range touches { - copies = append(copies, &touch{t}) + var ts []Touch + for _, id := range TouchIDs() { + x, y := TouchPosition(id) + ts = append(ts, &touch{ + id: id, + x: x, + y: y, + }) } - return copies -} - -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 + return ts } diff --git a/internal/input/input.go b/internal/input/input.go index 0f13a6d09..dd4e64389 100644 --- a/internal/input/input.go +++ b/internal/input/input.go @@ -81,19 +81,31 @@ func (i *Input) IsGamepadButtonPressed(id int, button driver.GamepadButton) bool return i.gamepads[id].buttonPressed[button] } -func (in *Input) Touches() []*Touch { - in.m.RLock() - defer in.m.RUnlock() +func (i *Input) TouchIDs() []int { + i.m.RLock() + defer i.m.RUnlock() - if len(in.touches) == 0 { + if len(i.touches) == 0 { return nil } - t := make([]*Touch, len(in.touches)) - for i := 0; i < len(t); i++ { - t[i] = in.touches[i] + var ids []int + for _, t := range i.touches { + 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 {