111 lines
2.4 KiB
Go
111 lines
2.4 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
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
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, W: buttonBarWidth, H: buttonBarWidth})
|
||
|
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.Buttons = []Button{
|
||
|
Button{Icon: "flower-poppy-1", Disabled: "flower-red-c-1"},
|
||
|
Button{Icon: "flower-poppy-1", Disabled: "flower-red-c-1", 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
|
||
|
}
|