Rename Rect to RectAbs and renamed RectSize to Rect.

This commit is contained in:
Sander Schobers 2020-05-12 13:55:30 +02:00
parent 8e7101e67d
commit c27d43e323
11 changed files with 69 additions and 27 deletions

View File

@ -30,7 +30,7 @@ func (b *ButtonBar) Arrange(ctx *Context, bounds Rectangle) {
}
offset := bounds.X
for i := range b.Buttons {
b.Buttons[i].Arrange(ctx, RectSize(offset, bounds.Y, length, bounds.H))
b.Buttons[i].Arrange(ctx, Rect(offset, bounds.Y, length, bounds.H))
offset += length
}
default:
@ -39,7 +39,7 @@ func (b *ButtonBar) Arrange(ctx *Context, bounds Rectangle) {
}
offset := bounds.Y
for i := range b.Buttons {
b.Buttons[i].Arrange(ctx, RectSize(bounds.X, offset, bounds.W, length))
b.Buttons[i].Arrange(ctx, Rect(bounds.X, offset, bounds.W, length))
offset += length
}
}

View File

@ -88,7 +88,7 @@ func (b *BuyFlowerButton) Render(ctx *Context) {
iconTexture := b.activeTexture(ctx)
pos := Pt(b.Bounds.X, b.Bounds.Y)
iconTexture.CopyResize(ctx.Renderer, RectSize(pos.X, pos.Y-60, b.Bounds.W, 120))
iconTexture.CopyResize(ctx.Renderer, Rect(pos.X, pos.Y-60, b.Bounds.W, 120))
if (b.IsMouseOver && !b.IsDisabled) || b.IsActive {
SetDrawColor(ctx.Renderer, TransparentWhite)
ctx.Renderer.FillRect(b.Bounds.SDLPtr())
@ -102,7 +102,7 @@ func (b *BuyFlowerButton) Render(ctx *Context) {
left := b.Bounds.W - 8 - b.hoverOffset
top := pos.Y + b.Bounds.H - 20
if left < 0 {
part := Rect(-left, 0, b.hoverTexture.Size().X, b.hoverTexture.Size().Y)
part := RectAbs(-left, 0, b.hoverTexture.Size().X, b.hoverTexture.Size().Y)
b.hoverTexture.CopyPart(ctx.Renderer, part, Pt(pos.X, top))
} else {
b.hoverTexture.Copy(ctx.Renderer, Pt(pos.X+left, top))

View File

@ -118,7 +118,7 @@ func run() error {
dialogs.ShowIntro(ctx)
w, h := window.GetSize()
app.Arrange(ctx, tins2020.Rect(0, 0, w, h))
app.Arrange(ctx, tins2020.RectAbs(0, 0, w, h))
for {
for event := sdl.PollEvent(); event != nil; event = sdl.PollEvent() {
@ -133,7 +133,7 @@ func run() error {
ctx.Settings.Window.Location = tins2020.PtPtr(x, y)
case sdl.WINDOWEVENT_SIZE_CHANGED:
w, h := window.GetSize()
app.Arrange(ctx, tins2020.Rect(0, 0, w, h))
app.Arrange(ctx, tins2020.RectAbs(0, 0, w, h))
ctx.Settings.Window.Size = tins2020.PtPtr(w, h)
}
}

42
conversion_test.go Normal file
View File

@ -0,0 +1,42 @@
package tins2020_test
import (
"math/rand"
"testing"
)
type native struct{ a, b, c, d int }
type similar struct{ a, b, c, d int }
func (s similar) toNative() native { return native{s.a, s.b, s.c, s.d} }
type wrapper struct{ native }
func (w wrapper) toNative() native { return w.native }
func nativeFunction(n native) int { return n.a + n.b + n.c + n.d }
func BenchmarkNative(b *testing.B) {
var sum int
for i := 0; i < b.N; i++ {
n := native{rand.Int(), rand.Int(), rand.Int(), rand.Int()}
sum += nativeFunction(n)
}
}
func BenchmarkSimilar(b *testing.B) {
var sum int
for i := 0; i < b.N; i++ {
s := similar{rand.Int(), rand.Int(), rand.Int(), rand.Int()}
sum += nativeFunction(s.toNative())
}
}
func BenchmarkWrapper(b *testing.B) {
var sum int
for i := 0; i < b.N; i++ {
w := wrapper{native{rand.Int(), rand.Int(), rand.Int(), rand.Int()}}
sum += nativeFunction(w.toNative())
}
}

View File

@ -77,10 +77,10 @@ func (c *GameControls) updateFlowerControls(ctx *Context) {
func (c *GameControls) Arrange(ctx *Context, bounds Rectangle) {
c.Bounds = bounds
c.menu.Arrange(ctx, RectSize(bounds.X, bounds.Y, buttonBarWidth, bounds.H))
c.top.Arrange(ctx, Rect(bounds.X+bounds.W/2+8, bounds.Y, bounds.Right(), bounds.Y+64))
c.flowers.Arrange(ctx, RectSize(bounds.Right()-buttonBarWidth, bounds.Y, buttonBarWidth, bounds.H))
c.otherTools.Arrange(ctx, RectSize(bounds.Right()-buttonBarWidth, bounds.Bottom()-2*buttonBarWidth, buttonBarWidth, 2*buttonBarWidth))
c.menu.Arrange(ctx, Rect(bounds.X, bounds.Y, buttonBarWidth, bounds.H))
c.top.Arrange(ctx, RectAbs(bounds.X+bounds.W/2+8, bounds.Y, bounds.Right(), bounds.Y+64))
c.flowers.Arrange(ctx, Rect(bounds.Right()-buttonBarWidth, bounds.Y, buttonBarWidth, bounds.H))
c.otherTools.Arrange(ctx, Rect(bounds.Right()-buttonBarWidth, bounds.Bottom()-2*buttonBarWidth, buttonBarWidth, 2*buttonBarWidth))
}
func (c *GameControls) Init(ctx *Context) error {
@ -185,7 +185,7 @@ func (c *GameControls) Handle(ctx *Context, event sdl.Event) bool {
func (c *GameControls) Render(ctx *Context) {
topBar := MustHexColor("#0000007f")
SetDrawColor(ctx.Renderer, topBar)
ctx.Renderer.FillRect(Rect(c.menu.Bounds.Right(), 0, c.flowers.Bounds.X, 64).SDLPtr())
ctx.Renderer.FillRect(RectAbs(c.menu.Bounds.Right(), 0, c.flowers.Bounds.X, 64).SDLPtr())
ctx.Fonts.Font("balance").RenderCopyAlign(ctx.Renderer, FmtMoney(c.game.Balance), Pt(c.top.Bounds.X-8, 58), MustHexColor("#4AC69A"), TextAlignmentRight)
c.Container.Render(ctx)

View File

@ -72,7 +72,7 @@ func (b *IconButton) Render(ctx *Context) {
} else if b.IconHeight != 0 {
size = Pt(b.IconHeight*size.X/size.Y, b.IconHeight)
}
iconTexture.CopyResize(ctx.Renderer, RectSize(b.Bounds.X+(b.Bounds.W-size.X)/2, b.Bounds.Y+(b.Bounds.H-size.Y)/2, size.X, size.Y))
iconTexture.CopyResize(ctx.Renderer, Rect(b.Bounds.X+(b.Bounds.W-size.X)/2, b.Bounds.Y+(b.Bounds.H-size.Y)/2, size.X, size.Y))
} else {
iconTexture.CopyResize(ctx.Renderer, b.Bounds)
}

View File

@ -56,9 +56,9 @@ type LargeDialog struct {
func (d *LargeDialog) Arrange(ctx *Context, bounds Rectangle) {
const titleHeight = 64
d.ControlBase.Arrange(ctx, bounds)
d.title.Arrange(ctx, RectSize(bounds.X, bounds.Y, bounds.W, titleHeight))
d.close.Arrange(ctx, RectSize(bounds.W-64, 0, 64, 64))
d.content.Arrange(ctx, RectSize(bounds.X+titleHeight, 96, bounds.W-2*titleHeight, bounds.H-titleHeight))
d.title.Arrange(ctx, Rect(bounds.X, bounds.Y, bounds.W, titleHeight))
d.close.Arrange(ctx, Rect(bounds.W-64, 0, 64, 64))
d.content.Arrange(ctx, Rect(bounds.X+titleHeight, 96, bounds.W-2*titleHeight, bounds.H-titleHeight))
}
func (d *LargeDialog) Init(ctx *Context) error {

View File

@ -55,11 +55,11 @@ func (p *projection) screenToMapRel(x, y int32) PointF {
}
func (p *projection) screenToTileFitRect(pos Point) Rectangle {
return RectSize(pos.X-p.tileFitScreenSize.X, pos.Y-p.tileFitScreenSize.Y, 2*p.tileFitScreenSize.X, 2*p.tileFitScreenSize.Y)
return Rect(pos.X-p.tileFitScreenSize.X, pos.Y-p.tileFitScreenSize.Y, 2*p.tileFitScreenSize.X, 2*p.tileFitScreenSize.Y)
}
func (p *projection) screenToTileRect(pos Point) Rectangle {
return RectSize(pos.X-p.tileScreenOffset.X, pos.Y-p.tileScreenOffset.Y, p.tileScreenSize.X, p.tileScreenSize.Y)
return Rect(pos.X-p.tileScreenOffset.X, pos.Y-p.tileScreenOffset.Y, p.tileScreenSize.X, p.tileScreenSize.Y)
}
func (p *projection) update(renderer *sdl.Renderer) {
@ -74,8 +74,8 @@ func (p *projection) update(renderer *sdl.Renderer) {
log.Fatal(err)
}
p.windowCenter = Pt(windowW/2, windowH/2)
p.windowInteractRect = Rect(buttonBarWidth, 64, windowW-buttonBarWidth, windowH)
p.windowVisibleRect = Rect(buttonBarWidth, 0, windowW-buttonBarWidth, windowH+p.tileScreenSize.Y) // Adding a tile height to the bottom for trees that stick out from the cells below.
p.windowInteractRect = RectAbs(buttonBarWidth, 64, windowW-buttonBarWidth, windowH)
p.windowVisibleRect = RectAbs(buttonBarWidth, 0, windowW-buttonBarWidth, windowH+p.tileScreenSize.Y) // Adding a tile height to the bottom for trees that stick out from the cells below.
}
func (p *projection) visibleTiles(action func(int32, int32, Point)) {

View File

@ -6,7 +6,7 @@ type Rectangle struct {
sdl.Rect
}
func Rect(x1, y1, x2, y2 int32) Rectangle {
func RectAbs(x1, y1, x2, y2 int32) Rectangle {
if x1 > x2 {
x1, x2 = x2, x1
}
@ -16,7 +16,7 @@ func Rect(x1, y1, x2, y2 int32) Rectangle {
return Rectangle{sdl.Rect{X: x1, Y: y1, W: x2 - x1, H: y2 - y1}}
}
func RectSize(x, y, w, h int32) Rectangle { return Rectangle{sdl.Rect{X: x, Y: y, W: w, H: h}} }
func Rect(x, y, w, h int32) Rectangle { return Rectangle{sdl.Rect{X: x, Y: y, W: w, H: h}} }
func (r Rectangle) Bottom() int32 { return r.Y + r.H }

View File

@ -92,8 +92,8 @@ func (r *Research) Init(ctx *Context) error {
func (r *Research) Arrange(ctx *Context, bounds Rectangle) {
r.Container.Arrange(ctx, bounds)
r.specialists.Arrange(ctx, RectSize(r.Bounds.X, r.Bounds.Y+40, r.Bounds.W, r.Bounds.H-40))
r.input.Arrange(ctx, RectSize(r.Bounds.X, r.Bounds.X+r.Bounds.H-48, r.Bounds.W, 24))
r.specialists.Arrange(ctx, Rect(r.Bounds.X, r.Bounds.Y+40, r.Bounds.W, r.Bounds.H-40))
r.input.Arrange(ctx, Rect(r.Bounds.X, r.Bounds.X+r.Bounds.H-48, r.Bounds.W, 24))
r.input.Alignment = TextAlignmentCenter
center := Pt(r.Bounds.X+r.Bounds.W/2, r.Bounds.Y+r.Bounds.H/2)
@ -103,7 +103,7 @@ func (r *Research) Arrange(ctx *Context, bounds Rectangle) {
angle := (float64((10-i)%10)*0.16 + .2) * math.Pi
pos := Pt(int32(distance*math.Cos(angle)), int32(.8*distance*math.Sin(angle)))
digitCenter := center.Add(pos)
r.digits[i].Arrange(ctx, RectSize(digitCenter.X-24, digitCenter.Y-24, 48, 48))
r.digits[i].Arrange(ctx, Rect(digitCenter.X-24, digitCenter.Y-24, 48, 48))
}
}

View File

@ -29,11 +29,11 @@ func (t *Texture) Size() Point { return t.size }
// func (t *Texture) SDLRectPtr() *sdl.Rect { return t.rect.SDLPtr() }
func (t *Texture) Copy(renderer *sdl.Renderer, dst Point) {
t.CopyResize(renderer, RectSize(dst.X, dst.Y, t.size.X, t.size.Y))
t.CopyResize(renderer, Rect(dst.X, dst.Y, t.size.X, t.size.Y))
}
func (t *Texture) CopyPart(renderer *sdl.Renderer, src Rectangle, dst Point) {
t.CopyPartResize(renderer, src, RectSize(dst.X, dst.Y, src.W, src.H))
t.CopyPartResize(renderer, src, Rect(dst.X, dst.Y, src.W, src.H))
}
func (t *Texture) CopyPartResize(renderer *sdl.Renderer, src Rectangle, dst Rectangle) {
@ -41,7 +41,7 @@ func (t *Texture) CopyPartResize(renderer *sdl.Renderer, src Rectangle, dst Rect
}
func (t *Texture) CopyResize(renderer *sdl.Renderer, dst Rectangle) {
t.CopyPartResize(renderer, Rect(0, 0, t.size.X, t.size.Y), dst)
t.CopyPartResize(renderer, RectAbs(0, 0, t.size.X, t.size.Y), dst)
}
func (t *Texture) SetColor(color sdl.Color) {