VEX Engine
Retro looking game engine made in vulkan mostly because i wanted to understand it better.
Loading...
Searching...
No Matches
Editor.hpp
Go to the documentation of this file.
1
6
7#pragma once
8#include "Engine.hpp"
9#include "EditorCamera.hpp"
10#include "EditorCommands.hpp"
11
12#include "EditorProperties.hpp"
13#include "ProjectProperties.hpp"
14
17
18#include "components/DebugConsole.hpp"
19
20#include <ImGuizmo.h>
21#include <nlohmann/json.hpp>
22
23#include <memory>
24#include <deque>
25#include <stack>
26
27namespace vex {
28
29 struct SceneRenderData;
30
32 class Editor : public Engine {
33 public:
34
43 Editor(const char* title, int width, int height, GameInfo gInfo, const std::string& projectBinaryPath);
44 ~Editor(){}
45
47 void render() override;
48
51 void update(float deltaTime) override;
52
56 void processEvent(const SDL_Event& event, float deltaTime) override;
57
59 void setFullscreen(bool enabled, bool exclusive = false) override { /*pass*/ };
60
63 void requestSceneReload(const std::string& scenePath);
64
67 std::string getProjectBinaryPath() const { return m_projectBinaryPath; }
68
71 AssetBrowser* getAssetBrowser() const { return m_assetBrowser.get(); }
72
75 const BrowserIcons& GetEditorIcons() const { return m_icons; }
76
79 EditorProperties* getEditorProperties() { return &m_editorProperties; }
80
83 ProjectProperties* getProjectProperties() { return &m_projectProperties; }
84
88 void HandleMeshDrop(const std::string& filepath, vex::Entity parent = vex::NULL_ENTITY);
89
91 void OnHotReload();
92
94 void refreshForObject() override {
95 m_frame = 0;
96 m_refresh = true;
97 }
98
101
103 void CopySelectedObject();
104
106 void PasteObjectToScene();
107
110
113
115 void Undo();
116
118 void Redo();
119
121 bool IsEditor() override { return true; }
122
124 void ShowConsole() {
125 showConsole = true;
126 }
127 private:
133 void drawEditorLayout(const SceneRenderData& data, glm::uvec2& outNewResolution);
134
140 void drawGizmo(const glm::vec2& viewportPos, const glm::vec2& viewportSize);
141
143 void loadAssetIcons();
144
153 float raySphereIntersect(const glm::vec3& rayOrigin, const glm::vec3& rayDir, const glm::vec3& sphereCenter, float sphereRadius);
154
164 float rayTriangleIntersect(const glm::vec3& rayOrigin, const glm::vec3& rayDir, const glm::vec3& v0, const glm::vec3& v1, const glm::vec3& v2);
165
171 void ExtractObjectByEntity(vex::Entity entity, std::pair<bool, vex::GameObject*>& selectedObject);
172
178 void SaveConfig(const EditorProperties& data, const std::string& filename) {
179 std::ofstream file(filename);
180 if (file.is_open()) {
181 nlohmann::json j = data;
182 file << j.dump(4);
183 }
184 }
185
191 void LoadConfig(EditorProperties& data, const std::string& filename) {
192 std::ifstream file(filename);
193 if (file.is_open()) {
194 try {
195 nlohmann::json j;
196 file >> j;
197 data = j;
198 } catch (const std::exception& e) {
199 //std::cerr << "JSON parsing error: " << e.what() << std::endl;
201 }
202 }
203 }
204
210 void SaveProjectConfig(const ProjectProperties& data, const std::string& filename) {
211 std::ofstream file(filename);
212 if (file.is_open()) {
213 nlohmann::json j = data;
214 file << j.dump(4);
215 }
216 }
217
223 void LoadProjectConfig(ProjectProperties& data, const std::string& filename) {
224 std::ifstream file(filename);
225 if (file.is_open()) {
226 try {
227 nlohmann::json j;
228 file >> j;
229 data = j;
230 } catch (const std::exception& e) {
231 //std::cerr << "JSON parsing error: " << e.what() << std::endl;
233 }
234 }
235 }
236
237 std::deque<std::unique_ptr<ICommand>> m_undoStack;
238 std::deque<std::unique_ptr<ICommand>> m_redoStack;
239 const size_t MAX_UNDO_STEPS = 50;
240
243 m_undoStack.push_back(std::unique_ptr<ICommand>(cmd));
244 if (m_undoStack.size() > MAX_UNDO_STEPS) m_undoStack.pop_front();
245 m_redoStack.clear();
246 }
247
249 std::string name;
250 std::string type;
251 std::unordered_map<std::string, nlohmann::json> components;
252 bool hasData = false;
253 } m_clipboard;
254
255 bool m_gizmoWasUsing = false;
256 glm::vec3 m_gizmoStartPos;
257 glm::vec3 m_gizmoStartRot;
258 glm::vec3 m_gizmoStartScale;
259
260 bool m_isAssetBrowserFocused = false;
261
262 glm::uvec2 m_viewportSize = {1280, 720};
263 std::unique_ptr<EditorCameraObject> m_camera;
264 std::unique_ptr<EditorMenuBar> m_editorMenuBar;
265 std::pair<bool, GameObject*> m_selectedObject = {false, nullptr};
266 std::unique_ptr<AssetBrowser> m_assetBrowser;
267 BrowserIcons m_icons;
268
269 std::string m_pendingSceneToLoad = "";
270 bool m_waitForGui = true;
271
272 std::string m_projectBinaryPath;
273
274 ImGuizmo::OPERATION m_currentGizmoOperation = ImGuizmo::TRANSLATE;
275 ImGuizmo::MODE m_currentGizmoMode = ImGuizmo::WORLD;
276 bool m_useSnap = false;
277 float m_snapValue[3] = { 0.5f, 0.5f, 0.5f };
278
279 bool m_isHoveringGizmoUI = false;
280
281 EditorProperties m_editorProperties;
282 EditorProperties m_SavedEditorProperties;
283
284 ProjectProperties m_projectProperties;
285 ProjectProperties m_SavedProjectProperties;
286
287 int m_fps = 0;
288
289 bool m_refresh = false;
290 bool showConsole = false;
291 };
292
293}
Class responsible for displaying and navigating project assets within an ImGUI window.
A specialized camera object for use within the editor, supporting free-fly movement and input process...
Command pattern implementation for Editor Undo/Redo system.
ImGUI component responsible for drawing the main editor menu bar and handling its actions.
Contains main Engine class.
Class responsible for displaying and navigating project assets within an ImGUI window.
void processEvent(const SDL_Event &event, float deltaTime) override
Overrides the Engine's processEvent function to handle editor-specific events (e.g....
Definition Editor.cpp:287
void LoadConfig(EditorProperties &data, const std::string &filename)
Loads the EditorProperties configuration from a JSON file.
Definition Editor.hpp:191
void LoadProjectConfig(ProjectProperties &data, const std::string &filename)
Loads the ProjectProperties configuration from a JSON file.
Definition Editor.hpp:223
bool IsEditor() override
Returns true if the engine is running in editor mode.
Definition Editor.hpp:121
void Undo()
Undoes the last action performed in the editor.
Definition Editor.cpp:145
void PushCommand(ICommand *cmd)
Pushes a command onto the undo stack and clears the redo stack.
Definition Editor.hpp:242
void DeleteSelectedObject()
Deletes the selected object from the scene.
Definition Editor.cpp:199
float raySphereIntersect(const glm::vec3 &rayOrigin, const glm::vec3 &rayDir, const glm::vec3 &sphereCenter, float sphereRadius)
Performs a ray-sphere intersection test.
Definition Editor.cpp:523
void refreshForObject() override
Overrides the base Engine function to trigger a frame and refresh.
Definition Editor.hpp:94
void SaveProjectConfig(const ProjectProperties &data, const std::string &filename)
Saves the current ProjectProperties configuration to a JSON file.
Definition Editor.hpp:210
void drawEditorLayout(const SceneRenderData &data, glm::uvec2 &outNewResolution)
Helper to draw the ImGUI dockspace and viewport window for the editor.
Definition Editor.cpp:604
void drawGizmo(const glm::vec2 &viewportPos, const glm::vec2 &viewportSize)
Draws the ImGuizmo for the currently selected object.
Definition Editor.cpp:408
ProjectProperties * getProjectProperties()
Gets a pointer to the mutable project properties.
Definition Editor.hpp:83
EditorProperties * getEditorProperties()
Gets a pointer to the mutable editor properties.
Definition Editor.hpp:79
void CopySelectedObject()
Copies the selected object to the clipboard.
Definition Editor.cpp:159
AssetBrowser * getAssetBrowser() const
Gets a pointer to the AssetBrowser instance.
Definition Editor.hpp:71
void Redo()
Redoes the last undone action in the editor.
Definition Editor.cpp:152
void OnHotReload()
Handler for post-hot-reload operations.
Definition Editor.cpp:939
void render() override
Overrides the Engine's render function to inject the Editor UI logic (dockspace, viewport,...
Definition Editor.cpp:291
void SaveConfig(const EditorProperties &data, const std::string &filename)
Saves the current EditorProperties configuration to a JSON file.
Definition Editor.hpp:178
void HandleMeshDrop(const std::string &filepath, vex::Entity parent=vex::NULL_ENTITY)
Handles the dropping of a mesh asset.
Definition Editor.cpp:572
void ExtractObjectByEntity(vex::Entity entity, std::pair< bool, vex::GameObject * > &selectedObject)
Retrieves the GameObject corresponding to a given entity ID and updates the selected object pair.
Definition Editor.cpp:560
void setFullscreen(bool enabled, bool exclusive=false) override
Overrides the Engine's setFullscreen function to disable auto fullscreen by gameModule.
Definition Editor.hpp:59
std::string getProjectBinaryPath() const
Gets the path to the project's binary directory.
Definition Editor.hpp:67
void DuplicateSelectedObject()
Duplicates the selected object in the scene.
Definition Editor.cpp:194
Editor(const char *title, int width, int height, GameInfo gInfo, const std::string &projectBinaryPath)
Constructs the Editor.
Definition Editor.cpp:36
const BrowserIcons & GetEditorIcons() const
Gets the collection of icons used within the editor's UI.
Definition Editor.hpp:75
void ShowConsole()
Shows the debug console.
Definition Editor.hpp:124
void ProcessEditorShortcuts()
Processes editor shortcuts.
Definition Editor.cpp:109
float rayTriangleIntersect(const glm::vec3 &rayOrigin, const glm::vec3 &rayDir, const glm::vec3 &v0, const glm::vec3 &v1, const glm::vec3 &v2)
Performs a ray-triangle intersection test (using the Möller–Trumbore algorithm).
Definition Editor.cpp:534
void PasteObjectToScene()
Pastes the object from the clipboard to the scene.
Definition Editor.cpp:177
void requestSceneReload(const std::string &scenePath)
Requests a scene reload with a specific path. The reload is typically executed in the main update loo...
Definition Editor.cpp:349
void loadAssetIcons()
Loads the texture icons used for the Asset Browser and other UI elements.
Definition Editor.cpp:355
void update(float deltaTime) override
Overrides the Engine's update function to handle editor-specific logic.
Definition Editor.cpp:212
Engine(const char *title, int width, int height, GameInfo gInfo)
Constructor for the Engine class.
Definition Engine.cpp:46
uint32_t Entity
Type alias representing a unique entity identifier in the ECS system.
Definition Types.hpp:11
void VEX_EXPORT handle_exception(const std::exception &e)
Handles an exception based on build configuration.
constexpr Entity NULL_ENTITY
Special entity value indicating an invalid or null entity.
Definition Types.hpp:15
Structure to hold all configurable editor settings and properties.
Structure to hold all configurable project settings and properties.
Structure holding texture IDs for various asset types displayed in the browser.
this struct contains information about the game. like project name and version.
Definition GameInfo.hpp:15
Base Command Interface.
Data structure to pass state between render stages.
Definition Renderer.hpp:57