ui: Add SetWindowPosition(x, y int)

Fixes #936
This commit is contained in:
Hajime Hoshi 2019-11-30 23:22:23 +09:00
parent c021d6be6a
commit 9b2f864fc8
6 changed files with 122 additions and 31 deletions

View File

@ -18,6 +18,7 @@ package main
import ( import (
"bytes" "bytes"
"flag"
"fmt" "fmt"
"image" "image"
"image/color" "image/color"
@ -25,6 +26,8 @@ import (
"log" "log"
"math" "math"
"math/rand" "math/rand"
"strconv"
"strings"
"time" "time"
"github.com/hajimehoshi/ebiten" "github.com/hajimehoshi/ebiten"
@ -33,7 +36,12 @@ import (
"github.com/hajimehoshi/ebiten/inpututil" "github.com/hajimehoshi/ebiten/inpututil"
) )
var (
flagWindowPosition = flag.String("windowposition", "", "window position (e.g., 100,200)")
)
func init() { func init() {
flag.Parse()
rand.Seed(time.Now().UnixNano()) rand.Seed(time.Now().UnixNano())
} }
@ -77,7 +85,9 @@ func update(screen *ebiten.Image) error {
vsyncEnabled := ebiten.IsVsyncEnabled() vsyncEnabled := ebiten.IsVsyncEnabled()
tps := ebiten.MaxTPS() tps := ebiten.MaxTPS()
decorated := ebiten.IsWindowDecorated() decorated := ebiten.IsWindowDecorated()
positionX, positionY := ebiten.WindowPosition()
if ebiten.IsKeyPressed(ebiten.KeyShift) {
if inpututil.IsKeyJustPressed(ebiten.KeyUp) { if inpututil.IsKeyJustPressed(ebiten.KeyUp) {
screenHeight += d screenHeight += d
} }
@ -94,6 +104,20 @@ func update(screen *ebiten.Image) error {
if inpututil.IsKeyJustPressed(ebiten.KeyRight) { if inpututil.IsKeyJustPressed(ebiten.KeyRight) {
screenWidth += d screenWidth += d
} }
} else {
if inpututil.IsKeyJustPressed(ebiten.KeyUp) {
positionY -= 4
}
if inpututil.IsKeyJustPressed(ebiten.KeyDown) {
positionY += 4
}
if inpututil.IsKeyJustPressed(ebiten.KeyLeft) {
positionX -= 4
}
if inpututil.IsKeyJustPressed(ebiten.KeyRight) {
positionX += 4
}
}
if inpututil.IsKeyJustPressed(ebiten.KeyS) { if inpututil.IsKeyJustPressed(ebiten.KeyS) {
switch screenScale { switch screenScale {
case 0.75: case 0.75:
@ -146,6 +170,7 @@ func update(screen *ebiten.Image) error {
ebiten.SetVsyncEnabled(vsyncEnabled) ebiten.SetVsyncEnabled(vsyncEnabled)
ebiten.SetMaxTPS(tps) ebiten.SetMaxTPS(tps)
ebiten.SetWindowDecorated(decorated) ebiten.SetWindowDecorated(decorated)
ebiten.SetWindowPosition(positionX, positionY)
if inpututil.IsKeyJustPressed(ebiten.KeyI) { if inpututil.IsKeyJustPressed(ebiten.KeyI) {
ebiten.SetWindowIcon([]image.Image{createRandomIconImage()}) ebiten.SetWindowIcon([]image.Image{createRandomIconImage()})
@ -173,7 +198,7 @@ func update(screen *ebiten.Image) error {
if t := ebiten.MaxTPS(); t != ebiten.UncappedTPS { if t := ebiten.MaxTPS(); t != ebiten.UncappedTPS {
tpsStr = fmt.Sprintf("%d", t) tpsStr = fmt.Sprintf("%d", t)
} }
msg := fmt.Sprintf(`Press arrow keys to change the window size msg := fmt.Sprintf(`Press shift + arrow keys to change the window size
Press S key to change the window scale (only for desktops) Press S key to change the window scale (only for desktops)
Press F key to switch the fullscreen state (only for desktops) Press F key to switch the fullscreen state (only for desktops)
Press B key to switch the run-in-background state Press B key to switch the run-in-background state
@ -191,6 +216,25 @@ Device Scale Factor: %0.2f`, wx, wy, cx, cy, ebiten.CurrentTPS(), tpsStr, ebiten
return nil return nil
} }
func parseWindowPosition() (int, int, bool) {
if *flagWindowPosition == "" {
return 0, 0, false
}
tokens := strings.Split(*flagWindowPosition, ",")
if len(tokens) != 2 {
return 0, 0, false
}
x, err := strconv.Atoi(tokens[0])
if err != nil {
return 0, 0, false
}
y, err := strconv.Atoi(tokens[1])
if err != nil {
return 0, 0, false
}
return x, y, true
}
func main() { func main() {
fmt.Printf("Device scale factor: %0.2f\n", ebiten.DeviceScaleFactor()) fmt.Printf("Device scale factor: %0.2f\n", ebiten.DeviceScaleFactor())
w, h := ebiten.ScreenSizeInFullscreen() w, h := ebiten.ScreenSizeInFullscreen()
@ -213,6 +257,10 @@ func main() {
ebiten.SetWindowIcon([]image.Image{createRandomIconImage()}) ebiten.SetWindowIcon([]image.Image{createRandomIconImage()})
if x, y, ok := parseWindowPosition(); ok {
ebiten.SetWindowPosition(x, y)
}
if err := ebiten.Run(update, initScreenWidth, initScreenHeight, initScreenScale, "Window Size (Ebiten Demo)"); err != nil { if err := ebiten.Run(update, initScreenWidth, initScreenHeight, initScreenScale, "Window Size (Ebiten Demo)"); err != nil {
log.Fatal(err) log.Fatal(err)
} }

View File

@ -57,6 +57,7 @@ type UI interface {
SetWindowIcon(iconImages []image.Image) SetWindowIcon(iconImages []image.Image)
SetWindowResizable(resizable bool) SetWindowResizable(resizable bool)
SetWindowTitle(title string) SetWindowTitle(title string)
SetWindowPosition(x, y int)
Input() Input Input() Input
} }

View File

@ -41,10 +41,6 @@ type UserInterface struct {
width int width int
windowWidth int windowWidth int
height int height int
initMonitor *glfw.Monitor
initFullscreenWidth int
initFullscreenHeight int
scale float64 scale float64
fullscreenScale float64 fullscreenScale float64
@ -57,10 +53,15 @@ type UserInterface struct {
lastActualScale float64 lastActualScale float64
initMonitor *glfw.Monitor
initFullscreenWidth int
initFullscreenHeight int
initFullscreen bool initFullscreen bool
initCursorVisible bool initCursorVisible bool
initWindowDecorated bool initWindowDecorated bool
initWindowResizable bool initWindowResizable bool
initWindowPositionX *int
initWindowPositionY *int
initIconImages []image.Image initIconImages []image.Image
reqWidth int reqWidth int
@ -667,8 +668,14 @@ func (u *UserInterface) run(width, height int, scale float64, title string, cont
u.window.SetTitle(title) u.window.SetTitle(title)
u.window.Show() u.window.Show()
x := mx + (v.Width-w)/2 x, y := 0, 0
y := my + (v.Height-h)/3 if u.initWindowPositionX != nil && u.initWindowPositionY != nil {
x = *u.initWindowPositionX
y = *u.initWindowPositionY
} else {
x = mx + (v.Width-w)/2
y = my + (v.Height-h)/3
}
// Adjusting the position is needed only when the monitor is primary. (#829) // Adjusting the position is needed only when the monitor is primary. (#829)
if mx == 0 && my == 0 { if mx == 0 && my == 0 {
x, y = adjustWindowPosition(x, y) x, y = adjustWindowPosition(x, y)
@ -997,6 +1004,24 @@ func (u *UserInterface) currentMonitor() *glfw.Monitor {
return u.currentMonitorFromPosition() return u.currentMonitorFromPosition()
} }
func (u *UserInterface) SetWindowPosition(x, y int) {
if !u.isRunning() {
if u.initWindowPositionX == nil {
u.initWindowPositionX = new(int)
}
if u.initWindowPositionY == nil {
u.initWindowPositionY = new(int)
}
*u.initWindowPositionX = x
*u.initWindowPositionY = y
return
}
_ = u.t.Call(func() error {
u.window.SetPos(x, y)
return nil
})
}
func (u *UserInterface) WindowPosition() (int, int) { func (u *UserInterface) WindowPosition() (int, int) {
if !u.isRunning() { if !u.isRunning() {
panic("ui: Run is not called yet") panic("ui: Run is not called yet")

View File

@ -479,6 +479,10 @@ func (u *UserInterface) updateScreenSize() {
u.sizeChanged = true u.sizeChanged = true
} }
func (u *UserInterface) SetWindowPosition(x, y int) {
// Do nothing
}
func (u *UserInterface) WindowPosition() (int, int) { func (u *UserInterface) WindowPosition() (int, int) {
return 0, 0 return 0, 0
} }

View File

@ -443,6 +443,10 @@ func (u *UserInterface) DeviceScaleFactor() float64 {
return deviceScale() return deviceScale()
} }
func (u *UserInterface) SetWindowPosition(x, y int) {
// Do nothing
}
func (u *UserInterface) WindowPosition() (int, int) { func (u *UserInterface) WindowPosition() (int, int) {
return 0, 0 return 0, 0
} }

View File

@ -100,6 +100,15 @@ func SetWindowIcon(iconImages []image.Image) {
// WindowPosition panics before Run is called. // WindowPosition panics before Run is called.
// //
// WindowPosition returns (0, 0) on browsers and mobiles. // WindowPosition returns (0, 0) on browsers and mobiles.
func WindowPosition() (int, int) { func WindowPosition() (x, y int) {
return uiDriver().WindowPosition() return uiDriver().WindowPosition()
} }
// SetWindowPosition sets the window position.
//
// SetWindowPosition works before and after Run is called.
//
// SetWindowPosition does nothing on browsers and mobiles.
func SetWindowPosition(x, y int) {
uiDriver().SetWindowPosition(x, y)
}