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

View File

@ -36,6 +36,7 @@ type userInterface struct {
scale float64
deviceScale float64
glfwScale float64
fullscreen bool
funcs chan func()
running bool
sizeChanged bool
@ -129,7 +130,7 @@ func SetScreenSize(width, height int) bool {
}
r := false
_ = u.runOnMainThread(func() error {
r = u.setScreenSize(width, height, u.scale)
r = u.setScreenSize(width, height, u.scale, u.fullscreen)
return nil
})
return r
@ -142,7 +143,20 @@ func SetScreenScale(scale float64) bool {
}
r := false
_ = 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 r
@ -161,6 +175,19 @@ func ScreenScale() float64 {
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) {
// This can be called before Run: change the state asyncly.
go func() {
@ -183,7 +210,7 @@ func Run(width, height int, scale float64, title string, g GraphicsContext) erro
if err := u.runOnMainThread(func() error {
m := glfw.GetPrimaryMonitor()
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")
}
u.window.SetTitle(title)
@ -290,8 +317,8 @@ func (u *userInterface) swapBuffers() {
u.window.SwapBuffers()
}
func (u *userInterface) setScreenSize(width, height int, scale float64) bool {
if u.width == width && u.height == height && u.scale == scale {
func (u *userInterface) setScreenSize(width, height int, scale float64, fullscreen bool) bool {
if u.width == width && u.height == height && u.scale == scale && u.fullscreen == fullscreen {
return false
}
@ -314,16 +341,23 @@ func (u *userInterface) setScreenSize(width, height int, scale float64) bool {
// swap buffers here before SetSize is called.
u.swapBuffers()
ch := make(chan struct{})
u.fullscreen = fullscreen
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(nil)
close(ch)
})
w, h := u.glfwSize()
window.SetSize(w, h)
event:
event:
for {
glfw.PollEvents()
select {
@ -332,6 +366,8 @@ event:
default:
}
}
}
// TODO: Rename this variable?
u.sizeChanged = true
return true
}

View File

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

View File

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

8
run.go
View File

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