-
Notifications
You must be signed in to change notification settings - Fork 52
/
Editor.cpp
1930 lines (1683 loc) · 57.4 KB
/
Editor.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
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
#include "stdafx.hpp"
#include "Editor.hpp"
IGNORE_WARNINGS_PUSH
#include <BulletCollision/CollisionShapes/btCylinderShape.h>
#include <BulletDynamics/Dynamics/btDynamicsWorld.h>
#include <BulletDynamics/Dynamics/btRigidBody.h>
#include <glm/gtx/intersect.hpp>
#include <glm/gtx/common.hpp> // For fmod
#include <glm/gtx/euler_angles.hpp>
IGNORE_WARNINGS_POP
#include "Audio/AudioManager.hpp"
#include "Cameras/BaseCamera.hpp"
#include "Cameras/CameraManager.hpp"
#include "Cameras/DebugCamera.hpp"
#include "FlexEngine.hpp"
#include "Graphics/DebugRenderer.hpp"
#include "Graphics/Renderer.hpp"
#include "Graphics/RendererTypes.hpp"
#include "Helpers.hpp"
#include "InputManager.hpp"
#include "Physics/PhysicsHelpers.hpp"
#include "Physics/PhysicsWorld.hpp"
#include "Physics/RigidBody.hpp"
#include "Platform/Platform.hpp"
#include "Player.hpp"
#include "Scene/BaseScene.hpp"
#include "Scene/GameObject.hpp"
#include "Scene/Mesh.hpp"
#include "Scene/MeshComponent.hpp"
#include "Scene/SceneManager.hpp"
namespace flex
{
// ImGui payload identifiers
// (Must be 12 chars or less)
const char* Editor::MaterialPayloadCStr = "material";
const char* Editor::MeshPayloadCStr = "mesh";
const char* Editor::PrefabPayloadCStr = "prefab";
const char* Editor::GameObjectPayloadCStr = "gameobject";
const char* Editor::AudioFileNameSIDPayloadCStr = "audiosid";
Editor::Editor() :
m_MouseButtonCallback(this, &Editor::OnMouseButtonEvent),
m_MouseMovedCallback(this, &Editor::OnMouseMovedEvent),
m_KeyEventCallback(this, &Editor::OnKeyEvent),
m_ActionCallback(this, &Editor::OnActionEvent),
m_StartPointOnPlane(VEC3_ZERO),
m_PreviousIntersectionPoint(VEC3_ZERO),
m_AngleSnap(glm::radians(45.0f))
{
SelectNone();
m_LMBDownPos = glm::vec2i(-1);
}
void Editor::Initialize()
{
PROFILE_AUTO("Editor Initialize");
// Transform gizmo materials
{
MaterialCreateInfo matCreateInfo = {};
matCreateInfo.shaderName = "colour";
matCreateInfo.constAlbedo = VEC4_ONE;
matCreateInfo.persistent = true;
matCreateInfo.bEditorMaterial = true;
matCreateInfo.bSerializable = false;
matCreateInfo.name = "transform x";
m_TransformGizmoMatXID = g_Renderer->InitializeMaterial(&matCreateInfo);
matCreateInfo.name = "transform y";
m_TransformGizmoMatYID = g_Renderer->InitializeMaterial(&matCreateInfo);
matCreateInfo.name = "transform z";
m_TransformGizmoMatZID = g_Renderer->InitializeMaterial(&matCreateInfo);
matCreateInfo.name = "transform all";
m_TransformGizmoMatAllID = g_Renderer->InitializeMaterial(&matCreateInfo);
matCreateInfo.name = "transform yz";
m_TransformGizmoMatYZID = g_Renderer->InitializeMaterial(&matCreateInfo);
matCreateInfo.name = "transform xz";
m_TransformGizmoMatXZID = g_Renderer->InitializeMaterial(&matCreateInfo);
matCreateInfo.name = "transform xy";
m_TransformGizmoMatXYID = g_Renderer->InitializeMaterial(&matCreateInfo);
}
g_InputManager->BindMouseButtonCallback(&m_MouseButtonCallback, 95);
g_InputManager->BindMouseMovedCallback(&m_MouseMovedCallback, 15);
g_InputManager->BindKeyEventCallback(&m_KeyEventCallback, 15);
g_InputManager->BindActionCallback(&m_ActionCallback, 15);
}
void Editor::PostInitialize()
{
PROFILE_AUTO("Editor PostInitialize");
CreateObjects();
}
void Editor::Destroy()
{
SelectNone();
if (m_TransformGizmo != nullptr)
{
g_SceneManager->CurrentScene()->RemoveEditorObjectImmediate(m_TransformGizmo);
m_TransformGizmo = nullptr;
}
if (m_GridObject != nullptr)
{
g_SceneManager->CurrentScene()->RemoveEditorObjectImmediate(m_GridObject);
m_GridObject = nullptr;
}
g_InputManager->UnbindMouseButtonCallback(&m_MouseButtonCallback);
g_InputManager->UnbindMouseMovedCallback(&m_MouseMovedCallback);
g_InputManager->UnbindKeyEventCallback(&m_KeyEventCallback);
g_InputManager->UnbindActionCallback(&m_ActionCallback);
}
void Editor::EarlyUpdate()
{
PROFILE_AUTO("Editor EarlyUpdate");
if (g_InputManager->IsMouseButtonDown(MouseButton::LEFT))
{
if (!m_bDraggingGizmo)
{
m_HoveringAxisIndex = -1;
}
else
{
if (m_HoveringAxisIndex != ALL_AXES_IDX)
{
Transform* gizmoTransform = m_TransformGizmo->GetTransform();
glm::vec3 axes[] = { gizmoTransform->GetRight(), gizmoTransform->GetUp(), gizmoTransform->GetForward() };
static real alpha = 0.8f;
static btVector4 colours[] = { btVector4(0.8f, 0.1f, 0.1f, alpha), btVector4(0.1f, 0.8f, 0.1f, alpha), btVector4(0.2f, 0.2f, 0.9f, alpha) };
glm::vec3 pos = gizmoTransform->GetWorldPosition();
glm::vec3 dir = axes[m_HoveringAxisIndex];
btVector3 p0 = ToBtVec3(pos + dir * 1000.0f);
btVector3 p1 = ToBtVec3(pos - dir * 1000.0f);
g_Renderer->GetDebugRenderer()->DrawLineWithAlpha(p0, p1, colours[m_HoveringAxisIndex], colours[m_HoveringAxisIndex]);
}
}
}
else
{
m_bDraggingGizmo = false;
if (!g_CameraManager->CurrentCamera()->bIsGameplayCam &&
!m_CurrentlySelectedObjectIDs.empty())
{
HandleGizmoHover();
}
}
#if 0
// TEMP
for (u32 i = 0; i < 128; ++i)
{
real x = sin(g_SecElapsedSinceProgramStart) + i * 0.25f;
real z = cos(g_SecElapsedSinceProgramStart + i / 10.0f);
g_Renderer->GetDebugRenderer()->drawLine(
btVector3(x, 0, z),
g_Editor->HasSelectedObject() ? ToBtVec3(g_Editor->GetSelectedObjectsCenter()) : btVector3(x, 10, z),
btVector3(sin(g_SecElapsedSinceProgramStart * 5.0f) * 0.5f + 0.5f, cos(g_SecElapsedSinceProgramStart * 2.5f) * 0.5f + 0.5f, 1.0f),
btVector3(1.0f, 0.0f, sin(g_SecElapsedSinceProgramStart * 5.0f) * 0.5f + 0.5f));
}
#endif
FadeOutHeadOnGizmos();
if (!m_CurrentlySelectedObjectIDs.empty() && !g_CameraManager->CurrentCamera()->bIsGameplayCam)
{
m_TransformGizmo->SetVisible(true);
UpdateGizmoVisibility();
CalculateSelectedObjectsCenter();
Transform* gizmoTransform = m_TransformGizmo->GetTransform();
if (!NearlyEquals(gizmoTransform->GetLocalPosition(), m_SelectedObjectsCenterPos, 0.0001f))
{
gizmoTransform->SetLocalPosition(m_SelectedObjectsCenterPos, true);
}
if (!NearlyEquals(gizmoTransform->GetLocalRotation(), m_SelectedObjectRotation, 0.0001f))
{
gizmoTransform->SetLocalRotation(m_SelectedObjectRotation, true);
}
glm::vec3 camPos = g_CameraManager->CurrentCamera()->position;
real scale = glm::max(glm::distance(gizmoTransform->GetWorldPosition(), camPos) / 80.0f + 0.5f, 0.1f);
if (!NearlyEquals(scale, gizmoTransform->GetLocalScale().x, 0.0001f))
{
gizmoTransform->SetWorldScale(glm::vec3(scale));
}
}
else
{
m_TransformGizmo->SetVisible(false);
}
}
void Editor::LateUpdate()
{
bool bControlDown = g_InputManager->GetKeyDown(KeyCode::KEY_LEFT_CONTROL);
if (bControlDown && g_InputManager->GetKeyDown(KeyCode::KEY_N))
{
g_SceneManager->OpenNewSceneWindow();
}
if (g_InputManager->GetKeyPressed(KeyCode::KEY_M))
{
AudioManager::ToggleMuted();
}
}
void Editor::FixedUpdate()
{
m_JitterDetector.FixedUpdate();
}
void Editor::PreSceneChange()
{
if (m_TransformGizmo != nullptr)
{
g_SceneManager->CurrentScene()->RemoveEditorObjectImmediate(m_TransformGizmo);
m_TransformGizmo = nullptr;
}
if (m_GridObject != nullptr)
{
g_SceneManager->CurrentScene()->RemoveEditorObjectImmediate(m_GridObject);
m_GridObject = nullptr;
}
SelectNone();
}
void Editor::OnSceneChanged()
{
CreateObjects();
}
void Editor::DrawImGuiObjects()
{
m_JitterDetector.DrawImGuiObjects();
}
bool Editor::HasSelectedObject() const
{
return !m_CurrentlySelectedObjectIDs.empty();
}
bool Editor::HasSelectedEditorObject() const
{
return m_CurrentlySelectedEditorObjectID != InvalidEditorObjectID;
}
std::vector<GameObjectID> Editor::GetSelectedObjectIDs(bool bForceIncludeChildren) const
{
if (bForceIncludeChildren)
{
std::vector<GameObjectID> selectedObjects;
BaseScene* currentScene = g_SceneManager->CurrentScene();
for (const GameObjectID& gameObjectID : m_CurrentlySelectedObjectIDs)
{
GameObject* gameObject = currentScene->GetGameObject(gameObjectID);
gameObject->AddSelfIDAndChildrenToVec(selectedObjects);
}
return selectedObjects;
}
else
{
return m_CurrentlySelectedObjectIDs;
}
}
GameObjectID Editor::GetFirstSelectedObjectID() const
{
return m_CurrentlySelectedObjectIDs[0];
}
void Editor::SetSelectedObject(const GameObjectID& gameObjectID, bool bSelectChildren /* = false */)
{
SelectNone();
GameObject* gameObject = g_SceneManager->CurrentScene()->GetGameObject(gameObjectID);
if (gameObject != nullptr)
{
if (bSelectChildren)
{
gameObject->AddSelfIDAndChildrenToVec(m_CurrentlySelectedObjectIDs);
}
else
{
m_CurrentlySelectedObjectIDs.push_back(gameObjectID);
}
}
CalculateSelectedObjectsCenter();
}
void Editor::SetSelectedObjects(const std::vector<GameObjectID>& selectedObjects)
{
SelectNone();
m_CurrentlySelectedObjectIDs = selectedObjects;
CalculateSelectedObjectsCenter();
}
void Editor::ToggleSelectedObject(const GameObjectID& gameObjectID)
{
auto iter = FindIter(m_CurrentlySelectedObjectIDs, gameObjectID);
if (iter == m_CurrentlySelectedObjectIDs.end())
{
GameObject* gameObject = g_SceneManager->CurrentScene()->GetGameObject(gameObjectID);
gameObject->AddSelfIDAndChildrenToVec(m_CurrentlySelectedObjectIDs);
}
else
{
m_CurrentlySelectedObjectIDs.erase(iter);
if (m_CurrentlySelectedObjectIDs.empty())
{
SelectNone();
}
}
CalculateSelectedObjectsCenter();
}
void Editor::AddSelectedObject(const GameObjectID& gameObjectID)
{
if (!Contains(m_CurrentlySelectedObjectIDs, gameObjectID))
{
GameObject* gameObject = g_SceneManager->CurrentScene()->GetGameObject(gameObjectID);
if (gameObject != nullptr)
{
gameObject->AddSelfIDAndChildrenToVec(m_CurrentlySelectedObjectIDs);
CalculateSelectedObjectsCenter();
}
}
}
void Editor::DeselectObject(const GameObjectID& gameObjectID)
{
for (const GameObjectID& selectedObj : m_CurrentlySelectedObjectIDs)
{
if (selectedObj == gameObjectID)
{
GameObject* gameObject = g_SceneManager->CurrentScene()->GetGameObject(gameObjectID);
if (gameObject != nullptr)
{
gameObject->RemoveSelfIDAndChildrenFromVec(m_CurrentlySelectedObjectIDs);
CalculateSelectedObjectsCenter();
}
else
{
Erase(m_CurrentlySelectedObjectIDs, gameObjectID);
}
return;
}
}
PrintWarn("Attempted to deselect object which wasn't selected!\n");
}
bool Editor::IsObjectSelected(const GameObjectID& gameObjectID)
{
bool bSelected = Contains(m_CurrentlySelectedObjectIDs, gameObjectID);
return bSelected;
}
glm::vec3 Editor::GetSelectedObjectsCenter()
{
return m_SelectedObjectsCenterPos;
}
void Editor::SelectNone()
{
m_CurrentlySelectedObjectIDs.clear();
m_SelectedObjectsCenterPos = VEC3_ZERO;
m_SelectedObjectDragStartPos = VEC3_ZERO;
m_DraggingGizmoScaleLast1 = VEC3_ZERO;
m_DraggingGizmoScaleLast2 = VEC3_ZERO;
m_DraggingGizmoOffset = 0.0f;
m_DraggingGizmoOffset2D = VEC2_ZERO;
m_DraggingAxisIndex = -1;
m_bDraggingGizmo = false;
}
void Editor::SetSelectedEditorObject(EditorObjectID* editorObjectID)
{
m_CurrentlySelectedEditorObjectID = *editorObjectID;
}
bool Editor::IsEditorObjectSelected(EditorObjectID* editorObjectID)
{
return m_CurrentlySelectedEditorObjectID == *editorObjectID;
}
EditorObjectID Editor::GetSelectedEditorObject() const
{
return m_CurrentlySelectedEditorObjectID;
}
void Editor::UpdateGizmoVisibility()
{
if (m_TransformGizmo->IsVisible())
{
m_TranslationGizmo->SetVisible(m_CurrentTransformGizmoState == TransformState::TRANSLATE);
m_RotationGizmo->SetVisible(m_CurrentTransformGizmoState == TransformState::ROTATE);
m_ScaleGizmo->SetVisible(m_CurrentTransformGizmoState == TransformState::SCALE);
}
}
void Editor::SetTransformState(TransformState state)
{
if (state != m_CurrentTransformGizmoState)
{
m_CurrentTransformGizmoState = state;
UpdateGizmoVisibility();
}
}
bool Editor::GetWantRenameActiveElement() const
{
return m_bWantRenameActiveElement;
}
void Editor::ClearWantRenameActiveElement()
{
m_bWantRenameActiveElement = false;
}
TransformState Editor::GetTransformState() const
{
return m_CurrentTransformGizmoState;
}
void Editor::CalculateSelectedObjectsCenter()
{
if (m_CurrentlySelectedObjectIDs.empty())
{
return;
}
glm::vec3 avgPos(0.0f);
BaseScene* currentScene = g_SceneManager->CurrentScene();
for (const GameObjectID& gameObjectID : m_CurrentlySelectedObjectIDs)
{
GameObject* gameObject = currentScene->GetGameObject(gameObjectID);
// Object may not have been added to scene yet (e.g. during obj duplication), silently fail
if (gameObject != nullptr)
{
avgPos += gameObject->GetTransform()->GetWorldPosition();
}
}
m_SelectedObjectsCenterPos = m_SelectedObjectDragStartPos;
GameObject* firstObject = currentScene->GetGameObject(m_CurrentlySelectedObjectIDs[0]);
m_SelectedObjectRotation = (firstObject != nullptr ? firstObject->GetTransform()->GetWorldRotation() : QUAT_IDENTITY);
m_SelectedObjectsCenterPos = (avgPos / (real)m_CurrentlySelectedObjectIDs.size());
}
void Editor::SelectAll()
{
std::vector<GameObject*> nearbyInteractables;
m_CurrentlySelectedObjectIDs.clear();
BaseScene* currentScene = g_SceneManager->CurrentScene();
for (GameObject* rootObject : currentScene->GetRootObjects())
{
rootObject->FilterID([](GameObject*)
{
return true;
}, m_CurrentlySelectedObjectIDs);
}
CalculateSelectedObjectsCenter();
}
bool Editor::IsDraggingGizmo() const
{
return m_bDraggingGizmo;
}
bool Editor::HandleGizmoHover()
{
if (m_bDraggingGizmo == true)
{
PrintError("Called HandleGizmoHover when m_bDraggingGizmo == true\n");
return false;
}
if (m_DraggingAxisIndex != -1)
{
PrintError("Called HandleGizmoHover when m_DraggingAxisIndex == %i\n", m_DraggingAxisIndex);
return false;
}
if (g_InputManager->GetMousePosition().x == -1.0f)
{
return false;
}
PhysicsWorld* physicsWorld = g_SceneManager->CurrentScene()->GetPhysicsWorld();
btVector3 rayStart, rayEnd;
FlexEngine::GenerateRayAtMousePos(rayStart, rayEnd);
std::string gizmoTag = (
m_CurrentTransformGizmoState == TransformState::TRANSLATE ? m_TranslationGizmoTag :
(m_CurrentTransformGizmoState == TransformState::ROTATE ? m_RotationGizmoTag :
m_ScaleGizmoTag));
GameObject* pickedTransformGizmo = physicsWorld->PickTaggedBody(rayStart, rayEnd, gizmoTag, (u32)CollisionType::EDITOR_OBJECT);
Material* xMat = g_Renderer->GetMaterial(m_TransformGizmoMatXID);
Material* yMat = g_Renderer->GetMaterial(m_TransformGizmoMatYID);
Material* zMat = g_Renderer->GetMaterial(m_TransformGizmoMatZID);
Material* allMat = g_Renderer->GetMaterial(m_TransformGizmoMatAllID);
Material* yzMat = g_Renderer->GetMaterial(m_TransformGizmoMatYZID);
Material* xzMat = g_Renderer->GetMaterial(m_TransformGizmoMatXZID);
Material* xyMat = g_Renderer->GetMaterial(m_TransformGizmoMatXYID);
glm::vec4 white(1.0f);
static const real gizmoHoverMultiplier = 0.6f;
static const glm::vec4 hoverColour(gizmoHoverMultiplier, gizmoHoverMultiplier, gizmoHoverMultiplier, 1.0f);
if (pickedTransformGizmo)
{
real alphaThreshold = 0.5f; // Anything less opaque than this is not selectable
switch (m_CurrentTransformGizmoState)
{
case TransformState::TRANSLATE:
{
std::vector<GameObject*> translationAxes = m_TranslationGizmo->GetChildren();
if (pickedTransformGizmo == translationAxes[X_AXIS_IDX] && xMat->colourMultiplier.a > alphaThreshold)
{
m_HoveringAxisIndex = X_AXIS_IDX;
xMat->colourMultiplier = hoverColour;
}
else if (pickedTransformGizmo == translationAxes[Y_AXIS_IDX] && yMat->colourMultiplier.a > alphaThreshold)
{
m_HoveringAxisIndex = Y_AXIS_IDX;
yMat->colourMultiplier = hoverColour;
}
else if (pickedTransformGizmo == translationAxes[Z_AXIS_IDX] && zMat->colourMultiplier.a > alphaThreshold)
{
m_HoveringAxisIndex = Z_AXIS_IDX;
zMat->colourMultiplier = hoverColour;
}
std::vector<GameObject*> translationPlanes = m_TranslationGizmoPlanes->GetChildren();
if (pickedTransformGizmo == translationPlanes[X_AXIS_IDX] && yzMat->colourMultiplier.a > alphaThreshold)
{
m_HoveringAxisIndex = YZ_AXIS_IDX;
yzMat->colourMultiplier = hoverColour;
}
else if (pickedTransformGizmo == translationPlanes[Y_AXIS_IDX] && xzMat->colourMultiplier.a > alphaThreshold)
{
m_HoveringAxisIndex = XZ_AXIS_IDX;
xzMat->colourMultiplier = hoverColour;
}
else if (pickedTransformGizmo == translationPlanes[Z_AXIS_IDX] && xyMat->colourMultiplier.a > alphaThreshold)
{
m_HoveringAxisIndex = XY_AXIS_IDX;
xyMat->colourMultiplier = hoverColour;
}
} break;
case TransformState::ROTATE:
{
std::vector<GameObject*> rotationAxes = m_RotationGizmo->GetChildren();
if (pickedTransformGizmo == rotationAxes[X_AXIS_IDX] && xMat->colourMultiplier.a > alphaThreshold)
{
m_HoveringAxisIndex = X_AXIS_IDX;
xMat->colourMultiplier = hoverColour;
}
else if (pickedTransformGizmo == rotationAxes[Y_AXIS_IDX] && yMat->colourMultiplier.a > alphaThreshold)
{
m_HoveringAxisIndex = Y_AXIS_IDX;
yMat->colourMultiplier = hoverColour;
}
else if (pickedTransformGizmo == rotationAxes[Z_AXIS_IDX] && zMat->colourMultiplier.a > alphaThreshold)
{
m_HoveringAxisIndex = Z_AXIS_IDX;
zMat->colourMultiplier = hoverColour;
}
} break;
case TransformState::SCALE:
{
std::vector<GameObject*> scaleAxes = m_ScaleGizmo->GetChildren();
if (pickedTransformGizmo == scaleAxes[X_AXIS_IDX] && xMat->colourMultiplier.a > alphaThreshold)
{
m_HoveringAxisIndex = X_AXIS_IDX;
xMat->colourMultiplier = hoverColour;
}
else if (pickedTransformGizmo == scaleAxes[Y_AXIS_IDX] && yMat->colourMultiplier.a > alphaThreshold)
{
m_HoveringAxisIndex = Y_AXIS_IDX;
yMat->colourMultiplier = hoverColour;
}
else if (pickedTransformGizmo == scaleAxes[Z_AXIS_IDX] && zMat->colourMultiplier.a > alphaThreshold)
{
m_HoveringAxisIndex = Z_AXIS_IDX;
zMat->colourMultiplier = hoverColour;
}
else if (pickedTransformGizmo == scaleAxes[ALL_AXES_IDX] && allMat->colourMultiplier.a > alphaThreshold)
{
m_HoveringAxisIndex = ALL_AXES_IDX;
allMat->colourMultiplier = hoverColour;
}
} break;
default:
{
PrintError("Unhandled transform state in Editor::HandleGizmoHover\n");
} break;
}
}
else
{
m_HoveringAxisIndex = -1;
}
if (m_DraggingAxisIndex != X_AXIS_IDX && m_HoveringAxisIndex != X_AXIS_IDX)
{
xMat->colourMultiplier = white;
}
if (m_DraggingAxisIndex != Y_AXIS_IDX && m_HoveringAxisIndex != Y_AXIS_IDX)
{
yMat->colourMultiplier = white;
}
if (m_DraggingAxisIndex != Z_AXIS_IDX && m_HoveringAxisIndex != Z_AXIS_IDX)
{
zMat->colourMultiplier = white;
}
if (m_DraggingAxisIndex != ALL_AXES_IDX && m_HoveringAxisIndex != ALL_AXES_IDX)
{
allMat->colourMultiplier = white;
}
if (m_DraggingAxisIndex != YZ_AXIS_IDX && m_HoveringAxisIndex != YZ_AXIS_IDX)
{
yzMat->colourMultiplier = white;
}
if (m_DraggingAxisIndex != XZ_AXIS_IDX && m_HoveringAxisIndex != XZ_AXIS_IDX)
{
xzMat->colourMultiplier = white;
}
if (m_DraggingAxisIndex != XY_AXIS_IDX && m_HoveringAxisIndex != XY_AXIS_IDX)
{
xyMat->colourMultiplier = white;
}
return m_HoveringAxisIndex != -1;
}
void Editor::HandleGizmoClick()
{
CHECK_EQ(m_bDraggingGizmo, false);
CHECK_NE(m_HoveringAxisIndex, -1);
m_DraggingAxisIndex = m_HoveringAxisIndex;
m_bDraggingGizmo = true;
m_SelectedObjectDragStartPos = m_TransformGizmo->GetTransform()->GetWorldPosition();
real gizmoSelectedMultiplier = 0.4f;
glm::vec4 selectedColour(gizmoSelectedMultiplier, gizmoSelectedMultiplier, gizmoSelectedMultiplier, 1.0f);
Material* xMat = g_Renderer->GetMaterial(m_TransformGizmoMatXID);
Material* yMat = g_Renderer->GetMaterial(m_TransformGizmoMatYID);
Material* zMat = g_Renderer->GetMaterial(m_TransformGizmoMatZID);
Material* allMat = g_Renderer->GetMaterial(m_TransformGizmoMatAllID);
Material* yzMat = g_Renderer->GetMaterial(m_TransformGizmoMatYZID);
Material* xzMat = g_Renderer->GetMaterial(m_TransformGizmoMatXZID);
Material* xyMat = g_Renderer->GetMaterial(m_TransformGizmoMatXYID);
switch (m_CurrentTransformGizmoState)
{
case TransformState::TRANSLATE:
{
if (m_HoveringAxisIndex == X_AXIS_IDX)
{
xMat->colourMultiplier = selectedColour;
}
else if (m_HoveringAxisIndex == Y_AXIS_IDX)
{
yMat->colourMultiplier = selectedColour;
}
else if (m_HoveringAxisIndex == Z_AXIS_IDX)
{
zMat->colourMultiplier = selectedColour;
}
else if (m_HoveringAxisIndex == YZ_AXIS_IDX)
{
yzMat->colourMultiplier = selectedColour;
}
else if (m_HoveringAxisIndex == XZ_AXIS_IDX)
{
xzMat->colourMultiplier = selectedColour;
}
else if (m_HoveringAxisIndex == XY_AXIS_IDX)
{
xyMat->colourMultiplier = selectedColour;
}
m_DraggingGizmoOffsetNeedsRecalculation = true;
} break;
case TransformState::ROTATE:
{
m_bFirstFrameDraggingRotationGizmo = true;
if (m_HoveringAxisIndex == X_AXIS_IDX)
{
xMat->colourMultiplier = selectedColour;
}
else if (m_HoveringAxisIndex == Y_AXIS_IDX)
{
yMat->colourMultiplier = selectedColour;
}
else if (m_HoveringAxisIndex == Z_AXIS_IDX)
{
zMat->colourMultiplier = selectedColour;
}
m_DraggingGizmoOffsetNeedsRecalculation = true;
} break;
case TransformState::SCALE:
{
if (m_HoveringAxisIndex == X_AXIS_IDX)
{
xMat->colourMultiplier = selectedColour;
}
else if (m_HoveringAxisIndex == Y_AXIS_IDX)
{
yMat->colourMultiplier = selectedColour;
}
else if (m_HoveringAxisIndex == Z_AXIS_IDX)
{
zMat->colourMultiplier = selectedColour;
}
else if (m_HoveringAxisIndex == ALL_AXES_IDX)
{
allMat->colourMultiplier = selectedColour;
}
m_DraggingGizmoOffsetNeedsRecalculation = true;
} break;
default:
{
PrintError("Unhandled transform state in Editor::HandleGizmoClick\n");
} break;
}
}
void Editor::HandleGizmoMovement()
{
CHECK_NE(m_HoveringAxisIndex, -1);
CHECK_NE(m_DraggingAxisIndex, -1);
CHECK(m_bDraggingGizmo);
if (m_CurrentlySelectedObjectIDs.empty())
{
SelectNone();
return;
}
Transform* gizmoTransform = m_TransformGizmo->GetTransform();
const glm::vec3 gizmoUp = gizmoTransform->GetUp();
const glm::vec3 gizmoRight = gizmoTransform->GetRight();
const glm::vec3 gizmoForward = gizmoTransform->GetForward();
btVector3 btRayStart, btRayEnd;
FlexEngine::GenerateRayAtMousePos(btRayStart, btRayEnd);
glm::vec3 rayStart = ToVec3(btRayStart);
glm::vec3 rayEnd = ToVec3(btRayEnd);
BaseCamera* cam = g_CameraManager->CurrentCamera();
glm::vec3 camForward = cam->forward;
glm::vec3 camRight = cam->right;
glm::vec3 camUp = cam->up;
glm::vec3 planeOrigin = gizmoTransform->GetWorldPosition();
BaseScene* currentScene = g_SceneManager->CurrentScene();
Transform* transform = currentScene->GetGameObject(m_CurrentlySelectedObjectIDs[0])->GetTransform();
if (g_InputManager->DidMouseWrap())
{
m_DraggingGizmoOffsetNeedsRecalculation = true; // Signal to recalculate offset in CalculateRayPlaneIntersectionAlongAxis
m_SelectedObjectDragStartPos = gizmoTransform->GetWorldPosition();
if (m_CurrentTransformGizmoState == TransformState::ROTATE)
{
m_bFirstFrameDraggingRotationGizmo = true;
}
}
switch (m_CurrentTransformGizmoState)
{
case TransformState::TRANSLATE:
{
// Axes for translating along X, Y, and Z respectively
const glm::vec3* axes[] = { &gizmoRight, &gizmoUp, &gizmoForward };
const bool bDraggingPlane = m_DraggingAxisIndex > Z_AXIS_IDX;
glm::vec3 deltaPosWS;
if (bDraggingPlane)
{
u32 axisIdx = m_DraggingAxisIndex - YZ_AXIS_IDX;
glm::vec3 planeN = *axes[axisIdx];
glm::vec3 planeT = *axes[(axisIdx + 1) % 3];
glm::vec3 planeB = *axes[(axisIdx + 2) % 3];
glm::vec3 intersectionPont = FlexEngine::CalculateRayPlaneIntersection(
rayStart,
rayEnd,
planeOrigin,
planeN,
planeT,
planeB,
m_SelectedObjectDragStartPos,
camForward,
m_DraggingGizmoOffset2D,
m_DraggingGizmoOffsetNeedsRecalculation,
m_PreviousIntersectionPoint);
// Dragging along plane
deltaPosWS = (intersectionPont - planeOrigin);
}
else
{
const glm::vec3* altAxes[] = { &gizmoUp, &gizmoForward, &gizmoRight };
const glm::vec3* planeNormals[] = { &gizmoForward, &gizmoRight, &gizmoUp };
// Dragging along single axis
u32 axisIdx = m_DraggingAxisIndex;
glm::vec3 axis = *axes[axisIdx];
glm::vec3 planeN = *planeNormals[axisIdx];
if (glm::abs(glm::dot(planeN, camForward)) < 0.5f)
{
planeN = *altAxes[axisIdx];
}
glm::vec3 constrainedIntersectionPont = FlexEngine::CalculateRayPlaneIntersectionAlongAxis(
axis,
rayStart,
rayEnd,
planeOrigin,
planeN,
m_SelectedObjectDragStartPos,
camForward,
m_DraggingGizmoOffset,
m_DraggingGizmoOffsetNeedsRecalculation,
m_PreviousIntersectionPoint);
deltaPosWS = (constrainedIntersectionPont - planeOrigin);
}
glm::vec3 deltaPos = transform->GetLocalRotation() * glm::inverse(transform->GetWorldRotation()) * deltaPosWS;
if (deltaPos != VEC3_ZERO)
{
for (const GameObjectID& gameObjectID : m_CurrentlySelectedObjectIDs)
{
GameObject* gameObject = currentScene->GetGameObject(gameObjectID);
GameObject* parent = gameObject->GetParent();
bool bObjectIsntChild = (parent == nullptr) || !Contains(m_CurrentlySelectedObjectIDs, parent->ID);
if (bObjectIsntChild)
{
gameObject->GetTransform()->Translate(deltaPos);
}
}
}
} break;
case TransformState::ROTATE:
{
// Axes for rotating about X, Y, and Z respectively
const glm::vec3* axesProjectedOnto[] = { &gizmoUp, &gizmoRight, &gizmoUp };
const glm::vec3* axesOfRotation[] = { &gizmoRight, &gizmoUp, &gizmoForward };
static glm::vec3 planeN = VEC3_UP;
static bool bCtrlWasDown = false;
bool bCtrlDown = g_InputManager->GetKeyDown(KeyCode::KEY_LEFT_CONTROL);
if (m_bFirstFrameDraggingRotationGizmo)
{
m_AxisProjectedOnto = *axesProjectedOnto[m_DraggingAxisIndex];
m_AxisOfRotation = *axesOfRotation[m_DraggingAxisIndex];
planeN = m_AxisOfRotation;
if (glm::abs(glm::dot(m_AxisProjectedOnto, camForward)) > 0.5f)
{
m_AxisProjectedOnto = gizmoForward;
}
}
glm::vec3 intersectionPoint(0.0f);
glm::vec3 rayDir = glm::normalize(rayEnd - rayStart);
if (glm::dot(planeN, camForward) > 0.0f)
{
planeN = -planeN;
}
real intersectionDistance;
if (glm::intersectRayPlane(rayStart, rayDir, planeOrigin, planeN, intersectionDistance))
{
intersectionPoint = rayStart + rayDir * intersectionDistance;
if (m_DraggingGizmoOffset == -1.0f)
{
m_DraggingGizmoOffset = glm::dot(intersectionPoint - m_SelectedObjectDragStartPos, m_AxisProjectedOnto);
}
if (m_bFirstFrameDraggingRotationGizmo)
{
m_StartPointOnPlane = intersectionPoint;
}
}
glm::vec3 startVec = glm::normalize(m_StartPointOnPlane - planeOrigin);
glm::vec3 intersectVec = glm::normalize(intersectionPoint - planeOrigin);
glm::vec3 vecPerp = glm::cross(m_AxisOfRotation, startVec);
// NOTE: This dot product somehow results in values > 1 occasionally, causing acos to return NaN below; clamp it
real projectedDiff = glm::clamp(glm::dot(startVec, intersectVec), -1.0f, 1.0f);
bool intersectVecOnSameHalfAsPerp = (glm::dot(intersectVec, vecPerp) > 0.0f);
bool intersectVecOnSameHalfAsStartVec = (projectedDiff > 0.0f);
// __Increment/decrement wrap count on quadrant changes__
// The cross product above gives us a perpendicular vector to startVec
// to project onto so we can monitor when the intersection vector
// (from plane origin to mouse cursor on plane) crosses over 180
// degree marks to properly negate angle returned from acos below.
if (intersectVecOnSameHalfAsPerp && !m_bLastDotPos)
{
if (intersectVecOnSameHalfAsStartVec)
{
m_RotationGizmoWrapCount++;
}
else
{
m_RotationGizmoWrapCount--;
}
}
else if (!intersectVecOnSameHalfAsPerp && m_bLastDotPos)
{
if (intersectVecOnSameHalfAsStartVec)
{
m_RotationGizmoWrapCount--;
}
else
{
m_RotationGizmoWrapCount++;
}
}
// We only care if this is even or odd
m_RotationGizmoWrapCount %= 2;
m_bLastDotPos = intersectVecOnSameHalfAsPerp;
real angleAbs = acos(projectedDiff);
real angle = (m_RotationGizmoWrapCount % 2 == 0) ? -angleAbs : angleAbs;
if (m_bFirstFrameDraggingRotationGizmo)
{
m_LastAngle = angle;
}
static real currentAngleExtra = 0.0f;
if (bCtrlDown)
{
//Print("## extra: %.2f, angle: %.2f\n", glm::degrees(currentAngleExtra), glm::degrees(angle));
Transform* objTransform = currentScene->GetGameObject(m_CurrentlySelectedObjectIDs[0])->GetTransform();
glm::quat firstSelectedObjRotOS = objTransform->GetLocalRotation();
real objAngle = glm::eulerAngles(firstSelectedObjRotOS)[m_DraggingAxisIndex];
if (!bCtrlWasDown || m_bFirstFrameDraggingRotationGizmo)
{
// TODO: This doesn't properly handle all cases, figure out why
currentAngleExtra = glm::round((objAngle) / m_AngleSnap) * m_AngleSnap - objAngle;
//Print("-- cur: %.2f, extra: %.2f, angle: %.2f, last: %.2f\n", glm::degrees(objAngle), glm::degrees(currentAngleExtra), glm::degrees(angle), glm::degrees(m_LastAngle));
}
angle = glm::round(angle / m_AngleSnap) * m_AngleSnap;
angle += currentAngleExtra;
//real dAngle = (angle - m_LastAngle);
//Print(" cur: %.2f, extra: %.2f, angle: %.2f, dAngle: %.2f\n", glm::degrees(objAngle), glm::degrees(currentAngleExtra), glm::degrees(angle), glm::degrees(dAngle));
}
else
{
currentAngleExtra = 0.0f;
}
if (m_bFirstFrameDraggingRotationGizmo)
{
m_bFirstFrameDraggingRotationGizmo = false;