ui: Refactoring

This commit is contained in:
Hajime Hoshi 2016-02-27 01:35:14 +09:00
parent baede92b16
commit 138a689382

View File

@ -68,7 +68,7 @@ func Init() *opengl.Context {
return u.context return u.context
} }
func Start(width, height, scale int, title string) (actualScale int, err error) { func Start(width, height, scale int, title string) (framebufferScale int, err error) {
return currentUI.start(width, height, scale, title) return currentUI.start(width, height, scale, title)
} }
@ -90,12 +90,12 @@ func SwapBuffers() {
func SetScreenSize(width, height int) (bool, int) { func SetScreenSize(width, height int) (bool, int) {
result := currentUI.setScreenSize(width, height, currentUI.scale) result := currentUI.setScreenSize(width, height, currentUI.scale)
return result, currentUI.actualScale() return result, currentUI.framebufferScale()
} }
func SetScreenScale(scale int) (bool, int) { func SetScreenScale(scale int) (bool, int) {
result := currentUI.setScreenSize(currentUI.width, currentUI.height, scale) result := currentUI.setScreenSize(currentUI.width, currentUI.height, scale)
return result, currentUI.actualScale() return result, currentUI.framebufferScale()
} }
type userInterface struct { type userInterface struct {
@ -104,16 +104,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) (actualScale int, err error) { func (u *userInterface) start(width, height, scale int, title string) (framebufferScale int, err 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)
@ -124,16 +124,19 @@ func (u *userInterface) start(width, height, scale int, title string) (actualSca
u.window.SetTitle(title) u.window.SetTitle(title)
u.window.Show() u.window.Show()
s := int(float64(scale) * u.deviceScaleFactor) x := (v.Width - width*u.windowScale()) / 2
x := (v.Width - width*s) / 2 y := (v.Height - height*u.windowScale()) / 3
y := (v.Height - height*s) / 3
u.window.SetPos(x, y) u.window.SetPos(x, y)
return u.actualScale(), nil return u.framebufferScale(), nil
} }
func (u *userInterface) actualScale() int { func (u *userInterface) windowScale() int {
return int(float64(u.scale)*u.deviceScaleFactor) * u.framebufferScale return int(float64(u.scale) * u.deviceScaleFactor)
}
func (u *userInterface) framebufferScale() int {
return u.windowScale() * u.framebufferScale_
} }
func (u *userInterface) pollEvents() error { func (u *userInterface) pollEvents() error {
@ -175,6 +178,10 @@ func (u *userInterface) setScreenSize(width, height, scale int) bool {
if u.width == width && u.height == height && u.scale == scale { if u.width == width && u.height == height && u.scale == scale {
return false return false
} }
// These properties should be set first since this affects windowScale().
u.width = width
u.height = height
u.scale = scale
// To make sure the current existing framebuffers are rendered, // To make sure the current existing framebuffers are rendered,
// swap buffers here before SetSize is called. // swap buffers here before SetSize is called.
@ -187,8 +194,7 @@ func (u *userInterface) setScreenSize(width, height, scale int) bool {
window.SetFramebufferSizeCallback(nil) window.SetFramebufferSizeCallback(nil)
close(ch) close(ch)
}) })
s := int(float64(scale) * u.deviceScaleFactor) window.SetSize(width*u.windowScale(), height*u.windowScale())
window.SetSize(width*s, height*s)
event: event:
for { for {
@ -201,9 +207,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 / s u.framebufferScale_ = fw / width / u.windowScale()
u.width = width
u.height = height
u.scale = scale
return true return true
} }