ui: Add WindowPosition()

Fixes #936
This commit is contained in:
Hajime Hoshi 2019-11-30 22:37:53 +09:00
parent 5ecd010f5a
commit c021d6be6a
6 changed files with 39 additions and 4 deletions

View File

@ -167,7 +167,8 @@ func update(screen *ebiten.Image) error {
op.GeoM.Translate(dx, dy) op.GeoM.Translate(dx, dy)
screen.DrawImage(gophersImage, op) screen.DrawImage(gophersImage, op)
x, y := ebiten.CursorPosition() wx, wy := ebiten.WindowPosition()
cx, cy := ebiten.CursorPosition()
tpsStr := "Uncapped" tpsStr := "Uncapped"
if t := ebiten.MaxTPS(); t != ebiten.UncappedTPS { if t := ebiten.MaxTPS(); t != ebiten.UncappedTPS {
tpsStr = fmt.Sprintf("%d", t) 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 V key to switch vsync
Press T key to switch TPS (ticks per second) Press T key to switch TPS (ticks per second)
Press D key to switch the window decoration Press D key to switch the window decoration
Windows Position: (%d, %d)
Cursor: (%d, %d) Cursor: (%d, %d)
TPS: Current: %0.2f / Max: %s TPS: Current: %0.2f / Max: %s
FPS: %0.2f 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) ebitenutil.DebugPrint(screen, msg)
return nil return nil
} }

View File

@ -32,6 +32,9 @@ type UIContext interface {
var RegularTermination = errors.New("regular termination") var RegularTermination = errors.New("regular termination")
type UI interface { 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 DeviceScaleFactor() float64
IsCursorVisible() bool IsCursorVisible() bool
IsFullscreen() bool IsFullscreen() bool
@ -39,11 +42,11 @@ type UI interface {
IsVsyncEnabled() bool IsVsyncEnabled() bool
IsWindowDecorated() bool IsWindowDecorated() bool
IsWindowResizable() 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) ScreenPadding() (x0, y0, x1, y1 float64)
ScreenScale() float64 ScreenScale() float64
ScreenSizeInFullscreen() (int, int) ScreenSizeInFullscreen() (int, int)
WindowPosition() (int, int)
SetCursorVisible(visible bool) SetCursorVisible(visible bool)
SetFullscreen(fullscreen bool) SetFullscreen(fullscreen bool)
SetRunnableInBackground(runnableInBackground bool) SetRunnableInBackground(runnableInBackground bool)
@ -54,5 +57,6 @@ type UI interface {
SetWindowIcon(iconImages []image.Image) SetWindowIcon(iconImages []image.Image)
SetWindowResizable(resizable bool) SetWindowResizable(resizable bool)
SetWindowTitle(title string) SetWindowTitle(title string)
Input() Input Input() Input
} }

View File

@ -997,6 +997,18 @@ func (u *UserInterface) currentMonitor() *glfw.Monitor {
return u.currentMonitorFromPosition() 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 { func (u *UserInterface) Input() driver.Input {
return &u.input return &u.input
} }

View File

@ -479,6 +479,10 @@ func (u *UserInterface) updateScreenSize() {
u.sizeChanged = true u.sizeChanged = true
} }
func (u *UserInterface) WindowPosition() (int, int) {
return 0, 0
}
func (u *UserInterface) Input() driver.Input { func (u *UserInterface) Input() driver.Input {
return &u.input return &u.input
} }

View File

@ -443,6 +443,10 @@ func (u *UserInterface) DeviceScaleFactor() float64 {
return deviceScale() return deviceScale()
} }
func (u *UserInterface) WindowPosition() (int, int) {
return 0, 0
}
func (u *UserInterface) Input() driver.Input { func (u *UserInterface) Input() driver.Input {
return &u.input return &u.input
} }

View File

@ -94,3 +94,12 @@ func SetWindowTitle(title string) {
func SetWindowIcon(iconImages []image.Image) { func SetWindowIcon(iconImages []image.Image) {
uiDriver().SetWindowIcon(iconImages) 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()
}