Elevate Engine 1
Loading...
Searching...
No Matches
ScenePanel.cpp
Go to the documentation of this file.
1#include "eepch.h"
2#include "ScenePanel.h"
3
4#ifdef EE_EDITOR_BUILD
5
6#include "imgui.h"
7#include "ImGuizmo.h"
8
9#include "glm/glm.hpp"
10#include <glm/gtc/type_ptr.hpp>
11
14
18
19void Elevate::Editor::ScenePanel::OnImGuiRender()
20{
21 ImGui::Begin("Scene View", nullptr, ImGuiWindowFlags_MenuBar);
22
23 if (ImGui::BeginMenuBar())
24 {
25 // Aspect Ratio Selection
26 if (ImGui::BeginMenu(GuizmoOperationToString(m_CurrentEditorTool).c_str()))
27 {
28 ImGui::RadioButton("Translate", &m_CurrentEditorTool, 7);
29 ImGui::RadioButton("Rotate", &m_CurrentEditorTool, 120);
30 ImGui::RadioButton("Scale", &m_CurrentEditorTool, 896);
31 ImGui::RadioButton("Universal", &m_CurrentEditorTool, 14463);
32 ImGui::EndMenu();
33 }
34
35 ImGui::SameLine();
36 if (ImGui::Button("Edit"))
37 {
38 // Ouvre un autre menu custom
39 }
40
41 // Permet d'aligner le prochain menu � gauche en fonction de la taille du texte
42 ImGui::Dummy(ImVec2(ImGui::GetWindowSize().x - ImGui::CalcTextSize("XXX:XXX").x - ImGui::GetCursorPos().x - 10.0f, 0.0f));
43
44 // Aspect Ratio Selection
45 if (ImGui::BeginMenu(GetAspectRatioText(s_AspectRatioSettings[m_AspectRatioValue]).c_str()))
46 {
47 for (int i = 0; i < sizeof(s_AspectRatioSettings) / sizeof(s_AspectRatioSettings[0]); i++)
48 {
49 if (ImGui::RadioButton(GetAspectRatioText(s_AspectRatioSettings[i]).c_str(), &m_AspectRatioValue, i)) { UpdateViewportAspectRatio(); }
50 }
51 ImGui::EndMenu();
52 }
53 ImGui::EndMenuBar();
54 }
55
56 // we access the ImGui window size
57 uint32_t window_width = (uint32_t) ImGui::GetContentRegionAvail().x;
58 uint32_t window_height = (uint32_t) ImGui::GetContentRegionAvail().y;
59
60 // Keeping the aspect ratio for the scene view
61 const glm::ivec2 aspect = s_AspectRatioSettings[m_AspectRatioValue];
62 const float arX = (float)aspect.x;
63 const float arY = (float)aspect.y;
64 const float qtX = window_width / arX;
65 const float qtY = window_height / arY;
66 if (qtX < qtY)
67 window_height = (uint32_t)(qtX * arY);
68 else
69 window_width = (uint32_t)(qtY * arX);
70
71 // we rescale the framebuffer to the actual window size here and reset the glViewport
72 Application::Get().FrameBuffer->Rescale(window_width, window_height);
73 Renderer::SetViewport(0, 0, window_width, window_height);
74
75 // we get the screen position of the window
76 ImVec2 pos = ImGui::GetCursorScreenPos();
77 ImGui::Image((ImTextureID)Application::Get().FrameBuffer->GetNativeTextureHandle(), ImVec2((float)window_width, (float)window_height), ImVec2(0, 1), ImVec2(1, 0));
78
79 // ImGuizmo //////////////////////////////////////////
80 if (Application::GetGameState() == EditorMode)
81 {
82 std::weak_ptr<GameObject> selected = EditorLayer::Get().GetSelectedObject();
83 Camera* cam = EditorLayer::Get().GetCamera();
84 if (selected.lock())
85 {
86 std::shared_ptr<GameObject> selectedShared = selected.lock();
87 ImGuizmo::SetDrawlist(ImGui::GetWindowDrawList());
88 ImGuizmo::SetOrthographic(false); // TODO SET DINAMICLY FROM THE EDITOR AND SETUP THE CAMERA ACCORDINGLY
89 ImGuizmo::SetRect(pos.x, pos.y, (float)window_width, (float)window_height);
90
91 glm::mat4 cameraProjection = cam->GetProjectionMatrix();
92 glm::mat4 cameraView = cam->GenViewMatrix();
93 glm::mat4 entityMatrix = selectedShared->GenGlobalMatrix();
94
95 // TODO SET VIA BUTTONS
96 ImGuizmo::Manipulate(
97 glm::value_ptr(cameraView),
98 glm::value_ptr(cameraProjection),
99 (ImGuizmo::OPERATION)m_CurrentEditorTool,
100 ImGuizmo::LOCAL, // Change to WORLD if needed
101 glm::value_ptr(entityMatrix)
102 );
103
104 if (ImGuizmo::IsUsingAny())
105 {
106 selectedShared->SetFromGlobalMatrix(entityMatrix);
107 }
108 }
109 }
110
111 ImGui::End();
112}
113
114void Elevate::Editor::ScenePanel::UpdateViewportAspectRatio()
115{
116 Camera* cam = EditorLayer::Get().GetCamera();
117 const glm::ivec2 values = s_AspectRatioSettings[m_AspectRatioValue];
118 const float ratio = (float)values.x / (float)values.y;
119 cam->UpdateAspectRatio(ratio);
120}
121
122std::string Elevate::Editor::ScenePanel::GetAspectRatioText(glm::ivec2 ar)
123{
124 return (std::to_string(ar.x) + ":" + std::to_string(ar.y));
125}
126
127#endif // EE_EDITOR_BUILD
std::string GuizmoOperationToString(int tool)
Definition GizmoUtility.h:6