ebiten: add FloatLayouter

Closes #2285
This commit is contained in:
Hajime Hoshi 2022-11-09 01:49:51 +09:00
parent 0d6b42fedd
commit 8567c3c654
3 changed files with 25 additions and 5 deletions

View File

@ -90,12 +90,18 @@ func createRandomIconImage() image.Image {
type game struct { type game struct {
count int count int
width int width float64
height int height float64
transparent bool transparent bool
} }
func (g *game) Layout(outsideWidth, outsideHeight int) (int, int) { func (g *game) Layout(outsideWidth, outsideHeight int) (int, int) {
// As game implements the interface FloatLayouter, Layout is never called and LayoutF is called instead.
// However, game has to implement Layout to satisfy the interface Game.
panic("windowsize: Layout must not be called")
}
func (g *game) LayoutF(outsideWidth, outsideHeight float64) (float64, float64) {
if *flagAutoAdjusting { if *flagAutoAdjusting {
g.width, g.height = outsideWidth, outsideHeight g.width, g.height = outsideWidth, outsideHeight
return outsideWidth, outsideHeight return outsideWidth, outsideHeight
@ -106,14 +112,14 @@ func (g *game) Layout(outsideWidth, outsideHeight int) (int, int) {
func (g *game) Update() error { func (g *game) Update() error {
var ( var (
screenWidth int screenWidth float64
screenHeight int screenHeight float64
screenScale float64 screenScale float64
) )
screenWidth = g.width screenWidth = g.width
screenHeight = g.height screenHeight = g.height
if ww, wh := ebiten.WindowSize(); ww > 0 && wh > 0 { if ww, wh := ebiten.WindowSize(); ww > 0 && wh > 0 {
screenScale = math.Min(float64(ww)/float64(g.width), float64(wh)/float64(g.height)) screenScale = math.Min(float64(ww)/g.width, float64(wh)/g.height)
} else { } else {
// ebiten.WindowSize can return (0, 0) on browsers or mobiles. // ebiten.WindowSize can return (0, 0) on browsers or mobiles.
screenScale = 1 screenScale = 1

View File

@ -126,6 +126,10 @@ func (g *gameForUI) NewScreenImage(width, height int) *ui.Image {
} }
func (g *gameForUI) Layout(outsideWidth, outsideHeight float64) (float64, float64) { func (g *gameForUI) Layout(outsideWidth, outsideHeight float64) (float64, float64) {
if l, ok := g.game.(FloatLayouter); ok {
return l.LayoutF(outsideWidth, outsideHeight)
}
// Even if the original value is less than 1, the value must be a positive integer (#2340). // Even if the original value is less than 1, the value must be a positive integer (#2340).
// This is for a simple implementation of Layout, which returns the argument values without modifications. // This is for a simple implementation of Layout, which returns the argument values without modifications.
// TODO: Remove this hack when Game.Layout takes floats instead of integers. // TODO: Remove this hack when Game.Layout takes floats instead of integers.

10
run.go
View File

@ -79,9 +79,19 @@ type Game interface {
// //
// You can return a fixed screen size if you don't care, or you can also return a calculated screen size // You can return a fixed screen size if you don't care, or you can also return a calculated screen size
// adjusted with the given outside size. // adjusted with the given outside size.
//
// If the game implements the interface FloatLayouter, Layout is never called and LayoutF is called instead.
Layout(outsideWidth, outsideHeight int) (screenWidth, screenHeight int) Layout(outsideWidth, outsideHeight int) (screenWidth, screenHeight int)
} }
// FloatLayouter is an interface for the float version of Game.Layout.
type FloatLayouter interface {
// LayoutF is the float version of Game.Layout.
//
// If the game implements this interface, Layout is never called and LayoutF is called instead.
LayoutF(outsideWidth, outsideHeight float64) (screenWidth, screenHeight float64)
}
// FinalScreen represents the final screen image. // FinalScreen represents the final screen image.
// FinalScreen implements a part of Image functions. // FinalScreen implements a part of Image functions.
type FinalScreen interface { type FinalScreen interface {