package ui import ( "log" "github.com/hajimehoshi/ebiten/v2" "muhq.space/muhqs-game/go/assets" "muhq.space/muhqs-game/go/game" "golang.org/x/exp/slices" ) type HandCard struct { C *game.Card } type HandView struct { WidgetBase hand *game.Hand cardWidth float64 cardHeight float64 scale float64 bottomAligned bool highlights []*game.Card } func NewHandView(x, y, width, height int, h *game.Hand, bottomAligned bool) *HandView { hv := &HandView{ WidgetBase: NewWidgetBase(x, y, width, height), hand: h, cardWidth: 0, cardHeight: 0, scale: 0, bottomAligned: bottomAligned, highlights: []*game.Card{}, } hv.renderImpl = func() *ebiten.Image { return hv.render() } return hv } func (hv *HandView) render() *ebiten.Image { img := ebiten.NewImage(hv.Width, hv.Height) handCards := hv.hand.Cards() nHandCards := len(handCards) for i, card := range handCards { cardImg := assets.GetCard(card.Path(), "en") if cardImg == nil { log.Fatalf("No image found for %s", card.Path()) } if hv.scale == 0 { b := cardImg.Bounds() x_scale := (float64(hv.Width) / float64(nHandCards)) / float64(b.Dx()) y_scale := float64(hv.Height) / float64(b.Dy()) hv.scale = x_scale if y_scale < x_scale { hv.scale = y_scale } hv.cardWidth = float64(b.Dx()) * hv.scale hv.cardHeight = float64(b.Dy()) * hv.scale } yOffset := 0.0 if hv.bottomAligned { yOffset = float64(hv.Height) - hv.cardHeight } op := &ebiten.DrawImageOptions{} op.GeoM.Scale(hv.scale, hv.scale) op.GeoM.Translate(float64(i)*hv.cardWidth, yOffset) if slices.Contains(hv.highlights, card) { op.ColorM.Scale(1, 0, 0, 0.5) } img.DrawImage(cardImg, op) } return img } func (hv *HandView) FindObjectAt(x, y int) interface{} { if !hv.Contains(x, y) { return nil } if hv.bottomAligned && float64(y-hv.Y) < float64(hv.Height)-hv.cardHeight { return nil } i := int(float64(x) / hv.cardWidth) if i >= hv.hand.Size() { return nil } return HandCard{hv.hand.Cards()[i]} } func (hv *HandView) setHighlights(cards []*game.Card) { hv.highlights = cards hv.ForceRedraw() } func (hv *HandView) HighlightCard(card *game.Card) { hv.setHighlights([]*game.Card{card}) } func (hv *HandView) AddHighlightCard(card *game.Card) { hv.setHighlights(append(hv.highlights, card)) } func (hv *HandView) ClearHighlights() { hv.setHighlights([]*game.Card{}) }