-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathglay.go
More file actions
562 lines (485 loc) · 16 KB
/
glay.go
File metadata and controls
562 lines (485 loc) · 16 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
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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
package glay
//go:generate stringer -linecomment -output=stringers.go -type=ElementConfigType,LayoutDirection,LayoutAlignmentX,LayoutAlignmentY,SizingType,TextElementConfigWrapMode,TextAlignment,FloatingAttachPointType,MousePointerCaptureMode,FloatingAttachToElement,RenderCommandType,Error
// Internal clay types to better match the source and also
// allow easily switching between a 32-bit implementation or 64-bit.
type (
uintn = uint32
intn = int32
floatn = float32
strslice = []byte // Non-owning string slice
)
func ID(name string) ElementID {
return hashString(name, 0, 0)
}
type Context struct {
MaxElementCount intn
MaxMeasureTextCacheWordCount intn
WarningsEnabled bool
PointerInfo MousePointerData
LayoutDimensions Dimensions
DynamicElementIndexBaseHash ElementID
DynamicElementIndex uintn
DebugModeEnabled bool
DisableCulling bool
ExternalScrollHandlingEnabled bool
DebugSelectElementID uintn
Generation uintn
MeasureTextFunction func(text string, config *TextElementConfig, userData any) Dimensions
MeasureTextUserData any
QueryScrollOffsetFunction func(elementID uint32, userData any) Vector2
QueryScrollOffsetUserData any
// Layout elements / render commands
LayoutElements []LayoutElement
renderCommands []RenderCommand
OpenLayoutElementStack []intn
LayoutElementChildren []intn
LayoutElementChildrenBuffer []intn
TextElementData []TextElementData
AspectRatioElementIndexes []intn
ReusableElementIndexBuffer []intn
LayoutElementClipElementIDs []intn
// Configs
LayoutConfigs []LayoutConfig
ElementConfigs []ElementConfig
TextElementConfigs []TextElementConfig
AspectRatioElementConfigs []AspectRatioElementConfig
ImageElementConfigs []ImageElementConfig
FloatingElementConfigs []FloatingElementConfig
ClipElementConfigs []ClipElementConfig
CustomElementConfigs []any
BorderElementConfigs []BorderElementConfig
SharedElementConfigs []SharedElementConfig
// Misc Data Structures.
LayoutElementIDStrings []string
WrappedTextLines []WrappedTextLine
LayoutElementTreeNodes1 []layoutElementTreeNode
LayoutElementTreeRoots []layoutElementTreeRoot
measureTextHashMapInternal []measureTextCacheItem
MeasureTextHashMapInternalFreelist []intn
measureTextHashMap []intn
measuredWords []measuredWord
measuredWordsFreeList []intn
openClipElementStack []intn
PointerOverIDs []ElementID
scrollContainerDatas []scrollContainerDataInternal
TreeNodeVisited []bool
DynamicStringData []byte
debugElementData []debugElementData
GoHash map[uintn]*LayoutElementHashMapItem
logger
}
type SharedElementConfig struct {
BackgroundColor Color
CornerRadius CornerRadius
UserData any
}
type ElementConfigType uint8
const (
ElementConfigTypeNone ElementConfigType = iota // element config none
ElementConfigTypeBorder // element config border
ElementConfigTypeFloating // element config floating
ElementConfigTypeClip // element config clip
ElementConfigTypeAspectRatio // element config aspect ratio
ElementConfigTypeImage // element config image
ElementConfigTypeText // element config text
ElementConfigTypeCustom // element config custom
ElementConfigTypeShared // element config shared
)
func GetElementConfigType(a any) (Type ElementConfigType) {
switch a.(type) {
case *BorderElementConfig:
Type = ElementConfigTypeBorder
case *FloatingElementConfig:
Type = ElementConfigTypeFloating
case *ClipElementConfig:
Type = ElementConfigTypeClip
case *AspectRatioElementConfig:
Type = ElementConfigTypeAspectRatio
case *ImageElementConfig:
Type = ElementConfigTypeImage
case *TextElementConfig:
Type = ElementConfigTypeText
case *SharedElementConfig:
Type = ElementConfigTypeShared
default:
panic("invalid element Config, make sure it is pointer type")
}
return Type
}
type ElementConfig struct {
Type ElementConfigType
Config any
}
type WrappedTextLine struct {
Dimensions Dimensions
Line string
}
type TextElementData struct {
Text string
PreferredDimensions Dimensions
ElementIndex intn
WrappedLines []WrappedTextLine
}
type LayoutElement struct {
ChildrenOrTextContent any // Is either []intn when children or TextContent.
Dimensions Dimensions
MinDimensions Dimensions
LayoutConfig *LayoutConfig
ElementConfigs []ElementConfig
ID uintn
}
type layoutElementTreeNode struct {
layoutElement *LayoutElement
position Vector2
NextChildOffset Vector2
}
type layoutElementTreeRoot struct {
LayoutElementIndex intn
ParentID uintn
ClipElementID uintn
Zindex int16
PointerOffset Vector2
}
type LayoutElementHashMapItem struct {
BoundingBox BoundingBox
ElementID ElementID
LayoutElement *LayoutElement
OnHover func(_ ElementID, _ MousePointerData, userData any)
OnHoverUserData any
NextIndex intn
Generation uintn
IDAlias uintn
}
func (hmi LayoutElementHashMapItem) isdefault() bool {
return hmi.BoundingBox == BoundingBox{} && hmi.IDAlias == 0 && hmi.Generation == 0
}
type Dimensions struct {
Width, Height floatn
}
type Color struct {
R, G, B, A floatn
}
type BoundingBox struct {
Vector2
Dimensions
}
type Vector2 struct {
X, Y floatn
}
type ElementID struct {
ID uintn
Offset uintn
BaseID uintn
StringID string
}
type CornerRadius struct {
TopLeft, TopRight, BottomLeft, BottomRight floatn
}
type LayoutDirection uint8
const (
LeftToRight LayoutDirection = iota // left to right
TopToBottom // top to bottom
)
type LayoutAlignmentX uint8
const (
AlignXLeft LayoutAlignmentX = iota // align x left
AlignXRight // align x right
AlignXCenter // align x center
)
type LayoutAlignmentY uint8
const (
AlignYTop LayoutAlignmentY = iota // align y top
AlignYBottom // align y bottom
AlignYCenter // align y center
)
type SizingType uint8
const (
SizingFit SizingType = iota // sizing fit
SizingGrow // sizing grow
SizingPercent // sizing percent
SizingFixed // sizing fixed
)
type ChildAlignment struct {
X LayoutAlignmentX
Y LayoutAlignmentY
}
type SizingMinMax struct {
Min, Max floatn
}
type SizingAxis struct {
MinMax SizingMinMax
Percent floatn
Type SizingType
}
type Sizing struct {
Width SizingAxis
Height SizingAxis
}
func NewSizingAxis(kind SizingType, a ...floatn) (ax SizingAxis) {
ax.Type = kind
need1Arg := kind == SizingPercent || kind == SizingFixed
if len(a) == 0 || len(a) > 2 || need1Arg != (len(a) == 1) {
panic("invalid number of arguments")
} else if kind == SizingPercent {
ax.Percent = a[0]
return ax
} else if kind == SizingFixed {
ax.MinMax = SizingMinMax{Min: a[0], Max: a[0]}
return ax
}
ax.MinMax.Min = a[0]
ax.MinMax.Max = a[1]
return ax
}
func (sz Sizing) SizingAxis(xaxis bool) SizingAxis {
if xaxis {
return sz.Width
}
return sz.Height
}
func (sz Sizing) Clamp(d Dimensions) Dimensions {
d.Height = sz.ClampHeight(d.Height)
d.Width = sz.ClampWidth(d.Width)
return d
}
func (sz Sizing) ClampWidth(w floatn) floatn {
return clamp(sz.Width.MinMax.Min, sz.Width.MinMax.Max, w)
}
func (sz Sizing) ClampHeight(h floatn) floatn {
return clamp(sz.Height.MinMax.Min, sz.Height.MinMax.Max, h)
}
type Padding struct {
Left, Right, Top, Bottom uint16
}
func PaddingAll(padding uint16) Padding {
return Padding{
Left: padding, Right: padding, Top: padding, Bottom: padding,
}
}
func (pd Padding) Vertical() uint16 { return pd.Top + pd.Bottom }
func (pd Padding) Horizontal() uint16 { return pd.Left + pd.Right }
func (pd Padding) SizeAxis(xaxis bool) floatn {
if xaxis {
return floatn(pd.Horizontal())
}
return floatn(pd.Vertical())
}
type LayoutConfig struct {
Sizing Sizing
Padding Padding
ChildGap uint16
ChildAlignment ChildAlignment
LayoutDirection LayoutDirection
}
type TextElementConfigWrapMode uint8
const (
TextWrapWords TextElementConfigWrapMode = iota // text wrap words
TextWrapNewlines // text wrap newlines
TextWrapNone // text wrap none
)
type TextAlignment uint8
const (
TextAlignLeft TextAlignment = iota // text align left
TextAlignCenter // text align center
TextAlignRight // text align right
)
type TextElementConfig struct {
TextColor Color
FontID uint16
FontSize uint16
LetterSpacing uint16
LineHeight uint16
WrapMode TextElementConfigWrapMode
TextAlignment TextAlignment
}
type AspectRatioElementConfig struct {
AspectRatio floatn
}
type ImageElementConfig struct {
ImageData any
SourceDimensions Dimensions
}
type FloatingAttachPointType uint8
const (
AttachPointLeftTop FloatingAttachPointType = iota // attach left top
AttachPointLeftCenter // attach left center
AttachPointLeftBottom // attach left bottom
AttachPointCenterTop // attach center top
AttachPointCenterCenter // attach center center
AttachPointCenterBottom // attach center bottom
AttachPointRightTop // attach right top
AttachPointRightCenter // attach right center
AttachPointRightBottom // attach right bottom
)
type FloatingAttachPoints struct {
Element FloatingAttachPointType
Parent FloatingAttachPointType
}
type MousePointerCaptureMode uint8
const (
PointerCaptureModeCapture MousePointerCaptureMode = iota // pointer mode capture
PointerCaptureModePassthrough // pointer mode passthrough
)
type FloatingAttachToElement uint8
const (
AttachToNone FloatingAttachToElement = iota // attach to none
AttachToParent // attach to parent
AttachToElementWithID // attach to element with ID
AttachToRoot // attach to root
)
type FloatingClipToElement uint8
const (
ClipToNone FloatingClipToElement = iota // clip to none
ClipToAttachedParent // clip to attached parent
)
type FloatingElementConfig struct {
Offset Vector2
Expand Dimensions
ParentID uintn
Zindex int16
AttachPoints FloatingAttachPoints
PointerCaptureMode MousePointerCaptureMode
AttachTo FloatingAttachToElement
ClipTo FloatingClipToElement
}
type ClipElementConfig struct {
Horizontal bool
Vertical bool
ChildOffset Vector2
}
type BorderWidth struct {
Left, Right, Top, Bottom, BetweenChildren uint16
}
type BorderElementConfig struct {
Color Color
Width BorderWidth
}
type TextRenderData struct {
Contents strslice
TextColor Color
FontID uint16
FontSize uint16
LetterSpacing uint16
LineHeight uint16
}
type ImageRenderData struct {
BackgroundColor Color
CornerRadius CornerRadius
SourceDimensions Dimensions
ImageData any
}
type ClipRenderData struct {
Horizontal, Vertical bool
}
type RectangleRenderData struct {
BackgroundColor Color
CornerRadius CornerRadius
}
type BorderRenderData struct {
Color Color
CornerRadius CornerRadius
Width BorderWidth
}
type CustomRenderData struct {
BackgroundColor Color
CornerRadius CornerRadius
CustomData any
}
type RenderData any
type ScrollContainerData struct {
ScrollPosition *Vector2
ScrollContainerDimensions Dimensions
ContentDimensions Dimensions
Config ClipElementConfig
Found bool
}
type ElementData struct {
BoundingBox BoundingBox
Found bool
}
type RenderCommandType uint8
const (
RenderCommandTypeNone RenderCommandType = iota // render:none
RenderCommandTypeRectangle // render:rectangle
RenderCommandTypeBorder // render:border
RenderCommandTypeText // render:text
RenderCommandTypeImage // render:image
RenderCommandTypeScissorStart // render:scissor-start
RenderCommandTypeScissorEnd // render:scissor-end
RenderCommandTypeCustom // render:custom
)
type RenderCommand struct {
BoundingBox BoundingBox
RenderData RenderData
UserData any
ID uintn
Zindex int16
CommandType RenderCommandType
}
type MousePointerDataInteractionState uint8
const (
PointerDataPressedThisFrame MousePointerDataInteractionState = iota // pointer pressed this frame
PointerDataPressed // pointer pressed
PointerReleasedThisFrame // pointer released this frame
PointerReleased // pointer released
)
type MousePointerData struct {
Position Vector2
State MousePointerDataInteractionState
}
type ElementDeclaration struct {
ID ElementID
Layout LayoutConfig
BackgroundColor Color
CornerRadius CornerRadius
AspectRatio AspectRatioElementConfig
Image ImageElementConfig
Floating FloatingElementConfig
Clip ClipElementConfig
Border BorderElementConfig
UserData any
}
type Error uint8
const (
ErrTextMeasurementFunctionNotProvided Error = iota // a text measurement function wasn't provided using Clay_SetMeasureTextFunction(), or the provided function was null
ErrPercentageOver1 // an element was configured with CLAY_SIZING_PERCENT, but the provided percentage value was over 1.0. Clay expects a value between 0 and 1, i.e. 20% is 0.2
ErrFloatingContainerParentNotFound // a floating element was declared with a parentId, but no element with that ID was found
ErrElementsCapacityExceeded // Clay ran out of capacity while attempting to create render commands. This is usually caused by a large amount of wrapping text elements while close to the max element capacity. Try using Clay_SetMaxElementCount() with a higher value
// ErrArenaCapacityExceeded // Clay attempted to allocate memory in its arena, but ran out of capacity. Try increasing the capacity of the arena passed to Clay_Initialize()
)
func (e Error) Error() string {
return e.String()
}
type debugElementData struct {
collision, collapsed bool
}
type measuredWord struct {
StartOffset intn
Length intn
Width floatn
Next intn
}
type measureTextCacheItem struct {
unwrappedDimensions Dimensions
measureWordsStartIndex intn
minWidth floatn
containsNewlines bool
// hash map data.
ID uintn
nextIndex intn
generation uintn
}
type scrollContainerDataInternal struct {
LayoutElement *LayoutElement
BoundingBox BoundingBox
ContentSize Dimensions
ScrollOrigin Vector2
PointerOrigin Vector2
ScrollMomentum Vector2
ScrollPosition Vector2
PreviousDelta Vector2
MomentumTime floatn
ElementID uintn
OpenThisFrame bool
PointerScrollActive bool
}