-
Notifications
You must be signed in to change notification settings - Fork 0
/
Editor.h
1151 lines (947 loc) · 32.8 KB
/
Editor.h
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
#ifndef RR_EDITOR
#define RR_EDITOR
enum PickerSelectionTag
{
PickerSelecting_Nothing,
PickerSelecting_Slider,
PickerSelecting_ColorPicker,
PickerSelecting_Header,
};
// :ColorPicker
struct ColorPicker
{
v2 pos = {0.5f, 0.5f};
f32 width = 0.4f;
f32 height = 0.4f;
f32 border = 0.005f;
v2 headerPos = {0.5f, 0.49f}; // = pickerPos - V2(0.0f, pickerBorder + pickerHeaderSize)
f32 headerSize = 0.005f;
v2 sliderPos = {0.5f, 0.905f}; // = pickerPos + V2(0.0f, pickerBorder + pickerHeight);
f32 sliderHeight = 0.05f;
f32 sliderSelectedColorPos = 0.0f;
u32 sliderColor = 0xFFFF0000;
b32 selecting = 0;
v2 killRectPos = {0.895f, 0.49f}; // V2(headerPos.x + picker->width - picker->headerSize, headerPos.y);
v2 pickerPos = {0.0f, 0.0f}; // relative to the Color picker. (0, 0) is pure white
v4 *pickedColor;
b32 isTweeker;
Tweeker *tweeker; // for now, rethink this
};
DefineDynamicArray(ColorPicker);
// todo: i don't think it matters here, but I really have to get these color things straight.
static ColorPicker CreateColorPicker(v4 *initialColor)
{
// WARINIG : the factors DO NOT commute with the interpolation, do they?
// calculating bar pos and picker pos : colors in the bar are of the form 0xFF00abFF or 0xFFabFF00, or something like that (2F, 20, 2 something)
ColorPicker ret;
ret.isTweeker = false;
Tweekable(f32, pickerBorder);
Tweekable(f32, pickerWidth);
Tweekable(f32, pickerHeight);
Tweekable(f32, pickerSliderHeight);
Tweekable(f32, pickerHeaderSize);
ret.border = pickerBorder;
ret.width = pickerWidth;
ret.height = pickerHeight;
ret.sliderHeight = pickerSliderHeight;
ret.headerSize = pickerHeaderSize;
ret.headerPos = ret.pos - V2(0.0f, ret.headerSize + ret.border);
ret.sliderPos = ret.pos + V2(0.0f, ret.border + ret.height);
ret.killRectPos = V2(ret.headerPos.x + ret.width - ret.headerSize, ret.headerPos.y);
ret.pickedColor = initialColor;
u32 c = Pack4x8(*initialColor);
//u32 a = (c >> 24) & 0xFF;
u32 b = (c >> 16) & 0xFF;
u32 g = (c >> 8) & 0xFF;
u32 r = (c >> 0) & 0xFF;
u32 max = Max(Max(g, b), r);
if (max == 0)
{
ret.pickerPos = V2(ret.width, ret.height);
ret.sliderColor = (0xFF << 24) | (0xFF << 16) | (0u << 8) | 0u;
ret.sliderSelectedColorPos = 0.0f;
return ret;
}
f32 yfac = (f32)(0xFF - max) / 255.0f;
b *= 0xFF;
g *= 0xFF;
r *= 0xFF;
b /= max;
g /= max;
r /= max;
u32 min = Min(Min(g, b), r);
u32 addedInX = min;
f32 xfac = (f32)addedInX / 255.0f;
if (min == 0xFF)
{
ret.pickerPos = V2(0, 0);
ret.sliderColor = (0xFF << 24) | (0xFF << 16) | (0u << 8) | 0u;
ret.sliderSelectedColorPos = 0.0f;
return ret;
}
//work in u32, for percision
u32 bs = (b - addedInX) * 0xFF / (0xFF - addedInX); // added in X = 0xFF?
u32 rs = (r - addedInX) * 0xFF / (0xFF - addedInX);
u32 gs = (g - addedInX) * 0xFF / (0xFF - addedInX);
ret.pickerPos = V2( (1.0f - xfac) * ret.width, yfac * ret.height);
ret.sliderColor = (0xFF << 24) | (bs << 16) | (gs << 8) | rs;
f32 scale = 1.0f / 6.0f;
f32 scaledWidth = scale * ret.width;
if (bs == 0xFF)
{
if (rs > 0) // purple to blue
{
f32 factor = (f32)rs / 255.0f;
ret.sliderSelectedColorPos = (5.0f + factor) * scaledWidth;
}
else // blue to cyan
{
f32 factor = (f32)gs / 255.0f;
ret.sliderSelectedColorPos = (0.0f + factor) * scaledWidth;
}
}
else if(gs == 0xFF)
{
if (bs > 0) // cyan to green
{
f32 factor = (f32)bs / 255.0f;
ret.sliderSelectedColorPos = (1.0f + factor) * scaledWidth;
}
else // green to yellow
{
f32 factor = (f32)rs / 255.0f;
ret.sliderSelectedColorPos = (2.0f + factor) * scaledWidth;
}
}
else if(rs == 0xFF)
{
if (gs > 0) // yellow to red
{
f32 factor = (f32)gs / 255.0f;
ret.sliderSelectedColorPos = (3.0f + factor) * scaledWidth;
}
else // red to purple
{
f32 factor = (f32)bs / 255.0f;
ret.sliderSelectedColorPos = (4.0f + factor) * scaledWidth;
}
}
else
{
Die;
ret.sliderSelectedColorPos = 0.0f;
}
return ret;
}
DefineArray(ColorPicker);
static void UpdateColorPicker(ColorPicker *picker, Input input)
{
Tweekable(f32, pickerBorder);
Tweekable(f32, pickerWidth);
Tweekable(f32, pickerHeight);
Tweekable(f32, pickerSliderHeight);
Tweekable(f32, pickerHeaderSize);
// todo remove this, once we can drag the window
picker->border = pickerBorder;
picker->width = pickerWidth;
picker->height = pickerHeight;
picker->sliderHeight = pickerSliderHeight;
picker->headerSize = pickerHeaderSize;
if (picker->selecting == PickerSelecting_Header)
{
picker->pos += input.mouseZeroToOneDelta;
v2 sliderPos = picker->pos + V2(0.0f, picker->border + picker->height);
picker->sliderPos = sliderPos;
}
if (picker->selecting == PickerSelecting_ColorPicker)
{
picker->pickerPos.x = Clamp(input.mouseZeroToOne.x, picker->pos.x, picker->pos.x + picker->width) - picker->pos.x;
picker->pickerPos.y = Clamp(input.mouseZeroToOne.y, picker->pos.y, picker->pos.y + picker->height) - picker->pos.y;
}
u32 colors[7] =
{
0xFFFF0000, // blue
0xFFFFFF00, // cyan
0xFF00FF00, // green
0xFF00FFFF, // yellow
0xFF0000FF, // red
0xFFFF00FF, // purple
0xFFFF0000, // blue
};
f32 scale = 1.0f / 6.0f;
f32 scaledWidth = scale * picker->width;
if (picker->selecting == PickerSelecting_Slider)
{
MapRangeToRangeCapped(input.mouseZeroToOne.x, picker->sliderPos.x, picker->sliderPos.x + picker->width, 0.0f, 1.0f);
picker->sliderSelectedColorPos = Clamp(input.mouseZeroToOne.x - picker->sliderPos.x, 0.0f, picker->width);
u32 index = (u32)(picker->sliderSelectedColorPos / (scaledWidth));
Assert(index < ArrayCount(colors));
u32 secondIndex = index + 1;
if (index == 6) secondIndex = index;
picker->sliderColor = Pack3x8(LerpVector3(Unpack3x8(colors[index]), index * scaledWidth, Unpack3x8(colors[secondIndex]), secondIndex * scaledWidth, picker->sliderSelectedColorPos));
}
v3 ul = V3(1.0f, 1.0f, 1.0f);
v3 ur = Unpack3x8(picker->sliderColor);
v3 bottom = V3(0.0f, 0.0f, 0.0f);
f32 xfac = MapRangeToRangeCapped(picker->pickerPos.x, 0.0f, picker->width, 0.0f, 1.0f);
f32 yfac = MapRangeToRangeCapped(picker->pickerPos.y, 0.0f, picker->height, 0.0f, 1.0f);
v3 interpWithWhite = LerpVector3(ul, ur, xfac);
v3 color = LerpVector3(interpWithWhite, bottom, yfac);
*picker->pickedColor = V4(1.0f, color);
//todo if we use this in some other context, we either have to make everything tweekers, or have to re-formalize this.
//picker->tweeker.vec4 = picker->pickedColor;
//Tweek(picker->tweeker);
}
// :EditorPanel
struct TweekerPointer
{
TweekerType type;
String name;
union
{
u32 *u;
f32 *f;
v2 *vec2;
v3 *vec3;
v3i *vec3i;
v4 *vec4;
b32 *b;
Quaternion *q;
};
};
static TweekerPointer CreateTweekerPointer(TweekerType type, char *name, void *address)
{
TweekerPointer ret;
ret.type = type;
ret.name = CreateString(name);
ret.u = (u32 *)address;
return ret;
}
DefineDynamicArray(TweekerPointer);
struct EditorPanel
{
b32 visible;
v2 pos;
u32 hotValue;
Char hotValueXYZ;
TextInput textInput;
TweekerPointerDynamicArray values;
};
static f32 HeightForTweekerPanel(TweekerType type, f32 editorBorderWidth, f32 editorPanelFontSize)
{
switch (type)
{
case Tweeker_b32:
{
return (editorPanelFontSize + editorBorderWidth);
}break;
case Tweeker_u32:
{
return (editorPanelFontSize + editorBorderWidth);
}break;
case Tweeker_EntityType:
{
return (2.0f * editorPanelFontSize + editorBorderWidth);
}break;
case Tweeker_f32:
{
return (editorPanelFontSize + editorBorderWidth);
}break;
case Tweeker_v2:
{
return (3.0f * editorPanelFontSize + editorBorderWidth);
}break;
case Tweeker_v3:
{
return (4.0f * editorPanelFontSize + editorBorderWidth);
}break;
case Tweeker_v3i:
{
return (4.0f * editorPanelFontSize + editorBorderWidth);
}break;
case Tweeker_EulerAngle:
{
return (4.0f * editorPanelFontSize + editorBorderWidth);
}break;
case Tweeker_v4:
{
f32 colorRectSize = 3.0f * editorPanelFontSize;
f32 height = 2.0f * editorBorderWidth + colorRectSize;
return height;
}break;
default:
{
Die;
return 0.0f;
}break;
}
}
enum EditorState
{
EditorState_Default = 0x0,
EditorState_Scaling = 0x1,
EditorState_Rotating = 0x2,
EditorState_Moving = 0x3,
EditorState_DragingPanel = 0x4,
EditorState_PickingColor = 0x5,
EditorState_AlteringValue = 0x6,
EditorState_PlacingNewMesh = 0x7,
EditorState_DeleteSelection = 0x8,
EditorState_OrbitingCamera = 0x100,
};
static String EditorStateToString(EditorState state)
{
switch (state)
{
case EditorState_Default: return CreateString("Default");
case EditorState_Scaling: return CreateString("Scaling");
case EditorState_Rotating: return CreateString("Rotating");
case EditorState_Moving: return CreateString("Moving");
case EditorState_DragingPanel: return CreateString("DraggingPanel");
case EditorState_PickingColor: return CreateString("PickingColor");
case EditorState_AlteringValue: return CreateString("AlteringValue");
case EditorState_PlacingNewMesh: return CreateString("PlacingNewValue");
case EditorState_DeleteSelection: return CreateString("DeleteSelection");
case EditorState_OrbitingCamera: return CreateString("OrbitingCamera");
}
return CreateString("EditorState_Unknown");
}
enum EditorActionType
{
EditorAction_None,
EditorAction_AlterMesh,
EditorAction_DeleteMesh,
EditorAction_PlaceMesh,
EditorAction_AlterTile,
EditorAction_BeginBundle,
EditorAction_EndBundle,
EditorAction_Count,
};
struct EditorAction
{
EditorActionType type;
u32 serial;
// we could store only one, by altering it if we redo/undo
EntityData preModifyMesh;
EntityData postModifyMesh;
};
DefineDynamicArray(EditorAction);
struct EditorEntities
{
// todo make some sort of bucketed array.
EntityDynamicArray entities;
u32 entitySerializer;
u32DynamicArray entitySerialMap;
};
static Entity *GetEntity(EditorEntities *editorEntities, u32 serialNumber)
{
u32 index = editorEntities->entitySerialMap[serialNumber];
Entity *ret = editorEntities->entities + index;
return ret;
}
static Entity *CreateEntity(EditorEntities *editorEntities, EntityType type, u32 meshID, v3i pos, f32 scale, Quaternion orientation, v3 offset, v4 color, u64 flags)
{
Entity ret;
ret.meshId = meshID;
ret.scale = scale;
ret.orientation = orientation;
ret.physicalPos = pos;
ret.initialPos = pos;
ret.visualPos = V3(pos) + offset;
ret.color = color;
ret.frameColor = V4(1, 1, 1, 1);
ret.serial = editorEntities->entitySerializer++;
ret.type = type;
ret.flags = flags;
u32 arrayIndex = ArrayAdd(&editorEntities->entities, ret);
Assert(ret.serial == editorEntities->entitySerialMap.amount);
ArrayAdd(&editorEntities->entitySerialMap, arrayIndex);
return editorEntities->entities + arrayIndex;
};
static void RestoreEntity(EditorEntities *editorEntities, u32 serial, u32 meshID, f32 scale, Quaternion orientation, v3i pos, v3 offset, v4 color, EntityType type, u64 flags)
{
Entity ret;
ret.meshId = meshID;
ret.scale = scale;
ret.orientation = orientation;
ret.physicalPos = pos;
ret.initialPos = pos;
ret.visualPos = V3(pos) + offset;
ret.color = color;
ret.frameColor = V4(1, 1, 1, 1);
ret.serial = serial;
ret.type = type;
ret.flags = flags;
u32 arrayIndex = ArrayAdd(&editorEntities->entities, ret);
Assert(editorEntities->entitySerialMap[serial] == 0xFFFFFFFF);
editorEntities->entitySerialMap[serial] = arrayIndex;
}
static void RemoveEntity(EditorEntities *editorEntities, u32 serial)
{
u32 index = editorEntities->entitySerialMap[serial];
Entity *toRemove = editorEntities->entities + index;
Assert(toRemove->serial == serial);
editorEntities->entitySerialMap[serial] = 0xFFFFFFFF;
u32 lastIndex = editorEntities->entities.amount - 1;
if (index != lastIndex)
{
u32 serialNumberToChange = editorEntities->entities[lastIndex].serial;
editorEntities->entitySerialMap[serialNumberToChange] = index;
}
UnorderedRemove(&editorEntities->entities, index);
}
typedef EntityDataDynamicArray EditorClipBoard;
struct EditorLevelInfo
{
String name;
Camera camera;
LightSource lightSource;
u32 blocksNeeded;
};
struct Editor
{
EditorState state = EditorState_Default;
Camera camera;
v3 focusPoint;
EditorLevelInfo levelInfo;
EditorEntities editorEntities;
u32DynamicArray hotEntitySerials;
EntityDataDynamicArray hotEntityInitialStates;
EditorClipBoard clipBoard;
EditorActionDynamicArray undoRedoBuffer;
u32 undoRedoAt;
// todo: I could pack these...
b32 editingPhysical;
b32 orbitingCamera;
b32 panelIsHidden;
EditorPanel panel;
ColorPickerDynamicArray colorPickers;
};
static void EditorGoToNone(Editor *editor)
{
editor->state = EditorState_Default;
}
static bool EditorHasSelection(Editor *editor)
{
return (editor->hotEntitySerials.amount > 0);
}
static void ResetEditorPanel(Editor *editor)
{
editor->panel.values.amount = 0;
editor->panel.visible = false;
}
static void EditorSelectNothing(Editor *editor)
{
editor->hotEntitySerials.amount = 0;
editor->hotEntityInitialStates.amount = 0;
ResetEditorPanel(editor);
}
static void NewLevel(Editor *editor)
{
EditorSelectNothing(editor);
EditorGoToNone(editor);
ResetEditorPanel(editor);
editor->undoRedoBuffer.amount = 0;
editor->undoRedoAt = 0xFFFFFFFF;
editor->focusPoint = V3();
EditorEntities *editorEntities = &editor->editorEntities;
editorEntities->entities.amount = 0;
editorEntities->entitySerialMap.amount = 0;
editorEntities->entitySerializer = 0;
}
static v3 GetAveragePosForSelection(Editor *editor)
{
v3 averagePos = V3();
For(editor->hotEntitySerials)
{
Entity *e = GetEntity(&editor->editorEntities, *it);
averagePos += e->visualPos;
}
averagePos /= (f32)editor->hotEntitySerials.amount;
return averagePos;
}
static void ClearEditorEntities(EditorEntities *ret, Level *level)
{
ret->entitySerializer = 0;
ret->entities.amount = 0;
Reserve(&ret->entities, level->entities.amount);
ret->entitySerialMap.amount = 0;
Reserve(&ret->entitySerialMap, level->entities.amount);
For(level->entities)
{
CreateEntity(ret, it->type, it->meshId, it->physicalPos, it->scale, it->orientation, it->offset, it->color, it->flags);
}
}
static void EditorLoadLevel(Editor *editor, Arena *currentStateArena, Level *level)
{
ClearEditorEntities(&editor->editorEntities, level);
EditorSelectNothing(editor);
EditorGoToNone(editor);
editor->undoRedoBuffer.amount = 0;
editor->camera = level->camera;
editor->levelInfo.camera = level->camera;
editor->levelInfo.name = CopyString(level->name, currentStateArena);
editor->levelInfo.lightSource = level->lightSource;
editor->levelInfo.blocksNeeded = level->blocksNeeded;
editor->focusPoint = V3(ScreenZeroToOneToInGame(editor->camera, V2(0.5f, 0.5f)), 0.0f);
ResetEditorPanel(editor);
}
// should the editor be a thing you load when you open it or instanciate?
static Editor InitEditor()
{
Editor ret;
ret.state = EditorState_Default;
ret.colorPickers = ColorPickerCreateDynamicArray(globalAlloc);
ret.clipBoard = EntityDataCreateDynamicArray(globalAlloc);
ret.undoRedoBuffer = EditorActionCreateDynamicArray(globalAlloc);
ret.undoRedoAt = 0xFFFFFFFF;
ret.hotEntitySerials = u32CreateDynamicArray(globalAlloc);
ret.hotEntityInitialStates = EntityDataCreateDynamicArray(globalAlloc);
Tweekable(v2, initialEditorPanelPos, V2(0.75f, 0.2f));
ret.editorEntities.entitySerializer = 0;
ret.editorEntities.entities = EntityCreateDynamicArray(globalAlloc, 100);
ret.editorEntities.entitySerialMap = u32CreateDynamicArray(globalAlloc, 100);
ret.panel.pos = initialEditorPanelPos;
ret.panel.visible = false;
ret.panel.values = TweekerPointerCreateDynamicArray(globalAlloc);
ret.panel.textInput.amount = 0;
ret.panel.hotValue = 0xFFFFFFFF;
ret.focusPoint = V3();
return ret;
}
static void WriteSingleTweeker(Tweeker tweeker);
static void UpdateColorPickers(Editor *editor, Input input)
{
For(editor->colorPickers)
{
UpdateColorPicker(it, input);
}
}
static Entity *GetHotEntity(Camera cam, EditorEntities *editorEntities, AssetHandler *handler, v2 mousePosZeroToOne)
{
v3 camP = cam.pos; // todo camera or debugCamera? Maybe we should again unify them
v3 camD = ScreenZeroToOneToDirecion(cam, mousePosZeroToOne);
f32 minDist = F32MAX;
Entity *ret = NULL;
For(editorEntities->entities)
{
MeshInfo *info = GetMeshInfo(handler, it->meshId);
if (!info) continue; // probably not on screen, if never rendered
m4x4 mat = QuaternionToMatrix4(Inverse(it->orientation)); // todo save these?
v3 rayP = mat * (camP - it->visualPos);
v3 rayD = mat * camD;
// better rayCast system, right now this loads every mesh, to find out the aabb....
AABB aabb = info->aabb;
aabb.maxDim *= it->scale;
aabb.minDim *= it->scale;
f32 curIntersectionMin = MAXF32;
f32 x = rayP.x;
f32 dx = rayD.x;
f32 y = rayP.y;
f32 dy = rayD.y;
f32 z = rayP.z;
f32 dz = rayD.z;
f32 aabbMinX = aabb.minDim.x;
f32 aabbMaxX = aabb.maxDim.x;
f32 aabbMinY = aabb.minDim.y;
f32 aabbMaxY = aabb.maxDim.y;
f32 aabbMinZ = aabb.minDim.z;
f32 aabbMaxZ = aabb.maxDim.z;
f32 t1x = (aabbMaxX - x) / dx;
if (dx > 0 && t1x <= curIntersectionMin)
{
curIntersectionMin = t1x;
}
f32 t2x = (aabbMinX - x) / dx;
if (dx < 0 && t2x <= curIntersectionMin)
{
curIntersectionMin = t2x;
}
f32 t1y = (aabbMaxY - y) / dy;
if (dy > 0 && t1y <= curIntersectionMin)
{
curIntersectionMin = t1y;
}
f32 t2y = (aabbMinY - y) / dy;
if (dy < 0 && t2y <= curIntersectionMin)
{
curIntersectionMin = t2y;
}
f32 t1z = (aabbMaxZ - z) / dz;
if (dz > 0 && t1z <= curIntersectionMin)
{
curIntersectionMin = t1z;
}
f32 t2z = (aabbMinZ - z) / dz;
if (dz < 0 && t2z <= curIntersectionMin)
{
curIntersectionMin = t2z;
}
v3 curExit = rayD * curIntersectionMin + rayP;
if (PointInAABB(aabb, curExit))
{
f32 dist = Dist(curExit, rayP);
if (dist < minDist)
{
minDist = dist;
ret = it;
}
}
}
return ret;
}
static void ResetHotMeshes(Editor *editor)
{
for(u32 i = 0; i < editor->hotEntitySerials.amount; i++)
{
Entity *e = GetEntity(&editor->editorEntities, editor->hotEntitySerials[i]);
EntityData *data = editor->hotEntityInitialStates + i;
ApplyEntityDataToEntity(e, data);
}
}
// kills all redos
static void AddActionToUndoRedoBuffer(Editor *editor, EditorAction toAdd)
{
editor->undoRedoBuffer.amount = ++editor->undoRedoAt;
ArrayAdd(&editor->undoRedoBuffer, toAdd);
Assert((editor->undoRedoAt + 1) == editor->undoRedoBuffer.amount);
}
static void EditorPerformUndo(Editor *editor)
{
if (editor->undoRedoAt == 0xFFFFFFFF) return;
EditorEntities *editorEntities = &editor->editorEntities;
EditorAction toReverse = editor->undoRedoBuffer[editor->undoRedoAt--];
switch (toReverse.type)
{
case EditorAction_AlterMesh:
{
EntityData *data = &toReverse.preModifyMesh;
Entity *e = GetEntity(editorEntities, toReverse.serial);
ApplyEntityDataToEntity(e, data);
}break;
case EditorAction_DeleteMesh:
{
EntityData data = toReverse.preModifyMesh;
u32 serial = toReverse.serial;
RestoreEntity(editorEntities, serial, data.meshId, data.scale, data.orientation, data.physicalPos, data.offset, data.color, data.type, data.flags);
}break;
case EditorAction_PlaceMesh:
{
RemoveEntity(editorEntities, toReverse.serial);
}break;
case EditorAction_BeginBundle:
{
}break;
case EditorAction_EndBundle:
{
while (editor->undoRedoBuffer[editor->undoRedoAt].type != EditorAction_BeginBundle)
{
EditorPerformUndo(editor);
}
editor->undoRedoAt--; // for the BeginBundle
}break;
InvalidDefaultCase;
}
}
static void EditorPerformRedo(Editor *editor)
{
if (editor->undoRedoAt == (editor->undoRedoBuffer.amount - 1)) return;
EditorAction toReverse = editor->undoRedoBuffer[++editor->undoRedoAt];
EditorEntities *editorEntities = &editor->editorEntities;
switch (toReverse.type)
{
case EditorAction_AlterMesh:
{
EntityData *data = &toReverse.postModifyMesh;
Entity *e = GetEntity(editorEntities, toReverse.serial);
ApplyEntityDataToEntity(e, data);
}break;
case EditorAction_DeleteMesh:
{
RemoveEntity(editorEntities, toReverse.serial);
}break;
case EditorAction_PlaceMesh:
{
EntityData data = toReverse.postModifyMesh;
RestoreEntity(editorEntities, toReverse.serial, data.meshId, data.scale, data.orientation, data.physicalPos, data.offset, data.color, data.type, data.flags);
}break;
case EditorAction_BeginBundle:
{
while (editor->undoRedoBuffer[editor->undoRedoAt].type != EditorAction_EndBundle)
{
EditorPerformRedo(editor);
}
}break;
case EditorAction_EndBundle:
{
return;
}break;
InvalidDefaultCase;
}
}
static void PushAlterMeshModifies(Editor *editor)
{
EditorEntities *editorEntities = &editor->editorEntities;
for(u32 i = 0; i < editor->hotEntitySerials.amount; i++)
{
Entity *e = GetEntity(editorEntities, editor->hotEntitySerials[i]);
EntityData *data = editor->hotEntityInitialStates + i;
EditorAction toAdd;
toAdd.type = EditorAction_AlterMesh;
toAdd.serial = e->serial;
toAdd.preModifyMesh = *data;
toAdd.postModifyMesh = EntityToData(*e);
AddActionToUndoRedoBuffer(editor, toAdd);
}
}
static void EditorPushUndo(Editor *editor)
{
Assert(editor->hotEntitySerials.amount);
if (editor->hotEntitySerials.amount > 1)
{
EditorAction toAdd;
toAdd.type = EditorAction_BeginBundle;
AddActionToUndoRedoBuffer(editor, toAdd);
}
switch (editor->state)
{
case EditorState_Default: // for arrow moves
case EditorState_Scaling:
case EditorState_Rotating:
case EditorState_Moving:
case EditorState_PickingColor:
case EditorState_AlteringValue:
{
PushAlterMeshModifies(editor);
}break;
case EditorState_PlacingNewMesh:
{
// we assume that this is called right after we place the entities.
// and that we selected them
for(u32 i = 0; i < editor->hotEntitySerials.amount; i++)
{
EditorAction toAdd;
toAdd.type = EditorAction_PlaceMesh;
toAdd.serial = editor->hotEntitySerials[i];
toAdd.postModifyMesh = editor->hotEntityInitialStates[i];
AddActionToUndoRedoBuffer(editor, toAdd);
}
}break;
case EditorState_DeleteSelection:
{
// to be called when the selection did not get cleared yet.
for(u32 i = 0; i < editor->hotEntitySerials.amount; i++)
{
EditorAction toAdd;
toAdd.type = EditorAction_DeleteMesh;
toAdd.serial = editor->hotEntitySerials[i];
EditorEntities *manager = &editor->editorEntities;
Entity *e = GetEntity(manager, toAdd.serial);
toAdd.preModifyMesh = EntityToData(*e);
AddActionToUndoRedoBuffer(editor, toAdd);
}
}break;
default:
{
Die;
}break;
}
if (editor->hotEntitySerials.amount > 1)
{
EditorAction toAdd;
toAdd.type = EditorAction_EndBundle;
AddActionToUndoRedoBuffer(editor, toAdd);
}
}
static void ResetSelectionInitials(Editor *editor)
{
for(u32 i = 0; i < editor->hotEntitySerials.amount; i++)
{
u32 serial = editor->hotEntitySerials[i];
Entity *e = GetEntity(&editor->editorEntities, serial);
editor->hotEntityInitialStates[i] = EntityToData(*e);
}
}
static void FrameColorSelection(Editor *editor, v4 editorMeshSelectColor)
{
EditorEntities *editorEntities = &editor->editorEntities;
For(editor->hotEntitySerials)
{
GetEntity(editorEntities, *it)->frameColor *= editorMeshSelectColor;
}
}
// todo maybe make all this matrix stuff more consitent
static void UpdateEditor(Editor *editor, Input input)
{
EditorEntities *editorEntities = &editor->editorEntities;
Camera *cam = &editor->camera;
UpdateColorPickers(editor, input);
switch (editor->state)
{
case EditorState_DeleteSelection:
{
return;
}break;
case EditorState_OrbitingCamera:
{
UpdateCamFocus(editor->focusPoint, cam, input);
return;
}break;
}
if (!EditorHasSelection(editor))
{
return;
}
Tweekable(v4, editorMeshSelectColor, V4(1.0f, 0.4f, 0.5f, 0.1f));
switch (editor->state)
{
case EditorState_Default:
{
FrameColorSelection(editor, editorMeshSelectColor);
}break;
case EditorState_Rotating:
{
v3 averagePos = GetAveragePosForSelection(editor);
//todo not sure about all this being in the z= 0 plane
v2 oldP = ScreenZeroToOneToInGame(*cam, input.mouseZeroToOne - input.mouseZeroToOneDelta);
v2 newP = ScreenZeroToOneToInGame(*cam, input.mouseZeroToOne);
v2 relP1 = newP - p12(averagePos);
v2 relP2 = oldP - p12(averagePos);
f32 angle = AngleBetween(relP2, relP1);
Quaternion q = AxisAngleToQuaternion(angle, V3(0, 0, 1));
m4x4 mat = QuaternionToMatrix4(q);
if(editor->editingPhysical)
{
For(editor->hotEntitySerials)
{
Entity *e = GetEntity(editorEntities, *it);
v4 relPos = V4(e->visualPos - averagePos, 1.0f);