116 lines
2.9 KiB
Go
116 lines
2.9 KiB
Go
package tins2020
|
|
|
|
import (
|
|
"fmt"
|
|
"time"
|
|
|
|
"github.com/veandco/go-sdl2/sdl"
|
|
)
|
|
|
|
type BuyFlowerButton struct {
|
|
IconButton
|
|
|
|
FlowerID string
|
|
Flower FlowerDescriptor
|
|
|
|
hoverAnimation *Animation
|
|
hoverOffset int32
|
|
hoverTexture *Texture
|
|
priceTexture *Texture
|
|
}
|
|
|
|
func NewBuyFlowerButton(icon, iconDisabled, flowerID string, flower FlowerDescriptor, onClick EventContextFn) *BuyFlowerButton {
|
|
return &BuyFlowerButton{
|
|
IconButton: *NewIconButtonConfig(icon, onClick, func(b *IconButton) {
|
|
b.IconDisabled = iconDisabled
|
|
b.IsDisabled = !flower.Unlocked
|
|
}),
|
|
FlowerID: flowerID,
|
|
Flower: flower,
|
|
}
|
|
}
|
|
|
|
func (b *BuyFlowerButton) animate() {
|
|
b.hoverOffset++
|
|
if b.hoverOffset > b.hoverTexture.Size().X+b.Bounds.W {
|
|
b.hoverOffset = 0
|
|
}
|
|
}
|
|
|
|
func (b *BuyFlowerButton) fmtTooltipText() string {
|
|
if !b.Flower.Unlocked {
|
|
return fmt.Sprintf("%s - %s - %s", FmtMoney(b.Flower.BuyPrice), b.Flower.Name, "Traits are not known yet.")
|
|
}
|
|
return fmt.Sprintf("%s - %s - %s", FmtMoney(b.Flower.BuyPrice), b.Flower.Name, b.Flower.Description)
|
|
}
|
|
|
|
func (b *BuyFlowerButton) updateTexts(ctx *Context) error {
|
|
text := b.fmtTooltipText()
|
|
font := ctx.Fonts.Font("small")
|
|
color := MustHexColor("#ffffff")
|
|
texture, err := font.Render(ctx.Renderer, text, color)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if b.hoverTexture != nil {
|
|
b.hoverTexture.Destroy()
|
|
}
|
|
b.hoverTexture = texture
|
|
texture, err = font.Render(ctx.Renderer, FmtMoney(b.Flower.BuyPrice), color)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if b.priceTexture != nil {
|
|
b.priceTexture.Destroy()
|
|
}
|
|
b.priceTexture = texture
|
|
return nil
|
|
}
|
|
|
|
func (b *BuyFlowerButton) Init(ctx *Context) error {
|
|
return b.updateTexts(ctx)
|
|
}
|
|
|
|
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-60, b.Bounds.W, 120))
|
|
if (b.IsMouseOver && !b.IsDisabled) || b.IsActive {
|
|
mouseOverTexture.CopyResize(ctx.Renderer, b.Bounds)
|
|
}
|
|
|
|
if b.hoverAnimation != nil {
|
|
b.hoverAnimation.AnimateFn(b.animate)
|
|
}
|
|
|
|
if b.IsMouseOver {
|
|
left := b.Bounds.W - 8 - b.hoverOffset
|
|
top := pos.Y + b.Bounds.H - 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+b.Bounds.W-8-b.priceTexture.Size().X, pos.Y+b.Bounds.H-20))
|
|
}
|
|
}
|
|
|
|
func (b *BuyFlowerButton) Update(ctx *Context, desc FlowerDescriptor) {
|
|
b.Flower = desc
|
|
b.updateTexts(ctx)
|
|
}
|