-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpanel.go
127 lines (104 loc) · 3.03 KB
/
panel.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package main
import (
"fmt"
"image/color"
"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/ebitenutil"
"github.com/hajimehoshi/ebiten/v2/vector"
)
// パネルの枠を表示するための構造体
type infoPanel struct {
game *Game
x, y int
width, height int
zindex int
icon *icon
unit infoer
buttons []*Button
drawDescriptionFn func(screen *ebiten.Image, x, y int)
}
func newInfoPanel(g *Game, w, h int) *infoPanel {
bottomMargin := 10
return &infoPanel{
game: g,
x: screenWidth/2 - w/2,
y: screenHeight - h - bottomMargin,
width: w,
height: h,
zindex: 60,
}
}
func (p *infoPanel) setIcon(i *icon) {
p.game.drawHandler.Remove(p.icon)
p.icon = i
if p.icon == nil {
return
}
}
type infoer interface {
Name() string
Health() int
}
func (p *infoPanel) setUnit(u infoer) {
p.unit = u
}
func (p *infoPanel) Remove(u infoer) {
if p.unit == u {
p.unit = nil
}
}
func (p *infoPanel) AddButton(b *Button) {
p.buttons = append(p.buttons, b)
p.game.clickHandler.Add(b)
}
func (p *infoPanel) RemoveButton(b *Button) {
for i, button := range p.buttons {
if button == b {
p.game.clickHandler.Remove(b)
p.buttons = append(p.buttons[:i], p.buttons[i+1:]...)
return
}
}
}
func (p *infoPanel) ClearButtons() {
for _, button := range p.buttons {
p.game.clickHandler.Remove(button)
}
p.buttons = nil
p.drawDescriptionFn = nil
}
func (p *infoPanel) Draw(screen *ebiten.Image) {
// 枠を描画
strokeWidth := float32(2)
vector.StrokeLine(screen, float32(p.x), float32(p.y), float32(p.x+p.width), float32(p.y), strokeWidth, color.White, true)
vector.StrokeLine(screen, float32(p.x), float32(p.y), float32(p.x), float32(p.y+p.height), strokeWidth, color.White, true)
vector.StrokeLine(screen, float32(p.x+p.width), float32(p.y), float32(p.x+p.width), float32(p.y+p.height), strokeWidth, color.White, true)
vector.StrokeLine(screen, float32(p.x), float32(p.y+p.height), float32(p.x+p.width), float32(p.y+p.height), strokeWidth, color.White, true)
// 枠の中を塗りつぶす
vector.DrawFilledRect(screen, float32(p.x), float32(p.y), float32(p.width), float32(p.height), color.RGBA{0, 0x45, 0, 0x90}, true)
// TODO: アイコンを描画
// ユニット名とHPを描画
if p.unit == nil {
return
}
p.icon.Draw(screen)
name, health := p.unit.Name(), p.unit.Health()
ebitenutil.DebugPrintAt(screen, fmt.Sprintf("%s", name), p.x+100+40, p.y+30)
ebitenutil.DebugPrintAt(screen, fmt.Sprintf("HP: %d", health), p.x+100+40, p.y+50)
// 家だったら現在のクレジットも表示する
if name == "House" {
ebitenutil.DebugPrintAt(screen, fmt.Sprintf("$: %d", p.game.credit), p.x+100+40, p.y+70)
}
// ボタンを描画
for _, button := range p.buttons {
button.Draw(screen)
}
// description を描画
// ボタンの右側に表示するので、ボタンの数だけ右にずらす
if p.drawDescriptionFn != nil {
p.drawDescriptionFn(screen, 255+infoPanelHeight*len(p.buttons), p.y+30)
}
}
func (p *infoPanel) ZIndex() int {
return p.zindex
}