From d53803615afbcf1f4d5fe02d9b1b8272e39d9110 Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Sat, 21 Jan 2023 23:34:20 +0900 Subject: [PATCH] internal/ui: merge a window-closing state into an input state --- input.go | 7 +++++++ internal/ui/context.go | 2 -- internal/ui/input.go | 4 +++- internal/ui/ui_glfw.go | 18 ++---------------- internal/ui/ui_js.go | 5 +---- internal/ui/ui_mobile.go | 5 +---- internal/ui/ui_nintendosdk.go | 5 +---- internal/ui/window.go | 5 ----- internal/ui/window_glfw.go | 4 ---- window.go | 2 +- 10 files changed, 16 insertions(+), 41 deletions(-) diff --git a/input.go b/input.go index ddcac2acb..3119c4d09 100644 --- a/input.go +++ b/input.go @@ -476,3 +476,10 @@ func (i *inputState) touchPosition(id TouchID) (int, int) { } return 0, 0 } + +func (i *inputState) windowBeingClosed() bool { + i.m.Lock() + defer i.m.Unlock() + + return i.state.WindowBeingClosed +} diff --git a/internal/ui/context.go b/internal/ui/context.go index 69e460e81..54e1f40bc 100644 --- a/internal/ui/context.go +++ b/internal/ui/context.go @@ -143,8 +143,6 @@ func (c *context) updateFrameImpl(graphicsDriver graphicsdriver.Graphics, update if err := theGlobalState.error(); err != nil { return err } - - ui.resetForTick() } // Update window icons during a frame, since an icon might be *ebiten.Image and diff --git a/internal/ui/input.go b/internal/ui/input.go index 340c1d2f6..a7a439323 100644 --- a/internal/ui/input.go +++ b/internal/ui/input.go @@ -48,12 +48,14 @@ type InputState struct { Touches [16]Touch Runes [16]rune RunesCount int + WindowBeingClosed bool } -func (i *InputState) resetForTick() { +func (i *InputState) reset() { i.WheelX = 0 i.WheelY = 0 i.RunesCount = 0 + i.WindowBeingClosed = false } func (i *InputState) appendRune(r rune) { diff --git a/internal/ui/ui_glfw.go b/internal/ui/ui_glfw.go index 78ef7ca97..6c949b3b4 100644 --- a/internal/ui/ui_glfw.go +++ b/internal/ui/ui_glfw.go @@ -66,7 +66,6 @@ type userInterfaceImpl struct { iconImages []image.Image cursorShape CursorShape windowClosingHandled bool - windowBeingClosed bool windowResizingMode WindowResizingMode inFrame uint32 @@ -472,13 +471,6 @@ func (u *userInterfaceImpl) setWindowClosingHandled(handled bool) { u.m.Unlock() } -func (u *userInterfaceImpl) isWindowBeingClosed() bool { - u.m.RLock() - v := u.windowBeingClosed - u.m.RUnlock() - return v -} - func (u *userInterfaceImpl) ScreenSizeInFullscreen() (int, int) { if !u.isRunning() { return u.initFullscreenWidthInDIP, u.initFullscreenHeightInDIP @@ -713,7 +705,7 @@ func (u *userInterfaceImpl) registerWindowCloseCallback() { if u.closeCallback == nil { u.closeCallback = glfw.ToCloseCallback(func(_ *glfw.Window) { u.m.Lock() - u.windowBeingClosed = true + u.inputState.WindowBeingClosed = true u.m.Unlock() if !u.isWindowClosingHandled() { @@ -1346,17 +1338,11 @@ func monitorFromWindow(window *glfw.Window) *glfw.Monitor { return nil } -func (u *userInterfaceImpl) resetForTick() { - u.m.Lock() - defer u.m.Unlock() - u.windowBeingClosed = false -} - func (u *userInterfaceImpl) readInputState(inputState *InputState) { u.m.Lock() defer u.m.Unlock() *inputState = u.inputState - u.inputState.resetForTick() + u.inputState.reset() } func (u *userInterfaceImpl) Window() Window { diff --git a/internal/ui/ui_js.go b/internal/ui/ui_js.go index b92cb2cb2..3291151df 100644 --- a/internal/ui/ui_js.go +++ b/internal/ui/ui_js.go @@ -677,10 +677,7 @@ func (u *userInterfaceImpl) updateScreenSize() { func (u *userInterfaceImpl) readInputState(inputState *InputState) { *inputState = u.inputState - u.inputState.resetForTick() -} - -func (u *userInterfaceImpl) resetForTick() { + u.inputState.reset() u.keyboardLayoutMap = js.Value{} } diff --git a/internal/ui/ui_mobile.go b/internal/ui/ui_mobile.go index 13cc7fa7e..848f123d1 100644 --- a/internal/ui/ui_mobile.go +++ b/internal/ui/ui_mobile.go @@ -422,10 +422,7 @@ func (u *userInterfaceImpl) readInputState(inputState *InputState) { u.m.Lock() defer u.m.Unlock() *inputState = u.inputState - u.inputState.resetForTick() -} - -func (u *userInterfaceImpl) resetForTick() { + u.inputState.reset() } func (u *userInterfaceImpl) Window() Window { diff --git a/internal/ui/ui_nintendosdk.go b/internal/ui/ui_nintendosdk.go index 1325cbc90..8528f6f06 100644 --- a/internal/ui/ui_nintendosdk.go +++ b/internal/ui/ui_nintendosdk.go @@ -150,10 +150,7 @@ func (*userInterfaceImpl) ScreenSizeInFullscreen() (int, int) { func (u *userInterfaceImpl) readInputState(inputState *InputState) { *inputState = u.inputState - u.inputState.resetForTick() -} - -func (u *userInterfaceImpl) resetForTick() { + u.inputState.reset() } func (*userInterfaceImpl) CursorMode() CursorMode { diff --git a/internal/ui/window.go b/internal/ui/window.go index 9e6e86c85..7e3b130a6 100644 --- a/internal/ui/window.go +++ b/internal/ui/window.go @@ -38,7 +38,6 @@ type Window interface { SetIcon(iconImages []image.Image) SetTitle(title string) Restore() - IsBeingClosed() bool SetClosingHandled(handled bool) IsClosingHandled() bool } @@ -110,10 +109,6 @@ func (*nullWindow) SetTitle(title string) { func (*nullWindow) Restore() { } -func (*nullWindow) IsBeingClosed() bool { - return false -} - func (*nullWindow) SetClosingHandled(handled bool) { } diff --git a/internal/ui/window_glfw.go b/internal/ui/window_glfw.go index a111157fb..073edd093 100644 --- a/internal/ui/window_glfw.go +++ b/internal/ui/window_glfw.go @@ -253,10 +253,6 @@ func (w *glfwWindow) SetTitle(title string) { }) } -func (w *glfwWindow) IsBeingClosed() bool { - return w.ui.isWindowBeingClosed() -} - func (w *glfwWindow) SetClosingHandled(handled bool) { w.ui.setWindowClosingHandled(handled) } diff --git a/window.go b/window.go index 577dbe8fb..7325928f8 100644 --- a/window.go +++ b/window.go @@ -294,7 +294,7 @@ func RestoreWindow() { // // IsWindowBeingClosed is concurrent-safe. func IsWindowBeingClosed() bool { - return ui.Get().Window().IsBeingClosed() + return theInputState.windowBeingClosed() } // SetWindowClosingHandled sets whether the window closing is handled or not on desktops. The default state is false.