2016-06-18 22:04:38 +02:00
|
|
|
// Copyright 2016 Hajime Hoshi
|
|
|
|
//
|
|
|
|
// 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-06 08:08:22 +01:00
|
|
|
//go:build !ebitencbackend
|
|
|
|
// +build !ebitencbackend
|
|
|
|
|
|
|
|
package ui
|
2016-06-18 22:04:38 +02:00
|
|
|
|
2018-10-06 15:42:28 +02:00
|
|
|
import (
|
2022-07-04 05:55:34 +02:00
|
|
|
"errors"
|
2018-10-06 15:42:28 +02:00
|
|
|
"fmt"
|
2021-09-17 19:21:24 +02:00
|
|
|
"runtime"
|
2018-10-06 15:42:28 +02:00
|
|
|
"unsafe"
|
2016-09-11 15:34:39 +02:00
|
|
|
|
2019-01-03 21:28:27 +01:00
|
|
|
"golang.org/x/sys/windows"
|
|
|
|
|
2020-10-03 19:35:13 +02:00
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/glfw"
|
2022-03-22 15:33:21 +01:00
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver"
|
2022-02-11 12:38:45 +01:00
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver/directx"
|
2022-03-22 15:33:21 +01:00
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver/opengl"
|
2022-05-31 17:58:33 +02:00
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/microsoftgdk"
|
2018-10-06 10:29:40 +02:00
|
|
|
)
|
|
|
|
|
2022-06-16 19:10:29 +02:00
|
|
|
type graphicsDriverCreatorImpl struct {
|
2022-02-11 12:38:45 +01:00
|
|
|
transparent bool
|
|
|
|
}
|
2022-03-22 15:33:21 +01:00
|
|
|
|
2022-06-16 19:10:29 +02:00
|
|
|
func (g *graphicsDriverCreatorImpl) newAuto() (graphicsdriver.Graphics, error) {
|
2022-06-17 04:39:43 +02:00
|
|
|
d, err1 := g.newDirectX()
|
|
|
|
if err1 == nil {
|
2022-06-16 19:02:29 +02:00
|
|
|
return d, nil
|
2022-02-11 12:38:45 +01:00
|
|
|
}
|
2022-06-17 04:39:43 +02:00
|
|
|
o, err2 := g.newOpenGL()
|
|
|
|
if err2 == nil {
|
|
|
|
return o, nil
|
|
|
|
}
|
|
|
|
return nil, fmt.Errorf("ui: failed to choose graphics drivers: DirectX: %v, OpenGL: %v", err1, err2)
|
2022-03-22 15:33:21 +01:00
|
|
|
}
|
|
|
|
|
2022-06-16 19:10:29 +02:00
|
|
|
func (*graphicsDriverCreatorImpl) newOpenGL() (graphicsdriver.Graphics, error) {
|
2022-06-16 19:02:29 +02:00
|
|
|
return opengl.NewGraphics()
|
2022-03-22 15:33:21 +01:00
|
|
|
}
|
|
|
|
|
2022-06-17 04:39:43 +02:00
|
|
|
func (g *graphicsDriverCreatorImpl) newDirectX() (graphicsdriver.Graphics, error) {
|
2022-02-11 12:38:45 +01:00
|
|
|
if g.transparent {
|
2022-06-17 04:39:43 +02:00
|
|
|
return nil, fmt.Errorf("ui: DirectX is not available with a transparent window")
|
2022-02-11 12:38:45 +01:00
|
|
|
}
|
2022-06-17 04:39:43 +02:00
|
|
|
return directx.NewGraphics()
|
2022-03-25 11:43:32 +01:00
|
|
|
}
|
|
|
|
|
2022-06-16 19:10:29 +02:00
|
|
|
func (*graphicsDriverCreatorImpl) newMetal() (graphicsdriver.Graphics, error) {
|
|
|
|
return nil, nil
|
2022-03-22 15:33:21 +01:00
|
|
|
}
|
|
|
|
|
2018-10-06 15:42:28 +02:00
|
|
|
const (
|
2022-05-31 17:37:54 +02:00
|
|
|
_SM_CYCAPTION = 4
|
|
|
|
_MONITOR_DEFAULTTONEAREST = 2
|
2018-10-06 15:42:28 +02:00
|
|
|
)
|
|
|
|
|
2022-05-31 17:37:54 +02:00
|
|
|
type _RECT struct {
|
2018-10-06 15:42:28 +02:00
|
|
|
left int32
|
|
|
|
top int32
|
|
|
|
right int32
|
|
|
|
bottom int32
|
|
|
|
}
|
|
|
|
|
2022-05-31 17:37:54 +02:00
|
|
|
type _MONITORINFO struct {
|
2018-10-06 15:42:28 +02:00
|
|
|
cbSize uint32
|
2022-05-31 17:37:54 +02:00
|
|
|
rcMonitor _RECT
|
|
|
|
rcWork _RECT
|
2018-10-06 15:42:28 +02:00
|
|
|
dwFlags uint32
|
|
|
|
}
|
|
|
|
|
2022-05-31 17:37:54 +02:00
|
|
|
type _POINT struct {
|
2022-02-07 15:31:08 +01:00
|
|
|
x int32
|
|
|
|
y int32
|
|
|
|
}
|
|
|
|
|
2018-10-06 15:42:28 +02:00
|
|
|
var (
|
2022-05-22 14:50:38 +02:00
|
|
|
user32 = windows.NewLazySystemDLL("user32.dll")
|
|
|
|
|
2022-02-07 15:31:08 +01:00
|
|
|
procGetSystemMetrics = user32.NewProc("GetSystemMetrics")
|
|
|
|
procMonitorFromWindow = user32.NewProc("MonitorFromWindow")
|
|
|
|
procGetMonitorInfoW = user32.NewProc("GetMonitorInfoW")
|
|
|
|
procGetCursorPos = user32.NewProc("GetCursorPos")
|
2018-10-06 15:42:28 +02:00
|
|
|
)
|
|
|
|
|
2022-05-31 17:37:54 +02:00
|
|
|
func _GetSystemMetrics(nIndex int) (int32, error) {
|
2022-01-31 17:03:10 +01:00
|
|
|
r, _, _ := procGetSystemMetrics.Call(uintptr(nIndex))
|
2022-05-10 17:48:41 +02:00
|
|
|
if int32(r) == 0 {
|
2022-01-31 17:03:10 +01:00
|
|
|
// GetLastError doesn't provide an extended information.
|
|
|
|
// See https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-getsystemmetrics
|
2022-02-06 08:08:22 +01:00
|
|
|
return 0, fmt.Errorf("ui: GetSystemMetrics returned 0")
|
2018-10-06 15:42:28 +02:00
|
|
|
}
|
2022-01-30 07:36:09 +01:00
|
|
|
return int32(r), nil
|
2018-10-06 15:42:28 +02:00
|
|
|
}
|
|
|
|
|
2022-05-31 17:37:54 +02:00
|
|
|
func _MonitorFromWindow(hwnd windows.HWND, dwFlags uint32) uintptr {
|
2022-01-30 07:36:09 +01:00
|
|
|
r, _, _ := procMonitorFromWindow.Call(uintptr(hwnd), uintptr(dwFlags))
|
|
|
|
return r
|
2018-10-06 15:42:28 +02:00
|
|
|
}
|
|
|
|
|
2022-05-31 17:37:54 +02:00
|
|
|
func _GetMonitorInfoW(hMonitor uintptr) (_MONITORINFO, error) {
|
|
|
|
mi := _MONITORINFO{}
|
|
|
|
mi.cbSize = uint32(unsafe.Sizeof(mi))
|
|
|
|
|
|
|
|
r, _, e := procGetMonitorInfoW.Call(hMonitor, uintptr(unsafe.Pointer(&mi)))
|
2022-05-10 17:48:41 +02:00
|
|
|
if int32(r) == 0 {
|
2022-01-30 07:36:09 +01:00
|
|
|
if e != nil && e != windows.ERROR_SUCCESS {
|
2022-05-31 17:37:54 +02:00
|
|
|
return _MONITORINFO{}, fmt.Errorf("ui: GetMonitorInfoW failed: error code: %w", e)
|
2022-01-30 07:36:09 +01:00
|
|
|
}
|
2022-05-31 17:37:54 +02:00
|
|
|
return _MONITORINFO{}, fmt.Errorf("ui: GetMonitorInfoW failed: returned 0")
|
2018-10-06 15:42:28 +02:00
|
|
|
}
|
2022-05-31 17:37:54 +02:00
|
|
|
return mi, nil
|
2018-10-06 15:42:28 +02:00
|
|
|
}
|
|
|
|
|
2022-05-31 17:37:54 +02:00
|
|
|
func _GetCursorPos() (int32, int32, error) {
|
|
|
|
var pt _POINT
|
2022-02-07 15:31:08 +01:00
|
|
|
r, _, e := procGetCursorPos.Call(uintptr(unsafe.Pointer(&pt)))
|
2022-05-10 17:48:41 +02:00
|
|
|
if int32(r) == 0 {
|
2022-02-07 15:31:08 +01:00
|
|
|
if e != nil && e != windows.ERROR_SUCCESS {
|
|
|
|
return 0, 0, fmt.Errorf("ui: GetCursorPos failed: error code: %w", e)
|
|
|
|
}
|
|
|
|
return 0, 0, fmt.Errorf("ui: GetCursorPos failed: returned 0")
|
|
|
|
}
|
|
|
|
return pt.x, pt.y, nil
|
|
|
|
}
|
|
|
|
|
2021-09-15 05:47:04 +02:00
|
|
|
// clearVideoModeScaleCache must be called from the main thread.
|
|
|
|
func clearVideoModeScaleCache() {}
|
|
|
|
|
2021-10-10 08:38:13 +02:00
|
|
|
// dipFromGLFWMonitorPixel must be called from the main thread.
|
2022-03-21 15:00:50 +01:00
|
|
|
func (u *userInterfaceImpl) dipFromGLFWMonitorPixel(x float64, monitor *glfw.Monitor) float64 {
|
2021-10-09 09:49:47 +02:00
|
|
|
return x / u.deviceScaleFactor(monitor)
|
2020-09-18 17:21:08 +02:00
|
|
|
}
|
|
|
|
|
2021-10-10 08:38:13 +02:00
|
|
|
// dipFromGLFWPixel must be called from the main thread.
|
2022-03-21 15:00:50 +01:00
|
|
|
func (u *userInterfaceImpl) dipFromGLFWPixel(x float64, monitor *glfw.Monitor) float64 {
|
2021-10-09 09:49:47 +02:00
|
|
|
return x / u.deviceScaleFactor(monitor)
|
2020-09-18 17:31:34 +02:00
|
|
|
}
|
|
|
|
|
2021-10-10 08:38:13 +02:00
|
|
|
// dipToGLFWPixel must be called from the main thread.
|
2022-03-21 15:00:50 +01:00
|
|
|
func (u *userInterfaceImpl) dipToGLFWPixel(x float64, monitor *glfw.Monitor) float64 {
|
2021-10-09 09:49:47 +02:00
|
|
|
return x * u.deviceScaleFactor(monitor)
|
2016-07-04 04:37:34 +02:00
|
|
|
}
|
2017-04-18 17:51:15 +02:00
|
|
|
|
2022-03-21 15:00:50 +01:00
|
|
|
func (u *userInterfaceImpl) adjustWindowPosition(x, y int, monitor *glfw.Monitor) (int, int) {
|
2022-05-31 17:58:33 +02:00
|
|
|
if microsoftgdk.IsXbox() {
|
|
|
|
return x, y
|
|
|
|
}
|
|
|
|
|
2022-02-07 15:51:40 +01:00
|
|
|
mx, my := monitor.GetPos()
|
2017-04-18 17:51:15 +02:00
|
|
|
// As the video width/height might be wrong,
|
|
|
|
// adjust x/y at least to enable to handle the window (#328)
|
2020-03-28 13:26:57 +01:00
|
|
|
if x < mx {
|
|
|
|
x = mx
|
2017-04-18 17:51:15 +02:00
|
|
|
}
|
2022-05-31 17:37:54 +02:00
|
|
|
t, err := _GetSystemMetrics(_SM_CYCAPTION)
|
2018-10-06 15:42:28 +02:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2022-01-30 07:36:09 +01:00
|
|
|
if y < my+int(t) {
|
|
|
|
y = my + int(t)
|
2017-04-18 17:51:15 +02:00
|
|
|
}
|
|
|
|
return x, y
|
|
|
|
}
|
2018-10-06 15:42:28 +02:00
|
|
|
|
2022-02-07 15:31:08 +01:00
|
|
|
func initialMonitorByOS() (*glfw.Monitor, error) {
|
2022-05-31 17:58:33 +02:00
|
|
|
if microsoftgdk.IsXbox() {
|
|
|
|
return glfw.GetPrimaryMonitor(), nil
|
|
|
|
}
|
|
|
|
|
2022-05-31 17:37:54 +02:00
|
|
|
px, py, err := _GetCursorPos()
|
2022-07-04 07:42:17 +02:00
|
|
|
if err != nil {
|
|
|
|
if errors.Is(err, windows.ERROR_ACCESS_DENIED) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
2022-02-07 15:31:08 +01:00
|
|
|
return nil, err
|
2021-04-18 13:05:37 +02:00
|
|
|
}
|
2022-02-07 15:31:08 +01:00
|
|
|
x, y := int(px), int(py)
|
|
|
|
|
|
|
|
// Find the monitor including the cursor.
|
|
|
|
for _, m := range ensureMonitors() {
|
|
|
|
w, h := m.vm.Width, m.vm.Height
|
|
|
|
if x >= m.x && x < m.x+w && y >= m.y && y < m.y+h {
|
|
|
|
return m.m, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, nil
|
2021-10-09 09:49:47 +02:00
|
|
|
}
|
|
|
|
|
2022-02-08 16:26:49 +01:00
|
|
|
func monitorFromWindowByOS(w *glfw.Window) *glfw.Monitor {
|
2022-05-31 17:58:33 +02:00
|
|
|
if microsoftgdk.IsXbox() {
|
|
|
|
return glfw.GetPrimaryMonitor()
|
|
|
|
}
|
2022-01-30 07:36:09 +01:00
|
|
|
return monitorFromWin32Window(windows.HWND(w.GetWin32Window()))
|
2021-10-09 09:49:47 +02:00
|
|
|
}
|
2021-04-18 13:05:37 +02:00
|
|
|
|
2022-01-30 07:36:09 +01:00
|
|
|
func monitorFromWin32Window(w windows.HWND) *glfw.Monitor {
|
2018-10-07 18:42:43 +02:00
|
|
|
// Get the current monitor by the window handle instead of the window position. It is because the window
|
|
|
|
// position is not relaiable in some cases e.g. when the window is put across multiple monitors.
|
|
|
|
|
2022-05-31 17:37:54 +02:00
|
|
|
m := _MonitorFromWindow(w, _MONITOR_DEFAULTTONEAREST)
|
2022-01-30 07:36:09 +01:00
|
|
|
if m == 0 {
|
2020-08-22 19:31:52 +02:00
|
|
|
// monitorFromWindow can return error on Wine. Ignore this.
|
2020-08-23 20:27:38 +02:00
|
|
|
return nil
|
2018-10-06 15:42:28 +02:00
|
|
|
}
|
|
|
|
|
2022-05-31 17:37:54 +02:00
|
|
|
mi, err := _GetMonitorInfoW(m)
|
|
|
|
if err != nil {
|
2018-10-06 15:42:28 +02:00
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
x, y := int(mi.rcMonitor.left), int(mi.rcMonitor.top)
|
2021-09-09 04:11:56 +02:00
|
|
|
for _, m := range ensureMonitors() {
|
|
|
|
if m.x == x && m.y == y {
|
|
|
|
return m.m
|
2018-10-06 15:42:28 +02:00
|
|
|
}
|
|
|
|
}
|
2020-08-23 20:27:38 +02:00
|
|
|
return nil
|
2018-10-06 15:42:28 +02:00
|
|
|
}
|
2018-11-12 16:00:10 +01:00
|
|
|
|
2022-03-21 15:00:50 +01:00
|
|
|
func (u *userInterfaceImpl) nativeWindow() uintptr {
|
2018-12-29 18:44:51 +01:00
|
|
|
return u.window.GetWin32Window()
|
2018-11-12 16:00:10 +01:00
|
|
|
}
|
2021-04-18 10:35:46 +02:00
|
|
|
|
2022-03-21 15:00:50 +01:00
|
|
|
func (u *userInterfaceImpl) isNativeFullscreen() bool {
|
2021-04-18 10:35:46 +02:00
|
|
|
return false
|
|
|
|
}
|
2021-05-02 07:50:50 +02:00
|
|
|
|
2022-03-21 15:00:50 +01:00
|
|
|
func (u *userInterfaceImpl) setNativeCursor(shape CursorShape) {
|
2021-05-02 07:50:50 +02:00
|
|
|
// TODO: Use native API in the future (#1571)
|
|
|
|
u.window.SetCursor(glfwSystemCursors[shape])
|
|
|
|
}
|
2021-09-17 19:21:24 +02:00
|
|
|
|
2022-03-21 15:00:50 +01:00
|
|
|
func (u *userInterfaceImpl) isNativeFullscreenAvailable() bool {
|
2021-09-17 19:21:24 +02:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2022-03-21 15:00:50 +01:00
|
|
|
func (u *userInterfaceImpl) setNativeFullscreen(fullscreen bool) {
|
2022-02-06 08:08:22 +01:00
|
|
|
panic(fmt.Sprintf("ui: setNativeFullscreen is not implemented in this environment: %s", runtime.GOOS))
|
2021-09-17 19:21:24 +02:00
|
|
|
}
|
2021-09-18 15:11:26 +02:00
|
|
|
|
2022-03-21 15:00:50 +01:00
|
|
|
func (u *userInterfaceImpl) adjustViewSize() {
|
2021-09-18 15:11:26 +02:00
|
|
|
}
|
2021-09-24 19:46:18 +02:00
|
|
|
|
2022-03-21 15:00:50 +01:00
|
|
|
func (u *userInterfaceImpl) setWindowResizingModeForOS(mode WindowResizingMode) {
|
2021-12-29 14:08:59 +01:00
|
|
|
}
|
|
|
|
|
2021-09-24 19:46:18 +02:00
|
|
|
func initializeWindowAfterCreation(w *glfw.Window) {
|
|
|
|
}
|
2022-03-29 21:15:52 +02:00
|
|
|
|
2022-04-08 08:59:16 +02:00
|
|
|
func (u *userInterfaceImpl) origWindowPosByOS() (int, int, bool) {
|
2022-03-29 21:15:52 +02:00
|
|
|
return 0, 0, false
|
|
|
|
}
|
|
|
|
|
2022-04-08 08:59:16 +02:00
|
|
|
func (u *userInterfaceImpl) setOrigWindowPosByOS(x, y int) bool {
|
2022-03-29 21:15:52 +02:00
|
|
|
return false
|
|
|
|
}
|