Polyphase Game Engine
Loading...
Searching...
No Matches
EditorState.h
Go to the documentation of this file.
1#pragma once
2
3#if EDITOR
4
5#include <string>
6#include <set>
7#include <unordered_map>
8#include <vector>
9#include "Maths.h"
10#include "AssetRef.h"
11#include "SmartPointer.h"
12#include "Property.h"
13#include "Nodes/Widgets/Text.h"
14#include "imgui.h"
15#include "./ImGuizmo/ImGuizmo.h"
16
17class Node;
18class Scene;
19class Timeline;
21class Widget;
22class Text;
23class Asset;
24struct AssetStub;
25class AssetDir;
26class ActionList;
27class Canvas;
28class Camera3D;
29class Viewport3D;
30class Viewport2D;
31class PaintManager;
32class VoxelSculptManager;
33class TilePaintManager;
34struct SubSceneOverride;
35
36enum class ControlMode
37{
38 Default,
39 Pilot,
41 Rotate,
42 Scale,
43 Pan,
44 Orbit
45};
46
47enum class TransformLock
48{
49 None,
50 AxisX,
51 AxisY,
52 AxisZ,
53 PlaneYZ,
54 PlaneXZ,
55 PlaneXY,
56 Count
57};
58
59enum class EditorMode
60{
61 Scene,
62 Scene2D,
63 Scene3D,
64
65 Count
66};
67
68enum class PaintMode
69{
70 None,
71 Color,
72 Instance,
73 Voxel,
74 Terrain,
75 TilePaint,
76
77 Count
78};
79
80enum class AssetBrowserTab
81{
82 Project = 0,
83 Addons = 1,
84 Count
85};
86
87struct LinkedSceneProps
88{
89 Node* mNode = nullptr;
90 std::vector<Property> mProps;
91 std::vector<SubSceneOverride> mSubSceneOverrides;
92};
93
94struct EditScene
95{
96 SceneRef mSceneAsset;
97 NodePtr mRootNode;
98 glm::mat4 mCameraTransform;
99 std::vector<LinkedSceneProps> mLinkedSceneProps;
100};
101
102struct RecentScene
103{
104 std::string mSceneName;
105 uint64_t mTimestamp = 0;
106};
107
108// Per-asset state captured at BeginPlayInEditor and re-applied at
109// EndPlayInEditor so script mutations during play don't leak into the editor.
110struct PieAssetSnapshot
111{
112 std::vector<uint8_t> mBytes;
113 bool mWasDirty = false;
114};
115
116struct EditorState
117{
118 // Data
119 EditorMode mMode;
120 std::vector<Node*> mSelectedNodes;
121 Node* mShiftSelectAnchor = nullptr;
122 int32_t mSelectedInstance = -1;
123 std::vector<EditScene> mEditScenes;
124 AssetStub* mSelectedAssetStub = nullptr;
125 std::vector<AssetStub*> mSelectedAssetStubs;
126 ControlMode mControlMode = ControlMode::Default;
127 TransformLock mTransformLock = TransformLock::None;
128 SharedPtr<Camera3D> mEditorCamera;
129 float mPerspectiveNearZ = 0.25f;
130 float mPerspectiveFarZ = 4096.0f;
131 float mPerspectiveFov = 70.0f;
132 float mOrthoNearZ = -2048.0f;
133 float mOrthoFarZ = 2048.0f;
134 float mOrthoWidth = 12.8f;
135 bool mMouseNeedsRecenter = false;
136 bool mUiEnabled = true;
137 bool mPlayInEditor = false;
138 bool mPlayInGameWindow = false;
139 bool mEjected = false;
140 bool mPaused = false;
141 bool mHasEjectedOnce = false;
142 bool mSavedGridEnabled = false;
143 glm::vec4 mSavedEditorClearColor = glm::vec4(0.0f, 0.0f, 0.0f, 1.0f);
144 int32_t mSavedWindowRect[4] = {}; // x, y, w, h — saved before Play Full Screen resize
145 int32_t mEditSceneIndex = -1;
146 int32_t mPieEditSceneIdx = -1;
147 std::unordered_map<Asset*, PieAssetSnapshot> mPieAssetSnapshots;
148 // Lazy cache of instantiated default trees for sub-scene source assets,
149 // used by the inspector to detect overridden properties and revert them.
150 // Keyed by source Scene*. Invalidated when an EditScene closes, on
151 // editor shutdown, and whenever a source Scene is reimported/saved.
152 std::unordered_map<Scene*, NodePtr> mInspectorDefaultCache;
153 // Deferred dispatch for long-running editor operations: the click/hotkey
154 // handler sets the appropriate *AtEndOfFrame flag, and EditorMain.cpp
155 // picks them up *after* EditorImguiDraw has returned and runs the work
156 // synchronously. The EditorProgress modal pumps additional ImGui frames
157 // during the work so the user sees animated progress instead of a freeze.
158 bool mBeginPieAtEndOfFrame = false;
159 bool mEndPieAtEndOfFrame = false;
160 bool mSaveSceneAtEndOfFrame = false;
161 bool mSaveSelectedAssetAtEndOfFrame = false;
162 bool mResaveAllAtEndOfFrame = false;
163 bool mReloadScriptsAtEndOfFrame = false;
164 bool mOpenProjectAtEndOfFrame = false;
165 std::string mPendingOpenProjectPath;
166 // Deferred OpenScene. Either a stub (preferred -- path is captured at
167 // request time so a later directory rename doesn't break it) or empty
168 // to trigger the OS file dialog inside the worker. Scene* is held as
169 // a raw pointer; the asset map owns the lifetime and the dispatcher
170 // runs same-tick so dangling is not realistic.
171 bool mOpenSceneAtEndOfFrame = false;
172 AssetStub* mPendingOpenSceneStub = nullptr;
173
174 // Generic progress modal state, consumed by DrawProgressModal and driven
175 // by the EditorProgress::* API. Negative fraction = indeterminate sine
176 // marquee; non-negative = determinate 0..1 fill.
177 bool mProgressActive = false;
178 bool mProgressCancellable = false;
179 bool mProgressCancelRequested = false;
180 std::string mProgressTitle;
181 std::string mProgressStatus;
182 float mProgressFraction = -1.0f;
183 double mProgressLastPumpTime = 0.0;
184 AssetBrowserTab mActiveAssetTab = AssetBrowserTab::Project;
185 AssetDir* mTabCurrentDir[(int)AssetBrowserTab::Count] = {};
186 std::vector<AssetDir*> mTabDirPast[(int)AssetBrowserTab::Count];
187 std::vector<AssetDir*> mTabDirFuture[(int)AssetBrowserTab::Count];
188 std::string mTabFilterStr[(int)AssetBrowserTab::Count];
189 std::vector<AssetStub*> mTabFilteredStubs[(int)AssetBrowserTab::Count];
190 std::vector<Object*> mInspectPast;
191 std::vector<Object*> mInspectFuture;
192 Object* mInspectedObject;
193 Object* mPrevInspectedObject;
194 AssetRef mInspectedAsset;
195 bool mInspectLocked = false;
196 Viewport3D* mViewport3D = nullptr;
197 Viewport2D* mViewport2D = nullptr;
198 std::string mIOAssetPath;
199 bool mRequestSaveSceneAs = false;
200 bool mTrackSelectedAsset = false;
201 bool mTrackSelectedNode = false;
202 std::string mRevealScriptName = "";
203 std::set<AssetDir*> mRevealAssetExpandDirs;
204 uint32_t mViewportX = 0;
205 uint32_t mViewportY = 0;
206 uint32_t mViewportWidth = 100;
207 uint32_t mViewportHeight = 100;
208 glm::uvec4 mPrevViewport = {};
209 bool mShowLeftPane = true;
210 bool mShowRightPane = true;
211 bool mShowInterface = true;
212 bool mPreviewLighting = true;
213 SharedPtr<Text> mOverlayText = nullptr;
214 std::vector<std::string> mFavoritedDirs;
215 std::vector<std::string> mRecentProjects;
216 std::vector<RecentScene> mRecentScenes;
217 PaintMode mPaintMode = PaintMode::None;
218 PaintManager* mPaintManager = nullptr;
219 VoxelSculptManager* mVoxelSculptManager = nullptr;
220 class TerrainSculptManager* mTerrainSculptManager = nullptr;
221 TilePaintManager* mTilePaintManager = nullptr;
222
223 // Tile-paint mode stashes the editor camera's previous projection AND
224 // transform so it can switch to a top-down orthographic view of the
225 // currently-selected TileMap2D and restore the original camera on exit.
226 bool mTilePaintProjectionStashed = false;
227 bool mTilePaintPrevWasPerspective = true;
228 bool mTilePaintTransformStashed = false;
229 glm::vec3 mTilePaintPrevCameraPosition = { 0.0f, 0.0f, 0.0f };
230 glm::quat mTilePaintPrevCameraRotation = { 1.0f, 0.0f, 0.0f, 0.0f };
231 bool mNodePropertySelect = false;
232 int32_t mNodePropertySelectIndex = 0;
233 std::string mNodePropertySelectName = "";
234 std::string mPendingSceneImportPath = "";
235 AssetStub* mPendingReimportSceneStub = nullptr;
236 std::string mPendingReimportScenePath = "";
237 bool mShutdownUnsavedCheck = false;
238 bool mDevMode = false;
239 bool mShowBottomPane = true;
240 float mBottomPaneHeight = 180.0f;
241 bool mShowProjectUpgradeModal = false;
242 bool mProjectUpgradeInProgress = false;
243 std::vector<AssetStub*> mAssetsNeedingUpgrade;
244
245 // 3DS Preview Panel state
246 bool mShow3DSPreview = false;
247 int32_t mSceneScreenFilter = -1; // -1 = All Screens, 0 = Top Screen, 1 = Bottom Screen
248
249 // Game Preview Panel state
250 bool mShowGamePreview = false;
251 bool mGamePreviewCaptured = false;
252
253 // Play target (shared between viewport toolbar and Game Preview)
254 int32_t mPlayTarget = 0; // 0=PlayInEditor, 1=PlayFullScreen, 2=Dolphin, 3=Azahar, 4=Standalone, 5=Send3dsLink
255
256 // Node Graph Panel state
257 bool mShowNodeGraphPanel = false;
258
259 // Timeline Panel state
260 bool mShowTimelinePanel = false;
261
262 // Profiling Panel state
263 bool mShowProfilingPanel = false;
264
265 // Input Tester Panel state
266 bool mShowInputTesterPanel = false;
267
268 // Texture Atlas Viewer state
269 bool mShowTextureAtlasViewer = false;
270
271 // Git Panel state
272 bool mShowGitPanel = false;
273
274 // Animation Browser Panel state
275 bool mShowAnimationBrowser = false;
276 TimelineRef mEditedTimelineRef;
277 TimelineInstance* mTimelinePreviewInstance = nullptr;
278 float mTimelinePlayheadTime = 0.0f;
279 bool mTimelinePreviewing = false;
280 float mTimelineZoom = 100.0f;
281 float mTimelineScrollX = 0.0f;
282 float mTimelineSnapInterval = 0.1f;
283 int32_t mTimelineSelectedTrack = -1;
284 int32_t mTimelineSelectedClip = -1;
285 int32_t mTimelineSelectedKeyframe = -1;
286
287 // ImGuizmo state
288 ImGuizmo::OPERATION mGizmoOperation = ImGuizmo::TRANSLATE;
289 ImGuizmo::MODE mGizmoMode = ImGuizmo::WORLD;
290 bool mGizmoBlockedBySelection = false;
291
292 // Methods
293 void Init();
294 void Shutdown();
295 void Update(float deltaTime);
296
297 void GatherProperties(std::vector<Property>& props);
298
299 void SetEditorMode(EditorMode mode);
300 EditorMode GetEditorMode();
301
302 void SetPaintMode(PaintMode paintMode);
303 PaintMode GetPaintMode();
304
305 void ReadEditorSave();
306 void WriteEditorSave();
307
308 void ReadEditorProjectSave();
309 void WriteEditorProjectSave();
310
311 void HandleNodeDestroy(Node* node);
312
313 void SetSelectedNode(Node* newNode);
314 void AddSelectedNode(Node* node, bool addAllChildren);
315 void SelectNodesInRange(Node* rootNode, Node* endNode);
316 void RemoveSelectedNode(Node* node);
317 void SetSelectedAssetStub(AssetStub* newStub);
318 void AddSelectedAssetStub(AssetStub* stub);
319 void RemoveSelectedAssetStub(AssetStub* stub);
320 void ClearSelectedAssetStubs();
321 const std::vector<AssetStub*>& GetSelectedAssetStubs();
322 bool IsAssetStubSelected(AssetStub* stub);
323 void SetControlMode(ControlMode newMode);
324
325 void BeginPlayInEditor();
326 void EndPlayInEditor();
327 // Defer the start/stop by one frame so the loading modal can render
328 // before the (potentially slow) snapshot/clone or restore work runs.
329 void RequestBeginPlayInEditor();
330 void RequestEndPlayInEditor();
331 // Defer a Lua "Reload All Scripts" until end-of-frame so the progress
332 // modal can render before the script reload loop blocks. The worker is
333 // the global ::ReloadAllScripts() in Engine.cpp.
334 void RequestReloadAllScripts();
335 void EjectPlayInEditor();
336 void InjectPlayInEditor();
337 void SnapshotAssetsForPie();
338 void RestoreAssetsFromPie();
339
340 // Sub-scene override inspector helpers. The cache holds an instantiated
341 // copy of each source Scene asset that's currently being inspected; the
342 // inspector compares the live node's property values against the
343 // corresponding default node in the cached tree to detect overrides.
344 // Pass nullptr to InvalidateSubSceneDefaultCache to clear everything.
345 Node* GetSubSceneDefaultTree(Scene* src);
346 void InvalidateSubSceneDefaultCache(Scene* src = nullptr);
347 bool IsPropertyOverridden(Node* node, const std::string& propName);
348 void RevertPropertyToSource(Node* node, const std::string& propName);
349 void SetPlayInEditorPaused(bool paused);
350 bool IsPlayInEditorPaused();
351
352 void SetNodePropertySelect(bool enable, int32_t index, const std::string& propName);
353 void ClearNodePropertySelect();
354 void AssignNodePropertySelect(Node* targetNode);
355
356 Camera3D* GetEditorCamera();
357 void ToggleEditorCameraProjection();
358 void ApplyEditorCameraSettings();
359
360 void LoadStartupScene();
361
362 Node* GetSelectedNode();
363 Widget* GetSelectedWidget();
364 const std::vector<Node*>& GetSelectedNodes();
365 bool IsNodeSelected(Node* node);
366 void DeselectNode(Node* node);
367 int32_t GetSelectedInstance();
368 void SetSelectedInstance(int32_t instance);
369 //void ShowTextPrompt(const char* title, TextFieldHandlerFP confirmHandler, const char* defaultText = nullptr);
370
371 void OpenEditScene(Scene* scene);
372 void OpenEditScene(int32_t idx);
373 void CloseEditScene(int32_t idx);
374 void ShelveEditScene();
375 EditScene* GetEditScene(int32_t idx = -1);
376 void CloseAllEditScenes();
377 void EnsureActiveScene();
378
379 void ShowEditorUi(bool show);
380
381 Asset* GetSelectedAsset();
382 AssetStub* GetSelectedAssetStub();
383 ControlMode GetControlMode();
384 glm::vec3 GetTransformLockVector(TransformLock lock);
385 void SetTransformLock(TransformLock lock);
386
387 Object* GetInspectedObject();
388 Node* GetInspectedNode();
389 Asset* GetInspectedAsset();
390 void InspectObject(Object* obj, bool force = false, bool recordHistory = true);
391 void LockInspect(bool lock);
392 bool IsInspectLocked();
393 void RecordInspectHistory();
394 void ClearInspectHistory();
395 void RemoveFromInspectHistory(Object* obj);
396 void ProgressInspectFuture();
397 void RegressInspectPast();
398 void ClearAssetDirHistory();
399 void SetAssetDirectory(AssetDir* assetDir, bool recordHistory);
400 AssetDir* GetAssetDirectory();
401 void BrowseToAsset(const std::string& name);
402 void BrowseToScript(const std::string& scriptName);
403
404 void CaptureAndSaveScene(AssetStub* stub, Node* rootNode);
405 void DuplicateAsset(AssetStub* srcStub, const char* overrideName = nullptr);
406
407 void ProgressDirFuture();
408 void RegressDirPast();
409
410 void RemoveFilteredAssetStub(AssetStub* stub);
411 int ActiveTab() const { return (int)mActiveAssetTab; }
412
413 Viewport3D* GetViewport3D();
414 Viewport2D* GetViewport2D();
415
416 bool IsDirFavorited(const std::string& dirPath);
417 void AddFavoriteDir(const std::string& dirPath);
418 void RemoveFavoriteDir(const std::string& dirPath);
419
420 void AddRecentProject(const std::string& projPath);
421 void AddRecentScene(const std::string& sceneName);
422};
423
424EditorState* GetEditorState();
425
426#endif
bool Update()
Definition Engine.cpp:710
void Shutdown()
Definition Engine.cpp:916
Definition AssetDir.h:11
Definition AssetRef.h:18
Definition Asset.h:113
Definition Camera3d.h:12
Definition Canvas.h:9
Definition Node.h:67
Definition Object.h:13
Definition Scene.h:36
Definition Text.h:24
Definition TimelineInstance.h:32
Definition Timeline.h:10
Definition Viewport2d.h:33
Definition Viewport3d.h:7
Definition Widget.h:53
Definition Asset.h:82
Definition Scene.h:13