Elevate Engine 1
Loading...
Searching...
No Matches
EditorLayer.cpp
Go to the documentation of this file.
1#include "eepch.h"
2#include "EditorLayer.h"
3
4#ifdef EE_EDITOR_BUILD
5
6#include "imgui.h"
7
9
11
15
17
19
22
25
27
28// TODO : CHECK ESSENTIAL INCLUDES KNOW ESSENTIAL BELLOW
29// Core
31// Rendering
33
34namespace Elevate::Editor
35{
36 EditorLayer* EditorLayer::s_Instance = nullptr;
37
38 EditorLayer::EditorLayer()
39 {
40 s_Instance = this;
41 m_playTexture = Texture::CreateFromFile("editor://Icons/Light/Colored/play.png");
42 m_pauseTexture = Texture::CreateFromFile("editor://Icons/Light/pause.png");
43 m_stopTexture = Texture::CreateFromFile("editor://Icons/Light/stop.png");
44 m_coloredStopTexture = Texture::CreateFromFile("editor://Icons/Light/Colored/stop.png");
45 InitUI();
46 }
47
48 void EditorLayer::OnAttach()
49 {
50 m_EditorScene = Scene::Create("Editor", SceneType::EditorScene);
51
52 // Editor Camera
53 m_CameraObject = GameObject::Create("Editor Camera", m_EditorScene);
54 m_CameraObject->AddComponent<EditorCamera>(60.0f);
55
56 // Setup the grid shader ///////////////////////////
57 m_GridShader = Shader::CreateFromFiles(
58 "editor://Shaders/grid.vert",
59 "editor://Shaders/grid.frag",
62 );
63 MaterialPtr gridMaterial = MaterialRegistry::LoadMaterial(m_GridShader);
64 gridMaterial->Set<glm::vec4>("lineColor", { 0.9, 0.9, 0.9, 0.5 });
65 gridMaterial->Set<glm::vec4>("backgroundColor", { 0.6, 0.6, 0.6, 0.025 });
67
68 // Grid
69 m_GridObject = GameObject::Create("Editor Grid", m_EditorScene);
70 Model& gridModel = m_GridObject->AddComponent<Model>(PrimitiveType::Quad);
71 RenderState& state = gridModel.GetRenderState();
72 state.Cullface = false;
73 state.DepthWrite = true;
74 state.DepthTest = true;
75 gridModel.SetMaterial(gridMaterial);
76 m_GridObject->SetScale({ 50, 50, 50 });
77
78 CreateWidget<SkyboxEditorWidget>();
79 }
80
81 void EditorLayer::OnUpdate()
82 {
83 for (auto& widgetPtr : m_widgets)
84 widgetPtr->OnUpdate();
85
86 m_EditorScene->UpdateScene();
87
88 ExecuteStack(); // Execute the command buffer
89 }
90
91 void EditorLayer::OnRender()
92 {
93 // Rendering the editor grid
94 glm::vec3 camPos = m_CameraObject->GetPosition();
95 // todo use materials instead
96 Renderer::BindShader(m_GridShader);
97 m_GridShader->SetProjectionViewMatrix(*GetCamera());
98 m_GridObject->SetPosition({ camPos.x, 0, camPos.z });
99
100 m_EditorScene->RenderScene(GetCamera());
101
102 if (m_SelectedObject.lock())
103 {
104 m_SelectedObject.lock()->RenderWhenSelected();
105 }
106 }
107
108 void EditorLayer::OnImGuiRender()
109 {
110 ImGuiIO& io = ImGui::GetIO();
111
112 // MENU
113 if (ImGui::BeginMainMenuBar())
114 {
115 if (ImGui::BeginMenu("File"))
116 {
117 // TODO IMPL. FEATURES
118 // TODO handle projects
119 if (ImGui::MenuItem("New project")) { /* Action pour cr�er une nouvelle sc�ne */ }
120 if (ImGui::MenuItem("Open project")) { /* Action pour ouvrir un fichier */ }
121 if (ImGui::MenuItem("Save")) { /* Action pour sauvegarder */ }
122 if (ImGui::MenuItem("Exit")) { exit(0); }
123 ImGui::EndMenu();
124 }
125
126 if (ImGui::BeginMenu("Edit"))
127 {
128 // TODO impl. action stack
129 if (ImGui::MenuItem("Undo")) { /* Action pour annuler */ }
130 if (ImGui::MenuItem("Redo")) { /* Action pour refaire */ }
131 ImGui::EndMenu();
132 }
133
134 if (ImGui::BeginMenu("Objects"))
135 {
136 // TODO: CONVERT TO COMMANDS
137 if (ImGui::MenuItem("Cube")) {
139 std::shared_ptr<Elevate::GameObject> obj = Elevate::GameObject::Create("Cube", scene);
140 obj->AddComponent<Elevate::Model>(PrimitiveType::Cube);
141 }
142 if (ImGui::MenuItem("Plane")) {
144 std::shared_ptr<Elevate::GameObject> obj = Elevate::GameObject::Create("Plane", scene);
145 obj->AddComponent<Elevate::Model>(PrimitiveType::Plane);
146 }
147 if (ImGui::MenuItem("Quad")) {
149 std::shared_ptr<Elevate::GameObject> obj = Elevate::GameObject::Create("Quad", scene);
150 obj->AddComponent<Elevate::Model>(PrimitiveType::Quad);
151 }
152 if (ImGui::MenuItem("UV Sphere")) {
154 std::shared_ptr<Elevate::GameObject> obj = Elevate::GameObject::Create("UV Sphere", scene);
155 obj->AddComponent<Elevate::Model>(PrimitiveType::UVSphere);
156 }
157 // TODO ADD OTHER MODELS
158 ImGui::EndMenu();
159 }
160
161 if (ImGui::BeginMenu("Preferences"))
162 {
163 if (ImGui::BeginMenu("Theme"))
164 {
165 if (ImGui::MenuItem("Dark", NULL, UI::GetCurrentTheme() == UI::UITheme::Dark))
166 {
168 }
169 if (ImGui::MenuItem("Light", NULL, UI::GetCurrentTheme() == UI::UITheme::Light))
170 {
172 }
173 ImGui::EndMenu();
174 }
175 ImGui::EndMenu();
176 }
177
178 ImGui::EndMainMenuBar();
179 }
180
181 ImGuiViewport* viewport = ImGui::GetMainViewport();
182
183 float toolbarHeight = 50.0f;
184
185 ImGui::SetNextWindowPos(ImVec2(viewport->Pos.x, viewport->Pos.y + toolbarHeight + ImGui::GetFrameHeight()));
186 ImGui::SetNextWindowSize(ImVec2(viewport->Size.x, viewport->Size.y - ImGui::GetFrameHeight() - toolbarHeight));
187 ImGui::SetNextWindowViewport(viewport->ID);
188
189 ImGuiWindowFlags window_flags = ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoCollapse |
190 ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoBringToFrontOnFocus |
191 ImGuiWindowFlags_NoNavFocus | ImGuiWindowFlags_NoBackground;
192
193 ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2(0, 0)); // Prevent a weird black stripe arround the dockspace
194 ImGui::Begin("DockSpaceWindow", nullptr, window_flags);
195
196 ImGuiID dockspace_id = ImGui::GetID("MyDockspace");
197 ImGui::DockSpace(dockspace_id, ImVec2(0.0f, 0.0f), ImGuiDockNodeFlags_None);
198
199 ImGui::End();
200 ImGui::PopStyleVar();
201
202 ImGui::SetNextWindowPos(viewport->Pos + ImVec2(0, ImGui::GetFrameHeight()));
203 ImGui::SetNextWindowSize(ImVec2(viewport->Size.x, toolbarHeight));
204 ImGuiWindowFlags toolbarFlags = ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoResize |
205 ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoScrollbar | ImGuiWindowFlags_NoDocking;
206
207 ImGui::Begin("Toolbar", nullptr, toolbarFlags);
208
209 ImGui::SetCursorPosX(viewport->Size.x / 2 - toolbarHeight);
211 bool canPlay = CameraManager::GetRuntime() != nullptr;
212 ImTextureID playTexID = isPlaying ? (ImTextureID)m_pauseTexture->GetNativeHandle() : (ImTextureID)m_playTexture->GetNativeHandle();
213 ImGui::BeginDisabled(!isPlaying && !canPlay);
214 if (ImGui::ImageButton(isPlaying ? "Pause" : "Play", playTexID, ImVec2(32, 32)))
215 {
216 if (isPlaying)
217 {
219 }
220 else
221 {
223 }
224 }
225 ImGui::EndDisabled();
226
227 ImGui::SameLine();
229 ImTextureID stopTexID = isInEditorMode ? (ImTextureID)m_stopTexture->GetNativeHandle() : (ImTextureID)m_coloredStopTexture->GetNativeHandle();
230 ImGui::BeginDisabled(isInEditorMode);
231 if (ImGui::ImageButton("Stop", stopTexID, ImVec2(32, 32)))
232 {
234 }
235 ImGui::EndDisabled();
236 ImGui::End();
237
238 for (auto& widgetPtr : m_widgets)
239 widgetPtr->OnImGuiRender();
240 }
241
242 void EditorLayer::OnEvent(Event& event)
243 {
244 switch (event.GetEventType())
245 {
247 {
248 KeyEvent& ke = (KeyEvent&)event;
249
250 if (ke.GetKeyCode() == EE_KEY_DELETE) {
251 Execute(std::make_unique<DeleteGameobjectCommand>(m_SelectedObject));
252 }
253
255 if (ke.GetKeyCode() == EE_KEY_Z) {
256 Undo();
257 } else if (ke.GetKeyCode() == EE_KEY_Y) {
258 Redo();
259 }
260 }
261 break;
262 }
263 default:
264 break;
265 }
266
267 m_EditorScene->Notify(event);
268 }
269
270 EditorCamera* EditorLayer::GetCamera()
271 {
272 return m_CameraObject->GetComponent<EditorCamera>();
273 }
274
275 void EditorLayer::SelectObject(std::shared_ptr<GameObject> newSelection)
276 {
277 if (newSelection) {
278 m_SelectedObject = newSelection;
279 }
280 else {
281 m_SelectedObject.reset();
282 }
283 }
284}
285
286#endif // EE_EDITOR_BUILD
#define EE_SHADER_HEADER
Definition Core.h:32
#define EE_KEY_LEFT_CONTROL
Definition KeyCodes.h:118
#define EE_KEY_DELETE
Definition KeyCodes.h:61
#define EE_KEY_Z
Definition KeyCodes.h:47
#define EE_KEY_Y
Definition KeyCodes.h:46
static void SetGameState(GameContextState newState)
static const GameContextState & GetGameState()
static Camera * GetRuntime()
static std::shared_ptr< GameObject > Create(std::string name, std::shared_ptr< Scene > scene, std::shared_ptr< GameObject > parent=nullptr)
static bool IsKeyPressed(int keycode)
Definition Input.h:36
static MaterialPtr LoadMaterial(const std::shared_ptr< Shader > &shader)
Definition Material.cpp:109
static bool BindShader(const std::shared_ptr< Shader > &shader)
Definition Renderer.cpp:30
static ScenePtr GetCurrentScene()
static ScenePtr Create(std::string name, SceneType type=SceneType::RuntimeScene)
Definition Scene.cpp:153
static std::shared_ptr< Shader > CreateFromFiles(const std::string &vertexSrcPath, const std::string &fragSrcPath)
Definition Shader.cpp:79
static TexturePtr CreateFromFile(const std::string &path, TextureType usage=TextureType::Diffuse)
Definition Texture.cpp:22
void SetLightTheme()
void SetDarkTheme()
UITheme GetCurrentTheme()
@ RuntimeScene
Definition Scene.h:29
@ EditorScene
Definition Scene.h:30
std::shared_ptr< Material > MaterialPtr
Definition Material.h:24
std::shared_ptr< Scene > ScenePtr
Definition Renderer.h:12
@ EditorMode
Definition GameContext.h:8