|
4 | 4 | // Date: 2021-12-28
|
5 | 5 | // By Breno Cunha Queiroz
|
6 | 6 | //--------------------------------------------------
|
| 7 | +#include <atta/component/base.h> |
7 | 8 | #include <atta/component/components/material.h>
|
8 | 9 | #include <atta/component/components/mesh.h>
|
9 | 10 | #include <atta/component/components/name.h>
|
@@ -107,130 +108,36 @@ void ViewportWindows::renderUI() {
|
107 | 108 | ImGui::OpenPopup("Editor_AddBasicShape");
|
108 | 109 | addBasicShapePopup();
|
109 | 110 |
|
110 |
| - //----- Keyboard click -----// |
111 |
| - // static ImGuizmo::OPERATION mouseOperation = ImGuizmo::OPERATION::TRANSLATE; |
112 |
| - // static ImGuizmo::MODE mouseMode = ImGuizmo::MODE::LOCAL; |
113 |
| - // static bool snap = false; |
114 |
| - |
115 |
| - // if (ImGui::IsWindowHovered()) { |
116 |
| - // snap = false; |
117 |
| - // ImGuiIO& io = ImGui::GetIO(); |
118 |
| - // if (ImGui::IsKeyPressed(ImGuiKey_T) && io.KeyCtrl) { |
119 |
| - // mouseOperation = ImGuizmo::OPERATION::TRANSLATE; |
120 |
| - // mouseMode = ImGuizmo::MODE::LOCAL; |
121 |
| - // } else if (ImGui::IsKeyPressed(ImGuiKey_T) && io.KeyShift) { |
122 |
| - // mouseOperation = ImGuizmo::OPERATION::TRANSLATE; |
123 |
| - // mouseMode = ImGuizmo::MODE::WORLD; |
124 |
| - // } else if (ImGui::IsKeyPressed(ImGuiKey_S) && io.KeyCtrl) { |
125 |
| - // mouseOperation = ImGuizmo::OPERATION::SCALE; |
126 |
| - // mouseMode = ImGuizmo::MODE::LOCAL; |
127 |
| - // } else if (ImGui::IsKeyPressed(ImGuiKey_S) && io.KeyShift) { |
128 |
| - // mouseOperation = ImGuizmo::OPERATION::SCALE; |
129 |
| - // mouseMode = ImGuizmo::MODE::WORLD; |
130 |
| - // } else if (ImGui::IsKeyPressed(ImGuiKey_R) && io.KeyCtrl) { |
131 |
| - // mouseOperation = ImGuizmo::OPERATION::ROTATE; |
132 |
| - // mouseMode = ImGuizmo::MODE::LOCAL; |
133 |
| - // } else if (ImGui::IsKeyPressed(ImGuiKey_R) && io.KeyShift) { |
134 |
| - // mouseOperation = ImGuizmo::OPERATION::ROTATE; |
135 |
| - // mouseMode = ImGuizmo::MODE::WORLD; |
136 |
| - // } else if (io.KeyCtrl) |
137 |
| - // snap = true; |
138 |
| - // } |
139 |
| - |
140 |
| - //----- Render to texture -----// |
| 111 | + //----- Operation selection -----// |
| 112 | + if (ImGui::IsWindowHovered()) { |
| 113 | + ImGuiIO& io = ImGui::GetIO(); |
| 114 | + |
| 115 | + if (io.KeyCtrl) |
| 116 | + _gizmo.setMode(Gizmo::LOCAL); |
| 117 | + if (io.KeyShift) |
| 118 | + _gizmo.setMode(Gizmo::WORLD); |
| 119 | + |
| 120 | + if (ImGui::IsKeyPressed(ImGuiKey_T)) |
| 121 | + _gizmo.setOperation(Gizmo::TRANSLATE); |
| 122 | + else if (ImGui::IsKeyPressed(ImGuiKey_S)) |
| 123 | + _gizmo.setOperation(Gizmo::SCALE); |
| 124 | + else if (ImGui::IsKeyPressed(ImGuiKey_R)) |
| 125 | + _gizmo.setOperation(Gizmo::ROTATE); |
| 126 | + } |
| 127 | + |
| 128 | + //----- Render viewport to texture -----// |
141 | 129 | ImVec2 size = ImVec2(viewport->getWidth(), viewport->getHeight());
|
142 | 130 | ImGui::Image((ImTextureID)(intptr_t)viewport->getImGuiTexture(), size, ImVec2(0, 0), ImVec2(1, 1));
|
143 | 131 |
|
144 |
| - //----- ImGuizmo -----// |
145 |
| - bool imGuizmoUsingMouse = false; |
146 |
| - // component::EntityId entity = component::getSelectedEntity(); |
147 |
| - // if (entity >= 0) { |
148 |
| - // component::Transform* t = component::getComponent<component::Transform>(entity); |
149 |
| - |
150 |
| - // if (t) { |
151 |
| - // ImGuizmo::SetOrthographic(viewport->getCamera()->getName() == "OrthographicCamera"); |
152 |
| - // ImGuizmo::SetDrawlist(); |
153 |
| - // ImGuizmo::SetRect(ImGui::GetWindowPos().x + 5.0f, ImGui::GetWindowPos().y + 24.0f, viewport->getWidth(), |
154 |
| - // viewport->getHeight()); |
155 |
| - // mat4 view = transpose(viewport->getCamera()->getView()); |
156 |
| - // mat4 proj = viewport->getCamera()->getProj(); |
157 |
| - // proj.mat[1][1] *= -1; |
158 |
| - // proj.transpose(); |
159 |
| - |
160 |
| - // mat4 transform = transpose(t->getWorldTransformMatrix(entity)); |
161 |
| - |
162 |
| - // float snapValue = 0.5f; |
163 |
| - // if (mouseOperation == ImGuizmo::OPERATION::ROTATE) |
164 |
| - // snapValue = 45.0f; |
165 |
| - // float snapValues[3] = {snapValue, snapValue, snapValue}; |
166 |
| - |
167 |
| - // ImGuizmo::Manipulate(view.data, proj.data, mouseOperation, mouseMode, transform.data, nullptr, snap ? snapValues : |
168 |
| - // nullptr); |
169 |
| - |
170 |
| - // if (ImGuizmo::IsUsing()) { |
171 |
| - // imGuizmoUsingMouse = true; |
172 |
| - // transform.transpose(); |
173 |
| - |
174 |
| - // // Get changed |
175 |
| - // vec3 pos, scale; |
176 |
| - // quat newOri; |
177 |
| - // transform.getPosOriScale(pos, newOri, scale); |
178 |
| - // vec3 oriDelta = newOri.getEuler() - t->orientation.getEuler(); |
179 |
| - // quat ori; |
180 |
| - // ori.setEuler(t->orientation.getEuler() + oriDelta); |
181 |
| - |
182 |
| - // // Delta world to local |
183 |
| - // component::Relationship* r = component::getComponent<component::Relationship>(entity); |
184 |
| - // if (r && r->getParent() != -1) { |
185 |
| - // // Get transform of the first entity that has transform when going up in the hierarchy |
186 |
| - // component::Transform* pt = nullptr; |
187 |
| - // component::EntityId parentId = -1; |
188 |
| - // while (pt == nullptr) { |
189 |
| - // parentId = r->getParent(); |
190 |
| - // pt = component::getComponent<component::Transform>(parentId); |
191 |
| - // r = component::getComponent<component::Relationship>(parentId); |
192 |
| - // if (r->getParent() == -1) |
193 |
| - // break; |
194 |
| - // } |
195 |
| - |
196 |
| - // // If found some entity with transform component, convert result to be relative to it |
197 |
| - // if (pt) { |
198 |
| - // component::Transform pTransform = pt->getWorldTransform(parentId); |
199 |
| - // vec3 pPos = pTransform.position; |
200 |
| - // vec3 pScale = pTransform.scale; |
201 |
| - // quat pOri = pTransform.orientation; |
202 |
| - |
203 |
| - // // Calculate pos ori scale relative to parent |
204 |
| - // pos -= pPos; |
205 |
| - // scale /= pScale; |
206 |
| - // ori = ori * (-pOri); // Rotation from pOri to ori |
207 |
| - // } |
208 |
| - // } |
209 |
| - |
210 |
| - // // Update entity transform |
211 |
| - // if (mouseOperation == ImGuizmo::OPERATION::TRANSLATE) |
212 |
| - // t->position = pos; |
213 |
| - // else if (mouseOperation == ImGuizmo::OPERATION::ROTATE) |
214 |
| - // t->orientation = ori; |
215 |
| - // else if (mouseOperation == ImGuizmo::OPERATION::SCALE) |
216 |
| - // t->scale = scale; |
217 |
| - |
218 |
| - // // component::RigidBody2D* rb2d = component::getComponent<component::RigidBody2D>(entity); |
219 |
| - // // if (rb2d) { |
220 |
| - // // if (mouseOperation == ImGuizmo::OPERATION::TRANSLATE || mouseOperation == ImGuizmo::OPERATION::ROTATE) { |
221 |
| - // // vec2 pos = vec2(t->position); |
222 |
| - // // float angle = -t->orientation.getEuler().z; |
223 |
| - // // rb2d->setTransform(pos, angle); |
224 |
| - // // } else if (mouseOperation == ImGuizmo::OPERATION::SCALE) { |
225 |
| - // // // TODO Recreate box2d rigid body |
226 |
| - // // } |
227 |
| - // // } |
228 |
| - // } |
229 |
| - // } |
230 |
| - // } |
231 |
| - |
232 |
| - // //----- Mouse click selection -----// |
233 |
| - if (!imGuizmoUsingMouse) { |
| 132 | + //----- Gizmo manipulation -----// |
| 133 | + bool gizmoUsingMouse = false; |
| 134 | + _gizmo.setCamera(viewport->getCamera()); |
| 135 | + cmp::EntityId eid = component::getSelectedEntity(); |
| 136 | + if (eid >= 0 && _gizmo.manipulate(eid)) |
| 137 | + gizmoUsingMouse = true; |
| 138 | + |
| 139 | + //----- Mouse click selection -----// |
| 140 | + if (!gizmoUsingMouse) { |
234 | 141 | if (click.x >= 0 && click.y >= 0 && click.x < (int)viewport->getWidth() && click.y < (int)viewport->getHeight()) {
|
235 | 142 | cmp::EntityId eid = _computeEntityClick->click(viewport->getRenderer(), viewport->getCamera(), click);
|
236 | 143 | cmp::setSelectedEntity(eid);
|
|
0 commit comments