summaryrefslogtreecommitdiff
path: root/src/editor/view2d.cpp
blob: b68846e66336ca3b738657e66893a3c1f78ec7e9 (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
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
#include <math.h>
#include "editor.h"
#include "../render/gl_2d.h"
#include "../game/objlist.h"
#include "../game/world/bsp.h"

const I32 EDITORVIEW_TITLE_OFFSET = 15;
const I32 EDITORVIEW_GUTTERS_OFFSETX = 22;
const I32 EDITORVIEW_GUTTERS_OFFSETY = 16;
const I32 EDITORVIEW_TOOLBAR_OFFSET = 20;
const I32 EDITOR_POLY_SIDES_RUNTIME_MAX = 256;

VEC2 gui_editor_2dview_screen_to_world( GUI_EDITOR_2DVIEW* view, I32 x, I32 y );
VEC2 gui_editor_2dview_world_to_screen( GUI_EDITOR_2DVIEW* view, VEC2 world );
F32 gui_editor_2dview_placement_z();

VEC3 gui_editor_2dview_plane_to_world3( VEC2 plane, F32 depth ) {
  return { plane.x, plane.y, depth };
}

void gui_editor_2dview_set_plane( VEC3* v, VEC2 plane ) {
  if( !v )
    return;

  v->x = plane.x;
  v->y = plane.y;
}

void gui_editor_2dview_add_plane_delta( VEC3* v, VEC2 delta ) {
  if( !v )
    return;

  v->x += delta.x;
  v->y += delta.y;
}

void gui_editor_2dview_snap_plane( VEC3* v ) {
  if( !v )
    return;

  v->x = m_snap_to_grid( v->x, editor->grid );
  v->y = m_snap_to_grid( v->y, editor->grid );
}

VEC2 gui_editor_2dview_world3_to_screen( GUI_EDITOR_2DVIEW* view, const VEC3& v ) {
  return gui_editor_2dview_world_to_screen( view, { v.x, v.y } );
}

F32 gui_editor_2dview_ground_z() {
  WORLD_MAP* map = editor->map;
  if( !map )
    return 0.f;

  F32 wantedz = map->startpos.z;
  F32 bestz = 0.f;
  F32 bestdist = FLT_MAX;
  U8 found_floor = 0;
  map->polygons.each( fn( MAP_POLYGON* p ) {
    if( p->type != MPT_FLOOR || !p->vertices.size )
      return;

    F32 floorz = p->vertices[0].pos.z;
    F32 dist = fabsf( floorz - wantedz );
    if( !found_floor || dist < bestdist ) {
      bestz = floorz;
      bestdist = dist;
      found_floor = 1;
    }
  } );

  if( found_floor )
    return bestz;

  return isfinite( map->mins.z ) ? map->mins.z : 0.f;
}

F32 gui_editor_2dview_wall_height() {
  F32 wallheight = editor->tool.wallheight;
  if( !isfinite( wallheight ) || wallheight < 1.f )
    wallheight = EDITOR_DEFAULT_WALL_HEIGHT;
  return wallheight;
}

F32 gui_editor_2dview_placement_z() {
  F32 placeheight = editor->tool.placementheight;
  if( !isfinite( placeheight ) )
    placeheight = EDITOR_DEFAULT_PLACEMENT_HEIGHT;

  return gui_editor_2dview_ground_z() + placeheight;
}

F32 gui_editor_2dview_fit_scale_for_bounds( GUI_EDITOR_2DVIEW* view, VEC3 mins, VEC3 maxs ) {
  I32 w = view->w - EDITORVIEW_GUTTERS_OFFSETX - 1;
  I32 h = view->h - EDITORVIEW_GUTTERS_OFFSETY - 1;
  if( w <= 0 || h <= 0 )
    return 1.f;

  F32 mapw = maxs.x - mins.x;
  F32 maph = maxs.y - mins.y;
  if( !isfinite( mapw ) || !isfinite( maph ) || mapw <= 0.0001f || maph <= 0.0001f )
    return 1.f;

  return min( w / mapw, h / maph );
}

void gui_editor_2dview_check_bounds_preserve_view( GUI_EDITOR_2DVIEW* view ) {
  if( !view || !editor || !editor->map )
    return;

  I32 mx, my;
  gui_cursor_pos( &mx, &my );
  VEC2 world_before = gui_editor_2dview_screen_to_world( view, mx, my );

  VEC3 oldmins = editor->map->mins;
  VEC3 oldmaxs = editor->map->maxs;
  F32 oldfit = gui_editor_2dview_fit_scale_for_bounds( view, oldmins, oldmaxs );

  map_check_bounds( editor->map );

  VEC3 newmins = editor->map->mins;
  VEC3 newmaxs = editor->map->maxs;
  F32 newfit = gui_editor_2dview_fit_scale_for_bounds( view, newmins, newmaxs );
  if( newfit > 0.000001f ) {
    view->scale *= oldfit / newfit;
  }

  VEC2 world_after = gui_editor_2dview_screen_to_world( view, mx, my );
  view->posx += world_before.x - world_after.x;
  view->posy += world_before.y - world_after.y;
}

F32 gui_editor_2dview_calc_scale( GUI_EDITOR_2DVIEW* view ) {
  WORLD_MAP* m = editor->map;

  I32 w = view->w - EDITORVIEW_GUTTERS_OFFSETX - 1;
  I32 h = view->h - EDITORVIEW_GUTTERS_OFFSETY - 1;

  F32 mapw = m->maxs.x - m->mins.x;
  F32 maph = m->maxs.y - m->mins.y;

  F32 scale = min( w / mapw, h / maph );
  return scale * view->scale;
}

VEC2 gui_editor_2dview_screen_to_world( GUI_EDITOR_2DVIEW* view, I32 x, I32 y ) {
  WORLD_MAP* m = editor->map;
  F32 scale = gui_editor_2dview_calc_scale( view );
  if( !isfinite( scale ) || fabsf( scale ) < 0.000001f ) {
    return { 0.f, 0.f };
  }

  F32 xoff = m->mins.x + view->posx;
  F32 yoff = m->mins.y + view->posy;

  F32 vx = (F32)gui_relx( view ) + EDITORVIEW_GUTTERS_OFFSETX;
  F32 vy = (F32)gui_rely( view ) + EDITORVIEW_TITLE_OFFSET + EDITORVIEW_GUTTERS_OFFSETY;

  F32 worldx = ((F32)x - vx) / scale + xoff;
  F32 worldy = ((F32)y - vy) / scale + yoff;
  return { worldx, worldy };
}

VEC2 gui_editor_2dview_world_to_screen( GUI_EDITOR_2DVIEW* view, VEC2 world ) {
  WORLD_MAP* m = editor->map;
  F32 scale = gui_editor_2dview_calc_scale( view );

  F32 xoff = m->mins.x + view->posx;
  F32 yoff = m->mins.y + view->posy;

  F32 vx = (F32)gui_relx( view );
  F32 vy = (F32)gui_rely( view );
  vy += EDITORVIEW_TITLE_OFFSET + EDITORVIEW_GUTTERS_OFFSETY;
  vx += EDITORVIEW_GUTTERS_OFFSETX;

  F32 x = vx + (world.x - xoff) * scale;
  F32 y = vy + (world.y - yoff) * scale;

  return { x, y };
}

void gui_editor_2dview_select( GUI_EDITOR_2DVIEW* view, void* what, U8 seltype ) {
  view->curselect = what;
  view->seltype = seltype;
}

U8 gui_editor_2dview_is_gizmo_active( GUI_EDITOR_2DVIEW* view, void* what, U8 seltype ) {
  GUI_EDITOR_PROPVIEW* props = editor->gui.props;
  if( props->seltype == EDITOR_SELECT_WALL && (seltype == EDITOR_SELECT_WVERTEX) ) {
    MAP_WALL* s = (MAP_WALL*)props->curselect;
    if( what == &s->start || what == &s->end )
      return 1;
  }
  else if( props->seltype == seltype && props->curselect == what ) {
    return 1;
  }

  if( view->curdrag ) {
    return what == view->curdrag && seltype == view->dragtype;
  }
  else {
    return what == view->curselect && seltype == view->seltype;
  }
}

void gui_editor_2dview_draw_gizmo( I32 x, I32 y, CLR clr, U8 selected = 0 ) {
  const I32 GIZMO_SIZE = selected ? 10 : 6;
  const I32 ihalf = GIZMO_SIZE / 2;

  gui_draw_frect( x - ihalf - 1, y - ihalf - 1, GIZMO_SIZE, GIZMO_SIZE, { .7f, .7f, .7f, 1.f } );
  gui_draw_frect( x - ihalf + 1, y - ihalf + 1, GIZMO_SIZE, GIZMO_SIZE, { 0.f, 0.f, 0.f, 1.f } );
  gui_draw_frect( x - ihalf, y - ihalf, GIZMO_SIZE, GIZMO_SIZE, clr );
}

I32 gui_editor_2dview_poly_sides() {
  F32 raw = editor->tool.polysides;
  if( !isfinite( raw ) )
    raw = EDITOR_DEFAULT_POLY_SIDES;

  if( raw > (F32)EDITOR_POLY_SIDES_RUNTIME_MAX )
    raw = (F32)EDITOR_POLY_SIDES_RUNTIME_MAX;

  I32 sides = (I32)floorf( raw + 0.5f );
  if( sides < EDITOR_POLY_SIDES_MIN ) sides = EDITOR_POLY_SIDES_MIN;
  if( sides > EDITOR_POLY_SIDES_RUNTIME_MAX ) sides = EDITOR_POLY_SIDES_RUNTIME_MAX;
  return sides;
}

U8 gui_editor_2dview_poly_keep_regular( I32 sides ) {
  if( sides == 4 )
    return 0;

  return kb_down( SDLK_LSHIFT ) || kb_down( SDLK_RSHIFT );
}

U8 gui_editor_2dview_is_poly_drag_tool() {
  if( editor->tool.type == EDITOR_TOOL_POLY )
    return 1;

  return editor->tool.type == EDITOR_TOOL_WALL
      && editor->tool.wallshape == EDITOR_WALLSHAPE_POLYGON;
}

I32 gui_editor_2dview_poly_points( VEC2 start, VEC2 end, I32 sides, U8 regular, VEC2* out_points ) {
  if( !out_points || sides < EDITOR_POLY_SIDES_MIN )
    return 0;

  F32 minx = start.x < end.x ? start.x : end.x;
  F32 maxx = start.x > end.x ? start.x : end.x;
  F32 miny = start.y < end.y ? start.y : end.y;
  F32 maxy = start.y > end.y ? start.y : end.y;

  if( sides == 4 ) {
    out_points[0] = { maxx, maxy };
    out_points[1] = { minx, maxy };
    out_points[2] = { minx, miny };
    out_points[3] = { maxx, miny };
    return 4;
  }

  F32 cx = ( minx + maxx ) * 0.5f;
  F32 cy = ( miny + maxy ) * 0.5f;
  F32 sx = ( maxx - minx ) * 0.5f;
  F32 sy = ( maxy - miny ) * 0.5f;

  if( regular ) {
    F32 uniform = sx < sy ? sx : sy;
    sx = sy = uniform;
  }

  F32 step = ( 2.f * PI ) / sides;
  F32 rot = PI * 0.5f;
  for( I32 i = 0; i < sides; ++i ) {
    F32 ang = rot + step * i;
    out_points[i] = {
      cx + cosf( ang ) * sx,
      cy + sinf( ang ) * sy
    };
  }

  return sides;
}

struct EDITORVIEW_DRAG_RECT {
  F32 minx, maxx;
  F32 miny, maxy;
  F32 w, h;
};

struct EDITORVIEW_DRAG_SHAPE {
  EDITORVIEW_DRAG_RECT rect;
  LIST<VEC2> points;
  I32 pointc;
};

U8 gui_editor_2dview_build_drag_shape( GUI_EDITOR_2DVIEW* view, EDITORVIEW_DRAG_SHAPE* out_shape ) {
  if( !out_shape )
    return 0;

  EDITORVIEW_DRAG_RECT& rect = out_shape->rect;
  rect.minx = view->poly_start.x < view->poly_end.x ? view->poly_start.x : view->poly_end.x;
  rect.maxx = view->poly_start.x > view->poly_end.x ? view->poly_start.x : view->poly_end.x;
  rect.miny = view->poly_start.y < view->poly_end.y ? view->poly_start.y : view->poly_end.y;
  rect.maxy = view->poly_start.y > view->poly_end.y ? view->poly_start.y : view->poly_end.y;
  rect.w = rect.maxx - rect.minx;
  rect.h = rect.maxy - rect.miny;

  if( rect.w <= 0.0001f || rect.h <= 0.0001f )
    return 0;

  I32 sides = gui_editor_2dview_poly_sides();
  out_shape->points.resize( sides );
  U8 regular = gui_editor_2dview_poly_keep_regular( sides );
  out_shape->pointc = gui_editor_2dview_poly_points(
    view->poly_start,
    view->poly_end,
    sides,
    regular,
    out_shape->points.data
  );

  return out_shape->pointc >= 3;
}

U8 gui_editor_2dview_is_color( CLR c, F32 r, F32 g, F32 b ) {
  return fabsf( c.r - r ) < 0.01f
      && fabsf( c.g - g ) < 0.01f
      && fabsf( c.b - b ) < 0.01f;
}

I32 gui_editor_2dview_find_or_create_wallpoly_propid() {
  I32 propid = editor->map->props.idx_where( fn( SURF_PROPS* p ) {
    return !p->tex
        && ( gui_editor_2dview_is_color( p->clr, 0.f, 1.f, 1.f )
          || gui_editor_2dview_is_color( p->clr, 0.f, 0.f, 1.f ) );
  } );

  if( propid != -1 )
    return propid;

  editor->map->props.push( { .tex = 0, .clr = CLR::CYAN() } );
  return editor->map->props.size - 1;
}

void gui_editor_2dview_draw_dashed_hline( I32 x0, I32 x1, I32 y, CLR col ) {
  if( x0 > x1 ) {
    I32 tmp = x0;
    x0 = x1;
    x1 = tmp;
  }

  const I32 dash = 6;
  const I32 gap = 4;
  for( I32 x = x0; x <= x1; x += dash + gap ) {
    I32 ex = x + dash;
    if( ex > x1 ) ex = x1;
    gui_draw_line( x, y, ex, y, col );
  }
}

void gui_editor_2dview_draw_dashed_vline( I32 x, I32 y0, I32 y1, CLR col ) {
  if( y0 > y1 ) {
    I32 tmp = y0;
    y0 = y1;
    y1 = tmp;
  }

  const I32 dash = 6;
  const I32 gap = 4;
  for( I32 y = y0; y <= y1; y += dash + gap ) {
    I32 ey = y + dash;
    if( ey > y1 ) ey = y1;
    gui_draw_line( x, y, x, ey, col );
  }
}

void gui_editor_2dview_draw_poly_preview( GUI_EDITOR_2DVIEW* view ) {
  if( !gui_editor_2dview_is_poly_drag_tool() || !view->poly_drag )
    return;

  VEC2 start = gui_editor_2dview_world_to_screen( view, view->poly_start );
  VEC2 end = gui_editor_2dview_world_to_screen( view, view->poly_end );

  I32 minx = (I32)( start.x < end.x ? start.x : end.x );
  I32 maxx = (I32)( start.x > end.x ? start.x : end.x );
  I32 miny = (I32)( start.y < end.y ? start.y : end.y );
  I32 maxy = (I32)( start.y > end.y ? start.y : end.y );

  CLR dash_clr = CLR::WHITE( 0.7f );
  gui_editor_2dview_draw_dashed_hline( minx, maxx, miny, dash_clr );
  gui_editor_2dview_draw_dashed_hline( minx, maxx, maxy, dash_clr );
  gui_editor_2dview_draw_dashed_vline( minx, miny, maxy, dash_clr );
  gui_editor_2dview_draw_dashed_vline( maxx, miny, maxy, dash_clr );

  EDITORVIEW_DRAG_SHAPE shape{};
  if( !gui_editor_2dview_build_drag_shape( view, &shape ) )
    return;

  CLR poly_clr = editor->tool.type == EDITOR_TOOL_POLY
    ? CLR::MAGENTA( 0.9f )
    : CLR::CYAN( 0.9f );
  for( I32 i = 0; i < shape.pointc; ++i ) {
    I32 next = ( i + 1 ) % shape.pointc;
    VEC2 p0 = gui_editor_2dview_world_to_screen( view, shape.points[i] );
    VEC2 p1 = gui_editor_2dview_world_to_screen( view, shape.points[next] );
    gui_draw_line( (I32)p0.x, (I32)p0.y, (I32)p1.x, (I32)p1.y, poly_clr );
  }
}

void gui_editor_2dview_create_poly_from_drag( GUI_EDITOR_2DVIEW* view ) {
  EDITORVIEW_DRAG_SHAPE shape{};
  if( !gui_editor_2dview_build_drag_shape( view, &shape ) )
    return;

  MAP_POLYGON newp{};
  newp.propid = 0;
  newp.type = MPT_FLOOR;
  F32 depth = gui_editor_2dview_placement_z();

  F32 invw = 1.f / shape.rect.w;
  F32 invh = 1.f / shape.rect.h;
  for( I32 i = 0; i < shape.pointc; ++i ) {
    VEC2 p = shape.points[i];
    VEC3 world = gui_editor_2dview_plane_to_world3( p, depth );

    MAP_VERTEX v{};
    v.pos = world;
    v.uv = {
      ( p.x - shape.rect.minx ) * invw,
      ( shape.rect.maxy - p.y ) * invh
    };
    v.clr = CLR::WHITE();
    newp.vertices.push( v );
  }

  map_polygon_calc_bounds( &newp );
  I32 poly_idx = editor->map->polygons.size;
  editor->map->polygons.push( newp );
  editor_undo_record_create_poly( editor, poly_idx );
  gui_editor_2dview_check_bounds_preserve_view( view );
}

void gui_editor_2dview_create_wallpoly_from_drag( GUI_EDITOR_2DVIEW* view ) {
  EDITORVIEW_DRAG_SHAPE shape{};
  if( !gui_editor_2dview_build_drag_shape( view, &shape ) )
    return;

  I32 propid = gui_editor_2dview_find_or_create_wallpoly_propid();
  I32 first_wall_idx = editor->map->walls.size;

  F32 depth = gui_editor_2dview_placement_z();
  F32 wallheight = gui_editor_2dview_wall_height();
  for( I32 i = 0; i < shape.pointc; ++i ) {
    VEC2 p0 = shape.points[i];
    VEC2 p1 = shape.points[(i + 1) % shape.pointc];
    VEC3 w0 = gui_editor_2dview_plane_to_world3( p0, depth );
    VEC3 w1 = gui_editor_2dview_plane_to_world3( p1, depth );

    MAP_WALL wall{};
    wall.start = w0;
    wall.end = w1;
    wall.end.z = wallheight;
    wall.propid = propid;
    editor->map->walls.push( wall );
  }
  I32 new_wall_count = editor->map->walls.size - first_wall_idx;
  editor_undo_record_create_walls( editor, first_wall_idx, new_wall_count );

  gui_editor_2dview_check_bounds_preserve_view( view );
}

void gui_editor_2dview_draw_gutters( GUI_EDITOR_2DVIEW* view, I32 x, I32 y ) {
  WORLD_MAP* m = editor->map;
  F32 w = view->w;
  F32 h = view->h;
  F32 min_u = m->mins.x;
  F32 max_u = m->maxs.x;
  F32 min_v = m->mins.y;
  F32 max_v = m->maxs.y;

  gui_draw_str( x + 2, y, ALIGN_L, FNT_JPN12, ui_clr.txt, "%1.f,%1.f", view->posx, view->posy );
  for( U32 i = 1; i <= 5; ++i ) {
    F32 tx = x + i * (w / 5);
    F32 ty = y;

    F32 tp = (tx - x) / w;

    F32 scaledx = min_u + ( max_u - min_u ) * tp;
    if( w > h )
      scaledx *= ( w / h );
    scaledx /= view->scale;
    scaledx += view->posx;
    gui_draw_str( tx - 2, ty, ALIGN_R, FNT_JPN12, ui_clr.txt, "%1.f", scaledx );
    gui_draw_line( tx, ty, tx + 1, ty + 16, ui_clr.txt );
  }

  for( U32 i = 1; i <= 5; ++i ) {
    F32 tx = x + 2;
    F32 ty = y + i * (h / 5);

    F32 tp = (ty - y) / h;

    F32 scaledy = min_v + ( max_v - min_v ) * tp;
    if( w < h )
      scaledy *= h / w;
    scaledy /= view->scale;
    scaledy += view->posy;
    gui_draw_str( tx, ty - 16, ALIGN_L, FNT_JPN12, ui_clr.txt, "%1.f", scaledy );
    gui_draw_line( tx - 2, ty, tx + 20, ty, ui_clr.txt );
  }
}

void gui_editor_2dview_draw_polygons( GUI_EDITOR_2DVIEW* view, I32 x, I32 y ) {
  WORLD_MAP* m = editor->map;

  m->polygons.each( fn( MAP_POLYGON* p ) {
    SURF_PROPS fallback_props{ .tex = 0, .clr = CLR::WHITE() };
    SURF_PROPS* props = polygon_get_props( m, p );
    if( !props )
      props = &fallback_props;
    if( !editor->wireframe ) {
      LIST<VERTEX> vertices;
      p->vertices.each( fn( MAP_VERTEX* v ) {
        VEC2 screen = gui_editor_2dview_world3_to_screen( view, v->pos );
        VERTEX v2;
        v2.uv = v->uv;
        v2.pos.x = screen.x;
        v2.pos.y = screen.y;
        v2.clr = props->clr;
        vertices.push( v2 );
      } );

      if( props->tex )
        gl_textured_polygon( _gui.gl2d_font, vertices.data, vertices.size, props->tex );
      else
        gl_polygon( _gui.gl2d, vertices.data, vertices.size );
    }
    else {
      for( U32 i = 0; i < p->vertices.size; ++i ) {
        MAP_VERTEX* v1 = &p->vertices[i];
        MAP_VERTEX* v2 = &p->vertices[(i+1) % p->vertices.size];
        VEC2 s0 = gui_editor_2dview_world3_to_screen( view, v1->pos );
        VEC2 s1 = gui_editor_2dview_world3_to_screen( view, v2->pos );

        I32 x0 = (I32)s0.x;
        I32 y0 = (I32)s0.y;
        I32 x1 = (I32)s1.x;
        I32 y1 = (I32)s1.y;

        gui_draw_line( x0, y0, x1, y1, props->clr );
      }
    }

    if( editor->tool.type != EDITOR_TOOL_POLY && editor->tool.type != EDITOR_TOOL_SELECT )
      return;
    // draw gizmos
    p->vertices.each( fn( MAP_VERTEX* v ) {
      VEC2 screen = gui_editor_2dview_world3_to_screen( view, v->pos );
      I32 vx = (I32)screen.x;
      I32 vy = (I32)screen.y;
      U8 selected = gui_editor_2dview_is_gizmo_active( view, v, EDITOR_SELECT_PVERTEX )
                 || gui_editor_2dview_is_gizmo_active( view, p, EDITOR_SELECT_POLY );

      gui_editor_2dview_draw_gizmo( vx, vy, CLR::CYAN(), selected );
    } );
  } );
}

void gui_editor_2dview_draw_walls( GUI_EDITOR_2DVIEW* view, I32 x, I32 y ) {
  WORLD_MAP* m = editor->map;

  m->walls.each( fn( MAP_WALL* s ) {
    SURF_PROPS fallback_props{ .tex = 0, .clr = CLR::WHITE() };
    SURF_PROPS* props = wall_get_props( m, s );
    if( !props )
      props = &fallback_props;
    VEC3 w0 = { s->start.x, s->start.y, s->start.z };
    VEC3 w1 = { s->end.x, s->end.y, s->end.z };
    VEC2 p0 = gui_editor_2dview_world3_to_screen( view, w0 );
    VEC2 p1 = gui_editor_2dview_world3_to_screen( view, w1 );

    I32 x0 = (I32)p0.x;
    I32 y0 = (I32)p0.y;
    I32 x1 = (I32)p1.x;
    I32 y1 = (I32)p1.y;

    gui_draw_line( x0, y0, x1, y1, props->clr );
    if( gui_editor_2dview_is_gizmo_active( view, s, EDITOR_SELECT_WALL ) ) {
      gui_draw_line( x0, y0 - 1, x1, y1 - 1, CLR::WHITE( .5f ) );
      gui_draw_line( x0, y0 + 1, x1, y1 + 1, CLR::WHITE( .5f ) );
      gui_draw_line( x0 - 1, y0, x1 - 1, y1, CLR::WHITE( .5f ) );
      gui_draw_line( x0 + 1, y0, x1 + 1, y1, CLR::WHITE( .5f ) );
    }
  } );

  if( editor->tool.type != EDITOR_TOOL_WALL && editor->tool.type != EDITOR_TOOL_SELECT )
    return;

  // gizmos
  m->walls.each( fn( MAP_WALL* s ) {
    VEC3 w0 = { s->start.x, s->start.y, s->start.z };
    VEC3 w1 = { s->end.x, s->end.y, s->end.z };
    VEC2 p0 = gui_editor_2dview_world3_to_screen( view, w0 );
    VEC2 p1 = gui_editor_2dview_world3_to_screen( view, w1 );
    I32 x0 = (I32)p0.x;
    I32 y0 = (I32)p0.y;
    I32 x1 = (I32)p1.x;
    I32 y1 = (I32)p1.y;

    U8 sel  = gui_editor_2dview_is_gizmo_active( view, s, EDITOR_SELECT_WALL );
    U8 sel1 = gui_editor_2dview_is_gizmo_active( view, &s->start, EDITOR_SELECT_WVERTEX );
    U8 sel2 = gui_editor_2dview_is_gizmo_active( view, &s->end, EDITOR_SELECT_WVERTEX );

    gui_editor_2dview_draw_gizmo( x0, y0, CLR::GREEN(), sel || sel1 );
    gui_editor_2dview_draw_gizmo( x1, y1, CLR::GREEN(), sel || sel2 );
  } );
}

void gui_editor_2dview_draw_sprites( GUI_EDITOR_2DVIEW* view, I32 x, I32 y ) {
  WORLD_MAP* m = editor->map;

  m->sprites.each( fn( MAP_SPRITE* s ) {
    F32 wantedsize = editor->spritesize;
    F32 aspect = s->size.x / s->size.y;
    F32 w = (aspect > 1.0f)? wantedsize : wantedsize * aspect;
    F32 h = (aspect < 1.0f)? wantedsize : wantedsize / aspect;
    VEC2 pos = gui_editor_2dview_world3_to_screen( view, s->pos );

    U8 sel= gui_editor_2dview_is_gizmo_active( view, s, EDITOR_SELECT_SPRITE );
    if( sel ) {
      gui_draw_rect( pos.x - w/2 - 1, pos.y - h/2 - 1, w + 2, h + 2, CLR::WHITE( 0.5f ) );
    }

    gl_2d_textured_frect(
      _gui.gl2d_font,
      { (F32)((I32)pos.x - w/2), (F32)((I32)pos.y - h/2) },
      { (F32)w, (F32)h },
      s->tex,
      s->clr
    );
  } );
}

void gui_editor_2dview_draw_player( GUI_EDITOR_2DVIEW* view, I32 x, I32 y ) {

  if( !objl->pl )
    return;

  VEC3 pos = objl->pl->pos;
  VEC2 pos2d = gui_editor_2dview_world3_to_screen( view, pos );

  F32 yaw = objl->pl->rot.y;
  VEC2 dir = m_radial_offset( yaw, 20.f );
  VEC2 ray = pos2d + dir;
  gui_draw_line( (I32)pos2d.x, (I32)pos2d.y, (I32)ray.x, (I32)ray.y, CLR::GREEN() );

  gui_draw_frect( (I32)pos2d.x-4, (I32)pos2d.y-4, 8, 8, CLR::WHITE() );
  gui_draw_frect( (I32)pos2d.x-3, (I32)pos2d.y-3, 6, 6, CLR::RED() );
}

void gui_editor_2dview_draw_origin( GUI_EDITOR_2DVIEW* view, I32 x, I32 y ) {
  WORLD_MAP* m = editor->map;

  VEC3 pos = m->startpos;
  VEC2 screen = gui_editor_2dview_world3_to_screen( view, pos );
  I32 screenx = (I32)screen.x;
  I32 screeny = (I32)screen.y;

  if( gui_editor_2dview_is_gizmo_active( view, m, EDITOR_SELECT_ORIGIN ) ) {
    I32 w, h;
    gui_draw_get_str_bounds( &w, &h, FNT_JPN12, "(origin)" );
    gui_draw_frect( screenx - w / 2 - 1, screeny + h - 1, w, 1, CLR::BLACK() );
    gui_draw_frect( screenx - w / 2, screeny + h, w, 1, ui_clr.txt );
    gui_draw_str( screenx - 1, screeny - 1, ALIGN_C, FNT_JPN12, CLR::WHITE( .5f ), "(origin)" );
  }

  gui_draw_str( screenx + 1, screeny + 1, ALIGN_C, FNT_JPN12, CLR::BLACK(), "(origin)" );
  gui_draw_str( screenx, screeny, ALIGN_C, FNT_JPN12, ui_clr.txt, "(origin)" );
}

void gui_editor_2dview_draw_fn( void* ptr ) {
  GUI_EDITOR_2DVIEW* view = (GUI_EDITOR_2DVIEW*)ptr;
  if( !editor->map )
    return;

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

  gui_draw_str( x, y, ALIGN_L, FNT_JPN12, ui_clr.txt, "2d view" );
  y += EDITORVIEW_TITLE_OFFSET;

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

  gui_editor_2dview_draw_gutters( view, x, y );

  U32 offx = EDITORVIEW_GUTTERS_OFFSETX;
  U32 offy = EDITORVIEW_GUTTERS_OFFSETY;

  gui_draw_push_clip( x + offx, y + offy, w - offx, h - offy - EDITORVIEW_TOOLBAR_OFFSET );
  gui_editor_2dview_draw_polygons( view, x + offx, y + offy );
  gui_editor_2dview_draw_walls( view, x + offx, y + offy );
  gui_editor_2dview_draw_sprites( view, x + offx, y + offy );
  gui_editor_2dview_draw_player( view, x + offx, y + offy );
  gui_editor_2dview_draw_origin( view, x + offx, y + offy );
  gui_editor_2dview_draw_poly_preview( view );
  gui_draw_pop_clip();

  gui_draw_push_clip( x, y + 16, view->w, view->h );
  view->children.each( fn( GUI_BASE** childptr ) {
    GUI_BASE* child = *childptr;
    if( !child->enabled ) return;

    if( child->draw_fn ) child->draw_fn( child );
    else dlog( "gui_view_draw_fn(): child %p no draw_fn\n", child );
  } );
  gui_draw_pop_clip();
}

U8 gui_editor_2dview_input_drag( GUI_EDITOR_2DVIEW* view, U8 mouse ) {
  if( !mouse ) {
    view->dragheld = 0;
    return 0;
  }

  I32 mx, my;
  gui_cursor_pos( &mx, &my );

  I32 x = gui_relx( view );
  I32 y = gui_rely( view ) + EDITORVIEW_TITLE_OFFSET;
  I32 w = view->w - EDITORVIEW_GUTTERS_OFFSETX;
  I32 h = view->h - EDITORVIEW_GUTTERS_OFFSETY;
  x += EDITORVIEW_GUTTERS_OFFSETX;
  y += EDITORVIEW_GUTTERS_OFFSETY;

  U8 inbound = mx >= x && mx <= x + w && my >= y && my <= y + h;
  if( !inbound && !view->dragheld )
    return 0;

  I32 moffx = mx - x;
  I32 moffy = my - y;

  if( !view->dragheld ) {
    view->moffx = moffx;
    view->moffy = moffy;
    view->voffx = view->posx;
    view->voffy = view->posy;
    view->dragheld = 1;
    return 1;
  }

  F32 iscale = 1.f / gui_editor_2dview_calc_scale( view );

  view->posx = view->voffx - (moffx - view->moffx) * iscale;
  view->posy = view->voffy - (moffy - view->moffy) * iscale;
  return 1;
}

void gui_editor_2dview_input_tool_none( GUI_EDITOR_2DVIEW* view ) {
  U8 mouse = gui_mbutton_down( 0 ) || gui_mbutton_down( 1 );

  gui_editor_2dview_input_drag( view, mouse );
}

VEC2 gui_editor_2dview_input_get_drag_vec( GUI_EDITOR_2DVIEW* view ) {
  I32 mx, my;
  gui_cursor_pos( &mx, &my );

  F32 iscale = 1.f / gui_editor_2dview_calc_scale( view );
  F32 dx = (mx - view->oldmx) * iscale + view->mremainx;
  F32 dy = (my - view->oldmy) * iscale + view->mremainy;

  F32 igridx = copysignf( floorf( fabsf( dx / editor->grid ) ), dx );
  F32 igridy = copysignf( floorf( fabsf( dy / editor->grid ) ), dy );

  return { editor->grid * igridx, editor->grid * igridy };
}

void gui_editor_2dview_input_select_onmove( GUI_EDITOR_2DVIEW* view ) {
  I32 mx, my;
  gui_cursor_pos( &mx, &my );

  F32 iscale = 1.f / gui_editor_2dview_calc_scale( view );
  F32 dx = (mx - view->oldmx) * iscale + view->mremainx;
  F32 dy = (my - view->oldmy) * iscale + view->mremainy;

  F32 rmx = copysignf( fmodf( fabsf( dx ), editor->grid ), dx );
  F32 rmy = copysignf( fmodf( fabsf( dy ), editor->grid ), dy );

  view->oldmx = mx;
  view->oldmy = my;
  view->mremainx = rmx;
  view->mremainy = rmy;
  view->dragmoved = 1;

  GUI_EDITOR_PROPVIEW* props = editor->gui.props;
  // special case for dragging wall vertices, just always update
  U8 iswallv = props->seltype == EDITOR_SELECT_WALL && view->dragtype == EDITOR_SELECT_WVERTEX;
  if( props->curselect == view->curdrag || iswallv )
    gui_editor_propview_update( editor->gui.props );
}

void gui_editor_2dview_input_select_drag_wall( GUI_EDITOR_2DVIEW* view ) {
  MAP_WALL* s = (MAP_WALL*)view->curdrag;

  gui_editor_2dview_snap_plane( &s->start );
  gui_editor_2dview_snap_plane( &s->end );

  VEC2 mv = gui_editor_2dview_input_get_drag_vec( view );
  if( !is_zero( mv ) ) {
    gui_editor_2dview_add_plane_delta( &s->start, mv );
    gui_editor_2dview_add_plane_delta( &s->end, mv );
    map_check_bounds( editor->map );

    gui_editor_2dview_input_select_onmove( view );
  }
}

void gui_editor_2dview_input_select_drag_vertex( GUI_EDITOR_2DVIEW* view ) {
  if( view->dragtype == EDITOR_SELECT_WVERTEX ) {
    VEC3* v = (VEC3*)view->curdrag;
    gui_editor_2dview_snap_plane( v );

    VEC2 mv = gui_editor_2dview_input_get_drag_vec( view );
    if( !is_zero( mv ) ) {
      gui_editor_2dview_add_plane_delta( v, mv );
      map_check_bounds( editor->map );
      gui_editor_2dview_input_select_onmove( view );
    }
    return;
  }

  MAP_VERTEX* v = (MAP_VERTEX*)view->curdrag;
  gui_editor_2dview_snap_plane( &v->pos );

  VEC2 mv = gui_editor_2dview_input_get_drag_vec( view );
  if( !is_zero( mv ) ) {
    gui_editor_2dview_add_plane_delta( &v->pos, mv );
    map_check_bounds( editor->map );

    gui_editor_2dview_input_select_onmove( view );
  }
}

void gui_editor_2dview_input_select_drag_polygon( GUI_EDITOR_2DVIEW* view ) {
  MAP_POLYGON* p = (MAP_POLYGON*)view->curdrag;

  VEC2 mv = gui_editor_2dview_input_get_drag_vec( view );
  if( !is_zero( mv ) ) {
    p->vertices.each( fn( MAP_VERTEX* v ) {
      gui_editor_2dview_snap_plane( &v->pos );
      gui_editor_2dview_add_plane_delta( &v->pos, mv );
    } );

    map_polygon_calc_bounds( p );
    map_check_bounds( editor->map );
    gui_editor_2dview_input_select_onmove( view );
  }
}

void gui_editor_2dview_input_select_drag_sprite( GUI_EDITOR_2DVIEW* view ) {
  MAP_SPRITE* s = (MAP_SPRITE*)view->curdrag;

  gui_editor_2dview_snap_plane( &s->pos );

  VEC2 mv = gui_editor_2dview_input_get_drag_vec( view );
  if( !is_zero( mv ) ) {
    gui_editor_2dview_add_plane_delta( &s->pos, mv );
    gui_editor_2dview_input_select_onmove( view );
  }
}

void gui_editor_2dview_input_select_drag_origin( GUI_EDITOR_2DVIEW* view ) {
  WORLD_MAP* m = editor->map;

  gui_editor_2dview_snap_plane( &m->startpos );

  VEC2 mv = gui_editor_2dview_input_get_drag_vec( view );
  if( !is_zero( mv ) ) {
    gui_editor_2dview_add_plane_delta( &m->startpos, mv );
    gui_editor_2dview_input_select_onmove( view );
  }
}

void gui_editor_2dview_input_select_ondrop( GUI_EDITOR_2DVIEW* view ) {
  if( !view->dragmoved )
    gui_editor_propview_select( editor->gui.props, view->curdrag, view->dragtype );

  view->curdrag = 0;
}

void gui_editor_2dview_input_select_drag( GUI_EDITOR_2DVIEW* view ) {
  U8 m1 = gui_mbutton_down( 0 );
  I32 mx, my;
  gui_cursor_pos( &mx, &my );

  if( !m1 ) {
    if( view->held ) {
      gui_editor_2dview_input_select_ondrop( view );
      view->held = 0;
    }
    return;
  }

  if( !view->held ) {
    view->curdrag = view->curselect;
    view->dragtype = view->seltype;
    view->oldmx = mx;
    view->oldmy = my;
    view->mremainx = view->mremainy = 0;
    view->dragmoved = 0;
    view->held = 1;
    return;
  }

  switch( view->dragtype ) {
    case EDITOR_SELECT_WALL: gui_editor_2dview_input_select_drag_wall( view ); break;
    case EDITOR_SELECT_POLY: gui_editor_2dview_input_select_drag_polygon( view ); break;
    case EDITOR_SELECT_SPRITE: gui_editor_2dview_input_select_drag_sprite( view ); break;
    case EDITOR_SELECT_ORIGIN: gui_editor_2dview_input_select_drag_origin( view ); break;
    case EDITOR_SELECT_WVERTEX:
    case EDITOR_SELECT_PVERTEX:
      gui_editor_2dview_input_select_drag_vertex( view ); break;
    default: {
      view->oldmx = mx;
      view->oldmy = my;
    } break;
  }
}

void gui_editor_2dview_input_select_walls( GUI_EDITOR_2DVIEW* view ) {
  WORLD_MAP* m = editor->map;

  I32 mx, my;
  gui_cursor_pos( &mx, &my );

  VEC2 mpos = { (F32)mx, (F32)my };

  F32 mindist = 5.0001f;
  for( U32 i = 0; i < m->walls.size; ++i ) {
    MAP_WALL* w = &m->walls[i];

    VEC3 wp0 = { w->start.x, w->start.y, w->start.z };
    VEC3 wp1 = { w->end.x, w->end.y, w->end.z };
    VEC2 w1 = gui_editor_2dview_world3_to_screen( view, wp0 );
    VEC2 w2 = gui_editor_2dview_world3_to_screen( view, wp1 );

    F32 d1 = vec_dist( mpos, w1 );
    F32 d2 = vec_dist( mpos, w2 );
    if( d1 < mindist && d1 < d2 ) {
      gui_editor_2dview_select( view, &w->start, EDITOR_SELECT_WVERTEX );
      mindist = d1;
      continue;
    }
    else if( d2 < mindist ) {
      gui_editor_2dview_select( view, &w->end, EDITOR_SELECT_WVERTEX );
      mindist = d2;
      continue;
    }
    // give priority to vertices
    if( view->seltype == EDITOR_SELECT_WVERTEX )
      continue;

    F32 dist = m_dist_line_to_point( w1, w2, mpos );
    if( dist < 3.f && dist < mindist ) {
      gui_editor_2dview_select( view, w, EDITOR_SELECT_WALL );
      mindist = dist;
    }
  }

  if( !view->curselect && !view->held )
    return;

  gui_editor_2dview_input_select_drag( view );
}

void gui_editor_2dview_input_select_polygons( GUI_EDITOR_2DVIEW* view ) {
  WORLD_MAP* m = editor->map;

  I32 mx, my;
  gui_cursor_pos( &mx, &my );

  VEC2 mpos = { (F32)mx, (F32)my };

  F32 mindist = 5.0001f;
  for( U32 i = 0; i < m->polygons.size; ++i ) {
    MAP_POLYGON* p = &m->polygons[i];
    LIST<VEC2> plist;

    p->vertices.each( fn( MAP_VERTEX* v ) {
      VEC2 screen = gui_editor_2dview_world3_to_screen( view, v->pos );
      F32 d = vec_dist( mpos, screen );
      if( d < mindist ) {
        gui_editor_2dview_select( view, v, EDITOR_SELECT_PVERTEX );
        mindist = d;
      }

      plist.push( screen );
    } );

    if( view->seltype == EDITOR_SELECT_PVERTEX )
      continue;

    if( m_point_in_polygon( mpos, plist.data, plist.size ) )
      gui_editor_2dview_select( view, p, EDITOR_SELECT_POLY );
  }

  if( !view->curselect && !view->held )
    return;

  gui_editor_2dview_input_select_drag( view );
}

void gui_editor_2dview_input_select_sprites( GUI_EDITOR_2DVIEW* view ) {
  WORLD_MAP* m = editor->map;

  I32 mx, my;
  gui_cursor_pos( &mx, &my );

  VEC2 mpos = { (F32)mx, (F32)my };

  F32 hsize = editor->spritesize * .5f;
  F32 mindist = editor->spritesize;
  for( U32 i = 0; i < m->sprites.size; ++i ) {
    MAP_SPRITE* s = &m->sprites[i];

    VEC2 screen = gui_editor_2dview_world3_to_screen( view, s->pos );

    if( mpos.x >= screen.x - hsize && mpos.x <= screen.x + hsize
     && mpos.y >= screen.y - hsize && mpos.y <= screen.y + hsize )
    {
      F32 dist = vec_dist( mpos, screen );
      if( dist < mindist ) {
        mindist = dist;
        gui_editor_2dview_select( view, s, EDITOR_SELECT_SPRITE );
      }
    }
  }

  if( !view->curselect && !view->held )
    return;

  gui_editor_2dview_input_select_drag( view );
}

void gui_editor_2dview_input_select_origin( GUI_EDITOR_2DVIEW* view ) {
  WORLD_MAP* m = editor->map;

  I32 mx, my;
  gui_cursor_pos( &mx, &my );

  VEC2 mpos = { (F32)mx, (F32)my };
  VEC2 screen = gui_editor_2dview_world3_to_screen( view, m->startpos );

  I32 w, h;
  gui_draw_get_str_bounds( &w, &h, FNT_JPN12, "(origin)" );

  if( fabsf( (F32)mx - screen.x ) < (w * .5f + 2.f)
   && fabsf( (F32)my - h * .5f - screen.y ) < (h * .5f + 2.f) ) {
    gui_editor_2dview_select( view, m, EDITOR_SELECT_ORIGIN );
  }

  if( !view->curselect && !view->held )
    return;

  gui_editor_2dview_input_select_drag( view );
}

void gui_editor_2dview_input_tool_select( GUI_EDITOR_2DVIEW* view ) {
  U8 m1 = gui_mbutton_down( 0 );
  U8 m2 = !m1 && gui_mbutton_down( 1 );

  if( gui_editor_2dview_input_drag( view, m2 ) ) {
    return;
  }

  view->curselect = 0;
  view->seltype = EDITOR_SELECT_NONE;
  gui_editor_2dview_input_select_origin( view );
  if( view->curselect ) return;
  gui_editor_2dview_input_select_sprites( view );
  if( view->curselect ) return;
  gui_editor_2dview_input_select_walls( view );
  if( view->curselect ) return;
  gui_editor_2dview_input_select_polygons( view );
}

void gui_editor_2dview_input_scroll( GUI_EDITOR_2DVIEW* view ) {
  U8 scroll = gui_mbutton_down( GUI_MBTNSCROLL );
  gui_capture_scroll();

  F32 zoom_factor = 1.f;
  if( scroll == (U8)-1 && view->scale > 0.5f ) {
    zoom_factor = 0.75f;
  }
  else if( scroll == 1 && view->scale < 16.f ) {
    zoom_factor = 1.25f;
  }

  if( zoom_factor == 1.f )
    return;

  I32 mx, my;
  gui_cursor_pos( &mx, &my );
  VEC2 world_before = gui_editor_2dview_screen_to_world( view, mx, my );

  view->scale *= zoom_factor;

  VEC2 world_after = gui_editor_2dview_screen_to_world( view, mx, my );
  view->posx += world_before.x - world_after.x;
  view->posy += world_before.y - world_after.y;

  view->dragheld = 0;
}

void gui_editor_2dview_input_tool_wall( GUI_EDITOR_2DVIEW* view ) {
  if( editor->tool.wallshape == EDITOR_WALLSHAPE_POLYGON ) {
    U8 m1 = gui_mbutton_down( 0 );
    I32 mx, my;
    gui_cursor_pos( &mx, &my );
    VEC2 world = gui_editor_2dview_screen_to_world( view, mx, my );

    if( !m1 ) {
      if( view->poly_drag ) {
        view->poly_end = world;
        gui_editor_2dview_create_wallpoly_from_drag( view );
      }

      view->poly_drag = 0;
      return;
    }

    if( !view->poly_drag ) {
      view->poly_drag = 1;
      view->poly_start = world;
      view->poly_end = world;
      return;
    }

    view->poly_end = world;
    return;
  }

  U8 m1 = gui_mbutton_down( 0 );
  if( !m1 ) {
    if( view->held ) {
      if( view->pending_wall_undo_idx >= 0 )
        editor_undo_record_create_walls( editor, view->pending_wall_undo_idx, 1 );
      gui_editor_2dview_check_bounds_preserve_view( view );
    }
    view->pending_wall_undo_idx = -1;
    view->held = 0;
    view->curdrag = 0;
    return;
  }

  I32 mx, my;
  gui_cursor_pos( &mx, &my );

  VEC2 world = gui_editor_2dview_screen_to_world( view, mx, my );
  if( !view->held ) {
    F32 depth = gui_editor_2dview_placement_z();
    VEC3 wp = gui_editor_2dview_plane_to_world3( world, depth );
    MAP_WALL neww;
    neww.start = neww.end = {
      wp.x,
      wp.y,
      wp.z
    };
    gui_editor_2dview_snap_plane( &neww.start );
    gui_editor_2dview_snap_plane( &neww.end );
    neww.end.z = gui_editor_2dview_wall_height();
    neww.propid = 0;

    I32 idx = editor->map->walls.size;
    editor->map->walls.push( neww );
    view->pending_wall_undo_idx = idx;
    view->curdrag = &editor->map->walls[idx].end;
    view->held = 1;

    view->oldmx = mx;
    view->oldmy = my;
    view->mremainx = 0.0;
    view->mremainy = 0.0;
    return;
  }

  VEC3* end = (VEC3*)view->curdrag;
  if( end ) {
    gui_editor_2dview_set_plane( end, world );
    gui_editor_2dview_snap_plane( end );
  }
}

void gui_editor_2dview_input_tool_poly( GUI_EDITOR_2DVIEW* view ) {
  U8 m1 = gui_mbutton_down( 0 );
  I32 mx, my;
  gui_cursor_pos( &mx, &my );
  VEC2 world = gui_editor_2dview_screen_to_world( view, mx, my );

  if( !m1 ) {
    if( view->poly_drag ) {
      view->poly_end = world;
      gui_editor_2dview_create_poly_from_drag( view );
    }

    view->poly_drag = 0;
    return;
  }

  if( !view->poly_drag ) {
    view->poly_drag = 1;
    view->poly_start = world;
    view->poly_end = world;
    return;
  }

  view->poly_end = world;
}

void gui_editor_2dview_input_tool_ent( GUI_EDITOR_2DVIEW* view ) {
  
}

void gui_editor_2dview_input_tool_sprite( GUI_EDITOR_2DVIEW* view ) {
  U8 m1 = gui_mbutton_down( 0 );
  if( !m1 ) {
    view->held = 0;
    return;
  }

  I32 mx, my;
  gui_cursor_pos( &mx, &my );

  VEC2 world = gui_editor_2dview_screen_to_world( view, mx, my );
  if( !view->held ) {
    VEC3 wpos = gui_editor_2dview_plane_to_world3( world, gui_editor_2dview_placement_z() );
    MAP_SPRITE news;
    news.pos = wpos;
    gui_editor_2dview_snap_plane( &news.pos );
    news.size = { 20.f, 20.f };
    news.clr = { 1.f, 1.f, 1.f, 1.f };
    news.tex = 0;

    I32 idx = editor->map->sprites.size;
    editor->map->sprites.push( news );
    view->curdrag = &editor->map->sprites[idx];
    view->held = 1;

    view->oldmx = mx;
    view->oldmy = my;
    view->mremainx = 0.0;
    view->mremainy = 0.0;
    return;
  }

  gui_editor_2dview_input_select_drag_sprite( view );
}


void gui_editor_2dview_input_tool_draw( GUI_EDITOR_2DVIEW* view ) {
  U8 m1 = gui_mbutton_down( 0 );
  U8 m2 = !m1 && gui_mbutton_down( 1 );

  if( gui_editor_2dview_input_drag( view, m2 ) ) {
    return;
  }

  switch( editor->tool.type ) {
  case EDITOR_TOOL_WALL: return gui_editor_2dview_input_tool_wall( view );
  case EDITOR_TOOL_POLY: return gui_editor_2dview_input_tool_poly( view );
  case EDITOR_TOOL_ENT: return gui_editor_2dview_input_tool_ent( view );
  case EDITOR_TOOL_SPRITE: return gui_editor_2dview_input_tool_sprite( view );
  default: return;
  }
}

void gui_editor_view2d_delete_obj( GUI_EDITOR_2DVIEW* view ) {
  void* it;
  U8 type;

  if( view->curdrag && view->dragtype ) {
    it = view->curdrag;
    type = view->dragtype;
    view->curdrag = 0;
    view->dragtype = EDITOR_SELECT_NONE;
  } else if( editor->gui.props->curselect ) {
    it = editor->gui.props->curselect;
    type = editor->gui.props->seltype;
    view->curselect = 0;
    view->seltype = EDITOR_SELECT_NONE;
  }
  else return;

  U8 cleared = 1;
  switch( type ) {
    case EDITOR_SELECT_POLY: {
      I32 idx = editor->map->polygons.idx_of( (MAP_POLYGON*)it );
      if( idx != -1 )
        editor->map->polygons.erase( idx );
    } break;
    case EDITOR_SELECT_WALL: {
      I32 idx = editor->map->walls.idx_of( (MAP_WALL*)it );
      if( idx != -1 )
        editor->map->walls.erase( idx );
    }; break;
    case EDITOR_SELECT_SPRITE: {
      I32 idx = editor->map->sprites.idx_of( (MAP_SPRITE*)it );
      if( idx != -1 )
        editor->map->sprites.erase( idx );
    }; break;
    case EDITOR_SELECT_PVERTEX: {
      I32 vidx = -1, idx = editor->map->polygons.idx_where( fn( MAP_POLYGON* p ) {
        vidx = p->vertices.idx_where( fn( MAP_VERTEX* v ) {
          return v == it;
        } );
        return vidx != -1;
      } );

      if( idx != -1 && vidx != -1 ) {
        MAP_POLYGON* p = &editor->map->polygons[idx];
        if( p->vertices.size <= 3 ) {
          editor->map->polygons.erase( idx );
          break;
        }
        editor->map->polygons[idx].vertices.erase( vidx );
      }
    }; break;
    case EDITOR_SELECT_WVERTEX: {
      I32 idx = editor->map->walls.idx_where( fn( MAP_WALL* w ) {
        return &w->end == it || &w->start == it;
      } );

      if( idx != -1 )
        editor->map->walls.erase( idx );
    }; break;
    default:
      cleared = 0; break;
  }

  if( cleared && it == editor->gui.props->curselect ) {
    gui_editor_propview_select( editor->gui.props, 0, 0 );
  }
}

void gui_editor_2dview_key_input( GUI_EDITOR_2DVIEW* view ) {
  static U8 del_held = 0;
  static U8 undo_held = 0;
  static U8 redo_held = 0;
  if( kb_down( SDLK_DELETE ) && !input.mouselock ) {
    if( !del_held )
      gui_editor_view2d_delete_obj( view );

    del_held = 1;
  } else del_held = 0;

  U8 ctrl = kb_down( SDLK_LCTRL ) || kb_down( SDLK_RCTRL );
  U8 z = kb_down( SDLK_z );
  if( ctrl && z && !input.mouselock ) {
    if( !undo_held )
      editor_undo( editor );

    undo_held = 1;
  } else undo_held = 0;

  U8 r = kb_down( SDLK_r );
  if( ctrl && r && !input.mouselock ) {
    if( !redo_held )
      editor_redo( editor );

    redo_held = 1;
  } else redo_held = 0;
}

void gui_editor_2dview_input_fn( void* ptr ) {
  GUI_EDITOR_2DVIEW* view = (GUI_EDITOR_2DVIEW*)ptr;
  if( !editor->map )
    return;

  gui_editor_2dview_key_input( view );

  I32 x = gui_relx( view );
  I32 y = gui_rely( view ) + EDITORVIEW_TITLE_OFFSET;
  I32 w = view->w;
  I32 h = view->h;

  U8  mouse = gui_mbutton_down( 0 ) || gui_mbutton_down( 1 );
  I32 mx, my;
  gui_cursor_pos( &mx, &my );

  U8 inbounds = mx >= x && mx <= x + w && my >= y && my <= y + h;
  if( inbounds )
    gui_editor_2dview_input_scroll( view );

  if( !mouse )
    view->heldoutbounds = 0;
  else if( mouse && !inbounds )
    view->heldoutbounds = 1;

  if( view->heldoutbounds )
    return;

  gui_base_input_fn( view );
  if( my >= y + h - 18 )
    return;

  if( !gui_editor_2dview_is_poly_drag_tool() ) {
    view->poly_drag = 0;
  }

  switch( editor->tool.type ) {
    case EDITOR_TOOL_SELECT: return gui_editor_2dview_input_tool_select( view );
    case EDITOR_TOOL_WALL:
    case EDITOR_TOOL_POLY:
    case EDITOR_TOOL_SPRITE:
    case EDITOR_TOOL_ENT:
      return gui_editor_2dview_input_tool_draw( view );
    default: return gui_editor_2dview_input_tool_none( view );
  }
}

static U8 GRID_DEP_LABEL_TAG;
static U8 GRID_DEP_PROPVIEW_TAG;
static U8 GRID_DEP_TOOLVIEW_TAG;

void update_view_settings( GAME_EDITOR* e ) {
  GAME_EDITOR::EDITOR_GUI* egui = &e->gui;
  if( !egui->gridlabel )
    return;

  sprintf( egui->gridlabel->name, "grid: %1.2f", e->grid );
}

void editor_grid_dependency_update_label( GAME_EDITOR* e ) {
  update_view_settings( e );
}

void editor_grid_dependency_update_propview( GAME_EDITOR* e ) {
  if( e->propgrid && e->gui.props )
    editor_update_properties_column( e );
}

void editor_grid_dependency_update_toolview( GAME_EDITOR* e ) {
  if( e->gui.tool )
    gui_editor_toolview_update( e->gui.tool );
}

void grid_increment_cb( void* ) {
  if( editor->grid < 16.f ) editor->grid *= 2.f;
  editor_notify_grid_change( editor );
}

void grid_decrement_cb( void* ) {
  if( editor->grid > 0.25f ) editor->grid *= 0.5f;
  editor_notify_grid_change( editor );
}

void grid_propgrid_cb( void* ) {
  editor_notify_grid_change( editor );
}

void gui_editor_2dview_create_toolbar( GUI_EDITOR_2DVIEW* view ) {
  GAME_EDITOR* e = editor;
  I32 x = 150, y = view->h - 4;

  editor->gui.gridlabel = gui_label( x, y + 1, "grid: %1.2f", e->grid );
  editor_register_grid_dependency( e, &GRID_DEP_LABEL_TAG, editor_grid_dependency_update_label );
  editor_register_grid_dependency( e, &GRID_DEP_PROPVIEW_TAG, editor_grid_dependency_update_propview );
  editor_register_grid_dependency( e, &GRID_DEP_TOOLVIEW_TAG, editor_grid_dependency_update_toolview );
  x += 70;
  gui_button( x, y, 18, 18, "+", grid_increment_cb );
  gui_button( x + 23, y, 18, 18, "-", grid_decrement_cb );
  x += 50;
  GUI_CHECKBOX* check = gui_checkbox( x, y, "properties grid", &e->propgrid );
  check->cb = grid_propgrid_cb;
  x += 120;
  gui_checkbox( x, y, "wireframe", &e->wireframe );

  editor_notify_grid_change( e );
}

GUI_EDITOR_2DVIEW* gui_editor_2dview( I32 x, I32 y, I32 w, I32 h ) {
  GUI_EDITOR_2DVIEW* view = new GUI_EDITOR_2DVIEW();
  view->x = x;
  view->y = y;
  view->xbound = view->w = w;
  view->h = h;
  view->ybound = h + EDITORVIEW_TITLE_OFFSET;

  view->draw_fn = gui_editor_2dview_draw_fn;
  view->input_fn = gui_editor_2dview_input_fn;
  strcpy( view->name, "EDITOR_2D_VIEW" );

  view->scale = 1.f;
  view->poly_drag = 0;
  view->poly_start = { 0.f, 0.f };
  view->poly_end = { 0.f, 0.f };
  view->pending_wall_undo_idx = -1;

  GUI_BASE* parent = gui_get_view();
  if( !parent )
    parent = gui_get_window();

  parent->children.push( view );
  view->parent = parent;

  gui_set_view( view );
  gui_editor_2dview_create_toolbar( view );
  gui_set_view( (GUI_VIEW*)parent );

  return view;
}