Cogs.Core
EditorState.h
1#pragma once
2
3#include "Base.h"
4#include "Context.h"
5#include "EntityStore.h"
6
7#include "Resources/Resources.h"
8#include "Serialization/EntityWriter.h"
9
10#include "Foundation/ComponentModel/Entity.h"
11
12#include <string>
13#include <vector>
14#include <array>
15#include <regex>
16#include <span>
17#include <unordered_map>
18
19namespace Cogs::Core
20{
21 using EntityIds = std::vector<EntityId>;
22
23 enum class EditingMode
24 {
25 None = 0,
26 Select,
27 Translate,
28 Rotate,
29 Scale
30 };
31
32 enum class EditingPivot
33 {
34 ObjectPivot = 0,
35 ObjectCenter,
36 GroupCenter,
37 Default = ObjectCenter,
38 };
39
44 public:
45 virtual ~ICommandState() = default;
46 };
47
51 EntityId rootId = NoEntity;
52
54 std::string details;
55
57 std::string pickId;
58
60 std::string assetRootGroupName;
61
62 // Formatted bounding Box
63 std::string boundingBox;
64
66 void clear() {
67 rootId = NoEntity;
68 details.clear();
69 pickId.clear();
70 assetRootGroupName.clear();
71 boundingBox.clear();
72 }
73 };
74
76 {
77 EditingMode mode = EditingMode::Select;
78 EditingPivot pivot = EditingPivot::Default;
79
80 class Editor * editor = nullptr;
81 class Context * context = nullptr;
82
85
86 std::string fileName;
87 std::string directory;
88
89 LastPickState lastPickState;
90 EntityIds selected;
91 int pickId = -1;
92 bool scrolled = false;
93
94 bool findEntities = false;
95 size_t findOffset = 0;
96
97 bool showSettings = false;
98
100 void setSelection(const EntityIds& ids) {
101 selected = ids;
102 if (ids.size() != 1) this->pickId = -1;
104 }
105
106 void setSelection(EntityId id) {
107 selected = { id };
109 }
110
111 void clearSelection() {
112 selected.clear();
114 // lastPickState.clear();
115 }
116
118 void updateSelectedInfo();
119
120 size_t numSelected() const { return selected.size(); }
121
122 bool hasSelected() const { return !selected.empty(); }
123
124 bool isSelected(EntityId entityId) const {
125 for (auto & id : selected) { if (id == entityId) return true; }
126 return false;
127 }
128
130 EntityId getSelectedId() const { return numSelected() == 1U ? selected.back() : NoEntity; }
131
133 ComponentModel::Entity * getSelected() const { return getSelectedId() != NoEntity ? context->store->getEntityPtr(getSelectedId()) : nullptr; }
134
136 std::span<const EntityId> getAllSelectedIds() const { return this->selected; }
137
139 void setFindPattern(const std::string& pattern)
140 {
141 findPattern = pattern;
142 try {
143 findRegex = pattern;
144 validRegex = true;
145 }
146 catch (std::regex_error) {
147 findRegex = std::string();
148 validRegex = false;
149 }
150 }
151
153 const std::string& getFindPattern() const { return findPattern; }
155 const std::regex& getFindRegex() const { return findRegex; }
156 bool isFindRegexValid() const { return validRegex; }
157
158
161 std::string eraseInvalidSelected() {
162 std::string erasedSelections;
163 for (size_t i = selected.size(); i > 0; --i) {
164 const size_t index = i - 1u;
165 if (!context->store->getEntityPtr(selected[index])) {
166 erasedSelections += ' ' + std::to_string(selected[index]);
167 selected.erase(selected.begin() + index);
168 }
169 }
170
171 if (!erasedSelections.empty()) {
173 }
174
175 return erasedSelections;
176 }
177
178 std::vector<ComponentModel::Entity *> selectedPath;
179
180 std::unordered_map<int, TextureHandle> icons;
181
182 glm::vec3 startTranslation = glm::vec3(0);
183 glm::vec3 endTranslation = glm::vec3(0);
184
185 glm::quat startRotation;
186 glm::quat endRotation;
187
188 glm::vec3 startScale;
189 glm::vec3 endScale;
190
191 std::vector<glm::vec3> startTranslations;
192 std::vector<glm::quat> startRotations;
193 std::vector<glm::vec3> startScales;
194
195 glm::vec3 startMin;
196 glm::vec3 startMax;
197
198 bool wasUsingGizmo = false;
199
200 bool lButtonDown = false;
201
202 int downX = 0;
203 int downY = 0;
204
205 rapidjson::Document copied;
206
207 float translateSnap = 0.1f;
208 float rotateSnap = 1.0f;
209 float scaleSnap = 0.1f;
210
213 std::vector<std::shared_ptr<ICommandState>> commandStates;
214 private:
215 std::string findPattern;
216 std::regex findRegex;
217 bool validRegex = true;
218 };
219}
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
class EntityStore * store
Entity store.
Definition: Context.h:231
ComponentModel::Entity * getEntityPtr(const EntityId entityId)
Get a raw pointer to the entity with the given id.
Contains the Engine, Renderer, resource managers and other systems needed to run Cogs....
std::weak_ptr< ComponentModel::Entity > WeakEntityPtr
Weak Smart pointer for Entity access.
Definition: EntityPtr.h:18
std::vector< std::shared_ptr< ICommandState > > commandStates
Definition: EditorState.h:213
EntityId getSelectedId() const
Gets entity ID of the single selected entity.
Definition: EditorState.h:130
void updateSelectedInfo()
Update information in LastPickedState when selection changes.
Definition: Editor.cpp:2970
std::string eraseInvalidSelected()
Definition: EditorState.h:161
void setFindPattern(const std::string &pattern)
Entity search pattern.
Definition: EditorState.h:139
const std::regex & getFindRegex() const
Entity search pattern as Regexp for lookup.
Definition: EditorState.h:155
const std::string & getFindPattern() const
Gets Entity search pattern.
Definition: EditorState.h:153
std::span< const EntityId > getAllSelectedIds() const
Get Entity Ids of all selected entities.
Definition: EditorState.h:136
ComponentModel::Entity * getSelected() const
Gets entity Pointer to the single selected entity. Nullptr if not one selected or not found.
Definition: EditorState.h:133
WeakEntityPtr editorCamera
Editor scene camera.
Definition: EditorState.h:84
void setSelection(const EntityIds &ids)
Set new selected entities.
Definition: EditorState.h:100
Shows information about last picking.
Definition: EditorState.h:49
EntityId rootId
Root entity ID.
Definition: EditorState.h:51
std::string assetRootGroupName
Asset root name (Name of root Group node from rvmparser - grandchild).
Definition: EditorState.h:60
std::string pickId
Pick details. Picked Texture ID.
Definition: EditorState.h:57
std::string details
Pick details. AssetTag.
Definition: EditorState.h:54
void clear()
Clear Picking information.
Definition: EditorState.h:66