From b1a1f4f063994384769b38ff360f5812b2adad1d Mon Sep 17 00:00:00 2001 From: antopilo Date: Wed, 8 Sep 2021 18:35:10 -0400 Subject: [PATCH] Interface component, Serialization of mesh components --- Editor/src/EditorInterface.cpp | 36 +++++++++++++++++ Editor/src/FileSystemUI.cpp | 2 + Nuake/src/Rendering/Renderer2D.cpp | 3 +- .../src/Scene/Components/InterfaceComponent.h | 40 ++++++++++++++++--- Nuake/src/Scene/Components/MeshComponent.h | 18 ++++++++- Nuake/src/Scene/Entities/Entity.cpp | 4 ++ Nuake/src/Scene/Scene.cpp | 24 ++++++----- Nuake/src/UI/InterfaceParser.cpp | 15 ++++--- Nuake/src/UI/Nodes/Canvas.h | 12 ++++-- Nuake/src/UI/Nodes/Node.h | 2 - Nuake/src/UI/Nodes/TextNode.h | 26 ------------ Nuake/src/UI/UserInterface.cpp | 7 ++-- Nuake/src/UI/UserInterface.h | 8 +++- Nuake/src/Window.cpp | 12 ++++++ 14 files changed, 145 insertions(+), 64 deletions(-) diff --git a/Editor/src/EditorInterface.cpp b/Editor/src/EditorInterface.cpp index d203d663..37de6c97 100644 --- a/Editor/src/EditorInterface.cpp +++ b/Editor/src/EditorInterface.cpp @@ -33,6 +33,7 @@ #include #include #include "src/Rendering/Renderer.h" +#include namespace Nuake { Ref userInterface; @@ -503,6 +504,11 @@ namespace Nuake { { m_SelectedEntity.AddComponent(); } + ImGui::Separator(); + if (ImGui::MenuItem("Interface Component") && !m_SelectedEntity.HasComponent()) + { + m_SelectedEntity.AddComponent< InterfaceComponent>(); + } // your popup code ImGui::EndPopup(); @@ -523,6 +529,36 @@ namespace Nuake { ImGui::Separator(); } + if (m_SelectedEntity.HasComponent()) + { + if (ImGui::CollapsingHeader("Interface", ImGuiTreeNodeFlags_DefaultOpen)) + { + InterfaceComponent& component = m_SelectedEntity.GetComponent(); + std::string& path = component.Path; + char pathBuffer[256]; + memset(pathBuffer, 0, sizeof(pathBuffer)); + std::strncpy(pathBuffer, path.c_str(), sizeof(pathBuffer)); + + ImGui::Text("Interface: "); + ImGui::SameLine(); + + if (ImGui::InputText("##InterfacePath", pathBuffer, sizeof(pathBuffer))) + path = FileSystem::AbsoluteToRelative(std::string(pathBuffer)); + + if (ImGui::BeginDragDropTarget()) + { + if (const ImGuiPayload* payload = ImGui::AcceptDragDropPayload("_Interface")) + { + char* file = (char*)payload->Data; + std::string fullPath = std::string(file, 256); + path = FileSystem::AbsoluteToRelative(fullPath); + component.SetInterface(path); + } + ImGui::EndDragDropTarget(); + } + } + } + if (m_SelectedEntity.HasComponent()) { std::string icon = ICON_FA_MALE; diff --git a/Editor/src/FileSystemUI.cpp b/Editor/src/FileSystemUI.cpp index ebf6b954..b21f714a 100644 --- a/Editor/src/FileSystemUI.cpp +++ b/Editor/src/FileSystemUI.cpp @@ -121,6 +121,8 @@ namespace Nuake { dragType = "_Map"; else if (file->Type == ".obj" || file->Type == ".mdl" || file->Type == ".gltf" || file->Type == ".md3" || file->Type == ".fbx") dragType = "_Model"; + else if (file->Type == ".interface") + dragType = "_Interface"; ImGui::SetDragDropPayload(dragType.c_str(), (void*)(pathBuffer), sizeof(pathBuffer)); ImGui::Text(file->name.c_str()); diff --git a/Nuake/src/Rendering/Renderer2D.cpp b/Nuake/src/Rendering/Renderer2D.cpp index e56947b4..b8b789fa 100644 --- a/Nuake/src/Rendering/Renderer2D.cpp +++ b/Nuake/src/Rendering/Renderer2D.cpp @@ -180,7 +180,8 @@ namespace Nuake void Renderer2D::EndDraw() { - + glEnable(GL_DEPTH_TEST); + glEnable(GL_CULL_FACE); } } diff --git a/Nuake/src/Scene/Components/InterfaceComponent.h b/Nuake/src/Scene/Components/InterfaceComponent.h index 00390d5f..bc0381d2 100644 --- a/Nuake/src/Scene/Components/InterfaceComponent.h +++ b/Nuake/src/Scene/Components/InterfaceComponent.h @@ -1,8 +1,36 @@ #pragma once -#include "../Core/Core.h" -#include +#include "src/Core/Core.h" +#include "src/UI/UserInterface.h" +#include "src/Resource/Serializable.h" -class InterfaceComponent -{ - Ref Interface; -}; \ No newline at end of file +namespace Nuake { + class InterfaceComponent + { + public: + std::string Path; + Ref Interface; + + json Serialize() + { + BEGIN_SERIALIZE(); + SERIALIZE_VAL_LBL("Interface", Interface->GetPath()) + END_SERIALIZE(); + } + + bool Deserialize(const std::string& str) + { + BEGIN_DESERIALIZE(); + + if (j.contains("Interface")) + SetInterface(j["Interface"]); + + return true; + } + + void SetInterface(const std::string& path) + { + Path = path; + Interface = UI::UserInterface::New("No name", path); + } + }; +} diff --git a/Nuake/src/Scene/Components/MeshComponent.h b/Nuake/src/Scene/Components/MeshComponent.h index dfc35cf4..14a00725 100644 --- a/Nuake/src/Scene/Components/MeshComponent.h +++ b/Nuake/src/Scene/Components/MeshComponent.h @@ -2,7 +2,7 @@ #include #include #include "src/Rendering/Mesh/Mesh.h" - +#include "src/Resource/Serializable.h" #include "assimp/Importer.hpp" #include #include @@ -18,6 +18,7 @@ namespace Nuake { //loadModel(path); } + void LoadModel(); void Draw(); std::vector> meshes; @@ -26,5 +27,20 @@ namespace Nuake void ProcessNode(aiNode* node, const aiScene* scene); Ref ProcessMesh(aiMesh* mesh, const aiScene* scene); std::vector LoadMaterialTextures(aiMaterial* mat, aiTextureType type); + + json Serialize() + { + BEGIN_SERIALIZE(); + SERIALIZE_VAL(ModelPath); + END_SERIALIZE(); + } + + bool Deserialize(const std::string str) + { + BEGIN_DESERIALIZE(); + ModelPath = j["ModelPath"]; + LoadModel(); + return true; + } }; } diff --git a/Nuake/src/Scene/Entities/Entity.cpp b/Nuake/src/Scene/Entities/Entity.cpp index 45ad81cb..f3e990a5 100644 --- a/Nuake/src/Scene/Entities/Entity.cpp +++ b/Nuake/src/Scene/Entities/Entity.cpp @@ -8,6 +8,7 @@ #include "../Components/QuakeMap.h" #include "../Components/WrenScriptComponent.h" #include "../Components/CharacterControllerComponent.h" +#include namespace Nuake { @@ -38,6 +39,8 @@ namespace Nuake SERIALIZE_OBJECT_REF_LBL("WrenScriptComponent", GetComponent()); if (HasComponent()) SERIALIZE_OBJECT_REF_LBL("CharacterControllerComponent", GetComponent()); + if (HasComponent()) + SERIALIZE_OBJECT_REF_LBL("MeshComponent", GetComponent()); END_SERIALIZE(); } @@ -50,6 +53,7 @@ namespace Nuake DESERIALIZE_COMPONENT(CameraComponent); DESERIALIZE_COMPONENT(QuakeMapComponent); DESERIALIZE_COMPONENT(LightComponent); + DESERIALIZE_COMPONENT(MeshComponent); DESERIALIZE_COMPONENT(WrenScriptComponent); DESERIALIZE_COMPONENT(CharacterControllerComponent); return true; diff --git a/Nuake/src/Scene/Scene.cpp b/Nuake/src/Scene/Scene.cpp index 6fc69cc5..5f8fbcb3 100644 --- a/Nuake/src/Scene/Scene.cpp +++ b/Nuake/src/Scene/Scene.cpp @@ -2,10 +2,7 @@ #include "src/Scene/Systems/ScriptingSystem.h" #include "src/Scene/Systems/PhysicsSystem.h" #include "src/Scene/Systems/TransformSystem.h" - #include "src/Scene/Systems/QuakeMapBuilder.h" -#include "src/Scene/Components/BSPBrushComponent.h" - #include "Scene.h" #include "Entities/Entity.h" @@ -18,10 +15,12 @@ #include "Engine.h" #include "src/Core/FileSystem.h" -#include "Components/Components.h" -#include "Components/BoxCollider.h" -#include "Components/LuaScriptComponent.h" -#include "Components/WrenScriptComponent.h" +#include "src/Scene/Components/Components.h" +#include "src/Scene/Components/BoxCollider.h" +#include "src/Scene/Components/LuaScriptComponent.h" +#include "src/Scene/Components/WrenScriptComponent.h" +#include "src/Scene/Components/BSPBrushComponent.h" +#include "src/Scene/Components/InterfaceComponent.h" #include #include @@ -713,17 +712,20 @@ namespace Nuake { void Scene::ReloadInterfaces() { - for (auto& i : m_Interfaces) - i->Reload(); + auto interfaceView = m_Registry.view(); + for (auto i : interfaceView) + { + InterfaceComponent& uInterface = interfaceView.get(i); + if (uInterface.Interface) + uInterface.Interface->Reload(); + } } - void Scene::AddInterface(Ref interface) { this->m_Interfaces.push_back(interface); } - json Scene::Serialize() { BEGIN_SERIALIZE(); diff --git a/Nuake/src/UI/InterfaceParser.cpp b/Nuake/src/UI/InterfaceParser.cpp index 335b1fc4..9457b770 100644 --- a/Nuake/src/UI/InterfaceParser.cpp +++ b/Nuake/src/UI/InterfaceParser.cpp @@ -133,9 +133,9 @@ namespace Nuake node->NormalStyle.BackgroundColor = GetColor(a.value()); if (name == "onclick") { - std::string signature = a.value(); - Root->Script->RegisterMethod(a.value()); - node->OnClickSignature = a.value(); + //std::string signature = a.value(); + //Root->Script->RegisterMethod(a.value()); + //node->OnClickSignature = a.value(); } } @@ -289,15 +289,14 @@ namespace Nuake node->NormalStyle.BackgroundColor = GetColor(a.value()); if (name == "script") { - auto s = split(a.value(), ' '); - std::string path = s[0]; - std::string module = s[1]; - node->Script = ScriptingEngine::RegisterScript(path, module); + auto scriptModule = String::Split(a.value(), ' '); + node->ScriptsToLoad.push_back({ scriptModule[0], scriptModule[1] }); + //node->Script = ScriptingEngine::RegisterScript(FileSystem::Root + path, module); } if (name == "stylesheet") { std::string path = a.value(); - node->StyleSheet = UI::StyleSheet::New(path); + node->StyleSheet = UI::StyleSheet::New(FileSystem::Root + path); } } diff --git a/Nuake/src/UI/Nodes/Canvas.h b/Nuake/src/UI/Nodes/Canvas.h index 826d0a42..a29354e1 100644 --- a/Nuake/src/UI/Nodes/Canvas.h +++ b/Nuake/src/UI/Nodes/Canvas.h @@ -1,9 +1,11 @@ #pragma once #include "Node.h" -#include "../Core/Core.h" -#include "../Scripting/WrenScript.h" -#include -#include "../Styling/Stylesheet.h" +#include "src/Core/Core.h" +#include "src/Scripting/WrenScript.h" +#include "src/UI/Styling/Stylesheet.h" + +#include +#include namespace Nuake { class Canvas : public Node @@ -11,6 +13,8 @@ namespace Nuake { private: public: + std::vector> ScriptsToLoad; + Ref Script; Ref StyleSheet; diff --git a/Nuake/src/UI/Nodes/Node.h b/Nuake/src/UI/Nodes/Node.h index db38e242..ce7f8866 100644 --- a/Nuake/src/UI/Nodes/Node.h +++ b/Nuake/src/UI/Nodes/Node.h @@ -325,8 +325,6 @@ namespace Nuake float height = YGNodeLayoutGetHeight(YogaNode); float scrollRange = height; - - float padding = YGNodeLayoutGetPadding(YogaNode, YGEdgeLeft); float left = YGNodeLayoutGetLeft(YogaNode); //+ offset.x; float top = YGNodeLayoutGetTop(YogaNode);// +offset.y; diff --git a/Nuake/src/UI/Nodes/TextNode.h b/Nuake/src/UI/Nodes/TextNode.h index 1f7f5227..31c82764 100644 --- a/Nuake/src/UI/Nodes/TextNode.h +++ b/Nuake/src/UI/Nodes/TextNode.h @@ -51,34 +51,8 @@ namespace Nuake parentTop += YGNodeLayoutGetTop(parent); parent = YGNodeGetParent(parent); } - if (parent) - { - //parentLeft = YGNodeLayoutGetLeft(parent); - //parentTop = YGNodeLayoutGetTop(parent); - //float parentPaddingTop = YGNodeLayoutGetPadding(parent, YGEdgeTop); - //float parentPaddingLeft = YGNodeLayoutGetPadding(parent, YGEdgeTop); - // Overflow hidden. - //float parentwidth = YGNodeLayoutGetWidth(parent); - //if (parentwidth - YGNodeLayoutGetMargin(this->YogaNode, YGEdgeLeft) < width) - // width = parentwidth - parentPaddingLeft; - //float parentHeight = YGNodeLayoutGetHeight(parent); - //if (parentHeight < height) - // height = parentHeight - YGNodeLayoutGetMargin(this->YogaNode, YGEdgeTop) - parentPaddingTop; - } - transform = glm::translate(transform, Vector3(left + parentLeft, top + parentTop, 0.f)); - //transform = glm::scale(transform, Vector3(width, height, 1.0)); - //Renderer2D::UIShader->Bind(); - //Renderer2D::UIShader->SetUniformMat4f("model", transform); - //Renderer2D::UIShader->SetUniform1f("u_BorderRadius", Border.Left.Value); - //Renderer2D::UIShader->SetUniform2f("u_Size", width, height); - - //Renderer2D::DrawRect(); - - //Logger::Log("Left: " + std::to_string(left) + " Top:" + std::to_string(top)); - Renderer2D::DrawString(content, textStyle, transform); - //Renderer2D::DrawString(content, style.font, Vector2(Position.Left, Position.Top), 2.0); } }; } diff --git a/Nuake/src/UI/UserInterface.cpp b/Nuake/src/UI/UserInterface.cpp index 3d95a643..9bc30daa 100644 --- a/Nuake/src/UI/UserInterface.cpp +++ b/Nuake/src/UI/UserInterface.cpp @@ -15,7 +15,7 @@ namespace Nuake { m_Path = path; font = FontLoader::LoadFont("resources/Fonts/OpenSans-Regular.ttf"); - Root = InterfaceParser::Parse(path); + Root = InterfaceParser::Parse(FileSystem::Root + path); if (!Root) { @@ -37,7 +37,7 @@ namespace Nuake { void UserInterface::Reload() { - Root = InterfaceParser::Parse(this->m_Path); + Root = InterfaceParser::Parse(FileSystem::Root + this->m_Path); if (!Root) { Logger::Log("Failed to generate interface structure", CRITICAL); @@ -112,6 +112,7 @@ namespace Nuake { Calculate(size.x, size.y); Renderer2D::BeginDraw(size); DrawRecursive(Root, 0); + Renderer2D::EndDraw(); Size = size; } @@ -129,9 +130,7 @@ namespace Nuake { return; for (auto& c : node->Childrens) - { DrawRecursive(c, z + 1); - } } void UserInterface::Update(Timestep ts) diff --git a/Nuake/src/UI/UserInterface.h b/Nuake/src/UI/UserInterface.h index 1994fbb4..893454d5 100644 --- a/Nuake/src/UI/UserInterface.h +++ b/Nuake/src/UI/UserInterface.h @@ -8,6 +8,8 @@ #include "src/Core/Maths.h" #include "Styling/Stylesheet.h" #include "Font/Font.h" +#include +#include namespace Nuake { namespace UI { @@ -23,7 +25,9 @@ namespace Nuake { YGConfigRef yoga_config; YGNodeRef yoga_root; - + + std::vector m_Scripts; + Ref GetNodeByIDRecurse(Ref node, const std::string& id); void RecursiveHover(Ref node, Vector2 pos); public: @@ -53,6 +57,8 @@ namespace Nuake { Ref GetNodeByID(const std::string& id); void ConsumeMouseClick(Vector2 pos); + + std::string GetPath() const { return m_Path; } }; } } diff --git a/Nuake/src/Window.cpp b/Nuake/src/Window.cpp index 387d2ac6..52c0b189 100644 --- a/Nuake/src/Window.cpp +++ b/Nuake/src/Window.cpp @@ -18,6 +18,7 @@ #include #include "Resource/FontAwesome5.h" #include "Engine.h" +#include namespace Nuake { // TODO: Use abstraction for vertex buffers @@ -306,13 +307,24 @@ namespace Nuake { m_GBuffer->GetTexture(GL_COLOR_ATTACHMENT1)->Bind(7); m_GBuffer->GetTexture(GL_COLOR_ATTACHMENT2)->Bind(8); + glDisable(GL_CULL_FACE); + Ref deferredShader = ShaderManager::GetShader("resources/Shaders/deferred.shader"); + deferredShader->Bind(); deferredShader->SetUniform1i("m_Depth", 5); deferredShader->SetUniform1i("m_Albedo", 6); deferredShader->SetUniform1i("m_Normal", 7); deferredShader->SetUniform1i("m_Material", 8); Renderer::DrawQuad(Matrix4()); + + auto interfaceView = m_Scene->m_Registry.view(); + for (auto i : interfaceView) + { + InterfaceComponent& uInterface = interfaceView.get(i); + if (uInterface.Interface) + uInterface.Interface->Draw(m_DeferredBuffer->GetSize()); + } } m_DeferredBuffer->Unbind(); glEnable(GL_DEPTH_TEST);