forked from 4coder-archive/4coder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
4ed_layout.cpp
497 lines (433 loc) · 14.6 KB
/
4ed_layout.cpp
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
/*
* Mr. 4th Dimention - Allen Webster
*
* 19.08.2015
*
* Panel layout functions
*
*/
// TOP
internal Panel_Split
make_panel_split(Panel_Split_Kind kind, i32 v){
Panel_Split split = {};
split.kind = kind;
split.v_i32 = v;
return(split);
}
internal Panel_Split
make_panel_split(Panel_Split_Kind kind, f32 v){
Panel_Split split = {};
split.kind = kind;
split.v_f32 = v;
return(split);
}
internal Panel_Split
make_panel_split_50_50(void){
return(make_panel_split(PanelSplitKind_Ratio_Min, 0.5f));
}
internal Panel*
layout__alloc_panel(Layout *layout){
Panel *panel = 0;
Node *node = layout->free_panels.next;
if (node != &layout->free_panels){
dll_remove(node);
panel = CastFromMember(Panel, node, node);
}
return(panel);
}
internal void
layout__free_panel(Layout *layout, Panel *panel){
Assert(panel != layout->active_panel);
Assert(panel != layout->root);
dll_remove(&panel->node);
dll_insert(&layout->free_panels, &panel->node);
panel->kind = PanelKind_Unused;
}
internal void
layout__set_panel_rectangle(Layout *layout, Panel *panel, Rect_i32 rect){
panel->rect_full = rect;
panel->rect_inner = rect_inner(rect, layout->margin);
}
internal i32
layout__evaluate_split(Panel_Split split, i32 v0, i32 v1){
i32 v = 0;
switch (split.kind){
case PanelSplitKind_Ratio_Min:
{
v = i32_round32(lerp((f32)v0, split.v_f32, (f32)v1));
}break;
case PanelSplitKind_Ratio_Max:
{
v = i32_round32(lerp((f32)v1, split.v_f32, (f32)v0));
}break;
case PanelSplitKind_FixedPixels_Min:
{
v = clamp_top(v0 + split.v_i32, v1);
}break;
case PanelSplitKind_FixedPixels_Max:
{
v = clamp_bot(v0, v1 - split.v_i32);
}break;
}
return(v);
}
internal void
layout_propogate_sizes_down_from_node(Layout *layout, Panel *panel){
if (panel->kind == PanelKind_Intermediate){
Panel *tl_panel = panel->tl_panel;
Panel *br_panel = panel->br_panel;
Rect_i32 r1 = panel->rect_full;
Rect_i32 r2 = panel->rect_full;
if (panel->vertical_split){
i32 x_pos = layout__evaluate_split(panel->split, r1.x0, r1.x1);
r1.x1 = x_pos;
r2.x0 = x_pos;
}
else{
i32 y_pos = layout__evaluate_split(panel->split, r1.y0, r1.y1);
r1.y1 = y_pos;
r2.y0 = y_pos;
}
layout__set_panel_rectangle(layout, tl_panel, r1);
layout__set_panel_rectangle(layout, br_panel, r2);
layout_propogate_sizes_down_from_node(layout, tl_panel);
layout_propogate_sizes_down_from_node(layout, br_panel);
}
}
internal i32
layout_get_open_panel_count(Layout *layout){
return(layout->open_panel_count);
}
internal Panel*
layout_get_first_open_panel(Layout *layout){
Panel *panel = CastFromMember(Panel, node, layout->open_panels.next);
if (panel != 0 && &panel->node == &layout->open_panels){
panel = 0;
}
AssertImplies(panel != 0, panel->kind == PanelKind_Final);
return(panel);
}
internal Panel*
layout_get_last_open_panel(Layout *layout){
Panel *panel = CastFromMember(Panel, node, layout->open_panels.prev);
if (panel != 0 && &panel->node == &layout->open_panels){
panel = 0;
}
AssertImplies(panel != 0, panel->kind == PanelKind_Final);
return(panel);
}
internal Panel*
layout_get_next_open_panel(Layout *layout, Panel *panel){
panel = CastFromMember(Panel, node, panel->node.next);
if (&panel->node == &layout->open_panels){
panel = 0;
}
AssertImplies(panel != 0, panel->kind == PanelKind_Final);
return(panel);
}
internal Panel*
layout_get_prev_open_panel(Layout *layout, Panel *panel){
panel = CastFromMember(Panel, node, panel->node.prev);
if (&panel->node == &layout->open_panels){
panel = 0;
}
AssertImplies(panel != 0, panel->kind == PanelKind_Final);
return(panel);
}
internal Panel*
layout_get_active_panel(Layout *layout){
return(layout->active_panel);
}
internal b32
layout_split_panel(Layout *layout, Panel *panel, b32 vertical_split, Panel **new_panel_out){
b32 result = false;
if (layout->open_panel_count < layout->open_panel_max_count){
Panel *min_panel = layout__alloc_panel(layout);
Panel *max_panel = layout__alloc_panel(layout);
if (panel->kind == PanelKind_Final){
dll_remove(&panel->node);
dll_insert(&layout->intermediate_panels, &panel->node);
// init min_panel
dll_insert(&layout->open_panels, &min_panel->node);
panel->view->panel = min_panel;
min_panel->parent = panel;
min_panel->kind = PanelKind_Final;
min_panel->view = panel->view;
}
else{
// init min_panel
dll_insert(&layout->intermediate_panels, &min_panel->node);
panel->tl_panel->parent = min_panel;
panel->br_panel->parent = min_panel;
min_panel->parent = panel;
min_panel->kind = PanelKind_Intermediate;
min_panel->tl_panel = panel->tl_panel;
min_panel->br_panel = panel->br_panel;
min_panel->vertical_split = panel->vertical_split;
min_panel->split = panel->split;
}
// init max_panel
dll_insert(&layout->open_panels, &max_panel->node);
*new_panel_out = max_panel;
max_panel->parent = panel;
max_panel->kind = PanelKind_Final;
max_panel->view = 0;
// modify panel
panel->kind = PanelKind_Intermediate;
panel->tl_panel = min_panel;
panel->br_panel = max_panel;
panel->vertical_split = vertical_split;
panel->split = make_panel_split_50_50();
// propogate rectangle sizes down from the new intermediate to
// resize the panel and the new panel.
layout_propogate_sizes_down_from_node(layout, panel);
// update layout state
layout->open_panel_count += 1;
layout->active_panel = max_panel;
layout->panel_state_dirty = true;
result = true;
}
return(result);
}
internal b32
layout_close_panel(Layout *layout, Panel *panel){
b32 result = false;
if (layout->open_panel_count > 1){
Panel *parent = panel->parent;
Assert(parent != 0);
// find sibling
Panel *sibling = 0;
if (parent->tl_panel == panel){
sibling = parent->br_panel;
}
else{
Assert(parent->br_panel == panel);
sibling = parent->tl_panel;
}
// update layout state
if (layout->active_panel == panel){
Panel *new_active = sibling;
for (;new_active->kind == PanelKind_Intermediate;){
new_active = new_active->br_panel;
}
layout->active_panel = new_active;
}
layout->panel_state_dirty = true;
layout->open_panel_count -= 1;
// link grand parent and sibling
Panel *g_parent = parent->parent;
sibling->parent = g_parent;
if (g_parent != 0){
if (g_parent->tl_panel == parent){
g_parent->tl_panel = sibling;
}
else{
Assert(g_parent->br_panel == parent);
g_parent->br_panel = sibling;
}
}
else{
Assert(parent == layout->root);
layout->root = sibling;
}
// set sibling's size
sibling->screen_region = parent->screen_region;
// set the sizes down stream of sibling
layout_propogate_sizes_down_from_node(layout, sibling);
// free panel and parent
layout__free_panel(layout, panel);
layout__free_panel(layout, parent);
result = true;
}
return(result);
}
internal Panel*
layout_initialize(Arena *arena, Layout *layout){
i32 panel_alloc_count = MAX_VIEWS*2 - 1;
Panel *panels = push_array(arena, Panel, panel_alloc_count);
block_zero(panels, sizeof(*panels)*panel_alloc_count);
layout->panel_first = panels;
layout->panel_one_past_last = panels + panel_alloc_count;
layout->margin = 3;
layout->open_panel_count = 0;
layout->open_panel_max_count = MAX_VIEWS;
dll_init_sentinel(&layout->open_panels);
dll_init_sentinel(&layout->intermediate_panels);
Panel *panel = panels;
layout->free_panels.next = &panel->node;
panel->node.prev = &layout->free_panels;
for (i32 i = 1; i < panel_alloc_count; i += 1, panel += 1){
panel[1].node.prev = &panel[0].node;
panel[0].node.next = &panel[1].node;
}
panel->node.next = &layout->free_panels;
layout->free_panels.prev = &panel->node;
panel = layout__alloc_panel(layout);
panel->parent = 0;
panel->kind = PanelKind_Final;
panel->view = 0;
block_zero_struct(&panel->screen_region);
dll_insert(&layout->open_panels, &panel->node);
layout->open_panel_count += 1;
layout->root = panel;
layout->active_panel = panel;
layout->panel_state_dirty = true;
return(panel);
}
internal void
layout_set_margin(Layout *layout, i32 margin){
if (layout->margin != margin){
layout->margin = margin;
layout__set_panel_rectangle(layout, layout->root, Ri32(0, 0, layout->full_dim.x, layout->full_dim.y));
layout_propogate_sizes_down_from_node(layout, layout->root);
layout->panel_state_dirty = true;
}
}
internal void
layout_set_root_size(Layout *layout, Vec2_i32 dim){
if (layout->full_dim != dim){
layout->full_dim = dim;
layout__set_panel_rectangle(layout, layout->root, Ri32(0, 0, dim.x, dim.y));
layout_propogate_sizes_down_from_node(layout, layout->root);
layout->panel_state_dirty = true;
}
}
internal Vec2_i32
layout_get_root_size(Layout *layout){
return(layout->full_dim);
}
internal i32
layout_get_absolute_position_of_split(Panel *panel){
i32 pos = 0;
if (panel->vertical_split){
pos = layout__evaluate_split(panel->split, panel->rect_full.x0, panel->rect_full.x1);
}
else{
pos = layout__evaluate_split(panel->split, panel->rect_full.y0, panel->rect_full.y1);
}
return(pos);
}
internal Range_i32
layout__get_limiting_range_on_split_children(i32 mid, b32 vertical_split, Panel *panel, Range_i32 range){
if (panel->kind == PanelKind_Intermediate){
if (vertical_split == panel->vertical_split){
i32 pos = layout_get_absolute_position_of_split(panel);
if (mid < pos && pos < range.max){
range.max = pos;
}
else if (range.min < pos && pos < mid){
range.min = pos;
}
}
range = layout__get_limiting_range_on_split_children(mid, vertical_split, panel->tl_panel, range);
range = layout__get_limiting_range_on_split_children(mid, vertical_split, panel->br_panel, range);
}
return(range);
}
internal Range_i32
layout_get_limiting_range_on_split(Layout *layout, Panel *panel){
// root level min max
Range_i32 range = {};
if (panel->vertical_split){
range.max = layout->full_dim.x;
}
else{
range.max = layout->full_dim.y;
}
// get mid
i32 mid = layout_get_absolute_position_of_split(panel);
// parents min max
for (Panel *panel_it = panel;
panel_it != 0;
panel_it = panel_it->parent){
if (panel->vertical_split == panel_it->vertical_split){
i32 pos = layout_get_absolute_position_of_split(panel_it);
if (mid < pos && pos < range.max){
range.max = pos;
}
else if (range.min < pos && pos < mid){
range.min = pos;
}
}
}
// children min max
if (panel->kind == PanelKind_Intermediate){
range = layout__get_limiting_range_on_split_children(mid, panel->vertical_split, panel->tl_panel, range);
range = layout__get_limiting_range_on_split_children(mid, panel->vertical_split, panel->br_panel, range);
}
return(range);
}
internal void
layout__reverse_evaluate_panel_split(Panel *panel, i32 position){
i32 v0 = 0;
i32 v1 = 0;
if (panel->vertical_split){
v0 = panel->rect_full.x0;
v1 = panel->rect_full.x1;
}
else{
v0 = panel->rect_full.y0;
v1 = panel->rect_full.y1;
}
switch (panel->split.kind){
case PanelSplitKind_Ratio_Min:
{
panel->split.v_f32 = unlerp((f32)v0, (f32)position, (f32)v1);
}break;
case PanelSplitKind_Ratio_Max:
{
panel->split.v_f32 = unlerp((f32)v1, (f32)position, (f32)v0);
}break;
case PanelSplitKind_FixedPixels_Min:
{
panel->split.v_i32 = clamp(v0, position, v1) - v0;
}break;
case PanelSplitKind_FixedPixels_Max:
{
panel->split.v_i32 = v1 - clamp(v0, position, v1);
}break;
}
}
internal void
layout__set_split_absolute_position_inner(Panel *panel){
if (panel->kind == PanelKind_Intermediate){
Rect_i32 r = panel->rect_full;
i32 position = 0;
if (panel->vertical_split){
position = layout__evaluate_split(panel->split, r.x0, r.x1);
}
else{
position = layout__evaluate_split(panel->split, r.y0, r.y1);
}
layout__reverse_evaluate_panel_split(panel, position);
}
}
internal void
layout_set_split_absolute_position(Layout *layout, Panel *panel, i32 absolute_position){
if (panel->kind == PanelKind_Intermediate){
layout__reverse_evaluate_panel_split(panel, absolute_position);
layout__set_split_absolute_position_inner(panel->tl_panel);
layout__set_split_absolute_position_inner(panel->br_panel);
layout_propogate_sizes_down_from_node(layout, panel);
layout->panel_state_dirty = true;
}
}
internal Panel_ID
panel_get_id(Layout *layout, Panel *panel){
Panel_ID id = 0;
if (layout->panel_first <= panel && panel < layout->panel_one_past_last){
id = (Panel_ID)(panel - layout->panel_first) + 1;
}
return(id);
}
////////////////////////////////
internal Panel*
imp_get_panel(Models *models, Panel_ID panel_id){
Layout *layout = &models->layout;
Panel *panel = layout->panel_first + panel_id - 1;
if (!(layout->panel_first <= panel && panel < layout->panel_one_past_last)){
panel = 0;
}
return(panel);
}
// BOTTOM