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{} var theInput = &Input{}
type pos struct {
X int
Y int
}
func Get() *Input { func Get() *Input {
return theInput return theInput
} }
@ -90,8 +95,8 @@ func (i *Input) TouchIDs() []int {
} }
var ids []int var ids []int
for _, t := range i.touches { for id := range i.touches {
ids = append(ids, t.ID) ids = append(ids, id)
} }
return ids return ids
} }
@ -100,9 +105,9 @@ func (i *Input) TouchPosition(id int) (x, y int) {
i.m.RLock() i.m.RLock()
defer i.m.RUnlock() defer i.m.RUnlock()
for _, t := range i.touches { for tid, pos := range i.touches {
if id == t.ID { if id == tid {
return t.X, t.Y return pos.X, pos.Y
} }
} }
return 0, 0 return 0, 0

View File

@ -36,7 +36,7 @@ type Input struct {
cursorX int cursorX int
cursorY int cursorY int
gamepads [16]gamePad 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 runeBuffer []rune
m sync.RWMutex m sync.RWMutex
} }

View File

@ -40,7 +40,7 @@ type Input struct {
wheelX float64 wheelX float64
wheelY float64 wheelY float64
gamepads [16]gamePad gamepads [16]gamePad
touches []*Touch touches map[int]pos
runeBuffer []rune runeBuffer []rune
m mockRWLock m mockRWLock
} }
@ -271,12 +271,11 @@ func setMouseCursorFromEvent(e js.Value) {
func (i *Input) updateTouches(e js.Value) { func (i *Input) updateTouches(e js.Value) {
j := e.Get("targetTouches") j := e.Get("targetTouches")
ts := make([]*Touch, j.Get("length").Int()) ts := map[int]pos{}
for i := 0; i < len(ts); i++ { for i := 0; i < len(ts); i++ {
jj := j.Call("item", i) jj := j.Call("item", i)
id := jj.Get("identifier").Int() id := jj.Get("identifier").Int()
ts[i] = &Touch{ ts[id] = pos{
ID: id,
X: jj.Get("clientX").Int(), X: jj.Get("clientX").Int(),
Y: jj.Get("clientY").Int(), Y: jj.Get("clientY").Int(),
} }

View File

@ -26,7 +26,7 @@ type Input struct {
cursorX int cursorX int
cursorY int cursorY int
gamepads [16]gamePad gamepads [16]gamePad
touches []*Touch touches map[int]pos
m sync.RWMutex m sync.RWMutex
} }
@ -48,6 +48,12 @@ func (i *Input) IsMouseButtonPressed(key driver.MouseButton) bool {
func (i *Input) SetTouches(touches []*Touch) { func (i *Input) SetTouches(touches []*Touch) {
i.m.Lock() 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() i.m.Unlock()
} }