From f54d64c6dd766989b6a1ca3021a28b857719761c Mon Sep 17 00:00:00 2001 From: Antoine Pilote Date: Tue, 1 Aug 2023 15:15:27 -0400 Subject: [PATCH] Added sprite component --- Editor/src/ComponentsPanel/SpritePanel.h | 73 +++++++++++++++++++ Editor/src/Windows/EditorSelectionPanel.cpp | 9 +-- Editor/src/Windows/EditorSelectionPanel.h | 4 +- Editor/src/Windows/FileSystemUI.cpp | 18 ++++- .../src/Scene/Components/SpriteComponent.cpp | 53 ++++++++++++++ Nuake/src/Scene/Components/SpriteComponent.h | 24 ++++++ Nuake/src/Scene/Entities/Entity.cpp | 29 +++++--- 7 files changed, 188 insertions(+), 22 deletions(-) create mode 100644 Editor/src/ComponentsPanel/SpritePanel.h create mode 100644 Nuake/src/Scene/Components/SpriteComponent.cpp create mode 100644 Nuake/src/Scene/Components/SpriteComponent.h diff --git a/Editor/src/ComponentsPanel/SpritePanel.h b/Editor/src/ComponentsPanel/SpritePanel.h new file mode 100644 index 00000000..5db42544 --- /dev/null +++ b/Editor/src/ComponentsPanel/SpritePanel.h @@ -0,0 +1,73 @@ +#pragma once +#include "ComponentPanel.h" + +#include +#include +#include +#include + + +class SpritePanel : ComponentPanel +{ +public: + SpritePanel() = default; + ~SpritePanel() = default; + + void Draw(Nuake::Entity entity) override + { + if (!entity.HasComponent()) + { + return; + } + + auto& component = entity.GetComponent(); + BeginComponentTable(SPRITE, Nuake::SpriteComponent); + { + { + ImGui::Text("Sprite"); + ImGui::TableNextColumn(); + + std::string path = component.SpritePath; + ImGui::Button(path.empty() ? "Drag image" : component.SpritePath.c_str(), ImVec2(ImGui::GetContentRegionAvail().x, 0)); + if (ImGui::BeginDragDropTarget()) + { + if (const ImGuiPayload* payload = ImGui::AcceptDragDropPayload("_Image")) + { + char* file = (char*)payload->Data; + + std::string fullPath = std::string(file, 512); + path = Nuake::FileSystem::AbsoluteToRelative(std::move(fullPath)); + component.SpritePath = path; + + component.LoadSprite(); + } + ImGui::EndDragDropTarget(); + } + + ImGui::TableNextColumn(); + ComponentTableReset(component.LockYRotation, false); + } + ImGui::TableNextColumn(); + { + ImGui::Text("Billboard"); + ImGui::TableNextColumn(); + + ImGui::Checkbox("##billboard", &component.Billboard); + ImGui::TableNextColumn(); + + ComponentTableReset(component.Billboard, false); + } + ImGui::TableNextColumn(); + { + ImGui::Text("Lock Y rotation"); + ImGui::TableNextColumn(); + + ImGui::Checkbox("##lockYRotation", &component.LockYRotation); + ImGui::TableNextColumn(); + + ComponentTableReset(component.LockYRotation, false); + } + } + EndComponentTable(); + } +}; \ No newline at end of file diff --git a/Editor/src/Windows/EditorSelectionPanel.cpp b/Editor/src/Windows/EditorSelectionPanel.cpp index b278e5cc..5822096c 100644 --- a/Editor/src/Windows/EditorSelectionPanel.cpp +++ b/Editor/src/Windows/EditorSelectionPanel.cpp @@ -14,13 +14,6 @@ EditorSelectionPanel::EditorSelectionPanel() { - mTransformPanel = TransformPanel(); - mLightPanel = LightPanel(); - mScriptPanel = ScriptPanel(); - mQuakeMapPanel = QuakeMapPanel(); - mCameraPanel = CameraPanel(); - mRigidbodyPanel = RigidbodyPanel(); - mBoxColliderPanel = BoxColliderPanel(); } void EditorSelectionPanel::ResolveFile(Ref file) @@ -100,6 +93,7 @@ void EditorSelectionPanel::DrawEntity(Nuake::Entity entity) mTransformPanel.Draw(entity); mLightPanel.Draw(entity); mScriptPanel.Draw(entity); + mSpritePanel.Draw(entity); mMeshPanel.Draw(entity); mQuakeMapPanel.Draw(entity); mCameraPanel.Draw(entity); @@ -129,6 +123,7 @@ void EditorSelectionPanel::DrawAddComponentMenu(Nuake::Entity entity) MenuItemComponent("Camera", Nuake::CameraComponent) MenuItemComponent("Light", Nuake::LightComponent) MenuItemComponent("Model", Nuake::ModelComponent) + MenuItemComponent("Sprite", Nuake::SpriteComponent) ImGui::Separator(); MenuItemComponent("Character Controller", Nuake::CharacterControllerComponent) MenuItemComponent("Rigid body", Nuake::RigidBodyComponent) diff --git a/Editor/src/Windows/EditorSelectionPanel.h b/Editor/src/Windows/EditorSelectionPanel.h index 88b2cd7b..847406ad 100644 --- a/Editor/src/Windows/EditorSelectionPanel.h +++ b/Editor/src/Windows/EditorSelectionPanel.h @@ -18,6 +18,8 @@ #include "../ComponentsPanel/SphereColliderPanel.h" #include "../ComponentsPanel/MeshColliderPanel.h" #include "../ComponentsPanel/CharacterControllerPanel.h" +#include "../ComponentsPanel/SpritePanel.h" + class EditorSelectionPanel { private: @@ -33,7 +35,7 @@ private: MeshColliderPanel mMeshColliderPanel; CapsuleColliderPanel mCapsuleColliderPanel; CylinderColliderPanel mCylinderColliderPanel; - + SpritePanel mSpritePanel; CharacterControllerPanel mCharacterControllerPanel; Ref currentFile; diff --git a/Editor/src/Windows/FileSystemUI.cpp b/Editor/src/Windows/FileSystemUI.cpp index 2e10a976..c1af61eb 100644 --- a/Editor/src/Windows/FileSystemUI.cpp +++ b/Editor/src/Windows/FileSystemUI.cpp @@ -211,28 +211,42 @@ namespace Nuake { Editor->Selection = EditorSelection(file); } + } - if (ImGui::BeginDragDropSource()) + if (ImGui::BeginDragDropSource()) { char pathBuffer[256]; std::strncpy(pathBuffer, file->GetAbsolutePath().c_str(), sizeof(pathBuffer)); std::string dragType; if (fileExtension == ".wren") + { dragType = "_Script"; + } else if (fileExtension == ".map") + { dragType = "_Map"; + } else if (fileExtension == ".obj" || fileExtension == ".mdl" || fileExtension == ".gltf" || fileExtension == ".md3" || fileExtension == ".fbx") + { dragType = "_Model"; + } else if (fileExtension == ".interface") + { dragType = "_Interface"; + } else if (fileExtension == ".prefab") + { dragType = "_Prefab"; + } + else if (fileExtension == ".png" || fileExtension == ".jpg") + { + dragType = "_Image"; + } ImGui::SetDragDropPayload(dragType.c_str(), (void*)(pathBuffer), sizeof(pathBuffer)); ImGui::Text(file->GetName().c_str()); ImGui::EndDragDropSource(); } - } const std::string hoverMenuId = std::string("item_hover_menu") + std::to_string(drawId); if (ImGui::IsItemHovered() && ImGui::IsMouseReleased(1)) diff --git a/Nuake/src/Scene/Components/SpriteComponent.cpp b/Nuake/src/Scene/Components/SpriteComponent.cpp new file mode 100644 index 00000000..414d99d3 --- /dev/null +++ b/Nuake/src/Scene/Components/SpriteComponent.cpp @@ -0,0 +1,53 @@ +#include "SpriteComponent.h" + +#include "src/Rendering/Textures/TextureManager.h" + +namespace Nuake +{ + SpriteComponent::SpriteComponent() : + Billboard(false), + LockYRotation(false), + SpritePath("") + { + + } + + bool SpriteComponent::LoadSprite() + { + const auto texture = TextureManager::Get()->GetTexture(SpritePath); + Sprite = texture; + return true; + } + + json SpriteComponent::Serialize() + { + BEGIN_SERIALIZE(); + SERIALIZE_VAL(Billboard) + SERIALIZE_VAL(LockYRotation) + SERIALIZE_VAL(SpritePath) + END_SERIALIZE(); + } + + bool SpriteComponent::Deserialize(const std::string& str) + { + BEGIN_DESERIALIZE(); + + if (j.contains("Billboard")) + { + Billboard = j["Billboard"]; + } + + if (j.contains("LockYRotation")) + { + LockYRotation = j["LockYRotation"]; + } + + if (j.contains("SpritePath")) + { + SpritePath = j["SpritePath"]; + LoadSprite(); + } + + return true; + } +} \ No newline at end of file diff --git a/Nuake/src/Scene/Components/SpriteComponent.h b/Nuake/src/Scene/Components/SpriteComponent.h new file mode 100644 index 00000000..97761f76 --- /dev/null +++ b/Nuake/src/Scene/Components/SpriteComponent.h @@ -0,0 +1,24 @@ +#pragma once +#include "src/Core/Core.h" +#include "src/Resource/Serializable.h" +#include "src/Rendering/Textures/Texture.h" + +namespace Nuake +{ + class SpriteComponent + { + public: + bool Billboard; + bool LockYRotation; + std::string SpritePath; + Ref Sprite; + + SpriteComponent(); + + public: + bool LoadSprite(); + + json Serialize(); + bool Deserialize(const std::string& str); + }; +} diff --git a/Nuake/src/Scene/Entities/Entity.cpp b/Nuake/src/Scene/Entities/Entity.cpp index 30c9583a..87dbca05 100644 --- a/Nuake/src/Scene/Entities/Entity.cpp +++ b/Nuake/src/Scene/Entities/Entity.cpp @@ -1,19 +1,21 @@ -#include "../Components/ParentComponent.h" #include "Entity.h" -#include "../Components/NameComponent.h" -#include "../Components/TransformComponent.h" -#include "../Components/CameraComponent.h" -#include "../Components/QuakeMap.h" -#include "../Components/LightComponent.h" -#include "../Components/QuakeMap.h" -#include "../Components/WrenScriptComponent.h" -#include "../Components/CharacterControllerComponent.h" -#include "../Components/RigidbodyComponent.h" -#include "src/Scene/Components/BSPBrushComponent.h" +#include "src/Scene/Components/ParentComponent.h" +#include "src/Scene/Components/NameComponent.h" +#include "src/Scene/Components/TransformComponent.h" +#include "src/Scene/Components/CameraComponent.h" +#include "src/Scene/Components/QuakeMap.h" +#include "src/Scene/Components/LightComponent.h" +#include "src/Scene/Components/QuakeMap.h" +#include "src/Scene/Components/WrenScriptComponent.h" +#include "src/Scene/Components/CharacterControllerComponent.h" +#include "src/Scene/Components/RigidbodyComponent.h" +#include "src/Scene/Components/BSPBrushComponent.h" #include "src/Scene/Components/Components.h" -#include +#include "src/Scene/Components/BoxCollider.h" #include "src/Scene/Components/CapsuleColliderComponent.h" +#include "src/Scene/Components/SpriteComponent.h" + namespace Nuake { @@ -37,6 +39,8 @@ namespace Nuake SERIALIZE_OBJECT_REF_LBL("VisibilityComponent", GetComponent()) if (HasComponent()) SERIALIZE_OBJECT_REF_LBL("CameraComponent", GetComponent()) + if (HasComponent()) + SERIALIZE_OBJECT_REF_LBL("SpriteComponent", GetComponent()) if (HasComponent()) SERIALIZE_OBJECT_REF_LBL("QuakeMapComponent", GetComponent()) if (HasComponent()) @@ -78,6 +82,7 @@ namespace Nuake } DESERIALIZE_COMPONENT(NameComponent) DESERIALIZE_COMPONENT(ParentComponent) + DESERIALIZE_COMPONENT(SpriteComponent) DESERIALIZE_COMPONENT(CameraComponent) DESERIALIZE_COMPONENT(QuakeMapComponent) DESERIALIZE_COMPONENT(LightComponent)