-
Notifications
You must be signed in to change notification settings - Fork 0
/
view.go
402 lines (371 loc) · 13.5 KB
/
view.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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
package game_ui
import (
"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/vector"
"image"
"image/color"
)
type viewComponent struct {
components []Component
size *image.Point
style ViewStyle
extraStyles []ViewStyle
drawnArea image.Rectangle
screenSize image.Point
}
type View = *viewComponent
type ViewStyle struct {
/* top_left top_right bottom_right bottom_left */
BackgroundColor, BorderColor *[4]color.Color
/* top right bottom left */
Padding, Margin, BorderWidth *[4]sizeSeg
/* top_left top_right bottom_right bottom_left */
Radius *[4]int
Width, Height *sizeSeg
Direction *DirectionType
PositionHorizontal *PositionType
PositionVertical *PositionType
IsFloating bool
}
type DirectionType = string
const (
Horizontal DirectionType = "horizontal"
Vertical DirectionType = "vertical"
)
type PositionType = string
const (
First PositionType = "first"
Center PositionType = "center"
Last PositionType = "last"
)
func mergeViewStyle(target ViewStyle, styles []ViewStyle) ViewStyle {
for i := range styles {
if styles[i].BackgroundColor != nil {
target.BackgroundColor = styles[i].BackgroundColor
}
if styles[i].Padding != nil {
target.Padding = styles[i].Padding
}
if styles[i].Margin != nil {
target.Margin = styles[i].Margin
}
if styles[i].Radius != nil {
target.Radius = styles[i].Radius
}
if styles[i].BorderWidth != nil {
target.BorderWidth = styles[i].BorderWidth
}
if styles[i].BorderColor != nil {
target.BorderColor = styles[i].BorderColor
}
if styles[i].Width != nil {
target.Width = styles[i].Width
}
if styles[i].Height != nil {
target.Height = styles[i].Height
}
if styles[i].Direction != nil {
target.Direction = styles[i].Direction
}
if styles[i].PositionHorizontal != nil {
target.PositionHorizontal = styles[i].PositionHorizontal
}
if styles[i].PositionVertical != nil {
target.PositionVertical = styles[i].PositionVertical
}
target.IsFloating = target.IsFloating || styles[i].IsFloating
}
return target
}
func NewView(components []Component, styles ...ViewStyle) View {
var style = mergeViewStyle(ViewStyle{}, styles)
return &viewComponent{components: components, style: style, extraStyles: []ViewStyle{}}
}
func mixColorCode(r1, g1, b1, a1, r2, g2, b2, a2 uint32, rate float32) (uint32, uint32, uint32, uint32) {
var r = uint32(int64(r2) - int64(float64(int64(r2)-int64(r1))*float64(rate)))
var g = uint32(int64(g2) - int64(float64(int64(g2)-int64(g1))*float64(rate)))
var b = uint32(int64(b2) - int64(float64(int64(b2)-int64(b1))*float64(rate)))
var a = uint32(int64(a2) - int64(float64(int64(a2)-int64(a1))*float64(rate)))
return r, g, b, a
}
func (v View) PushStyle(style ViewStyle) {
v.extraStyles = append(v.extraStyles, style)
}
func (v View) PopStyle() {
if len(v.extraStyles) > 0 {
v.extraStyles = v.extraStyles[:len(v.extraStyles)-1]
}
}
func (v View) ReplaceStyle(position int, style ViewStyle) {
if position >= v.GetStylesCount() {
v.extraStyles = append(v.extraStyles, style)
} else {
v.extraStyles[position] = style
}
}
func (v View) GetStylesCount() int {
return len(v.extraStyles)
}
func getSizePx(screenSize image.Point, size [4]sizeSeg) (int, int, int, int) {
return calcSize(screenSize, size[0]), calcSize(screenSize, size[1]), calcSize(screenSize, size[2]), calcSize(screenSize, size[3])
}
func (v View) getContentSize() image.Point {
var x, y = 0, 0
var style = mergeViewStyle(v.style, v.extraStyles)
for _, component := range v.components {
if component.IsFloating() {
continue
}
var contentSize = component.GetSize()
if style.Direction == nil || *style.Direction == Vertical {
if x <= contentSize.X {
x = contentSize.X
}
y += contentSize.Y
} else {
if y <= contentSize.Y {
y = contentSize.Y
}
x += contentSize.X
}
}
for _, size := range []*[4]sizeSeg{style.Padding, style.Margin, style.BorderWidth} {
if size == nil {
continue
}
var top, right, bottom, left = getSizePx(v.screenSize, *size)
x += left + right
y += top + bottom
}
return image.Point{X: x, Y: y}
}
func (v View) GetSize() image.Point {
var point = v.getContentSize()
var x = point.X
var y = point.Y
var style = mergeViewStyle(v.style, v.extraStyles)
var width = 0
if style.Width != nil {
width = calcSize(v.screenSize, *style.Width)
}
var height = 0
if style.Height != nil {
height = calcSize(v.screenSize, *style.Height)
}
if x < width {
x = width
}
if y < height {
y = height
}
return image.Point{X: x, Y: y}
}
func (v View) Draw(screen *ebiten.Image, x, y int) {
var style = mergeViewStyle(v.style, v.extraStyles)
var marginTop, marginRight, marginBottom, marginLeft int
var borderTop, borderRight, borderBottom, borderLeft int
var paddingTop, paddingRight, paddingBottom, paddingLeft int
v.screenSize = screen.Bounds().Size()
if style.Margin != nil {
marginTop, marginRight, marginBottom, marginLeft = getSizePx(v.screenSize, *style.Margin)
}
if style.BorderWidth != nil {
borderTop, borderRight, borderBottom, borderLeft = getSizePx(v.screenSize, *style.BorderWidth)
}
if style.Padding != nil {
paddingTop, paddingRight, paddingBottom, paddingLeft = getSizePx(v.screenSize, *style.Padding)
}
var radiusTopLeft, radiusTopRight, radiusBottomRight, radiusBottomLeft int
if style.Radius != nil {
radiusTopLeft, radiusTopRight, radiusBottomRight, radiusBottomLeft = style.Radius[0], style.Radius[1], style.Radius[2], style.Radius[3]
}
var marginWidth = marginLeft + marginRight
var borderWidth = borderLeft + borderRight
var paddingWidth = paddingLeft + paddingRight
var marginHeight = marginTop + marginBottom
var borderHeight = borderTop + borderBottom
var paddingHeight = paddingTop + paddingBottom
var contentSize = v.getContentSize()
var size = v.GetSize()
var positionH float64 = 0
var positionV float64 = 0
var radiusMin = size.X - marginWidth - borderWidth
if (size.Y - marginHeight - borderHeight) < radiusMin {
radiusMin = size.Y - marginHeight - borderHeight
}
radiusMin /= 2
if radiusMin < radiusTopLeft {
radiusTopLeft = radiusMin
}
if radiusMin < radiusTopRight {
radiusTopRight = radiusMin
}
if radiusMin < radiusBottomRight {
radiusBottomRight = radiusMin
}
if radiusMin < radiusBottomLeft {
radiusBottomLeft = radiusMin
}
if style.PositionHorizontal != nil {
if *style.PositionHorizontal == Center {
positionH = 0.5
} else if *style.PositionHorizontal == Last {
positionH = 1
}
}
if style.PositionVertical != nil {
if *style.PositionVertical == Center {
positionV = 0.5
} else if *style.PositionVertical == Last {
positionV = 1
}
}
var contentLeft = int(positionH * float64(size.X-contentSize.X))
var contentTop = int(positionV * float64(size.Y-contentSize.Y))
var minX, minY = x + marginLeft, y + marginTop
v.drawnArea = image.Rect(minX, minY, minX+size.X-marginWidth, minY+size.Y-marginHeight)
// draw border
if style.BorderColor != nil {
var r1, g1, b1, a1 = style.BorderColor[0].RGBA()
var r2, g2, b2, a2 = style.BorderColor[1].RGBA()
var r3, g3, b3, a3 = style.BorderColor[2].RGBA()
var r4, g4, b4, a4 = style.BorderColor[3].RGBA()
if (borderWidth > 0 || borderHeight > 0) && (a1 > 0 || a2 > 0 || a3 > 0 || a4 > 0) {
var path = vector.Path{}
path.MoveTo(float32(radiusTopLeft), 0)
path.LineTo(float32(size.X-marginWidth-radiusTopRight), 0)
path.QuadTo(float32(size.X-marginWidth), 0, float32(size.X-marginWidth), float32(radiusTopRight))
path.LineTo(float32(size.X-marginWidth), float32(size.Y-marginHeight-radiusBottomRight))
path.QuadTo(float32(size.X-marginWidth), float32(size.Y-marginHeight), float32(size.X-marginWidth-radiusBottomRight), float32(size.Y-marginHeight))
path.LineTo(float32(radiusBottomLeft), float32(size.Y-marginHeight))
path.QuadTo(0, float32(size.Y-marginHeight), float32(0), float32(size.Y-marginHeight-radiusBottomLeft))
path.LineTo(float32(0), float32(radiusTopLeft))
path.QuadTo(0, 0, float32(radiusTopLeft), 0)
path.MoveTo(float32(borderLeft+radiusTopLeft), float32(borderTop))
path.LineTo(float32(size.X-marginWidth-borderRight-radiusTopRight), float32(borderTop))
path.QuadTo(float32(size.X-marginWidth-borderRight), float32(borderTop), float32(size.X-marginWidth-borderRight), float32(borderTop+radiusTopRight))
path.LineTo(float32(size.X-marginWidth-borderRight), float32(size.Y-marginHeight-borderBottom-radiusBottomRight))
path.QuadTo(float32(size.X-marginWidth-borderRight), float32(size.Y-marginHeight-borderBottom), float32(size.X-marginWidth-borderRight-radiusBottomRight), float32(size.Y-marginHeight-borderBottom))
path.LineTo(float32(borderLeft+radiusBottomLeft), float32(size.Y-marginHeight-borderBottom))
path.QuadTo(float32(borderLeft), float32(size.Y-marginHeight-borderBottom), float32(borderLeft), float32(size.Y-marginHeight-borderBottom-radiusBottomLeft))
path.LineTo(float32(borderLeft), float32(borderTop+radiusTopLeft))
path.QuadTo(float32(borderLeft), float32(borderTop), float32(borderLeft+radiusTopLeft), float32(borderTop))
path.Close()
var vs, is = path.AppendVerticesAndIndicesForFilling(nil, nil)
var minX, maxX, minY, maxY float32 = 0, 0, 0, 0
for i := range vs {
if minX < vs[i].DstX {
minX = vs[i].DstX
}
if maxX > vs[i].DstX {
maxX = vs[i].DstX
}
if minY < vs[i].DstY {
minY = vs[i].DstY
}
if maxY > vs[i].DstY {
maxY = vs[i].DstY
}
}
for i := range vs {
var rateX = (vs[i].DstX - minX) / (maxX - minX)
var rateY = (vs[i].DstY - minY) / (maxY - minY)
var rX1, gX1, bX1, aX1 = mixColorCode(r1, g1, b1, a1, r2, g2, b2, a2, rateX)
var rX2, gX2, bX2, aX2 = mixColorCode(r4, g4, b4, a4, r3, g3, b3, a3, rateX)
var r, g, b, a = mixColorCode(rX1, gX1, bX1, aX1, rX2, gX2, bX2, aX2, rateY)
vs[i].DstX += float32(x + marginLeft)
vs[i].DstY += float32(y + marginTop)
vs[i].ColorR = float32(r) / 0xffff
vs[i].ColorG = float32(g) / 0xffff
vs[i].ColorB = float32(b) / 0xffff
vs[i].ColorA = float32(a) / 0xffff
}
screen.DrawTriangles(vs, is, emptySubImage, &ebiten.DrawTrianglesOptions{
FillRule: ebiten.EvenOdd,
})
}
}
// draw base
if style.BackgroundColor != nil {
var r1, g1, b1, a1 = style.BackgroundColor[0].RGBA()
var r2, g2, b2, a2 = style.BackgroundColor[1].RGBA()
var r3, g3, b3, a3 = style.BackgroundColor[2].RGBA()
var r4, g4, b4, a4 = style.BackgroundColor[3].RGBA()
if a1 > 0 || a2 > 0 || a3 > 0 || a4 > 0 {
var path = vector.Path{}
path.MoveTo(float32(borderLeft+radiusTopLeft), float32(borderTop))
path.LineTo(float32(size.X-marginWidth-borderRight-radiusTopRight), float32(borderTop))
path.QuadTo(float32(size.X-marginWidth-borderRight), float32(borderTop), float32(size.X-marginWidth-borderRight), float32(borderTop+radiusTopRight))
path.LineTo(float32(size.X-marginWidth-borderRight), float32(size.Y-marginHeight-borderBottom-radiusBottomRight))
path.QuadTo(float32(size.X-marginWidth-borderRight), float32(size.Y-marginHeight-borderBottom), float32(size.X-marginWidth-borderRight-radiusBottomRight), float32(size.Y-marginHeight-borderBottom))
path.LineTo(float32(borderLeft+radiusBottomLeft), float32(size.Y-marginHeight-borderBottom))
path.QuadTo(float32(borderLeft), float32(size.Y-marginHeight-borderBottom), float32(borderLeft), float32(size.Y-marginHeight-borderBottom-radiusBottomLeft))
path.LineTo(float32(borderLeft), float32(borderTop+radiusTopLeft))
path.QuadTo(float32(borderLeft), float32(borderTop), float32(borderLeft+radiusTopLeft), float32(borderTop))
path.Close()
var vs, is = path.AppendVerticesAndIndicesForFilling(nil, nil)
var maxX, minX, maxY, minY float32 = 0, 0, 0, 0
for i := range vs {
if maxX > vs[i].DstX {
maxX = vs[i].DstX
}
if minX < vs[i].DstX {
minX = vs[i].DstX
}
if maxY > vs[i].DstY {
maxY = vs[i].DstY
}
if minY < vs[i].DstY {
minY = vs[i].DstY
}
}
for i := range vs {
var rateX = (vs[i].DstX - minX) / (maxX - minX)
var rateY = (vs[i].DstY - minY) / (maxY - minY)
var rX1, gX1, bX1, aX1 = mixColorCode(r1, g1, b1, a1, r2, g2, b2, a2, rateX)
var rX2, gX2, bX2, aX2 = mixColorCode(r4, g4, b4, a4, r3, g3, b3, a3, rateX)
var r, g, b, a = mixColorCode(rX1, gX1, bX1, aX1, rX2, gX2, bX2, aX2, rateY)
vs[i].DstX += float32(x + marginLeft)
vs[i].DstY += float32(y + marginTop)
vs[i].ColorR = float32(r) / 0xffff
vs[i].ColorG = float32(g) / 0xffff
vs[i].ColorB = float32(b) / 0xffff
vs[i].ColorA = float32(a) / 0xffff
}
screen.DrawTriangles(vs, is, emptySubImage, &ebiten.DrawTrianglesOptions{
FillRule: ebiten.EvenOdd,
})
}
}
var _x = marginLeft + borderLeft + paddingLeft + contentLeft
var _y = marginTop + borderTop + paddingTop + contentTop
for _, component := range v.components {
var componentSize = component.GetSize()
var __x = int(positionH * float64((contentSize.X-marginWidth-borderWidth-paddingWidth)-componentSize.X))
var __y = int(positionV * float64((contentSize.Y-marginHeight-borderHeight-paddingHeight)-componentSize.Y))
if style.Direction == nil || *style.Direction == Vertical {
__y = 0
} else {
__x = 0
}
component.Draw(screen, x+_x+__x, y+_y+__y)
if component.IsFloating() {
continue
}
if style.Direction == nil || *style.Direction == Vertical {
_y += componentSize.Y
} else {
_x += componentSize.X
}
}
}
func (v View) IsFloating() bool {
var style = mergeViewStyle(v.style, v.extraStyles)
return style.IsFloating
}
func (v View) Components() []Component {
return v.components
}
func (v View) Area() image.Rectangle {
return v.drawnArea
}