LCOV - code coverage report
Current view: top level - widgets/hud - game_info.go Coverage Total Hit
Test: coverage.lcov Lines: 92.3 % 130 120
Test Date: 2026-11-19 00:25:22 Functions: - 0 0

            Line data    Source code
       1              : package hud
       2              : 
       3              : import (
       4              :         "fmt"
       5              :         "image"
       6              :         "image/color"
       7              : 
       8              :         "github.com/hajimehoshi/ebiten/v2"
       9              :         "github.com/hajimehoshi/ebiten/v2/text/v2"
      10              :         "github.com/kijimaD/ruins/internal/consts"
      11              :         theme "github.com/kijimaD/ruins/internal/widgets/theme"
      12              :         "github.com/kijimaD/ruins/internal/widgets/uicore"
      13              :         w "github.com/kijimaD/ruins/internal/world"
      14              : )
      15              : 
      16              : // TempDirection は体温変化の向き
      17              : type TempDirection string
      18              : 
      19              : const (
      20              :         // TempDirectionUp は温まっている状態
      21              :         TempDirectionUp TempDirection = "up"
      22              :         // TempDirectionDown は冷えている状態
      23              :         TempDirectionDown TempDirection = "down"
      24              : )
      25              : 
      26              : // TemperatureArrow は体温の変化方向を示す矢印。体温ゲージの右に出す。
      27              : // 温まると赤の上向き、冷えると青の下向き。体温がほぼ変わらないときは出さない。色の濃さが変化の速さ
      28              : type TemperatureArrow struct {
      29              :         Visible   bool
      30              :         Direction TempDirection
      31              :         Color     color.RGBA
      32              : }
      33              : 
      34              : // tempArrowSlotW は体温ゲージの右に確保する矢印スロットの幅
      35              : const tempArrowSlotW = 26
      36              : 
      37              : // GameInfo はHUDの基本ゲーム情報エリア
      38              : type GameInfo struct {
      39              :         bodyFace    text.Face
      40              :         headingFace text.Face     // 階層表示用の大きなフォント
      41              :         gaugeFill   *ebiten.Image // ゲージ埋め。縦方向グラデーション
      42              :         enabled     bool
      43              : }
      44              : 
      45              : // NewGameInfo は新しいHUDGameInfoを作成する
      46              : func NewGameInfo(bodyFace text.Face, headingFace text.Face, gaugeFill *ebiten.Image) *GameInfo {
      47            1 :         return &GameInfo{
      48            1 :                 bodyFace:    bodyFace,
      49            1 :                 headingFace: headingFace,
      50            1 :                 gaugeFill:   gaugeFill,
      51            1 :                 enabled:     true,
      52            1 :         }
      53              : }
      54              : 
      55              : // Update はゲーム情報エリアを更新する
      56            0 : func (info *GameInfo) Update(_ w.World) {
      57              :         // 現在は更新処理なし
      58              : }
      59              : 
      60              : // Draw はゲーム情報エリアを描画する
      61              : func (info *GameInfo) Draw(cv uicore.Canvas, data GameInfoData) {
      62            1 :         if !info.enabled {
      63            0 :                 return
      64            0 :         }
      65              : 
      66              :         // 左上ゲージ。HPゲージ・体温ゲージと体温矢印を縦に積む
      67            1 :         info.drawGauges(cv, data)
      68            1 : 
      69              :         // 右下スタック。周囲気温・所持重量・所持通貨をメッセージエリアの上へ下から積む
      70            1 :         info.drawBottomRightStack(cv, data)
      71            1 : 
      72              :         // フロア情報(最後に描画して最前面に表示)
      73            1 :         info.drawFloorNumber(cv, data)
      74              : }
      75              : 
      76              : // drawFloorNumber は階層番号を右上へ描画する。オーバーワールドでは階層の概念が無いので描かない。
      77              : // 右端揃えは Text の Align に委ね、画面幅からの逆算をしない。
      78              : func (info *GameInfo) drawFloorNumber(cv uicore.Canvas, data GameInfoData) {
      79            1 :         if !data.ShowFloor {
      80            1 :                 return
      81            1 :         }
      82            1 :         floorText := fmt.Sprintf("%3dF", data.FloorNumber)
      83            1 :         // 高さ0の矩形は VCenter や高さ参照で壊れるのでテキスト高を持たせる
      84            1 :         _, h := uicore.MeasureText(floorText, info.headingFace)
      85            1 :         t := &uicore.Text{Value: floorText, Face: info.headingFace, Color: theme.TextPrimary, OutlineColor: theme.HUDTextOutline, Align: uicore.AlignRight}
      86            1 :         t.Layout(image.Rect(0, theme.Space4, data.ScreenDimensions.Width-theme.Space4, theme.Space4+h))
      87            1 :         t.Draw(cv)
      88              : }
      89              : 
      90              : // ゲージ共通のレイアウト定数
      91              : const (
      92              :         gaugeBaseX      = theme.Space4                   // 左マージン
      93              :         gaugeBaseY      = theme.Space4                   // 最初のゲージの上マージン
      94              :         gaugeWidth      = 180                            // ゲージ塗りの幅
      95              :         gaugeBorderH    = 2                              // 白枠線の合計(上1 + 下1)
      96              :         gaugeFillHeight = 12                             // ゲージ塗り部分の高さ
      97              :         gaugeHeight     = gaugeBorderH + gaugeFillHeight // 白枠 + 塗り
      98              :         gaugeSpacing    = 4                              // ゲージ間の間隔
      99              : )
     100              : 
     101              : // drawGauges はHPゲージ・体温ゲージ・体温矢印を左上へ縦に積む。ゲージと矢印スロットを横に並べ、
     102              : // HP の下へ間隔を空けて体温を置く。位置決めは FlexColumn/Row に委ね、段や右寄せの手計算をしない。
     103              : func (info *GameInfo) drawGauges(cv uicore.Canvas, data GameInfoData) {
     104            1 :         tempRow := uicore.Row([]int{gaugeWidth, tempArrowSlotW},
     105            1 :                 info.bodyTempGauge(data),
     106            1 :                 info.arrowWidget(data.TempArrow),
     107            1 :         )
     108            1 :         hpRow := uicore.Row([]int{gaugeWidth, tempArrowSlotW},
     109            1 :                 info.healthGauge(data),
     110            1 :                 uicore.NewGroup(),
     111            1 :         )
     112            1 :         items := []uicore.FlexItem{
     113            1 :                 {W: hpRow, Height: gaugeHeight},
     114            1 :                 {Height: gaugeSpacing},
     115            1 :                 {W: tempRow, Height: gaugeHeight},
     116            1 :                 {Grow: true},
     117            1 :         }
     118            1 :         inner := image.Rect(gaugeBaseX, gaugeBaseY, gaugeBaseX+tempArrowSlotW+gaugeWidth, data.ScreenDimensions.Height)
     119            1 :         uicore.FlexColumn(inner, items)
     120            1 :         drawFlexItems(cv, items)
     121            1 : }
     122              : 
     123              : // arrowWidget は体温変化の矢印を返す。温まると上向き、冷えると下向き。三角形でなく
     124              : // アイコンフォントの字で描き、キーキャップの記号と意匠も描画経路もそろえる。非表示なら空を返す。
     125              : func (info *GameInfo) arrowWidget(arrow TemperatureArrow) uicore.Widget {
     126            1 :         if !arrow.Visible {
     127            1 :                 return uicore.NewGroup()
     128            1 :         }
     129            1 :         var glyph string
     130            1 :         switch arrow.Direction {
     131              :         case TempDirectionUp:
     132            1 :                 glyph = consts.IconArrowUp
     133              :         case TempDirectionDown:
     134            1 :                 glyph = consts.IconArrowDown
     135              :         }
     136              :         // VCenter で体温ゲージの縦中心にそろえる
     137            1 :         return &uicore.Text{Value: glyph, Face: info.bodyFace, Color: arrow.Color, OutlineColor: theme.HUDTextOutline, VCenter: true, Align: uicore.AlignCenter}
     138              : }
     139              : 
     140              : // bodyTempGauge は体温ゲージを返す。満タンが平熱、減って青くなるほど冷える片方向。非表示なら空を返す。
     141              : func (info *GameInfo) bodyTempGauge(data GameInfoData) uicore.Widget {
     142            1 :         if !data.BodyTempVisible {
     143            1 :                 return uicore.NewGroup()
     144            1 :         }
     145            1 :         return &gaugeWidget{fill: info.gaugeFill, ratio: data.BodyTempRatio, fillColor: bodyTempFillColor(data.BodyTempRatio), border: theme.HUDGaugeBorder}
     146              : }
     147              : 
     148              : // healthGauge は HP ゲージを返す。半分を境に緑から黄、黄から赤へ寄る。
     149              : func (info *GameInfo) healthGauge(data GameInfoData) uicore.Widget {
     150            1 :         ratio := 0.0
     151            1 :         if data.PlayerMaxHP > 0 {
     152            1 :                 ratio = max(0, min(1, float64(data.PlayerHP)/float64(data.PlayerMaxHP)))
     153            1 :         }
     154            1 :         var fill color.RGBA
     155            1 :         if ratio > 0.5 {
     156            1 :                 fill = theme.LerpColor(theme.HUDHealthFull, theme.HUDHealthHalf, (1.0-ratio)*2)
     157            1 :         } else {
     158            1 :                 fill = theme.LerpColor(theme.HUDHealthEmpty, theme.HUDHealthHalf, ratio*2)
     159            1 :         }
     160            1 :         return &gaugeWidget{fill: info.gaugeFill, ratio: ratio, fillColor: fill, border: theme.HUDGaugeBorder}
     161              : }
     162              : 
     163              : // fuelGauge はキューブの燃料ゲージを返す。満で琥珀、空へ近づくと赤へ寄る。
     164              : func fuelGauge(gaugeFill *ebiten.Image, ratio float64) uicore.Widget {
     165            1 :         fill := theme.LerpColor(theme.HUDFuelEmpty, theme.HUDFuelFull, ratio)
     166            1 :         return &gaugeWidget{fill: gaugeFill, ratio: ratio, fillColor: fill, border: theme.HUDGaugeBorder}
     167            1 : }
     168              : 
     169              : // bodyTempFillColor は体温ゲージの塗り色を返す。平熱の白から、冷えるほど青へ寄る片方向
     170              : func bodyTempFillColor(ratio float64) color.RGBA {
     171              :         // 体温は片方向。0が平熱かつ上限で寒さ方向へ負に動くので、ratio=1 が平熱、下がるほど冷えの色へ寄る
     172            1 :         return theme.LerpColor(theme.HUDTempNeutral, theme.HUDTempCold, 1-ratio)
     173            1 : }
     174              : 
     175              : // gaugeCornerRadius はゲージの角丸半径。細いゲージなのでパネルより控えめにする。
     176              : const gaugeCornerRadius = 4
     177              : 
     178              : // gaugeWidget は1本のゲージ。与えられた矩形へ白枠を引き、比率ぶんの塗りをグラデーションの
     179              : // テクスチャで色掛けする。上が明るく下が暗い光沢になる。
     180              : type gaugeWidget struct {
     181              :         rect      image.Rectangle
     182              :         fill      *ebiten.Image
     183              :         ratio     float64
     184              :         fillColor color.RGBA
     185              :         border    color.RGBA
     186              : }
     187              : 
     188              : // Layout は uicore.Widget を満たす。
     189            1 : func (g *gaugeWidget) Layout(r image.Rectangle) { g.rect = r }
     190              : 
     191              : // Draw は uicore.Widget を満たす。比率ぶんの塗りを枠内へ敷き、白枠を重ねる。
     192              : func (g *gaugeWidget) Draw(cv uicore.Canvas) {
     193            1 :         fillW := int(float64(g.rect.Dx()) * g.ratio)
     194            1 :         if fillW > 0 && g.fill != nil {
     195              :                 // 塗りは上下1pxだけ内へ寄せ、枠の線と角の丸みに塗りが食み出さないようにする
     196            1 :                 dst := image.Rect(g.rect.Min.X, g.rect.Min.Y+1, g.rect.Min.X+fillW, g.rect.Max.Y-1)
     197            1 :                 cv.DrawImageTintedRect(dst, g.fill, color.NRGBA(g.fillColor))
     198            1 :         }
     199            1 :         cv.StrokeRect(g.rect, 1, g.border, uicore.RectOptions{Radius: gaugeCornerRadius})
     200              : }
     201              : 
     202              : // Children は uicore.Widget を満たす。子は持たない。
     203            0 : func (g *gaugeWidget) Children() []uicore.Widget { return nil }
     204              : 
     205              : // drawBottomRightStack は現在地・周囲気温・所持重量・所持通貨を右下、メッセージエリアの上へ下から積んで描く。
     206              : // 画面端からの逆算や段の手積みをせず、下寄せの FlexColumn へ流して各行を右端に揃える。先頭の Grow
     207              : // スペーサが上の余りを吸収して全体を下端へ押し、末尾スペーサがメッセージエリアの高さを確保する。
     208              : // 温度は場所依存なので、屋内へ入る・火に近づく効果がその場で読める。囲われラベルは白、温度は快適帯の
     209              : // 内外の色、重量は積載率で色を変える。
     210              : func (info *GameInfo) drawBottomRightStack(cv uicore.Canvas, data GameInfoData) {
     211            1 :         face := info.bodyFace
     212            1 :         outline := theme.HUDTextOutline
     213            1 : 
     214            1 :         items := []uicore.FlexItem{{Grow: true}}
     215            1 : 
     216            1 :         if data.AreaLabel != "" {
     217            0 :                 _, ah := uicore.MeasureText(data.AreaLabel, face)
     218            0 :                 items = append(items,
     219            0 :                         uicore.FlexItem{W: &uicore.Text{Value: data.AreaLabel, Face: face, Color: theme.TextPrimary, OutlineColor: outline, Align: uicore.AlignRight}, Height: ah},
     220            0 :                         uicore.FlexItem{Height: theme.Space2},
     221            0 :                 )
     222            0 :         }
     223              : 
     224            1 :         if data.AmbientTempVisible {
     225            1 :                 labelText := data.WeatherName + " " + data.AmbientShelterLabel + " "
     226            1 :                 tempText := fmt.Sprintf("%d%s", data.AmbientTemp, consts.IconDegree)
     227            1 :                 labelW, h := uicore.MeasureText(labelText, face)
     228            1 :                 tempW, _ := uicore.MeasureText(tempText, face)
     229            1 :                 // 先頭0幅列が余り幅を吸収し、ラベルと温度を右端へ寄せる
     230            1 :                 row := uicore.Row([]int{0, labelW, tempW},
     231            1 :                         uicore.NewGroup(),
     232            1 :                         &uicore.Text{Value: labelText, Face: face, Color: theme.TextPrimary, OutlineColor: outline},
     233            1 :                         &uicore.Text{Value: tempText, Face: face, Color: data.AmbientTempColor, OutlineColor: outline},
     234            1 :                 )
     235            1 :                 items = append(items, uicore.FlexItem{W: row, Height: h}, uicore.FlexItem{Height: theme.Space2})
     236            1 :         }
     237              : 
     238            1 :         weightText := fmt.Sprintf("%s / %s", data.PlayerWeight.KgString(), data.PlayerMaxWeight.KgString())
     239            1 :         _, wh := uicore.MeasureText(weightText, face)
     240            1 :         items = append(items,
     241            1 :                 uicore.FlexItem{W: &uicore.Text{Value: weightText, Face: face, Color: weightColor(data), OutlineColor: outline, Align: uicore.AlignRight}, Height: wh},
     242            1 :                 uicore.FlexItem{Height: theme.Space2},
     243            1 :         )
     244            1 : 
     245            1 :         currencyText := data.Currency.String()
     246            1 :         _, ch := uicore.MeasureText(currencyText, face)
     247            1 :         items = append(items, uicore.FlexItem{W: &uicore.Text{Value: currencyText, Face: face, Color: theme.TextPrimary, OutlineColor: outline, Align: uicore.AlignRight}, Height: ch})
     248            1 : 
     249              :         // スタックの下端をメッセージログ枠の上端の1余白ぶん上へ置く
     250            1 :         items = append(items, uicore.FlexItem{Height: data.MessageAreaHeight + theme.Space4})
     251            1 : 
     252            1 :         inner := image.Rect(0, 0, data.ScreenDimensions.Width-theme.Space4, data.ScreenDimensions.Height)
     253            1 :         uicore.FlexColumn(inner, items)
     254            1 :         drawFlexItems(cv, items)
     255              : }
     256              : 
     257              : // weightColor は所持重量の文字色を返す。積載超過は赤、8割超は黄、通常は白。
     258              : func weightColor(data GameInfoData) color.RGBA {
     259            1 :         if data.PlayerMaxWeight <= 0 {
     260            1 :                 return theme.TextPrimary
     261            1 :         }
     262            1 :         switch ratio := float64(data.PlayerWeight) / float64(data.PlayerMaxWeight); {
     263              :         case ratio > 1.0:
     264            1 :                 return theme.HUDWeightDanger
     265              :         case ratio > 0.8:
     266            1 :                 return theme.HUDWeightWarning
     267              :         default:
     268            1 :                 return theme.TextPrimary
     269              :         }
     270              : }
     271              : 
     272              : // drawFlexItems は FlexColumn/Row で矩形が確定済みの各行 Widget を描く。W が nil のスペーサ行は飛ばす。
     273              : func drawFlexItems(cv uicore.Canvas, items []uicore.FlexItem) {
     274            1 :         for _, it := range items {
     275            1 :                 if it.W != nil {
     276            1 :                         it.W.Draw(cv)
     277            1 :                 }
     278              :         }
     279              : }
        

Generated by: LCOV version 2.0-1