Line data Source code
1 : package hud
2 :
3 : import (
4 : "image"
5 : "image/color"
6 :
7 : "github.com/hajimehoshi/ebiten/v2"
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 : // FacilityCellKind は設備グリッドの1マスの見た目の種別。装着可否と下地色を分ける。
14 : type FacilityCellKind int
15 :
16 : // 設備グリッドのマス種別。装着可否と下地色を分ける
17 : const (
18 : FacilityCellCube FacilityCellKind = iota // キューブ本体
19 : FacilityCellEmpty // 空き。装着できる
20 : FacilityCellUsed // 設備が装着されている
21 : FacilityCellBlocked // 壁や障害物。装着できない
22 : )
23 :
24 : // FacilityCell は設備グリッドの1マス。下地の種別と、重ねるスプライトを持つ。
25 : type FacilityCell struct {
26 : Kind FacilityCellKind
27 : Icon *ebiten.Image // キューブや設備のスプライト。無ければ nil
28 : }
29 :
30 : // FacilityGridView は設備画面の描画に要る値。展開空間の各マスとカーソル、選択マスの説明を持つ。
31 : // ドメインの判定は呼び出し側が済ませ、ここは見た目だけを受け取る。
32 : type FacilityGridView struct {
33 : Cols, Rows int
34 : Cells []FacilityCell // 行優先で Cols*Rows 個
35 : CursorCol, CursorRow int
36 : Title string
37 : Content string // カーソルのマスの内容名
38 : Hint string // カーソルのマスで押せる操作。無ければ空
39 : Footer string // 共通の操作説明
40 : }
41 :
42 : // 設備グリッドの寸法。1マスの辺と、下端から情報行を持ち上げる量
43 : const (
44 : facilityGridCellPx = 44 // 1マスの辺
45 : facilityContentBottomPx = 52 // 選択マスの内容名を下端から上げる量
46 : facilityFooterBottomPx = 28 // 操作説明を下端から上げる量
47 : )
48 :
49 : // DrawFacilityGrid は設備画面のパネル・グリッド・カーソル・説明を cv へ描く。画面側は view を組んで渡すだけで、
50 : // uicore の組み立てはここへ閉じる。
51 : func DrawFacilityGrid(cv uicore.Canvas, rect image.Rectangle, face text.Face, view FacilityGridView) {
52 1 : Chrome{}.Panel(cv, rect)
53 1 : cv.DrawText(image.Pt(rect.Min.X+16, rect.Min.Y+12), view.Title, face, theme.TextPrimary)
54 1 :
55 1 : gridW := view.Cols * facilityGridCellPx
56 1 : gridH := view.Rows * facilityGridCellPx
57 1 : originX := rect.Min.X + (rect.Dx()-gridW)/2
58 1 : originY := rect.Min.Y + (rect.Dy()-gridH)/2
59 1 : for row := range view.Rows {
60 1 : for col := range view.Cols {
61 1 : cell := view.Cells[row*view.Cols+col]
62 1 : r := image.Rect(originX+col*facilityGridCellPx, originY+row*facilityGridCellPx,
63 1 : originX+(col+1)*facilityGridCellPx, originY+(row+1)*facilityGridCellPx)
64 1 : cv.FillRect(r.Inset(2), facilityCellColor(cell.Kind), uicore.RectOptions{Radius: 3})
65 1 : if cell.Icon != nil {
66 1 : cv.DrawImageRect(r.Inset(6), cell.Icon)
67 1 : }
68 1 : if col == view.CursorCol && row == view.CursorRow {
69 1 : cv.StrokeRect(r.Inset(1), 2, theme.HUDSlotSelectedBorder, uicore.RectOptions{Radius: 4})
70 1 : }
71 : }
72 : }
73 :
74 1 : cv.DrawText(image.Pt(rect.Min.X+16, rect.Max.Y-facilityContentBottomPx), view.Content, face, theme.TextPrimary)
75 1 : footer := view.Footer
76 1 : if view.Hint != "" {
77 1 : footer = view.Hint + " " + footer
78 1 : }
79 1 : cv.DrawText(image.Pt(rect.Min.X+16, rect.Max.Y-facilityFooterBottomPx), footer, face, theme.TextDisabled)
80 : }
81 :
82 : // facilityCellColor はマス種別ごとの下地色を返す
83 : func facilityCellColor(kind FacilityCellKind) color.Color {
84 1 : switch kind {
85 : case FacilityCellCube:
86 1 : return theme.PanelHighlight
87 : case FacilityCellUsed:
88 1 : return theme.StatusSuccess
89 : case FacilityCellBlocked:
90 1 : return theme.HUDGaugeBg
91 : default:
92 1 : return theme.ListSelectedBg
93 : }
94 : }
|