2020-05-09 22:19:44 +00:00
|
|
|
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
|
2020-05-09 23:40:54 +00:00
|
|
|
} else {
|
|
|
|
b.Hover = -1
|
2020-05-09 22:19:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
2020-05-10 08:56:20 +00:00
|
|
|
texture.Copy(ctx.Renderer, &sdl.Rect{X: pos.X, Y: pos.Y - 40, W: buttonBarWidth, H: 120})
|
2020-05-09 22:19:44 +00:00
|
|
|
if b.Hover == i {
|
|
|
|
hoverTexture.Copy(ctx.Renderer, hoverTexture.RectOffset(pos))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewGameControls() *GameControls {
|
|
|
|
return &GameControls{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *GameControls) Init(ctx *Context) error {
|
2020-05-09 23:40:54 +00:00
|
|
|
c.flowers.Hover = -1
|
2020-05-09 22:19:44 +00:00
|
|
|
c.flowers.Buttons = []Button{
|
2020-05-09 23:40:54 +00:00
|
|
|
Button{Icon: "flower-poppy-1", Disabled: "flower-poppy-disabled"},
|
|
|
|
Button{Icon: "flower-red-c-1", Disabled: "flower-red-c-disabled", IsDisabled: true},
|
2020-05-09 22:19:44 +00:00
|
|
|
}
|
|
|
|
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
|
|
|
|
}
|