Sander Schobers
69c8ae72f3
Textures used to be 512x512px, cropped them to 128x160px. The width of the texture was first cropped to 130px and then resized to 128px (height was not resized).
114 lines
2.5 KiB
Go
114 lines
2.5 KiB
Go
package tins2020
|
|
|
|
import (
|
|
"log"
|
|
|
|
"github.com/veandco/go-sdl2/sdl"
|
|
)
|
|
|
|
type GameControls struct {
|
|
ControlBase
|
|
|
|
menu ButtonBar
|
|
flowers ButtonBar
|
|
}
|
|
|
|
type ButtonBar struct {
|
|
Top int32
|
|
Left int32
|
|
Bottom int32
|
|
Hover int
|
|
Buttons []Button
|
|
}
|
|
|
|
type Button struct {
|
|
Icon string
|
|
Disabled string
|
|
|
|
IsDisabled bool
|
|
}
|
|
|
|
const buttonBarWidth = 96
|
|
|
|
func (b *ButtonBar) Handle(ctx *Context, event sdl.Event) {
|
|
switch e := event.(type) {
|
|
case *sdl.MouseMotionEvent:
|
|
if e.X > b.Left && e.X < b.Left+buttonBarWidth {
|
|
button := int(e.Y-b.Top) / buttonBarWidth
|
|
if button < 0 || button >= len(b.Buttons) || b.Buttons[button].IsDisabled {
|
|
button = -1
|
|
}
|
|
b.Hover = button
|
|
} else {
|
|
b.Hover = -1
|
|
}
|
|
}
|
|
}
|
|
|
|
func (b *ButtonBar) Render(ctx *Context) {
|
|
ctx.Renderer.FillRect(&sdl.Rect{X: b.Left, Y: b.Top, W: b.Left + buttonBarWidth, H: b.Bottom})
|
|
texture := func(b Button) *Texture {
|
|
if b.IsDisabled {
|
|
texture := ctx.Textures.Texture(b.Disabled)
|
|
if texture != nil {
|
|
return texture
|
|
}
|
|
}
|
|
return ctx.Textures.Texture(b.Icon)
|
|
}
|
|
hoverTexture := ctx.Textures.Texture("game-control-hover")
|
|
for i, button := range b.Buttons {
|
|
pos := Pt(b.Left, b.Top+int32(i)*buttonBarWidth)
|
|
texture := texture(button)
|
|
texture.Copy(ctx.Renderer, &sdl.Rect{X: pos.X, Y: pos.Y - 40, W: buttonBarWidth, H: 120})
|
|
if b.Hover == i {
|
|
hoverTexture.Copy(ctx.Renderer, hoverTexture.RectOffset(pos))
|
|
}
|
|
}
|
|
}
|
|
|
|
func NewGameControls() *GameControls {
|
|
return &GameControls{}
|
|
}
|
|
|
|
func (c *GameControls) Init(ctx *Context) error {
|
|
c.flowers.Hover = -1
|
|
c.flowers.Buttons = []Button{
|
|
Button{Icon: "flower-poppy-1", Disabled: "flower-poppy-disabled"},
|
|
Button{Icon: "flower-red-c-1", Disabled: "flower-red-c-disabled", IsDisabled: true},
|
|
}
|
|
return c.updateBarPositions(ctx)
|
|
}
|
|
|
|
func (c *GameControls) Handle(ctx *Context, event sdl.Event) {
|
|
c.menu.Handle(ctx, event)
|
|
c.flowers.Handle(ctx, event)
|
|
|
|
switch e := event.(type) {
|
|
case *sdl.WindowEvent:
|
|
switch e.Event {
|
|
case sdl.WINDOWEVENT_RESIZED:
|
|
err := c.updateBarPositions(ctx)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
func (c *GameControls) Render(ctx *Context) {
|
|
ctx.Renderer.SetDrawColor(74, 198, 154, 255)
|
|
c.menu.Render(ctx)
|
|
c.flowers.Render(ctx)
|
|
}
|
|
|
|
func (c *GameControls) updateBarPositions(ctx *Context) error {
|
|
w, h, err := ctx.Renderer.GetOutputSize()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
c.menu.Top, c.menu.Left, c.menu.Bottom = 0, 0, h
|
|
c.flowers.Top, c.flowers.Left, c.flowers.Bottom = 0, w-buttonBarWidth, h
|
|
return nil
|
|
}
|