From d6ecfb3476b3165ba73d477b67ab0deeb98e7792 Mon Sep 17 00:00:00 2001 From: SweetId <2630750+SweetId@users.noreply.github.com> Date: Thu, 12 Oct 2023 19:14:07 -0400 Subject: [PATCH] add NameComponent and rework level and inspector windows --- include/NazaraEditor/Core.hpp | 1 + .../Core/Components/NameComponent.hpp | 37 +++++++++++++++++++ include/NazaraEditor/Core/Reflection/Core.hpp | 16 +++++++- .../NazaraEditor/Core/Reflection/Editor.hpp | 21 +++++++++++ .../NazaraEditor/Core/Reflection/Editor.inl | 6 +++ .../NazaraEditor/Core/Reflection/Graphics.hpp | 2 +- .../NazaraEditor/Core/Reflection/Utility.hpp | 2 +- .../Editor/UI/InspectorWindow.cpp | 2 +- src/NazaraEditor/Editor/UI/LevelWindow.cpp | 9 +++-- src/NazaraEditor/Editor/main.cpp | 3 ++ 10 files changed, 91 insertions(+), 8 deletions(-) create mode 100644 include/NazaraEditor/Core/Components/NameComponent.hpp diff --git a/include/NazaraEditor/Core.hpp b/include/NazaraEditor/Core.hpp index 5082203..86914d6 100644 --- a/include/NazaraEditor/Core.hpp +++ b/include/NazaraEditor/Core.hpp @@ -4,6 +4,7 @@ #include #include #include +#include #include #include #include diff --git a/include/NazaraEditor/Core/Components/NameComponent.hpp b/include/NazaraEditor/Core/Components/NameComponent.hpp new file mode 100644 index 0000000..22934ed --- /dev/null +++ b/include/NazaraEditor/Core/Components/NameComponent.hpp @@ -0,0 +1,37 @@ +#pragma once + +#include + +#include + +namespace Nz +{ + enum EditorEntityFlags : uint64_t + { + EditorEntityFlags_None = 0, + + EditorEntityFlags_Hidden = 1 << 1, + }; + + class NAZARAEDITOR_CORE_API EditorNameComponent + { + public: + EditorNameComponent() = default; + EditorNameComponent(const EditorNameComponent&) = delete; + EditorNameComponent(EditorNameComponent&&) = default; + ~EditorNameComponent() = default; + + EditorNameComponent& operator=(const EditorNameComponent&) = delete; + EditorNameComponent& operator=(EditorNameComponent&&) = default; + + void SetName(const std::string& name) { m_name = name; } + const std::string& GetName() const { return m_name; } + + void SetFlags(const uint64_t flags) { m_flags = flags; } + uint64_t GetFlags() const { return m_flags; } + + private: + std::string m_name; + uint64_t m_flags; + }; +} \ No newline at end of file diff --git a/include/NazaraEditor/Core/Reflection/Core.hpp b/include/NazaraEditor/Core/Reflection/Core.hpp index 079fa97..1e374a5 100644 --- a/include/NazaraEditor/Core/Reflection/Core.hpp +++ b/include/NazaraEditor/Core/Reflection/Core.hpp @@ -10,6 +10,7 @@ #include #include #include +#include namespace Nz { @@ -44,6 +45,18 @@ namespace Nz inline void End(Color&) {} } + namespace EditorImgui + { + inline bool Begin(std::string& obj, const std::string& name, const std::string& tooltip) + { + ImGui::InputText(name.c_str(), &obj); + Tooltip(tooltip); + return false; + } + + inline void End(std::string&) {} + } + // Wrapper for array values template struct ArrayEntry @@ -126,8 +139,7 @@ namespace Nz { inline bool Begin(entt::handle& /*obj*/, const std::string& name, const std::string& /*tooltip*/) { - std::string n = "Entity " + name; - return ImGui::TreeNode(n.c_str()); + return ImGui::TreeNode(name.c_str()); } inline void End(entt::handle&) { diff --git a/include/NazaraEditor/Core/Reflection/Editor.hpp b/include/NazaraEditor/Core/Reflection/Editor.hpp index b6484e5..0cddaeb 100644 --- a/include/NazaraEditor/Core/Reflection/Editor.hpp +++ b/include/NazaraEditor/Core/Reflection/Editor.hpp @@ -5,12 +5,33 @@ namespace Nz { + template <> + class TypeReflect + { + public: + template + static void Reflect(TPropertyEnumerator& p, EditorNameComponent& obj) + { + p.AddProperty([&obj]() { return obj.GetName(); }, [&obj](const std::string& v) { obj.SetName(v); }, "Name", "Entity Name"); + } + }; + + namespace EditorImgui + { + inline bool Begin(Nz::EditorNameComponent& /*obj*/, const std::string& /*name*/, const std::string& /*tooltip*/) { return true; } + + inline void End(Nz::EditorNameComponent& /*obj*/) { } + } + template struct EditorPropertyInspector { template void AddProperty(T& prop, const std::string& name, const std::string& tooltip); + template + void AddPropertyNoWrapper(T& prop, const std::string& name, const std::string& tooltip); + /*template void AddProperty(T& parent, V(U::* Getter)() const, void(U::*Setter(const V&))(), const std::string& name, const std::string& tooltip) { diff --git a/include/NazaraEditor/Core/Reflection/Editor.inl b/include/NazaraEditor/Core/Reflection/Editor.inl index 18837fa..ec658bf 100644 --- a/include/NazaraEditor/Core/Reflection/Editor.inl +++ b/include/NazaraEditor/Core/Reflection/Editor.inl @@ -12,4 +12,10 @@ namespace Nz } } + template + template void EditorPropertyInspector::AddPropertyNoWrapper(T& prop, const std::string& name, const std::string& tooltip) + { + TypeReflect::Reflect(*this, prop); + } + } \ No newline at end of file diff --git a/include/NazaraEditor/Core/Reflection/Graphics.hpp b/include/NazaraEditor/Core/Reflection/Graphics.hpp index 2b37d38..ccd3ad6 100644 --- a/include/NazaraEditor/Core/Reflection/Graphics.hpp +++ b/include/NazaraEditor/Core/Reflection/Graphics.hpp @@ -42,7 +42,7 @@ namespace Nz inline bool Begin(Nz::LightComponent& /*obj*/, const std::string& /*name*/, const std::string& /*tooltip*/) { - return ImGui::TreeNode("LightComponent"); + return ImGui::TreeNodeEx("LightComponent", ImGuiTreeNodeFlags_Framed); } inline void End(Nz::LightComponent&) { diff --git a/include/NazaraEditor/Core/Reflection/Utility.hpp b/include/NazaraEditor/Core/Reflection/Utility.hpp index 07d6d4e..a3d955b 100644 --- a/include/NazaraEditor/Core/Reflection/Utility.hpp +++ b/include/NazaraEditor/Core/Reflection/Utility.hpp @@ -26,7 +26,7 @@ namespace Nz { inline bool Begin(Nz::NodeComponent& /*obj*/, const std::string& /*name*/, const std::string& /*tooltip*/) { - return ImGui::TreeNode("NodeComponent"); + return ImGui::TreeNodeEx("NodeComponent", ImGuiTreeNodeFlags_Framed); } inline void End(Nz::NodeComponent& /*obj*/) { diff --git a/src/NazaraEditor/Editor/UI/InspectorWindow.cpp b/src/NazaraEditor/Editor/UI/InspectorWindow.cpp index 213100e..ba40010 100644 --- a/src/NazaraEditor/Editor/UI/InspectorWindow.cpp +++ b/src/NazaraEditor/Editor/UI/InspectorWindow.cpp @@ -16,7 +16,7 @@ namespace NzEditor return; Nz::EditorPropertyInspector enumerator; - enumerator.AddProperty(m_currentEntity, "", ""); + enumerator.AddPropertyNoWrapper(m_currentEntity, "", ""); ImGui::End(); } diff --git a/src/NazaraEditor/Editor/UI/LevelWindow.cpp b/src/NazaraEditor/Editor/UI/LevelWindow.cpp index 0adda07..c4256d7 100644 --- a/src/NazaraEditor/Editor/UI/LevelWindow.cpp +++ b/src/NazaraEditor/Editor/UI/LevelWindow.cpp @@ -1,6 +1,7 @@ #include #include +#include namespace NzEditor { @@ -22,9 +23,11 @@ namespace NzEditor std::function drawHierarchy = [&](Nz::Node* c) { entt::handle entity = m_nodeToEntity[c]; - std::ostringstream oss; - oss << "(" << (uint32_t)entity.entity() << ")"; - if (Nz::EditorImgui::Begin(entity, oss.str(), "")) + Nz::EditorNameComponent* nameComponent = entity.try_get(); + if (nameComponent->GetFlags() & Nz::EditorEntityFlags_Hidden) + return; + + if (Nz::EditorImgui::Begin(entity, nameComponent->GetName(), "")) { for (auto& child : c->GetChilds()) drawHierarchy(child); diff --git a/src/NazaraEditor/Editor/main.cpp b/src/NazaraEditor/Editor/main.cpp index 8cae399..ec8948a 100644 --- a/src/NazaraEditor/Editor/main.cpp +++ b/src/NazaraEditor/Editor/main.cpp @@ -40,6 +40,9 @@ int WinMain(int argc, char* argv[]) entt::meta() .type(entt::type_hash::value()) .func<&Nz::ReflectComponent, Nz::LightComponent>>(entt::hashed_string("Reflect")); + entt::meta() + .type(entt::type_hash::value()) + .func<&Nz::ReflectComponent, Nz::EditorNameComponent>>(entt::hashed_string("Reflect")); entt::handle entity = app.CreateEntity();