mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-12-26 11:48:55 +01:00
uidriver/glfw: Refactoring: Add toDeviceIndependentPixel / toDeviceDependentPixel
This commit is contained in:
parent
a0cf8bac21
commit
e00a190f22
@ -47,6 +47,8 @@ type Input struct {
|
|||||||
touches map[int]pos // This is not updated until GLFW 3.3 is available (#417)
|
touches map[int]pos // This is not updated until GLFW 3.3 is available (#417)
|
||||||
runeBuffer []rune
|
runeBuffer []rune
|
||||||
ui *UserInterface
|
ui *UserInterface
|
||||||
|
|
||||||
|
m sync.RWMutex
|
||||||
}
|
}
|
||||||
|
|
||||||
type pos struct {
|
type pos struct {
|
||||||
@ -55,15 +57,15 @@ type pos struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (i *Input) CursorPosition() (x, y int) {
|
func (i *Input) CursorPosition() (x, y int) {
|
||||||
i.ui.m.RLock()
|
i.m.RLock()
|
||||||
cx, cy := i.cursorX, i.cursorY
|
cx, cy := i.cursorX, i.cursorY
|
||||||
i.ui.m.RUnlock()
|
i.m.RUnlock()
|
||||||
return i.ui.adjustPosition(cx, cy)
|
return i.ui.adjustPosition(cx, cy)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *Input) GamepadIDs() []int {
|
func (i *Input) GamepadIDs() []int {
|
||||||
i.ui.m.RLock()
|
i.m.RLock()
|
||||||
defer i.ui.m.RUnlock()
|
defer i.m.RUnlock()
|
||||||
if len(i.gamepads) == 0 {
|
if len(i.gamepads) == 0 {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@ -77,8 +79,8 @@ func (i *Input) GamepadIDs() []int {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (i *Input) GamepadAxisNum(id int) int {
|
func (i *Input) GamepadAxisNum(id int) int {
|
||||||
i.ui.m.RLock()
|
i.m.RLock()
|
||||||
defer i.ui.m.RUnlock()
|
defer i.m.RUnlock()
|
||||||
if len(i.gamepads) <= id {
|
if len(i.gamepads) <= id {
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
@ -86,8 +88,8 @@ func (i *Input) GamepadAxisNum(id int) int {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (i *Input) GamepadAxis(id int, axis int) float64 {
|
func (i *Input) GamepadAxis(id int, axis int) float64 {
|
||||||
i.ui.m.RLock()
|
i.m.RLock()
|
||||||
defer i.ui.m.RUnlock()
|
defer i.m.RUnlock()
|
||||||
if len(i.gamepads) <= id {
|
if len(i.gamepads) <= id {
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
@ -95,8 +97,8 @@ func (i *Input) GamepadAxis(id int, axis int) float64 {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (i *Input) GamepadButtonNum(id int) int {
|
func (i *Input) GamepadButtonNum(id int) int {
|
||||||
i.ui.m.RLock()
|
i.m.RLock()
|
||||||
defer i.ui.m.RUnlock()
|
defer i.m.RUnlock()
|
||||||
if len(i.gamepads) <= id {
|
if len(i.gamepads) <= id {
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
@ -104,8 +106,8 @@ func (i *Input) GamepadButtonNum(id int) int {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (i *Input) IsGamepadButtonPressed(id int, button driver.GamepadButton) bool {
|
func (i *Input) IsGamepadButtonPressed(id int, button driver.GamepadButton) bool {
|
||||||
i.ui.m.RLock()
|
i.m.RLock()
|
||||||
defer i.ui.m.RUnlock()
|
defer i.m.RUnlock()
|
||||||
if len(i.gamepads) <= id {
|
if len(i.gamepads) <= id {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
@ -113,8 +115,8 @@ func (i *Input) IsGamepadButtonPressed(id int, button driver.GamepadButton) bool
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (i *Input) TouchIDs() []int {
|
func (i *Input) TouchIDs() []int {
|
||||||
i.ui.m.RLock()
|
i.m.RLock()
|
||||||
defer i.ui.m.RUnlock()
|
defer i.m.RUnlock()
|
||||||
|
|
||||||
if len(i.touches) == 0 {
|
if len(i.touches) == 0 {
|
||||||
return nil
|
return nil
|
||||||
@ -128,7 +130,7 @@ func (i *Input) TouchIDs() []int {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (i *Input) TouchPosition(id int) (x, y int) {
|
func (i *Input) TouchPosition(id int) (x, y int) {
|
||||||
i.ui.m.RLock()
|
i.m.RLock()
|
||||||
found := false
|
found := false
|
||||||
var p pos
|
var p pos
|
||||||
for tid, pos := range i.touches {
|
for tid, pos := range i.touches {
|
||||||
@ -138,7 +140,7 @@ func (i *Input) TouchPosition(id int) (x, y int) {
|
|||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
i.ui.m.RUnlock()
|
i.m.RUnlock()
|
||||||
|
|
||||||
if !found {
|
if !found {
|
||||||
return 0, 0
|
return 0, 0
|
||||||
@ -147,21 +149,21 @@ func (i *Input) TouchPosition(id int) (x, y int) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (i *Input) RuneBuffer() []rune {
|
func (i *Input) RuneBuffer() []rune {
|
||||||
i.ui.m.RLock()
|
i.m.RLock()
|
||||||
defer i.ui.m.RUnlock()
|
defer i.m.RUnlock()
|
||||||
return i.runeBuffer
|
return i.runeBuffer
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *Input) ResetForFrame() {
|
func (i *Input) ResetForFrame() {
|
||||||
i.ui.m.RLock()
|
i.m.RLock()
|
||||||
defer i.ui.m.RUnlock()
|
defer i.m.RUnlock()
|
||||||
i.runeBuffer = i.runeBuffer[:0]
|
i.runeBuffer = i.runeBuffer[:0]
|
||||||
i.scrollX, i.scrollY = 0, 0
|
i.scrollX, i.scrollY = 0, 0
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *Input) IsKeyPressed(key driver.Key) bool {
|
func (i *Input) IsKeyPressed(key driver.Key) bool {
|
||||||
i.ui.m.RLock()
|
i.m.RLock()
|
||||||
defer i.ui.m.RUnlock()
|
defer i.m.RUnlock()
|
||||||
if i.keyPressed == nil {
|
if i.keyPressed == nil {
|
||||||
i.keyPressed = map[glfw.Key]bool{}
|
i.keyPressed = map[glfw.Key]bool{}
|
||||||
}
|
}
|
||||||
@ -177,8 +179,8 @@ func (i *Input) IsKeyPressed(key driver.Key) bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (i *Input) IsMouseButtonPressed(button driver.MouseButton) bool {
|
func (i *Input) IsMouseButtonPressed(button driver.MouseButton) bool {
|
||||||
i.ui.m.RLock()
|
i.m.RLock()
|
||||||
defer i.ui.m.RUnlock()
|
defer i.m.RUnlock()
|
||||||
if i.mouseButtonPressed == nil {
|
if i.mouseButtonPressed == nil {
|
||||||
i.mouseButtonPressed = map[glfw.MouseButton]bool{}
|
i.mouseButtonPressed = map[glfw.MouseButton]bool{}
|
||||||
}
|
}
|
||||||
@ -194,8 +196,8 @@ func (i *Input) IsMouseButtonPressed(button driver.MouseButton) bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (i *Input) Wheel() (xoff, yoff float64) {
|
func (i *Input) Wheel() (xoff, yoff float64) {
|
||||||
i.ui.m.RLock()
|
i.m.RLock()
|
||||||
defer i.ui.m.RUnlock()
|
defer i.m.RUnlock()
|
||||||
return i.scrollX, i.scrollY
|
return i.scrollX, i.scrollY
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -209,21 +211,21 @@ func (i *Input) appendRuneBuffer(char rune) {
|
|||||||
if !unicode.IsPrint(char) {
|
if !unicode.IsPrint(char) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
i.ui.m.Lock()
|
i.m.Lock()
|
||||||
i.runeBuffer = append(i.runeBuffer, char)
|
i.runeBuffer = append(i.runeBuffer, char)
|
||||||
i.ui.m.Unlock()
|
i.m.Unlock()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *Input) setWheel(xoff, yoff float64) {
|
func (i *Input) setWheel(xoff, yoff float64) {
|
||||||
i.ui.m.Lock()
|
i.m.Lock()
|
||||||
i.scrollX = xoff
|
i.scrollX = xoff
|
||||||
i.scrollY = yoff
|
i.scrollY = yoff
|
||||||
i.ui.m.Unlock()
|
i.m.Unlock()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *Input) update(window *glfw.Window, scale float64) {
|
func (i *Input) update(window *glfw.Window) {
|
||||||
i.ui.m.Lock()
|
i.m.Lock()
|
||||||
defer i.ui.m.Unlock()
|
defer i.m.Unlock()
|
||||||
|
|
||||||
i.onceCallback.Do(func() {
|
i.onceCallback.Do(func() {
|
||||||
window.SetCharModsCallback(func(w *glfw.Window, char rune, mods glfw.ModifierKey) {
|
window.SetCharModsCallback(func(w *glfw.Window, char rune, mods glfw.ModifierKey) {
|
||||||
@ -246,8 +248,8 @@ func (i *Input) update(window *glfw.Window, scale float64) {
|
|||||||
i.mouseButtonPressed[gb] = window.GetMouseButton(gb) == glfw.Press
|
i.mouseButtonPressed[gb] = window.GetMouseButton(gb) == glfw.Press
|
||||||
}
|
}
|
||||||
x, y := window.GetCursorPos()
|
x, y := window.GetCursorPos()
|
||||||
i.cursorX = int(x / scale)
|
i.cursorX = int(i.ui.toDeviceIndependentPixel(x) / i.ui.getScale())
|
||||||
i.cursorY = int(y / scale)
|
i.cursorY = int(i.ui.toDeviceIndependentPixel(y) / i.ui.getScale())
|
||||||
for id := glfw.Joystick(0); id < glfw.Joystick(len(i.gamepads)); id++ {
|
for id := glfw.Joystick(0); id < glfw.Joystick(len(i.gamepads)); id++ {
|
||||||
i.gamepads[id].valid = false
|
i.gamepads[id].valid = false
|
||||||
if !id.Present() {
|
if !id.Present() {
|
||||||
|
@ -134,9 +134,8 @@ func initialize() error {
|
|||||||
theUI.window = w
|
theUI.window = w
|
||||||
theUI.initMonitor = theUI.currentMonitorFromPosition()
|
theUI.initMonitor = theUI.currentMonitorFromPosition()
|
||||||
v := theUI.initMonitor.GetVideoMode()
|
v := theUI.initMonitor.GetVideoMode()
|
||||||
s := theUI.glfwScale()
|
theUI.initFullscreenWidth = int(theUI.toDeviceIndependentPixel(float64(v.Width)))
|
||||||
theUI.initFullscreenWidth = int(float64(v.Width) / s)
|
theUI.initFullscreenHeight = int(theUI.toDeviceIndependentPixel(float64(v.Height)))
|
||||||
theUI.initFullscreenHeight = int(float64(v.Height) / s)
|
|
||||||
theUI.window.Destroy()
|
theUI.window.Destroy()
|
||||||
theUI.window = nil
|
theUI.window = nil
|
||||||
|
|
||||||
@ -306,19 +305,29 @@ func (u *UserInterface) setInitWindowPosition(x, y int) {
|
|||||||
u.initWindowPositionY = y
|
u.initWindowPositionY = y
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// toDeviceIndependentPixel must be called from the main thread.
|
||||||
|
func (u *UserInterface) toDeviceIndependentPixel(x float64) float64 {
|
||||||
|
return x / u.glfwScale()
|
||||||
|
}
|
||||||
|
|
||||||
|
// toDeviceDependentPixel must be called from the main thread.
|
||||||
|
func (u *UserInterface) toDeviceDependentPixel(x float64) float64 {
|
||||||
|
return x * u.glfwScale()
|
||||||
|
}
|
||||||
|
|
||||||
func (u *UserInterface) ScreenSizeInFullscreen() (int, int) {
|
func (u *UserInterface) ScreenSizeInFullscreen() (int, int) {
|
||||||
if !u.isRunning() {
|
if !u.isRunning() {
|
||||||
return u.initFullscreenWidth, u.initFullscreenHeight
|
return u.initFullscreenWidth, u.initFullscreenHeight
|
||||||
}
|
}
|
||||||
|
|
||||||
var v *glfw.VidMode
|
var w, h int
|
||||||
s := 0.0
|
|
||||||
_ = u.t.Call(func() error {
|
_ = u.t.Call(func() error {
|
||||||
v = u.currentMonitor().GetVideoMode()
|
v := u.currentMonitor().GetVideoMode()
|
||||||
s = u.glfwScale()
|
w = int(u.toDeviceIndependentPixel(float64(v.Width)))
|
||||||
|
h = int(u.toDeviceIndependentPixel(float64(v.Height)))
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
return int(float64(v.Width) / s), int(float64(v.Height) / s)
|
return w, h
|
||||||
}
|
}
|
||||||
|
|
||||||
func (u *UserInterface) SetScreenSize(width, height int) {
|
func (u *UserInterface) SetScreenSize(width, height int) {
|
||||||
@ -446,21 +455,18 @@ func (u *UserInterface) ScreenPadding() (x0, y0, x1, y1 float64) {
|
|||||||
d := 0.0
|
d := 0.0
|
||||||
sx := 0.0
|
sx := 0.0
|
||||||
sy := 0.0
|
sy := 0.0
|
||||||
gs := 0.0
|
mx := 0.0
|
||||||
vw := 0.0
|
my := 0.0
|
||||||
vh := 0.0
|
|
||||||
_ = u.t.Call(func() error {
|
_ = u.t.Call(func() error {
|
||||||
d = u.deviceScaleFactor()
|
|
||||||
sx = float64(u.width) * u.actualScreenScale()
|
sx = float64(u.width) * u.actualScreenScale()
|
||||||
sy = float64(u.height) * u.actualScreenScale()
|
sy = float64(u.height) * u.actualScreenScale()
|
||||||
gs = u.glfwScale()
|
|
||||||
|
|
||||||
v := u.window.GetMonitor().GetVideoMode()
|
v := u.currentMonitor().GetVideoMode()
|
||||||
vw, vh = float64(v.Width), float64(v.Height)
|
d = u.deviceScaleFactor()
|
||||||
|
mx = u.toDeviceIndependentPixel(float64(v.Width)) * d
|
||||||
|
my = u.toDeviceIndependentPixel(float64(v.Height)) * d
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
mx := vw * d / gs
|
|
||||||
my := vh * d / gs
|
|
||||||
|
|
||||||
ox := (mx - sx) / 2
|
ox := (mx - sx) / 2
|
||||||
oy := (my - sy) / 2
|
oy := (my - sy) / 2
|
||||||
@ -659,9 +665,8 @@ func (u *UserInterface) createWindow() error {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
s := u.glfwScale()
|
w := int(u.toDeviceIndependentPixel(float64(width)) / u.scale)
|
||||||
w := int(float64(width) / u.scale / s)
|
h := int(u.toDeviceIndependentPixel(float64(height)) / u.scale)
|
||||||
h := int(float64(height) / u.scale / s)
|
|
||||||
u.reqWidth = w
|
u.reqWidth = w
|
||||||
u.reqHeight = h
|
u.reqHeight = h
|
||||||
})
|
})
|
||||||
@ -745,8 +750,8 @@ func (u *UserInterface) run(width, height int, scale float64, title string, cont
|
|||||||
|
|
||||||
_ = u.t.Call(func() error {
|
_ = u.t.Call(func() error {
|
||||||
// Get the window size before showing since window.Show might change the current
|
// Get the window size before showing since window.Show might change the current
|
||||||
// monitor which affects glfwSize result.
|
// monitor which affects deviceDependentWindowSize result.
|
||||||
w, h := u.glfwSize()
|
w, h := u.deviceDependentWindowSize()
|
||||||
|
|
||||||
u.title = title
|
u.title = title
|
||||||
u.window.SetTitle(title)
|
u.window.SetTitle(title)
|
||||||
@ -774,10 +779,10 @@ func (u *UserInterface) run(width, height int, scale float64, title string, cont
|
|||||||
return u.loop(context)
|
return u.loop(context)
|
||||||
}
|
}
|
||||||
|
|
||||||
// getSize must be called from the main thread.
|
// deviceDependentWindowSize must be called from the main thread.
|
||||||
func (u *UserInterface) glfwSize() (int, int) {
|
func (u *UserInterface) deviceDependentWindowSize() (int, int) {
|
||||||
w := int(float64(u.windowWidth) * u.getScale() * u.glfwScale())
|
w := int(u.toDeviceDependentPixel(float64(u.windowWidth) * u.getScale()))
|
||||||
h := int(float64(u.height) * u.getScale() * u.glfwScale())
|
h := int(u.toDeviceDependentPixel(float64(u.height) * u.getScale()))
|
||||||
return w, h
|
return w, h
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -787,9 +792,9 @@ func (u *UserInterface) getScale() float64 {
|
|||||||
return u.scale
|
return u.scale
|
||||||
}
|
}
|
||||||
if u.fullscreenScale == 0 {
|
if u.fullscreenScale == 0 {
|
||||||
v := u.window.GetMonitor().GetVideoMode()
|
v := u.currentMonitor().GetVideoMode()
|
||||||
sw := float64(v.Width) / u.glfwScale() / float64(u.width)
|
sw := u.toDeviceIndependentPixel(float64(v.Width)) / float64(u.width)
|
||||||
sh := float64(v.Height) / u.glfwScale() / float64(u.height)
|
sh := u.toDeviceIndependentPixel(float64(v.Height)) / float64(u.height)
|
||||||
s := sw
|
s := sw
|
||||||
if s > sh {
|
if s > sh {
|
||||||
s = sh
|
s = sh
|
||||||
@ -848,7 +853,7 @@ func (u *UserInterface) update(context driver.UIContext) error {
|
|||||||
_ = u.t.Call(func() error {
|
_ = u.t.Call(func() error {
|
||||||
glfw.PollEvents()
|
glfw.PollEvents()
|
||||||
|
|
||||||
u.input.update(u.window, u.getScale()*u.glfwScale())
|
u.input.update(u.window)
|
||||||
|
|
||||||
defer hooks.ResumeAudio()
|
defer hooks.ResumeAudio()
|
||||||
|
|
||||||
@ -1018,14 +1023,14 @@ func (u *UserInterface) setScreenSize(width, height int, scale float64, fullscre
|
|||||||
}
|
}
|
||||||
|
|
||||||
oldW, oldH := u.window.GetSize()
|
oldW, oldH := u.window.GetSize()
|
||||||
newW, newH := u.glfwSize()
|
newW, newH := u.deviceDependentWindowSize()
|
||||||
if oldW != newW || oldH != newH {
|
if oldW != newW || oldH != newH {
|
||||||
ch := make(chan struct{})
|
ch := make(chan struct{})
|
||||||
u.window.SetFramebufferSizeCallback(func(_ *glfw.Window, _, _ int) {
|
u.window.SetFramebufferSizeCallback(func(_ *glfw.Window, _, _ int) {
|
||||||
u.window.SetFramebufferSizeCallback(nil)
|
u.window.SetFramebufferSizeCallback(nil)
|
||||||
close(ch)
|
close(ch)
|
||||||
})
|
})
|
||||||
u.window.SetSize(u.glfwSize())
|
u.window.SetSize(newW, newH)
|
||||||
event:
|
event:
|
||||||
for {
|
for {
|
||||||
glfw.PollEvents()
|
glfw.PollEvents()
|
||||||
|
Loading…
Reference in New Issue
Block a user