-
Notifications
You must be signed in to change notification settings - Fork 0
/
gui.go
198 lines (177 loc) · 4.79 KB
/
gui.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
package ebitenlg
import (
"bytes"
"image/color"
"log"
"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/text/v2"
"github.com/hajimehoshi/ebiten/v2/vector"
"golang.org/x/image/font/gofont/goregular"
)
var (
textFaceSource *text.GoTextFaceSource
)
type HorizontalAlign int
const (
HorizontalAlignLeft HorizontalAlign = iota
HorizontalAlignRight
)
func init() {
s, err := text.NewGoTextFaceSource(bytes.NewReader(goregular.TTF))
if err != nil {
log.Fatal(err)
}
textFaceSource = s
}
type GUI struct {
components []Component
whiteImage *ebiten.Image
title string
X float32
Y float32
Width float32
ComponentHeight float32
Scale float32
HorizontalAlign HorizontalAlign
}
type Component interface {
Label() string
Update(x, y, width, height, scale float32)
Draw(image *ebiten.Image, whiteImage *ebiten.Image, top, left, width, height, scale float32)
}
func NewGUI() *GUI {
whiteImage := ebiten.NewImage(3, 3)
whiteImage.Fill(color.White)
gui := &GUI{
whiteImage: whiteImage,
title: "Controls",
Width: 200,
ComponentHeight: 24,
X: 0,
Y: 0,
Scale: 1,
HorizontalAlign: HorizontalAlignLeft,
}
return gui
}
func (g *GUI) Update() {
cx, cy := ebiten.CursorPosition()
x := g.X
y := g.Y
if g.HorizontalAlign == HorizontalAlignRight {
x = g.X - g.Width*g.Scale
}
y += g.ComponentHeight * g.Scale
for _, c := range g.components {
c.Update(float32(cx)-x, float32(cy)-y, g.Width, g.ComponentHeight, g.Scale)
y += g.ComponentHeight * g.Scale
}
}
func (g *GUI) Draw(image *ebiten.Image) {
var x, y float32
x = g.X
y = g.Y
if g.HorizontalAlign == HorizontalAlignRight {
x = g.X - g.Width*g.Scale
}
// Draw background
drawRect(
image,
g.whiteImage,
x, y,
g.Width*g.Scale,
g.ComponentHeight*g.Scale*float32(len(g.components)+1),
color.NRGBA{0x31, 0x31, 0x31, 0xff},
)
// Draw title
drawRect(
image,
g.whiteImage,
x, y,
g.Width*g.Scale,
g.ComponentHeight*g.Scale,
color.NRGBA{0x17, 0x17, 0x17, 0xff},
)
textPadding := 5.0 * g.Scale
fontSize := g.ComponentHeight*g.Scale - textPadding*2
drawText(image, g.title, x+textPadding, y+textPadding, fontSize, color.NRGBA{R: 0xeb, G: 0xeb, B: 0xeb, A: 0xff})
// Draw components
y += g.ComponentHeight * g.Scale
for _, c := range g.components {
// Draw label
textPadding := 5.0 * g.Scale
fontSize := g.ComponentHeight*g.Scale - textPadding*2
drawText(image, c.Label(), x+textPadding, y+textPadding, fontSize, color.NRGBA{R: 0xeb, G: 0xeb, B: 0xeb, A: 0xff})
// Draw component
c.Draw(image, g.whiteImage, x, y, g.Width, g.ComponentHeight, g.Scale)
y += g.ComponentHeight * g.Scale
}
}
func (g *GUI) SetTitle(t string) {
g.title = t
}
func drawLine(screen *ebiten.Image, whiteImage *ebiten.Image, x1, y1, x2, y2, width float32, c color.NRGBA) {
path := vector.Path{}
path.MoveTo(x1, y1)
path.LineTo(x2, y2)
path.Close()
sop := &vector.StrokeOptions{}
sop.Width = width
sop.LineJoin = vector.LineJoinMiter
vs, is := path.AppendVerticesAndIndicesForStroke(nil, nil, sop)
for i := range vs {
vs[i].SrcX = 1
vs[i].SrcY = 1
vs[i].ColorR = float32(c.R) / float32(0xff)
vs[i].ColorG = float32(c.G) / float32(0xff)
vs[i].ColorB = float32(c.B) / float32(0xff)
vs[i].ColorA = float32(c.A) / float32(0xff)
}
op := &ebiten.DrawTrianglesOptions{}
op.FillRule = ebiten.FillAll
// AntiAlias is not used to reduce the number of draw-triangles issued.
// op.AntiAlias = true
screen.DrawTriangles(vs, is, whiteImage, op)
}
func drawRect(screen *ebiten.Image, whiteImage *ebiten.Image, x, y, width, height float32, c color.NRGBA) {
path := vector.Path{}
path.MoveTo(x, y)
path.LineTo(x, y+height)
path.LineTo(x+width, y+height)
path.LineTo(x+width, y)
path.Close()
drawFill(screen, whiteImage, path, c)
}
func drawFill(screen *ebiten.Image, whiteImage *ebiten.Image, path vector.Path, c color.NRGBA) {
vs, is := path.AppendVerticesAndIndicesForFilling(nil, nil)
for i := range vs {
vs[i].SrcX = 1
vs[i].SrcY = 1
vs[i].ColorR = float32(c.R) / float32(0xff)
vs[i].ColorG = float32(c.G) / float32(0xff)
vs[i].ColorB = float32(c.B) / float32(0xff)
vs[i].ColorA = float32(c.A) / float32(0xff)
}
op := &ebiten.DrawTrianglesOptions{}
op.FillRule = ebiten.FillAll
// AntiAlias is not used to reduce the number of draw-triangles issued.
// op.AntiAlias = true
screen.DrawTriangles(vs, is, whiteImage, op)
}
func drawText(image *ebiten.Image, str string, x, y, fontSize float32, c color.NRGBA) {
op := &text.DrawOptions{}
op.GeoM.Translate(float64(x), float64(y))
op.ColorScale.ScaleWithColor(c)
op.LineSpacing = 0
op.PrimaryAlign = text.AlignStart
fontFace := &text.GoTextFace{
Source: textFaceSource,
Size: float64(fontSize),
}
text.Draw(
image,
str,
fontFace,
op,
)
}