package tins2020 type GameControls struct { Container game *Game menu ButtonBar top ButtonBar flowers ButtonBar pause *IconButton run *IconButton runFast *IconButton } func NewGameControls(game *Game) *GameControls { return &GameControls{game: game} } func (c *GameControls) updateSpeedControls() { disable := func(b *IconButton, speed GameSpeed) { b.IsDisabled = speed == c.game.Speed } disable(c.pause, GameSpeedPaused) disable(c.run, GameSpeedNormal) disable(c.runFast, GameSpeedFast) } 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)) } func (c *GameControls) buyPoppy(ctx *Context) { c.game.Balance -= 10 } func (c *GameControls) Init(ctx *Context) error { c.flowers.Background = MustHexColor("#356dad") // brown alternative? #4ac69a c.flowers.Buttons = []Control{ NewBuyFlowerButton("flower-poppy-1", "flower-poppy-disabled", "Poppy", 10, "A very generic flower that thrives in a moderate climate.", false, c.buyPoppy), NewBuyFlowerButton("flower-red-c-1", "flower-poppy-disabled", "Unknown", 100, "Traits are not known yet.", true, nil), } c.top.Orientation = OrientationHorizontal c.pause = NewIconButtonConfig("control-pause", EmptyEvent(func() { c.game.Pause() c.updateSpeedControls() }), func(b *IconButton) { b.IconDisabled = "control-pause-disabled" }) c.run = NewIconButtonConfig("control-run", EmptyEvent(func() { c.game.Run() c.updateSpeedControls() }), func(b *IconButton) { b.IconDisabled = "control-run-disabled" }) c.runFast = NewIconButtonConfig("control-run-fast", EmptyEvent(func() { c.game.RunFast() c.updateSpeedControls() }), func(b *IconButton) { b.IconDisabled = "control-run-fast-disabled" }) c.updateSpeedControls() c.top.Buttons = []Control{c.pause, c.run, c.runFast} c.menu.Background = MustHexColor("#356dad") c.menu.Buttons = []Control{ NewIconButton("control-settings", EmptyEvent(func() {})), NewIconButton("control-save", EmptyEvent(func() {})), NewIconButton("control-load", EmptyEvent(func() {})), } c.Container.AddChild(&c.menu) c.Container.AddChild(&c.top) c.Container.AddChild(&c.flowers) return c.Container.Init(ctx) } func (c *GameControls) Render(ctx *Context) { topBar := MustHexColor("#0000007f") ctx.Renderer.SetDrawColor(topBar.R, topBar.G, topBar.B, topBar.A) ctx.Renderer.FillRect(Rect(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) }