ebiten/internal/ui/window_glfw.go

272 lines
5.9 KiB
Go
Raw Normal View History

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.
//go:build !android && !ios && !js && !ebitenginecbackend && !ebitencbackend
// +build !android,!ios,!js,!ebitenginecbackend,!ebitencbackend
2019-12-24 16:05:56 +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
)
type glfwWindow struct {
ui *userInterfaceImpl
2019-12-24 16:05:56 +01:00
}
func (w *glfwWindow) IsDecorated() bool {
2019-12-24 16:05:56 +01:00
if !w.ui.isRunning() {
return w.ui.isInitWindowDecorated()
}
v := false
w.ui.t.Call(func() {
2019-12-24 16:05:56 +01:00
v = w.ui.window.GetAttrib(glfw.Decorated) == glfw.True
})
return v
}
func (w *glfwWindow) SetDecorated(decorated bool) {
2019-12-24 16:05:56 +01:00
if !w.ui.isRunning() {
w.ui.setInitWindowDecorated(decorated)
return
}
w.ui.t.Call(func() {
if w.ui.isNativeFullscreen() {
return
}
w.ui.setWindowDecorated(decorated)
2019-12-24 16:05:56 +01:00
})
}
func (w *glfwWindow) ResizingMode() WindowResizingMode {
2019-12-24 16:05:56 +01:00
if !w.ui.isRunning() {
w.ui.m.Lock()
mode := w.ui.windowResizingMode
w.ui.m.Unlock()
return mode
2019-12-24 16:05:56 +01:00
}
var mode WindowResizingMode
w.ui.t.Call(func() {
mode = w.ui.windowResizingMode
2019-12-24 16:05:56 +01:00
})
return mode
2019-12-24 16:05:56 +01:00
}
func (w *glfwWindow) SetResizingMode(mode WindowResizingMode) {
2019-12-24 16:05:56 +01:00
if !w.ui.isRunning() {
w.ui.m.Lock()
w.ui.windowResizingMode = mode
w.ui.m.Unlock()
2019-12-24 16:05:56 +01:00
return
}
w.ui.t.Call(func() {
if w.ui.isNativeFullscreen() {
return
}
w.ui.setWindowResizingMode(mode)
2019-12-24 16:05:56 +01:00
})
}
func (w *glfwWindow) IsFloating() bool {
if !w.ui.isRunning() {
return w.ui.isInitWindowFloating()
}
var v bool
w.ui.t.Call(func() {
v = w.ui.window.GetAttrib(glfw.Floating) == glfw.True
})
return v
}
func (w *glfwWindow) SetFloating(floating bool) {
if !w.ui.isRunning() {
w.ui.setInitWindowFloating(floating)
return
}
w.ui.t.Call(func() {
if w.ui.isNativeFullscreen() {
return
}
w.ui.setWindowFloating(floating)
})
}
func (w *glfwWindow) IsMaximized() bool {
if !w.ui.isRunning() {
return w.ui.isInitWindowMaximized()
}
if w.ResizingMode() != WindowResizingModeEnabled {
2022-02-10 11:31:38 +01:00
return false
}
var v bool
w.ui.t.Call(func() {
v = w.ui.window.GetAttrib(glfw.Maximized) == glfw.True
})
return v
}
func (w *glfwWindow) 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.
if w.ResizingMode() != WindowResizingModeEnabled {
panic("ui: a window to maximize must be resizable")
}
if !w.ui.isRunning() {
w.ui.setInitWindowMaximized(true)
return
}
w.ui.t.Call(w.ui.maximizeWindow)
}
func (w *glfwWindow) IsMinimized() bool {
if !w.ui.isRunning() {
return false
}
var v bool
w.ui.t.Call(func() {
v = w.ui.window.GetAttrib(glfw.Iconified) == glfw.True
})
return v
}
func (w *glfwWindow) Minimize() {
if !w.ui.isRunning() {
// Do nothing
return
}
w.ui.t.Call(w.ui.iconifyWindow)
}
func (w *glfwWindow) Restore() {
if !w.ui.isRunning() {
// Do nothing
return
}
w.ui.t.Call(w.ui.restoreWindow)
}
func (w *glfwWindow) Position() (int, int) {
2019-12-24 16:05:56 +01:00
if !w.ui.isRunning() {
panic("ui: WindowPosition can't be called before the main loop starts")
2019-12-24 16:05:56 +01:00
}
x, y := 0, 0
w.ui.t.Call(func() {
2019-12-24 16:05:56 +01:00
var wx, wy int
if w.ui.isFullscreen() {
wx, wy = w.ui.origWindowPos()
2019-12-24 16:05:56 +01:00
} else {
wx, wy = w.ui.window.GetPos()
}
m := w.ui.currentMonitor()
mx, my := m.GetPos()
wx -= mx
wy -= my
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
}
func (w *glfwWindow) SetPosition(x, y int) {
2019-12-24 16:05:56 +01:00
if !w.ui.isRunning() {
w.ui.setInitWindowPositionInDIP(x, y)
2019-12-24 16:05:56 +01:00
return
}
w.ui.t.Call(func() {
w.ui.setWindowPositionInDIP(x, y, w.ui.currentMonitor())
2019-12-24 16:05:56 +01:00
})
}
func (w *glfwWindow) Size() (int, int) {
2019-12-24 16:05:56 +01:00
if !w.ui.isRunning() {
ww, wh := w.ui.getInitWindowSizeInDIP()
return w.ui.adjustWindowSizeBasedOnSizeLimitsInDIP(ww, wh)
2019-12-24 16:05:56 +01:00
}
ww, wh := 0, 0
w.ui.t.Call(func() {
// Unlike origWindowPos, windowWidth/HeightInDPI is always updated via the callback.
ww = w.ui.windowWidthInDIP
wh = w.ui.windowHeightInDIP
})
2019-12-24 16:05:56 +01:00
return ww, wh
}
func (w *glfwWindow) SetSize(width, height int) {
2019-12-24 16:05:56 +01:00
if !w.ui.isRunning() {
w.ui.setInitWindowSizeInDIP(width, height)
2019-12-24 16:05:56 +01:00
return
}
w.ui.t.Call(func() {
// When a window is a native fullscreen, forcing to resize the window might leave unexpected image lags.
// Forbid this.
// TODO: Remove this condition (#1590).
if w.ui.isNativeFullscreen() {
return
}
w.ui.setWindowSizeInDIP(width, height, w.ui.isFullscreen())
2020-10-16 21:21:07 +02:00
})
2019-12-24 16:05:56 +01:00
}
func (w *glfwWindow) SizeLimits() (minw, minh, maxw, maxh int) {
return w.ui.getWindowSizeLimitsInDIP()
}
func (w *glfwWindow) SetSizeLimits(minw, minh, maxw, maxh int) {
if !w.ui.setWindowSizeLimitsInDIP(minw, minh, maxw, maxh) {
return
}
if !w.ui.isRunning() {
return
}
w.ui.t.Call(w.ui.updateWindowSizeLimits)
}
func (w *glfwWindow) SetIcon(iconImages []image.Image) {
// The icons are actually set at (*UserInterface).loop.
w.ui.setIconImages(iconImages)
2019-12-24 16:05:56 +01:00
}
func (w *glfwWindow) SetTitle(title string) {
2019-12-24 16:05:56 +01:00
if !w.ui.isRunning() {
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
w.ui.t.Call(func() {
w.ui.setWindowTitle(title)
2019-12-24 16:05:56 +01:00
})
}
func (w *glfwWindow) IsBeingClosed() bool {
return w.ui.isWindowBeingClosed()
}
func (w *glfwWindow) SetClosingHandled(handled bool) {
w.ui.setWindowClosingHandled(handled)
}
func (w *glfwWindow) IsClosingHandled() bool {
return w.ui.isWindowClosingHandled()
}