-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathui.go
267 lines (224 loc) · 9.34 KB
/
ui.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
package archetype
import (
"amaru/assets"
"image"
"image/color"
uiimage "github.com/ebitenui/ebitenui/image"
"github.com/ebitenui/ebitenui/widget"
"github.com/fogleman/gg"
"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/text"
)
func DrawMainMenuRoundedRect(screen *ebiten.Image, x, y, width, height, borderRadius float64, fillColor, borderColor color.Color, borderWidth float64, label string) *ebiten.Image {
// Create a gg.Context to draw the rounded rectangle and border
img := image.NewRGBA(image.Rect(0, 0, int(width+2*borderWidth), int(height+2*borderWidth)))
gc := gg.NewContextForRGBA(img)
// Draw the rounded border
gc.SetColor(borderColor)
gc.DrawRoundedRectangle(borderWidth/2, borderWidth/2, width+borderWidth, height+borderWidth, borderRadius+borderWidth/2)
gc.SetLineWidth(borderWidth)
gc.Stroke()
// Draw the rounded rectangle
gc.SetColor(fillColor)
gc.DrawRoundedRectangle(borderWidth, borderWidth, width, height, borderRadius)
gc.Fill()
menuRectangle := ebiten.NewImageFromImage(img)
// Draw the img onto the screen
op := &ebiten.DrawImageOptions{}
op.GeoM.Translate(x-borderWidth/2, y-borderWidth/2)
if label != "" {
gc.SetFontFace(assets.MainMidFont)
gc.SetColor(borderColor)
textWidth, textHeight := gc.MeasureString(label)
textX := x + (width-textWidth)/2
textY := y - textHeight // Adjust the textY position
text.Draw(
screen,
label,
assets.MainMidFont,
int(textX),
int(textY),
borderColor,
)
}
return menuRectangle
}
func CreateRoundedButtonImages(width, height, borderRadius float64, fillColor, borderColor, pressedBorderColor color.Color, hoverBorderColor color.Color, borderWidth float64) *widget.ButtonImage {
idleImg := ebiten.NewImage(int(width), int(height))
idleImg = DrawRoundedRect(idleImg, borderWidth, borderWidth, width-2*borderWidth, height-2*borderWidth, borderRadius, fillColor, borderColor, borderWidth)
hoverImg := ebiten.NewImage(int(width), int(height))
hoverImg = DrawRoundedRect(hoverImg, borderWidth, borderWidth, width-2*borderWidth, height-2*borderWidth, borderRadius, fillColor, hoverBorderColor, borderWidth)
pressedImg := ebiten.NewImage(int(width), int(height))
pressedImg = DrawRoundedRect(pressedImg, borderWidth, borderWidth, width-2*borderWidth, height-2*borderWidth, borderRadius, fillColor, pressedBorderColor, borderWidth)
// Create the NineSlice images for idle and pressed states
widths := [3]int{int(borderRadius), int(width) - 2*int(borderRadius), int(borderWidth)}
heights := [3]int{int(borderRadius), int(height) - 2*int(borderRadius), int(borderWidth)}
idleNineSlice := uiimage.NewNineSlice(idleImg, widths, heights)
hoverNineSlice := uiimage.NewNineSlice(hoverImg, widths, heights)
pressedNineSlice := uiimage.NewNineSlice(pressedImg, widths, heights)
return &widget.ButtonImage{
Idle: idleNineSlice,
Hover: hoverNineSlice,
Pressed: pressedNineSlice,
}
}
func CreateRoundedTextInputImages(width, height, borderRadius float64, fillColor, borderColor, disabledBorderColor color.Color, borderWidth float64) *widget.TextInputImage {
idleImg := ebiten.NewImage(int(width), int(height))
idleImg = DrawRoundedRect(idleImg, borderWidth, borderWidth, width-2*borderWidth, height-2*borderWidth, borderRadius, fillColor, borderColor, borderWidth)
disabledImg := ebiten.NewImage(int(width), int(height))
disabledImg = DrawRoundedRect(disabledImg, borderWidth, borderWidth, width-2*borderWidth, height-2*borderWidth, borderRadius, fillColor, disabledBorderColor, borderWidth)
// Create the NineSlice images for idle and pressed states
widths := [3]int{int(borderRadius), int(width) - 2*int(borderRadius), int(borderWidth)}
heights := [3]int{int(borderRadius), int(height) - 2*int(borderRadius), int(borderWidth)}
idleNineSlice := uiimage.NewNineSlice(idleImg, widths, heights)
hoverNineSlice := uiimage.NewNineSlice(disabledImg, widths, heights)
return &widget.TextInputImage{
Disabled: hoverNineSlice,
Idle: idleNineSlice,
}
}
func DrawRoundedRect(screen *ebiten.Image, x, y, width, height, borderRadius float64, fillColor, borderColor color.Color, borderWidth float64) *ebiten.Image {
// Create a gg.Context to draw the rounded rectangle and border
img := image.NewRGBA(image.Rect(0, 0, int(width+2*borderWidth), int(height+2*borderWidth)))
gc := gg.NewContextForRGBA(img)
// Draw the rounded border
gc.SetColor(borderColor)
gc.DrawRoundedRectangle(borderWidth/2, borderWidth/2, width+borderWidth, height+borderWidth, borderRadius+borderWidth/2)
gc.SetLineWidth(borderWidth)
gc.Stroke()
// Draw the rounded rectangle
gc.SetColor(fillColor)
gc.DrawRoundedRectangle(borderWidth, borderWidth, width, height, borderRadius)
gc.Fill()
// Draw a transparent inner rectangle
gc.SetColor(color.Transparent)
gc.DrawRectangle(borderWidth+borderRadius, borderWidth, width-2*borderRadius, height)
gc.Fill()
return ebiten.NewImageFromImage(img)
}
func UpdateCursorImage(isPointer bool) {
if isPointer {
ebiten.SetCursorShape(ebiten.CursorShapePointer)
} else {
ebiten.SetCursorShape(ebiten.CursorShapeDefault)
}
}
func NewTransparentEbitenImage(width, height int) *ebiten.Image {
// Create a new RGBA image
img := image.NewRGBA(image.Rect(0, 0, width, height))
// Fill it with a transparent color
for y := 0; y < height; y++ {
for x := 0; x < width; x++ {
img.SetRGBA(x, y, color.RGBA{0, 0, 0, 0})
}
}
// Convert it to an ebiten.Image
ebitenImage := ebiten.NewImageFromImage(img)
return ebitenImage
}
func NewColoredEbitenImage(width, height int, color color.RGBA) *ebiten.Image {
// Create a new RGBA image
img := image.NewRGBA(image.Rect(0, 0, width, height))
// Fill it with a transparent color
for y := 0; y < height; y++ {
for x := 0; x < width; x++ {
img.SetRGBA(x, y, color)
}
}
// Convert it to an ebiten.Image
ebitenImage := ebiten.NewImageFromImage(img)
return ebitenImage
}
func ColorToRGBA(c color.Color) color.RGBA {
r, g, b, a := c.RGBA()
return color.RGBA{
R: uint8(r / 257), // or r >> 8
G: uint8(g / 257), // or g >> 8
B: uint8(b / 257), // or b >> 8
A: uint8(a / 257), // or a >> 8
}
}
func CreateSVGImageButton(svgKey string, tooltipText string, targetInsets *widget.Insets, onClick func()) (*widget.Container, *widget.Button) {
containerInsets := targetInsets
if containerInsets == nil {
containerInsets = &widget.Insets{
Left: 15,
Right: 15,
Top: 5,
Bottom: 5,
}
}
tt := widget.NewContainer(
widget.ContainerOpts.BackgroundImage(uiimage.NewNineSlice(NewToolTipImage(), [3]int{19, 6, 13}, [3]int{19, 5, 13})),
widget.ContainerOpts.Layout(widget.NewRowLayout(
widget.RowLayoutOpts.Direction(widget.DirectionVertical),
widget.RowLayoutOpts.Padding(*containerInsets),
widget.RowLayoutOpts.Spacing(2),
)),
)
text := widget.NewText(
widget.TextOpts.Text(tooltipText, assets.MainFont, assets.BlueColor),
)
tt.AddChild(text)
image := assets.MustLoadImage(svgKey)
// Create the button without text
button := widget.NewButton(
widget.ButtonOpts.WidgetOpts(widget.WidgetOpts.ToolTip(widget.NewToolTip(
widget.ToolTipOpts.Content(tt),
))),
widget.ButtonOpts.Image(loadButtonImage()),
widget.ButtonOpts.ClickedHandler(func(args *widget.ButtonClickedEventArgs) {
if onClick != nil {
onClick()
}
}),
widget.ButtonOpts.WidgetOpts(
widget.WidgetOpts.LayoutData(widget.AnchorLayoutData{
HorizontalPosition: widget.AnchorLayoutPositionCenter,
VerticalPosition: widget.AnchorLayoutPositionCenter,
}),
widget.WidgetOpts.CursorHovered("buttonHover"),
widget.WidgetOpts.CursorPressed("buttonPressed"),
),
)
imageWidget := widget.NewGraphic(widget.GraphicOpts.Image(image))
// Bundle the button and the graphics widget together using a stacked layout container
buttonStackedLayout := widget.NewContainer(
widget.ContainerOpts.Layout(widget.NewStackedLayout()),
// instruct the container's anchor layout to center the button both horizontally and vertically;
// since our button is a 2-widget object, we add the anchor info to the wrapping container
// instead of the button
widget.ContainerOpts.WidgetOpts(widget.WidgetOpts.LayoutData(widget.AnchorLayoutData{
HorizontalPosition: widget.AnchorLayoutPositionCenter,
VerticalPosition: widget.AnchorLayoutPositionCenter,
})),
)
// Add button and image widget to the stacked layout
buttonStackedLayout.AddChild(button)
buttonStackedLayout.AddChild(imageWidget)
return buttonStackedLayout, button
}
func loadButtonImage() *widget.ButtonImage {
idle := uiimage.NewNineSliceColor(color.NRGBA{R: 255, G: 255, B: 255, A: 0})
hover := uiimage.NewNineSliceColor(color.NRGBA{R: 255, G: 255, B: 255, A: 0})
pressed := uiimage.NewNineSliceColor(color.NRGBA{R: 255, G: 255, B: 255, A: 0})
return &widget.ButtonImage{
Idle: idle,
Hover: hover,
Pressed: pressed,
}
}
func NewToolTipImage() *ebiten.Image {
width, height := 38, 37
radius := float64(5)
dc := gg.NewContext(width, height)
dc.SetColor(color.White)
dc.DrawRoundedRectangle(0, 0, float64(width), float64(height), radius)
// Draw square corners on the bottom left, top right, and bottom right
dc.DrawRectangle(0, float64(height)-radius, radius, radius) // Bottom Left
dc.DrawRectangle(float64(width)-radius, 0, radius, radius) // Top Right
dc.DrawRectangle(float64(width)-radius, float64(height)-radius, radius, radius) // Bottom Right
dc.Fill()
img := ebiten.NewImageFromImage(dc.Image())
return img
}