Line data Source code
1 : package hud
2 :
3 : import (
4 : "fmt"
5 : "image"
6 : "image/color"
7 :
8 : "github.com/hajimehoshi/ebiten/v2/text/v2"
9 : theme "github.com/kijimaD/ruins/internal/widgets/theme"
10 : "github.com/kijimaD/ruins/internal/widgets/uicore"
11 : )
12 :
13 : // StatusBadge はステータスバッジの情報
14 : type StatusBadge struct {
15 : Text string // 表示テキスト
16 : Color color.RGBA // 背景色
17 : }
18 :
19 : // StatusBadgesData はステータスバッジ表示に必要なデータ
20 : type StatusBadgesData struct {
21 : Badges []StatusBadge // 表示するバッジ一覧
22 : MessageAreaHeight int // メッセージエリアの高さ
23 : ScreenDimensions ScreenDimensions // 画面サイズ
24 : }
25 :
26 : // StatusBadges は左下にステータスバッジを表示するウィジェット
27 : type StatusBadges struct {
28 : bodyFace text.Face
29 : enabled bool
30 : }
31 :
32 : // NewStatusBadges は新しい StatusBadges を作成する
33 : func NewStatusBadges(bodyFace text.Face) *StatusBadges {
34 1 : return &StatusBadges{
35 1 : bodyFace: bodyFace,
36 1 : enabled: true,
37 1 : }
38 : }
39 :
40 : // Draw はステータスバッジを左下へ下から積んで描く。メッセージエリアの上端を底にして、下寄せの
41 : // FlexColumn へ各バッジを流す。画面下端からの逆算や段の手積みをしない。表示上限を超えた分は先頭の
42 : // 「+N」バッジにまとめる。
43 : func (sb *StatusBadges) Draw(cv uicore.Canvas, data StatusBadgesData) {
44 1 : if !sb.enabled || len(data.Badges) == 0 {
45 1 : return
46 1 : }
47 :
48 1 : const (
49 1 : badgeGap = 4 // バッジ間の隙間
50 1 : paddingX = 6 // バッジ内の左右パディング
51 1 : paddingY = 4 // バッジ内の上下パディング
52 1 : maxVisible = 5 // 最大表示数
53 1 : )
54 1 :
55 1 : badges := data.Badges
56 1 : hasMore := false
57 1 : if len(badges) > maxVisible {
58 1 : badges = badges[:maxVisible]
59 1 : hasMore = true
60 1 : }
61 :
62 1 : _, textHeight := uicore.MeasureText("W", sb.bodyFace)
63 1 : badgeHeight := textHeight + paddingY*2
64 1 :
65 1 : items := []uicore.FlexItem{{Grow: true}}
66 1 : add := func(str string, fill color.RGBA) {
67 1 : items = append(items,
68 1 : uicore.FlexItem{W: &badgeWidget{text: str, face: sb.bodyFace, fill: fill, padX: paddingX, padY: paddingY}, Height: badgeHeight},
69 1 : uicore.FlexItem{Height: badgeGap},
70 1 : )
71 1 : }
72 :
73 : // 上から順に積む。上限超過の「+N」を最上段、続けて表示するバッジを並べる。下寄せなので最後の
74 : // バッジがメッセージエリアの直上へ来る
75 1 : if hasMore {
76 1 : add(fmt.Sprintf("+%d", len(data.Badges)-maxVisible), theme.HUDBadgeBg)
77 1 : }
78 1 : for _, badge := range badges {
79 1 : add(badge.Text, badge.Color)
80 1 : }
81 : // 末尾の隙間をメッセージエリア分の下マージンへ置き換え、スタックの底をログ枠の上へ固定する
82 1 : items[len(items)-1] = uicore.FlexItem{Height: data.MessageAreaHeight + theme.Space4}
83 1 :
84 1 : inner := image.Rect(theme.Space4, 0, data.ScreenDimensions.Width, data.ScreenDimensions.Height)
85 1 : uicore.FlexColumn(inner, items)
86 1 : drawFlexItems(cv, items)
87 : }
88 :
89 : // badgeWidget は1つのステータスバッジ。与えられた矩形の左端から内容幅ぶんの箱を描き、白文字を重ねる。
90 : // FlexColumn は幅を内側いっぱいへ伸ばすが、バッジは内容幅の左寄せなので Draw で左端から必要幅だけ描く。
91 : type badgeWidget struct {
92 : rect image.Rectangle
93 : text string
94 : face text.Face
95 : fill color.RGBA
96 : padX, padY int
97 : }
98 :
99 : // Layout は uicore.Widget を満たす。
100 1 : func (b *badgeWidget) Layout(r image.Rectangle) { b.rect = r }
101 :
102 : // Draw は uicore.Widget を満たす。左端から内容幅の箱を描き、白文字を padding ぶん内側へ置く。
103 : func (b *badgeWidget) Draw(cv uicore.Canvas) {
104 1 : textWidth, _ := uicore.MeasureText(b.text, b.face)
105 1 : bgWidth := uicore.FitWidth([]int{textWidth}, b.padX*2, 0)
106 1 : box := image.Rect(b.rect.Min.X, b.rect.Min.Y, b.rect.Min.X+bgWidth, b.rect.Max.Y)
107 1 : badgeChrome(cv, box, b.fill)
108 1 : drawOutlinedText(cv, b.text, b.face, image.Pt(b.rect.Min.X+b.padX, b.rect.Min.Y+b.padY), theme.TextPrimary)
109 1 : }
110 :
111 : // Children は uicore.Widget を満たす。子は持たない。
112 0 : func (b *badgeWidget) Children() []uicore.Widget { return nil }
113 :
114 : // badgeChrome はバッジの箱を描く。塗りの色は状態を表すデータなので、意匠のテクスチャでなく
115 : // 塗りと枠で表す。枠の色はパネルと同じにして、HUD の他の箱と質感を揃える
116 : func badgeChrome(cv uicore.Canvas, r image.Rectangle, fill color.RGBA) {
117 1 : cv.FillRect(r, fill, uicore.RectOptions{Radius: theme.CornerRadius})
118 1 : cv.StrokeRect(r, 1, theme.PanelHighlight, uicore.RectOptions{Radius: theme.CornerRadius})
119 1 : }
|