2020-05-10 15:16:18 +00:00
|
|
|
package tins2020
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/veandco/go-sdl2/sdl"
|
|
|
|
)
|
|
|
|
|
|
|
|
type BuyFlowerButton struct {
|
|
|
|
IconButton
|
|
|
|
|
2020-05-10 18:44:20 +00:00
|
|
|
FlowerID string
|
2020-05-10 15:16:18 +00:00
|
|
|
Name string
|
|
|
|
Price int
|
|
|
|
Description string
|
|
|
|
|
2020-05-10 18:44:20 +00:00
|
|
|
IsActive bool
|
|
|
|
|
2020-05-10 15:16:18 +00:00
|
|
|
hoverAnimation *Animation
|
|
|
|
hoverOffset int32
|
|
|
|
hoverTexture *Texture
|
|
|
|
priceTexture *Texture
|
|
|
|
}
|
|
|
|
|
2020-05-10 18:44:20 +00:00
|
|
|
func NewBuyFlowerButton(icon, iconDisabled, flowerID, name string, price int, description string, isDisabled bool, onClick EventContextFn) *BuyFlowerButton {
|
2020-05-10 15:16:18 +00:00
|
|
|
return &BuyFlowerButton{
|
|
|
|
IconButton: *NewIconButtonConfig(icon, onClick, func(b *IconButton) {
|
|
|
|
b.IconDisabled = iconDisabled
|
|
|
|
b.IsDisabled = isDisabled
|
|
|
|
}),
|
2020-05-10 18:44:20 +00:00
|
|
|
FlowerID: flowerID,
|
2020-05-10 15:16:18 +00:00
|
|
|
Name: name,
|
|
|
|
Price: price,
|
|
|
|
Description: description,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *BuyFlowerButton) animate() {
|
|
|
|
b.hoverOffset++
|
|
|
|
if b.hoverOffset > b.hoverTexture.Size().X+b.Bounds.W {
|
|
|
|
b.hoverOffset = b.priceTexture.Size().X
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *BuyFlowerButton) Init(ctx *Context) error {
|
|
|
|
text := fmt.Sprintf("%s - %s - %s", FmtMoney(b.Price), b.Name, b.Description)
|
|
|
|
font := ctx.Fonts.Font("small")
|
|
|
|
color := MustHexColor("#ffffff")
|
|
|
|
texture, err := font.Render(ctx.Renderer, text, color)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
b.hoverTexture = texture
|
|
|
|
texture, err = font.Render(ctx.Renderer, FmtMoney(b.Price), color)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
b.priceTexture = texture
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *BuyFlowerButton) Handle(ctx *Context, event sdl.Event) {
|
|
|
|
b.IconButton.Handle(ctx, event)
|
|
|
|
if b.IsMouseOver && b.hoverAnimation == nil {
|
|
|
|
b.hoverAnimation = NewAnimationPtr(10 * time.Millisecond)
|
|
|
|
b.hoverOffset = b.priceTexture.Size().X
|
|
|
|
} else if !b.IsMouseOver {
|
|
|
|
b.hoverAnimation = nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *BuyFlowerButton) Render(ctx *Context) {
|
|
|
|
iconTexture := b.activeTexture(ctx)
|
|
|
|
mouseOverTexture := ctx.Textures.Texture("control-hover")
|
|
|
|
|
|
|
|
pos := Pt(b.Bounds.X, b.Bounds.Y)
|
|
|
|
iconTexture.CopyResize(ctx.Renderer, RectSize(pos.X, pos.Y-40, buttonBarWidth, 120))
|
2020-05-10 18:44:20 +00:00
|
|
|
if (b.IsMouseOver && !b.IsDisabled) || b.IsActive {
|
2020-05-10 15:16:18 +00:00
|
|
|
mouseOverTexture.Copy(ctx.Renderer, pos)
|
|
|
|
}
|
|
|
|
|
|
|
|
if b.hoverAnimation != nil {
|
|
|
|
b.hoverAnimation.AnimateFn(b.animate)
|
|
|
|
}
|
|
|
|
|
|
|
|
if b.IsMouseOver {
|
|
|
|
left := buttonBarWidth - 8 - b.hoverOffset
|
|
|
|
top := pos.Y + buttonBarWidth - 20
|
|
|
|
if left < 0 {
|
|
|
|
part := Rect(-left, 0, b.hoverTexture.Size().X, b.hoverTexture.Size().Y)
|
|
|
|
b.hoverTexture.CopyPart(ctx.Renderer, part, Pt(pos.X, top))
|
|
|
|
} else {
|
|
|
|
b.hoverTexture.Copy(ctx.Renderer, Pt(pos.X+left, top))
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
b.priceTexture.Copy(ctx.Renderer, Pt(pos.X+buttonBarWidth-8-b.priceTexture.Size().X, pos.Y+buttonBarWidth-20))
|
|
|
|
}
|
|
|
|
}
|