package ui import ( "github.com/hajimehoshi/ebiten/v2" "muhq.space/muhqs-game/go/assets" ) type CardView struct { WidgetBase cardPath string } func NewScaledCardView(x, y int, width, height int, cardPath string) *CardView { cv := &CardView{NewWidgetBase(x, y, width, height), cardPath} cv.renderImpl = func() *ebiten.Image { return cv.render() } return cv } func NewAutoCardView(x, y int, cardPath string) *CardView { cv := &CardView{NewWidgetBase(x, y, -1, -1), cardPath} cv.renderImpl = func() *ebiten.Image { return cv.render() } return cv } func (cv *CardView) render() *ebiten.Image { card := assets.GetCard(cv.cardPath, "en") op := &ebiten.DrawImageOptions{} b := card.Bounds() if cv.Width == -1 && cv.Height == -1 { cv.Width = b.Dx() cv.Height = b.Dy() } else { x_scale := float64(cv.Width) / float64(b.Dx()) y_scale := float64(cv.Height) / float64(b.Dy()) scale := x_scale if y_scale < x_scale { scale = y_scale } op.GeoM.Scale(scale, scale) } img := ebiten.NewImage(cv.Width, cv.Height) img.DrawImage(card, op) return img } type ToggleCardView struct { EventHandlersMap cv *CardView tgglBtn *SimpleButton expanded bool } func NewToggleCardView(x, y, wdth, hght int, cardPath string) *ToggleCardView { cv := &ToggleCardView{ EventHandlersMap: NewEventHandlersMap(), cv: NewScaledCardView(x, y, wdth, hght, cardPath), expanded: true, } cv.tgglBtn = NewSimpleButton(x, y+10, 80, 50, ">", func(b *SimpleButton) { shift := float64(cv.cv.Width - 80) if cv.expanded { b.label = "<" // cv.cv.Op.GeoM.Translate(shift, 0) cv.cv.X = cv.cv.X + int(shift) cv.tgglBtn.X = cv.tgglBtn.X + int(shift) - 20 } else { b.label = ">" // cv.cv.Op.GeoM.Reset() cv.cv.X = cv.cv.X - int(shift) cv.tgglBtn.X = cv.tgglBtn.X - int(shift) + 20 } cv.tgglBtn.ForceRedraw() cv.expanded = !cv.expanded }) return cv } func (cv *ToggleCardView) Draw(screen *ebiten.Image) { cv.cv.Draw(screen) cv.tgglBtn.Draw(screen) } func (cv *ToggleCardView) Layout() (int, int) { // FIXME: include btn layout return cv.cv.Layout() } func (cv *ToggleCardView) Contains(x, y int) bool { return cv.cv.Contains(x, y) || cv.tgglBtn.Contains(x, y) } func (cv *ToggleCardView) FindObjectAt(x, y int) any { return cv.cv.FindObjectAt(x, y) } func (*ToggleCardView) IsClickable() bool { return true } func (cv *ToggleCardView) Click(x, y int) { if cv.tgglBtn.Contains(x, y) { cv.tgglBtn.Click(x, y) } }