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-07-09 08:19:47 +02:00
|
|
|
//go:build !ebitenginecbackend && !ebitencbackend
|
|
|
|
// +build !ebitenginecbackend,!ebitencbackend
|
2022-02-06 08:08:22 +01:00
|
|
|
|
|
|
|
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"
|
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-07-30 19:56:16 +02:00
|
|
|
func (g *graphicsDriverCreatorImpl) newAuto() (graphicsdriver.Graphics, GraphicsLibrary, error) {
|
2022-06-17 04:39:43 +02:00
|
|
|
d, err1 := g.newDirectX()
|
|
|
|
if err1 == nil {
|
2022-07-30 19:56:16 +02:00
|
|
|
return d, GraphicsLibraryDirectX, nil
|
2022-02-11 12:38:45 +01:00
|
|
|
}
|
2022-06-17 04:39:43 +02:00
|
|
|
o, err2 := g.newOpenGL()
|
|
|
|
if err2 == nil {
|
2022-07-30 19:56:16 +02:00
|
|
|
return o, GraphicsLibraryOpenGL, nil
|
2022-06-17 04:39:43 +02:00
|
|
|
}
|
2022-07-30 19:56:16 +02:00
|
|
|
return nil, GraphicsLibraryUnknown, 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
|
|
|
}
|
|
|
|
|
2022-08-10 04:43:58 +02:00
|
|
|
type userInterfaceImplNative struct {
|
|
|
|
origWindowPosX int
|
|
|
|
origWindowPosY int
|
|
|
|
}
|
|
|
|
|
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-08-10 04:43:58 +02:00
|
|
|
func (u *userInterfaceImpl) origWindowPos() (int, int) {
|
|
|
|
return u.native.origWindowPosX, u.native.origWindowPosY
|
2022-03-29 21:15:52 +02:00
|
|
|
}
|
|
|
|
|
2022-08-10 04:43:58 +02:00
|
|
|
func (u *userInterfaceImpl) setOrigWindowPos(x, y int) {
|
|
|
|
u.native.origWindowPosX = x
|
|
|
|
u.native.origWindowPosY = y
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *userInterfaceImplNative) initialize() {
|
|
|
|
u.origWindowPosX = invalidPos
|
|
|
|
u.origWindowPosY = invalidPos
|
2022-03-29 21:15:52 +02:00
|
|
|
}
|