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 21:00:19 +00:00
|
|
|
FlowerID string
|
|
|
|
Flower FlowerDescriptor
|
2020-05-10 18:44:20 +00:00
|
|
|
|
2020-05-10 15:16:18 +00:00
|
|
|
hoverAnimation *Animation
|
|
|
|
hoverOffset int32
|
|
|
|
hoverTexture *Texture
|
|
|
|
priceTexture *Texture
|
|
|
|
}
|
|
|
|
|
2020-05-10 21:00:19 +00:00
|
|
|
func NewBuyFlowerButton(icon, iconDisabled, flowerID string, flower FlowerDescriptor, onClick EventContextFn) *BuyFlowerButton {
|
2020-05-10 15:16:18 +00:00
|
|
|
return &BuyFlowerButton{
|
2020-05-14 06:29:23 +00:00
|
|
|
IconButton: *NewIconButtonConfigure(icon, onClick, func(b *IconButton) {
|
2020-05-10 15:16:18 +00:00
|
|
|
b.IconDisabled = iconDisabled
|
2020-05-10 21:00:19 +00:00
|
|
|
b.IsDisabled = !flower.Unlocked
|
2020-05-10 15:16:18 +00:00
|
|
|
}),
|
2020-05-10 21:00:19 +00:00
|
|
|
FlowerID: flowerID,
|
|
|
|
Flower: flower,
|
2020-05-10 15:16:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *BuyFlowerButton) animate() {
|
|
|
|
b.hoverOffset++
|
|
|
|
if b.hoverOffset > b.hoverTexture.Size().X+b.Bounds.W {
|
2020-05-10 21:00:19 +00:00
|
|
|
b.hoverOffset = 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *BuyFlowerButton) fmtTooltipText() string {
|
2020-05-11 01:09:01 +00:00
|
|
|
if !b.Flower.Unlocked {
|
2020-05-10 21:00:19 +00:00
|
|
|
return fmt.Sprintf("%s - %s - %s", FmtMoney(b.Flower.BuyPrice), b.Flower.Name, "Traits are not known yet.")
|
2020-05-10 15:16:18 +00:00
|
|
|
}
|
2020-05-10 21:00:19 +00:00
|
|
|
return fmt.Sprintf("%s - %s - %s", FmtMoney(b.Flower.BuyPrice), b.Flower.Name, b.Flower.Description)
|
2020-05-10 15:16:18 +00:00
|
|
|
}
|
|
|
|
|
2020-05-11 01:09:01 +00:00
|
|
|
func (b *BuyFlowerButton) updateTexts(ctx *Context) error {
|
|
|
|
text := b.fmtTooltipText()
|
2020-05-10 15:16:18 +00:00
|
|
|
font := ctx.Fonts.Font("small")
|
|
|
|
color := MustHexColor("#ffffff")
|
|
|
|
texture, err := font.Render(ctx.Renderer, text, color)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-05-11 01:09:01 +00:00
|
|
|
if b.hoverTexture != nil {
|
|
|
|
b.hoverTexture.Destroy()
|
|
|
|
}
|
2020-05-10 15:16:18 +00:00
|
|
|
b.hoverTexture = texture
|
2020-05-10 21:00:19 +00:00
|
|
|
texture, err = font.Render(ctx.Renderer, FmtMoney(b.Flower.BuyPrice), color)
|
2020-05-10 15:16:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-05-11 01:09:01 +00:00
|
|
|
if b.priceTexture != nil {
|
|
|
|
b.priceTexture.Destroy()
|
|
|
|
}
|
2020-05-10 15:16:18 +00:00
|
|
|
b.priceTexture = texture
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-05-11 01:09:01 +00:00
|
|
|
func (b *BuyFlowerButton) Init(ctx *Context) error {
|
|
|
|
return b.updateTexts(ctx)
|
|
|
|
}
|
|
|
|
|
2020-05-11 09:44:50 +00:00
|
|
|
func (b *BuyFlowerButton) Handle(ctx *Context, event sdl.Event) bool {
|
|
|
|
if b.IconButton.Handle(ctx, event) {
|
|
|
|
return true
|
|
|
|
}
|
2020-05-10 15:16:18 +00:00
|
|
|
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
|
|
|
|
}
|
2020-05-11 09:44:50 +00:00
|
|
|
return false
|
2020-05-10 15:16:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (b *BuyFlowerButton) Render(ctx *Context) {
|
|
|
|
iconTexture := b.activeTexture(ctx)
|
|
|
|
|
|
|
|
pos := Pt(b.Bounds.X, b.Bounds.Y)
|
2020-05-12 11:55:30 +00:00
|
|
|
iconTexture.CopyResize(ctx.Renderer, Rect(pos.X, pos.Y-60, b.Bounds.W, 120))
|
2020-05-10 18:44:20 +00:00
|
|
|
if (b.IsMouseOver && !b.IsDisabled) || b.IsActive {
|
2020-05-11 09:44:50 +00:00
|
|
|
SetDrawColor(ctx.Renderer, TransparentWhite)
|
|
|
|
ctx.Renderer.FillRect(b.Bounds.SDLPtr())
|
2020-05-10 15:16:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if b.hoverAnimation != nil {
|
|
|
|
b.hoverAnimation.AnimateFn(b.animate)
|
|
|
|
}
|
|
|
|
|
|
|
|
if b.IsMouseOver {
|
2020-05-11 00:49:53 +00:00
|
|
|
left := b.Bounds.W - 8 - b.hoverOffset
|
|
|
|
top := pos.Y + b.Bounds.H - 20
|
2020-05-10 15:16:18 +00:00
|
|
|
if left < 0 {
|
2020-05-12 11:55:30 +00:00
|
|
|
part := RectAbs(-left, 0, b.hoverTexture.Size().X, b.hoverTexture.Size().Y)
|
2020-05-10 15:16:18 +00:00
|
|
|
b.hoverTexture.CopyPart(ctx.Renderer, part, Pt(pos.X, top))
|
|
|
|
} else {
|
|
|
|
b.hoverTexture.Copy(ctx.Renderer, Pt(pos.X+left, top))
|
|
|
|
}
|
|
|
|
} else {
|
2020-05-11 00:49:53 +00:00
|
|
|
b.priceTexture.Copy(ctx.Renderer, Pt(pos.X+b.Bounds.W-8-b.priceTexture.Size().X, pos.Y+b.Bounds.H-20))
|
2020-05-10 15:16:18 +00:00
|
|
|
}
|
|
|
|
}
|
2020-05-11 01:09:01 +00:00
|
|
|
|
|
|
|
func (b *BuyFlowerButton) Update(ctx *Context, desc FlowerDescriptor) {
|
|
|
|
b.Flower = desc
|
|
|
|
b.updateTexts(ctx)
|
|
|
|
}
|