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 {
var ids []int
for _, t := range ui.AdjustedTouches() {
ids = append(ids, t.ID())
ids = append(ids, t.ID)
}
return ids
}
@ -140,8 +140,8 @@ func TouchIDs() []int {
// TouchPosition is cuncurrent-safe.
func TouchPosition(id int) (int, int) {
for _, t := range ui.AdjustedTouches() {
if t.ID() == id {
return t.Position()
if t.ID == id {
return t.X, t.Y
}
}
return 0, 0
@ -156,12 +156,24 @@ type Touch interface {
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.
func Touches() []Touch {
touches := ui.AdjustedTouches()
var copies []Touch
for _, touch := range touches {
copies = append(copies, touch)
for _, t := range touches {
copies = append(copies, &touch{t})
}
return copies
}

View File

@ -105,23 +105,7 @@ type gamePad struct {
}
type Touch struct {
id int
x 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
ID int
X int
Y int
}

View File

@ -276,9 +276,9 @@ func (i *Input) updateTouches(e js.Value) {
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(),
ID: id,
X: jj.Get("clientX").Int(),
Y: jj.Get("clientY").Int(),
}
}
i.touches = ts

View File

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

View File

@ -33,7 +33,11 @@ var (
func updateTouches() {
ts := []*input.Touch{}
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)
}