summaryrefslogtreecommitdiff
path: root/src/editor/gui.cpp
blob: ea03e8f1958580dd4938ff64f2a63fc34d602d94 (plain)
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
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
#include "editor.h"
#include "../game/world/bsp.h"
#include "../game/assets.h"
#include "../game.h"
#include "../render/gl_2d.h"

const I32 EDITOR_LAYOUT_MARGIN = 10;
const I32 EDITOR_LAYOUT_TITLE_OFFSET = 15;
const I32 EDITOR_LAYOUT_MENU_Y = 1;
const I32 EDITOR_LAYOUT_MENU_H = 22;
const I32 EDITOR_LAYOUT_NAV_Y = EDITOR_LAYOUT_MENU_Y + EDITOR_LAYOUT_MENU_H + 4;
const I32 EDITOR_LAYOUT_CONTENT_Y = EDITOR_LAYOUT_NAV_Y + 28;
const I32 EDITOR_LAYOUT_COLUMN_GAP = 10;
const I32 EDITOR_LAYOUT_VIEW_TOOL_GAP = 17;
const I32 EDITOR_LAYOUT_TOOL_BTN_TOP_GAP = 3;
const I32 EDITOR_LAYOUT_TOOL_PANEL_GAP = 5;
const I32 EDITOR_LAYOUT_LEFT_BOX_GAP = 10;
const I32 EDITOR_LAYOUT_STATUS_H = 22;
const I32 EDITOR_LAYOUT_STATUS_GAP = 6;
const I32 EDITOR_LAYOUT_TOOL_PANEL_W = 300;
const I32 EDITOR_LAYOUT_TOOL_PANEL_H = 150;
const I32 EDITOR_LAYOUT_PROPS_DEFAULT_W = 300;
const I32 EDITOR_LAYOUT_PROPS_DEFAULT_H = 300;
const I32 EDITOR_LAYOUT_VIEW_DEFAULT_H = 370;
const I32 EDITOR_LAYOUT_PROPS_MIN_W = 200;
const I32 EDITOR_LAYOUT_PROPS_MIN_H = 120;
const I32 EDITOR_LAYOUT_ASSETS_MIN_H = 120;
const I32 EDITOR_LAYOUT_VIEW_MIN_H = 140;
const I32 EDITOR_LAYOUT_RIGHT_MIN_W = 260;

enum EditorInfoBoxType_t {
  EDITOR_INFOBOX_ASSETS = 1,
  EDITOR_INFOBOX_STATUS = 2
};

struct GUI_EDITOR_INFOBOX : GUI_VIEW {
  U8 type{};
};

struct GUI_EDITOR_VIEWTYPE_SEGMENT : GUI_BASE {
  U8 held{};
  I32 held_seg{-1};
};

struct GUI_EDITOR_TOOLBAR : GUI_BASE {
  U8 held{};
  I32 held_item{-1};
  U8 file_open{};
  U8 edit_open{};
};

enum EditorViewMode_t {
  EDITOR_VIEWMODE_2D = 0,
  EDITOR_VIEWMODE_3D = 1,
  EDITOR_VIEWMODE_SIM = 2
};

enum Editor2DViewType_t {
  EDITOR_2DVIEW_TOP_DOWN = 0,
  EDITOR_2DVIEW_SIDE_ELEVATION = 1,
  EDITOR_2DVIEW_FRONT_ELEVATION = 2
};

static void editor_layout_map_view( GAME_EDITOR* e );
static U8 editor_menu_hover_mask_active = 0;
static I32 editor_menu_hover_real_x = 0;
static I32 editor_menu_hover_real_y = 0;

enum EditorToolbarHit_t {
  EDITOR_TOOLBAR_HIT_NONE = -1,
  EDITOR_TOOLBAR_HIT_FILE = 0,
  EDITOR_TOOLBAR_HIT_EDIT = 1,
  EDITOR_TOOLBAR_HIT_VIEW = 2,
  EDITOR_TOOLBAR_HIT_TOOLS = 3,
  EDITOR_TOOLBAR_HIT_SAVE = 4,
  EDITOR_TOOLBAR_HIT_UNDO = 5,
  EDITOR_TOOLBAR_HIT_REDO = 6
};

static void editor_toolbar_set_open( GUI_EDITOR_TOOLBAR* bar, U8 file_open, U8 edit_open );

static void editor_toolbar_close_menu() {
  GUI_EDITOR_TOOLBAR* bar = (GUI_EDITOR_TOOLBAR*)editor->gui.header_toolbar;
  editor_toolbar_set_open( bar, 0, 0 );
}

static void editor_apply_view_mode( GAME_EDITOR* e ) {
  if( !e || !e->gui.v2d || !e->gui.v3d )
    return;

  U8 mode = e->gui.view_mode;
  if( mode == EDITOR_VIEWMODE_2D ) {
    e->gui.v2d->enabled = 1;
    e->gui.v3d->enabled = 0;
    return;
  }

  e->gui.v2d->enabled = 0;
  e->gui.v3d->enabled = 1;
}

static void editor_header_back_cb( void* ) {
  editor_toolbar_close_menu();
  editor_close( editor );
}

static void editor_set_view_mode( I32 mode ) {
  editor_toolbar_close_menu();
  editor->gui.view_mode = mode;
  editor_apply_view_mode( editor );
  editor_layout_map_view( editor );
}

static void editor_header_mode_2d_cb( void* ) {
  editor_set_view_mode( EDITOR_VIEWMODE_2D );
}

static void editor_header_mode_3d_cb( void* ) {
  editor_set_view_mode( EDITOR_VIEWMODE_3D );
}

static void editor_header_mode_sim_cb( void* ) {
  editor_set_view_mode( EDITOR_VIEWMODE_SIM );
}

static void editor_header_viewtype_segment_widths( I32 w, I32* w0, I32* w1, I32* w2 ) {
  I32 base = max( 1, w / 3 );
  I32 rem = max( 0, w - base * 3 );
  *w0 = base + ( rem > 0 ? 1 : 0 );
  *w1 = base + ( rem > 1 ? 1 : 0 );
  *w2 = max( 1, w - *w0 - *w1 );
}

static I32 editor_header_viewtype_segment_index( GUI_EDITOR_VIEWTYPE_SEGMENT* seg, I32 mx, I32 my ) {
  I32 x = gui_relx( seg );
  I32 y = gui_rely( seg );
  if( mx < x || mx >= x + seg->w || my < y || my >= y + seg->h )
    return -1;

  I32 w0 = 0, w1 = 0, w2 = 0;
  editor_header_viewtype_segment_widths( seg->w, &w0, &w1, &w2 );
  I32 rel = mx - x;
  if( rel < w0 ) return 0;
  if( rel < w0 + w1 ) return 1;
  return 2;
}

static void gui_editor_viewtype_segment_draw_fn( void* ptr ) {
  GUI_EDITOR_VIEWTYPE_SEGMENT* seg = (GUI_EDITOR_VIEWTYPE_SEGMENT*)ptr;
  I32 x = gui_relx( seg );
  I32 y = gui_rely( seg );
  I32 mx, my;
  gui_cursor_pos( &mx, &my );
  I32 hover_seg = editor_header_viewtype_segment_index( seg, mx, my );
  I32 active_seg = editor->gui.view2d_type;
  U8 down = gui_mbutton_down( GUI_MBTNLEFT );

  CLR border = gui_is_fg_window( seg ) ? ui_clr.border : ui_clr.border_inactive;
  I32 inner_x = x + 1;
  I32 inner_y = y + 1;
  I32 inner_w = max( 1, seg->w - 2 );
  I32 inner_h = max( 1, seg->h - 2 );
  gui_draw_frect( inner_x, inner_y, inner_w, inner_h, ui_clr.bg_sec );

  I32 w0 = 0, w1 = 0, w2 = 0;
  editor_header_viewtype_segment_widths( inner_w, &w0, &w1, &w2 );
  I32 segx[3] = { inner_x, inner_x + w0, inner_x + w0 + w1 };
  I32 segw[3] = { w0, w1, w2 };
  const char* txts[3] = { "x/y", "x/z", "y/z" };

  for( I32 i = 0; i < 3; ++i ) {
    U8 selected = i == active_seg;
    U8 hover = hover_seg == i;

    CLR fill = ui_clr.bg_sec;
    if( selected )
      fill = CLR::blend( ui_clr.bg_alt, ui_clr.border, 0.15f );
    else if( hover )
      fill = CLR::blend( ui_clr.bg_alt, ui_clr.bg_sec, 0.5f );

    if( selected && down && hover )
      fill = CLR::blend( fill, ui_clr.bg_sec, 0.35f );

    gui_draw_frect( segx[i], inner_y, max( 1, segw[i] ), inner_h, fill );

    CLR txt = selected ? ui_clr.txt : CLR::blend( ui_clr.txt, ui_clr.bg_sec, 0.45f );
    gui_draw_str( segx[i] + segw[i] / 2, y + ( seg->h / 2 ) - 7, ALIGN_C, FNT_JPN12, txt, txts[i] );
  }

  I32 split1_x = inner_x + w0;
  I32 split2_x = inner_x + w0 + w1;
  gui_draw_line( split1_x, inner_y, split1_x, inner_y + inner_h - 1, border );
  gui_draw_line( split2_x, inner_y, split2_x, inner_y + inner_h - 1, border );
  gui_draw_rect( x, y, seg->w, seg->h, border );
}

static void gui_editor_viewtype_segment_input_fn( void* ptr ) {
  GUI_EDITOR_VIEWTYPE_SEGMENT* seg = (GUI_EDITOR_VIEWTYPE_SEGMENT*)ptr;
  I32 mx, my;
  gui_cursor_pos( &mx, &my );
  U8 m1 = gui_mbutton_down( GUI_MBTNLEFT );
  I32 cur_seg = editor_header_viewtype_segment_index( seg, mx, my );

  if( m1 ) {
    if( !seg->held ) {
      seg->held = 1;
      seg->held_seg = cur_seg;
    }
    return;
  }

  if( seg->held && seg->held_seg >= 0 && seg->held_seg == cur_seg ) {
    editor->gui.view2d_type = seg->held_seg;
    editor_layout_map_view( editor );
  }

  seg->held = 0;
  seg->held_seg = -1;
}

static GUI_EDITOR_VIEWTYPE_SEGMENT* gui_editor_viewtype_segment( I32 x, I32 y, I32 w, I32 h, const char* name ) {
  if( !gui_check_target() )
    return 0;

  GUI_EDITOR_VIEWTYPE_SEGMENT* seg = new GUI_EDITOR_VIEWTYPE_SEGMENT;
  seg->x = x;
  seg->y = y;
  seg->w = w;
  seg->h = h;
  seg->xbound = w;
  seg->ybound = h;
  seg->draw_fn = gui_editor_viewtype_segment_draw_fn;
  seg->input_fn = gui_editor_viewtype_segment_input_fn;
  seg->held = 0;
  seg->held_seg = -1;
  strcpy( seg->name, name );

  GUI_VIEW* parent = gui_get_view();
  seg->parent = parent;
  parent->children.push( seg );
  return seg;
}

const I32 EDITOR_TOOLBAR_DROPDOWN_W = 74;

static U8 editor_toolbar_pt_in_rect( I32 mx, I32 my, I32 x, I32 y, I32 w, I32 h ) {
  return mx >= x && mx < x + w && my >= y && my < y + h;
}

static void editor_toolbar_set_open( GUI_EDITOR_TOOLBAR* bar, U8 file_open, U8 edit_open ) {
  if( !bar )
    return;

  bar->file_open = file_open;
  bar->edit_open = edit_open;
}

static void editor_toolbar_item_rect( GUI_EDITOR_TOOLBAR* bar, I32 idx, I32* rx, I32* ry, I32* rw, I32* rh ) {
  static const I32 widths[4] = { 44, 44, 44, 52 };
  I32 x = gui_relx( bar );
  I32 y = gui_rely( bar );
  I32 cx = x + 8;
  for( I32 i = 0; i < idx; ++i )
    cx += widths[i] + 4;

  *rx = cx;
  *ry = y + 2;
  *rw = widths[idx];
  *rh = max( 1, bar->h - 4 );
}

static void editor_toolbar_dropdown_row_rect( GUI_EDITOR_TOOLBAR* bar, I32 anchor_idx, I32 row, I32* rx, I32* ry, I32* rw, I32* rh ) {
  I32 x = 0, y = 0, w = 0, h = 0;
  editor_toolbar_item_rect( bar, anchor_idx, &x, &y, &w, &h );
  *rx = x;
  *rh = max( 18, h );
  *ry = gui_rely( bar ) + bar->h + row * *rh;
  *rw = EDITOR_TOOLBAR_DROPDOWN_W;
}

static I32 editor_toolbar_hit_test( GUI_EDITOR_TOOLBAR* bar, I32 mx, I32 my ) {
  for( I32 i = 0; i < 4; ++i ) {
    I32 x = 0, y = 0, w = 0, h = 0;
    editor_toolbar_item_rect( bar, i, &x, &y, &w, &h );
    if( editor_toolbar_pt_in_rect( mx, my, x, y, w, h ) )
      return i;
  }

  if( bar->file_open ) {
    I32 x = 0, y = 0, w = 0, h = 0;
    editor_toolbar_dropdown_row_rect( bar, EDITOR_TOOLBAR_HIT_FILE, 0, &x, &y, &w, &h );
    if( editor_toolbar_pt_in_rect( mx, my, x, y, w, h ) )
      return EDITOR_TOOLBAR_HIT_SAVE;
  }

  if( bar->edit_open ) {
    for( I32 i = 0; i < 2; ++i ) {
      I32 x = 0, y = 0, w = 0, h = 0;
      editor_toolbar_dropdown_row_rect( bar, EDITOR_TOOLBAR_HIT_EDIT, i, &x, &y, &w, &h );
      if( editor_toolbar_pt_in_rect( mx, my, x, y, w, h ) )
        return i == 0 ? EDITOR_TOOLBAR_HIT_UNDO : EDITOR_TOOLBAR_HIT_REDO;
    }
  }

  return EDITOR_TOOLBAR_HIT_NONE;
}

static void gui_editor_toolbar_draw_fn( void* ptr ) {
  GUI_EDITOR_TOOLBAR* bar = (GUI_EDITOR_TOOLBAR*)ptr;
  I32 x = gui_relx( bar );
  I32 y = gui_rely( bar );
  I32 mx, my;
  if( editor_menu_hover_mask_active ) {
    mx = editor_menu_hover_real_x;
    my = editor_menu_hover_real_y;
  } else {
    gui_cursor_pos( &mx, &my );
  }
  U8 m1 = gui_mbutton_down( GUI_MBTNLEFT );
  I32 hover = editor_toolbar_hit_test( bar, mx, my );
  CLR hover_fill = CLR::blend( ui_clr.border, ui_clr.bg, 0.70f );
  CLR active_fill = CLR::blend( ui_clr.bg_sec, ui_clr.border, 0.22f );

  CLR border = gui_is_fg_window( bar ) ? ui_clr.border : ui_clr.border_inactive;
  gui_draw_frect( x, y, bar->w, bar->h, ui_clr.bg_sec );

  static const char* labels[4] = { "file", "edit", "view", "tools" };
  for( I32 i = 0; i < 4; ++i ) {
    I32 rx = 0, ry = 0, rw = 0, rh = 0;
    editor_toolbar_item_rect( bar, i, &rx, &ry, &rw, &rh );
    U8 is_hover = hover == i;
    U8 is_active = bar->held && bar->held_item == i && m1;
    U8 is_open = ( bar->file_open && i == EDITOR_TOOLBAR_HIT_FILE )
              || ( bar->edit_open && i == EDITOR_TOOLBAR_HIT_EDIT );

    CLR fill = ui_clr.bg_sec;
    if( is_open )
      fill = hover_fill;
    else if( is_active )
      fill = active_fill;
    else if( is_hover )
      fill = hover_fill;

    if( is_open || is_active || is_hover )
      gui_draw_frect( rx, ry, rw, rh, fill );
    gui_draw_str( rx + 6, y + bar->h / 2 - 8, ALIGN_L, FNT_JPN12, ui_clr.txt, labels[i] );
  }

  if( bar->file_open ) {
    I32 rx = 0, ry = 0, rw = 0, rh = 0;
    editor_toolbar_dropdown_row_rect( bar, EDITOR_TOOLBAR_HIT_FILE, 0, &rx, &ry, &rw, &rh );
    U8 is_hover = hover == EDITOR_TOOLBAR_HIT_SAVE;
    U8 is_active = bar->held && bar->held_item == EDITOR_TOOLBAR_HIT_SAVE && m1;

    gui_draw_frect( rx, ry, rw, rh, border );
    CLR fill = ui_clr.bg_sec;
    if( is_active )
      fill = active_fill;
    else if( is_hover )
      fill = hover_fill;

    gui_draw_frect( rx + 1, ry + 1, max( 1, rw - 2 ), max( 1, rh - 2 ), fill );
    gui_draw_str( rx + 8, ry + rh / 2 - 8, ALIGN_L, FNT_JPN12, ui_clr.txt, "save" );
  }

  if( bar->edit_open ) {
    const U8 enabled[2] = { editor->undo_actions.size > 0, editor->redo_actions.size > 0 };
    const char* labels[2] = { "undo", "redo" };
    const I32 hit_id[2] = { EDITOR_TOOLBAR_HIT_UNDO, EDITOR_TOOLBAR_HIT_REDO };

    I32 panel_x = 0, panel_y = 0, panel_w = 0, panel_h = 0;
    editor_toolbar_dropdown_row_rect( bar, EDITOR_TOOLBAR_HIT_EDIT, 0, &panel_x, &panel_y, &panel_w, &panel_h );
    I32 row_h = panel_h;
    gui_draw_frect( panel_x, panel_y, panel_w, row_h * 2, border );
    gui_draw_frect( panel_x + 1, panel_y + 1, max( 1, panel_w - 2 ), max( 1, row_h * 2 - 2 ), ui_clr.bg_sec );

    for( I32 i = 0; i < 2; ++i ) {
      I32 rx = 0, ry = 0, rw = 0, rh = 0;
      editor_toolbar_dropdown_row_rect( bar, EDITOR_TOOLBAR_HIT_EDIT, i, &rx, &ry, &rw, &rh );
      U8 is_hover = hover == hit_id[i];
      U8 is_active = bar->held && bar->held_item == hit_id[i] && m1;

      CLR fill = ui_clr.bg_sec;
      if( enabled[i] && is_active )
        fill = active_fill;
      else if( is_hover )
        fill = hover_fill;
      gui_draw_frect( rx + 1, ry + 1, max( 1, rw - 2 ), max( 1, rh - 2 ), fill );

      CLR txt_clr = enabled[i] ? ui_clr.txt : ui_clr.txt_inactive;
      gui_draw_str( rx + 8, ry + rh / 2 - 8, ALIGN_L, FNT_JPN12, txt_clr, labels[i] );
    }
  }
}

static void gui_editor_toolbar_input_fn( void* ptr ) {
  GUI_EDITOR_TOOLBAR* bar = (GUI_EDITOR_TOOLBAR*)ptr;
  I32 mx, my;
  gui_cursor_pos( &mx, &my );
  U8 m1 = gui_mbutton_down( GUI_MBTNLEFT );
  I32 hit = editor_toolbar_hit_test( bar, mx, my );

  if( m1 ) {
    if( !bar->held ) {
      bar->held = 1;
      bar->held_item = hit;
      if( hit == EDITOR_TOOLBAR_HIT_NONE && ( bar->file_open || bar->edit_open ) )
        editor_toolbar_set_open( bar, 0, 0 );
    }
    return;
  }

  if( bar->held && bar->held_item == hit ) {
    switch( hit ) {
      case EDITOR_TOOLBAR_HIT_FILE:
        editor_toolbar_set_open( bar, !bar->file_open, 0 );
        break;
      case EDITOR_TOOLBAR_HIT_EDIT:
        editor_toolbar_set_open( bar, 0, !bar->edit_open );
        break;
      case EDITOR_TOOLBAR_HIT_SAVE:
        editor_toolbar_set_open( bar, 0, 0 );
        editor_save_map( editor );
        break;
      case EDITOR_TOOLBAR_HIT_UNDO:
        editor_toolbar_set_open( bar, 0, 0 );
        editor_undo( editor );
        break;
      case EDITOR_TOOLBAR_HIT_REDO:
        editor_toolbar_set_open( bar, 0, 0 );
        editor_redo( editor );
        break;
      case EDITOR_TOOLBAR_HIT_VIEW:
      case EDITOR_TOOLBAR_HIT_TOOLS:
        editor_toolbar_set_open( bar, 0, 0 );
        break;
      default:
        break;
    }
  }

  bar->held = 0;
  bar->held_item = EDITOR_TOOLBAR_HIT_NONE;
}

static GUI_EDITOR_TOOLBAR* gui_editor_toolbar( I32 x, I32 y, I32 w, I32 h, const char* name ) {
  if( !gui_check_target() )
    return 0;

  GUI_EDITOR_TOOLBAR* bar = new GUI_EDITOR_TOOLBAR;
  bar->x = x;
  bar->y = y;
  bar->w = w;
  bar->h = h;
  bar->xbound = w;
  bar->ybound = h + 52;
  bar->draw_fn = gui_editor_toolbar_draw_fn;
  bar->input_fn = gui_editor_toolbar_input_fn;
  bar->held = 0;
  bar->held_item = EDITOR_TOOLBAR_HIT_NONE;
  bar->file_open = 0;
  bar->edit_open = 0;
  strcpy( bar->name, name );

  GUI_VIEW* parent = gui_get_view();
  bar->parent = parent;
  parent->children.push( bar );
  return bar;
}

static void editor_raise_header_toolbar( GAME_EDITOR* e ) {
  if( !e || !e->map || !e->wnd || !e->gui.header_toolbar )
    return;

  GUI_BASE* bar = e->gui.header_toolbar;
  GUI_BASE* parent = bar->parent;
  if( !parent )
    return;

  I32 idx = parent->children.idx_of( bar );
  if( idx == -1 || idx == (I32)parent->children.size - 1 )
    return;

  parent->children.erase( idx );
  parent->children.push( bar );
}

struct EDITOR_LAYOUT {
  I32 left_x;
  I32 left_w;
  I32 right_x;
  I32 right_w;
  I32 top_y;
  I32 props_y;
  I32 props_h;
  I32 assets_y;
  I32 assets_h;
  I32 view_y;
  I32 view_h;
  I32 tool_y;
  I32 tool_h;
  I32 tool_btn_x;
  I32 tool_btn_y;
  I32 tool_btn_w;
  I32 tool_btn_step;
  I32 tool_panel_x;
  I32 tool_panel_w;
  I32 tool_panel_h;
  I32 status_x;
  I32 status_y;
  I32 status_w;
  I32 status_h;
};

static void editor_set_bounds( GUI_BASE* node, I32 x, I32 y, I32 w, I32 h ) {
  if( !node )
    return;

  I32 x_extra = node->xbound - node->w;
  I32 y_extra = node->ybound - node->h;
  if( x_extra < 0 ) x_extra = 0;
  if( y_extra < 0 ) y_extra = 0;

  node->x = x;
  node->y = y;
  node->w = max( 1, w );
  node->h = max( 1, h );
  node->xbound = node->w + x_extra;
  node->ybound = node->h + y_extra;
}

static void editor_create_header_controls( GAME_EDITOR* e ) {
  GAME_EDITOR::EDITOR_GUI* egui = &e->gui;

  egui->header_toolbar = gui_editor_toolbar( 1, EDITOR_LAYOUT_MENU_Y, e->wnd->w - 3, EDITOR_LAYOUT_MENU_H, "editor toolbar" );

  egui->header_back = gui_button( 10, EDITOR_LAYOUT_NAV_Y, 92, 20, "[ back ]", editor_header_back_cb );

  egui->header_viewtype_label = gui_label( 250, EDITOR_LAYOUT_NAV_Y + 4, "view type:" );
  egui->header_viewtype = gui_editor_viewtype_segment( 320, EDITOR_LAYOUT_NAV_Y, 186, 20, "viewtype segment" );

  egui->header_mode_2d = gui_button( 500, EDITOR_LAYOUT_NAV_Y, 92, 20, "[ 2d view ]", editor_header_mode_2d_cb );
  egui->header_mode_3d = gui_button( 600, EDITOR_LAYOUT_NAV_Y, 92, 20, "[ 3d view ]", editor_header_mode_3d_cb );
  egui->header_mode_sim = gui_button( 700, EDITOR_LAYOUT_NAV_Y, 92, 20, "[ simulation ]", editor_header_mode_sim_cb );
}

static I32 editor_layout_props_w_max( GUI_EDITORWINDOW* wnd ) {
  I32 max_w = wnd->w - ( EDITOR_LAYOUT_MARGIN * 2 ) - EDITOR_LAYOUT_COLUMN_GAP - EDITOR_LAYOUT_RIGHT_MIN_W;
  return max( EDITOR_LAYOUT_PROPS_MIN_W, max_w );
}

static I32 editor_layout_content_bottom( GUI_EDITORWINDOW* wnd ) {
  return wnd->h - EDITOR_LAYOUT_MARGIN - EDITOR_LAYOUT_STATUS_H - EDITOR_LAYOUT_STATUS_GAP;
}

static I32 editor_layout_left_available_h( GUI_EDITORWINDOW* wnd ) {
  return max( 1, editor_layout_content_bottom( wnd ) - EDITOR_LAYOUT_CONTENT_Y );
}

static I32 editor_layout_view_h_max( GUI_EDITORWINDOW* wnd ) {
  I32 max_h = editor_layout_content_bottom( wnd )
            - EDITOR_LAYOUT_CONTENT_Y
            - EDITOR_LAYOUT_VIEW_TOOL_GAP
            - EDITOR_LAYOUT_TOOL_PANEL_H
            - ( EDITOR_LAYOUT_TITLE_OFFSET * 2 );
  return max( EDITOR_LAYOUT_VIEW_MIN_H, max_h );
}

static void editor_layout_clamp_custom_sizes( GAME_EDITOR* e ) {
  if( !e || !e->wnd )
    return;

  GAME_EDITOR::EDITOR_GUI* egui = &e->gui;
  if( egui->props_w <= 0 )
    egui->props_w = EDITOR_LAYOUT_PROPS_DEFAULT_W;
  if( egui->props_h <= 0 )
    egui->props_h = EDITOR_LAYOUT_PROPS_DEFAULT_H;
  if( egui->view_h <= 0 )
    egui->view_h = EDITOR_LAYOUT_VIEW_DEFAULT_H;

  egui->props_w = min( max( egui->props_w, EDITOR_LAYOUT_PROPS_MIN_W ), editor_layout_props_w_max( e->wnd ) );
  I32 left_avail_h = editor_layout_left_available_h( e->wnd );
  I32 max_props_h = left_avail_h
                  - EDITOR_LAYOUT_LEFT_BOX_GAP
                  - EDITOR_LAYOUT_ASSETS_MIN_H
                  - ( EDITOR_LAYOUT_TITLE_OFFSET * 2 );
  max_props_h = max( EDITOR_LAYOUT_PROPS_MIN_H, max_props_h );
  egui->props_h = min( max( egui->props_h, EDITOR_LAYOUT_PROPS_MIN_H ), max_props_h );

  I32 view_max = editor_layout_view_h_max( e->wnd );
  egui->view_h = view_max;
}

static const char* editor_tool_name() {
  switch( editor->tool.type ) {
    case EDITOR_TOOL_SELECT: return "select";
    case EDITOR_TOOL_WALL: return "wall";
    case EDITOR_TOOL_POLY: return "poly";
    case EDITOR_TOOL_SPRITE: return "sprite";
    case EDITOR_TOOL_ENT: return "ent";
    default: return "none";
  }
}

const I32 EDITOR_ASSETS_ROW_H = 28;
const I32 EDITOR_ASSETS_THUMB = 20;
const I32 EDITOR_ASSETS_INNER_PAD_X = 8;
const I32 EDITOR_ASSETS_INNER_PAD_Y = 6;
const I32 EDITOR_ASSETS_SCROLLBAR_W = 7;
const I32 EDITOR_ASSETS_SCROLLBAR_GAP = 4;
const I32 EDITOR_ASSETS_SCROLL_STEP = 20;

struct EDITOR_ASSETS_LAYOUT {
  I32 inner_x{};
  I32 inner_y{};
  I32 inner_w{};
  I32 list_h{};
  I32 row_x{};
  I32 row_w{};
  I32 count{};
  I32 content_h{};
  I32 max_scroll{};
  U8 show_scroll{};
  I32 track_x{};
  I32 track_y{};
  I32 track_h{};
};

static EDITOR_ASSETS_LAYOUT editor_assets_layout( GUI_EDITOR_INFOBOX* box, I32 panel_x, I32 panel_y, I32 prop_count ) {
  EDITOR_ASSETS_LAYOUT l{};
  l.inner_x = panel_x + EDITOR_ASSETS_INNER_PAD_X;
  l.inner_y = panel_y + EDITOR_ASSETS_INNER_PAD_Y;
  l.inner_w = max( 1, box->w - ( EDITOR_ASSETS_INNER_PAD_X * 2 ) );
  l.list_h = max( 1, box->h - ( EDITOR_ASSETS_INNER_PAD_Y * 2 ) );
  l.count = prop_count + 1;
  l.content_h = l.count * EDITOR_ASSETS_ROW_H;
  l.max_scroll = max( 0, l.content_h - l.list_h );
  l.show_scroll = l.content_h > l.list_h;
  l.row_x = l.inner_x + 1;
  l.row_w = l.inner_w - 2;
  if( l.show_scroll )
    l.row_w -= EDITOR_ASSETS_SCROLLBAR_W + EDITOR_ASSETS_SCROLLBAR_GAP;
  l.row_w = max( 1, l.row_w );
  l.track_x = l.inner_x + l.inner_w - EDITOR_ASSETS_SCROLLBAR_W - 1 - ( EDITOR_ASSETS_SCROLLBAR_GAP / 2 );
  l.track_y = l.inner_y;
  l.track_h = l.list_h - 2;
  return l;
}

static void editor_assets_clamp_scroll( const EDITOR_ASSETS_LAYOUT& l ) {
  editor->gui.assets_scroll = min( max( 0, editor->gui.assets_scroll ), l.max_scroll );
}

static void gui_editor_infobox_draw_assets( GUI_EDITOR_INFOBOX* box, I32 panel_x, I32 panel_y ) {
  if( !editor->map )
    return;

  WORLD_MAP* map = editor->map;
  EDITOR_ASSETS_LAYOUT l = editor_assets_layout( box, panel_x, panel_y, (I32)map->props.size );
  editor_assets_clamp_scroll( l );
  I32 yoff = -editor->gui.assets_scroll;

  for( I32 idx = 0; idx < l.count; ++idx ) {
    I32 row_y = l.inner_y + idx * EDITOR_ASSETS_ROW_H + yoff;
    if( row_y + EDITOR_ASSETS_ROW_H - 2 <= l.inner_y || row_y >= l.inner_y + l.list_h )
      continue;

    U8 map_entry = idx == 0;
    SURF_PROPS* p = map_entry ? 0 : &map->props[idx - 1];
    U8 selected = 0;
    if( editor->gui.props ) {
      if( map_entry ) {
        selected = editor->gui.props->seltype == EDITOR_SELECT_ORIGIN
                && editor->gui.props->curselect == editor->map;
      } else {
        selected = editor->gui.props->seltype == EDITOR_SELECT_SURFPROPS
                && editor->gui.props->curselect == p;
      }
    }

    CLR rowbg = idx % 2 ? ui_clr.bg : ui_clr.bg_alt;
    if( selected )
      rowbg = CLR::blend( ui_clr.border, ui_clr.bg, 0.70f );

    gui_draw_frect( l.row_x, row_y, l.row_w, EDITOR_ASSETS_ROW_H - 2, rowbg );

    I32 tx = l.row_x + 3;
    I32 ty = row_y + 3;
    gui_draw_frect( tx, ty, EDITOR_ASSETS_THUMB, EDITOR_ASSETS_THUMB, ui_clr.border );
    if( map_entry ) {
      gui_draw_frect( tx + 1, ty + 1, EDITOR_ASSETS_THUMB - 2, EDITOR_ASSETS_THUMB - 2, ui_clr.bg_alt );
      gui_draw_str( tx + EDITOR_ASSETS_THUMB / 2, ty + 4, ALIGN_C, FNT_JPN12, ui_clr.txt, "m" );
    } else if( p->tex ) {
      gl_2d_textured_frect( _gui.gl2d_font, { (F32)(tx + 1), (F32)(ty + 1) }, { (F32)(EDITOR_ASSETS_THUMB - 2), (F32)(EDITOR_ASSETS_THUMB - 2) }, p->tex );
    } else {
      gui_draw_frect( tx + 1, ty + 1, EDITOR_ASSETS_THUMB - 2, EDITOR_ASSETS_THUMB - 2, p->clr );
    }

    I32 text_x = tx + EDITOR_ASSETS_THUMB + 8;
    CLR txt = ui_clr.txt;
    if( map_entry ) {
      gui_draw_str( text_x, row_y + 7, ALIGN_L, FNT_JPN12, txt, "[map] -> %s", map->name );
    } else if( p->tex ) {
      gui_draw_str( text_x, row_y + 7, ALIGN_L, FNT_JPN12, txt, "[%d] -> %s", idx - 1, p->tex->name );
    } else {
      gui_draw_str( text_x, row_y + 7, ALIGN_L, FNT_JPN12, txt, "[%d] -> %.2f, %.2f, %.2f", idx - 1, p->clr.r, p->clr.g, p->clr.b );
    }

  }

  if( l.show_scroll ) {
    gui_draw_frect( l.track_x, l.track_y, EDITOR_ASSETS_SCROLLBAR_W, l.track_h, ui_clr.bg_alt );
    gui_draw_rect( l.track_x, l.track_y, EDITOR_ASSETS_SCROLLBAR_W, l.track_h, ui_clr.border );

    I32 thumb_h = max( 18, ( l.track_h * l.list_h ) / max( 1, l.content_h ) );
    thumb_h = min( thumb_h, l.track_h );
    I32 travel = max( 1, l.track_h - thumb_h );
    I32 thumb_y = l.track_y + ( travel * editor->gui.assets_scroll ) / max( 1, l.max_scroll );
    gui_draw_frect( l.track_x, thumb_y + 1, EDITOR_ASSETS_SCROLLBAR_W, max( 1, thumb_h - 1 ), ui_clr.txt );
  }
}

static void gui_editor_infobox_draw_fn( void* ptr ) {
  GUI_EDITOR_INFOBOX* box = (GUI_EDITOR_INFOBOX*)ptr;

  I32 x = gui_relx( box );
  I32 y = gui_rely( box );
  I32 w = box->w;
  I32 h = box->h;

  if( box->type == EDITOR_INFOBOX_STATUS ) {
    CLR col = gui_is_fg_window( box )? ui_clr.border : ui_clr.border_inactive;
    gui_draw_frect( x, y, w, h, col );
    gui_draw_frect( x + 1, y + 1, w - 2, h - 2, ui_clr.bg_sec );
    gui_draw_str(
      x + 6,
      y + 4,
      ALIGN_L,
      FNT_JPN12,
      ui_clr.txt,
      "selected tool: %s. click in viewport to create/edit.",
      editor_tool_name()
    );

    if( editor->game && editor->game->gl ) {
      GL_DATA* gl = editor->game->gl;
      gui_draw_str(
        x + w - 6,
        y + 4,
        ALIGN_R,
        FNT_JPN12,
        ui_clr.txt,
        "fps: %.3f (%.5f ms) %dx%d",
        gl->fps,
        gl->frametime,
        gl->canvas_size[0],
        gl->canvas_size[1]
      );
    }
    return;
  }

  gui_draw_str( x, y, ALIGN_L, FNT_JPN12, ui_clr.txt, "assets" );
  y += EDITOR_LAYOUT_TITLE_OFFSET;

  CLR col = gui_is_fg_window( box )? ui_clr.border : ui_clr.border_inactive;
  gui_draw_frect( x, y, w, h, col );
  gui_draw_frect( x + 1, y + 1, w - 2, h - 2, ui_clr.bg_sec );

  gui_draw_push_clip( x + 2, y + 2, w - 4, h - 4 );
  gui_editor_infobox_draw_assets( box, x, y );
  gui_draw_pop_clip();
}

static void gui_editor_infobox_input_fn( void* ptr ) {
  GUI_EDITOR_INFOBOX* box = (GUI_EDITOR_INFOBOX*)ptr;
  static U8 assets_click_held = 0;
  if( box->type == EDITOR_INFOBOX_ASSETS ) {
    I32 x = gui_relx( box );
    I32 y = gui_rely( box ) + EDITOR_LAYOUT_TITLE_OFFSET;
    I32 w = box->w;
    I32 h = box->h;
    I32 prop_count = editor->map ? (I32)editor->map->props.size : 0;
    EDITOR_ASSETS_LAYOUT l = editor_assets_layout( box, x, y, prop_count );
    editor_assets_clamp_scroll( l );
    I32 mx, my;
    gui_cursor_pos( &mx, &my );
    U8 inbounds = mx >= x && mx <= x + w && my >= y && my <= y + h;
    if( inbounds ) {
      U8 scroll = gui_mbutton_down( GUI_MBTNSCROLL );
      if( scroll ) {
        gui_capture_scroll();
        if( scroll == 1 )
          editor->gui.assets_scroll -= EDITOR_ASSETS_SCROLL_STEP;
        else if( scroll == (U8)-1 )
          editor->gui.assets_scroll += EDITOR_ASSETS_SCROLL_STEP;

        editor_assets_clamp_scroll( l );
      }

      U8 m1 = gui_mbutton_down( GUI_MBTNLEFT );
      if( m1 && !assets_click_held && editor->map ) {
        if( mx >= l.row_x && mx < l.row_x + l.row_w ) {
          I32 idx = ( my - l.inner_y + editor->gui.assets_scroll ) / EDITOR_ASSETS_ROW_H;
          if( idx >= 0 && idx < l.count && editor->gui.props ) {
            if( idx == 0 )
              gui_editor_propview_select( editor->gui.props, editor->map, EDITOR_SELECT_ORIGIN );
            else
              gui_editor_propview_select( editor->gui.props, &editor->map->props[idx - 1], EDITOR_SELECT_SURFPROPS );
          }
        }
      }
      assets_click_held = m1;
    }
    if( !gui_mbutton_down( GUI_MBTNLEFT ) )
      assets_click_held = 0;
  }

  gui_base_input_fn( box );
}

static GUI_EDITOR_INFOBOX* gui_editor_infobox( I32 x, I32 y, I32 w, I32 h, U8 type, const char* name ) {
  if( !gui_check_target() )
    return 0;

  GUI_EDITOR_INFOBOX* box = new GUI_EDITOR_INFOBOX;
  box->x = x;
  box->y = y;
  box->w = w;
  box->h = h;
  box->xbound = w;
  box->ybound = h + ( type == EDITOR_INFOBOX_STATUS ? 0 : EDITOR_LAYOUT_TITLE_OFFSET );
  box->draw_fn = gui_editor_infobox_draw_fn;
  box->input_fn = gui_editor_infobox_input_fn;
  box->type = type;
  strcpy( box->name, name );

  GUI_VIEW* parent = gui_get_view();
  box->parent = parent;
  parent->children.push( box );
  return box;
}

static EDITOR_LAYOUT editor_calc_layout( GAME_EDITOR* e ) {
  GUI_EDITORWINDOW* wnd = e->wnd;
  editor_layout_clamp_custom_sizes( e );

  EDITOR_LAYOUT l{};
  l.left_x = EDITOR_LAYOUT_MARGIN;
  l.left_w = e->gui.props_w;
  l.right_x = l.left_x + l.left_w + EDITOR_LAYOUT_COLUMN_GAP;
  l.right_w = max( 1, wnd->w - EDITOR_LAYOUT_MARGIN - l.right_x );
  l.tool_btn_x = l.right_x;
  l.tool_btn_w = 45;
  l.tool_panel_x = l.tool_btn_x + l.tool_btn_w + EDITOR_LAYOUT_TOOL_PANEL_GAP;
  l.tool_panel_w = EDITOR_LAYOUT_TOOL_PANEL_W;

  l.top_y = EDITOR_LAYOUT_NAV_Y;
  l.props_y = EDITOR_LAYOUT_CONTENT_Y;
  l.view_y = EDITOR_LAYOUT_CONTENT_Y;
  l.props_h = e->gui.props_h;
  I32 left_total_h = max( 1, editor_layout_content_bottom( wnd ) - l.props_y );
  I32 props_max_h = left_total_h
                  - EDITOR_LAYOUT_LEFT_BOX_GAP
                  - EDITOR_LAYOUT_ASSETS_MIN_H
                  - ( EDITOR_LAYOUT_TITLE_OFFSET * 2 );
  props_max_h = max( EDITOR_LAYOUT_PROPS_MIN_H, props_max_h );
  l.props_h = min( max( l.props_h, EDITOR_LAYOUT_PROPS_MIN_H ), props_max_h );
  l.assets_y = l.props_y + EDITOR_LAYOUT_TITLE_OFFSET + l.props_h + EDITOR_LAYOUT_LEFT_BOX_GAP;
  l.assets_h = max( EDITOR_LAYOUT_ASSETS_MIN_H, editor_layout_content_bottom( wnd ) - l.assets_y - EDITOR_LAYOUT_TITLE_OFFSET );

  l.view_h = e->gui.view_h;
  l.tool_y = l.view_y + EDITOR_LAYOUT_TITLE_OFFSET + l.view_h + EDITOR_LAYOUT_VIEW_TOOL_GAP;
  l.tool_btn_y = l.tool_y + EDITOR_LAYOUT_TOOL_BTN_TOP_GAP;

  I32 bottom_content = editor_layout_content_bottom( wnd );
  l.tool_h = max( 1, bottom_content - l.tool_y - EDITOR_LAYOUT_TITLE_OFFSET );
  l.tool_panel_h = l.tool_h;
  l.tool_btn_step = 23;

  l.tool_panel_w = EDITOR_LAYOUT_TOOL_PANEL_W;
  I32 required_w = l.tool_btn_w + EDITOR_LAYOUT_TOOL_PANEL_GAP + l.tool_panel_w;
  if( required_w > l.right_w ) {
    I32 overflow = required_w - l.right_w;
    I32 shrink_panel = min( overflow, max( 0, l.tool_panel_w - 180 ) );
    l.tool_panel_w -= shrink_panel;
  }

  l.tool_panel_x = l.tool_btn_x + l.tool_btn_w + EDITOR_LAYOUT_TOOL_PANEL_GAP;

  l.status_x = EDITOR_LAYOUT_MARGIN;
  l.status_w = max( 1, wnd->w - EDITOR_LAYOUT_MARGIN * 2 );
  l.status_h = EDITOR_LAYOUT_STATUS_H;
  l.status_y = wnd->h - EDITOR_LAYOUT_MARGIN - l.status_h;

  return l;
}

static void editor_resize_base_view( GUI_EDITORWINDOW* wnd ) {
  GUI_VIEW* base = (GUI_VIEW*)gui_find_node( wnd, "BASE_VIEW" );
  if( !base )
    return;

  editor_set_bounds( base, 1, 1, wnd->w - 2, wnd->h - 2 );
}

static void editor_layout_start_menu( GAME_EDITOR* e ) {
  GUI_EDITORWINDOW* wnd = e->wnd;
  GUI_LIST* list = (GUI_LIST*)gui_find_node( wnd, "map list" );
  GUI_BUTTON* new_map = (GUI_BUTTON*)gui_find_node( wnd, "new map" );
  GUI_BUTTON* load_map = (GUI_BUTTON*)gui_find_node( wnd, "load map" );
  if( !list || !new_map || !load_map )
    return;

  const I32 min_margin = 10;
  const I32 list_title_offset = 15;
  const I32 button_gap = 10;
  const I32 button_h = 25;
  const I32 list_button_gap = 5;
  I32 list_w = 200;
  I32 list_h = 200;
  I32 max_list_w = max( 80, wnd->w - min_margin * 2 );
  I32 start_y = EDITOR_LAYOUT_CONTENT_Y;
  I32 max_list_h = max( 80, wnd->h - ( start_y + list_title_offset + list_button_gap + button_h + min_margin ) );
  if( list_w > max_list_w ) list_w = max_list_w;
  if( list_h > max_list_h ) list_h = max_list_h;

  I32 x = ( wnd->w - list_w ) / 2;
  I32 y = start_y;
  I32 bottom = y + list_title_offset + list_h + list_button_gap + button_h;
  if( bottom > wnd->h - min_margin ) {
    y = max( min_margin, wnd->h - min_margin - ( list_title_offset + list_h + list_button_gap + button_h ) );
  }

  editor_set_bounds( list, x, y, list_w, list_h );
  I32 bw = max( 50, ( list_w - button_gap ) / 2 );
  I32 by = y + list_title_offset + list_h + list_button_gap;
  editor_set_bounds( new_map, x, by, bw, button_h );
  editor_set_bounds( load_map, x + bw + button_gap, by, bw, button_h );
}

static void editor_layout_tool_buttons( GUI_EDITORWINDOW* wnd, const EDITOR_LAYOUT& l ) {
  const char* names[] = { "none", "select", "wall", "poly", "sprite", "ent" };
  I32 y = l.tool_btn_y;
  for( U32 i = 0; i < sizeof( names ) / sizeof( names[0] ); ++i ) {
    GUI_BUTTON* btn = (GUI_BUTTON*)gui_find_node( wnd, names[i] );
    editor_set_bounds( btn, l.tool_btn_x, y, l.tool_btn_w, 20 );
    y += l.tool_btn_step;
  }
}

static void editor_layout_map_view( GAME_EDITOR* e ) {
  if( !e->gui.v2d || !e->gui.v3d || !e->gui.props || !e->gui.tool )
    return;

  EDITOR_LAYOUT l = editor_calc_layout( e );
  editor_raise_header_toolbar( e );

  GAME_EDITOR::EDITOR_GUI* egui = &e->gui;
  GUI_BASE* toolbar = egui->header_toolbar;
  GUI_BUTTON* back = egui->header_back;
  GUI_BUTTON* v2btn = egui->header_mode_2d;
  GUI_BUTTON* v3btn = egui->header_mode_3d;
  GUI_BUTTON* simbtn = egui->header_mode_sim;
  GUI_LABEL* view_type_lbl = egui->header_viewtype_label;
  GUI_BASE* view_type_seg = egui->header_viewtype;

  const I32 menu_btn_y = EDITOR_LAYOUT_MENU_Y;
  const I32 top_btn_y = l.top_y;
  const I32 top_btn_h = 20;
  const I32 nav_left_x = 10;
  const I32 nav_btn_w = 92;
  const I32 nav_gap = 8;
  const I32 mode_btn_w = 92;
  I32 mode_group_w = mode_btn_w * 3 + nav_gap * 2;
  I32 mode_x = e->wnd->w - EDITOR_LAYOUT_MARGIN - mode_group_w;
  I32 type_seg_x = l.right_x;
  I32 type_lbl_w = 64;
  I32 type_lbl_x = type_seg_x - type_lbl_w - 6;
  I32 max_type_w = mode_x - type_seg_x - 8;
  I32 type_seg_w = min( 174, max( 120, max_type_w ) );

  editor_set_bounds( toolbar, 1, menu_btn_y, e->wnd->w - 3, EDITOR_LAYOUT_MENU_H );

  editor_set_bounds( back, nav_left_x, top_btn_y, nav_btn_w, top_btn_h );

  GUI_BUTTON* mode_btns[] = { v2btn, v3btn, simbtn };
  const char* mode_names[] = { "[ 2d view ]", "[ 3d view ]", "[ simulation ]" };
  for( I32 i = 0; i < 3; ++i ) {
    I32 bx = mode_x + ( mode_btn_w + nav_gap ) * i;
    editor_set_bounds( mode_btns[i], bx, top_btn_y, mode_btn_w, top_btn_h );
    if( mode_btns[i] )
      snprintf( mode_btns[i]->name, GUI_NAME_LEN, "%s", mode_names[i] );
  }

  U8 show_viewtype = e->gui.view_mode == EDITOR_VIEWMODE_2D;
  if( view_type_lbl ) {
    view_type_lbl->x = type_lbl_x;
    view_type_lbl->y = top_btn_y + 4;
    view_type_lbl->enabled = show_viewtype;
  }
  if( view_type_seg ) {
    editor_set_bounds( view_type_seg, type_seg_x, top_btn_y, type_seg_w, top_btn_h );
    view_type_seg->enabled = show_viewtype;
  }

  editor_set_bounds( e->gui.props, l.left_x, l.props_y, l.left_w, l.props_h );
  if( e->gui.props->itemview ) {
    editor_set_bounds(
      e->gui.props->itemview,
      0,
      EDITOR_LAYOUT_TITLE_OFFSET,
      e->gui.props->w,
      e->gui.props->h
    );
  }
  if( e->gui.assets ) {
    editor_set_bounds( e->gui.assets, l.left_x, l.assets_y, l.left_w, l.assets_h );
  }

  editor_set_bounds( e->gui.v2d, l.right_x, l.view_y, l.right_w, l.view_h );
  e->gui.v2d->ybound = e->gui.v2d->h + EDITOR_LAYOUT_TITLE_OFFSET;
  editor_set_bounds( e->gui.v3d, l.right_x, l.view_y, l.right_w, l.view_h );
  e->gui.v3d->ybound = e->gui.v3d->h + EDITOR_LAYOUT_TITLE_OFFSET;

  editor_layout_tool_buttons( e->wnd, l );

  editor_set_bounds( e->gui.tool, l.tool_panel_x, l.tool_y, l.tool_panel_w, l.tool_panel_h );
  if( e->gui.tool->itemview ) {
    editor_set_bounds(
      e->gui.tool->itemview,
      0,
      EDITOR_LAYOUT_TITLE_OFFSET,
      e->gui.tool->w,
      e->gui.tool->h
    );
  }
  if( e->gui.status ) {
    editor_set_bounds( e->gui.status, l.status_x, l.status_y, l.status_w, l.status_h );
  }

  editor_apply_view_mode( e );

  if( e->gui.gridlabel ) {
    I32 gx = 150;
    I32 gy = e->gui.v2d->h - 4;
    e->gui.gridlabel->x = gx;
    e->gui.gridlabel->y = gy + 1;

    GUI_BUTTON* plus = (GUI_BUTTON*)gui_find_node( e->gui.v2d, "+" );
    GUI_BUTTON* minus = (GUI_BUTTON*)gui_find_node( e->gui.v2d, "-" );
    GUI_CHECKBOX* propgrid = (GUI_CHECKBOX*)gui_find_node( e->gui.v2d, "properties grid" );
    GUI_CHECKBOX* wireframe = (GUI_CHECKBOX*)gui_find_node( e->gui.v2d, "wireframe" );
    editor_set_bounds( plus, gx + 70, gy, 18, 18 );
    editor_set_bounds( minus, gx + 93, gy, 18, 18 );
    editor_set_bounds( propgrid, gx + 120, gy, propgrid ? propgrid->w : 120, 18 );
    editor_set_bounds( wireframe, gx + 240, gy, wireframe ? wireframe->w : 80, 18 );
  }

  GUI_BUTTON* compile = (GUI_BUTTON*)gui_find_node( e->gui.v3d, "compile bsp" );
  GUI_CHECKBOX* drawbsp = (GUI_CHECKBOX*)gui_find_node( e->gui.v3d, "draw bsp" );
  I32 gy3d = e->gui.v3d->h - 4;
  editor_set_bounds( compile, 1, gy3d, 90, 18 );
  editor_set_bounds( drawbsp, 101, gy3d, drawbsp ? drawbsp->w : 80, 18 );

  editor_update_properties_column( e );
  gui_editor_toolview_update( e->gui.tool );
  editor_notify_grid_change( e );
}

void gui_editorwindow_draw_fn( void* ptr ) {
  GUI_EDITORWINDOW* wnd = (GUI_EDITORWINDOW*)ptr;
  editor_raise_header_toolbar( editor );
  GUI_BASE* toolbar = ( editor && editor->map ) ? editor->gui.header_toolbar : 0;
  GUI_EDITOR_TOOLBAR* tbar = (GUI_EDITOR_TOOLBAR*)toolbar;
  U8 menu_open = tbar && ( tbar->file_open || tbar->edit_open );
  F32 saved_mx = input.mouse.pos.x;
  F32 saved_my = input.mouse.pos.y;
  if( menu_open ) {
    editor_menu_hover_mask_active = 1;
    editor_menu_hover_real_x = (I32)saved_mx;
    editor_menu_hover_real_y = (I32)saved_my;
    input.mouse.pos.x = -100000.f;
    input.mouse.pos.y = -100000.f;
  } else {
    editor_menu_hover_mask_active = 0;
  }

  CLR clr = gui_is_fg_window( wnd )? ui_clr.border : ui_clr.border_inactive;
  gui_draw_frect( wnd->x, wnd->y, wnd->w, wnd->h, clr );
  gui_draw_frect( wnd->x + 1, wnd->y + 1, wnd->w - 2, wnd->h - 2, ui_clr.bg );

  wnd->children.each( fn( GUI_BASE** ptr ) {
    GUI_BASE* it = *ptr;
    if( !it->enabled ) return;

    if( it->draw_fn ) it->draw_fn( it );
    else dlog( "gui_editorwindow_draw_fn(): child %p has no draw_fn", it );
  } );

  input.mouse.pos.x = saved_mx;
  input.mouse.pos.y = saved_my;
  editor_menu_hover_mask_active = 0;
}

static void gui_editorwindow_input_fn( void* ptr ) {
  GUI_EDITORWINDOW* wnd = (GUI_EDITORWINDOW*)ptr;
  editor_raise_header_toolbar( editor );

  GUI_BASE* toolbar = ( editor && editor->map ) ? editor->gui.header_toolbar : 0;
  if( toolbar && toolbar->enabled && toolbar->input_fn )
    toolbar->input_fn( toolbar );

  GUI_EDITOR_TOOLBAR* tbar = (GUI_EDITOR_TOOLBAR*)toolbar;
  U8 menu_open = tbar && ( tbar->file_open || tbar->edit_open );
  if( menu_open )
    return;

  wnd->children.each( fn( GUI_BASE** childptr ) {
    GUI_BASE* child = *childptr;
    if( !child || child == toolbar || !child->enabled || !child->input_fn )
      return;

    child->input_fn( child );
  } );
}

GUI_EDITORWINDOW* gui_editorwindow_create( I32 w, I32 h ) {
  GUI_EDITORWINDOW* wnd = new GUI_EDITORWINDOW;
  wnd->x = 0;
  wnd->y = 0;
  wnd->xbound = wnd->w = w;
  wnd->ybound = wnd->h = h;
  wnd->locked = 1;
  wnd->draw_fn = gui_editorwindow_draw_fn;
  wnd->input_fn = gui_editorwindow_input_fn;
  strcpy( wnd->name, "EDITORWINDOW" );

  _gui.windows.push( wnd );
  gui_set_window( wnd );

  gui_set_view( 0 );
  gui_view( 1, 1, w - 2, h - 2 );
  return wnd;
}

GUI_EDITORWINDOW* gui_editorwindow( I32 w, I32 h ) {
  GUI_EDITORWINDOW* wnd = gui_editorwindow_create( w, h );

  GAME_EDITOR* e = editor;
  LIST<GUI_LIST_ENTRY>* map_list = editor_get_map_list( e );

  gui_list( wnd->w / 2 - 100, EDITOR_LAYOUT_CONTENT_Y, 200, 200, "map list", map_list, &editor->gui.map_select );
  gui_button( wnd->w / 2 - 100, EDITOR_LAYOUT_CONTENT_Y + 220, 95, 25, "new map", editor_new_map_cb );
  gui_button( wnd->w / 2 + 5, EDITOR_LAYOUT_CONTENT_Y + 220, 95, 25, "load map", editor_load_map_cb );
  return wnd;
}


void editor_update_properties_column( GAME_EDITOR* e ) {
  GAME_EDITOR::EDITOR_GUI* egui = &e->gui;
  gui_editor_propview_update( egui->props );
}

void editor_create_properties_column( GAME_EDITOR* e ) {
  GAME_EDITOR::EDITOR_GUI* egui = &e->gui;
  egui->props = gui_editor_propview( 10, EDITOR_LAYOUT_CONTENT_Y, 300, 460 );
  gui_editor_propview_select( egui->props, e->map, EDITOR_SELECT_ORIGIN );
}

void editor_create_auxiliary_panels( GAME_EDITOR* e ) {
  GAME_EDITOR::EDITOR_GUI* egui = &e->gui;
  egui->assets = gui_editor_infobox( 10, EDITOR_LAYOUT_CONTENT_Y + 310, 300, 180, EDITOR_INFOBOX_ASSETS, "EDITOR_ASSETS_VIEW" );
  egui->status = gui_editor_infobox( 10, 568, 780, EDITOR_LAYOUT_STATUS_H, EDITOR_INFOBOX_STATUS, "EDITOR_STATUS_VIEW" );
}

void editor_update_toolview( GAME_EDITOR* e ) {
  GAME_EDITOR::EDITOR_GUI* egui = &e->gui;
  gui_editor_toolview_update( egui->tool );
}

void settool( U8 t ) {
  editor->tool.type = t;

  if( editor->gui.v2d ) {
    editor->gui.v2d->poly_drag = 0;
  }

  if( editor->gui.tool ) {
    editor_update_toolview( editor );
  }
}

static U8 EDITOR_TOOL_BUTTON_TYPES[] = {
  EDITOR_TOOL_NONE,
  EDITOR_TOOL_SELECT,
  EDITOR_TOOL_WALL,
  EDITOR_TOOL_POLY,
  EDITOR_TOOL_SPRITE,
  EDITOR_TOOL_ENT
};

static void settool_btn_cb( void* ptr ) {
  GUI_BUTTON* btn = (GUI_BUTTON*)ptr;
  if( !btn || !btn->extra )
    return;

  settool( *(U8*)btn->extra );
}

void editor_create_toolview_column( GAME_EDITOR* e ) {
  GAME_EDITOR::EDITOR_GUI* egui = &e->gui;

  I32 x = 320;
  I32 y = EDITOR_LAYOUT_CONTENT_Y + EDITOR_LAYOUT_VIEW_DEFAULT_H + EDITOR_LAYOUT_VIEW_TOOL_GAP + EDITOR_LAYOUT_TOOL_BTN_TOP_GAP;
  I32 off = 23;
  const char* labels[] = { "none", "select", "wall", "poly", "sprite", "ent" };

  for( U32 i = 0; i < sizeof( labels ) / sizeof( labels[0] ); ++i ) {
    GUI_BUTTON* btn = gui_button( x, y, 45, 20, labels[i], settool_btn_cb );
    btn->extra = &EDITOR_TOOL_BUTTON_TYPES[i];
    y += off;
  }

  egui->tool = gui_editor_toolview( 370, y - EDITOR_LAYOUT_TOOL_BTN_TOP_GAP, 300, 150 );
  gui_editor_toolview_update( egui->tool );
}

void editor_create_game_view_column( GAME_EDITOR* e ) {
  GAME_EDITOR::EDITOR_GUI* egui = &e->gui;
  I32 x = 320, y = EDITOR_LAYOUT_CONTENT_Y;
  egui->v2d = gui_editor_2dview( x, y, 468, 370 );
  egui->v3d = gui_editor_3dview( x, y, 468, 370 );
  egui->v2d->enabled = 1;
  egui->v3d->enabled = 0;
}

void editor_create_map_view( GAME_EDITOR* e ) {
  if( !e->map ) {
    dlog( "editor_create_map_views() : no map loaded\n" );
    return;
  }

  GUI_EDITORWINDOW* w = e->wnd;
  w->children.each( fn( GUI_BASE** ptr ) {
    gui_free( *ptr );
  } );
  w->children.clear();
  editor_clear_gui_state_refs( e );
  e->gui.view2d_type = EDITOR_2DVIEW_TOP_DOWN;
  e->gui.view_mode = EDITOR_VIEWMODE_2D;

  gui_set_window( w );
  gui_set_view( 0 );

  gui_view( 1, 1, w->w - 2, w->h - 2 );

  editor_create_header_controls( e );
  editor_create_properties_column( e );
  editor_create_toolview_column( e );
  editor_create_game_view_column( e );
  editor_create_auxiliary_panels( e );
  editor_layout_map_view( e );
}

void editor_resize( GAME_EDITOR* e, I32 w, I32 h ) {
  if( !e || !e->wnd )
    return;

  w = max( 1, w );
  h = max( 1, h );

  GUI_EDITORWINDOW* wnd = e->wnd;
  if( wnd->w == w && wnd->h == h )
    return;

  editor_set_bounds( wnd, 0, 0, w, h );
  editor_resize_base_view( wnd );

  if( e->map )
    editor_layout_map_view( e );
  else
    editor_layout_start_menu( e );

  if( e->gui.new_map_popup ) {
    GUI_WINDOW* popup = e->gui.new_map_popup;
    if( popup->x > w - 5 ) popup->x = w - 5;
    if( popup->x + popup->w < 5 ) popup->x = 5 - popup->w;
    if( popup->y > h - 5 ) popup->y = h - 5;
    if( popup->y + popup->h < 5 ) popup->y = 5 - popup->h;
  }
}

void close_new_map_popup( void* ) {
  gui_push_callback( pfn( void* ) {
    GUI_WINDOW* popup = editor->gui.new_map_popup;
    if( !popup )
      return;

    gui_free( popup );
    editor->gui.new_map_popup = 0;
  } );
}

void editor_new_map_cb( void* ptr ) {
  if( editor->gui.new_map_popup ) return;

  GUI_WINDOW* cwnd = gui_get_window();
  GUI_VIEW* view = gui_get_view();

  I32 wx = 250;
  I32 wy = 140;
  I32 ww = 300;
  I32 wh = 200;

  editor->gui.new_map_popup = gui_window( wx, wy, ww, wh );
  gui_title( "new map" );
  GUI_TEXTBOX* tb = gui_textbox( 10, 20, ww - 20, 20, "name", 32 );
  tb->active = 1;

  gui_button( ww - 50 - 70, wh - 20 - 10, 50, 20, "ok", pfn( void* ptr ) {
    GUI_BUTTON* btn = (GUI_BUTTON*)ptr;
    GUI_TEXTBOX* name = (GUI_TEXTBOX*)gui_find_node( btn->parent, "name" );
    if( !name )
      return;

    editor_new_map( editor, name->value );
    close_new_map_popup( 0 );
  } );

  gui_button( ww - 50 - 10, wh - 20 - 10, 50, 20, "cancel", close_new_map_popup );

  gui_set_window( cwnd );
  gui_set_view( view );
}

void editor_load_map_cb( void* ptr ) {
  GUI_LIST* list = (GUI_LIST*)gui_find_node( editor->wnd, "map list" );

  GUI_LIST_ENTRY* e = gui_list_get_selected( list );
  if( !e )
    return;

  char full_path[256];
  sprintf( full_path, "../assets/maps/%s", e->title );

  if( editor->map )
    editor_close( editor );

  editor_load_map( editor, full_path );
}