input: Refactoring: Make Touch a struct

This commit is contained in:
Hajime Hoshi 2019-03-31 01:08:04 +09:00
parent 853dbdf19d
commit 43ec1e7fb3
5 changed files with 39 additions and 32 deletions

View File

@ -128,7 +128,7 @@ func IsGamepadButtonPressed(id int, button GamepadButton) bool {
func TouchIDs() []int { func TouchIDs() []int {
var ids []int var ids []int
for _, t := range ui.AdjustedTouches() { for _, t := range ui.AdjustedTouches() {
ids = append(ids, t.ID()) ids = append(ids, t.ID)
} }
return ids return ids
} }
@ -140,8 +140,8 @@ func TouchIDs() []int {
// TouchPosition is cuncurrent-safe. // TouchPosition is cuncurrent-safe.
func TouchPosition(id int) (int, int) { func TouchPosition(id int) (int, int) {
for _, t := range ui.AdjustedTouches() { for _, t := range ui.AdjustedTouches() {
if t.ID() == id { if t.ID == id {
return t.Position() return t.X, t.Y
} }
} }
return 0, 0 return 0, 0
@ -156,12 +156,24 @@ type Touch interface {
Position() (x, y int) Position() (x, y int)
} }
type touch struct {
t *input.Touch
}
func (t *touch) ID() int {
return t.t.ID
}
func (t *touch) Position() (x, y int) {
return t.t.X, t.t.Y
}
// Touches is deprecated as of 1.7.0. Use TouchIDs instead. // Touches is deprecated as of 1.7.0. Use TouchIDs instead.
func Touches() []Touch { func Touches() []Touch {
touches := ui.AdjustedTouches() touches := ui.AdjustedTouches()
var copies []Touch var copies []Touch
for _, touch := range touches { for _, t := range touches {
copies = append(copies, touch) copies = append(copies, &touch{t})
} }
return copies return copies
} }

View File

@ -105,23 +105,7 @@ type gamePad struct {
} }
type Touch struct { type Touch struct {
id int ID int
x int X int
y int Y int
}
func NewTouch(id int, x, y int) *Touch {
return &Touch{
id: id,
x: x,
y: y,
}
}
func (t *Touch) ID() int {
return t.id
}
func (t *Touch) Position() (x, y int) {
return t.x, t.y
} }

View File

@ -276,9 +276,9 @@ func (i *Input) updateTouches(e js.Value) {
jj := j.Call("item", i) jj := j.Call("item", i)
id := jj.Get("identifier").Int() id := jj.Get("identifier").Int()
ts[i] = &Touch{ ts[i] = &Touch{
id: id, ID: id,
x: jj.Get("clientX").Int(), X: jj.Get("clientX").Int(),
y: jj.Get("clientY").Int(), Y: jj.Get("clientY").Int(),
} }
} }
i.touches = ts i.touches = ts

View File

@ -128,8 +128,11 @@ func appMain(a app.App) {
s := getDeviceScale() s := getDeviceScale()
x, y := float64(e.X)/s, float64(e.Y)/s x, y := float64(e.X)/s, float64(e.Y)/s
// TODO: Is it ok to cast from int64 to int here? // TODO: Is it ok to cast from int64 to int here?
t := input.NewTouch(int(e.Sequence), int(x), int(y)) touches[e.Sequence] = &input.Touch{
touches[e.Sequence] = t ID: int(e.Sequence),
X: int(x),
Y: int(y),
}
case touch.TypeEnd: case touch.TypeEnd:
delete(touches, e.Sequence) delete(touches, e.Sequence)
} }
@ -356,8 +359,12 @@ func AdjustedTouches() []*input.Touch {
ts := input.Get().Touches() ts := input.Get().Touches()
adjusted := make([]*input.Touch, len(ts)) adjusted := make([]*input.Touch, len(ts))
for i, t := range ts { for i, t := range ts {
x, y := currentUI.adjustPosition(t.Position()) x, y := currentUI.adjustPosition(t.X, t.Y)
adjusted[i] = input.NewTouch(t.ID(), x, y) adjusted[i] = &input.Touch{
ID: t.ID,
X: x,
Y: y,
}
} }
return adjusted return adjusted
} }

View File

@ -33,7 +33,11 @@ var (
func updateTouches() { func updateTouches() {
ts := []*input.Touch{} ts := []*input.Touch{}
for id, position := range touches { for id, position := range touches {
ts = append(ts, input.NewTouch(id, position.x, position.y)) ts = append(ts, &input.Touch{
ID: id,
X: position.x,
Y: position.y,
})
} }
ui.UpdateTouches(ts) ui.UpdateTouches(ts)
} }