Vivid
Loading...
Searching...
No Matches
TransformComponent.cpp
1#include "TransformComponent.h"
2
3#include "imgui/imgui/imgui.h"
4#include "common/maths/Vec.h"
5#include <glm/glm.hpp>
6#include <glm/gtc/type_ptr.hpp>
7
8namespace Vivid
9{
10 void TransformComponent::updateTransformImGUI()
11 {
12 if (ImGui::IsKeyPressed((ImGuiKey)90))
13 m_CurrentGizmoOperation = ImGuizmo::TRANSLATE;
14 if (ImGui::IsKeyPressed((ImGuiKey)69))
15 m_CurrentGizmoOperation = ImGuizmo::ROTATE;
16 if (ImGui::IsKeyPressed((ImGuiKey)82)) // r Key
17 m_CurrentGizmoOperation = ImGuizmo::SCALE;
18 if (ImGui::RadioButton("Translate", m_CurrentGizmoOperation == ImGuizmo::TRANSLATE))
19 m_CurrentGizmoOperation = ImGuizmo::TRANSLATE;
20 ImGui::SameLine();
21 if (ImGui::RadioButton("Rotate", m_CurrentGizmoOperation == ImGuizmo::ROTATE))
22 m_CurrentGizmoOperation = ImGuizmo::ROTATE;
23 ImGui::SameLine();
24 if (ImGui::RadioButton("Scale", m_CurrentGizmoOperation == ImGuizmo::SCALE))
25 m_CurrentGizmoOperation = ImGuizmo::SCALE;
26
27 ImGuizmo::DecomposeMatrixToComponents(&m_Transform[0][0], &m_Position.x, &m_Rotation.x, &m_Scale.x);
28 ImGui::Text("Position");
29 ImGui::SliderFloat3("##Position", &m_Position.x, -500.0f, 500.0f);
30 ImGui::Text("Rotation");
31 ImGui::SliderFloat3("##Rotation", &m_Rotation.x, -180.0f, 180.0f);
32 ImGui::Text("Scale");
33 ImGui::SliderFloat3("##Scale", &m_Scale.x, 0.0f, 100.0f, "%.2f");
34 ImGui::SameLine();
35 ImGui::Text("Fix Scale");
36 ImGui::SameLine();
37 ImGui::Checkbox("##FixScale", &m_FixScale);
38 ImGuizmo::RecomposeMatrixFromComponents(&m_Position.x, &m_Rotation.x, &m_Scale.x, &m_Transform[0][0]);
39
40 if (m_CurrentGizmoOperation != ImGuizmo::SCALE)
41 {
42 if (ImGui::RadioButton("Local", m_CurrentGizmoMode == ImGuizmo::LOCAL))
43 m_CurrentGizmoMode = ImGuizmo::LOCAL;
44 ImGui::SameLine();
45 if (ImGui::RadioButton("World", m_CurrentGizmoMode == ImGuizmo::WORLD))
46 m_CurrentGizmoMode = ImGuizmo::WORLD;
47 }
48 if (ImGui::IsKeyPressed((ImGuiKey)83))
49 m_UsingSnap = !m_UsingSnap;
50 ImGui::Checkbox("", &m_UsingSnap);
51 ImGui::SameLine();
52
53 switch (m_CurrentGizmoOperation)
54 {
55 case ImGuizmo::TRANSLATE:
56 ImGui::InputFloat3("Snap", &m_Snap[0]);
57 break;
58 case ImGuizmo::ROTATE:
59 ImGui::InputFloat("Angle Snap", &m_Snap[0]);
60 break;
61 case ImGuizmo::SCALE:
62 ImGui::InputFloat("Scale Snap", &m_Snap[0]);
63 break;
64 }
65 // ImGui::Checkbox("Bound Sizing", &boundSizing);
66 // if (boundSizing)
67 // {
68 // ImGui::PushID(3);
69 // ImGui::Checkbox("", &boundSizingSnap);
70 // ImGui::SameLine();
71 // ImGui::InputFloat3("Snap", boundsSnap);
72 // ImGui::PopID();
73 // }
74 }
75
76 void TransformComponent::ImGuiRender()
77 {
78 Maths::Vec3 prevScale = m_Scale;
79
80 updateTransformImGUI();
81
82 // TODO: This is a hack, fix it
83 if (m_FixScale)
84 {
85 if (m_Scale.x != prevScale.x)
86 {
87 m_Scale.y = m_Scale.x;
88 m_Scale.z = m_Scale.x;
89 }
90
91 if (m_Scale.y != prevScale.y)
92 {
93 m_Scale.x = m_Scale.y;
94 m_Scale.z = m_Scale.y;
95 }
96
97 if (m_Scale.z != prevScale.z)
98 {
99 m_Scale.x = m_Scale.z;
100 m_Scale.y = m_Scale.z;
101 }
102 }
103
104 float epsilon = 0.0001f;
105 if (m_Scale.x < epsilon)
106 m_Scale.x = epsilon;
107 if (m_Scale.y < epsilon)
108 m_Scale.y = epsilon;
109 if (m_Scale.z < epsilon)
110 m_Scale.z = epsilon;
111 }
112
113 void TransformComponent::Draw(Camera* camera)
114 {
115 }
116
117 void TransformComponent::DrawGizmo(Camera* camera)
118 {
119 ImGuizmo::RecomposeMatrixFromComponents(&m_Position.x, &m_Rotation.x, &m_Scale.x, glm::value_ptr(m_Transform));
120 ImGuizmo::SetOrthographic(false);
121
122 glm::mat4 cameraProjection = camera->GetProjectionMatrix();
123 glm::mat4 cameraView = camera->GetViewMatrix();
124
125 float windowWidth = camera->GetViewportWidth();
126 float windowHeight = camera->GetViewportHeight();
127
128 ImGuizmo::SetRect(0, 0, windowWidth, windowHeight);
129
130 ImGuizmo::SetDrawlist();
131 ImGuizmo::Manipulate(glm::value_ptr(cameraView), glm::value_ptr(cameraProjection), m_CurrentGizmoOperation,
132 m_CurrentGizmoMode, glm::value_ptr(m_Transform), NULL, NULL);
133
134 ImGuizmo::DecomposeMatrixToComponents(glm::value_ptr(m_Transform), &m_Position.x, &m_Rotation.x, &m_Scale.x);
135 }
136}
A class that represents the camera.
Definition: Camera.h:25