2019-12-24 16:05:56 +01:00
|
|
|
// 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.
|
|
|
|
|
2022-02-08 07:42:29 +01:00
|
|
|
//go:build !android && !ios && !js && !ebitencbackend
|
|
|
|
// +build !android,!ios,!js,!ebitencbackend
|
2019-12-24 16:05:56 +01:00
|
|
|
|
2022-02-06 08:08:22 +01:00
|
|
|
package ui
|
2019-12-24 16:05:56 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"image"
|
|
|
|
|
2020-10-03 19:35:13 +02:00
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/glfw"
|
2019-12-24 16:05:56 +01:00
|
|
|
)
|
|
|
|
|
2022-02-06 12:02:03 +01:00
|
|
|
type Window struct {
|
2022-03-21 15:00:50 +01:00
|
|
|
ui *userInterfaceImpl
|
2019-12-24 16:05:56 +01:00
|
|
|
}
|
|
|
|
|
2022-02-06 12:02:03 +01:00
|
|
|
func (w *Window) IsDecorated() bool {
|
2019-12-24 16:05:56 +01:00
|
|
|
if !w.ui.isRunning() {
|
|
|
|
return w.ui.isInitWindowDecorated()
|
|
|
|
}
|
|
|
|
v := false
|
2022-01-02 19:30:29 +01:00
|
|
|
w.ui.t.Call(func() {
|
2019-12-24 16:05:56 +01:00
|
|
|
v = w.ui.window.GetAttrib(glfw.Decorated) == glfw.True
|
|
|
|
})
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
|
2022-02-06 12:02:03 +01:00
|
|
|
func (w *Window) SetDecorated(decorated bool) {
|
2019-12-24 16:05:56 +01:00
|
|
|
if !w.ui.isRunning() {
|
|
|
|
w.ui.setInitWindowDecorated(decorated)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-01-02 19:30:29 +01:00
|
|
|
w.ui.t.Call(func() {
|
2021-04-18 10:35:46 +02:00
|
|
|
if w.ui.isNativeFullscreen() {
|
2022-01-02 19:30:29 +01:00
|
|
|
return
|
2021-04-18 10:35:46 +02:00
|
|
|
}
|
|
|
|
|
2021-04-20 11:16:41 +02:00
|
|
|
w.ui.setWindowDecorated(decorated)
|
2019-12-24 16:05:56 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-12-29 14:08:59 +01:00
|
|
|
func (w *Window) ResizingMode() WindowResizingMode {
|
2019-12-24 16:05:56 +01:00
|
|
|
if !w.ui.isRunning() {
|
2021-12-29 14:08:59 +01:00
|
|
|
w.ui.m.Lock()
|
|
|
|
mode := w.ui.windowResizingMode
|
|
|
|
w.ui.m.Unlock()
|
|
|
|
return mode
|
2019-12-24 16:05:56 +01:00
|
|
|
}
|
2021-12-29 14:08:59 +01:00
|
|
|
var mode WindowResizingMode
|
2022-01-02 19:30:29 +01:00
|
|
|
w.ui.t.Call(func() {
|
2021-12-29 14:08:59 +01:00
|
|
|
mode = w.ui.windowResizingMode
|
2019-12-24 16:05:56 +01:00
|
|
|
})
|
2021-12-29 14:08:59 +01:00
|
|
|
return mode
|
2019-12-24 16:05:56 +01:00
|
|
|
}
|
|
|
|
|
2021-12-29 14:08:59 +01:00
|
|
|
func (w *Window) SetResizingMode(mode WindowResizingMode) {
|
2019-12-24 16:05:56 +01:00
|
|
|
if !w.ui.isRunning() {
|
2021-12-29 14:08:59 +01:00
|
|
|
w.ui.m.Lock()
|
|
|
|
w.ui.windowResizingMode = mode
|
|
|
|
w.ui.m.Unlock()
|
2019-12-24 16:05:56 +01:00
|
|
|
return
|
|
|
|
}
|
2022-01-02 19:30:29 +01:00
|
|
|
w.ui.t.Call(func() {
|
2021-04-18 10:35:46 +02:00
|
|
|
if w.ui.isNativeFullscreen() {
|
2022-01-02 19:30:29 +01:00
|
|
|
return
|
2021-04-18 10:35:46 +02:00
|
|
|
}
|
2021-12-29 14:08:59 +01:00
|
|
|
w.ui.setWindowResizingMode(mode)
|
2019-12-24 16:05:56 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-02-06 12:02:03 +01:00
|
|
|
func (w *Window) IsFloating() bool {
|
2020-03-20 13:46:23 +01:00
|
|
|
if !w.ui.isRunning() {
|
|
|
|
return w.ui.isInitWindowFloating()
|
|
|
|
}
|
|
|
|
var v bool
|
2022-01-02 19:30:29 +01:00
|
|
|
w.ui.t.Call(func() {
|
2020-03-20 13:46:23 +01:00
|
|
|
v = w.ui.window.GetAttrib(glfw.Floating) == glfw.True
|
|
|
|
})
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
|
2022-02-06 12:02:03 +01:00
|
|
|
func (w *Window) SetFloating(floating bool) {
|
2020-03-20 13:46:23 +01:00
|
|
|
if !w.ui.isRunning() {
|
|
|
|
w.ui.setInitWindowFloating(floating)
|
|
|
|
return
|
|
|
|
}
|
2022-01-02 19:30:29 +01:00
|
|
|
w.ui.t.Call(func() {
|
2021-04-18 10:35:46 +02:00
|
|
|
if w.ui.isNativeFullscreen() {
|
2022-01-02 19:30:29 +01:00
|
|
|
return
|
2021-04-18 10:35:46 +02:00
|
|
|
}
|
2021-04-20 11:16:41 +02:00
|
|
|
w.ui.setWindowFloating(floating)
|
2020-03-20 13:46:23 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-02-06 12:02:03 +01:00
|
|
|
func (w *Window) IsMaximized() bool {
|
2020-03-21 11:26:28 +01:00
|
|
|
if !w.ui.isRunning() {
|
|
|
|
return w.ui.isInitWindowMaximized()
|
|
|
|
}
|
2021-12-29 14:08:59 +01:00
|
|
|
if w.ResizingMode() != WindowResizingModeEnabled {
|
2022-02-10 11:31:38 +01:00
|
|
|
return false
|
|
|
|
}
|
2020-03-21 11:26:28 +01:00
|
|
|
var v bool
|
2022-01-02 19:30:29 +01:00
|
|
|
w.ui.t.Call(func() {
|
2020-03-21 11:26:28 +01:00
|
|
|
v = w.ui.window.GetAttrib(glfw.Maximized) == glfw.True
|
|
|
|
})
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
|
2022-02-06 12:02:03 +01:00
|
|
|
func (w *Window) Maximize() {
|
2022-02-10 11:31:38 +01:00
|
|
|
// Do not allow maximizing the window when the window is not resizable.
|
|
|
|
// On Windows, it is possible to restore the window from being maximized by mouse-dragging,
|
|
|
|
// and this can be an unexpected behavior.
|
2021-12-29 14:08:59 +01:00
|
|
|
if w.ResizingMode() != WindowResizingModeEnabled {
|
2022-02-06 08:08:22 +01:00
|
|
|
panic("ui: a window to maximize must be resizable")
|
2020-03-28 14:00:40 +01:00
|
|
|
}
|
2020-03-21 11:26:28 +01:00
|
|
|
if !w.ui.isRunning() {
|
|
|
|
w.ui.setInitWindowMaximized(true)
|
|
|
|
return
|
|
|
|
}
|
2022-01-02 19:30:29 +01:00
|
|
|
w.ui.t.Call(w.ui.maximizeWindow)
|
2020-03-21 11:26:28 +01:00
|
|
|
}
|
|
|
|
|
2022-02-06 12:02:03 +01:00
|
|
|
func (w *Window) IsMinimized() bool {
|
2020-03-21 11:26:28 +01:00
|
|
|
if !w.ui.isRunning() {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
var v bool
|
2022-01-02 19:30:29 +01:00
|
|
|
w.ui.t.Call(func() {
|
2020-03-21 11:26:28 +01:00
|
|
|
v = w.ui.window.GetAttrib(glfw.Iconified) == glfw.True
|
|
|
|
})
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
|
2022-02-06 12:02:03 +01:00
|
|
|
func (w *Window) Minimize() {
|
2020-03-21 11:26:28 +01:00
|
|
|
if !w.ui.isRunning() {
|
|
|
|
// Do nothing
|
|
|
|
return
|
|
|
|
}
|
2022-01-02 19:30:29 +01:00
|
|
|
w.ui.t.Call(w.ui.iconifyWindow)
|
2020-03-21 11:26:28 +01:00
|
|
|
}
|
|
|
|
|
2022-02-06 12:02:03 +01:00
|
|
|
func (w *Window) Restore() {
|
2020-03-21 11:26:28 +01:00
|
|
|
if !w.ui.isRunning() {
|
|
|
|
// Do nothing
|
|
|
|
return
|
|
|
|
}
|
2022-01-02 19:30:29 +01:00
|
|
|
w.ui.t.Call(w.ui.restoreWindow)
|
2020-03-21 11:26:28 +01:00
|
|
|
}
|
|
|
|
|
2022-02-06 12:02:03 +01:00
|
|
|
func (w *Window) Position() (int, int) {
|
2019-12-24 16:05:56 +01:00
|
|
|
if !w.ui.isRunning() {
|
2022-02-06 08:08:22 +01:00
|
|
|
panic("ui: WindowPosition can't be called before the main loop starts")
|
2019-12-24 16:05:56 +01:00
|
|
|
}
|
|
|
|
x, y := 0, 0
|
2022-01-02 19:30:29 +01:00
|
|
|
w.ui.t.Call(func() {
|
2019-12-24 16:05:56 +01:00
|
|
|
var wx, wy int
|
2022-03-29 21:15:52 +02:00
|
|
|
if w.ui.isFullscreen() {
|
2022-04-08 08:59:16 +02:00
|
|
|
wx, wy = w.ui.origWindowPos()
|
2019-12-24 16:05:56 +01:00
|
|
|
} else {
|
|
|
|
wx, wy = w.ui.window.GetPos()
|
|
|
|
}
|
2021-10-09 09:49:47 +02:00
|
|
|
m := w.ui.currentMonitor()
|
|
|
|
mx, my := m.GetPos()
|
2020-03-28 11:51:44 +01:00
|
|
|
wx -= mx
|
|
|
|
wy -= my
|
2021-10-10 08:38:13 +02:00
|
|
|
xf := w.ui.dipFromGLFWPixel(float64(wx), m)
|
|
|
|
yf := w.ui.dipFromGLFWPixel(float64(wy), m)
|
2019-12-24 16:05:56 +01:00
|
|
|
x, y = int(xf), int(yf)
|
|
|
|
})
|
|
|
|
return x, y
|
|
|
|
}
|
|
|
|
|
2022-02-06 12:02:03 +01:00
|
|
|
func (w *Window) SetPosition(x, y int) {
|
2019-12-24 16:05:56 +01:00
|
|
|
if !w.ui.isRunning() {
|
2021-10-10 08:38:13 +02:00
|
|
|
w.ui.setInitWindowPositionInDIP(x, y)
|
2019-12-24 16:05:56 +01:00
|
|
|
return
|
|
|
|
}
|
2022-01-02 19:30:29 +01:00
|
|
|
w.ui.t.Call(func() {
|
2021-10-10 08:50:12 +02:00
|
|
|
w.ui.setWindowPositionInDIP(x, y, w.ui.currentMonitor())
|
2019-12-24 16:05:56 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-02-06 12:02:03 +01:00
|
|
|
func (w *Window) Size() (int, int) {
|
2019-12-24 16:05:56 +01:00
|
|
|
if !w.ui.isRunning() {
|
2021-10-10 08:38:13 +02:00
|
|
|
ww, wh := w.ui.getInitWindowSizeInDIP()
|
|
|
|
return w.ui.adjustWindowSizeBasedOnSizeLimitsInDIP(ww, wh)
|
2019-12-24 16:05:56 +01:00
|
|
|
}
|
2021-04-20 12:35:52 +02:00
|
|
|
ww, wh := 0, 0
|
2022-01-02 19:30:29 +01:00
|
|
|
w.ui.t.Call(func() {
|
2022-04-08 11:40:10 +02:00
|
|
|
// Unlike origWindowPos, windowWidth/HeightInDPI is always updated via the callback.
|
2021-10-10 08:38:13 +02:00
|
|
|
ww = w.ui.windowWidthInDIP
|
|
|
|
wh = w.ui.windowHeightInDIP
|
2021-04-20 12:35:52 +02:00
|
|
|
})
|
2019-12-24 16:05:56 +01:00
|
|
|
return ww, wh
|
|
|
|
}
|
|
|
|
|
2022-02-06 12:02:03 +01:00
|
|
|
func (w *Window) SetSize(width, height int) {
|
2019-12-24 16:05:56 +01:00
|
|
|
if !w.ui.isRunning() {
|
2021-10-10 08:38:13 +02:00
|
|
|
w.ui.setInitWindowSizeInDIP(width, height)
|
2019-12-24 16:05:56 +01:00
|
|
|
return
|
|
|
|
}
|
2022-01-02 19:30:29 +01:00
|
|
|
w.ui.t.Call(func() {
|
2021-09-17 19:21:24 +02:00
|
|
|
// When a window is a native fullscreen, forcing to resize the window might leave unexpected image lags.
|
|
|
|
// Forbid this.
|
2022-04-08 11:40:10 +02:00
|
|
|
// TODO: Remove this condition (#1590).
|
2021-09-17 19:21:24 +02:00
|
|
|
if w.ui.isNativeFullscreen() {
|
2022-01-02 19:30:29 +01:00
|
|
|
return
|
2021-09-17 19:21:24 +02:00
|
|
|
}
|
|
|
|
|
2021-10-10 08:38:13 +02:00
|
|
|
w.ui.setWindowSizeInDIP(width, height, w.ui.isFullscreen())
|
2020-10-16 21:21:07 +02:00
|
|
|
})
|
2019-12-24 16:05:56 +01:00
|
|
|
}
|
|
|
|
|
2022-02-06 12:02:03 +01:00
|
|
|
func (w *Window) SizeLimits() (minw, minh, maxw, maxh int) {
|
2021-10-10 08:38:13 +02:00
|
|
|
return w.ui.getWindowSizeLimitsInDIP()
|
2021-04-16 19:27:04 +02:00
|
|
|
}
|
|
|
|
|
2022-02-06 12:02:03 +01:00
|
|
|
func (w *Window) SetSizeLimits(minw, minh, maxw, maxh int) {
|
2021-10-10 08:38:13 +02:00
|
|
|
if !w.ui.setWindowSizeLimitsInDIP(minw, minh, maxw, maxh) {
|
2021-04-16 19:27:04 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if !w.ui.isRunning() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-01-02 19:30:29 +01:00
|
|
|
w.ui.t.Call(w.ui.updateWindowSizeLimits)
|
2021-04-16 19:27:04 +02:00
|
|
|
}
|
|
|
|
|
2022-02-06 12:02:03 +01:00
|
|
|
func (w *Window) SetIcon(iconImages []image.Image) {
|
2021-02-07 13:16:09 +01:00
|
|
|
// The icons are actually set at (*UserInterface).loop.
|
|
|
|
w.ui.setIconImages(iconImages)
|
2019-12-24 16:05:56 +01:00
|
|
|
}
|
|
|
|
|
2022-02-06 12:02:03 +01:00
|
|
|
func (w *Window) SetTitle(title string) {
|
2019-12-24 16:05:56 +01:00
|
|
|
if !w.ui.isRunning() {
|
2021-12-29 14:26:56 +01:00
|
|
|
w.ui.m.Lock()
|
|
|
|
w.ui.title = title
|
|
|
|
w.ui.m.Unlock()
|
2019-12-24 16:05:56 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
w.ui.title = title
|
2022-01-02 19:30:29 +01:00
|
|
|
w.ui.t.Call(func() {
|
2021-04-20 11:16:41 +02:00
|
|
|
w.ui.setWindowTitle(title)
|
2019-12-24 16:05:56 +01:00
|
|
|
})
|
|
|
|
}
|
2021-06-13 13:52:14 +02:00
|
|
|
|
2022-02-06 12:02:03 +01:00
|
|
|
func (w *Window) IsBeingClosed() bool {
|
2021-06-13 13:52:14 +02:00
|
|
|
return w.ui.isWindowBeingClosed()
|
|
|
|
}
|
|
|
|
|
2022-02-06 12:02:03 +01:00
|
|
|
func (w *Window) SetClosingHandled(handled bool) {
|
2021-06-13 13:52:14 +02:00
|
|
|
w.ui.setWindowClosingHandled(handled)
|
|
|
|
}
|
|
|
|
|
2022-02-06 12:02:03 +01:00
|
|
|
func (w *Window) IsClosingHandled() bool {
|
2021-06-13 13:52:14 +02:00
|
|
|
return w.ui.isWindowClosingHandled()
|
|
|
|
}
|