ui: Adjust touch positions at ui package (mobile)

This commit is contained in:
Hajime Hoshi 2018-04-03 01:19:47 +09:00
parent 76cb43a7f8
commit fcf4657a70
4 changed files with 24 additions and 32 deletions

View File

@ -40,17 +40,8 @@ func (i *Input) IsMouseButtonPressed(key MouseButton) bool {
return false return false
} }
func (i *Input) UpdateTouches(touches []*Touch, dx, dy int) { func (i *Input) UpdateTouches(touches []*Touch) {
i.m.Lock() i.m.Lock()
ts := make([]*Touch, len(touches)) i.touches = touches // TODO: Need copy?
for i := 0; i < len(ts); i++ {
x, y := touches[i].Position()
ts[i] = &Touch{
id: touches[i].id,
x: x + dx,
y: y + dy,
}
}
i.touches = ts
i.m.Unlock() i.m.Unlock()
} }

View File

@ -25,6 +25,7 @@ import (
"golang.org/x/mobile/event/touch" "golang.org/x/mobile/event/touch"
"golang.org/x/mobile/gl" "golang.org/x/mobile/gl"
"github.com/hajimehoshi/ebiten/internal/devicescale"
"github.com/hajimehoshi/ebiten/internal/input" "github.com/hajimehoshi/ebiten/internal/input"
"github.com/hajimehoshi/ebiten/internal/opengl" "github.com/hajimehoshi/ebiten/internal/opengl"
) )
@ -65,9 +66,10 @@ func appMain(a app.App) {
case touch.Event: case touch.Event:
switch e.Type { switch e.Type {
case touch.TypeBegin, touch.TypeMove: case touch.TypeBegin, touch.TypeMove:
s := float32(actualScale()) s := devicescale.DeviceScale()
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(e.X/s), int(e.Y/s)) t := input.NewTouch(int(e.Sequence), int(x), int(y))
touches[e.Sequence] = t touches[e.Sequence] = t
case touch.TypeEnd: case touch.TypeEnd:
delete(touches, e.Sequence) delete(touches, e.Sequence)

View File

@ -95,7 +95,7 @@ func (u *userInterface) updateGraphicsContext(g GraphicsContext) {
if sizeChanged { if sizeChanged {
width = u.width width = u.width
height = u.height height = u.height
actualScale = u.actualScaleImpl() actualScale = u.scaleImpl() * devicescale.DeviceScale()
} }
u.sizeChanged = false u.sizeChanged = false
u.m.Unlock() u.m.Unlock()
@ -112,17 +112,17 @@ func actualScale() float64 {
func (u *userInterface) actualScale() float64 { func (u *userInterface) actualScale() float64 {
u.m.Lock() u.m.Lock()
s := u.actualScaleImpl() s := u.scaleImpl() * devicescale.DeviceScale()
u.m.Unlock() u.m.Unlock()
return s return s
} }
func (u *userInterface) actualScaleImpl() float64 { func (u *userInterface) scaleImpl() float64 {
scale := u.scale scale := u.scale
if u.fullscreenScale != 0 { if u.fullscreenScale != 0 {
scale = u.fullscreenScale scale = u.fullscreenScale
} }
return scale * devicescale.DeviceScale() return scale
} }
func (u *userInterface) update(g GraphicsContext) error { func (u *userInterface) update(g GraphicsContext) error {
@ -239,20 +239,26 @@ func (u *userInterface) screenPaddingImpl() (x0, y0, x1, y1 float64) {
} }
func AdjustedCursorPosition() (x, y int) { func AdjustedCursorPosition() (x, y int) {
return currentUI.adjustCursorPosition(input.Get().CursorPosition()) return currentUI.adjustPosition(input.Get().CursorPosition())
} }
func AdjustedTouches() []*input.Touch { func AdjustedTouches() []*input.Touch {
// TODO: Apply adjustment here ts := input.Get().Touches()
return 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)
}
return adjusted
} }
func (u *userInterface) adjustCursorPosition(x, y int) (int, int) { func (u *userInterface) adjustPosition(x, y int) (int, int) {
u.m.Lock() u.m.Lock()
ox, oy, _, _ := u.screenPaddingImpl() ox, oy, _, _ := u.screenPaddingImpl()
s := u.actualScaleImpl() s := u.scaleImpl()
as := s * devicescale.DeviceScale()
u.m.Unlock() u.m.Unlock()
return x - int(ox/s), y - int(oy/s) return int(float64(x)/s - ox/as), int(float64(y)/s - oy/as)
} }
func IsCursorVisible() bool { func IsCursorVisible() bool {
@ -292,9 +298,5 @@ func SetWindowDecorated(decorated bool) {
} }
func UpdateTouches(touches []*input.Touch) { func UpdateTouches(touches []*input.Touch) {
currentUI.m.Lock() input.Get().UpdateTouches(touches)
ox, oy, _, _ := currentUI.screenPaddingImpl()
s := currentUI.actualScaleImpl()
currentUI.m.Unlock()
input.Get().UpdateTouches(touches, -int(ox/s), -int(oy/s))
} }

View File

@ -33,10 +33,7 @@ var (
func updateTouches() { func updateTouches() {
ts := []*input.Touch{} ts := []*input.Touch{}
for id, position := range touches { for id, position := range touches {
// TODO: Is this OK to adjust the position here? ts = append(ts, input.NewTouch(id, position.x, position.y))
x := int(float64(position.x) / ui.ScreenScale())
y := int(float64(position.y) / ui.ScreenScale())
ts = append(ts, input.NewTouch(id, x, y))
} }
ui.UpdateTouches(ts) ui.UpdateTouches(ts)
} }