-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathelement.odin
More file actions
204 lines (172 loc) · 4.73 KB
/
element.odin
File metadata and controls
204 lines (172 loc) · 4.73 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
package syl
import "core:fmt"
import "base:intrinsics"
import "base:runtime"
MessageHandler :: struct($T: typeid) {
handler: proc(rawptr, rawptr),
destroy: proc(rawptr),
base: ^T,
element: rawptr,
message_type: typeid
}
// ^custom_element, base_type, message_type, update, destroy
make_handler :: proc(element: ^$E, $B:typeid, $M: typeid, update_proc: proc(^E, ^M), destroy_proc: proc(^E)) -> MessageHandler(B) where (intrinsics.type_field_type(E, "base") == B) {
return {
element = element,
base = &element.base,
handler = auto_cast update_proc,
destroy = auto_cast destroy_proc,
message_type = typeid_of(M)
}
}
Element_Type :: enum { Box, Text, Button }
Handler :: struct {
element: rawptr,
handler: proc(rawptr, rawptr),
destroy: proc(rawptr),
message_type: typeid
}
Element :: struct {
owner: ^Element,
type: Element_Type,
parent: ^Element,
children: [dynamic]^Element,
position: [2]f32,
global_position: [2]f32,
size: [2]f32,
min_size: [2]f32,
sizing: Sizing,
style_sheet: ^Style_Sheet,
handler: Maybe(Handler),
constructor_caller_loc: runtime.Source_Code_Location,
}
SizingKind :: enum {
Fit,
Fixed,
Expand,
}
Sizing :: [2]SizingKind
update :: proc(el: ^Element) {
calculate_layout(el)
element_update(el)
update_transitions()
}
element_add_child:: proc(element: ^Element, child: ^Element, use_transitions: bool = false) {
if child.parent == element do return
append_elem(&element.children, child)
element_set_style_sheet_recursive(child, element.style_sheet, use_transitions)
child.parent = element
element_set_owner(child, element.owner)
}
element_remove_child:: proc(element: ^Element, child: ^Element) {
if child.parent == element {
child.style_sheet = nil
child.parent = nil
child.owner = nil
remove_item(&element.children, child)
}
}
element_remove_children:: proc(element: ^Element) {
for child in element.children {
element_remove_child(element, child)
}
}
element_set_position :: proc(element: ^Element, pos: [2]f32) {
element.position = pos
element.global_position = pos
if element.parent != nil {
element.global_position += element.parent.global_position
}
for child in element.children {
child.global_position = child.position + element.global_position
}
}
element_set_global_position :: proc(element: ^Element, pos: [2]f32) {
element.position = pos
element.global_position = pos
if element.parent != nil {
element.position -= element.parent.global_position
}
for child in element.children {
child.global_position = child.position + element.global_position
}
}
events: [dynamic]any
element_update :: proc(el: ^Element) {
#partial switch el.type {
case .Box: update_box(cast(^Box)el)
case .Button: update_button(cast(^Button)el)
}
}
element_set_owner :: proc(element: ^Element, owner: ^Element) {
if element.owner == nil {
element.owner = owner
ensure_correct_messages_type(element)
for child in element.children {
element_set_owner(child, owner)
}
}
}
button_ensure_correct_messages_type :: proc(button: ^Button) {
if button.owner == nil do return
if handler, ok := button.owner.handler.?; ok {
messages := [2]any{button.on_click, button.on_mouse_over}
for message in messages do if message != nil {
assert(
message.id == handler.message_type,
fmt.tprintf(
"message type is incorrect, expected: %s, got: %s. At: %s",
handler.message_type,
message.id,
button.constructor_caller_loc
)
)
}
}
}
ensure_correct_messages_type :: proc(element: ^Element) {
switch element.type {
case .Box:
box := cast(^Box)element
case .Button:
button_ensure_correct_messages_type(auto_cast element)
case .Text:
// Text elements do not have message handlers
}
}
clear_children :: proc(element: ^Element) {
box := cast(^Box)element
if box == nil do return
// Free all children elements
for child in box.children {
element_destroy(child)
}
// Clear the dynamic array
clear(&box.children)
}
element_append :: proc(element: ^Element, child: ^Element) {
append_elem(&element.children, child)
child.parent = element // Will be set by the parent later if needed
}
base_element_deinit :: proc(element: ^Element) {
if element == nil do return
for child in element.children do element_destroy(child)
delete(element.children)
}
element_destroy :: proc(element: ^Element) {
if element == nil do return
if h, ok := element.handler.?; ok {
switch element.type {
case .Text: text_deinit(cast(^Text)element)
case .Box: box_deinit(cast(^Box)element)
case .Button: button_deinit(cast(^Button)element)
}
h.destroy(h.element)
return
}
switch element.type {
case .Text: text_destroy(cast(^Text)element)
case .Box: box_destroy(cast(^Box)element)
case .Button: button_destroy(cast(^Button)element)
}
}