diff --git a/internal/input/input.go b/internal/input/input.go index dd4e64389..976fa5be8 100644 --- a/internal/input/input.go +++ b/internal/input/input.go @@ -20,6 +20,11 @@ import ( var theInput = &Input{} +type pos struct { + X int + Y int +} + func Get() *Input { return theInput } @@ -90,8 +95,8 @@ func (i *Input) TouchIDs() []int { } var ids []int - for _, t := range i.touches { - ids = append(ids, t.ID) + for id := range i.touches { + ids = append(ids, id) } return ids } @@ -100,9 +105,9 @@ 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 + for tid, pos := range i.touches { + if id == tid { + return pos.X, pos.Y } } return 0, 0 diff --git a/internal/input/input_glfw.go b/internal/input/input_glfw.go index 3aa9da81b..84c21c900 100644 --- a/internal/input/input_glfw.go +++ b/internal/input/input_glfw.go @@ -36,7 +36,7 @@ type Input struct { cursorX int cursorY int gamepads [16]gamePad - touches []*Touch // This is not updated until GLFW 3.3 is available (#417) + touches map[int]pos // This is not updated until GLFW 3.3 is available (#417) runeBuffer []rune m sync.RWMutex } diff --git a/internal/input/input_js.go b/internal/input/input_js.go index 1df51ea7e..b20c1c9b0 100644 --- a/internal/input/input_js.go +++ b/internal/input/input_js.go @@ -40,7 +40,7 @@ type Input struct { wheelX float64 wheelY float64 gamepads [16]gamePad - touches []*Touch + touches map[int]pos runeBuffer []rune m mockRWLock } @@ -271,14 +271,13 @@ func setMouseCursorFromEvent(e js.Value) { func (i *Input) updateTouches(e js.Value) { j := e.Get("targetTouches") - ts := make([]*Touch, j.Get("length").Int()) + ts := map[int]pos{} for i := 0; i < len(ts); i++ { jj := j.Call("item", i) id := jj.Get("identifier").Int() - ts[i] = &Touch{ - ID: id, - X: jj.Get("clientX").Int(), - Y: jj.Get("clientY").Int(), + ts[id] = pos{ + X: jj.Get("clientX").Int(), + Y: jj.Get("clientY").Int(), } } i.touches = ts diff --git a/internal/input/input_mobile.go b/internal/input/input_mobile.go index 515c8b2a6..b1e7f35dc 100644 --- a/internal/input/input_mobile.go +++ b/internal/input/input_mobile.go @@ -26,7 +26,7 @@ type Input struct { cursorX int cursorY int gamepads [16]gamePad - touches []*Touch + touches map[int]pos m sync.RWMutex } @@ -48,6 +48,12 @@ func (i *Input) IsMouseButtonPressed(key driver.MouseButton) bool { func (i *Input) SetTouches(touches []*Touch) { i.m.Lock() - i.touches = touches // TODO: Need copy? + i.touches = map[int]pos{} + for _, t := range touches { + i.touches[t.ID] = pos{ + X: t.X, + Y: t.Y, + } + } i.m.Unlock() }