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) Init(ctx *Context) error { text := fmt.Sprintf("%s - %s - %s", FmtMoney(b.Flower.BuyPrice), b.Flower.Name, b.Flower.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.Flower.BuyPrice), 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)) if (b.IsMouseOver && !b.IsDisabled) || b.IsActive { 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)) } }