Line data Source code
1 : package messagelog
2 :
3 : import (
4 : "image"
5 :
6 : "github.com/hajimehoshi/ebiten/v2"
7 : "github.com/kijimaD/ruins/internal/gamelog"
8 : "github.com/kijimaD/ruins/internal/widgets/theme"
9 : "github.com/kijimaD/ruins/internal/widgets/uicore"
10 : w "github.com/kijimaD/ruins/internal/world"
11 :
12 : "github.com/kijimaD/ruins/internal/world/query"
13 : )
14 :
15 : // Insets はパディング設定を表す
16 : type Insets struct {
17 : Top int
18 : Bottom int
19 : Left int
20 : Right int
21 : }
22 :
23 : // WidgetConfig はMessageLogWidgetの設定を表す
24 : type WidgetConfig struct {
25 : MaxLines int // 表示する最大行数
26 : LineHeight int // 1行の高さ
27 : Spacing int // 行間のスペース
28 : Padding Insets // 内部パディング
29 : }
30 :
31 : // Widget はメッセージログ表示ウィジェット。
32 : //
33 : // 各エントリを1行とし、行内の色付きフラグメントを水平に連ねて描く。描画は uicore の
34 : // ツリーで組み、グローバル可変状態に触れない。フラグメント幅はフェイスの測定で決める
35 : type Widget struct {
36 : config WidgetConfig
37 : world w.World
38 : entries []gamelog.LogEntry
39 : lastVersion int
40 : loaded bool
41 : }
42 :
43 : // NewWidget は新しいMessageLogWidgetを作成する
44 : func NewWidget(config WidgetConfig, world w.World) *Widget {
45 1 : return &Widget{
46 1 : config: config,
47 1 : world: world,
48 1 : }
49 : }
50 :
51 : // Update はウィジェットを更新する
52 : func (widget *Widget) Update() {
53 1 : widget.refresh()
54 1 : }
55 :
56 : // Draw はウィジェットを指定位置に描画する
57 : func (widget *Widget) Draw(cv uicore.Canvas, x, y, width, height int) {
58 : // モーダルなど別 state が前面にある間、このウィジェットを所有する state は Update されない。
59 : // それでも最新ログを映すため、描画時にも取り込みを行う。version 一致時は即座に戻るため軽量
60 1 : widget.refresh()
61 1 :
62 1 : if width <= 0 || height <= 0 {
63 0 : return
64 0 : }
65 :
66 : // いったん別の画へ組んでから貼る。行が枠をはみ出しても外へ漏れない
67 1 : offscreen := ebiten.NewImage(width, height)
68 1 : widget.buildTree(width, height).Draw(uicore.NewEbitenCanvas(offscreen))
69 1 : cv.DrawImage(image.Pt(x, y), offscreen)
70 : }
71 :
72 : // refresh はログのバージョンが変わったときだけ最新エントリを取り込む
73 : func (widget *Widget) refresh() {
74 1 : store := query.GetGameLog(widget.world)
75 1 : if store == nil {
76 0 : return
77 0 : }
78 1 : version := store.Version()
79 1 : if widget.loaded && version == widget.lastVersion {
80 1 : return
81 1 : }
82 1 : widget.entries = store.GetRecentEntries(widget.config.MaxLines)
83 1 : widget.lastVersion = version
84 1 : widget.loaded = true
85 : }
86 :
87 : // buildTree はエントリ列を uicore のツリーへ組む。
88 : // 各行を Padding.Top から LineHeight+Spacing 刻みで下へ、行内フラグメントは測定幅ぶん右へ並べる
89 : func (widget *Widget) buildTree(width, height int) uicore.Widget {
90 1 : res := widget.world.Resources.UIResources
91 1 : face := res.Text.BodyFace
92 1 :
93 1 : var children []uicore.Widget
94 1 : x0 := widget.config.Padding.Left
95 1 : y := widget.config.Padding.Top
96 1 :
97 1 : visible := 0
98 1 : for _, entry := range widget.entries {
99 1 : if entry.IsEmpty() {
100 0 : continue
101 : }
102 1 : x := x0
103 1 : for _, fragment := range entry.Fragments {
104 1 : if fragment.Text == "" {
105 0 : continue
106 : }
107 1 : t := uicore.NewText(fragment.Text, face, fragment.Color)
108 1 : t.Layout(image.Rect(x, y, width, y+widget.config.LineHeight))
109 1 : children = append(children, t)
110 1 : adv := uicore.MeasureTextWidth(fragment.Text, face)
111 1 : x += adv
112 : }
113 1 : visible++
114 1 : y += widget.config.LineHeight + widget.config.Spacing
115 : }
116 :
117 : // エントリが無いときは案内文を出す
118 1 : if visible == 0 {
119 1 : t := uicore.NewText(query.T(widget.world, "No log messages"), face, theme.TextSecondary)
120 1 : t.Layout(image.Rect(x0, widget.config.Padding.Top, width, widget.config.Padding.Top+widget.config.LineHeight))
121 1 : children = append(children, t)
122 1 : }
123 :
124 1 : root := uicore.NewGroup(children...)
125 1 : root.Layout(image.Rect(0, 0, width, height))
126 1 : return root
127 : }
|