diff --git a/internal/ui/ui_glfw.go b/internal/ui/ui_glfw.go index f353e85c1..25cbc2b0d 100644 --- a/internal/ui/ui_glfw.go +++ b/internal/ui/ui_glfw.go @@ -77,7 +77,7 @@ const ( ) var ( - currentUI = &userInterface{ + theUI = &userInterface{ origPosX: invalidPos, origPosY: invalidPos, initCursorVisible: true, @@ -109,14 +109,14 @@ func initialize() error { return err } // TODO: Fix this hack. currentMonitorImpl now requires u.window on POSIX. - currentUI.window = w - currentUI.initMonitor = currentUI.currentMonitorFromPosition() - v := currentUI.initMonitor.GetVideoMode() + theUI.window = w + theUI.initMonitor = theUI.currentMonitorFromPosition() + v := theUI.initMonitor.GetVideoMode() s := glfwScale() - currentUI.initFullscreenWidth = int(float64(v.Width) / s) - currentUI.initFullscreenHeight = int(float64(v.Height) / s) - currentUI.window.Destroy() - currentUI.window = nil + theUI.initFullscreenWidth = int(float64(v.Width) / s) + theUI.initFullscreenHeight = int(float64(v.Height) / s) + theUI.window.Destroy() + theUI.window = nil return nil } @@ -164,11 +164,11 @@ func getCachedMonitor(wx, wy int) (*cachedMonitor, bool) { } func Loop(ch <-chan error) error { - currentUI.setRunning(true) + theUI.setRunning(true) if err := mainthread.Loop(ch); err != nil { return err } - currentUI.setRunning(false) + theUI.setRunning(false) return nil } @@ -264,7 +264,7 @@ func (u *userInterface) setInitIconImages(iconImages []image.Image) { } func ScreenSizeInFullscreen() (int, int) { - u := currentUI + u := theUI if !u.isRunning() { return u.initFullscreenWidth, u.initFullscreenHeight } @@ -280,7 +280,7 @@ func ScreenSizeInFullscreen() (int, int) { } func SetScreenSize(width, height int) { - u := currentUI + u := theUI if !u.isRunning() { panic("ui: Run is not called yet") } @@ -292,7 +292,7 @@ func SetScreenSize(width, height int) { } func SetScreenScale(scale float64) bool { - u := currentUI + u := theUI if !u.isRunning() { panic("ui: Run is not called yet") } @@ -306,7 +306,7 @@ func SetScreenScale(scale float64) bool { } func ScreenScale() float64 { - u := currentUI + u := theUI if !u.isRunning() { return 0 } @@ -327,7 +327,7 @@ func (u *userInterface) isFullscreen() bool { } func IsFullscreen() bool { - u := currentUI + u := theUI if !u.isRunning() { return u.isInitFullscreen() } @@ -340,28 +340,28 @@ func IsFullscreen() bool { } func SetFullscreen(fullscreen bool) { - u := currentUI + u := theUI if !u.isRunning() { u.setInitFullscreen(fullscreen) return } _ = mainthread.Run(func() error { - u := currentUI + u := theUI u.setScreenSize(u.width, u.height, u.scale, fullscreen, u.vsync) return nil }) } func SetRunnableInBackground(runnableInBackground bool) { - currentUI.setRunnableInBackground(runnableInBackground) + theUI.setRunnableInBackground(runnableInBackground) } func IsRunnableInBackground() bool { - return currentUI.isRunnableInBackground() + return theUI.isRunnableInBackground() } func SetVsyncEnabled(enabled bool) { - u := currentUI + u := theUI if !u.isRunning() { // In general, m is used for locking init* values. // m is not used for updating vsync in setScreenSize so far, but @@ -373,14 +373,14 @@ func SetVsyncEnabled(enabled bool) { return } _ = mainthread.Run(func() error { - u := currentUI + u := theUI u.setScreenSize(u.width, u.height, u.scale, u.isFullscreen(), enabled) return nil }) } func IsVsyncEnabled() bool { - u := currentUI + u := theUI u.m.Lock() r := u.vsync u.m.Unlock() @@ -388,28 +388,28 @@ func IsVsyncEnabled() bool { } func SetWindowTitle(title string) { - if !currentUI.isRunning() { + if !theUI.isRunning() { return } _ = mainthread.Run(func() error { - currentUI.window.SetTitle(title) + theUI.window.SetTitle(title) return nil }) } func SetWindowIcon(iconImages []image.Image) { - if !currentUI.isRunning() { - currentUI.setInitIconImages(iconImages) + if !theUI.isRunning() { + theUI.setInitIconImages(iconImages) return } _ = mainthread.Run(func() error { - currentUI.window.SetIcon(iconImages) + theUI.window.SetIcon(iconImages) return nil }) } func ScreenPadding() (x0, y0, x1, y1 float64) { - u := currentUI + u := theUI if !u.isRunning() { return 0, 0, 0, 0 } @@ -452,34 +452,34 @@ func ScreenPadding() (x0, y0, x1, y1 float64) { } func AdjustPosition(x, y int) (int, int) { - u := currentUI + u := theUI if !u.isRunning() { return x, y } ox, oy, _, _ := ScreenPadding() s := 0.0 _ = mainthread.Run(func() error { - s = currentUI.actualScreenScale() + s = theUI.actualScreenScale() return nil }) return x - int(ox/s), y - int(oy/s) } func IsCursorVisible() bool { - u := currentUI + u := theUI if !u.isRunning() { return u.isInitCursorVisible() } v := false _ = mainthread.Run(func() error { - v = currentUI.window.GetInputMode(glfw.CursorMode) == glfw.CursorNormal + v = theUI.window.GetInputMode(glfw.CursorMode) == glfw.CursorNormal return nil }) return v } func SetCursorVisible(visible bool) { - u := currentUI + u := theUI if !u.isRunning() { u.setInitCursorVisible(visible) return @@ -489,26 +489,26 @@ func SetCursorVisible(visible bool) { if !visible { c = glfw.CursorHidden } - currentUI.window.SetInputMode(glfw.CursorMode, c) + theUI.window.SetInputMode(glfw.CursorMode, c) return nil }) } func IsWindowDecorated() bool { - u := currentUI + u := theUI if !u.isRunning() { return u.isInitWindowDecorated() } v := false _ = mainthread.Run(func() error { - v = currentUI.window.GetAttrib(glfw.Decorated) == glfw.True + v = theUI.window.GetAttrib(glfw.Decorated) == glfw.True return nil }) return v } func SetWindowDecorated(decorated bool) { - u := currentUI + u := theUI if !u.isRunning() { u.setInitWindowDecorated(decorated) return @@ -525,26 +525,26 @@ func SetWindowDecorated(decorated bool) { // v = glfw.True // } // }) - // currentUI.window.SetAttrib(glfw.Decorated, v) + // theUI.window.SetAttrib(glfw.Decorated, v) // return nil } func IsWindowResizable() bool { - u := currentUI + u := theUI if !u.isRunning() { return u.isInitWindowResizable() } v := false _ = mainthread.Run(func() error { - v = currentUI.window.GetAttrib(glfw.Resizable) == glfw.True + v = theUI.window.GetAttrib(glfw.Resizable) == glfw.True return nil }) return v } func SetWindowResizable(resizable bool) { - if !currentUI.isRunning() { - currentUI.setInitWindowResizable(resizable) + if !theUI.isRunning() { + theUI.setInitWindowResizable(resizable) return } @@ -555,7 +555,7 @@ func SetWindowResizable(resizable bool) { func DeviceScaleFactor() float64 { f := 0.0 - u := currentUI + u := theUI if !u.isRunning() { return devicescale.GetAt(u.initMonitor.GetPos()) } @@ -569,7 +569,7 @@ func DeviceScaleFactor() float64 { } func Run(width, height int, scale float64, title string, g driver.GraphicsContext, mainloop bool, graphics driver.Graphics, input driver.Input) error { - u := currentUI + u := theUI _ = mainthread.Run(func() error { u.graphics = graphics u.input = input @@ -750,7 +750,7 @@ func (u *userInterface) update(g driver.GraphicsContext) error { _ = mainthread.Run(func() error { if u.isInitFullscreen() { - u := currentUI + u := theUI u.setScreenSize(u.width, u.height, u.scale, true, u.vsync) u.setInitFullscreen(false) } @@ -851,7 +851,7 @@ func (u *userInterface) forceSetScreenSize(width, height int, scale float64, ful // To prevent hanging up, return asap if the width is too small. // 252 is an arbitrary number and I guess this is small enough. minWindowWidth := 252 - if currentUI.window.GetAttrib(glfw.Decorated) == glfw.False { + if theUI.window.GetAttrib(glfw.Decorated) == glfw.False { minWindowWidth = 1 } diff --git a/internal/ui/ui_js.go b/internal/ui/ui_js.go index 4f05af658..f6dc3cfd1 100644 --- a/internal/ui/ui_js.go +++ b/internal/ui/ui_js.go @@ -56,7 +56,7 @@ type userInterface struct { input inputDriver } -var currentUI = &userInterface{ +var theUI = &userInterface{ sizeChanged: true, windowFocus: true, pageVisible: true, @@ -75,39 +75,39 @@ func ScreenSizeInFullscreen() (int, int) { } func SetScreenSize(width, height int) bool { - return currentUI.setScreenSize(width, height, currentUI.scale, currentUI.fullscreen) + return theUI.setScreenSize(width, height, theUI.scale, theUI.fullscreen) } func SetScreenScale(scale float64) bool { - return currentUI.setScreenSize(currentUI.width, currentUI.height, scale, currentUI.fullscreen) + return theUI.setScreenSize(theUI.width, theUI.height, scale, theUI.fullscreen) } func ScreenScale() float64 { - return currentUI.scale + return theUI.scale } func SetFullscreen(fullscreen bool) { - currentUI.setScreenSize(currentUI.width, currentUI.height, currentUI.scale, fullscreen) + theUI.setScreenSize(theUI.width, theUI.height, theUI.scale, fullscreen) } func IsFullscreen() bool { - return currentUI.fullscreen + return theUI.fullscreen } func SetRunnableInBackground(runnableInBackground bool) { - currentUI.runnableInBackground = runnableInBackground + theUI.runnableInBackground = runnableInBackground } func IsRunnableInBackground() bool { - return currentUI.runnableInBackground + return theUI.runnableInBackground } func SetVsyncEnabled(enabled bool) { - currentUI.vsync = enabled + theUI.vsync = enabled } func IsVsyncEnabled() bool { - return currentUI.vsync + return theUI.vsync } func ScreenPadding() (x0, y0, x1, y1 float64) { @@ -118,7 +118,7 @@ func AdjustPosition(x, y int) (int, int) { rect := canvas.Call("getBoundingClientRect") x -= rect.Get("left").Int() y -= rect.Get("top").Int() - scale := currentUI.getScale() + scale := theUI.getScale() return int(float64(x) / scale), int(float64(y) / scale) } @@ -259,31 +259,31 @@ func init() { } window.Call("addEventListener", "focus", js.NewCallback(func([]js.Value) { - currentUI.windowFocus = true - if currentUI.suspended() { + theUI.windowFocus = true + if theUI.suspended() { hooks.SuspendAudio() } else { hooks.ResumeAudio() } })) window.Call("addEventListener", "blur", js.NewCallback(func([]js.Value) { - currentUI.windowFocus = false - if currentUI.suspended() { + theUI.windowFocus = false + if theUI.suspended() { hooks.SuspendAudio() } else { hooks.ResumeAudio() } })) document.Call("addEventListener", "visibilitychange", js.NewCallback(func([]js.Value) { - currentUI.pageVisible = !document.Get("hidden").Bool() - if currentUI.suspended() { + theUI.pageVisible = !document.Get("hidden").Bool() + if theUI.suspended() { hooks.SuspendAudio() } else { hooks.ResumeAudio() } })) window.Call("addEventListener", "resize", js.NewCallback(func([]js.Value) { - currentUI.updateScreenSize() + theUI.updateScreenSize() })) // Adjust the initial scale to 1. @@ -329,38 +329,38 @@ func init() { // Keyboard // Don't 'preventDefault' on keydown events or keypress events wouldn't work (#715). canvas.Call("addEventListener", "keydown", js.NewEventCallback(0, func(e js.Value) { - currentUI.input.Update(e) + theUI.input.Update(e) })) canvas.Call("addEventListener", "keypress", js.NewEventCallback(js.PreventDefault, func(e js.Value) { - currentUI.input.Update(e) + theUI.input.Update(e) })) canvas.Call("addEventListener", "keyup", js.NewEventCallback(js.PreventDefault, func(e js.Value) { - currentUI.input.Update(e) + theUI.input.Update(e) })) // Mouse canvas.Call("addEventListener", "mousedown", js.NewEventCallback(js.PreventDefault, func(e js.Value) { - currentUI.input.Update(e) + theUI.input.Update(e) })) canvas.Call("addEventListener", "mouseup", js.NewEventCallback(js.PreventDefault, func(e js.Value) { - currentUI.input.Update(e) + theUI.input.Update(e) })) canvas.Call("addEventListener", "mousemove", js.NewEventCallback(js.PreventDefault, func(e js.Value) { - currentUI.input.Update(e) + theUI.input.Update(e) })) canvas.Call("addEventListener", "wheel", js.NewEventCallback(js.PreventDefault, func(e js.Value) { - currentUI.input.Update(e) + theUI.input.Update(e) })) // Touch canvas.Call("addEventListener", "touchstart", js.NewEventCallback(js.PreventDefault, func(e js.Value) { - currentUI.input.Update(e) + theUI.input.Update(e) })) canvas.Call("addEventListener", "touchend", js.NewEventCallback(js.PreventDefault, func(e js.Value) { - currentUI.input.Update(e) + theUI.input.Update(e) })) canvas.Call("addEventListener", "touchmove", js.NewEventCallback(js.PreventDefault, func(e js.Value) { - currentUI.input.Update(e) + theUI.input.Update(e) })) // Gamepad @@ -374,10 +374,10 @@ func init() { // Context canvas.Call("addEventListener", "webglcontextlost", js.NewEventCallback(js.PreventDefault, func(js.Value) { - currentUI.contextLost = true + theUI.contextLost = true })) canvas.Call("addEventListener", "webglcontextrestored", js.NewCallback(func(e []js.Value) { - currentUI.contextLost = false + theUI.contextLost = false })) } @@ -386,7 +386,7 @@ func Loop(ch <-chan error) error { } func Run(width, height int, scale float64, title string, g driver.GraphicsContext, mainloop bool, graphics driver.Graphics, input driver.Input) error { - u := currentUI + u := theUI u.input = input.(inputDriver) document.Set("title", title) diff --git a/internal/ui/ui_mobile.go b/internal/ui/ui_mobile.go index effa4752e..8739f2ab1 100644 --- a/internal/ui/ui_mobile.go +++ b/internal/ui/ui_mobile.go @@ -41,7 +41,7 @@ var ( glContextCh = make(chan gl.Context) renderCh = make(chan struct{}) renderChEnd = make(chan struct{}) - currentUI = &userInterface{} + theUI = &userInterface{} ) func Render(chError <-chan error) error { @@ -153,7 +153,7 @@ func Run(width, height int, scale float64, title string, g driver.GraphicsContex panic("ui: graphics driver must be OpenGL") } - u := currentUI + u := theUI u.m.Lock() u.width = width @@ -211,7 +211,7 @@ func (u *userInterface) updateGraphicsContext(g driver.GraphicsContext) { } func actualScale() float64 { - return currentUI.actualScale() + return theUI.actualScale() } func (u *userInterface) actualScale() float64 { @@ -255,7 +255,7 @@ render: } func screenSize() (int, int) { - return currentUI.screenSize() + return theUI.screenSize() } func (u *userInterface) screenSize() (int, int) { @@ -272,7 +272,7 @@ func ScreenSizeInFullscreen() (int, int) { } func SetScreenSize(width, height int) bool { - currentUI.setScreenSize(width, height) + theUI.setScreenSize(width, height) return true } @@ -288,7 +288,7 @@ func (u *userInterface) setScreenSize(width, height int) { } func SetScreenScale(scale float64) bool { - currentUI.setScreenScale(scale) + theUI.setScreenScale(scale) return false } @@ -302,7 +302,7 @@ func (u *userInterface) setScreenScale(scale float64) { } func ScreenScale() float64 { - u := currentUI + u := theUI u.m.RLock() s := u.scale u.m.RUnlock() @@ -310,7 +310,7 @@ func ScreenScale() float64 { } func setFullscreen(widthPx, heightPx int) { - currentUI.setFullscreen(widthPx, heightPx) + theUI.setFullscreen(widthPx, heightPx) } func (u *userInterface) setFullscreen(widthPx, heightPx int) { @@ -338,7 +338,7 @@ func (u *userInterface) updateFullscreenScaleIfNeeded() { } func ScreenPadding() (x0, y0, x1, y1 float64) { - return currentUI.screenPadding() + return theUI.screenPadding() } func (u *userInterface) screenPadding() (x0, y0, x1, y1 float64) { @@ -359,7 +359,7 @@ func (u *userInterface) screenPaddingImpl() (x0, y0, x1, y1 float64) { } func AdjustPosition(x, y int) (int, int) { - return currentUI.adjustPosition(x, y) + return theUI.adjustPosition(x, y) } func (u *userInterface) adjustPosition(x, y int) (int, int) { diff --git a/internal/ui/ui_unix.go b/internal/ui/ui_unix.go index 182606a3f..5014da6bf 100644 --- a/internal/ui/ui_unix.go +++ b/internal/ui/ui_unix.go @@ -25,9 +25,9 @@ import ( func glfwScale() float64 { // This function must be called on the main thread. - cm, ok := getCachedMonitor(currentUI.window.GetPos()) + cm, ok := getCachedMonitor(theUI.window.GetPos()) if !ok { - return devicescale.GetAt(currentUI.currentMonitor().GetPos()) + return devicescale.GetAt(theUI.currentMonitor().GetPos()) } return devicescale.GetAt(cm.x, cm.y) } diff --git a/internal/ui/ui_windows.go b/internal/ui/ui_windows.go index af7f5251f..e93baf709 100644 --- a/internal/ui/ui_windows.go +++ b/internal/ui/ui_windows.go @@ -102,7 +102,7 @@ func getMonitorInfoW(hMonitor uintptr, lpmi *monitorInfo) error { func glfwScale() float64 { // This function must be called on the main thread. - return devicescale.GetAt(currentUI.currentMonitor().GetPos()) + return devicescale.GetAt(theUI.currentMonitor().GetPos()) } func adjustWindowPosition(x, y int) (int, int) {