Hymn to Beauty
C++ 3D Engine
EntityEditor.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <memory>
4#include <vector>
5#include <string>
6#include <functional>
8#include <imgui.h>
9#include "../ResourceSelector.hpp"
10
11namespace Component {
12class Mesh;
13class Camera;
14class Material;
15class DirectionalLight;
16class PointLight;
17class SpotLight;
18class Listener;
19class RigidBody;
20class Script;
21class Shape;
22class SoundSource;
23class Sprite;
24class Trigger;
25} // namespace Component
26
27namespace GUI {
28class IShapeEditor;
29class RigidBodyEditor;
30class TriggerEditor;
31
34 public:
37
40
42 void Show();
43
45
48 void SetEntity(Entity* entity);
49
51
55
57
61 bool ShowsEntity(Entity* entity);
62
64
67 bool IsVisible() const;
68
70
73 void SetVisible(bool visible);
74
75 private:
76 template <typename type> void AddEditor(const std::string& name, std::function<void(type*)> editorFunction);
77 template <typename type> void AddComponent(const std::string& name);
78 template <typename type> void EditComponent(const std::string& name, std::function<void(type*)> editorFunction);
79
80 // Editors
81 void MeshEditor(Component::Mesh* mesh);
82 void CameraEditor(Component::Camera* camera);
83 void MaterialEditor(Component::Material* material);
84 void DirectionalLightEditor(Component::DirectionalLight* directionalLight);
85 void PointLightEditor(Component::PointLight* pointLight);
86 void SpotLightEditor(Component::SpotLight* spotLight);
87 void ListenerEditor(Component::Listener* listener);
89 void ScriptEditor(Component::Script* script);
90 void ShapeEditor(Component::Shape* shape);
91 void SoundSourceEditor(Component::SoundSource* soundSource);
92 void SpriteEditor(Component::Sprite* sprite);
93 void TriggerEditor(Component::Trigger* trigger);
94
95 Entity* entity = nullptr;
96 bool visible = false;
97 char name[128];
98 char stringPropertyBuffer[128];
99 glm::vec3 position;
100 glm::vec3 scale;
101
102 struct Editor {
103 std::function<void()> addFunction;
104 std::function<void()> editFunction;
105 };
106 std::vector<Editor> editors;
107 std::vector<IShapeEditor*> shapeEditors;
108 int selectedShape = -1;
109
110 std::unique_ptr<GUI::RigidBodyEditor> rigidBodyEditor;
111
112 ResourceSelector resourceSelector;
113 std::unique_ptr<GUI::TriggerEditor> triggerEditor;
114
115 bool albedoShow = false;
116 bool normalShow = false;
117 bool roughnessMetallicShow = false;
118
119 bool textureShow = false;
120
121 bool cameraLayers[32];
122 bool meshLayers[32];
123 bool spriteLayers[32];
124};
125} // namespace GUI
126
127template <typename type> void GUI::EntityEditor::AddEditor(const std::string& name, std::function<void(type*)> editorFunction) {
128 Editor editor;
129 editor.addFunction = std::bind(&EntityEditor::AddComponent<type>, this, name);
130 editor.editFunction = std::bind(&EntityEditor::EditComponent<type>, this, name, editorFunction);
131 editors.push_back(editor);
132}
133
134template <typename type> void GUI::EntityEditor::AddComponent(const std::string& name) {
135 if (entity->GetComponent<type>() == nullptr) {
136 if (ImGui::Selectable(name.c_str())) {
137 entity->AddComponent<type>();
138 SetEntity(entity);
139 }
140 }
141}
142
143template <typename type> void GUI::EntityEditor::EditComponent(const std::string& name, std::function<void(type*)> editorFunction) {
144 type* component = entity->GetComponent<type>();
145 if (component != nullptr && ImGui::CollapsingHeader(name.c_str())) {
146 ImGui::PushID(name.c_str());
147
148 editorFunction(component);
149
150 if (ImGui::Button("Remove"))
151 entity->KillComponent<type>();
152
153 ImGui::PopID();
154 }
155}
Component handling a camera through which the world can be rendered.
Definition: Camera.hpp:8
Component describing a directional light source (like the sun).
Definition: DirectionalLight.hpp:8
Component describing a listener that can listen to sounds.
Definition: Listener.hpp:7
Component providing a material to Entity.
Definition: Material.hpp:9
Component providing geometry to an entity.
Definition: Mesh.hpp:11
Component describing a point light.
Definition: PointLight.hpp:8
Component that allows interacting with other physics components.
Definition: RigidBody.hpp:19
Component controlled by a script.
Definition: Script.hpp:13
Definition: Shape.hpp:15
Component describing a sound source.
Definition: SoundSource.hpp:11
Component describing a spot light.
Definition: SpotLight.hpp:8
Component defining a 2D sprite.
Definition: Sprite.hpp:11
Component defining a trigger event.
Definition: Trigger.hpp:14
Handles the main editor.
Definition: Editor.hpp:24
Editor(Utility::Window *window, Video::LowLevelRenderer *lowLevelRenderer)
Create new editor.
Definition: Editor.cpp:40
Entity containing various components.
Definition: Entity.hpp:16
Used to edit an entity.
Definition: EntityEditor.hpp:33
EntityEditor()
Create new entity editor.
Definition: EntityEditor.cpp:46
~EntityEditor()
Destructor.
Definition: EntityEditor.cpp:75
void Show()
Show the editor.
Definition: EntityEditor.cpp:81
bool ShowsEntity(Entity *entity)
Checks if the editor is showing this entity.
Definition: EntityEditor.cpp:203
void SetVisible(bool visible)
Set whether the window should be visible.
Definition: EntityEditor.cpp:211
void SetEntity(Entity *entity)
Set the entity to edit.
Definition: EntityEditor.cpp:161
Entity * GetEntity()
Get the entity being edited.
Definition: EntityEditor.cpp:199
bool IsVisible() const
Get whether the window is visible.
Definition: EntityEditor.cpp:207
Used to select a resource from the resource list.
Definition: ResourceSelector.hpp:7
Editor for rigid body components.
Definition: RigidBodyEditor.hpp:9
Used to edit a script.
Definition: ScriptEditor.hpp:9
Definition: TriggerEditor.hpp:12
Definition: BoxShapeEditor.hpp:5
Definition: AssetEditor.hpp:9