internal/driver: remove Window

Closes #1983
This commit is contained in:
Hajime Hoshi 2022-02-06 20:02:03 +09:00
parent 80c03792cb
commit 0f09ddfbd2
8 changed files with 154 additions and 169 deletions

View File

@ -1,52 +0,0 @@
// Copyright 2019 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.
package driver
import (
"image"
)
type Window interface {
IsDecorated() bool
SetDecorated(decorated bool)
IsResizable() bool
SetResizable(resizable bool)
Position() (int, int)
SetPosition(x, y int)
Size() (int, int)
SetSize(width, height int)
SizeLimits() (minw, minh, maxw, maxh int)
SetSizeLimits(minw, minh, maxw, maxh int)
IsFloating() bool
SetFloating(floating bool)
Maximize()
IsMaximized() bool
Minimize()
IsMinimized() bool
SetIcon(iconImages []image.Image)
SetTitle(title string)
Restore()
IsBeingClosed() bool
SetClosingHandled(handled bool)
IsClosingHandled() bool
}

View File

@ -22,7 +22,6 @@ import (
"time"
"github.com/hajimehoshi/ebiten/v2/internal/cbackend"
"github.com/hajimehoshi/ebiten/v2/internal/driver"
)
const deviceScaleFactor = 1
@ -132,6 +131,6 @@ func (*UserInterface) Input() *Input {
return &theUserInterface.input
}
func (*UserInterface) Window() driver.Window {
return nil
func (*UserInterface) Window() *Window {
return &Window{}
}

View File

@ -27,7 +27,6 @@ import (
"time"
"github.com/hajimehoshi/ebiten/v2/internal/devicescale"
"github.com/hajimehoshi/ebiten/v2/internal/driver"
"github.com/hajimehoshi/ebiten/v2/internal/glfw"
"github.com/hajimehoshi/ebiten/v2/internal/hooks"
"github.com/hajimehoshi/ebiten/v2/internal/thread"
@ -102,7 +101,7 @@ type UserInterface struct {
fpsModeInited bool
input Input
iwindow window
iwindow Window
sizeCallback glfw.SizeCallback
closeCallback glfw.CloseCallback
@ -1435,7 +1434,7 @@ func (u *UserInterface) Input() *Input {
return &u.input
}
func (u *UserInterface) Window() driver.Window {
func (u *UserInterface) Window() *Window {
return &u.iwindow
}

View File

@ -19,7 +19,6 @@ import (
"time"
"github.com/hajimehoshi/ebiten/v2/internal/devicescale"
"github.com/hajimehoshi/ebiten/v2/internal/driver"
"github.com/hajimehoshi/ebiten/v2/internal/gamepad"
"github.com/hajimehoshi/ebiten/v2/internal/hooks"
)
@ -665,6 +664,6 @@ func (u *UserInterface) Input() *Input {
return &u.input
}
func (u *UserInterface) Window() driver.Window {
return nil
func (u *UserInterface) Window() *Window {
return &Window{}
}

View File

@ -34,7 +34,6 @@ import (
"golang.org/x/mobile/gl"
"github.com/hajimehoshi/ebiten/v2/internal/devicescale"
"github.com/hajimehoshi/ebiten/v2/internal/driver"
"github.com/hajimehoshi/ebiten/v2/internal/gamepad"
"github.com/hajimehoshi/ebiten/v2/internal/graphicscommand"
"github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver/opengl"
@ -450,8 +449,8 @@ func (u *UserInterface) Input() *Input {
return &u.input
}
func (u *UserInterface) Window() driver.Window {
return nil
func (u *UserInterface) Window() *Window {
return &Window{}
}
type Touch struct {

View File

@ -23,11 +23,11 @@ import (
"github.com/hajimehoshi/ebiten/v2/internal/glfw"
)
type window struct {
type Window struct {
ui *UserInterface
}
func (w *window) IsDecorated() bool {
func (w *Window) IsDecorated() bool {
if !w.ui.isRunning() {
return w.ui.isInitWindowDecorated()
}
@ -38,7 +38,7 @@ func (w *window) IsDecorated() bool {
return v
}
func (w *window) SetDecorated(decorated bool) {
func (w *Window) SetDecorated(decorated bool) {
if !w.ui.isRunning() {
w.ui.setInitWindowDecorated(decorated)
return
@ -53,7 +53,7 @@ func (w *window) SetDecorated(decorated bool) {
})
}
func (w *window) IsResizable() bool {
func (w *Window) IsResizable() bool {
if !w.ui.isRunning() {
return w.ui.isInitWindowResizable()
}
@ -64,7 +64,7 @@ func (w *window) IsResizable() bool {
return v
}
func (w *window) SetResizable(resizable bool) {
func (w *Window) SetResizable(resizable bool) {
if !w.ui.isRunning() {
w.ui.setInitWindowResizable(resizable)
return
@ -77,7 +77,7 @@ func (w *window) SetResizable(resizable bool) {
})
}
func (w *window) IsFloating() bool {
func (w *Window) IsFloating() bool {
if !w.ui.isRunning() {
return w.ui.isInitWindowFloating()
}
@ -88,7 +88,7 @@ func (w *window) IsFloating() bool {
return v
}
func (w *window) SetFloating(floating bool) {
func (w *Window) SetFloating(floating bool) {
if !w.ui.isRunning() {
w.ui.setInitWindowFloating(floating)
return
@ -101,7 +101,7 @@ func (w *window) SetFloating(floating bool) {
})
}
func (w *window) IsMaximized() bool {
func (w *Window) IsMaximized() bool {
if !w.ui.isRunning() {
return w.ui.isInitWindowMaximized()
}
@ -112,7 +112,7 @@ func (w *window) IsMaximized() bool {
return v
}
func (w *window) Maximize() {
func (w *Window) Maximize() {
if !w.IsResizable() {
panic("ui: a window to maximize must be resizable")
}
@ -123,7 +123,7 @@ func (w *window) Maximize() {
w.ui.t.Call(w.ui.maximizeWindow)
}
func (w *window) IsMinimized() bool {
func (w *Window) IsMinimized() bool {
if !w.ui.isRunning() {
return false
}
@ -134,7 +134,7 @@ func (w *window) IsMinimized() bool {
return v
}
func (w *window) Minimize() {
func (w *Window) Minimize() {
if !w.ui.isRunning() {
// Do nothing
return
@ -142,7 +142,7 @@ func (w *window) Minimize() {
w.ui.t.Call(w.ui.iconifyWindow)
}
func (w *window) Restore() {
func (w *Window) Restore() {
if !w.ui.isRunning() {
// Do nothing
return
@ -150,7 +150,7 @@ func (w *window) Restore() {
w.ui.t.Call(w.ui.restoreWindow)
}
func (w *window) Position() (int, int) {
func (w *Window) Position() (int, int) {
if !w.ui.isRunning() {
panic("ui: WindowPosition can't be called before the main loop starts")
}
@ -173,7 +173,7 @@ func (w *window) Position() (int, int) {
return x, y
}
func (w *window) SetPosition(x, y int) {
func (w *Window) SetPosition(x, y int) {
if !w.ui.isRunning() {
w.ui.setInitWindowPositionInDIP(x, y)
return
@ -183,7 +183,7 @@ func (w *window) SetPosition(x, y int) {
})
}
func (w *window) Size() (int, int) {
func (w *Window) Size() (int, int) {
if !w.ui.isRunning() {
ww, wh := w.ui.getInitWindowSizeInDIP()
return w.ui.adjustWindowSizeBasedOnSizeLimitsInDIP(ww, wh)
@ -196,7 +196,7 @@ func (w *window) Size() (int, int) {
return ww, wh
}
func (w *window) SetSize(width, height int) {
func (w *Window) SetSize(width, height int) {
if !w.ui.isRunning() {
w.ui.setInitWindowSizeInDIP(width, height)
return
@ -212,11 +212,11 @@ func (w *window) SetSize(width, height int) {
})
}
func (w *window) SizeLimits() (minw, minh, maxw, maxh int) {
func (w *Window) SizeLimits() (minw, minh, maxw, maxh int) {
return w.ui.getWindowSizeLimitsInDIP()
}
func (w *window) SetSizeLimits(minw, minh, maxw, maxh int) {
func (w *Window) SetSizeLimits(minw, minh, maxw, maxh int) {
if !w.ui.setWindowSizeLimitsInDIP(minw, minh, maxw, maxh) {
return
}
@ -227,12 +227,12 @@ func (w *window) SetSizeLimits(minw, minh, maxw, maxh int) {
w.ui.t.Call(w.ui.updateWindowSizeLimits)
}
func (w *window) SetIcon(iconImages []image.Image) {
func (w *Window) SetIcon(iconImages []image.Image) {
// The icons are actually set at (*UserInterface).loop.
w.ui.setIconImages(iconImages)
}
func (w *window) SetTitle(title string) {
func (w *Window) SetTitle(title string) {
if !w.ui.isRunning() {
w.ui.m.Lock()
w.ui.title = title
@ -245,14 +245,14 @@ func (w *window) SetTitle(title string) {
})
}
func (w *window) IsBeingClosed() bool {
func (w *Window) IsBeingClosed() bool {
return w.ui.isWindowBeingClosed()
}
func (w *window) SetClosingHandled(handled bool) {
func (w *Window) SetClosingHandled(handled bool) {
w.ui.setWindowClosingHandled(handled)
}
func (w *window) IsClosingHandled() bool {
func (w *Window) IsClosingHandled() bool {
return w.ui.isWindowClosingHandled()
}

100
internal/ui/window_null.go Normal file
View File

@ -0,0 +1,100 @@
// Copyright 2022 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.
//go:build android || js || ios || ebitencbackend
// +build android js ios ebitencbackend
package ui
import (
"image"
)
type Window struct{}
func (*Window) IsDecorated() bool {
return false
}
func (*Window) SetDecorated(decorated bool) {
}
func (*Window) IsResizable() bool {
return false
}
func (*Window) SetResizable(resizable bool) {
}
func (*Window) Position() (int, int) {
return 0, 0
}
func (*Window) SetPosition(x, y int) {
}
func (*Window) Size() (int, int) {
return 0, 0
}
func (*Window) SetSize(width, height int) {
}
func (*Window) SizeLimits() (minw, minh, maxw, maxh int) {
return -1, -1, -1, -1
}
func (*Window) SetSizeLimits(minw, minh, maxw, maxh int) {
}
func (*Window) IsFloating() bool {
return false
}
func (*Window) SetFloating(floating bool) {
}
func (*Window) Maximize() {
}
func (*Window) IsMaximized() bool {
return false
}
func (*Window) Minimize() {
}
func (*Window) IsMinimized() bool {
return false
}
func (*Window) SetIcon(iconImages []image.Image) {
}
func (*Window) SetTitle(title string) {
}
func (*Window) Restore() {
}
func (*Window) IsBeingClosed() bool {
return false
}
func (*Window) SetClosingHandled(handled bool) {
}
func (*Window) IsClosingHandled() bool {
return false
}

105
window.go
View File

@ -31,10 +31,7 @@ const (
//
// IsWindowDecorated is concurrent-safe.
func IsWindowDecorated() bool {
if w := ui.Get().Window(); w != nil {
return w.IsDecorated()
}
return false
return ui.Get().Window().IsDecorated()
}
// SetWindowDecorated sets the state if the window is decorated.
@ -49,9 +46,7 @@ func IsWindowDecorated() bool {
//
// SetWindowDecorated is concurrent-safe.
func SetWindowDecorated(decorated bool) {
if w := ui.Get().Window(); w != nil {
w.SetDecorated(decorated)
}
ui.Get().Window().SetDecorated(decorated)
}
// IsWindowResizable reports whether the window is resizable by the user's dragging on desktops.
@ -59,10 +54,7 @@ func SetWindowDecorated(decorated bool) {
//
// IsWindowResizable is concurrent-safe.
func IsWindowResizable() bool {
if w := ui.Get().Window(); w != nil {
return w.IsResizable()
}
return false
return ui.Get().Window().IsResizable()
}
// SetWindowResizable sets whether the window is resizable by the user's dragging on desktops.
@ -77,9 +69,7 @@ func IsWindowResizable() bool {
//
// SetWindowResizable is concurrent-safe.
func SetWindowResizable(resizable bool) {
if w := ui.Get().Window(); w != nil {
w.SetResizable(resizable)
}
ui.Get().Window().SetResizable(resizable)
}
// SetWindowTitle sets the title of the window.
@ -88,9 +78,7 @@ func SetWindowResizable(resizable bool) {
//
// SetWindowTitle is concurrent-safe.
func SetWindowTitle(title string) {
if w := ui.Get().Window(); w != nil {
w.SetTitle(title)
}
ui.Get().Window().SetTitle(title)
}
// SetWindowIcon sets the icon of the game window.
@ -114,9 +102,7 @@ func SetWindowTitle(title string) {
//
// SetWindowIcon is concurrent-safe.
func SetWindowIcon(iconImages []image.Image) {
if w := ui.Get().Window(); w != nil {
w.SetIcon(iconImages)
}
ui.Get().Window().SetIcon(iconImages)
}
// WindowPosition returns the window position.
@ -131,10 +117,7 @@ func SetWindowIcon(iconImages []image.Image) {
//
// WindowPosition is concurrent-safe.
func WindowPosition() (x, y int) {
if w := ui.Get().Window(); w != nil {
return w.Position()
}
return 0, 0
return ui.Get().Window().Position()
}
// SetWindowPosition sets the window position.
@ -148,9 +131,7 @@ func WindowPosition() (x, y int) {
// SetWindowPosition is concurrent-safe.
func SetWindowPosition(x, y int) {
atomic.StoreUint32(&windowPositionSetExplicitly, 1)
if w := ui.Get().Window(); w != nil {
w.SetPosition(x, y)
}
ui.Get().Window().SetPosition(x, y)
}
var (
@ -158,16 +139,11 @@ var (
)
func initializeWindowPositionIfNeeded(width, height int) {
w := ui.Get().Window()
if w == nil {
return
}
if atomic.LoadUint32(&windowPositionSetExplicitly) == 0 {
sw, sh := ui.Get().ScreenSizeInFullscreen()
x := (sw - width) / 2
y := (sh - height) / 3
w.SetPosition(x, y)
ui.Get().Window().SetPosition(x, y)
}
}
@ -178,10 +154,7 @@ func initializeWindowPositionIfNeeded(width, height int) {
//
// WindowSize is concurrent-safe.
func WindowSize() (int, int) {
if w := ui.Get().Window(); w != nil {
return w.Size()
}
return 0, 0
return ui.Get().Window().Size()
}
// SetWindowSize sets the window size on desktops.
@ -196,9 +169,7 @@ func SetWindowSize(width, height int) {
if width <= 0 || height <= 0 {
panic("ebiten: width and height must be positive")
}
if w := ui.Get().Window(); w != nil {
w.SetSize(width, height)
}
ui.Get().Window().SetSize(width, height)
}
// WindowSizeLimits returns the limitation of the window size on desktops.
@ -206,10 +177,7 @@ func SetWindowSize(width, height int) {
//
// WindowSizeLimits is concurrent-safe.
func WindowSizeLimits() (minw, minh, maxw, maxh int) {
if w := ui.Get().Window(); w != nil {
return w.SizeLimits()
}
return -1, -1, -1, -1
return ui.Get().Window().SizeLimits()
}
// SetWindowSizeLimits sets the limitation of the window size on desktops.
@ -217,9 +185,7 @@ func WindowSizeLimits() (minw, minh, maxw, maxh int) {
//
// SetWindowSizeLimits is concurrent-safe.
func SetWindowSizeLimits(minw, minh, maxw, maxh int) {
if w := ui.Get().Window(); w != nil {
w.SetSizeLimits(minw, minh, maxw, maxh)
}
ui.Get().Window().SetSizeLimits(minw, minh, maxw, maxh)
}
// IsWindowFloating reports whether the window is always shown above all the other windows.
@ -228,10 +194,7 @@ func SetWindowSizeLimits(minw, minh, maxw, maxh int) {
//
// IsWindowFloating is concurrent-safe.
func IsWindowFloating() bool {
if w := ui.Get().Window(); w != nil {
return w.IsFloating()
}
return false
return ui.Get().Window().IsFloating()
}
// SetWindowFloating sets the state whether the window is always shown above all the other windows.
@ -243,9 +206,7 @@ func IsWindowFloating() bool {
//
// SetWindowFloating is concurrent-safe.
func SetWindowFloating(float bool) {
if w := ui.Get().Window(); w != nil {
w.SetFloating(float)
}
ui.Get().Window().SetFloating(float)
}
// MaximizeWindow maximizes the window.
@ -259,9 +220,7 @@ func MaximizeWindow() {
if !IsWindowResizable() {
panic("ebiten: a window to maximize must be resizable")
}
if w := ui.Get().Window(); w != nil {
w.Maximize()
}
ui.Get().Window().Maximize()
}
// IsWindowMaximized reports whether the window is maximized or not.
@ -275,10 +234,7 @@ func IsWindowMaximized() bool {
if !IsWindowResizable() {
return false
}
if w := ui.Get().Window(); w != nil {
return w.IsMaximized()
}
return false
return ui.Get().Window().IsMaximized()
}
// MinimizeWindow minimizes the window.
@ -289,9 +245,7 @@ func IsWindowMaximized() bool {
//
// MinimizeWindow is concurrent-safe.
func MinimizeWindow() {
if w := ui.Get().Window(); w != nil {
w.Minimize()
}
ui.Get().Window().Minimize()
}
// IsWindowMinimized reports whether the window is minimized or not.
@ -300,10 +254,7 @@ func MinimizeWindow() {
//
// IsWindowMinimized is concurrent-safe.
func IsWindowMinimized() bool {
if w := ui.Get().Window(); w != nil {
return w.IsMinimized()
}
return false
return ui.Get().Window().IsMinimized()
}
// RestoreWindow restores the window from its maximized or minimized state.
@ -315,9 +266,7 @@ func RestoreWindow() {
if !IsWindowMaximized() && !IsWindowMinimized() {
panic("ebiten: RestoreWindow must be called on a maximized or a minimized window")
}
if w := ui.Get().Window(); w != nil {
w.Restore()
}
ui.Get().Window().Restore()
}
// IsWindowBeingClosed returns true when the user is trying to close the window on desktops.
@ -328,10 +277,7 @@ func RestoreWindow() {
//
// IsWindowBeingClosed is concurrent-safe.
func IsWindowBeingClosed() bool {
if w := ui.Get().Window(); w != nil {
return w.IsBeingClosed()
}
return false
return ui.Get().Window().IsBeingClosed()
}
// SetWindowClosingHandled sets whether the window closing is handled or not on desktops. The default state is false.
@ -346,9 +292,7 @@ func IsWindowBeingClosed() bool {
//
// SetWindowClosingHandled is concurrent-safe.
func SetWindowClosingHandled(handled bool) {
if w := ui.Get().Window(); w != nil {
w.SetClosingHandled(handled)
}
ui.Get().Window().SetClosingHandled(handled)
}
// IsWindowClosingHandled reports whether the window closing is handled or not on desktops by SetWindowClosingHandled.
@ -357,8 +301,5 @@ func SetWindowClosingHandled(handled bool) {
//
// IsWindowClosingHandled is concurrent-safe.
func IsWindowClosingHandled() bool {
if w := ui.Get().Window(); w != nil {
return w.IsClosingHandled()
}
return false
return ui.Get().Window().IsClosingHandled()
}