mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-12-25 03:08:54 +01:00
internal/ui: use the cursor position to choose the initial monitor for Windows
Updates #1918 Updates #1982
This commit is contained in:
parent
cafb71719c
commit
ed04021151
@ -170,8 +170,12 @@ func initialize() error {
|
|||||||
glfw.WindowHint(glfw.ClientAPI, glfw.NoAPI)
|
glfw.WindowHint(glfw.ClientAPI, glfw.NoAPI)
|
||||||
|
|
||||||
var m *glfw.Monitor
|
var m *glfw.Monitor
|
||||||
if runtime.GOOS == "darwin" {
|
if runtime.GOOS == "darwin" || runtime.GOOS == "windows" {
|
||||||
m = initialMonitorByOS()
|
var err error
|
||||||
|
m, err = initialMonitorByOS()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
if m == nil {
|
if m == nil {
|
||||||
m = glfw.GetPrimaryMonitor()
|
m = glfw.GetPrimaryMonitor()
|
||||||
}
|
}
|
||||||
@ -189,7 +193,10 @@ func initialize() error {
|
|||||||
defer w.Destroy()
|
defer w.Destroy()
|
||||||
initializeWindowAfterCreation(w)
|
initializeWindowAfterCreation(w)
|
||||||
theUI.waitForFramebufferSizeCallback(w, nil)
|
theUI.waitForFramebufferSizeCallback(w, nil)
|
||||||
m = initialMonitorByOS()
|
m, err = initialMonitorByOS()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
if m == nil {
|
if m == nil {
|
||||||
m = currentMonitorImpl(w)
|
m = currentMonitorImpl(w)
|
||||||
}
|
}
|
||||||
|
@ -158,7 +158,7 @@ func (u *UserInterface) adjustWindowPosition(x, y int) (int, int) {
|
|||||||
return x, y
|
return x, y
|
||||||
}
|
}
|
||||||
|
|
||||||
func initialMonitorByOS() *glfw.Monitor {
|
func initialMonitorByOS() (*glfw.Monitor, error) {
|
||||||
var cx, cy C.int
|
var cx, cy C.int
|
||||||
C.currentMouseLocation(&cx, &cy)
|
C.currentMouseLocation(&cx, &cy)
|
||||||
x, y := int(cx), int(cy)
|
x, y := int(cx), int(cy)
|
||||||
@ -176,11 +176,11 @@ func initialMonitorByOS() *glfw.Monitor {
|
|||||||
for _, m := range ensureMonitors() {
|
for _, m := range ensureMonitors() {
|
||||||
w, h := m.vm.Width, m.vm.Height
|
w, h := m.vm.Width, m.vm.Height
|
||||||
if x >= m.x && x < m.x+w && y >= m.y && y < m.y+h {
|
if x >= m.x && x < m.x+w && y >= m.y && y < m.y+h {
|
||||||
return m.m
|
return m.m, nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func currentMonitorByOS(w *glfw.Window) *glfw.Monitor {
|
func currentMonitorByOS(w *glfw.Window) *glfw.Monitor {
|
||||||
|
@ -133,8 +133,8 @@ func (u *UserInterface) adjustWindowPosition(x, y int) (int, int) {
|
|||||||
return x, y
|
return x, y
|
||||||
}
|
}
|
||||||
|
|
||||||
func initialMonitorByOS() *glfw.Monitor {
|
func initialMonitorByOS() (*glfw.Monitor, error) {
|
||||||
return nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func currentMonitorByOS(_ *glfw.Window) *glfw.Monitor {
|
func currentMonitorByOS(_ *glfw.Window) *glfw.Monitor {
|
||||||
|
@ -46,12 +46,17 @@ type monitorInfo struct {
|
|||||||
dwFlags uint32
|
dwFlags uint32
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type point struct {
|
||||||
|
x int32
|
||||||
|
y int32
|
||||||
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
// user32 is defined at hideconsole_windows.go
|
// user32 is defined at hideconsole_windows.go
|
||||||
procGetSystemMetrics = user32.NewProc("GetSystemMetrics")
|
procGetSystemMetrics = user32.NewProc("GetSystemMetrics")
|
||||||
procGetForegroundWindow = user32.NewProc("GetForegroundWindow")
|
|
||||||
procMonitorFromWindow = user32.NewProc("MonitorFromWindow")
|
procMonitorFromWindow = user32.NewProc("MonitorFromWindow")
|
||||||
procGetMonitorInfoW = user32.NewProc("GetMonitorInfoW")
|
procGetMonitorInfoW = user32.NewProc("GetMonitorInfoW")
|
||||||
|
procGetCursorPos = user32.NewProc("GetCursorPos")
|
||||||
)
|
)
|
||||||
|
|
||||||
func getSystemMetrics(nIndex int) (int32, error) {
|
func getSystemMetrics(nIndex int) (int32, error) {
|
||||||
@ -64,11 +69,6 @@ func getSystemMetrics(nIndex int) (int32, error) {
|
|||||||
return int32(r), nil
|
return int32(r), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func getForegroundWindow() windows.HWND {
|
|
||||||
r, _, _ := procGetForegroundWindow.Call()
|
|
||||||
return windows.HWND(r)
|
|
||||||
}
|
|
||||||
|
|
||||||
func monitorFromWindow(hwnd windows.HWND, dwFlags uint32) uintptr {
|
func monitorFromWindow(hwnd windows.HWND, dwFlags uint32) uintptr {
|
||||||
r, _, _ := procMonitorFromWindow.Call(uintptr(hwnd), uintptr(dwFlags))
|
r, _, _ := procMonitorFromWindow.Call(uintptr(hwnd), uintptr(dwFlags))
|
||||||
return r
|
return r
|
||||||
@ -85,6 +85,18 @@ func getMonitorInfoW(hMonitor uintptr, lpmi *monitorInfo) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func getCursorPos() (int32, int32, error) {
|
||||||
|
var pt point
|
||||||
|
r, _, e := procGetCursorPos.Call(uintptr(unsafe.Pointer(&pt)))
|
||||||
|
if r == 0 {
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|
||||||
// clearVideoModeScaleCache must be called from the main thread.
|
// clearVideoModeScaleCache must be called from the main thread.
|
||||||
func clearVideoModeScaleCache() {}
|
func clearVideoModeScaleCache() {}
|
||||||
|
|
||||||
@ -120,14 +132,22 @@ func (u *UserInterface) adjustWindowPosition(x, y int) (int, int) {
|
|||||||
return x, y
|
return x, y
|
||||||
}
|
}
|
||||||
|
|
||||||
func initialMonitorByOS() *glfw.Monitor {
|
func initialMonitorByOS() (*glfw.Monitor, error) {
|
||||||
// Get the foreground window, that is common among multiple processes.
|
px, py, err := getCursorPos()
|
||||||
w := getForegroundWindow()
|
if err != nil {
|
||||||
if w == 0 {
|
return nil, err
|
||||||
// GetForegroundWindow can return null according to the document.
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
return monitorFromWin32Window(w)
|
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
|
||||||
}
|
}
|
||||||
|
|
||||||
func currentMonitorByOS(w *glfw.Window) *glfw.Monitor {
|
func currentMonitorByOS(w *glfw.Window) *glfw.Monitor {
|
||||||
|
Loading…
Reference in New Issue
Block a user