SpiecsEngine
 
Loading...
Searching...
No Matches
ImguiGizmos.cpp
Go to the documentation of this file.
1/**
2* @file ImguiGizmos.cpp.
3* @brief The ImguiGizmos Class Implementation.
4* @author Spices.
5*/
6
7#include "Pchheader.h"
8#include "ImguiGizmos.h"
9
10#include "Core/Math/Math.h"
11#include "Core/Input/Input.h"
12#include "Core/Input/KeyCodes.h"
13#include "World/World/World.h"
14#include "World/Entity.h"
15#include "Systems/SlateSystem.h"
16
17#include <glm/gtc/type_ptr.hpp>
18
19namespace Spices {
20
22 {
24
25 {
26 SPICES_PROFILE_ZONEN("Set ImGuizmo Draw Info");
27
28 /**
29 * @brief Set Gizmo Projection type.
30 */
31 ImGuizmo::SetOrthographic(false);
32
33 /**
34 * @brief Use Window Draw List.
35 */
36 ImGuizmo::SetDrawlist();
37
38 /**
39 * @brief Locating Gizmo.
40 */
41 ImGuizmo::SetRect(
42 m_Owner->GetPanelPos().x,
43 m_Owner->GetPanelPos().y,
44 m_Owner->GetPanelSize().x,
45 m_Owner->GetPanelSize().y
46 );
47 }
48
49 /**
50 * @brief Instnce empty camera.
51 */
52 Entity CameraEntity;
53 glm::mat4 viewMat = glm::mat4(1.0f);
54 glm::mat4 projectionMat = glm::mat4(1.0f);
55
56 /**
57 * @brief Fill in camera.
58 */
59 {
60 SPICES_PROFILE_ZONEN("Get Camera Matrix");
61
62 /**
63 * @brief Iter by view.
64 */
65 m_FrameInfo.m_World->ViewComponent<CameraComponent>([&](auto e, auto& tComp){
66
67 auto& transComp = m_FrameInfo.m_World->GetComponent<TransformComponent>(e);
68
69 if (tComp.IsActive())
70 {
71 viewMat = glm::inverse(transComp.GetModelMatrix());
72 projectionMat = tComp.GetCamera()->GetPMatrix();
73
74 CameraEntity = Entity(e, m_FrameInfo.m_World.get());
75 return true;
76 }
77
78 return false;
79 });
80 }
81
82 /**
83 * @brief Draw Editor ViewManipulate.
84 */
85 {
86 SPICES_PROFILE_ZONEN("ImGuizmo::ViewManipulate");
87
88 /**
89 * @todo fix uncorrect manipulate.
90 */
91 ImGuizmo::ViewManipulate(
92 glm::value_ptr(viewMat),
93 8.0f,
94 ImVec2(m_Owner->GetPanelPos().x, m_Owner->GetPanelPos().y + m_Owner->GetPanelSize().y - 96),
95 ImVec2(96, 96),
96 0x10101010
97 );
98 }
99
100 /**
101 * @brief Set New Camera transform.
102 */
103 //{
104 // SPICES_PROFILE_ZONEN("DecomposeTransform");
105
106 // /**
107 // * @brief Split Channels from Matrix.
108 // */
109 // glm::vec3 translation, rotation, scale;
110 // DecomposeTransform(glm::inverse(viewMat), translation, rotation, scale);
111 //}
112
113 /**
114 * @brief Draw Editor Gizmo.
115 */
116 {
117 SPICES_PROFILE_ZONEN("Draw Editor Gizmo");
118
119 if (m_FrameInfo.m_PickEntityID.size() > 0 && bEnableGizmo)
120 {
121 /**
122 * @brief Get Selected Entity.
123 */
124 Entity entity(
125 static_cast<entt::entity>(*m_FrameInfo.m_PickEntityID.end_k()),
126 m_FrameInfo.m_World.get()
127 );
128
129 /**
130 * @brief Get Transform Component.
131 */
132 auto& tc = entity.GetComponent<TransformComponent>();
133
134 /**
135 * @brief Get Entity Model Matrix.
136 */
137 glm::mat4 model = tc.GetModelMatrix();
138
139 /**
140 * @brief Snapping.
141 */
143 float snapValue = 0.5f; // Snap to 0.5m for translation/scale
144 // Snap to 45 degrees for rotation
145 if (m_GizmoType == ImGuizmo::OPERATION::ROTATE)
146 {
147 snapValue = 45.0F;
148 }
149
150 float snapValues[3] = { snapValue, snapValue ,snapValue };
151
152 /**
153 * @brief Gozmo Manipulater.
154 */
155 {
156 SPICES_PROFILE_ZONEN("ImGuizmo::Manipulate");
157
158 ImGuizmo::Manipulate(
159 glm::value_ptr(viewMat),
160 glm::value_ptr(projectionMat),
161 (ImGuizmo::OPERATION)m_GizmoType,
162 ImGuizmo::LOCAL,
163 glm::value_ptr(model),
164 nullptr, snap ? snapValues : nullptr
165 );
166 }
167
168 /**
169 * @brief Add to Entity Transform.
170 */
171 {
172 SPICES_PROFILE_ZONEN("Add Transform to Manipulatd Entity");
173
174 if (ImGuizmo::IsUsing())
175 {
176 /**
177 * @brief Split Channels from Matrix.
178 */
179 glm::vec3 translation, rotation, scale;
180 DecomposeTransform(model, translation, rotation, scale);
181 rotation = { glm::degrees(rotation.x), glm::degrees(rotation.y), glm::degrees(rotation.z) };
182
183
184 glm::vec3 deltaRotation = rotation - tc.GetRotation();
185 tc.SetPosition(translation);
186 tc.SetRotation(tc.GetRotation() + deltaRotation);
187 tc.SetScale(scale);
188
190 }
191 }
192 }
193 }
194 }
195
197 {
199
200 /**
201 * @brief Instance a EventDispatcher.
202 */
203 EventDispatcher dispatcher(event);
204
205 /**
206 * @brief Dispatch KeyPressedEvent to ImguiGizmos::OnKeyPressed.
207 */
209 }
210
212 {
214
215 switch (e.GetKeyCode())
216 {
217 case Key::Q:
218 bEnableGizmo = true;
219 m_GizmoType = ImGuizmo::OPERATION::UNIVERSAL;
220 break;
221 case Key::W:
222 bEnableGizmo = true;
223 m_GizmoType = ImGuizmo::OPERATION::TRANSLATE;
224 break;
225 case Key::E:
226 bEnableGizmo = true;
227 m_GizmoType = ImGuizmo::OPERATION::ROTATE;
228 break;
229 case Key::R:
230 bEnableGizmo = true;
231 m_GizmoType = ImGuizmo::OPERATION::SCALE;
232 break;
233 default:
234 bEnableGizmo = false;
235 m_GizmoType = -1;
236 break;
237 }
238
239 return false;
240 }
241}
#define BIND_EVENT_FN(x)
Bind Event.
Definition Event.h:88
#define SPICES_PROFILE_ZONEN(...)
#define SPICES_PROFILE_ZONE
CameraComponent Class. This class defines the specific behaves of CameraComponent.
Entity Class. This class defines the specific behaves of Entity.
Definition Entity.h:20
EventDispatcher(Event &event)
Constructor Function.
Definition Event.h:172
This Class store a Specific Event type first and Dispatch a event handle function to it.
Definition Event.h:156
This Class is the basic Event Class. Inherit from it and create specific event class.
Definition Event.h:96
static FrameInfo & Get()
Get FrameInfo.
Definition FrameInfo.cpp:14
FrameInfo Class. This class defines the FrameInfo data.
Definition FrameInfo.h:32
virtual void OnRender() override
This interface is called On SlateRenderer Render.
virtual void OnEvent(Event &event) override
This interface is called On Global Event Function Pointer is called.
bool OnKeyPressed(KeyPressedEvent &e)
Event Dispatcher target. Registry on Key Resized.
int m_GizmoType
Gizmo Type, Transpose, rotate, scale, universal.
Definition ImguiGizmos.h:84
bool bEnableGizmo
Is Enable Gizmo.
Definition ImguiGizmos.h:89
The ImguiGizmos Class. This class defines how to render a viewport gizmo.
Definition ImguiGizmos.h:18
FrameInfo & m_FrameInfo
The FrameData reference.
Definition ImguiUtils.h:166
static bool IsKeyPressed(const int &keycode)
Query If given Key is Pressed.
Definition Input.h:34
This Class Is a wrapper of Platform Specific Input Query.
Definition Input.h:16
const int & GetKeyCode() const
Get Key Input Code.
Definition KeyEvent.h:30
This Class is inherited from KeyEvent Class.
Definition KeyEvent.h:57
TransformComponent Class. This class defines the specific behaves of TransformComponent.
@ FrushStableFrame
Definition World.h:48
@ NeedUpdateTLAS
Definition World.h:49
World Class. This class defines the basic behaves of World. When we create an new world,...
Definition World.h:41