/
column.go
141 lines (117 loc) · 3.4 KB
/
column.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
package ui
import (
// "fmt"
"image"
"github.com/go-gl/glfw/v3.3/glfw"
)
type Column struct {
Properties Properties
Style Style
Children []UIElement
Image string
}
func (column Column) Initialize(skip SkipAlignment) UIElement {
column.Properties = DefaultProperties(column.Properties, skip, UIColumn)
column.Style = DefaultStyle(column.Style)
return column
}
func (column Column) Draw(img *image.RGBA, window *glfw.Window) []Area {
// fmt.Println("--------------------")
areas := []Area{}
if !column.Properties.Initialized {
column = column.Initialize(SkipAlignmentNone).(Column)
}
column = ApplyRelative(column).(Column)
column = ApplyAlignment(column).(Column)
column = ApplyPadding(column).(Column)
for i, child := range column.Children {
child = child.SetParent(&column.Properties)
column.Children[i] = child.Initialize(SkipAlignmentVert)
}
// fmt.Println("Column")
// fmt.Println(column.Properties)
areas = append(areas, Draw(img, window, column))
availableHeight := column.Properties.Size.Height
maxHeight := column.Properties.Size.Height
if column.Properties.Size.Scale == ScaleRelative {
availableHeight = column.Properties.Size.Height * maxHeight / 100
}
// Compute the available width
for _, child := range column.Children {
childProps := child.GetProperties()
if childProps.Size.Scale == ScalePixel {
availableHeight -= childProps.Size.Height
}
}
// Compute the total percentage of width required by the children
childrenHeight := 0
for _, child := range column.Children {
childProps := child.GetProperties()
if childProps.Size.Scale == ScaleRelative {
childrenHeight += childProps.Size.Height
}
}
// Compute the width of each child
for i, child := range column.Children {
childProps := child.GetProperties()
if childProps.Size.Scale == ScaleRelative {
column.Children[i] = child.SetProperties(
Size{
Scale: ScalePixel,
Width: column.Properties.Size.Width,
Height: childProps.Size.Height * availableHeight / childrenHeight,
},
Point{
X: childProps.Center.X,
Y: childProps.Center.Y,
},
)
}
}
// Compute the center of each child
currentY := column.Properties.Center.Y - maxHeight/2
for i, child := range column.Children {
childProps := child.GetProperties()
pixelHeight := childProps.Size.Height
if childProps.Size.Scale == ScaleRelative {
pixelHeight = childProps.Size.Height * availableHeight / childrenHeight
}
column.Children[i] = child.SetProperties(
Size{
Scale: childProps.Size.Scale,
Width: childProps.Size.Width,
Height: childProps.Size.Height,
},
Point{
X: column.Properties.Center.X,
Y: currentY + pixelHeight/2,
},
)
currentY += pixelHeight
}
for _, child := range column.Children {
// fmt.Println("child")
// fmt.Println(child)
areas = append(areas, child.Draw(img, window)...)
}
return areas
}
func (column Column) SetProperties(size Size, center Point) UIElement {
column.Properties.Size = size
column.Properties.Center = center
return column
}
func (column Column) SetParent(parent *Properties) UIElement {
column.Properties.Parent = parent
return column
}
func (column Column) GetProperties() Properties {
return column.Properties
}
func (column Column) ToString() string {
result := column.Properties.ToString() + column.Style.ToString()
for _, child := range column.Children {
result += child.ToString()
}
return result
}