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
}
func (i *Input) UpdateTouches(touches []*Touch, dx, dy int) {
func (i *Input) UpdateTouches(touches []*Touch) {
i.m.Lock()
ts := make([]*Touch, len(touches))
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.touches = touches // TODO: Need copy?
i.m.Unlock()
}

View File

@ -25,6 +25,7 @@ import (
"golang.org/x/mobile/event/touch"
"golang.org/x/mobile/gl"
"github.com/hajimehoshi/ebiten/internal/devicescale"
"github.com/hajimehoshi/ebiten/internal/input"
"github.com/hajimehoshi/ebiten/internal/opengl"
)
@ -65,9 +66,10 @@ func appMain(a app.App) {
case touch.Event:
switch e.Type {
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?
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
case touch.TypeEnd:
delete(touches, e.Sequence)

View File

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

View File

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