internal/ui: give up SetWindowAspectRatioFixed

Unfortunately, it is almost impossible to provide a consistent behavior
to keep the aspect ratio of the window on Windows. Instead of having an
incomplete API, let's remove this.

Closes #1988
This commit is contained in:
Hajime Hoshi 2022-02-10 18:31:55 +09:00
parent 2773d2456d
commit 35b9dd0846
5 changed files with 22 additions and 93 deletions

View File

@ -38,19 +38,18 @@ import (
)
var (
flagFullscreen = flag.Bool("fullscreen", false, "fullscreen")
flagResizable = flag.Bool("resizable", false, "make the window resizable")
flagWindowPosition = flag.String("windowposition", "", "window position (e.g., 100,200)")
flagTransparent = flag.Bool("transparent", false, "screen transparent")
flagAutoAdjusting = flag.Bool("autoadjusting", false, "make the game screen auto-adjusting")
flagFloating = flag.Bool("floating", false, "make the window floating")
flagMaximize = flag.Bool("maximize", false, "maximize the window")
flagVsync = flag.Bool("vsync", true, "enable vsync")
flagAutoRestore = flag.Bool("autorestore", false, "restore the window automatically")
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)")
flagMaxWindowSize = flag.String("maxwindowsize", "", "maximium window size (e.g., 1920x1080)")
flagFullscreen = flag.Bool("fullscreen", false, "fullscreen")
flagResizable = flag.Bool("resizable", false, "make the window resizable")
flagWindowPosition = flag.String("windowposition", "", "window position (e.g., 100,200)")
flagTransparent = flag.Bool("transparent", false, "screen transparent")
flagAutoAdjusting = flag.Bool("autoadjusting", false, "make the game screen auto-adjusting")
flagFloating = flag.Bool("floating", false, "make the window floating")
flagMaximize = flag.Bool("maximize", false, "maximize the window")
flagVsync = flag.Bool("vsync", true, "enable vsync")
flagAutoRestore = flag.Bool("autorestore", false, "restore the window automatically")
flagInitFocused = flag.Bool("initfocused", true, "whether the window is focused on start")
flagMinWindowSize = flag.String("minwindowsize", "", "minimum window size (e.g., 100x200)")
flagMaxWindowSize = flag.String("maxwindowsize", "", "maximium window size (e.g., 1920x1080)")
)
func init() {
@ -291,10 +290,6 @@ func (g *game) Update() error {
ebiten.SetWindowIcon([]image.Image{createRandomIconImage()})
}
if inpututil.IsKeyJustPressed(ebiten.KeyA) {
ebiten.SetWindowAspectRatioFixed(!ebiten.IsWindowAspectRatioFixed())
}
g.count++
return nil
}
@ -348,7 +343,6 @@ func (g *game) Draw(screen *ebiten.Image) {
[D] Switch the window decoration (only for desktops)
[L] Switch the window floating state (only for desktops)
[W] Switch whether to skip clearing the screen
[A] Switch whether to fix window aspect ratio (only for desktops)
%s
IsFocused?: %s
Window Position: (%d, %d)
@ -432,10 +426,6 @@ func main() {
ebiten.SetRunnableOnUnfocused(true)
}
if *flagAspectRatioFixed {
ebiten.SetWindowAspectRatioFixed(true)
}
minw, minh, maxw, maxh := -1, -1, -1, -1
reSize := regexp.MustCompile(`^(\d+)x(\d+)$`)
if m := reSize.FindStringSubmatch(*flagMinWindowSize); m != nil {

View File

@ -61,16 +61,15 @@ type UserInterface struct {
maxWindowWidthInDIP int
maxWindowHeightInDIP int
running uint32
origPosX int
origPosY int
runnableOnUnfocused bool
fpsMode FPSMode
iconImages []image.Image
cursorShape CursorShape
windowClosingHandled bool
windowBeingClosed bool
windowAspectRatioFixed bool
running uint32
origPosX int
origPosY int
runnableOnUnfocused bool
fpsMode FPSMode
iconImages []image.Image
cursorShape CursorShape
windowClosingHandled bool
windowBeingClosed bool
// setSizeCallbackEnabled must be accessed from the main thread.
setSizeCallbackEnabled bool
@ -280,19 +279,6 @@ func (u *UserInterface) setWindowSizeLimitsInDIP(minw, minh, maxw, maxh int) boo
return true
}
func (u *UserInterface) isWindowAspectRatioFixed() bool {
u.m.RLock()
v := u.windowAspectRatioFixed
u.m.RUnlock()
return v
}
func (u *UserInterface) setWindowAspectRatioFixed(fixed bool) {
u.m.Lock()
u.windowAspectRatioFixed = fixed
u.m.Unlock()
}
func (u *UserInterface) isInitFullscreen() bool {
u.m.RLock()
v := u.initFullscreen
@ -1138,12 +1124,6 @@ func (u *UserInterface) swapBuffers() {
// updateWindowSizeLimits must be called from the main thread.
func (u *UserInterface) updateWindowSizeLimits() {
aspectRatio := 0.0
if !u.isFullscreen() && u.isWindowAspectRatioFixed() {
w, h := u.window.GetSize()
aspectRatio = float64(h) / float64(w)
}
m := u.currentMonitor()
minw, minh, maxw, maxh := u.getWindowSizeLimitsInDIP()
@ -1154,11 +1134,7 @@ func (u *UserInterface) updateWindowSizeLimits() {
minw = int(u.dipToGLFWPixel(float64(minw), m))
}
if minh < 0 {
if aspectRatio > 0 {
minh = int(float64(minw) * aspectRatio)
} else {
minh = glfw.DontCare
}
minh = glfw.DontCare
} else {
minh = int(u.dipToGLFWPixel(float64(minh), m))
}
@ -1231,14 +1207,6 @@ func (u *UserInterface) setWindowSizeInDIP(width, height int, fullscreen bool) {
u.setWindowSizeInDIPImpl(width, height, fullscreen)
// TODO: This must be called just after the window is created.
// This relies on the initial value of lastDeviceScaleFactor is 0 so this is called, but the condition is fragile.
// Refactor this.
n, d := glfw.DontCare, glfw.DontCare
if !fullscreen && u.isWindowAspectRatioFixed() {
n, d = width, height
}
u.window.SetAspectRatio(n, d)
u.updateWindowSizeLimits()
u.adjustViewSize()

View File

@ -227,14 +227,6 @@ func (w *Window) SetSizeLimits(minw, minh, maxw, maxh int) {
w.ui.t.Call(w.ui.updateWindowSizeLimits)
}
func (w *Window) SetAspectRatioFixed(fixed bool) {
w.ui.setWindowAspectRatioFixed(fixed)
}
func (w *Window) IsAspectRatioFixed() bool {
return w.ui.isWindowAspectRatioFixed()
}
func (w *Window) SetIcon(iconImages []image.Image) {
// The icons are actually set at (*UserInterface).loop.
w.ui.setIconImages(iconImages)

View File

@ -79,13 +79,6 @@ func (*Window) IsMinimized() bool {
return false
}
func (*Window) SetAspectRatioFixed(fixed bool) {
}
func (*Window) IsAspectRatioFixed() bool {
return false
}
func (*Window) SetIcon(iconImages []image.Image) {
}

View File

@ -72,20 +72,6 @@ func SetWindowResizable(resizable bool) {
ui.Get().Window().SetResizable(resizable)
}
// SetWindowAspectRatioFixed sets whether the window should keep its aspect ratio while resizing.
//
// SetWindowAspectRatioFixed is concurrent-safe.
func SetWindowAspectRatioFixed(fixed bool) {
ui.Get().Window().SetAspectRatioFixed(fixed)
}
// IsWindowAspectRatioFixed reports whether the window should keep its aspect ratio while resizing.
//
// IsWindowAspectRatioFixed is concurrent-safe.
func IsWindowAspectRatioFixed() bool {
return ui.Get().Window().IsAspectRatioFixed()
}
// SetWindowTitle sets the title of the window.
//
// SetWindowTitle does nothing on browsers or mobiles.