ebiten: add RunGameOptions.InitUnfocused

Updates #2378
This commit is contained in:
Hajime Hoshi 2022-12-09 19:04:52 +09:00
parent bb68ebfcad
commit d31b0189a2
7 changed files with 26 additions and 53 deletions

View File

@ -431,7 +431,6 @@ func main() {
ebiten.SetWindowResizingMode(ebiten.WindowResizingModeEnabled) ebiten.SetWindowResizingMode(ebiten.WindowResizingModeEnabled)
} }
ebiten.SetInitFocused(*flagInitFocused)
if !*flagInitFocused { if !*flagInitFocused {
ebiten.SetRunnableOnUnfocused(true) ebiten.SetRunnableOnUnfocused(true)
} }
@ -464,6 +463,7 @@ func main() {
default: default:
log.Fatalf("unexpected graphics library: %s", *flagGraphicsLibrary) log.Fatalf("unexpected graphics library: %s", *flagGraphicsLibrary)
} }
op.InitUnfocused = !*flagInitFocused
const title = "Window Size (Ebitengine Demo)" const title = "Window Size (Ebitengine Demo)"
ww := int(float64(g.width) * initScreenScale) ww := int(float64(g.width) * initScreenScale)

View File

@ -98,4 +98,5 @@ func (u *UserInterface) dumpImages(dir string) (string, error) {
type RunOptions struct { type RunOptions struct {
GraphicsLibrary GraphicsLibrary GraphicsLibrary GraphicsLibrary
InitUnfocused bool
} }

View File

@ -93,7 +93,6 @@ type userInterfaceImpl struct {
initWindowFloating bool initWindowFloating bool
initWindowMaximized bool initWindowMaximized bool
initScreenTransparent bool initScreenTransparent bool
initFocused bool
origWindowPosX int origWindowPosX int
origWindowPosY int origWindowPosY int
@ -135,7 +134,6 @@ func init() {
initWindowPositionYInDIP: invalidPos, initWindowPositionYInDIP: invalidPos,
initWindowWidthInDIP: 640, initWindowWidthInDIP: 640,
initWindowHeightInDIP: 480, initWindowHeightInDIP: 480,
initFocused: true,
fpsMode: FPSModeVsyncOn, fpsMode: FPSModeVsyncOn,
origWindowPosX: invalidPos, origWindowPosX: invalidPos,
origWindowPosY: invalidPos, origWindowPosY: invalidPos,
@ -490,27 +488,6 @@ func (u *userInterfaceImpl) isWindowBeingClosed() bool {
return v return v
} }
func (u *userInterfaceImpl) isInitFocused() bool {
if microsoftgdk.IsXbox() {
return true
}
u.m.RLock()
v := u.initFocused
u.m.RUnlock()
return v
}
func (u *userInterfaceImpl) setInitFocused(focused bool) {
if microsoftgdk.IsXbox() {
return
}
u.m.Lock()
u.initFocused = focused
u.m.Unlock()
}
func (u *userInterfaceImpl) ScreenSizeInFullscreen() (int, int) { func (u *userInterfaceImpl) ScreenSizeInFullscreen() (int, int) {
if !u.isRunning() { if !u.isRunning() {
return u.initFullscreenWidthInDIP, u.initFullscreenHeightInDIP return u.initFullscreenWidthInDIP, u.initFullscreenHeightInDIP
@ -933,9 +910,9 @@ func (u *userInterfaceImpl) init(options *RunOptions) error {
} }
glfw.WindowHint(glfw.Floating, floating) glfw.WindowHint(glfw.Floating, floating)
focused := glfw.False focused := glfw.True
if u.isInitFocused() { if options.InitUnfocused {
focused = glfw.True focused = glfw.False
} }
glfw.WindowHint(glfw.FocusOnShow, focused) glfw.WindowHint(glfw.FocusOnShow, focused)
@ -1467,13 +1444,6 @@ func (u *userInterfaceImpl) resetForTick() {
u.m.Unlock() u.m.Unlock()
} }
func (u *userInterfaceImpl) SetInitFocused(focused bool) {
if u.isRunning() {
panic("ui: SetInitFocused must be called before the main loop")
}
u.setInitFocused(focused)
}
func (u *userInterfaceImpl) Input() *Input { func (u *userInterfaceImpl) Input() *Input {
return &u.input return &u.input
} }

View File

@ -77,7 +77,6 @@ type userInterfaceImpl struct {
fpsMode FPSModeType fpsMode FPSModeType
renderingScheduled bool renderingScheduled bool
running bool running bool
initFocused bool
cursorMode CursorMode cursorMode CursorMode
cursorPrevMode CursorMode cursorPrevMode CursorMode
cursorShape CursorShape cursorShape CursorShape
@ -96,7 +95,6 @@ type userInterfaceImpl struct {
func init() { func init() {
theUI.userInterfaceImpl = userInterfaceImpl{ theUI.userInterfaceImpl = userInterfaceImpl{
runnableOnUnfocused: true, runnableOnUnfocused: true,
initFocused: true,
} }
theUI.input.ui = &theUI.userInterfaceImpl theUI.input.ui = &theUI.userInterfaceImpl
} }
@ -638,7 +636,7 @@ func (u *userInterfaceImpl) forceUpdateOnMinimumFPSMode() {
} }
func (u *userInterfaceImpl) Run(game Game, options *RunOptions) error { func (u *userInterfaceImpl) Run(game Game, options *RunOptions) error {
if u.initFocused && window.Truthy() { if !options.InitUnfocused && window.Truthy() {
// Do not focus the canvas when the current document is in an iframe. // Do not focus the canvas when the current document is in an iframe.
// Otherwise, the parent page tries to focus the iframe on every loading, which is annoying (#1373). // Otherwise, the parent page tries to focus the iframe on every loading, which is annoying (#1373).
isInIframe := !window.Get("location").Equal(window.Get("parent").Get("location")) isInIframe := !window.Get("location").Equal(window.Get("parent").Get("location"))
@ -689,13 +687,6 @@ func (u *userInterfaceImpl) resetForTick() {
u.input.resetForTick() u.input.resetForTick()
} }
func (u *userInterfaceImpl) SetInitFocused(focused bool) {
if u.running {
panic("ui: SetInitFocused must be called before the main loop")
}
u.initFocused = focused
}
func (u *userInterfaceImpl) Input() *Input { func (u *userInterfaceImpl) Input() *Input {
return &u.input return &u.input
} }

View File

@ -433,10 +433,6 @@ func (u *userInterfaceImpl) resetForTick() {
u.input.resetForTick() u.input.resetForTick()
} }
func (u *userInterfaceImpl) SetInitFocused(focused bool) {
// Do nothing
}
func (u *userInterfaceImpl) Input() *Input { func (u *userInterfaceImpl) Input() *Input {
return &u.input return &u.input
} }

View File

@ -133,9 +133,6 @@ func (*userInterfaceImpl) IsScreenTransparent() bool {
func (*userInterfaceImpl) SetScreenTransparent(transparent bool) { func (*userInterfaceImpl) SetScreenTransparent(transparent bool) {
} }
func (*userInterfaceImpl) SetInitFocused(focused bool) {
}
func (*userInterfaceImpl) Input() *Input { func (*userInterfaceImpl) Input() *Input {
return &theUI.input return &theUI.input
} }

22
run.go
View File

@ -232,8 +232,15 @@ func RunGame(game Game) error {
// RungameOptions represents options for RunGameWithOptions. // RungameOptions represents options for RunGameWithOptions.
type RunGameOptions struct { type RunGameOptions struct {
// GraphicsLibrary is a graphics library Ebitengine will use. // GraphicsLibrary is a graphics library Ebitengine will use.
//
// The default (zero) value is GraphicsLibraryAuto, which lets Ebitengine choose the graphics library. // The default (zero) value is GraphicsLibraryAuto, which lets Ebitengine choose the graphics library.
GraphicsLibrary GraphicsLibrary GraphicsLibrary GraphicsLibrary
// InitUnfocused represents whether the window is unfocused or not on launching.
// InitUnfocused is valid on desktops and browsers.
//
// The default (zero) value is false, which means that the window is focused.
InitUnfocused bool
} }
// RunGameWithOptions starts the main loop and runs the game with the specified options. // RunGameWithOptions starts the main loop and runs the game with the specified options.
@ -598,15 +605,26 @@ func SetScreenTransparent(transparent bool) {
// SetInitFocused panics if this is called after the main loop. // SetInitFocused panics if this is called after the main loop.
// //
// SetInitFocused is cuncurrent-safe. // SetInitFocused is cuncurrent-safe.
//
// Deprecated: as of v2.5. Use RunGameWithOptions instead.
func SetInitFocused(focused bool) { func SetInitFocused(focused bool) {
ui.Get().SetInitFocused(focused) if focused {
atomic.StoreInt32(&initUnfocused, 0)
} else {
atomic.StoreInt32(&initUnfocused, 1)
}
} }
var initUnfocused int32 = 0
func toUIRunOptions(options *RunGameOptions) *ui.RunOptions { func toUIRunOptions(options *RunGameOptions) *ui.RunOptions {
if options == nil { if options == nil {
return &ui.RunOptions{} return &ui.RunOptions{
InitUnfocused: atomic.LoadInt32(&initUnfocused) != 0,
}
} }
return &ui.RunOptions{ return &ui.RunOptions{
GraphicsLibrary: ui.GraphicsLibrary(options.GraphicsLibrary), GraphicsLibrary: ui.GraphicsLibrary(options.GraphicsLibrary),
InitUnfocused: options.InitUnfocused,
} }
} }