From bc18b5ef5dbf0b4915e75d126854dc9be99f1fda Mon Sep 17 00:00:00 2001 From: SweetId <2630750+SweetId@users.noreply.github.com> Date: Wed, 29 Nov 2023 17:19:09 +0530 Subject: [PATCH] add icon to EditorNameComponent --- .../Core/Components/NameComponent.hpp | 26 +++++++++ .../Core/Components/NameComponent.cpp | 57 +++++++++++++++++++ 2 files changed, 83 insertions(+) create mode 100644 src/NazaraEditor/Core/Components/NameComponent.cpp diff --git a/include/NazaraEditor/Core/Components/NameComponent.hpp b/include/NazaraEditor/Core/Components/NameComponent.hpp index db20883..d8966e3 100644 --- a/include/NazaraEditor/Core/Components/NameComponent.hpp +++ b/include/NazaraEditor/Core/Components/NameComponent.hpp @@ -2,10 +2,19 @@ #include +#include +#include + +#include +#include #include namespace Nz { + class Billboard; + class NodeComponent; + class WorldInstance; + enum EditorEntityFlags : uint64_t { EditorEntityFlags_None = 0, @@ -24,15 +33,32 @@ namespace Nz EditorNameComponent& operator=(const EditorNameComponent&) = delete; EditorNameComponent& operator=(EditorNameComponent&&) = default; + void Update(Time elapsedTime, NodeComponent& node); + void SetName(const std::string& name) { m_name = name; } const std::string& GetName() const { return m_name; } + void SetIcon(const std::filesystem::path& path); + void SetFlags(const uint64_t flags) { m_flags = flags; } uint64_t GetFlags() const { return m_flags; } + NazaraSignal(OnIconChanged, EditorNameComponent*, const std::filesystem::path& ); + private: + std::string m_name; uint64_t m_flags; + + std::filesystem::path m_iconPath; // @TODO replace with asset + + struct BillboardData + { + size_t index; + std::shared_ptr instance; + std::shared_ptr billboard; + }; + std::optional m_icon; }; } \ No newline at end of file diff --git a/src/NazaraEditor/Core/Components/NameComponent.cpp b/src/NazaraEditor/Core/Components/NameComponent.cpp new file mode 100644 index 0000000..6015159 --- /dev/null +++ b/src/NazaraEditor/Core/Components/NameComponent.cpp @@ -0,0 +1,57 @@ +#include +#include + +#include +#include +#include +#include +#include +#include + +namespace Nz +{ + void EditorNameComponent::Update(Time, NodeComponent& node) + { + if (!m_icon.has_value()) + return; + m_icon.value().instance->UpdateWorldMatrix(node.GetTransformMatrix()); + } + + void EditorNameComponent::SetIcon(const std::filesystem::path& path) + { + auto& level = EditorBaseApplication::Instance()->GetLevel(); + auto& renderer = level.GetEnttWorld()->GetSystem(); + + if (m_icon.has_value()) + { + renderer.GetFramePipeline().UnregisterRenderable(m_icon.value().index); + m_icon = {}; + } + + auto& fs = EditorBaseApplication::Instance()->GetComponent(); + + Nz::TextureParams params; + params.loadFormat = Nz::PixelFormat::RGBA8_SRGB; + std::shared_ptr tex = fs.Load(path.string(), params); + std::shared_ptr mat = Nz::MaterialInstance::GetDefault(Nz::MaterialType::Basic, Nz::MaterialInstancePreset::Transparent)->Clone(); + + mat->SetTextureProperty("BaseColorMap", tex); + mat->SetValueProperty("Billboard", true); + + m_icon = BillboardData{ + .index = 0, + .instance = std::make_shared(), + .billboard = std::make_shared(mat, Nz::Vector2f(1.f, 1.f)) + }; + + auto& icon = m_icon.value(); + + Nz::Recti scissorBox(-1, -1, -1, -1); + + Nz::ElementRendererRegistry elementRegistry; + icon.index = renderer.GetFramePipeline().RegisterWorldInstance(icon.instance); + renderer.GetFramePipeline().RegisterRenderable(icon.index, Nz::FramePipeline::NoSkeletonInstance, icon.billboard.get(), 0xFFFFFFFF, scissorBox); + + OnIconChanged(this, path); + } +} \ No newline at end of file