ui: Add SetFullscreen / IsFullscreen (#267)

This commit is contained in:
Hajime Hoshi 2017-06-30 00:35:34 +09:00
parent d0f086323b
commit b569eb9c5a
5 changed files with 92 additions and 21 deletions

View File

@ -40,6 +40,7 @@ var (
ebiten.KeyLeft: 0, ebiten.KeyLeft: 0,
ebiten.KeyRight: 0, ebiten.KeyRight: 0,
ebiten.KeyS: 0, ebiten.KeyS: 0,
ebiten.KeyF: 0,
} }
) )
@ -53,8 +54,9 @@ func update(screen *ebiten.Image) error {
} }
screenScale := ebiten.ScreenScale() screenScale := ebiten.ScreenScale()
d := int(32 / screenScale) d := int(32 / screenScale)
screenWidth, screenHeight := screen.Size() screenWidth, screenHeight := screen.Size()
fullscreen := ebiten.IsFullscreen()
if keyStates[ebiten.KeyUp] == 1 { if keyStates[ebiten.KeyUp] == 1 {
screenHeight += d screenHeight += d
} }
@ -83,8 +85,12 @@ func update(screen *ebiten.Image) error {
panic("not reach") panic("not reach")
} }
} }
if keyStates[ebiten.KeyF] == 1 {
fullscreen = !fullscreen
}
ebiten.SetScreenSize(screenWidth, screenHeight) ebiten.SetScreenSize(screenWidth, screenHeight)
ebiten.SetScreenScale(screenScale) ebiten.SetScreenScale(screenScale)
ebiten.SetFullscreen(fullscreen)
if ebiten.IsRunningSlowly() { if ebiten.IsRunningSlowly() {
return nil return nil
@ -100,6 +106,7 @@ func update(screen *ebiten.Image) error {
x, y := ebiten.CursorPosition() x, y := ebiten.CursorPosition()
msg := fmt.Sprintf(`Press arrow keys to change the window size msg := fmt.Sprintf(`Press arrow keys to change the window size
Press S key to change the window scale Press S key to change the window scale
Press F key to change the fullscreen state
Cursor: (%d, %d) Cursor: (%d, %d)
FPS: %0.2f`, x, y, ebiten.CurrentFPS()) FPS: %0.2f`, x, y, ebiten.CurrentFPS())
ebitenutil.DebugPrint(screen, msg) ebitenutil.DebugPrint(screen, msg)

View File

@ -36,6 +36,7 @@ type userInterface struct {
scale float64 scale float64
deviceScale float64 deviceScale float64
glfwScale float64 glfwScale float64
fullscreen bool
funcs chan func() funcs chan func()
running bool running bool
sizeChanged bool sizeChanged bool
@ -129,7 +130,7 @@ func SetScreenSize(width, height int) bool {
} }
r := false r := false
_ = u.runOnMainThread(func() error { _ = u.runOnMainThread(func() error {
r = u.setScreenSize(width, height, u.scale) r = u.setScreenSize(width, height, u.scale, u.fullscreen)
return nil return nil
}) })
return r return r
@ -142,7 +143,20 @@ func SetScreenScale(scale float64) bool {
} }
r := false r := false
_ = u.runOnMainThread(func() error { _ = u.runOnMainThread(func() error {
r = u.setScreenSize(u.width, u.height, scale) r = u.setScreenSize(u.width, u.height, scale, u.fullscreen)
return nil
})
return r
}
func SetFullscreen(fullscreen bool) bool {
u := currentUI
if !u.isRunning() {
panic("ui: Run is not called yet")
}
r := false
_ = u.runOnMainThread(func() error {
r = u.setScreenSize(u.width, u.height, u.scale, fullscreen)
return nil return nil
}) })
return r return r
@ -161,6 +175,19 @@ func ScreenScale() float64 {
return s return s
} }
func IsFullscreen() bool {
u := currentUI
if !u.isRunning() {
return false
}
f := false
_ = u.runOnMainThread(func() error {
f = u.fullscreen
return nil
})
return f
}
func SetCursorVisibility(visible bool) { func SetCursorVisibility(visible bool) {
// This can be called before Run: change the state asyncly. // This can be called before Run: change the state asyncly.
go func() { go func() {
@ -183,7 +210,7 @@ func Run(width, height int, scale float64, title string, g GraphicsContext) erro
if err := u.runOnMainThread(func() error { if err := u.runOnMainThread(func() error {
m := glfw.GetPrimaryMonitor() m := glfw.GetPrimaryMonitor()
v := m.GetVideoMode() v := m.GetVideoMode()
if !u.setScreenSize(width, height, scale) { if !u.setScreenSize(width, height, scale, false) {
return errors.New("ui: Fail to set the screen size") return errors.New("ui: Fail to set the screen size")
} }
u.window.SetTitle(title) u.window.SetTitle(title)
@ -290,8 +317,8 @@ func (u *userInterface) swapBuffers() {
u.window.SwapBuffers() u.window.SwapBuffers()
} }
func (u *userInterface) setScreenSize(width, height int, scale float64) bool { func (u *userInterface) setScreenSize(width, height int, scale float64, fullscreen bool) bool {
if u.width == width && u.height == height && u.scale == scale { if u.width == width && u.height == height && u.scale == scale && u.fullscreen == fullscreen {
return false return false
} }
@ -314,15 +341,22 @@ func (u *userInterface) setScreenSize(width, height int, scale float64) bool {
// swap buffers here before SetSize is called. // swap buffers here before SetSize is called.
u.swapBuffers() u.swapBuffers()
ch := make(chan struct{}) u.fullscreen = fullscreen
window := u.window window := u.window
m := glfw.GetPrimaryMonitor()
v := m.GetVideoMode()
if u.fullscreen {
window.SetMonitor(m, 0, 0, v.Width, v.Height, v.RefreshRate)
} else {
window.SetMonitor(nil, 0, 0, 16, 16, v.RefreshRate)
ch := make(chan struct{})
window.SetFramebufferSizeCallback(func(_ *glfw.Window, width, height int) { window.SetFramebufferSizeCallback(func(_ *glfw.Window, width, height int) {
window.SetFramebufferSizeCallback(nil) window.SetFramebufferSizeCallback(nil)
close(ch) close(ch)
}) })
w, h := u.glfwSize() w, h := u.glfwSize()
window.SetSize(w, h) window.SetSize(w, h)
event: event:
for { for {
glfw.PollEvents() glfw.PollEvents()
@ -332,6 +366,8 @@ event:
default: default:
} }
} }
}
// TODO: Rename this variable?
u.sizeChanged = true u.sizeChanged = true
return true return true
} }

View File

@ -56,6 +56,16 @@ func ScreenScale() float64 {
return currentUI.scale return currentUI.scale
} }
func SetFullscreen(fullscreen bool) bool {
// TODO: Implement
return false
}
func IsFullscreen() bool {
// TODO: Implement
return false
}
func SetCursorVisibility(visibility bool) { func SetCursorVisibility(visibility bool) {
if visibility { if visibility {
canvas.Get("style").Set("cursor", "auto") canvas.Get("style").Set("cursor", "auto")

View File

@ -110,6 +110,16 @@ func SetCursorVisibility(visibility bool) {
// Do nothing // Do nothing
} }
func SetFullscreen(fullscreen bool) bool {
// Do nothing
return false
}
func IsFullscreen() bool {
// Do nothing
return false
}
func (u *userInterface) actualScreenScale() float64 { func (u *userInterface) actualScreenScale() float64 {
return u.scale * deviceScale() return u.scale * deviceScale()
} }

8
run.go
View File

@ -149,3 +149,11 @@ func ScreenScale() float64 {
func SetCursorVisibility(visible bool) { func SetCursorVisibility(visible bool) {
ui.SetCursorVisibility(visible) ui.SetCursorVisibility(visible)
} }
func IsFullscreen() bool {
return ui.IsFullscreen()
}
func SetFullscreen(fullscreen bool) {
ui.SetFullscreen(fullscreen)
}