From 43ec1e7fb33d7d9ffba44d7ccb8b06048584d9e7 Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Sun, 31 Mar 2019 01:08:04 +0900 Subject: [PATCH] input: Refactoring: Make Touch a struct --- input.go | 22 +++++++++++++++++----- internal/input/input.go | 22 +++------------------- internal/input/input_js.go | 6 +++--- internal/ui/ui_mobile.go | 15 +++++++++++---- mobile/touches_mobile.go | 6 +++++- 5 files changed, 39 insertions(+), 32 deletions(-) diff --git a/input.go b/input.go index 79fe87fb1..4ffaa3c76 100644 --- a/input.go +++ b/input.go @@ -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 } diff --git a/internal/input/input.go b/internal/input/input.go index 57314f16d..0f13a6d09 100644 --- a/internal/input/input.go +++ b/internal/input/input.go @@ -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 } diff --git a/internal/input/input_js.go b/internal/input/input_js.go index 1bae31a9c..1df51ea7e 100644 --- a/internal/input/input_js.go +++ b/internal/input/input_js.go @@ -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 diff --git a/internal/ui/ui_mobile.go b/internal/ui/ui_mobile.go index e384fa2dd..9c45377a4 100644 --- a/internal/ui/ui_mobile.go +++ b/internal/ui/ui_mobile.go @@ -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 } diff --git a/mobile/touches_mobile.go b/mobile/touches_mobile.go index c6e7ab4ad..10ca32ff3 100644 --- a/mobile/touches_mobile.go +++ b/mobile/touches_mobile.go @@ -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) }