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.
|
|
|
|
|
|
|
|
// +build darwin freebsd linux windows
|
|
|
|
// +build !android
|
|
|
|
// +build !ios
|
|
|
|
|
|
|
|
package glfw
|
|
|
|
|
|
|
|
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 window struct {
|
2021-04-17 10:03:46 +02:00
|
|
|
ui *UserInterface
|
2019-12-24 16:05:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (w *window) IsDecorated() bool {
|
|
|
|
if !w.ui.isRunning() {
|
|
|
|
return w.ui.isInitWindowDecorated()
|
|
|
|
}
|
|
|
|
v := false
|
|
|
|
_ = w.ui.t.Call(func() error {
|
|
|
|
v = w.ui.window.GetAttrib(glfw.Decorated) == glfw.True
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *window) SetDecorated(decorated bool) {
|
|
|
|
if !w.ui.isRunning() {
|
|
|
|
w.ui.setInitWindowDecorated(decorated)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
_ = w.ui.t.Call(func() error {
|
2021-04-18 10:35:46 +02:00
|
|
|
if w.ui.isNativeFullscreen() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-12-24 16:05:56 +01:00
|
|
|
v := glfw.False
|
|
|
|
if decorated {
|
|
|
|
v = glfw.True
|
|
|
|
}
|
|
|
|
w.ui.window.SetAttrib(glfw.Decorated, v)
|
|
|
|
|
|
|
|
// The title can be lost when the decoration is gone. Recover this.
|
|
|
|
if v == glfw.True {
|
|
|
|
w.ui.window.SetTitle(w.ui.title)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *window) IsResizable() bool {
|
|
|
|
if !w.ui.isRunning() {
|
|
|
|
return w.ui.isInitWindowResizable()
|
|
|
|
}
|
|
|
|
v := false
|
|
|
|
_ = w.ui.t.Call(func() error {
|
|
|
|
v = w.ui.window.GetAttrib(glfw.Resizable) == glfw.True
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *window) SetResizable(resizable bool) {
|
|
|
|
if !w.ui.isRunning() {
|
|
|
|
w.ui.setInitWindowResizable(resizable)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
_ = w.ui.t.Call(func() error {
|
2021-04-18 10:35:46 +02:00
|
|
|
if w.ui.isNativeFullscreen() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-12-24 16:05:56 +01:00
|
|
|
v := glfw.False
|
|
|
|
if resizable {
|
|
|
|
v = glfw.True
|
|
|
|
}
|
|
|
|
w.ui.window.SetAttrib(glfw.Resizable, v)
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-03-20 13:46:23 +01:00
|
|
|
func (w *window) IsFloating() bool {
|
|
|
|
if !w.ui.isRunning() {
|
|
|
|
return w.ui.isInitWindowFloating()
|
|
|
|
}
|
|
|
|
var v bool
|
|
|
|
_ = w.ui.t.Call(func() error {
|
|
|
|
v = w.ui.window.GetAttrib(glfw.Floating) == glfw.True
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *window) SetFloating(floating bool) {
|
|
|
|
if !w.ui.isRunning() {
|
|
|
|
w.ui.setInitWindowFloating(floating)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
_ = w.ui.t.Call(func() error {
|
2021-04-18 10:35:46 +02:00
|
|
|
if w.ui.isNativeFullscreen() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-03-20 13:46:23 +01:00
|
|
|
v := glfw.False
|
|
|
|
if floating {
|
|
|
|
v = glfw.True
|
|
|
|
}
|
|
|
|
w.ui.window.SetAttrib(glfw.Floating, v)
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-03-21 11:26:28 +01:00
|
|
|
func (w *window) IsMaximized() bool {
|
|
|
|
if !w.ui.isRunning() {
|
|
|
|
return w.ui.isInitWindowMaximized()
|
|
|
|
}
|
|
|
|
var v bool
|
|
|
|
_ = w.ui.t.Call(func() error {
|
|
|
|
v = w.ui.window.GetAttrib(glfw.Maximized) == glfw.True
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *window) Maximize() {
|
2020-03-28 14:00:40 +01:00
|
|
|
if !w.IsResizable() {
|
|
|
|
panic("glfw: a window to maximize must be resizable")
|
|
|
|
}
|
2020-03-21 11:26:28 +01:00
|
|
|
if !w.ui.isRunning() {
|
|
|
|
w.ui.setInitWindowMaximized(true)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
_ = w.ui.t.Call(func() error {
|
2021-04-17 16:57:17 +02:00
|
|
|
w.ui.maximize()
|
2020-03-21 11:26:28 +01:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *window) IsMinimized() bool {
|
|
|
|
if !w.ui.isRunning() {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
var v bool
|
|
|
|
_ = w.ui.t.Call(func() error {
|
|
|
|
v = w.ui.window.GetAttrib(glfw.Iconified) == glfw.True
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *window) Minimize() {
|
|
|
|
if !w.ui.isRunning() {
|
|
|
|
// Do nothing
|
|
|
|
return
|
|
|
|
}
|
|
|
|
_ = w.ui.t.Call(func() error {
|
2021-04-17 18:03:06 +02:00
|
|
|
w.ui.iconify()
|
2020-03-21 11:26:28 +01:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *window) Restore() {
|
|
|
|
if !w.ui.isRunning() {
|
|
|
|
// Do nothing
|
|
|
|
return
|
|
|
|
}
|
|
|
|
_ = w.ui.t.Call(func() error {
|
2021-04-17 17:10:37 +02:00
|
|
|
w.ui.restore()
|
2020-03-21 11:26:28 +01:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-12-24 16:05:56 +01:00
|
|
|
func (w *window) Position() (int, int) {
|
|
|
|
if !w.ui.isRunning() {
|
|
|
|
panic("glfw: WindowPosition can't be called before the main loop starts")
|
|
|
|
}
|
|
|
|
x, y := 0, 0
|
|
|
|
_ = w.ui.t.Call(func() error {
|
|
|
|
var wx, wy int
|
|
|
|
if w.ui.isFullscreen() {
|
|
|
|
wx, wy = w.ui.origPosX, w.ui.origPosY
|
|
|
|
} else {
|
|
|
|
wx, wy = w.ui.window.GetPos()
|
|
|
|
}
|
2020-08-23 20:27:38 +02:00
|
|
|
mx, my := currentMonitor(w.ui.window).GetPos()
|
2020-03-28 11:51:44 +01:00
|
|
|
wx -= mx
|
|
|
|
wy -= my
|
2020-09-18 17:31:34 +02:00
|
|
|
xf := w.ui.fromGLFWPixel(float64(wx))
|
|
|
|
yf := w.ui.fromGLFWPixel(float64(wy))
|
2019-12-24 16:05:56 +01:00
|
|
|
x, y = int(xf), int(yf)
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
return x, y
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *window) SetPosition(x, y int) {
|
|
|
|
if !w.ui.isRunning() {
|
|
|
|
w.ui.setInitWindowPosition(x, y)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
_ = w.ui.t.Call(func() error {
|
2020-10-16 21:21:07 +02:00
|
|
|
w.setPosition(x, y)
|
2019-12-24 16:05:56 +01:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-10-16 21:21:07 +02:00
|
|
|
// setPosition must be called from the main thread
|
|
|
|
func (w *window) setPosition(x, y int) {
|
|
|
|
mx, my := currentMonitor(w.ui.window).GetPos()
|
|
|
|
xf := w.ui.toGLFWPixel(float64(x))
|
|
|
|
yf := w.ui.toGLFWPixel(float64(y))
|
|
|
|
if x, y := w.ui.adjustWindowPosition(mx+int(xf), my+int(yf)); w.ui.isFullscreen() {
|
|
|
|
w.ui.origPosX, w.ui.origPosY = x, y
|
|
|
|
} else {
|
|
|
|
w.ui.window.SetPos(x, y)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-24 16:05:56 +01:00
|
|
|
func (w *window) Size() (int, int) {
|
|
|
|
if !w.ui.isRunning() {
|
2021-04-16 19:27:04 +02:00
|
|
|
ww, wh := w.ui.getInitWindowSize()
|
2021-04-16 21:43:20 +02:00
|
|
|
return w.ui.adjustWindowSizeBasedOnSizeLimitsInDP(ww, wh)
|
2019-12-24 16:05:56 +01:00
|
|
|
}
|
2020-09-18 17:31:34 +02:00
|
|
|
ww := int(w.ui.fromGLFWPixel(float64(w.ui.windowWidth)))
|
|
|
|
wh := int(w.ui.fromGLFWPixel(float64(w.ui.windowHeight)))
|
2019-12-24 16:05:56 +01:00
|
|
|
return ww, wh
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *window) SetSize(width, height int) {
|
|
|
|
if !w.ui.isRunning() {
|
|
|
|
w.ui.setInitWindowSize(width, height)
|
|
|
|
return
|
|
|
|
}
|
2020-09-18 17:21:08 +02:00
|
|
|
ww := int(w.ui.toGLFWPixel(float64(width)))
|
|
|
|
wh := int(w.ui.toGLFWPixel(float64(height)))
|
2020-10-16 21:21:07 +02:00
|
|
|
_ = w.ui.t.Call(func() error {
|
|
|
|
w.ui.setWindowSize(ww, wh, w.ui.isFullscreen())
|
|
|
|
return nil
|
|
|
|
})
|
2019-12-24 16:05:56 +01:00
|
|
|
}
|
|
|
|
|
2021-04-16 19:27:04 +02:00
|
|
|
func (w *window) SizeLimits() (minw, minh, maxw, maxh int) {
|
2021-04-17 11:17:24 +02:00
|
|
|
return w.ui.getWindowSizeLimitsInDP()
|
2021-04-16 19:27:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (w *window) SetSizeLimits(minw, minh, maxw, maxh int) {
|
2021-04-16 21:43:20 +02:00
|
|
|
if !w.ui.setWindowSizeLimitsInDP(minw, minh, maxw, maxh) {
|
2021-04-16 19:27:04 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if !w.ui.isRunning() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
_ = w.ui.t.Call(func() error {
|
|
|
|
w.ui.updateWindowSizeLimits()
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-12-24 16:05:56 +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
|
|
|
}
|
|
|
|
|
|
|
|
func (w *window) SetTitle(title string) {
|
|
|
|
if !w.ui.isRunning() {
|
|
|
|
w.ui.setInitTitle(title)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
w.ui.title = title
|
|
|
|
_ = w.ui.t.Call(func() error {
|
|
|
|
w.ui.window.SetTitle(title)
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|