internal/ui: bug fix: the aspect ratio must be updated whenever the window size is forcibly updated

This also adds a new flag -aspectratiofixed to examples/windowsize

Updates #1804
This commit is contained in:
Hajime Hoshi 2022-02-09 19:56:23 +09:00
parent 740dfd5aed
commit 7482cae978
3 changed files with 50 additions and 55 deletions

View File

@ -48,6 +48,7 @@ var (
flagVsync = flag.Bool("vsync", true, "enable vsync") flagVsync = flag.Bool("vsync", true, "enable vsync")
flagAutoRestore = flag.Bool("autorestore", false, "restore the window automatically") flagAutoRestore = flag.Bool("autorestore", false, "restore the window automatically")
flagInitFocused = flag.Bool("initfocused", true, "whether the window is focused on start") flagInitFocused = flag.Bool("initfocused", true, "whether the window is focused on start")
flagAspectRatioFixed = flag.Bool("aspectratiofixed", false, "whether the window's aspect ratio is fixed or not")
flagMinWindowSize = flag.String("minwindowsize", "", "minimum window size (e.g., 100x200)") flagMinWindowSize = flag.String("minwindowsize", "", "minimum window size (e.g., 100x200)")
flagMaxWindowSize = flag.String("maxwindowsize", "", "maximium window size (e.g., 1920x1080)") flagMaxWindowSize = flag.String("maxwindowsize", "", "maximium window size (e.g., 1920x1080)")
) )
@ -426,6 +427,10 @@ func main() {
ebiten.SetRunnableOnUnfocused(true) ebiten.SetRunnableOnUnfocused(true)
} }
if *flagAspectRatioFixed {
ebiten.SetWindowAspectRatioFixed(true)
}
minw, minh, maxw, maxh := -1, -1, -1, -1 minw, minh, maxw, maxh := -1, -1, -1, -1
reSize := regexp.MustCompile(`^(\d+)x(\d+)$`) reSize := regexp.MustCompile(`^(\d+)x(\d+)$`)
if m := reSize.FindStringSubmatch(*flagMinWindowSize); m != nil { if m := reSize.FindStringSubmatch(*flagMinWindowSize); m != nil {

View File

@ -70,6 +70,7 @@ type UserInterface struct {
cursorShape CursorShape cursorShape CursorShape
windowClosingHandled bool windowClosingHandled bool
windowBeingClosed bool windowBeingClosed bool
windowAspectRatioFixed bool
// setSizeCallbackEnabled must be accessed from the main thread. // setSizeCallbackEnabled must be accessed from the main thread.
setSizeCallbackEnabled bool setSizeCallbackEnabled bool
@ -95,7 +96,6 @@ type UserInterface struct {
initWindowHeightInDIP int initWindowHeightInDIP int
initWindowFloating bool initWindowFloating bool
initWindowMaximized bool initWindowMaximized bool
initWindowAspectRatioFixed bool
initScreenTransparent bool initScreenTransparent bool
initFocused bool initFocused bool
@ -280,16 +280,16 @@ func (u *UserInterface) setWindowSizeLimitsInDIP(minw, minh, maxw, maxh int) boo
return true return true
} }
func (u *UserInterface) isInitWindowAspectRatioFixed() bool { func (u *UserInterface) isWindowAspectRatioFixed() bool {
u.m.RLock() u.m.RLock()
v := u.initWindowAspectRatioFixed v := u.windowAspectRatioFixed
u.m.RUnlock() u.m.RUnlock()
return v return v
} }
func (u *UserInterface) setInitWindowAspectRatioFixed(fixed bool) { func (u *UserInterface) setWindowAspectRatioFixed(fixed bool) {
u.m.Lock() u.m.Lock()
u.initWindowAspectRatioFixed = fixed u.windowAspectRatioFixed = fixed
u.m.Unlock() u.m.Unlock()
} }
@ -932,8 +932,6 @@ func (u *UserInterface) init() error {
u.window.Maximize() u.window.Maximize()
} }
u.setWindowAspectRatioFixed(u.isInitWindowAspectRatioFixed())
u.window.Show() u.window.Show()
if g, ok := Graphics().(interface{ SetWindow(uintptr) }); ok { if g, ok := Graphics().(interface{ SetWindow(uintptr) }); ok {
@ -1306,6 +1304,12 @@ func (u *UserInterface) setWindowSizeInDIPImpl(width, height int, fullscreen boo
} }
} }
n, d := glfw.DontCare, glfw.DontCare
if !fullscreen && u.isWindowAspectRatioFixed() {
n, d = u.window.GetSize()
}
u.window.SetAspectRatio(n, d)
return windowRecreated return windowRecreated
} }
@ -1623,11 +1627,3 @@ func (u *UserInterface) setOrigPos(x, y int) {
u.origPosX = x u.origPosX = x
u.origPosY = y u.origPosY = y
} }
func (u *UserInterface) setWindowAspectRatioFixed(fixed bool) {
n, d := glfw.DontCare, glfw.DontCare
if fixed {
n, d = u.window.GetSize()
}
u.window.SetAspectRatio(n, d)
}

View File

@ -228,13 +228,7 @@ func (w *Window) SetSizeLimits(minw, minh, maxw, maxh int) {
} }
func (w *Window) SetAspectRatioFixed(fixed bool) { func (w *Window) SetAspectRatioFixed(fixed bool) {
if !w.ui.isRunning() {
w.ui.setInitWindowAspectRatioFixed(fixed)
return
}
w.ui.t.Call(func() {
w.ui.setWindowAspectRatioFixed(fixed) w.ui.setWindowAspectRatioFixed(fixed)
})
} }
func (w *Window) SetIcon(iconImages []image.Image) { func (w *Window) SetIcon(iconImages []image.Image) {