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

View File

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

View File

@ -93,7 +93,6 @@ type userInterfaceImpl struct {
initWindowFloating bool
initWindowMaximized bool
initScreenTransparent bool
initFocused bool
origWindowPosX int
origWindowPosY int
@ -135,7 +134,6 @@ func init() {
initWindowPositionYInDIP: invalidPos,
initWindowWidthInDIP: 640,
initWindowHeightInDIP: 480,
initFocused: true,
fpsMode: FPSModeVsyncOn,
origWindowPosX: invalidPos,
origWindowPosY: invalidPos,
@ -490,27 +488,6 @@ func (u *userInterfaceImpl) isWindowBeingClosed() bool {
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) {
if !u.isRunning() {
return u.initFullscreenWidthInDIP, u.initFullscreenHeightInDIP
@ -933,9 +910,9 @@ func (u *userInterfaceImpl) init(options *RunOptions) error {
}
glfw.WindowHint(glfw.Floating, floating)
focused := glfw.False
if u.isInitFocused() {
focused = glfw.True
focused := glfw.True
if options.InitUnfocused {
focused = glfw.False
}
glfw.WindowHint(glfw.FocusOnShow, focused)
@ -1467,13 +1444,6 @@ func (u *userInterfaceImpl) resetForTick() {
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 {
return &u.input
}

View File

@ -77,7 +77,6 @@ type userInterfaceImpl struct {
fpsMode FPSModeType
renderingScheduled bool
running bool
initFocused bool
cursorMode CursorMode
cursorPrevMode CursorMode
cursorShape CursorShape
@ -96,7 +95,6 @@ type userInterfaceImpl struct {
func init() {
theUI.userInterfaceImpl = userInterfaceImpl{
runnableOnUnfocused: true,
initFocused: true,
}
theUI.input.ui = &theUI.userInterfaceImpl
}
@ -638,7 +636,7 @@ func (u *userInterfaceImpl) forceUpdateOnMinimumFPSMode() {
}
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.
// 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"))
@ -689,13 +687,6 @@ func (u *userInterfaceImpl) 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 {
return &u.input
}

View File

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

View File

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

22
run.go
View File

@ -232,8 +232,15 @@ func RunGame(game Game) error {
// RungameOptions represents options for RunGameWithOptions.
type RunGameOptions struct {
// GraphicsLibrary is a graphics library Ebitengine will use.
//
// The default (zero) value is GraphicsLibraryAuto, which lets Ebitengine choose the graphics library.
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.
@ -598,15 +605,26 @@ func SetScreenTransparent(transparent bool) {
// SetInitFocused panics if this is called after the main loop.
//
// SetInitFocused is cuncurrent-safe.
//
// Deprecated: as of v2.5. Use RunGameWithOptions instead.
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 {
if options == nil {
return &ui.RunOptions{}
return &ui.RunOptions{
InitUnfocused: atomic.LoadInt32(&initUnfocused) != 0,
}
}
return &ui.RunOptions{
GraphicsLibrary: ui.GraphicsLibrary(options.GraphicsLibrary),
InitUnfocused: options.InitUnfocused,
}
}