From 25ae96db89474d3cb7046ee6d2a522bd30bd8e59 Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Fri, 14 Oct 2022 01:58:03 +0900 Subject: [PATCH] internal/ui: move screenScaleAndOffsets to the ebiten package Updates #2046 --- gameforui.go | 20 +++++++++++++++++++- internal/ui/context.go | 22 ++++------------------ 2 files changed, 23 insertions(+), 19 deletions(-) diff --git a/gameforui.go b/gameforui.go index a3509b605..f43ed0fce 100644 --- a/gameforui.go +++ b/gameforui.go @@ -136,7 +136,8 @@ func (g *gameForUI) DrawOffscreen() { g.game.Draw(g.offscreen) } -func (g *gameForUI) DrawScreen(scale, offsetX, offsetY float64) { +func (g *gameForUI) DrawScreen() { + scale, offsetX, offsetY := g.ScreenScaleAndOffsets() switch { case !isScreenFilterEnabled(), math.Floor(scale) == scale: op := &DrawImageOptions{} @@ -158,3 +159,20 @@ func (g *gameForUI) DrawScreen(scale, offsetX, offsetY float64) { g.screen.DrawRectShader(w, h, g.screenShader, op) } } + +func (g *gameForUI) ScreenScaleAndOffsets() (scale, offsetX, offsetY float64) { + if g.screen == nil { + return + } + + sw, sh := g.screen.Size() + ow, oh := g.offscreen.Size() + scaleX := float64(sw) / float64(ow) + scaleY := float64(sh) / float64(oh) + scale = math.Min(scaleX, scaleY) + width := float64(ow) * scale + height := float64(oh) * scale + offsetX = (float64(sw) - width) / 2 + offsetY = (float64(sh) - height) / 2 + return +} diff --git a/internal/ui/context.go b/internal/ui/context.go index 12842a88a..708552a99 100644 --- a/internal/ui/context.go +++ b/internal/ui/context.go @@ -39,7 +39,8 @@ type Game interface { Layout(outsideWidth, outsideHeight int) (int, int) Update() error DrawOffscreen() - DrawScreen(scale, offsetX, offsteY float64) + DrawScreen() + ScreenScaleAndOffsets() (scale, offsetX, offsetY float64) } type context struct { @@ -169,7 +170,7 @@ func (c *context) drawGame(graphicsDriver graphicsdriver.Graphics) { c.screen.clear() } - c.game.DrawScreen(c.screenScaleAndOffsets()) + c.game.DrawScreen() } func (c *context) layoutGame(outsideWidth, outsideHeight float64, deviceScaleFactor float64) (int, int) { @@ -215,7 +216,7 @@ func (c *context) layoutGame(outsideWidth, outsideHeight float64, deviceScaleFac } func (c *context) adjustPosition(x, y float64, deviceScaleFactor float64) (float64, float64) { - s, ox, oy := c.screenScaleAndOffsets() + s, ox, oy := c.game.ScreenScaleAndOffsets() // The scale 0 indicates that the screen is not initialized yet. // As any cursor values don't make sense, just return NaN. if s == 0 { @@ -224,21 +225,6 @@ func (c *context) adjustPosition(x, y float64, deviceScaleFactor float64) (float return (x*deviceScaleFactor - ox) / s, (y*deviceScaleFactor - oy) / s } -func (c *context) screenScaleAndOffsets() (float64, float64, float64) { - if c.screen == nil { - return 0, 0, 0 - } - - scaleX := float64(c.screen.width) / float64(c.offscreen.width) - scaleY := float64(c.screen.height) / float64(c.offscreen.height) - scale := math.Min(scaleX, scaleY) - width := float64(c.offscreen.width) * scale - height := float64(c.offscreen.height) * scale - x := (float64(c.screen.width) - width) / 2 - y := (float64(c.screen.height) - height) / 2 - return scale, x, y -} - var theGlobalState = globalState{ isScreenClearedEveryFrame_: 1, }