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
}
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)
}
@ -90,12 +90,12 @@ func SwapBuffers() {
func SetScreenSize(width, height int) (bool, int) {
result := currentUI.setScreenSize(width, height, currentUI.scale)
return result, currentUI.actualScale()
return result, currentUI.framebufferScale()
}
func SetScreenScale(scale int) (bool, int) {
result := currentUI.setScreenSize(currentUI.width, currentUI.height, scale)
return result, currentUI.actualScale()
return result, currentUI.framebufferScale()
}
type userInterface struct {
@ -104,16 +104,16 @@ type userInterface struct {
height int
scale int
deviceScaleFactor float64
framebufferScale int
framebufferScale_ int
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()
v := m.GetVideoMode()
mw, _ := m.GetPhysicalSize()
u.deviceScaleFactor = 1
u.framebufferScale = 1
u.framebufferScale_ = 1
// mw can be 0 on some environment like Linux VM
if 0 < 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.Show()
s := int(float64(scale) * u.deviceScaleFactor)
x := (v.Width - width*s) / 2
y := (v.Height - height*s) / 3
x := (v.Width - width*u.windowScale()) / 2
y := (v.Height - height*u.windowScale()) / 3
u.window.SetPos(x, y)
return u.actualScale(), nil
return u.framebufferScale(), nil
}
func (u *userInterface) actualScale() int {
return int(float64(u.scale)*u.deviceScaleFactor) * u.framebufferScale
func (u *userInterface) windowScale() int {
return int(float64(u.scale) * u.deviceScaleFactor)
}
func (u *userInterface) framebufferScale() int {
return u.windowScale() * u.framebufferScale_
}
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 {
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,
// swap buffers here before SetSize is called.
@ -187,8 +194,7 @@ func (u *userInterface) setScreenSize(width, height, scale int) bool {
window.SetFramebufferSizeCallback(nil)
close(ch)
})
s := int(float64(scale) * u.deviceScaleFactor)
window.SetSize(width*s, height*s)
window.SetSize(width*u.windowScale(), height*u.windowScale())
event:
for {
@ -201,9 +207,6 @@ event:
}
// This is usually 1, but sometimes more than 1 (e.g. Retina Mac)
fw, _ := window.GetFramebufferSize()
u.framebufferScale = fw / width / s
u.width = width
u.height = height
u.scale = scale
u.framebufferScale_ = fw / width / u.windowScale()
return true
}