input: Change touches type

This commit is contained in:
Hajime Hoshi 2019-03-31 16:28:50 +09:00
parent 64d3e26fb0
commit 9e5261c156
4 changed files with 24 additions and 14 deletions

View File

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

View File

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

View File

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

View File

@ -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()
}