Cogs.Core
Editor.h
1#pragma once
2
3#include "Editor/IEditor.h"
4#include "EventBasedInput.h"
5
6#include "EditorExtension.h"
7
8#include "Commands/EditorCommand.h"
9#include "Foundation/Geometry/BoundingBox.hpp"
10
11#include <memory>
12#include <functional>
13#include <vector>
14
16{
17 class Entity;
18}
19
20namespace Cogs::Core
21{
22 class Context;
23 struct FieldInfo;
24
25 using CommandCreator = std::function<EditorCommand* (EditorState*)>;
26
28 {
29 std::string key;
30 CommandCreator creator;
31 };
32
34 {
35 std::string name;
36 std::string key;
37 };
38
39
40 enum class Icon
41 {
42 None = 0,
43 New,
44 Open,
45 Save,
46
47 Undo,
48 Redo,
49
50 Select,
51 Move,
52 Rotate,
53 Scale,
54
55 Plus,
56 Minus,
57 Info,
58 Gears,
59
60 Eye,
61
62 Solid,
64 BoundingBox,
65
66 PivotLocal,
67 PivotCenter,
68
69 Hide,
70 HideUnselected,
71 ShowAll,
72 SelectFurthestAway,
73 IconInvertVisibility
74 };
75
76 class Editor : public IEditor
77 {
78 public:
79 explicit Editor(Context * context);
80 ~Editor() override;
81
82 static void COGSCORE_EDITOR_API registerExtensionCommand(const StringView & key, CommandCreator creator);
83 static COGSCORE_EDITOR_API std::vector<ExtensionCommand> & getExtensionCommands();
84
85 static void COGSCORE_EDITOR_API registerExtensionItem(const StringView & key, const StringView & name);
86 static COGSCORE_EDITOR_API std::vector<ExtensionItem> & getExtensionItems();
87
88 void initialize() override;
89 void cleanup() override;
90
91 void show() override;
92 void processShortcuts();
93
94 void beginFrame() override;
95
96 bool isActive() const override { return active; }
97
98 EditorState * getState() override;
99
101 void showText(const char* format, ...) const;
102 bool showModel(ModelHandle & modelHandle);
103 bool showTexture(const StringView & header, TextureHandle & textureHandle) override;
104 bool showMaterialInstance(FieldInfo & fieldInfo, MaterialInstanceHandle & materialInstanceHandle);
105
106 bool imageButton(Icon icon, int size, int padding);
107
108 void clear(bool interactive=true) override;
109
116 void open(const StringView & path, bool openInRoot, bool synchronous) override;
117 void save() override;
118 void saveAs() override;
119 void saveIncremental() override;
120
121 void importStatic();
122 void exportEntities(bool exportScene = false);
123
124 void cut() override;
125 void copy() override;
126 void paste() override;
127
128 void invokeCommand(const StringView & key) override;
129
130 void destroy() override;
131 void destroyPermanently();
132
133 void changeMode(EditingMode mode);
134 void focusSelected();
135 void focusAll();
136 bool focusBoundingBox(const Cogs::Geometry::DBoundingBox& boundingBox);
137
140 void setStatusbarText(const std::string& text, bool guiStatus);
141
143 void showHide(bool visible);
144
146 void hideUnselected();
147
149 void showAll();
150
152 void selectFurthestAway();
153
156 void invertVisibility();
157
158 void selectAll();
159 void selectOne(EntityId id);
160
161 void clearCommands();
162
163 void onlineHelp(bool generalHelp);
164
165 void showLicenses();
166 void showAbout();
167
168 EditorCommand * doApply(std::unique_ptr<EditorCommand>&& newCommand)
169 {
170 modalCommand = dynamic_cast<ModalEditorCommand*>(newCommand.get());
171 if (modalCommand) {
172 modalCommand->beginModal();
173 }
174 else {
175 newCommand->apply();
176 if (commands.size() && commands.back()->merge(newCommand.get())) {
177 return commands.back().get();
178 }
179 }
180
181 commands.resize(commandIndex + 1);
182 commands.emplace_back(std::move(newCommand));
183 ++commandIndex;
184
185 return commands.back().get();
186 }
187
188
189 template<typename Command, typename... Args>
190 Command * apply(Args... args)
191 {
192 assert(modalCommand == nullptr);
193 auto newCommand = std::make_unique<Command>(state.get(), std::move(args)...);
194 return (Command*)doApply(std::move(newCommand));
195 }
196
197 template<typename Command, typename... Args>
198 Command * applySelected(Args... args)
199 {
200 return apply<Command, EntityIds, Args...>(state->selected, std::move(args)...);
201 }
202
203 void undo();
204 void redo();
205
206 private:
207 void handleMouseEvent(const struct Cogs::Core::InputEvent& event);
208 void showEntities();
209 void showEntityDetails(ComponentModel::Entity * entity);
210 void showGizmo();
211 void selectLayer(ComponentModel::Entity* entity, StringView layer);
212 void setLayerVisibility(ComponentModel::Entity* entity, StringView layer, bool visible);
213
215 bool getLayerVisibility(const ComponentModel::Entity* entity, StringView layer);
216
219 bool isStandardEntity(const Cogs::ComponentModel::Entity* entity) const;
220
222 bool isUserRootEntity(const Cogs::ComponentModel::Entity* entity) const;
223
224 // If not interactive, don't present user with open dialogs or options
225 void openInternal(const StringView& path, bool openInRoot, bool synchronous, bool interactive);
226
227 private:
228 Context * context;
229
230 std::unique_ptr<struct EditorState> state;
231
232 ModalEditorCommand* modalCommand = nullptr;
233
234 std::vector<std::unique_ptr<EditorCommand>> commands;
235 static constexpr size_t NoCommand = static_cast<size_t>(-1);
236 size_t commandIndex = NoCommand;
237
238 std::unique_ptr<class MaterialEditor> materialEditor;
239
241 std::vector<Cogs::Core::WeakEntityPtr> standardEntities;
242
243 mutable int textElementId = 0;
244
245 bool initialized = false;
246 bool active = false;
247
248 float minWidth = 50;
249 float sidebarWidth = 200;
250 float inspectorWidth = 250;
251 float menuHeight = 20;
252 float toolbarHeight = 40;
253 glm::vec2 buttonSize = glm::vec2{ 24 };
254
255 float statusbarHeight = 20;
256 bool showStatusbar = true;
257 std::string statusbarText;
258 bool guiStatusInStatusbar = true;
259
260 bool showToolbar = true;
261 bool showSidebar = true;
262 bool showInspector = true;
263 bool showMenu = true;
264
265 private:
266 const std::string& getStatusbarText() const { return statusbarText; }
267 bool showingGuiStatusbarText() const { return guiStatusInStatusbar; }
268 float getStatusbarHeight() const { return showStatusbar ? statusbarHeight : 0.0f; }
269 float getToolbarHeight() const { return showToolbar ? toolbarHeight : 0.0f; }
270 float getSidebarWidth() const { return showSidebar ? sidebarWidth : 0.0f; }
271 float getInspectorWidth() const { return showInspector ? inspectorWidth : 0.0f; }
272 float getMenuHeight() const { return showMenu ? menuHeight : 0.0f; }
273
274 virtual void exportScene(const StringView & path) override;
275 virtual EntityIds getSelected() const override;
276 virtual void select(const EntityIds & ids, SelectMode mode = SelectMode::Exclusive) override;
277 virtual EditorCommand * createCommand(const StringView & key) override;
278 virtual void createEntity(const StringView & type, const StringView & name) override;
279};
280}
Container for components, providing composition of dynamic entities.
Definition: Entity.h:18
A Context instance contains all the services, systems and runtime components needed to use Cogs.
Definition: Context.h:83
bool getLayerVisibility(const ComponentModel::Entity *entity, StringView layer)
Returns true if any Node in layer is visible.
Definition: Editor.cpp:2072
std::vector< Cogs::Core::WeakEntityPtr > standardEntities
Set of entities created when the Editor loads.
Definition: Editor.h:241
bool isUserRootEntity(const Cogs::ComponentModel::Entity *entity) const
Checks if the entity is.
Definition: Editor.cpp:2109
void showAll()
Make all entities visible.
Definition: Editor.cpp:2791
void showHide(bool visible)
Show or hide selected entity and all children.
Definition: Editor.cpp:2740
void showText(const char *format,...) const
Show the same type of label as ImGui::Text, but with the added ability to highlight and copy text....
Definition: Editor.cpp:1831
void selectFurthestAway()
Search and select visible entity furthest away from selection. Ie.e. find outliers.
Definition: Editor.cpp:2813
void invertVisibility()
Definition: Editor.cpp:2871
void open(const StringView &path, bool openInRoot, bool synchronous) override
Open file.
Definition: Editor.cpp:2348
void setStatusbarText(const std::string &text, bool guiStatus)
Definition: Editor.cpp:2675
void hideUnselected()
Make all non-selected entities invisible.
Definition: Editor.cpp:2761
bool isStandardEntity(const Cogs::ComponentModel::Entity *entity) const
Definition: Editor.cpp:2093
Provides a weakly referenced view over the contents of a string.
Definition: StringView.h:24
Contains code for composing and managing entities built from components.
Contains the Engine, Renderer, resource managers and other systems needed to run Cogs....
@ Wireframe
Wireframe rendering.
@ Solid
Opaque geometry in arbitrary order.
Base class for Cogs Editor commands.
Definition: EditorCommand.h:19
Event input queue event. Contains either a keyboard or mouse event. Keyboard event:
Abstract base class for all editor commands showing GUI in interactive mode.
Definition: EditorCommand.h:40
virtual void beginModal()=0
Called when the command is initiated.