mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-12-26 11:48:55 +01:00
ui: Reduce members from uiContext
This commit is contained in:
parent
97ddef0047
commit
664cf62a29
49
uicontext.go
49
uicontext.go
@ -66,10 +66,7 @@ type uiContext struct {
|
|||||||
game Game
|
game Game
|
||||||
offscreen *Image
|
offscreen *Image
|
||||||
screen *Image
|
screen *Image
|
||||||
screenScale float64
|
|
||||||
scaleForWindow float64
|
scaleForWindow float64
|
||||||
offsetX float64
|
|
||||||
offsetY float64
|
|
||||||
|
|
||||||
outsideSizeUpdated bool
|
outsideSizeUpdated bool
|
||||||
outsideWidth float64
|
outsideWidth float64
|
||||||
@ -156,17 +153,32 @@ func (c *uiContext) updateOffscreen() {
|
|||||||
d := uiDriver().DeviceScaleFactor()
|
d := uiDriver().DeviceScaleFactor()
|
||||||
c.screen = newScreenFramebufferImage(int(c.outsideWidth*d), int(c.outsideHeight*d))
|
c.screen = newScreenFramebufferImage(int(c.outsideWidth*d), int(c.outsideHeight*d))
|
||||||
|
|
||||||
scaleX := c.outsideWidth / float64(sw) * d
|
|
||||||
scaleY := c.outsideHeight / float64(sh) * d
|
|
||||||
c.screenScale = math.Min(scaleX, scaleY)
|
|
||||||
if uiDriver().CanHaveWindow() && !uiDriver().IsFullscreen() {
|
if uiDriver().CanHaveWindow() && !uiDriver().IsFullscreen() {
|
||||||
c.setScaleForWindow(c.screenScale / d)
|
c.setScaleForWindow(c.screenScale() / d)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
width := float64(sw) * c.screenScale
|
func (c *uiContext) screenScale() float64 {
|
||||||
height := float64(sh) * c.screenScale
|
if c.offscreen == nil {
|
||||||
c.offsetX = (c.outsideWidth*d - width) / 2
|
return 0
|
||||||
c.offsetY = (c.outsideHeight*d - height) / 2
|
}
|
||||||
|
sw, sh := c.offscreen.Size()
|
||||||
|
d := uiDriver().DeviceScaleFactor()
|
||||||
|
scaleX := c.outsideWidth / float64(sw) * d
|
||||||
|
scaleY := c.outsideHeight / float64(sh) * d
|
||||||
|
return math.Min(scaleX, scaleY)
|
||||||
|
}
|
||||||
|
|
||||||
|
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 {
|
func (c *uiContext) Update(afterFrameUpdate func()) error {
|
||||||
@ -201,25 +213,26 @@ func (c *uiContext) Update(afterFrameUpdate func()) error {
|
|||||||
|
|
||||||
op := &DrawImageOptions{}
|
op := &DrawImageOptions{}
|
||||||
|
|
||||||
|
s := c.screenScale()
|
||||||
switch vd := graphicsDriver().VDirection(); vd {
|
switch vd := graphicsDriver().VDirection(); vd {
|
||||||
case driver.VDownward:
|
case driver.VDownward:
|
||||||
// c.screen is special: its Y axis is down to up,
|
// c.screen is special: its Y axis is down to up,
|
||||||
// and the origin point is lower left.
|
// and the origin point is lower left.
|
||||||
op.GeoM.Scale(c.screenScale, -c.screenScale)
|
op.GeoM.Scale(s, -s)
|
||||||
_, h := c.offscreen.Size()
|
_, h := c.offscreen.Size()
|
||||||
op.GeoM.Translate(0, float64(h)*c.screenScale)
|
op.GeoM.Translate(0, float64(h)*s)
|
||||||
case driver.VUpward:
|
case driver.VUpward:
|
||||||
op.GeoM.Scale(c.screenScale, c.screenScale)
|
op.GeoM.Scale(s, s)
|
||||||
default:
|
default:
|
||||||
panic(fmt.Sprintf("ebiten: invalid v-direction: %d", vd))
|
panic(fmt.Sprintf("ebiten: invalid v-direction: %d", vd))
|
||||||
}
|
}
|
||||||
|
|
||||||
op.GeoM.Translate(c.offsetX, c.offsetY)
|
op.GeoM.Translate(c.offsets())
|
||||||
op.CompositeMode = CompositeModeCopy
|
op.CompositeMode = CompositeModeCopy
|
||||||
|
|
||||||
// filterScreen works with >=1 scale, but does not well with <1 scale.
|
// filterScreen works with >=1 scale, but does not well with <1 scale.
|
||||||
// Use regular FilterLinear instead so far (#669).
|
// Use regular FilterLinear instead so far (#669).
|
||||||
if c.screenScale >= 1 {
|
if s >= 1 {
|
||||||
op.Filter = filterScreen
|
op.Filter = filterScreen
|
||||||
} else {
|
} else {
|
||||||
op.Filter = FilterLinear
|
op.Filter = FilterLinear
|
||||||
@ -234,5 +247,7 @@ func (c *uiContext) Update(afterFrameUpdate func()) error {
|
|||||||
|
|
||||||
func (c *uiContext) AdjustPosition(x, y float64) (float64, float64) {
|
func (c *uiContext) AdjustPosition(x, y float64) (float64, float64) {
|
||||||
d := uiDriver().DeviceScaleFactor()
|
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
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user