diff options
| author | Florian Fischer <florian.fischer@muhq.space> | 2023-01-26 22:16:10 +0100 |
|---|---|---|
| committer | Florian Fischer <florian.fischer@muhq.space> | 2025-01-27 16:43:44 +0100 |
| commit | 926ca5cdd20a39e3e05aaa3486cb166718dab5ca (patch) | |
| tree | 3d7bc8a0e1e0be1ce8d92d5503bdede878e718e9 /go/ui/cardView.go | |
| parent | 8720ebf9c74d9e11001772b5c00c293fa3a8b9e5 (diff) | |
| download | muhqs-game-926ca5cdd20a39e3e05aaa3486cb166718dab5ca.tar.gz muhqs-game-926ca5cdd20a39e3e05aaa3486cb166718dab5ca.zip | |
intermeditae commit
Diffstat (limited to 'go/ui/cardView.go')
| -rw-r--r-- | go/ui/cardView.go | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/go/ui/cardView.go b/go/ui/cardView.go new file mode 100644 index 00000000..a1c004c5 --- /dev/null +++ b/go/ui/cardView.go @@ -0,0 +1,46 @@ +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 +} |
