internal/uidriver/mobile: Refactoring

This commit is contained in:
Hajime Hoshi 2021-10-16 19:55:37 +09:00
parent ad3115b347
commit efdb31a524

View File

@ -21,15 +21,10 @@ import (
"github.com/hajimehoshi/ebiten/v2/internal/driver" "github.com/hajimehoshi/ebiten/v2/internal/driver"
) )
type pos struct {
X int
Y int
}
type Input struct { type Input struct {
keys map[driver.Key]struct{} keys map[driver.Key]struct{}
runes []rune runes []rune
touches map[driver.TouchID]pos touches []Touch
gamepads []Gamepad gamepads []Gamepad
ui *UserInterface ui *UserInterface
} }
@ -156,8 +151,8 @@ func (i *Input) AppendTouchIDs(touchIDs []driver.TouchID) []driver.TouchID {
i.ui.m.RLock() i.ui.m.RLock()
defer i.ui.m.RUnlock() defer i.ui.m.RUnlock()
for id := range i.touches { for _, t := range i.touches {
touchIDs = append(touchIDs, id) touchIDs = append(touchIDs, t.ID)
} }
return touchIDs return touchIDs
} }
@ -166,9 +161,9 @@ func (i *Input) TouchPosition(id driver.TouchID) (x, y int) {
i.ui.m.RLock() i.ui.m.RLock()
defer i.ui.m.RUnlock() defer i.ui.m.RUnlock()
for tid, pos := range i.touches { for _, t := range i.touches {
if id == tid { if t.ID == id {
return i.ui.adjustPosition(pos.X, pos.Y) return i.ui.adjustPosition(t.X, t.Y)
} }
} }
return 0, 0 return 0, 0
@ -184,9 +179,6 @@ func (i *Input) IsKeyPressed(key driver.Key) bool {
i.ui.m.RLock() i.ui.m.RLock()
defer i.ui.m.RUnlock() defer i.ui.m.RUnlock()
if i.keys == nil {
return false
}
_, ok := i.keys[key] _, ok := i.keys[key]
return ok return ok
} }
@ -203,7 +195,12 @@ func (i *Input) update(keys map[driver.Key]struct{}, runes []rune, touches []Tou
i.ui.m.Lock() i.ui.m.Lock()
defer i.ui.m.Unlock() defer i.ui.m.Unlock()
if i.keys == nil {
i.keys = map[driver.Key]struct{}{} i.keys = map[driver.Key]struct{}{}
}
for k := range i.keys {
delete(i.keys, k)
}
for k := range keys { for k := range keys {
i.keys[k] = struct{}{} i.keys[k] = struct{}{}
} }
@ -211,13 +208,8 @@ func (i *Input) update(keys map[driver.Key]struct{}, runes []rune, touches []Tou
i.runes = make([]rune, len(runes)) i.runes = make([]rune, len(runes))
copy(i.runes, runes) copy(i.runes, runes)
i.touches = map[driver.TouchID]pos{} i.touches = make([]Touch, len(touches))
for _, t := range touches { copy(i.touches, touches)
i.touches[t.ID] = pos{
X: t.X,
Y: t.Y,
}
}
i.gamepads = make([]Gamepad, len(gamepads)) i.gamepads = make([]Gamepad, len(gamepads))
copy(i.gamepads, gamepads) copy(i.gamepads, gamepads)