ebiten: Add IsWindowBeingClosed / SetWindowClosingHandled / IsWindowClosingHandled

IsWindowBeingClosed reports whether the window is being closed by
the user.

SetWindowClosingHandled sets whether the window closing is handled
or not. If the state is true, the window is not closed immediately
by the user and the game can handle the closing state. In this case,
the Update function should return an error in order to end the game.

This change also adds examples/windowclosing.

Closes #1574
This commit is contained in:
Hajime Hoshi 2021-06-13 20:52:14 +09:00
parent f989ce4e64
commit 49c3c30c79
10 changed files with 227 additions and 8 deletions

View File

@ -0,0 +1,67 @@
// Copyright 2021 The Ebiten Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// +build example
package main
import (
"errors"
"log"
"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/ebitenutil"
"github.com/hajimehoshi/ebiten/v2/inpututil"
)
var regularTermination = errors.New("regular termination")
type Game struct {
windowClosingHandled bool
}
func (g *Game) Update() error {
if ebiten.IsWindowBeingClosed() {
g.windowClosingHandled = true
}
if g.windowClosingHandled {
if inpututil.IsKeyJustPressed(ebiten.KeyY) {
return regularTermination
}
if inpututil.IsKeyJustPressed(ebiten.KeyN) {
g.windowClosingHandled = false
}
}
return nil
}
func (g *Game) Draw(screen *ebiten.Image) {
if !g.windowClosingHandled {
ebitenutil.DebugPrint(screen, "Try to close this window.")
return
}
ebitenutil.DebugPrint(screen, "Do you really want to close this window? [y/n]")
}
func (g *Game) Layout(outsideWidth, outsideHeight int) (int, int) {
return outsideWidth, outsideHeight
}
func main() {
ebiten.SetWindowClosingHandled(true)
ebiten.SetWindowTitle("Window Closing (Ebiten Demo)")
if err := ebiten.RunGame(&Game{}); err != nil && err != regularTermination {
log.Fatal(err)
}
}

View File

@ -93,4 +93,8 @@ type Window interface {
SetIcon(iconImages []image.Image)
SetTitle(title string)
Restore()
IsBeingClosed() bool
SetClosingHandled(handled bool)
IsClosingHandled() bool
}

View File

@ -23,6 +23,7 @@ import (
var (
charModsCallbacks = map[CharModsCallback]glfw.CharModsCallback{}
closeCallbacks = map[CloseCallback]glfw.CloseCallback{}
framebufferSizeCallbacks = map[FramebufferSizeCallback]glfw.FramebufferSizeCallback{}
scrollCallbacks = map[ScrollCallback]glfw.ScrollCallback{}
sizeCallbacks = map[SizeCallback]glfw.SizeCallback{}
@ -40,6 +41,18 @@ func ToCharModsCallback(cb func(window *Window, char rune, mods ModifierKey)) Ch
return id
}
func ToCloseCallback(cb func(window *Window)) CloseCallback {
if cb == nil {
return 0
}
id := CloseCallback(len(closeCallbacks) + 1)
var gcb glfw.CloseCallback = func(window *glfw.Window) {
cb(theWindows.get(window))
}
closeCallbacks[id] = gcb
return id
}
func ToFramebufferSizeCallback(cb func(window *Window, width int, height int)) FramebufferSizeCallback {
if cb == nil {
return 0

View File

@ -28,6 +28,16 @@ func ToCharModsCallback(cb func(window *Window, char rune, mods ModifierKey)) Ch
}))
}
func ToCloseCallback(cb func(window *Window)) CloseCallback {
if cb == nil {
return 0
}
return CloseCallback(windows.NewCallbackCDecl(func(window uintptr) uintptr {
cb(theGLFWWindows.get(window))
return 0
}))
}
func ToFramebufferSizeCallback(cb func(window *Window, width int, height int)) FramebufferSizeCallback {
if cb == nil {
return 0

View File

@ -174,6 +174,11 @@ func (w *Window) SetCursor(cursor *Cursor) {
w.w.SetCursor(c)
}
func (w *Window) SetCloseCallback(cbfun CloseCallback) (previous CloseCallback) {
w.w.SetCloseCallback(closeCallbacks[cbfun])
return ToCloseCallback(nil) // TODO
}
func (w *Window) SetFramebufferSizeCallback(cbfun FramebufferSizeCallback) (previous FramebufferSizeCallback) {
w.w.SetFramebufferSizeCallback(framebufferSizeCallbacks[cbfun])
return ToFramebufferSizeCallback(nil) // TODO
@ -184,6 +189,10 @@ func (w *Window) SetScrollCallback(cbfun ScrollCallback) (previous ScrollCallbac
return ToScrollCallback(nil) // TODO
}
func (w *Window) SetShouldClose(value bool) {
w.w.SetShouldClose(value)
}
func (w *Window) SetSizeCallback(cbfun SizeCallback) (previous SizeCallback) {
w.w.SetSizeCallback(sizeCallbacks[cbfun])
prev := w.prevSizeCallback

View File

@ -201,6 +201,12 @@ func (w *Window) SetCharModsCallback(cbfun CharModsCallback) (previous CharModsC
return ToCharModsCallback(nil) // TODO
}
func (w *Window) SetCloseCallback(cbfun CloseCallback) (previous CloseCallback) {
glfwDLL.call("glfwSetWindowCloseCallback", w.w, uintptr(cbfun))
panicError()
return ToCloseCallback(nil) // TODO
}
func (w *Window) SetCursor(cursor *Cursor) {
var c uintptr
if cursor != nil {
@ -221,6 +227,15 @@ func (w *Window) SetScrollCallback(cbfun ScrollCallback) (previous ScrollCallbac
return ToScrollCallback(nil) // TODO
}
func (w *Window) SetShouldClose(value bool) {
var v uintptr = False
if value {
v = True
}
glfwDLL.call("glfwSetWindowShouldClose", w.w, v)
panicError()
}
func (w *Window) SetSizeCallback(cbfun SizeCallback) (previous SizeCallback) {
glfwDLL.call("glfwSetWindowSizeCallback", w.w, uintptr(cbfun))
panicError()

View File

@ -19,6 +19,7 @@ package glfw
type (
CharModsCallback uintptr
CloseCallback uintptr
FramebufferSizeCallback uintptr
ScrollCallback uintptr
SizeCallback uintptr

View File

@ -64,14 +64,16 @@ type UserInterface struct {
maxWindowWidthInDP int
maxWindowHeightInDP int
running uint32
toChangeSize bool
origPosX int
origPosY int
runnableOnUnfocused bool
vsync bool
iconImages []image.Image
cursorShape driver.CursorShape
running uint32
toChangeSize bool
origPosX int
origPosY int
runnableOnUnfocused bool
vsync bool
iconImages []image.Image
cursorShape driver.CursorShape
windowClosingHandled bool
windowBeingClosed bool
// setSizeCallbackEnabled must be accessed from the main thread.
setSizeCallbackEnabled bool
@ -108,6 +110,7 @@ type UserInterface struct {
iwindow window
sizeCallback glfw.SizeCallback
closeCallback glfw.CloseCallback
framebufferSizeCallback glfw.FramebufferSizeCallback
framebufferSizeCallbackCh chan struct{}
@ -476,6 +479,26 @@ func (u *UserInterface) setInitWindowMaximized(maximized bool) {
u.m.Unlock()
}
func (u *UserInterface) isWindowClosingHandled() bool {
u.m.Lock()
v := u.windowClosingHandled
u.m.Unlock()
return v
}
func (u *UserInterface) setWindowClosingHandled(handled bool) {
u.m.Lock()
u.windowClosingHandled = handled
u.m.Unlock()
}
func (u *UserInterface) isWindowBeingClosed() bool {
u.m.Lock()
v := u.windowBeingClosed
u.m.Unlock()
return v
}
func (u *UserInterface) isInitFocused() bool {
u.m.Lock()
v := u.initFocused
@ -724,6 +747,7 @@ func (u *UserInterface) createWindow() error {
// TODO: Set icons
u.registerWindowSetSizeCallback()
u.registerWindowCloseCallback()
return nil
}
@ -776,6 +800,23 @@ func (u *UserInterface) registerWindowSetSizeCallback() {
u.window.SetSizeCallback(u.sizeCallback)
}
// registerWindowCloseCallback must be called from the main thread.
func (u *UserInterface) registerWindowCloseCallback() {
if u.closeCallback == 0 {
u.closeCallback = glfw.ToCloseCallback(func(_ *glfw.Window) {
u.m.Lock()
u.windowBeingClosed = true
u.m.Unlock()
if !u.isWindowClosingHandled() {
return
}
u.window.SetShouldClose(false)
})
}
u.window.SetCloseCallback(u.closeCallback)
}
func (u *UserInterface) init() error {
if u.Graphics().IsGL() {
glfw.WindowHint(glfw.ClientAPI, glfw.OpenGLAPI)
@ -1347,6 +1388,10 @@ func (u *UserInterface) ResetForFrame() {
u.context.Layout(w, h)
}
u.input.resetForFrame()
u.m.Lock()
u.windowBeingClosed = false
u.m.Unlock()
}
func (u *UserInterface) MonitorPosition() (int, int) {

View File

@ -264,3 +264,15 @@ func (w *window) SetTitle(title string) {
return nil
})
}
func (w *window) IsBeingClosed() bool {
return w.ui.isWindowBeingClosed()
}
func (w *window) SetClosingHandled(handled bool) {
w.ui.setWindowClosingHandled(handled)
}
func (w *window) IsClosingHandled() bool {
return w.ui.isWindowClosingHandled()
}

View File

@ -315,3 +315,46 @@ func RestoreWindow() {
w.Restore()
}
}
// IsWindowBeingClosed returns true when the user is trying to close the window on desktops.
// As the window is closed immediately by default,
// you might want to call SetWindowClosingHandled(true) to prevent the window is automatically closed.
//
// IsWindowBeingClosed always returns false on other platforms.
//
// IsWindowBeingClosed is concurrent-safe.
func IsWindowBeingClosed() bool {
if w := uiDriver().Window(); w != nil {
return w.IsBeingClosed()
}
return false
}
// SetWindowClosingHandled sets whether the window closing is handled or not on desktops. The default state is false.
//
// If the window closing is handled, the window is not closed immediately and
// the game can know whether the window is begin closed or not by IsWindowBeingClosed.
// In this case, the window is not closed automatically.
// To end the game, you have to return an error value at the Game's Update function.
//
// SetWindowClosingHandled works only on desktops.
// SetWindowClosingHandled does nothing on other platforms.
//
// SetWindowClosingHandled is concurrent-safe.
func SetWindowClosingHandled(handled bool) {
if w := uiDriver().Window(); w != nil {
w.SetClosingHandled(handled)
}
}
// IsWindowClosingHandled reports whether the window closing is handled or not on desktops by SetWindowClosingHandled.
//
// IsWindowClosingHandled always returns false on other platforms.
//
// IsWindowClosingHandled is concurrent-safe.
func IsWindowClosingHandled() bool {
if w := uiDriver().Window(); w != nil {
return w.IsClosingHandled()
}
return false
}