package tins2020 import ( "fmt" "time" "github.com/veandco/go-sdl2/sdl" ) type BuyFlowerButton struct { IconButton Name string Price int Description string hoverAnimation *Animation hoverOffset int32 hoverTexture *Texture priceTexture *Texture } func NewBuyFlowerButton(icon, iconDisabled, name string, price int, description string, isDisabled bool, onClick EventFn) *BuyFlowerButton { return &BuyFlowerButton{ IconButton: *NewIconButtonConfig(icon, onClick, func(b *IconButton) { b.IconDisabled = iconDisabled b.IsDisabled = isDisabled }), 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)) if b.IsMouseOver && !b.IsDisabled { 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)) } }