Line data Source code
1 : package uicore
2 :
3 : import (
4 : "image"
5 : "image/color"
6 : "slices"
7 :
8 : "github.com/hajimehoshi/ebiten/v2"
9 : text "github.com/hajimehoshi/ebiten/v2/text/v2"
10 : "github.com/yohamta/furex/v2"
11 : )
12 :
13 : // Drawable は描くことだけができるツリー。配置は済んでいる前提で、画面にはこの面を見せる。
14 : // 画面が絶対座標で組めないよう、Layout をここに置かない。
15 : type Drawable interface {
16 : Draw(cv Canvas)
17 : }
18 :
19 : // Placeable は描くだけの面で受け取ったツリーを、配置もできる Widget へ戻す。
20 : // このパッケージの型はすべて配置を持つので、通常はそのまま通る。
21 : // 配置を持たないものは、与えられた矩形を無視して描くだけの葉として包む。
22 : func Placeable(ds []Drawable) []Widget {
23 1 : out := make([]Widget, len(ds))
24 1 : for i, d := range ds {
25 1 : if w, ok := d.(Widget); ok {
26 1 : out[i] = w
27 1 : continue
28 : }
29 1 : out[i] = &drawOnly{d: d}
30 : }
31 1 : return out
32 : }
33 :
34 : // drawOnly は配置を持たないツリーを Widget として扱う包み。行や列の枠は占めるが、
35 : // 与えられた矩形は中身へ伝わらない。
36 : type drawOnly struct {
37 : base
38 : d Drawable
39 : }
40 :
41 : // Layout は Widget を実装する。矩形は自身の記録だけに使う。
42 1 : func (o *drawOnly) Layout(b image.Rectangle) { o.rect = b }
43 :
44 : // Draw は Widget を実装する。
45 1 : func (o *drawOnly) Draw(cv Canvas) { o.d.Draw(cv) }
46 :
47 : // Children は Widget を実装する。子は持たない。
48 1 : func (o *drawOnly) Children() []Widget { return nil }
49 :
50 : // Widget は保持型 UI ツリーの要素。
51 : type Widget interface {
52 : // Layout は与えられた矩形内で自身と子の矩形を確定する。
53 : Layout(bounds image.Rectangle)
54 : // Draw は確定済みの矩形で Canvas に描く。
55 : Draw(cv Canvas)
56 : // Children は子を返す。ツリーの検査に使う。
57 : Children() []Widget
58 : }
59 :
60 : // base は確定済みの矩形を保持する共通部。各ウィジェットが埋め込む。
61 : // Layout が書き、Draw が読む。この受け渡しが配置と描画を2段に分ける。
62 : type base struct {
63 : rect image.Rectangle
64 : }
65 :
66 : // Align はテキストの横方向の寄せ。
67 : type Align int
68 :
69 : const (
70 : // AlignLeft は矩形の左端に寄せる。既定。
71 : AlignLeft Align = iota
72 : // AlignRight は矩形の右端に寄せる。
73 : AlignRight
74 : // AlignCenter は矩形の中央に寄せる。
75 : AlignCenter
76 : )
77 :
78 : // Text は1行のラベル。既定は左上寄せで、Align で右寄せや中央寄せに、VCenter で縦中央にできる。
79 : type Text struct {
80 : base
81 : Value string
82 : Face text.Face
83 : Color color.Color
84 : OutlineColor color.RGBA // A が非ゼロなら本体の前に8方向へずらした縁取りを描く。ゼロ値は縁取りなし
85 : Align Align
86 : VCenter bool // 真なら矩形内で縦中央へ寄せる。行高が本文より高い一覧行でアイコンや強調とそろえる
87 : }
88 :
89 : // NewText は左上寄せのラベルを作る。
90 : func NewText(value string, face text.Face, c color.Color) *Text {
91 1 : return &Text{Value: value, Face: face, Color: c}
92 1 : }
93 :
94 : // Layout は Text を実装する。
95 1 : func (t *Text) Layout(b image.Rectangle) { t.rect = b }
96 :
97 : // Draw は Text を実装する。Align に応じて矩形内での横位置、VCenter に応じて縦位置を決める。
98 : // 幅・高さの測定にフェイスが要るので、フェイスが無ければ左上寄せにフォールバックする。
99 : func (t *Text) Draw(cv Canvas) {
100 1 : x, y := t.rect.Min.X, t.rect.Min.Y
101 1 : if t.Face != nil {
102 1 : width, height := MeasureText(t.Value, t.Face)
103 1 : switch t.Align {
104 1 : case AlignLeft:
105 : // 左寄せは x をそのまま
106 : case AlignRight:
107 1 : x = t.rect.Max.X - width
108 : case AlignCenter:
109 0 : x = t.rect.Min.X + (t.rect.Dx()-width)/2
110 : }
111 1 : if t.VCenter {
112 0 : y = t.rect.Min.Y + (t.rect.Dy()-height)/2
113 0 : }
114 : }
115 1 : pos := image.Pt(x, y)
116 1 : if t.OutlineColor.A != 0 {
117 1 : for _, off := range textOutlineOffsets {
118 1 : cv.DrawText(pos.Add(off), t.Value, t.Face, t.OutlineColor)
119 1 : }
120 : }
121 1 : cv.DrawText(pos, t.Value, t.Face, t.Color)
122 : }
123 :
124 : // textOutlineOffsets は OutlineColor 付き Text の縁取りを描く8方向のずらし量。本体の前にこの分だけ
125 : // ずらして縁色で描き、世界へ重ねる文字を背景の明暗によらず読ませる。
126 : var textOutlineOffsets = []image.Point{
127 : {X: -1, Y: -1}, {X: 0, Y: -1}, {X: 1, Y: -1},
128 : {X: -1, Y: 0}, {X: 1, Y: 0},
129 : {X: -1, Y: 1}, {X: 0, Y: 1}, {X: 1, Y: 1},
130 : }
131 :
132 : // Children は Text を実装する。子は持たない。
133 1 : func (t *Text) Children() []Widget { return nil }
134 :
135 : // Graphic は画像を1枚描くウィジェット。
136 : type Graphic struct {
137 : base
138 : Image *ebiten.Image
139 : }
140 :
141 : // NewGraphic は画像ウィジェットを作る。
142 1 : func NewGraphic(img *ebiten.Image) *Graphic { return &Graphic{Image: img} }
143 :
144 : // Layout は Graphic を実装する。
145 1 : func (g *Graphic) Layout(b image.Rectangle) { g.rect = b }
146 :
147 : // Draw は Graphic を実装する。矩形に収まるよう縮小し、左寄せ・縦中央で描く。一覧のアイコンを
148 : // 行の高さへ合わせ、左に揃えて文字と縦位置をそろえる。
149 : func (g *Graphic) Draw(cv Canvas) {
150 1 : if g.Image != nil {
151 1 : cv.DrawImageRect(g.rect, g.Image)
152 1 : }
153 : }
154 :
155 : // Children は Graphic を実装する。子は持たない。
156 1 : func (g *Graphic) Children() []Widget { return nil }
157 :
158 : // NineSlice はテクスチャを9スライスで矩形いっぱいに引き伸ばして描くウィジェット。
159 : // 枠付きの背景、窓・タイトルバー・入力枠・選択バー、に使う。BX・BY はソースの左中右・上中下の幅。
160 : type NineSlice struct {
161 : base
162 : Image *ebiten.Image
163 : BX [3]int
164 : BY [3]int
165 : }
166 :
167 : // NewNineSlice はテクスチャとスライス幅から NineSlice を作る。
168 : func NewNineSlice(img *ebiten.Image, bx, by [3]int) *NineSlice {
169 1 : return &NineSlice{Image: img, BX: bx, BY: by}
170 1 : }
171 :
172 : // Layout は NineSlice を実装する。
173 1 : func (n *NineSlice) Layout(b image.Rectangle) { n.rect = b }
174 :
175 : // Draw は NineSlice を実装する。
176 : func (n *NineSlice) Draw(cv Canvas) {
177 1 : if n.Image != nil {
178 1 : cv.DrawNineSlice(n.rect, n.Image, n.BX, n.BY)
179 1 : }
180 : }
181 :
182 : // Children は NineSlice を実装する。子は持たない。
183 1 : func (n *NineSlice) Children() []Widget { return nil }
184 :
185 : // Group は配置済みの子をそのまま束ねて描く。子の矩形は各自が確定済みで、Group は再配置しない。
186 : // 自前の絶対配置レイアウトを組むときに使う。
187 : type Group struct {
188 : base
189 : children []Widget
190 : }
191 :
192 : // NewGroup は配置済みの子を束ねる。子は呼び出し側が Layout 済みにしておく。
193 1 : func NewGroup(children ...Widget) *Group { return &Group{children: children} }
194 :
195 : // Layout は Group を実装する。子は再配置しない。
196 1 : func (g *Group) Layout(b image.Rectangle) { g.rect = b }
197 :
198 : // Draw は Group を実装する。子を順に描く。
199 : func (g *Group) Draw(cv Canvas) {
200 1 : for _, c := range g.children {
201 1 : c.Draw(cv)
202 1 : }
203 : }
204 :
205 : // Children は Group を実装する。
206 1 : func (g *Group) Children() []Widget { return g.children }
207 :
208 : // Box は BoxStyle の単色ボックスを1つ描く葉ウィジェット。Group の層として単体で敷く
209 : // パネル背景に使う。子を持たず自分の矩形だけを塗る点が Container の背景と違う。
210 : type Box struct {
211 : base
212 : Style BoxStyle
213 : }
214 :
215 : // NewBox は単色ボックスの葉を作る。border が nil なら枠を描かない。枠幅は1。
216 : func NewBox(fill, border color.Color, radius int) *Box {
217 1 : return &Box{Style: BoxStyle{Fill: fill, Border: border, BorderWidth: 1, Radius: radius}}
218 1 : }
219 :
220 : // Layout は Box を実装する。
221 1 : func (r *Box) Layout(b image.Rectangle) { r.rect = b }
222 :
223 : // Draw は Box を実装する。
224 1 : func (r *Box) Draw(cv Canvas) { drawBox(cv, r.rect, r.Style) }
225 :
226 : // drawBox は単色ボックスを敷く。Fill があれば塗り、Border があり幅が正なら枠を描く。Radius が正
227 : // なら四隅を丸める。Box 葉と Container 背景の共通描画で、単色ボックスの描き方を1箇所にする。
228 : func drawBox(cv Canvas, rect image.Rectangle, s BoxStyle) {
229 1 : if s.Fill != nil {
230 1 : cv.FillRect(rect, s.Fill, RectOptions{Radius: s.Radius})
231 1 : }
232 1 : if s.Border != nil && s.BorderWidth > 0 {
233 1 : cv.StrokeRect(rect, s.BorderWidth, s.Border, RectOptions{Radius: s.Radius})
234 1 : }
235 : }
236 :
237 : // Children は Box を実装する。子は持たない。
238 0 : func (r *Box) Children() []Widget { return nil }
239 :
240 : // Dir はコンテナの主軸方向。
241 : type Dir int
242 :
243 : const (
244 : // Vertical は子を縦に積む。
245 : Vertical Dir = iota
246 : // Horizontal は子を横に並べる。
247 : Horizontal
248 : )
249 :
250 : // Container は子を主軸方向に並べる入れ物。背景の塗りと枠、テクスチャ背景、内側余白を持てる。
251 : type Container struct {
252 : base
253 : dir Dir
254 : sizes []int // 主軸方向の各子のサイズ。Vertical は高さ、Horizontal は幅
255 : style BoxStyle
256 : bgImage *ebiten.Image // 9スライスで敷くテクスチャ背景。選択行など横帯の意匠に使う
257 : bgBX [3]int
258 : bgBY [3]int
259 : lineImg *ebiten.Image // 非 nil なら下端に敷く区切り線のテクスチャ。横グラデを行幅へ伸ばす
260 : lineTint color.Color // 区切り線の色。テクスチャに掛ける
261 : pad int // 内側余白。子はこのぶん内側へ寄せる。背景と枠は矩形いっぱいに描く
262 : children []Widget
263 : }
264 :
265 : // SetPadding は内側余白を設定する。子を余白ぶん内側へ寄せる。
266 : func (c *Container) SetPadding(pad int) *Container {
267 1 : c.pad = pad
268 1 : return c
269 1 : }
270 :
271 : // SetStyle は単色ボックスの背景 style を設定する。BoxStyle.Radius でパネルの角丸背景にもできる。
272 : // style が塗りか枠を持てば NineSlice 背景より優先される。
273 : func (c *Container) SetStyle(s BoxStyle) *Container {
274 1 : c.style = s
275 1 : return c
276 1 : }
277 :
278 : // SetBackgroundNineSlice はテクスチャ背景を9スライスで敷く。選択行など横帯の意匠に使う。
279 : func (c *Container) SetBackgroundNineSlice(img *ebiten.Image, bx, by [3]int) *Container {
280 1 : c.bgImage = img
281 1 : c.bgBX = bx
282 1 : c.bgBY = by
283 1 : return c
284 1 : }
285 :
286 : // SetBottomLine は下端に区切り線のテクスチャを敷く。横グラデを行幅へ伸ばし tint で着色する。
287 : // 一覧行の下線に使う。
288 : func (c *Container) SetBottomLine(img *ebiten.Image, tint color.Color) *Container {
289 1 : c.lineImg = img
290 1 : c.lineTint = tint
291 1 : return c
292 1 : }
293 :
294 : // Layout は Container を実装する。余白を除いた内側に、furex の flexbox で子を並べる。
295 : // 縦は sizes の固定高で積む。横は sizes の固定幅で並べ、0 幅の列すべて、1つも無ければ末尾の列が
296 : // flex-grow で余り幅を吸収して内側を埋める。名前列を伸ばし右の数値列を右端へ寄せる用途。
297 : // 交差軸は既定の AlignItemStretch で内側いっぱいに広がる。座標計算は furex に委譲し、
298 : // 自前の数式を持たない。
299 : func (c *Container) Layout(b image.Rectangle) {
300 1 : c.rect = b
301 1 : if len(c.children) == 0 {
302 1 : return
303 1 : }
304 1 : inner := image.Rect(b.Min.X+c.pad, b.Min.Y+c.pad, b.Max.X-c.pad, b.Max.Y-c.pad)
305 1 :
306 1 : dir := furex.Column
307 1 : fallbackIdx := -1
308 1 : if c.dir == Horizontal {
309 1 : dir = furex.Row
310 1 : hasZero := slices.Contains(c.sizes, 0)
311 1 : if !hasZero {
312 : // 0 幅の列が無ければ末尾の列が余り幅を吸収する。sizes と children の長さは
313 : // Row・VBox の構築で一致する。万一 sizes が短くても範囲外の子を伸ばして
314 : // 余り幅が消えないよう、幅を持つ末尾の子に上界を切る
315 1 : fallbackIdx = min(len(c.children), len(c.sizes)) - 1
316 1 : }
317 : }
318 :
319 1 : root := &furex.View{
320 1 : Left: inner.Min.X,
321 1 : Top: inner.Min.Y,
322 1 : Width: inner.Dx(),
323 1 : Height: inner.Dy(),
324 1 : Direction: dir,
325 1 : }
326 1 : for i, ch := range c.children {
327 1 : size := 0
328 1 : if i < len(c.sizes) {
329 1 : size = c.sizes[i]
330 1 : }
331 1 : child := &furex.View{Handler: frameTo{w: ch}}
332 1 : if c.dir == Vertical {
333 1 : child.Height = size
334 1 : } else {
335 1 : child.Width = size
336 1 : // 0 幅の列はすべて余り幅を等分して伸びる。複数あれば均等割になる
337 1 : if (size == 0 && i < len(c.sizes)) || i == fallbackIdx {
338 1 : child.Grow = 1
339 1 : }
340 : }
341 1 : root.AddChild(child)
342 : }
343 1 : root.Layout()
344 1 : root.Draw(layoutProbe())
345 : }
346 :
347 : // Draw は Container を実装する。背景を敷いてから子を描く。単色 style と NineSlice は排他で、
348 : // style が塗りか枠を持てばそちらを優先し、無ければ NineSlice を敷く。
349 : func (c *Container) Draw(cv Canvas) {
350 1 : if c.style.Fill != nil || (c.style.Border != nil && c.style.BorderWidth > 0) {
351 1 : drawBox(cv, c.rect, c.style)
352 2 : } else if c.bgImage != nil {
353 1 : cv.DrawNineSlice(c.rect, c.bgImage, c.bgBX, c.bgBY)
354 1 : }
355 1 : for _, ch := range c.children {
356 1 : ch.Draw(cv)
357 1 : }
358 1 : if c.lineImg != nil {
359 1 : h := c.lineImg.Bounds().Dy()
360 1 : cv.DrawImageTintedRect(image.Rect(c.rect.Min.X, c.rect.Max.Y-h, c.rect.Max.X, c.rect.Max.Y), c.lineImg, c.lineTint)
361 1 : }
362 : }
363 :
364 : // Children は Container を実装する。
365 1 : func (c *Container) Children() []Widget { return c.children }
366 :
367 : // ---- 宣言的コンストラクタ ----
368 : // ツリーを式として組む。状態が変わったら画面を組み直す。可変グローバルには触れない。
369 :
370 : // VBox は子を縦に積む。rowH は各行の高さ。
371 : func VBox(rowH int, children ...Widget) *Container {
372 1 : sizes := make([]int, len(children))
373 1 : for i := range sizes {
374 1 : sizes[i] = rowH
375 1 : }
376 1 : return &Container{dir: Vertical, sizes: sizes, children: children}
377 : }
378 :
379 : // Row は子を横に並べる。colWidths は各列の幅。
380 : func Row(colWidths []int, cells ...Widget) *Container {
381 1 : return &Container{dir: Horizontal, sizes: colWidths, children: cells}
382 1 : }
383 :
384 : // Panel は背景付きの縦積み。rowH は各行の高さ。
385 : func Panel(style BoxStyle, rowH int, children ...Widget) *Container {
386 1 : c := VBox(rowH, children...)
387 1 : c.style = style
388 1 : return c
389 1 : }
390 :
391 : // CollectLabels はツリー内の Text.Value を出現順に集める。
392 : func CollectLabels(w Widget) []string {
393 1 : var out []string
394 1 : if t, ok := w.(*Text); ok {
395 1 : out = append(out, t.Value)
396 1 : }
397 1 : for _, ch := range w.Children() {
398 1 : out = append(out, CollectLabels(ch)...)
399 1 : }
400 1 : return out
401 : }
|