From c021d6be6a90674d41f112d600c532dabe5f18ec Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Sat, 30 Nov 2019 22:37:53 +0900 Subject: [PATCH] ui: Add WindowPosition() Fixes #936 --- examples/windowsize/main.go | 6 ++++-- internal/driver/ui.go | 8 ++++++-- internal/uidriver/glfw/ui.go | 12 ++++++++++++ internal/uidriver/js/ui.go | 4 ++++ internal/uidriver/mobile/ui.go | 4 ++++ window.go | 9 +++++++++ 6 files changed, 39 insertions(+), 4 deletions(-) diff --git a/examples/windowsize/main.go b/examples/windowsize/main.go index a9f2acd59..b14188fcc 100644 --- a/examples/windowsize/main.go +++ b/examples/windowsize/main.go @@ -167,7 +167,8 @@ func update(screen *ebiten.Image) error { op.GeoM.Translate(dx, dy) screen.DrawImage(gophersImage, op) - x, y := ebiten.CursorPosition() + wx, wy := ebiten.WindowPosition() + cx, cy := ebiten.CursorPosition() tpsStr := "Uncapped" if t := ebiten.MaxTPS(); t != ebiten.UncappedTPS { tpsStr = fmt.Sprintf("%d", t) @@ -181,10 +182,11 @@ Press I key to change the window icon (only for desktops) Press V key to switch vsync Press T key to switch TPS (ticks per second) Press D key to switch the window decoration +Windows Position: (%d, %d) Cursor: (%d, %d) TPS: Current: %0.2f / Max: %s FPS: %0.2f -Device Scale Factor: %0.2f`, x, y, ebiten.CurrentTPS(), tpsStr, ebiten.CurrentFPS(), ebiten.DeviceScaleFactor()) +Device Scale Factor: %0.2f`, wx, wy, cx, cy, ebiten.CurrentTPS(), tpsStr, ebiten.CurrentFPS(), ebiten.DeviceScaleFactor()) ebitenutil.DebugPrint(screen, msg) return nil } diff --git a/internal/driver/ui.go b/internal/driver/ui.go index d18d7aacc..2e4f0f9f3 100644 --- a/internal/driver/ui.go +++ b/internal/driver/ui.go @@ -32,6 +32,9 @@ type UIContext interface { var RegularTermination = errors.New("regular termination") type UI interface { + Run(width, height int, scale float64, title string, context UIContext, graphics Graphics) error + RunWithoutMainLoop(width, height int, scale float64, title string, context UIContext, graphics Graphics) <-chan error + DeviceScaleFactor() float64 IsCursorVisible() bool IsFullscreen() bool @@ -39,11 +42,11 @@ type UI interface { IsVsyncEnabled() bool IsWindowDecorated() bool IsWindowResizable() bool - Run(width, height int, scale float64, title string, context UIContext, graphics Graphics) error - RunWithoutMainLoop(width, height int, scale float64, title string, context UIContext, graphics Graphics) <-chan error ScreenPadding() (x0, y0, x1, y1 float64) ScreenScale() float64 ScreenSizeInFullscreen() (int, int) + WindowPosition() (int, int) + SetCursorVisible(visible bool) SetFullscreen(fullscreen bool) SetRunnableInBackground(runnableInBackground bool) @@ -54,5 +57,6 @@ type UI interface { SetWindowIcon(iconImages []image.Image) SetWindowResizable(resizable bool) SetWindowTitle(title string) + Input() Input } diff --git a/internal/uidriver/glfw/ui.go b/internal/uidriver/glfw/ui.go index b231ec34c..98c311e9e 100644 --- a/internal/uidriver/glfw/ui.go +++ b/internal/uidriver/glfw/ui.go @@ -997,6 +997,18 @@ func (u *UserInterface) currentMonitor() *glfw.Monitor { return u.currentMonitorFromPosition() } +func (u *UserInterface) WindowPosition() (int, int) { + if !u.isRunning() { + panic("ui: Run is not called yet") + } + x, y := 0, 0 + _ = u.t.Call(func() error { + x, y = u.window.GetPos() + return nil + }) + return x, y +} + func (u *UserInterface) Input() driver.Input { return &u.input } diff --git a/internal/uidriver/js/ui.go b/internal/uidriver/js/ui.go index 1c8cbf7ed..cdc83b31c 100644 --- a/internal/uidriver/js/ui.go +++ b/internal/uidriver/js/ui.go @@ -479,6 +479,10 @@ func (u *UserInterface) updateScreenSize() { u.sizeChanged = true } +func (u *UserInterface) WindowPosition() (int, int) { + return 0, 0 +} + func (u *UserInterface) Input() driver.Input { return &u.input } diff --git a/internal/uidriver/mobile/ui.go b/internal/uidriver/mobile/ui.go index 25c33323d..4c6776a27 100644 --- a/internal/uidriver/mobile/ui.go +++ b/internal/uidriver/mobile/ui.go @@ -443,6 +443,10 @@ func (u *UserInterface) DeviceScaleFactor() float64 { return deviceScale() } +func (u *UserInterface) WindowPosition() (int, int) { + return 0, 0 +} + func (u *UserInterface) Input() driver.Input { return &u.input } diff --git a/window.go b/window.go index f9307c39b..496ae5517 100644 --- a/window.go +++ b/window.go @@ -94,3 +94,12 @@ func SetWindowTitle(title string) { func SetWindowIcon(iconImages []image.Image) { uiDriver().SetWindowIcon(iconImages) } + +// WindowPosition returns the window position. +// +// WindowPosition panics before Run is called. +// +// WindowPosition returns (0, 0) on browsers and mobiles. +func WindowPosition() (int, int) { + return uiDriver().WindowPosition() +}