internal/uidriver/glfw: Remove the dirty hack creating a temporary UI instance

Updates #1122
This commit is contained in:
Hajime Hoshi 2021-02-11 03:45:35 +09:00
parent fea802b39d
commit ec912e5cad
5 changed files with 23 additions and 21 deletions

View File

@ -303,8 +303,9 @@ func (i *Input) update(window *glfw.Window, context driver.UIContext) {
}
cx, cy := window.GetCursorPos()
// TODO: This is tricky. Rename the function?
cx = i.ui.fromGLFWMonitorPixel(cx)
cy = i.ui.fromGLFWMonitorPixel(cy)
s := i.ui.deviceScaleFactor()
cx = fromGLFWMonitorPixel(cx, s)
cy = fromGLFWMonitorPixel(cy, s)
cx, cy = context.AdjustPosition(cx, cy, i.ui.deviceScaleFactor())
i.cursorX, i.cursorY = int(cx), int(cy)

View File

@ -50,6 +50,7 @@ type UserInterface struct {
origPosY int
runnableOnUnfocused bool
vsync bool
iconImages []image.Image
// err must be accessed from the main thread.
err error
@ -73,7 +74,6 @@ type UserInterface struct {
initWindowMaximized bool
initScreenTransparent bool
initFocused bool
iconImages []image.Image
vsyncInited bool
@ -143,15 +143,14 @@ func initialize() error {
// This can happen on Windows Remote Desktop (#903).
panic("glfw: glfw.CreateWindow must not return nil")
}
defer w.Destroy()
// Create a window and set it: this affects fromGLFWMonitorPixel and deviceScaleFactor.
theUI.window = w
theUI.initMonitor = currentMonitor(w)
v := theUI.initMonitor.GetVideoMode()
theUI.initFullscreenWidthInDP = int(theUI.fromGLFWMonitorPixel(float64(v.Width)))
theUI.initFullscreenHeightInDP = int(theUI.fromGLFWMonitorPixel(float64(v.Height)))
theUI.window.Destroy()
theUI.window = nil
m := currentMonitor(w)
theUI.initMonitor = m
v := m.GetVideoMode()
scale := devicescale.GetAt(currentMonitor(w).GetPos())
theUI.initFullscreenWidthInDP = int(fromGLFWMonitorPixel(float64(v.Width), scale))
theUI.initFullscreenHeightInDP = int(fromGLFWMonitorPixel(float64(v.Height), scale))
return nil
}
@ -398,8 +397,9 @@ func (u *UserInterface) ScreenSizeInFullscreen() (int, int) {
var w, h int
_ = u.t.Call(func() error {
v := currentMonitor(u.window).GetVideoMode()
w = int(u.fromGLFWMonitorPixel(float64(v.Width)))
h = int(u.fromGLFWMonitorPixel(float64(v.Height)))
s := u.deviceScaleFactor()
w = int(fromGLFWMonitorPixel(float64(v.Width), s))
h = int(fromGLFWMonitorPixel(float64(v.Height), s))
return nil
})
return w, h
@ -762,8 +762,9 @@ func (u *UserInterface) updateSize() (float64, float64, bool) {
if u.isFullscreen() {
v := currentMonitor(u.window).GetVideoMode()
ww, wh := v.Width, v.Height
w = u.fromGLFWMonitorPixel(float64(ww))
h = u.fromGLFWMonitorPixel(float64(wh))
s := u.deviceScaleFactor()
w = fromGLFWMonitorPixel(float64(ww), s)
h = fromGLFWMonitorPixel(float64(wh), s)
} else {
// Instead of u.windowWidth and u.windowHeight, use the actual window size here.
// On Windows, the specified size at SetSize and the actual window size might not

View File

@ -44,7 +44,7 @@ import (
"github.com/hajimehoshi/ebiten/v2/internal/glfw"
)
func (u *UserInterface) fromGLFWMonitorPixel(x float64) float64 {
func fromGLFWMonitorPixel(x float64, deviceScale float64) float64 {
return x
}

View File

@ -24,14 +24,14 @@ import (
)
// fromGLFWMonitorPixel must be called from the main thread.
func (u *UserInterface) fromGLFWMonitorPixel(x float64) float64 {
return math.Ceil(x / u.deviceScaleFactor())
func fromGLFWMonitorPixel(x float64, deviceScale float64) float64 {
return math.Ceil(x / deviceScale)
}
// fromGLFWPixel must be called from the main thread.
func (u *UserInterface) fromGLFWPixel(x float64) float64 {
// deviceScaleFactor() is a scale by desktop environment (e.g., Cinnamon), while GetContentScale() is X's scale.
// They are different things and then need to be treated different ways (#1350).
// They are different things and then need to be treated in different ways (#1350).
s, _ := currentMonitor(u.window).GetContentScale()
return x / float64(s)
}

View File

@ -98,8 +98,8 @@ func getMonitorInfoW(hMonitor uintptr, lpmi *monitorInfo) error {
}
// fromGLFWMonitorPixel must be called from the main thread.
func (u *UserInterface) fromGLFWMonitorPixel(x float64) float64 {
return x / u.deviceScaleFactor()
func fromGLFWMonitorPixel(x float64, deviceScale float64) float64 {
return x / deviceScale
}
// fromGLFWPixel must be called from the main thread.