Vivid
Loading...
Searching...
No Matches
SceneUI.cpp
1#include "SceneUI.h"
2#include "UIFlags.h"
3#include "editor/assets/Assets.h"
4#include "core/ecs/ECS.h"
5#include "core/ecs/ComponentFactory.h"
6
7namespace VividGUI
8{
9 Vivid::Entity* VividGUI::SceneUI::m_SelectedEntity;
10
11 void SceneUI::DrawSceneUI()
12 {
13 ImGui::Begin("Scene");
14 for (auto entity : Vivid::ECS::g_Entities)
15 {
16 if (ImGui::Selectable(entity.second->GetName().c_str(), m_SelectedEntity == entity.second.get()))
17 {
18 m_SelectedEntity = entity.second.get();
19 }
20 }
21
22 unsigned int texId = VividGui::Assets::GetInstance()->GetTexPlus()->GetRendererID();
23 if (ImGui::ImageButton((ImTextureID)texId,
24 ImVec2(VividGui::Assets::GetInstance()->GetButtonWidth(), VividGui::Assets::GetInstance()->GetButtonWidth()),
25 { 0, 0 }, { 1, 1 }, 2))
26 {
27 Vivid::ECS::CreateEntity("Entity" + std::to_string(Vivid::ECS::s_EntityID));
28 }
29
30 if (m_SelectedEntity)
31 {
32 ImGui::Begin("Inspector");
33 m_SelectedEntity->ImguiRender();
34
35 char* name = m_SelectedEntity->GetName().data();
36 static const ImGuiInputTextFlags flags = ImGuiInputTextFlags_EnterReturnsTrue;
37 if (ImGui::InputText(m_SelectedEntity->GetName().c_str(), name, 255, flags))
38 {
39 if (name == "")
40 {
41 m_SelectedEntity->SetName("Entity" + std::to_string(Vivid::ECS::s_EntityID));
42 }
43
44 String temp = name;
45 // temp must not contain space
46 for (int i = 0; i < temp.size(); i++)
47 {
48 if (name[i] == ' ')
49 {
50 name[i] = '_';
51 }
52 }
53 m_SelectedEntity->SetName(String(name));
54 }
55
56 for (auto& entity : Vivid::ECS::g_Entities)
57 {
58 if (entity.first == m_SelectedEntity->GetEntityID())
59 {
60 continue;
61 }
62
63 if (m_SelectedEntity->GetName() == entity.second->GetName())
64 {
65 ImGui::Text("Name Already Exist");
66 }
67 }
68 ImGui::End();
69
70 // A Popup which is triggered when you right click a entity and you can add components through a list of checklist
71 if (ImGui::BeginPopupContextWindow())
72 {
73 if (ImGui::BeginMenu("Add Component"))
74 {
75 for (auto ct : Vivid::g_AllComponentStrings)
76 {
77 bool doesComponentExist = false;
78 auto component = m_SelectedEntity->HasComponent(ct.first);
79 if (component)
80 {
81 doesComponentExist = true;
82 }
83
84 if (ImGui::MenuItem(Vivid::g_AllComponentStrings.at(ct.first).c_str(), NULL, doesComponentExist))
85 {
86 if (doesComponentExist)
87 {
88 if (ct.first == Vivid::ComponentType::TransformComponent)
89 {
90 doesComponentExist = true;
91 continue;
92 }
93 Vivid::ECS::RemoveComponent(component, m_SelectedEntity->GetEntityID());
94 }
95 else
96 {
97 Vivid::ECS::AddComponent(component, m_SelectedEntity->GetEntityID());
98 }
99 }
100 }
101 ImGui::EndMenu();
102 }
103 ImGui::EndPopup();
104 }
105 }
106
107 ImGui::SetWindowPos(ImVec2(200, 50));
108 ImGui::End();
109 }
110
111 void SceneUI::DrawGizmo(Camera* camera)
112 {
113 if (m_SelectedEntity)
114 {
115 m_SelectedEntity->DrawGizmo(camera);
116 }
117 }
118}
A class that represents the camera.
Definition: Camera.h:25
Contains an Entity.
Definition: Entity.h:19
int HasComponent(ComponentType ct)
Checks if the Entity has a component.
Definition: Entity.cpp:54
VividGUI namespace.
Definition: DockUI.h:7