117 lines
3.3 KiB
Go
117 lines
3.3 KiB
Go
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) createBuyFlowerButton(id string) *BuyFlowerButton {
|
|
flower, _ := c.game.Herbarium.Find(id)
|
|
return NewBuyFlowerButton(
|
|
flower.IconTemplate.Variant(1),
|
|
flower.IconTemplate.Disabled(),
|
|
id,
|
|
flower.Name,
|
|
flower.Price,
|
|
flower.Description,
|
|
!flower.Unlocked,
|
|
EmptyEvent(func() {
|
|
c.game.SelectPlantFlowerTool(id)
|
|
}),
|
|
)
|
|
}
|
|
|
|
func (c *GameControls) toolChanged(state interface{}) {
|
|
tool, _ := state.(Tool)
|
|
var flowerID string
|
|
if tool, ok := tool.(*PlantFlowerTool); ok {
|
|
flowerID = tool.FlowerID
|
|
}
|
|
for _, control := range c.flowers.Buttons {
|
|
button := control.(*BuyFlowerButton)
|
|
button.IsActive = button.FlowerID == flowerID
|
|
}
|
|
}
|
|
|
|
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) Init(ctx *Context) error {
|
|
c.game.ToolChanged().RegisterItf(c.toolChanged)
|
|
|
|
c.flowers.Background = MustHexColor("#356dad") // brown alternative? #4ac69a
|
|
|
|
for _, id := range c.game.Herbarium.Flowers() {
|
|
c.flowers.Buttons = append(c.flowers.Buttons, c.createBuyFlowerButton(id))
|
|
}
|
|
|
|
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)
|
|
}
|