From 664cf62a29791eb7bd983fbe2de581cf0f5945a5 Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Mon, 16 Dec 2019 02:10:58 +0900 Subject: [PATCH] ui: Reduce members from uiContext --- uicontext.go | 49 ++++++++++++++++++++++++++++++++----------------- 1 file changed, 32 insertions(+), 17 deletions(-) diff --git a/uicontext.go b/uicontext.go index 37c19fd4c..f9c22e3c4 100644 --- a/uicontext.go +++ b/uicontext.go @@ -66,10 +66,7 @@ type uiContext struct { game Game offscreen *Image screen *Image - screenScale float64 scaleForWindow float64 - offsetX float64 - offsetY float64 outsideSizeUpdated bool outsideWidth float64 @@ -156,17 +153,32 @@ func (c *uiContext) updateOffscreen() { d := uiDriver().DeviceScaleFactor() c.screen = newScreenFramebufferImage(int(c.outsideWidth*d), int(c.outsideHeight*d)) + if uiDriver().CanHaveWindow() && !uiDriver().IsFullscreen() { + c.setScaleForWindow(c.screenScale() / d) + } +} + +func (c *uiContext) screenScale() float64 { + if c.offscreen == nil { + return 0 + } + sw, sh := c.offscreen.Size() + d := uiDriver().DeviceScaleFactor() scaleX := c.outsideWidth / float64(sw) * d scaleY := c.outsideHeight / float64(sh) * d - c.screenScale = math.Min(scaleX, scaleY) - if uiDriver().CanHaveWindow() && !uiDriver().IsFullscreen() { - c.setScaleForWindow(c.screenScale / d) - } + return math.Min(scaleX, scaleY) +} - width := float64(sw) * c.screenScale - height := float64(sh) * c.screenScale - c.offsetX = (c.outsideWidth*d - width) / 2 - c.offsetY = (c.outsideHeight*d - height) / 2 +func (c *uiContext) offsets() (float64, float64) { + if c.offscreen == nil { + return 0, 0 + } + sw, sh := c.offscreen.Size() + d := uiDriver().DeviceScaleFactor() + s := c.screenScale() + width := float64(sw) * s + height := float64(sh) * s + return (c.outsideWidth*d - width) / 2, (c.outsideHeight*d - height) / 2 } func (c *uiContext) Update(afterFrameUpdate func()) error { @@ -201,25 +213,26 @@ func (c *uiContext) Update(afterFrameUpdate func()) error { op := &DrawImageOptions{} + s := c.screenScale() switch vd := graphicsDriver().VDirection(); vd { case driver.VDownward: // c.screen is special: its Y axis is down to up, // and the origin point is lower left. - op.GeoM.Scale(c.screenScale, -c.screenScale) + op.GeoM.Scale(s, -s) _, h := c.offscreen.Size() - op.GeoM.Translate(0, float64(h)*c.screenScale) + op.GeoM.Translate(0, float64(h)*s) case driver.VUpward: - op.GeoM.Scale(c.screenScale, c.screenScale) + op.GeoM.Scale(s, s) default: panic(fmt.Sprintf("ebiten: invalid v-direction: %d", vd)) } - op.GeoM.Translate(c.offsetX, c.offsetY) + op.GeoM.Translate(c.offsets()) op.CompositeMode = CompositeModeCopy // filterScreen works with >=1 scale, but does not well with <1 scale. // Use regular FilterLinear instead so far (#669). - if c.screenScale >= 1 { + if s >= 1 { op.Filter = filterScreen } else { op.Filter = FilterLinear @@ -234,5 +247,7 @@ func (c *uiContext) Update(afterFrameUpdate func()) error { func (c *uiContext) AdjustPosition(x, y float64) (float64, float64) { d := uiDriver().DeviceScaleFactor() - return (x*d - c.offsetX) / c.screenScale, (y*d - c.offsetY) / c.screenScale + ox, oy := c.offsets() + s := c.screenScale() + return (x*d - ox) / s, (y*d - oy) / s }