-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathelementdata.go
More file actions
335 lines (258 loc) · 6.24 KB
/
Copy pathelementdata.go
File metadata and controls
335 lines (258 loc) · 6.24 KB
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
package glui
import (
"github.com/veandco/go-sdl2/sdl"
)
// base type for all elements
type ElementData struct {
parent Element
children []Element
Root *Frame
p1Tris []uint32
p2Tris []uint32
closerThan []Element // these elements must get a smaller z-index than self (i.e. be further away from viewer)
evtListeners map[string]EventListener // only one eventlistener per event type
// basic positioning settings
width int
height int
padding [4]int
spacing int
// state
rect Rect
zIndex int // returned by succesful Hit test, must be normalized before using in Pos
visible bool
enabled bool
deleted bool
}
func newElementData(frame *Frame, nInitTris1 int, nInitTris2 int) ElementData {
p1Tris := make([]uint32, 0)
if nInitTris1 > 0 {
p1Tris = frame.P1.Alloc(nInitTris1)
}
p2Tris := make([]uint32, 0)
if nInitTris2 > 0 {
p2Tris = frame.P2.Alloc(nInitTris2)
}
return ElementData{
nil,
make([]Element, 0),
frame, p1Tris, p2Tris,
make([]Element, 0),
make(map[string]EventListener),
0, 0, [4]int{0, 0, 0, 0}, 0,
Rect{0, 0, 0, 0}, -1, true, true, false,
}
}
func NewElementData(nInitTris1 int, nInitTris2 int) ElementData {
frame := ActiveFrame()
return newElementData(frame, nInitTris1, nInitTris2)
}
func (e *ElementData) ZIndex() int {
return e.zIndex
}
func (e *ElementData) Visible() bool {
return e.visible
}
func (e *ElementData) nChildren() int {
return len(e.children)
}
func (e *ElementData) nVisibleChildren() int {
c := 0
for _, child := range e.children {
if child.Visible() {
c += 1
}
}
return c
}
func (e *ElementData) visibleChildren() []Element {
els := make([]Element, 0)
for _, child := range e.children {
if child.Visible() {
els = append(els, child)
}
}
return els
}
func (e *ElementData) GetSize() (int, int) {
return e.width, e.height
}
func (e *ElementData) Hide() {
for _, tri := range e.p1Tris {
e.Root.P1.SetTriType(tri, VTYPE_HIDDEN)
}
for _, tri := range e.p2Tris {
e.Root.P2.SetTriType(tri, VTYPE_HIDDEN)
}
for _, child := range e.children {
child.Hide()
}
e.visible = false
}
func (e *ElementData) Show() {
for _, child := range e.children {
child.Show()
}
e.visible = true
}
func (e *ElementData) Enable() {
for _, child := range e.children {
child.Enable()
}
e.enabled = true
}
func (e *ElementData) Disable() {
for _, child := range e.children {
child.Disable()
}
e.enabled = false
}
func (e *ElementData) ButtonCursor(x, y int, enabled bool) int {
if enabled {
return sdl.SYSTEM_CURSOR_HAND
} else {
return e.Cursor(x, y)
}
}
func (e *ElementData) Cursor(x, y int) int {
return -1
}
func (e *ElementData) Tooltip() string {
return ""
}
func (e *ElementData) RegisterParent(parent Element) {
if parent == nil {
e.parent = nil
} else if e.parent != nil {
panic("parent already registered")
} else {
e.parent = parent
}
}
func (e *ElementData) GetEventListener(name string) EventListener {
l, ok := e.evtListeners[name]
if !ok {
return nil
} else {
return l
}
}
func (e *ElementData) Children() []Element {
return e.children
}
func (e *ElementData) Parent() Element {
return e.parent
}
func (e *ElementData) IsHit(x, y int) bool {
return e.visible && e.rect.Hit(x, y)
}
// -1 -> no hit
func (e *ElementData) Hit(x, y int) int {
if e.IsHit(x, y) {
return e.zIndex
} else {
return -1
}
}
func (e *ElementData) InitRect(w, h int) (int, int) {
e.rect = Rect{0, 0, w, h}
return w, h
}
func (e *ElementData) Rect() Rect {
return e.rect
}
func (e *ElementData) Translate(dx, dy int) {
for _, tri := range e.p1Tris {
e.Root.P1.TranslateTri(tri, dx, dy, 0.0)
}
for _, tri := range e.p2Tris {
e.Root.P2.TranslateTri(tri, dx, dy, 0.0)
}
for _, child := range e.children {
child.Translate(dx, dy)
}
e.rect = e.rect.Translate(dx, dy)
}
// default positioning of children
// placement elements like Hor can provide better control
func (e *ElementData) CalcPosChildren(maxWidth, maxHeight, maxZIndex int) (int, int) {
y := e.padding[0]
maxW := 0
for _, child := range e.children {
if child.Visible() {
w, dy := child.CalcPos(maxWidth - e.padding[1] - e.padding[3], maxHeight - y - e.padding[2], maxZIndex)
child.Translate(e.padding[3], y)
y += dy + e.spacing
if w > maxW {
maxW = w
}
}
}
return maxW + e.padding[1] + e.padding[3], y + e.padding[2]
}
func (e *ElementData) Animate(tick uint64) {
for _, child := range e.children {
child.Animate(tick)
}
}
func (e *ElementData) Delete() {
for _, child := range e.children {
child.Delete()
}
e.Root.P1.Dealloc(e.p1Tris)
e.Root.P2.Dealloc(e.p2Tris)
e.deleted = true
e.p1Tris = nil
e.p2Tris = nil
e.parent = nil
e.Root.ForcePosDirty()
e.Root = nil // so error is thrown if deletion is tried again
}
func (e *ElementData) Deleted() bool {
return e.deleted
}
func (e *ElementData) ClearChildren() {
for _, child := range e.children {
child.Delete()
}
e.children = []Element{}
}
func normalizeZIndex(idx int, maxZIndex int) float32 {
return float32(maxZIndex - idx)/float32(maxZIndex)
}
func (e *ElementData) Z(maxZIndex int) float32 {
return normalizeZIndex(e.zIndex, maxZIndex)
}
func (e *ElementData) SetButtonStyle() {
e.Root.P1.setButtonStyle(e.p1Tris)
}
func (e *ElementData) SetBorderedElementPos(w, h, t, maxZIndex int) {
e.Root.P1.setBorderedElementPos(e.p1Tris, w, h, t, e.Z(maxZIndex))
}
func (e *ElementData) SetButtonPos(maxWidth, maxHeight, maxZIndex int) (int, int) {
t := e.Root.P1.Skin.ButtonBorderThickness()
w, h := e.GetSize()
if w > maxWidth {
w = maxWidth
}
if h > maxHeight {
h = maxHeight
}
e.SetBorderedElementPos(w, h, t, maxZIndex)
e.CalcPosChildren(w, h, maxZIndex)
return e.InitRect(w, h)
}
func (e *ElementData) IsFocusable() bool {
return e.GetEventListener("focus") != nil && e.Visible()
}
func (e *ElementData) Crop(r Rect) {
e.rect = e.rect.Common(r)
for _, tri := range e.p1Tris {
e.Root.P1.CropTri(tri, r)
}
for _, tri := range e.p2Tris {
e.Root.P2.CropTri(tri, r)
}
for _, child := range e.children {
child.Crop(r)
}
}