ui: Refactoring: Add ui.ActualScale()

This commit is contained in:
Hajime Hoshi 2016-02-27 01:44:01 +09:00
parent 138a689382
commit 8069c980c3
3 changed files with 32 additions and 37 deletions

View File

@ -68,7 +68,7 @@ func Init() *opengl.Context {
return u.context return u.context
} }
func Start(width, height, scale int, title string) (framebufferScale int, err error) { func Start(width, height, scale int, title string) error {
return currentUI.start(width, height, scale, title) return currentUI.start(width, height, scale, title)
} }
@ -88,14 +88,16 @@ func SwapBuffers() {
currentUI.swapBuffers() currentUI.swapBuffers()
} }
func SetScreenSize(width, height int) (bool, int) { func SetScreenSize(width, height int) bool {
result := currentUI.setScreenSize(width, height, currentUI.scale) return currentUI.setScreenSize(width, height, currentUI.scale)
return result, currentUI.framebufferScale()
} }
func SetScreenScale(scale int) (bool, int) { func SetScreenScale(scale int) bool {
result := currentUI.setScreenSize(currentUI.width, currentUI.height, scale) return currentUI.setScreenSize(currentUI.width, currentUI.height, scale)
return result, currentUI.framebufferScale() }
func ActualScale() int {
return currentUI.actualScale()
} }
type userInterface struct { type userInterface struct {
@ -104,16 +106,16 @@ type userInterface struct {
height int height int
scale int scale int
deviceScaleFactor float64 deviceScaleFactor float64
framebufferScale_ int framebufferScale int
context *opengl.Context context *opengl.Context
} }
func (u *userInterface) start(width, height, scale int, title string) (framebufferScale int, err error) { func (u *userInterface) start(width, height, scale int, title string) error {
m := glfw.GetPrimaryMonitor() m := glfw.GetPrimaryMonitor()
v := m.GetVideoMode() v := m.GetVideoMode()
mw, _ := m.GetPhysicalSize() mw, _ := m.GetPhysicalSize()
u.deviceScaleFactor = 1 u.deviceScaleFactor = 1
u.framebufferScale_ = 1 u.framebufferScale = 1
// mw can be 0 on some environment like Linux VM // mw can be 0 on some environment like Linux VM
if 0 < mw { if 0 < mw {
dpi := float64(v.Width) * 25.4 / float64(mw) dpi := float64(v.Width) * 25.4 / float64(mw)
@ -128,15 +130,15 @@ func (u *userInterface) start(width, height, scale int, title string) (framebuff
y := (v.Height - height*u.windowScale()) / 3 y := (v.Height - height*u.windowScale()) / 3
u.window.SetPos(x, y) u.window.SetPos(x, y)
return u.framebufferScale(), nil return nil
} }
func (u *userInterface) windowScale() int { func (u *userInterface) windowScale() int {
return int(float64(u.scale) * u.deviceScaleFactor) return int(float64(u.scale) * u.deviceScaleFactor)
} }
func (u *userInterface) framebufferScale() int { func (u *userInterface) actualScale() int {
return u.windowScale() * u.framebufferScale_ return u.windowScale() * u.framebufferScale
} }
func (u *userInterface) pollEvents() error { func (u *userInterface) pollEvents() error {
@ -207,6 +209,6 @@ event:
} }
// This is usually 1, but sometimes more than 1 (e.g. Retina Mac) // This is usually 1, but sometimes more than 1 (e.g. Retina Mac)
fw, _ := window.GetFramebufferSize() fw, _ := window.GetFramebufferSize()
u.framebufferScale_ = fw / width / u.windowScale() u.framebufferScale = fw / width / u.windowScale()
return true return true
} }

View File

@ -29,7 +29,7 @@ func Now() int64 {
return int64(js.Global.Get("performance").Call("now").Float() * float64(time.Millisecond)) return int64(js.Global.Get("performance").Call("now").Float() * float64(time.Millisecond))
} }
func Start(width, height, scale int, title string) (actualScale int, err error) { func Start(width, height, scale int, title string) error {
return currentUI.start(width, height, scale, title) return currentUI.start(width, height, scale, title)
} }
@ -49,18 +49,18 @@ func SwapBuffers() {
currentUI.swapBuffers() currentUI.swapBuffers()
} }
func SetScreenSize(width, height int) (bool, int) { func SetScreenSize(width, height int) bool {
scale := canvas.Get("dataset").Get("ebitenScale").Int() scale := canvas.Get("dataset").Get("ebitenScale").Int()
result := currentUI.setScreenSize(width, height, scale) return currentUI.setScreenSize(width, height, scale)
actualScale := canvas.Get("dataset").Get("ebitenActualScale").Int()
return result, actualScale
} }
func SetScreenScale(scale int) (bool, int) { func SetScreenScale(scale int) bool {
width, height := currentUI.size() width, height := currentUI.size()
result := currentUI.setScreenSize(width, height, scale) return currentUI.setScreenSize(width, height, scale)
actualScale := canvas.Get("dataset").Get("ebitenActualScale").Int() }
return result, actualScale
func ActualScale() int {
return canvas.Get("dataset").Get("ebitenActualScale").Int()
} }
var canvas *js.Object var canvas *js.Object
@ -240,14 +240,12 @@ func devicePixelRatio() int {
return ratio return ratio
} }
func (u *userInterface) start(width, height, scale int, title string) (actualScale int, err error) { func (u *userInterface) start(width, height, scale int, title string) error {
doc := js.Global.Get("document") doc := js.Global.Get("document")
doc.Set("title", title) doc.Set("title", title)
u.setScreenSize(width, height, scale) u.setScreenSize(width, height, scale)
canvas.Call("focus") canvas.Call("focus")
return nil
actualScale = canvas.Get("dataset").Get("ebitenActualScale").Int()
return actualScale, nil
} }
func (*userInterface) size() (width, height int) { func (*userInterface) size() (width, height int) {

15
run.go
View File

@ -48,14 +48,13 @@ func Run(f func(*Image) error, width, height, scale int, title string) error {
runContext.running = false runContext.running = false
}() }()
actualScale, err := ui.Start(width, height, scale, title) if err := ui.Start(width, height, scale, title); err != nil {
if err != nil {
return err return err
} }
defer ui.Terminate() defer ui.Terminate()
glContext.Check() glContext.Check()
graphicsContext, err := newGraphicsContext(width, height, actualScale) graphicsContext, err := newGraphicsContext(width, height, ui.ActualScale())
if err != nil { if err != nil {
return err return err
} }
@ -67,21 +66,17 @@ func Run(f func(*Image) error, width, height, scale int, title string) error {
// TODO: setSize should be called after swapping buffers? // TODO: setSize should be called after swapping buffers?
if 0 < runContext.newScreenWidth || 0 < runContext.newScreenHeight || 0 < runContext.newScreenScale { if 0 < runContext.newScreenWidth || 0 < runContext.newScreenHeight || 0 < runContext.newScreenScale {
changed := false changed := false
actualScale := 0
if 0 < runContext.newScreenWidth || 0 < runContext.newScreenHeight { if 0 < runContext.newScreenWidth || 0 < runContext.newScreenHeight {
c, a := ui.SetScreenSize(runContext.newScreenWidth, runContext.newScreenHeight) c := ui.SetScreenSize(runContext.newScreenWidth, runContext.newScreenHeight)
changed = changed || c changed = changed || c
actualScale = a
} }
if 0 < runContext.newScreenScale { if 0 < runContext.newScreenScale {
c, a := ui.SetScreenScale(runContext.newScreenScale) c := ui.SetScreenScale(runContext.newScreenScale)
changed = changed || c changed = changed || c
// actualScale of SetScreenState is more reliable than one of SetScreenSize
actualScale = a
} }
if changed { if changed {
w, h := runContext.newScreenWidth, runContext.newScreenHeight w, h := runContext.newScreenWidth, runContext.newScreenHeight
if err := graphicsContext.setSize(w, h, actualScale); err != nil { if err := graphicsContext.setSize(w, h, ui.ActualScale()); err != nil {
return err return err
} }
} }