mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2025-01-27 03:02:49 +01:00
uidriver/mobile: Rely on uiContext's Layout function to determine the screen size
This commit is contained in:
parent
6a8013ed37
commit
9486defdae
@ -39,7 +39,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
glContextCh = make(chan gl.Context)
|
glContextCh = make(chan gl.Context, 1)
|
||||||
|
|
||||||
// renderCh receives when updating starts.
|
// renderCh receives when updating starts.
|
||||||
renderCh = make(chan struct{})
|
renderCh = make(chan struct{})
|
||||||
@ -102,10 +102,11 @@ type UserInterface struct {
|
|||||||
sizeChanged bool
|
sizeChanged bool
|
||||||
|
|
||||||
// Used for gomobile-build
|
// Used for gomobile-build
|
||||||
gbuildScale float64
|
gbuildWidthPx int
|
||||||
gbuildWidthPx int
|
gbuildHeightPx int
|
||||||
gbuildHeightPx int
|
setGBuildSizeCh chan struct{}
|
||||||
|
|
||||||
|
context driver.UIContext
|
||||||
graphics driver.Graphics
|
graphics driver.Graphics
|
||||||
|
|
||||||
input Input
|
input Input
|
||||||
@ -123,6 +124,7 @@ func deviceScale() float64 {
|
|||||||
// appMain is the main routine for gomobile-build mode.
|
// appMain is the main routine for gomobile-build mode.
|
||||||
func (u *UserInterface) appMain(a app.App) {
|
func (u *UserInterface) appMain(a app.App) {
|
||||||
var glctx gl.Context
|
var glctx gl.Context
|
||||||
|
var sizeInited bool
|
||||||
touches := map[touch.Sequence]*Touch{}
|
touches := map[touch.Sequence]*Touch{}
|
||||||
for e := range a.Events() {
|
for e := range a.Events() {
|
||||||
switch e := a.Filter(e).(type) {
|
switch e := a.Filter(e).(type) {
|
||||||
@ -141,8 +143,13 @@ func (u *UserInterface) appMain(a app.App) {
|
|||||||
glctx = nil
|
glctx = nil
|
||||||
}
|
}
|
||||||
case size.Event:
|
case size.Event:
|
||||||
u.setGBuild(e.WidthPx, e.HeightPx)
|
u.setGBuildSize(e.WidthPx, e.HeightPx)
|
||||||
|
sizeInited = true
|
||||||
case paint.Event:
|
case paint.Event:
|
||||||
|
if !sizeInited {
|
||||||
|
a.Send(paint.Event{})
|
||||||
|
continue
|
||||||
|
}
|
||||||
if glctx == nil || e.External {
|
if glctx == nil || e.External {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
@ -151,6 +158,9 @@ func (u *UserInterface) appMain(a app.App) {
|
|||||||
a.Publish()
|
a.Publish()
|
||||||
a.Send(paint.Event{})
|
a.Send(paint.Event{})
|
||||||
case touch.Event:
|
case touch.Event:
|
||||||
|
if !sizeInited {
|
||||||
|
continue
|
||||||
|
}
|
||||||
switch e.Type {
|
switch e.Type {
|
||||||
case touch.TypeBegin, touch.TypeMove:
|
case touch.TypeBegin, touch.TypeMove:
|
||||||
s := deviceScale()
|
s := deviceScale()
|
||||||
@ -174,8 +184,11 @@ func (u *UserInterface) appMain(a app.App) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (u *UserInterface) Run(width, height int, scale float64, title string, context driver.UIContext, graphics driver.Graphics) error {
|
func (u *UserInterface) Run(width, height int, scale float64, title string, context driver.UIContext, graphics driver.Graphics) error {
|
||||||
|
// TODO: Remove width/height/scale arguments. They are not used from gomobile-build.
|
||||||
|
|
||||||
|
u.setGBuildSizeCh = make(chan struct{})
|
||||||
go func() {
|
go func() {
|
||||||
if err := u.run(width, height, scale, title, context, graphics, true); err != nil {
|
if err := u.run(16, 16, 1, title, context, graphics, true); err != nil {
|
||||||
// As mobile apps never ends, Loop can't return. Just panic here.
|
// As mobile apps never ends, Loop can't return. Just panic here.
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
@ -192,6 +205,7 @@ func (u *UserInterface) RunWithoutMainLoop(width, height int, scale float64, tit
|
|||||||
ch <- err
|
ch <- err
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
return ch
|
return ch
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -211,6 +225,7 @@ func (u *UserInterface) run(width, height int, scale float64, title string, cont
|
|||||||
u.height = height
|
u.height = height
|
||||||
u.scale = scale
|
u.scale = scale
|
||||||
u.sizeChanged = true
|
u.sizeChanged = true
|
||||||
|
u.context = context
|
||||||
u.graphics = graphics
|
u.graphics = graphics
|
||||||
u.m.Unlock()
|
u.m.Unlock()
|
||||||
// title is ignored?
|
// title is ignored?
|
||||||
@ -228,6 +243,11 @@ func (u *UserInterface) run(width, height int, scale float64, title string, cont
|
|||||||
graphics.SetThread(u.t)
|
graphics.SetThread(u.t)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// If gomobile-build is used, wait for the outside size fixed.
|
||||||
|
if u.setGBuildSizeCh != nil {
|
||||||
|
<-u.setGBuildSizeCh
|
||||||
|
}
|
||||||
|
|
||||||
// Force to set the screen size
|
// Force to set the screen size
|
||||||
u.updateSize(context)
|
u.updateSize(context)
|
||||||
for {
|
for {
|
||||||
@ -244,7 +264,7 @@ func (u *UserInterface) updateSize(context driver.UIContext) {
|
|||||||
sizeChanged := u.sizeChanged
|
sizeChanged := u.sizeChanged
|
||||||
if sizeChanged {
|
if sizeChanged {
|
||||||
if u.gbuildWidthPx == 0 || u.gbuildHeightPx == 0 {
|
if u.gbuildWidthPx == 0 || u.gbuildHeightPx == 0 {
|
||||||
s := u.scaleImpl()
|
s := u.scale
|
||||||
width = float64(u.width) * s
|
width = float64(u.width) * s
|
||||||
height = float64(u.height) * s
|
height = float64(u.height) * s
|
||||||
} else {
|
} else {
|
||||||
@ -271,14 +291,6 @@ func (u *UserInterface) updateSize(context driver.UIContext) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (u *UserInterface) scaleImpl() float64 {
|
|
||||||
scale := u.scale
|
|
||||||
if u.gbuildScale != 0 {
|
|
||||||
scale = u.gbuildScale
|
|
||||||
}
|
|
||||||
return scale
|
|
||||||
}
|
|
||||||
|
|
||||||
func (u *UserInterface) update(context driver.UIContext) error {
|
func (u *UserInterface) update(context driver.UIContext) error {
|
||||||
t := time.NewTimer(500 * time.Millisecond)
|
t := time.NewTimer(500 * time.Millisecond)
|
||||||
defer t.Stop()
|
defer t.Stop()
|
||||||
@ -317,54 +329,28 @@ func (u *UserInterface) SetScreenSizeAndScale(width, height int, scale float64)
|
|||||||
u.width = width
|
u.width = width
|
||||||
u.height = height
|
u.height = height
|
||||||
u.scale = scale
|
u.scale = scale
|
||||||
u.updateGBuildScaleIfNeeded()
|
|
||||||
u.sizeChanged = true
|
u.sizeChanged = true
|
||||||
}
|
}
|
||||||
u.m.Unlock()
|
u.m.Unlock()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (u *UserInterface) setGBuild(widthPx, heightPx int) {
|
func (u *UserInterface) setGBuildSize(widthPx, heightPx int) {
|
||||||
u.m.Lock()
|
u.m.Lock()
|
||||||
u.gbuildWidthPx = widthPx
|
u.gbuildWidthPx = widthPx
|
||||||
u.gbuildHeightPx = heightPx
|
u.gbuildHeightPx = heightPx
|
||||||
u.updateGBuildScaleIfNeeded()
|
|
||||||
u.sizeChanged = true
|
u.sizeChanged = true
|
||||||
|
close(u.setGBuildSizeCh)
|
||||||
u.m.Unlock()
|
u.m.Unlock()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (u *UserInterface) updateGBuildScaleIfNeeded() {
|
|
||||||
if u.gbuildWidthPx == 0 || u.gbuildHeightPx == 0 {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
w, h := u.width, u.height
|
|
||||||
scaleX := float64(u.gbuildWidthPx) / float64(w)
|
|
||||||
scaleY := float64(u.gbuildHeightPx) / float64(h)
|
|
||||||
scale := scaleX
|
|
||||||
if scale > scaleY {
|
|
||||||
scale = scaleY
|
|
||||||
}
|
|
||||||
u.gbuildScale = scale / deviceScale()
|
|
||||||
u.sizeChanged = true
|
|
||||||
}
|
|
||||||
|
|
||||||
func (u *UserInterface) screenPaddingImpl() (x0, y0, x1, y1 float64) {
|
|
||||||
// TODO: Replace this with UIContext's Layout.
|
|
||||||
if u.gbuildScale == 0 {
|
|
||||||
return 0, 0, 0, 0
|
|
||||||
}
|
|
||||||
s := u.gbuildScale * deviceScale()
|
|
||||||
ox := (float64(u.gbuildWidthPx) - float64(u.width)*s) / 2
|
|
||||||
oy := (float64(u.gbuildHeightPx) - float64(u.height)*s) / 2
|
|
||||||
return ox, oy, ox, oy
|
|
||||||
}
|
|
||||||
|
|
||||||
func (u *UserInterface) adjustPosition(x, y int) (int, int) {
|
func (u *UserInterface) adjustPosition(x, y int) (int, int) {
|
||||||
// TODO: Replace this with UIContext's AdjustPosition.
|
// This function's caller already protects this function by the mutex.
|
||||||
ox, oy, _, _ := u.screenPaddingImpl()
|
if u.gbuildWidthPx == 0 || u.gbuildHeightPx == 0 {
|
||||||
s := u.scaleImpl()
|
s := u.scale
|
||||||
as := s * deviceScale()
|
return int(float64(x) / s), int(float64(y) / s)
|
||||||
return int(float64(x)/s - ox/as), int(float64(y)/s - oy/as)
|
}
|
||||||
|
xf, yf := u.context.AdjustPosition(float64(x), float64(y))
|
||||||
|
return int(xf), int(yf)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (u *UserInterface) CursorMode() driver.CursorMode {
|
func (u *UserInterface) CursorMode() driver.CursorMode {
|
||||||
|
Loading…
Reference in New Issue
Block a user