diff --git a/Editor/Editor.cpp b/Editor/Editor.cpp index e938ee67..96398676 100644 --- a/Editor/Editor.cpp +++ b/Editor/Editor.cpp @@ -27,65 +27,22 @@ #include #include "src/Actions/EditorSelection.h" -std::string WindowTitle = "Nuake Engine"; +#include "src/Misc/GizmoDrawer.h" -void OpenProject() -{ - // Parse the project and load it. - std::string projectPath = Nuake::FileDialog::OpenFile("Project file|*.project;"); - - Nuake::FileSystem::SetRootDirectory(projectPath + "/../"); - Ref project = Nuake::Project::New(); - if (!project->Deserialize(Nuake::FileSystem::ReadFile(projectPath, true))) - { - Nuake::Logger::Log("Error loading project: " + projectPath, Nuake::CRITICAL); - return; - } - - project->FullPath = projectPath; - Nuake::Engine::LoadProject(project); -} - -std::vector vertices -{ - Nuake::LineVertex {{10000.f, 0.0f, 0.0f}, {1.f, 0.f, 0.f, 1.f}}, - Nuake::LineVertex {{0.0f, 0.0f, 0.0f }, {1.f, 0.f, 0.f, 1.f}}, - Nuake::LineVertex {{0.f, 0.f, 10000.f }, {0.f, 1.f, 0.f, 1.f}}, - Nuake::LineVertex {{0.0f, 0.0f, 0.0f }, {0.f, 1.f, 0.f, 1.f}}, - Nuake::LineVertex {{0.f, 10000.f, 0.0f }, {0.f, 0.f, 1.f, 1.f}}, - Nuake::LineVertex {{0.0f, 0.0f, 0.0f }, {0.f, 0.f, 1.f, 1.f}} -}; +const std::string WindowTitle = "Nuake Editor"; int main() { Nuake::Engine::Init(); - Nuake::EditorInterface editor; editor.BuildFonts(); - glLineWidth(2.0f); - - Nuake::Shader* lineShader = Nuake::ShaderManager::GetShader("resources/Shaders/line.shader"); - - lineShader->Bind(); - - Nuake::VertexArray* lineVertexArray = new Nuake::VertexArray(); - lineVertexArray->Bind(); - Nuake::VertexBuffer* lineVertexBuffer = new Nuake::VertexBuffer(vertices.data(), vertices.size() * sizeof(Nuake::LineVertex)); - Nuake::VertexBufferLayout* vblayout = new Nuake::VertexBufferLayout(); - vblayout->Push(3); - vblayout->Push(4); - lineVertexArray->AddBuffer(*lineVertexBuffer, *vblayout); - - // Register Gizmo textures - Ref lightTexture = Nuake::TextureManager::Get()->GetTexture("resources/Icons/Gizmo/Light.png"); - Ref camTexture = Nuake::TextureManager::Get()->GetTexture("resources/Icons/Gizmo/Camera.png"); - Nuake::Shader* GuizmoShader = Nuake::ShaderManager::GetShader("resources/Shaders/gizmo.shader"); - Nuake::Shader* ditherShader = Nuake::ShaderManager::GetShader("resources/Shaders/dither.shader"); - - //Nuake::NewEditor newEditor = Nuake::NewEditor(); Ref window = Nuake::Engine::GetCurrentWindow(); - window->SetTitle("Nuake Editor"); + window->SetTitle(WindowTitle); + + using namespace Nuake; + + GizmoDrawer gizmoDrawer = GizmoDrawer(); while (!window->ShouldClose()) { @@ -96,58 +53,26 @@ int main() glViewport(0, 0, WindowSize.x, WindowSize.y); Nuake::Renderer2D::BeginDraw(WindowSize); - //newEditor.Draw(WindowSize); - - Ref sceneFramebuffer = window->GetFrameBuffer(); + auto sceneFramebuffer = window->GetFrameBuffer(); sceneFramebuffer->Bind(); - - Ref currentScene = Nuake::Engine::GetCurrentScene(); - if (currentScene && !Nuake::Engine::IsPlayMode) { - glDisable(GL_DEPTH_TEST); + Ref currentScene = Nuake::Engine::GetCurrentScene(); + Ref camera; + if (currentScene) + { + camera = currentScene->m_EditorCamera; + } - lineShader->Bind(); - lineShader->SetUniformMat4f("u_View", Nuake::Engine::GetCurrentScene()->m_EditorCamera->GetTransform()); - lineShader->SetUniformMat4f("u_Projection", Nuake::Engine::GetCurrentScene()->m_EditorCamera->GetPerspective()); - - lineVertexArray->Bind(); - Nuake::RenderCommand::DrawLines(0, 6); - - glEnable(GL_DEPTH_TEST); - - ditherShader->Bind(); - ditherShader->SetUniformMat4f("u_View", Nuake::Engine::GetCurrentScene()->m_EditorCamera->GetTransform()); - ditherShader->SetUniformMat4f("u_Projection", Nuake::Engine::GetCurrentScene()->m_EditorCamera->GetPerspective()); - ditherShader->SetUniform1f("u_Time", Nuake::Engine::GetTime()); - ditherShader->SetUniform4f("u_Color", 252.0 / 255.0, 3.0 / 255.0, 65.0 / 255.0, 1.0); - - if (editor.Selection.Type == EditorSelectionType::Entity && editor.Selection.Entity.HasComponent()) - { - for (auto& m : editor.Selection.Entity.GetComponent().Meshes) - Nuake::Renderer::SubmitMesh(m, editor.Selection.Entity.GetComponent().GetGlobalTransform()); - - Nuake::Renderer::Flush(ditherShader, true); - } - - ditherShader->SetUniform1f("u_Time", Nuake::Engine::GetTime() / 10.0f); - ditherShader->SetUniform4f("u_Color", 52.f / 255.f, 235.f / 255.f, 88.f / 255.f, 1); - auto cubeCollidersView = currentScene->m_Registry.view(); - if (editor.Selection.Type == EditorSelectionType::Entity && editor.Selection.Entity.HasComponent()) - { - auto transformComponent = editor.Selection.Entity.GetComponent(); - auto colliderComponent = editor.Selection.Entity.GetComponent(); - Nuake::Matrix4 transform = transformComponent.GetGlobalTransform(); - transform = glm::scale(transform, colliderComponent.Size); - Nuake::Renderer::SubmitCube(transform); - - Nuake::Renderer::Flush(ditherShader, true); - } - glEnable(GL_DEPTH_TEST); + if (currentScene && !Nuake::Engine::IsPlayMode) + { + gizmoDrawer.DrawGizmos(currentScene); + } } sceneFramebuffer->Unbind(); editor.Draw(); + // Swap buffers. Nuake::Engine::EndDraw(); } diff --git a/Editor/src/Misc/GizmoDrawer.cpp b/Editor/src/Misc/GizmoDrawer.cpp new file mode 100644 index 00000000..2943a46b --- /dev/null +++ b/Editor/src/Misc/GizmoDrawer.cpp @@ -0,0 +1,39 @@ +#include "GizmoDrawer.h" +#include + +GizmoDrawer::GizmoDrawer() +{ + mLineShader = Nuake::ShaderManager::GetShader("resources/Shaders/line.shader"); + + mAxisLineBuffer = CreateRef(); + mAxisLineBuffer->Bind(); + + mAxisLineVertexBuffer = CreateRef(vertices.data(), vertices.size() * sizeof(Nuake::LineVertex)); + auto vblayout = CreateRef(); + vblayout->Push(3); + vblayout->Push(4); + + mAxisLineBuffer->AddBuffer(*mAxisLineVertexBuffer, *vblayout); +} + +void GizmoDrawer::DrawGizmos(Ref scene) +{ + RenderCommand::Disable(RendererEnum::DEPTH_TEST); + + // Draw Axis lignes. + { + mLineShader->Bind(); + mLineShader->SetUniformMat4f("u_View", scene->m_EditorCamera->GetTransform()); + mLineShader->SetUniformMat4f("u_Projection", scene->m_EditorCamera->GetPerspective()); + + mAxisLineBuffer->Bind(); + Nuake::RenderCommand::DrawLines(0, 6); + } + + // Draw Cameras Frustum. + { + + } + + RenderCommand::Enable(RendererEnum::DEPTH_TEST); +} \ No newline at end of file diff --git a/Editor/src/Misc/GizmoDrawer.h b/Editor/src/Misc/GizmoDrawer.h new file mode 100644 index 00000000..870a2b2a --- /dev/null +++ b/Editor/src/Misc/GizmoDrawer.h @@ -0,0 +1,32 @@ +#pragma once +#include +#include +#include +#include +#include +#include +using namespace Nuake; + +class GizmoDrawer +{ +private: + const std::vector vertices + { + LineVertex {{10000.f, 0.0f, 0.0f}, {1.f, 0.f, 0.f, 1.f}}, + LineVertex {{0.0f, 0.0f, 0.0f }, {1.f, 0.f, 0.f, 1.f}}, + LineVertex {{0.f, 0.f, 10000.f }, {0.f, 1.f, 0.f, 1.f}}, + LineVertex {{0.0f, 0.0f, 0.0f }, {0.f, 1.f, 0.f, 1.f}}, + LineVertex {{0.f, 10000.f, 0.0f }, {0.f, 0.f, 1.f, 1.f}}, + LineVertex {{0.0f, 0.0f, 0.0f }, {0.f, 0.f, 1.f, 1.f}} + }; + + Ref mAxisLineBuffer; + Ref mAxisLineVertexBuffer; + Shader* mLineShader; + +public: + GizmoDrawer(); + ~GizmoDrawer() = default; + + void DrawGizmos(Ref scene); +}; \ No newline at end of file diff --git a/Editor/src/Windows/EditorInterface.cpp b/Editor/src/Windows/EditorInterface.cpp index 5fbc9220..3156cffc 100644 --- a/Editor/src/Windows/EditorInterface.cpp +++ b/Editor/src/Windows/EditorInterface.cpp @@ -114,7 +114,6 @@ namespace Nuake { CurrentOperation, CurrentMode, glm::value_ptr(oldTransform), 0, 0 ); - if (ImGuizmo::IsUsing()) if (ImGuizmo::IsUsing()) { Vector3 globalPos = Vector3(); @@ -1541,6 +1540,8 @@ namespace Nuake { return; } + pInterface.m_CurrentProject = Engine::GetProject(); + if (ImGui::BeginMainMenuBar()) { if (ImGui::BeginMenu("File")) diff --git a/Editor/src/Windows/EditorSelectionPanel.cpp b/Editor/src/Windows/EditorSelectionPanel.cpp index 56977814..fa2b0dc6 100644 --- a/Editor/src/Windows/EditorSelectionPanel.cpp +++ b/Editor/src/Windows/EditorSelectionPanel.cpp @@ -2,6 +2,11 @@ #include "../Misc/ImGuiTextHelper.h" #include +#include +#include +#include +#include + EditorSelectionPanel::EditorSelectionPanel() { mTransformPanel = TransformPanel(); @@ -10,6 +15,24 @@ EditorSelectionPanel::EditorSelectionPanel() mQuakeMapPanel = QuakeMapPanel(); } +void EditorSelectionPanel::ResolveFile(Ref file) +{ + using namespace Nuake; + + currentFile = file; + + if (currentFile->GetExtension() == ".project") + { + + } + + if (currentFile->GetExtension() == ".material") + { + Ref material = ResourceLoader::LoadResource(currentFile->GetRelativePath()); + selectedResource = material; + } +} + void EditorSelectionPanel::Draw(EditorSelection selection) { if (ImGui::Begin("Propreties")) @@ -29,6 +52,11 @@ void EditorSelectionPanel::Draw(EditorSelection selection) } case EditorSelectionType::File: { + if (currentFile != selection.File) + { + ResolveFile(selection.File); + } + DrawFile(selection.File.get()); break; } @@ -345,9 +373,18 @@ void EditorSelectionPanel::DrawAddComponentMenu(Nuake::Entity entity) } + void EditorSelectionPanel::DrawFile(Nuake::File* file) { - + using namespace Nuake; + if (file->GetExtension() == ".material") + { + DrawMaterialPanel(std::static_pointer_cast(selectedResource)); + } + if (file->GetExtension() == ".project") + { + DrawProjectPanel(Nuake::Engine::GetProject()); + } } void EditorSelectionPanel::DrawResource(Nuake::Resource resource) @@ -355,3 +392,260 @@ void EditorSelectionPanel::DrawResource(Nuake::Resource resource) } +void EditorSelectionPanel::DrawMaterialPanel(Ref material) +{ + using namespace Nuake; + + std::string materialTitle = material->Path; + { + UIFont boldfont = UIFont(Fonts::SubTitle); + ImGui::Text(material->Path.c_str()); + + } + ImGui::SameLine(); + { + UIFont boldfont = UIFont(Fonts::Icons); + if (ImGui::Button(ICON_FA_SAVE)) + { + std::string fileData = material->Serialize().dump(4); + + FileSystem::BeginWriteFile(FileSystem::Root + material->Path); + FileSystem::WriteLine(fileData); + FileSystem::EndWriteFile(); + } + } + + bool flagsHeaderOpened; + { + UIFont boldfont = UIFont(Fonts::Bold); + ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(0.f, 0.f)); + ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, ImVec2(0.f, 8.f)); + flagsHeaderOpened = ImGui::CollapsingHeader(" FLAGS", ImGuiTreeNodeFlags_DefaultOpen); + ImGui::PopStyleVar(2); + } + + if (flagsHeaderOpened) + { + ImGui::BeginTable("##Flags", 3, ImGuiTableFlags_BordersInner); + { + ImGui::TableSetupColumn("name", 0, 0.3f); + ImGui::TableSetupColumn("set", 0, 0.6f); + ImGui::TableSetupColumn("reset", 0, 0.1f); + ImGui::TableNextColumn(); + + ImGui::Text("Unlit"); + ImGui::TableNextColumn(); + + bool unlit = material->data.u_Unlit == 1; + ImGui::Checkbox("Unlit", &unlit); + material->data.u_Unlit = (int)unlit; + } + ImGui::EndTable(); + } + + const auto TexturePanelHeight = 100; + const ImVec2 TexturePanelSize = ImVec2(0, TexturePanelHeight); + bool AlbedoOpened; + { + UIFont boldfont = UIFont(Fonts::Bold); + ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(0.f, 0.f)); + ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, ImVec2(0.f, 8.f)); + AlbedoOpened = ImGui::CollapsingHeader("Albedo", ImGuiTreeNodeFlags_DefaultOpen); + ImGui::PopStyleVar(2); + } + + if (AlbedoOpened) + { + ImGui::BeginChild("##albedo", TexturePanelSize, true); + { + uint32_t textureID = 0; + if (material->HasAlbedo()) + { + textureID = material->m_Albedo->GetID(); + } + else + { + textureID = TextureManager::Get()->GetTexture("default")->GetID(); + } + + if (ImGui::ImageButtonEx(ImGui::GetCurrentWindow()->GetID("#image1"), (void*)textureID, ImVec2(80, 80), ImVec2(0, 1), ImVec2(1, 0), ImVec2(2, 2), ImVec4(0, 0, 0, 0), ImVec4(1, 1, 1, 1))) + { + std::string texture = FileDialog::OpenFile("*.png | *.jpg"); + if (texture != "") + { + material->SetAlbedo(TextureManager::Get()->GetTexture(texture)); + } + } + + if (ImGui::BeginPopupContextWindow()) + { + if (ImGui::MenuItem("Clear Texture")) + { + material->m_Albedo = nullptr; + } + ImGui::EndPopup(); + } + + ImGui::SameLine(); + ImGui::ColorEdit3("Color", &material->data.m_AlbedoColor.r); + } + ImGui::EndChild(); + } + + if (ImGui::CollapsingHeader("Normal", ImGuiTreeNodeFlags_DefaultOpen)) + { + ImGui::BeginChild("##normal", TexturePanelSize, true); + { + uint32_t textureID = 0; + if (material->HasNormal()) + { + textureID = material->m_Normal->GetID(); + } + else + { + textureID = TextureManager::Get()->GetTexture("default")->GetID(); + } + + if (ImGui::ImageButtonEx(ImGui::GetCurrentWindow()->GetID("#image3"), (void*)textureID, ImVec2(80, 80), ImVec2(0, 1), ImVec2(1, 0), ImVec2(2, 2), ImVec4(0, 0, 0, 1), ImVec4(1, 1, 1, 1))) + { + std::string texture = FileDialog::OpenFile("*.png | *.jpg"); + if (texture != "") + { + material->SetNormal(TextureManager::Get()->GetTexture(texture)); + } + } + + if (ImGui::BeginPopupContextWindow()) + { + if (ImGui::MenuItem("Clear Texture")) + { + material->m_Normal = nullptr; + } + ImGui::EndPopup(); + } + } + ImGui::EndChild(); + } + + if (ImGui::CollapsingHeader("AO", ImGuiTreeNodeFlags_DefaultOpen)) + { + ImGui::BeginChild("##ao", TexturePanelSize, true); + { + uint32_t textureID = 0; + if (material->HasAO()) + { + textureID = material->m_AO->GetID(); + } + else + { + textureID = TextureManager::Get()->GetTexture("default")->GetID(); + } + + if (ImGui::ImageButtonEx(ImGui::GetCurrentWindow()->GetID("#image2"), (void*)textureID, ImVec2(80, 80), ImVec2(0, 1), ImVec2(1, 0), ImVec2(2, 2), ImVec4(1, 1, 1, 1), ImVec4(1, 1, 1, 1))) + { + std::string texture = FileDialog::OpenFile("Image files (*.png) | *.png | Image files (*.jpg) | *.jpg"); + if (texture != "") + { + material->SetAO(TextureManager::Get()->GetTexture(texture)); + } + } + + if (ImGui::BeginPopupContextWindow()) + { + if (ImGui::MenuItem("Clear Texture")) + { + material->m_AO = nullptr; + } + ImGui::EndPopup(); + } + } + ImGui::EndChild(); + } + + if (ImGui::CollapsingHeader("Metalness", ImGuiTreeNodeFlags_DefaultOpen)) + { + ImGui::BeginChild("##metalness", TexturePanelSize, true); + { + uint32_t textureID = 0; + if (material->HasMetalness()) + { + textureID = material->m_Metalness->GetID(); + } + else + { + textureID = TextureManager::Get()->GetTexture("default")->GetID(); + } + + if (ImGui::ImageButtonEx(ImGui::GetCurrentWindow()->GetID("#image4"), (void*)textureID, ImVec2(80, 80), ImVec2(0, 1), ImVec2(1, 0), ImVec2(2, 2), ImVec4(0, 0, 0, 1), ImVec4(1, 1, 1, 1))) + { + std::string texture = FileDialog::OpenFile("*.png | *.jpg"); + if (texture != "") + { + material->SetMetalness(TextureManager::Get()->GetTexture(texture)); + } + } + + if (ImGui::BeginPopupContextWindow()) + { + if (ImGui::MenuItem("Clear Texture")) + { + material->m_Metalness = nullptr; + } + ImGui::EndPopup(); + } + + ImGui::SameLine(); + ImGui::DragFloat("Value##4", &material->data.u_MetalnessValue, 0.01f, 0.0f, 1.0f); + } + ImGui::EndChild(); + } + + if (ImGui::CollapsingHeader("Roughness", ImGuiTreeNodeFlags_DefaultOpen)) + { + ImGui::BeginChild("##roughness", TexturePanelSize, true); + { + uint32_t textureID = 0; + if (material->HasRoughness()) + { + textureID = material->m_Roughness->GetID(); + } + else + { + textureID = TextureManager::Get()->GetTexture("default")->GetID(); + } + + if (ImGui::ImageButtonEx(ImGui::GetCurrentWindow()->GetID("#image5"), (void*)textureID, ImVec2(80, 80), ImVec2(0, 1), ImVec2(1, 0), ImVec2(2, 2), ImVec4(0, 0, 0, 0), ImVec4(1, 1, 1, 1))) + { + std::string texture = FileDialog::OpenFile("*.png | *.jpg"); + if (texture != "") + { + material->SetRoughness(TextureManager::Get()->GetTexture(texture)); + } + } + + if (ImGui::BeginPopupContextWindow()) + { + if (ImGui::MenuItem("Clear Texture")) + { + material->m_Roughness = nullptr; + } + ImGui::EndPopup(); + } + } + ImGui::EndChild(); + } +} + +void EditorSelectionPanel::DrawProjectPanel(Ref project) +{ + ImGui::InputText("Project Name", &project->Name); + ImGui::InputTextMultiline("Project Description", &project->Description); + + if (ImGui::Button("Locate")) + { + + } + ImGui::SameLine(); + ImGui::InputText("Trenchbroom Path", &project->TrenchbroomPath); +} + diff --git a/Editor/src/Windows/EditorSelectionPanel.h b/Editor/src/Windows/EditorSelectionPanel.h index 0a2c332e..82faf812 100644 --- a/Editor/src/Windows/EditorSelectionPanel.h +++ b/Editor/src/Windows/EditorSelectionPanel.h @@ -8,6 +8,8 @@ #include "../ComponentsPanel/ScriptPanel.h" #include "../ComponentsPanel/MeshPanel.h" #include "../ComponentsPanel/QuakeMapPanel.h" +#include "../Actions/EditorSelection.h" +#include class EditorSelectionPanel { private: @@ -16,6 +18,9 @@ private: ScriptPanel mScriptPanel; MeshPanel mMeshPanel; QuakeMapPanel mQuakeMapPanel; + + Ref currentFile; + Ref selectedResource; public: EditorSelectionPanel(); @@ -27,4 +32,9 @@ public: void DrawFile(Nuake::File* file); void DrawResource(Nuake::Resource resource); + +private: + void ResolveFile(Ref file); + void DrawMaterialPanel(Ref material); + void DrawProjectPanel(Ref project); }; \ No newline at end of file diff --git a/Editor/src/Windows/FileSystemUI.cpp b/Editor/src/Windows/FileSystemUI.cpp index 578677d8..a0f0e27d 100644 --- a/Editor/src/Windows/FileSystemUI.cpp +++ b/Editor/src/Windows/FileSystemUI.cpp @@ -39,6 +39,8 @@ exit() {\ }\ }"; +#include + namespace Nuake { // TODO: add filetree in same panel void FileSystemUI::Draw() @@ -176,6 +178,49 @@ namespace Nuake { return SplitterBehavior(bb, id, split_vertically ? ImGuiAxis_X : ImGuiAxis_Y, size1, size2, min_size1, min_size2, 0.0f); } + void FileSystemUI::DrawContextMenu() + { + if (ImGui::BeginPopupContextWindow()) + { + if (ImGui::MenuItem("New folder")) + { + + } + + if (ImGui::MenuItem("New Scene")) + { + } + + if (ImGui::BeginMenu("New Resource")) + { + if (ImGui::MenuItem("Material")) + { + std::string path = FileDialog::SaveFile("*.material"); + if (!String::EndsWith(path, ".material")) + { + path += ".material"; + } + + if (path != "") + { + Ref material = CreateRef(); + material->IsEmbedded = false; + auto jsonData = material->Serialize(); + + FileSystem::BeginWriteFile(path); + FileSystem::WriteLine(jsonData.dump(4)); + FileSystem::EndWriteFile(); + } + } + + ImGui::EndMenu(); + } + + ImGui::EndPopup(); + } + + } + float h = 200; static float sz1 = 300; static float sz2 = 300; @@ -230,7 +275,6 @@ namespace Nuake { currentParent = currentParent->Parent; } - avail = ImGui::GetContentRegionAvail(); if (ImGui::BeginChild("Wrapper", avail)) { @@ -300,6 +344,7 @@ namespace Nuake { for (auto f : m_CurrentDirectory->Files) { DrawFile(f); + if (i - 1 % amount != 0) ImGui::TableNextColumn(); else @@ -307,63 +352,8 @@ namespace Nuake { i++; } } - - - - if (ImGui::BeginPopupContextWindow()) - { - if (ImGui::MenuItem("New Wren script")) - { - ImGui::OpenPopup("new_file"); - - } - - if (ImGui::MenuItem("New interface script")) - { - } - - if (ImGui::MenuItem("New Scene")) - { - } - - if (ImGui::MenuItem("New folder")) - { - } - - if (ImGui::MenuItem("New interface")) - { - } - - if (ImGui::MenuItem("New stylesheet")) - { - } - - ImGui::EndPopup(); - } - - if (ImGui::BeginPopup("new_file")) - { - static char name[32] = "NewWrenScript"; - char buf[64]; - sprintf(buf, "Button: %s###Button", name); // ### operator override ID ignoring the preceding label - - ImGui::Text("Edit name:"); - ImGui::InputText("##edit", name, IM_ARRAYSIZE(name)); - if (ImGui::Button("Close")) - ImGui::CloseCurrentPopup(); - if (ImGui::IsKeyPressed(ImGui::GetKeyIndex(ImGuiKey_Enter)) && name != "") - { - FileSystem::BeginWriteFile(m_CurrentDirectory->fullPath + name + ".wren"); - - FileSystem::WriteLine(TEMPLATE_SCRIPT_BEGIN + name + TEMPLATE_SCRIPT_END); - - FileSystem::EndWriteFile(); - ImGui::CloseCurrentPopup(); - } - - ImGui::EndPopup(); - } + DrawContextMenu(); ImGui::EndTable(); } diff --git a/Editor/src/Windows/FileSystemUI.h b/Editor/src/Windows/FileSystemUI.h index 6ce45ae0..aa630bed 100644 --- a/Editor/src/Windows/FileSystemUI.h +++ b/Editor/src/Windows/FileSystemUI.h @@ -25,5 +25,6 @@ namespace Nuake { bool EntityContainsItself(Entity source, Entity target); void DrawFile(Ref file); void DrawDirectoryExplorer(); + void DrawContextMenu(); }; } diff --git a/Editor/src/Windows/WelcomeWindow.cpp b/Editor/src/Windows/WelcomeWindow.cpp index 21ccd803..32b98272 100644 --- a/Editor/src/Windows/WelcomeWindow.cpp +++ b/Editor/src/Windows/WelcomeWindow.cpp @@ -230,7 +230,7 @@ namespace Nuake void WelcomeWindow::ParseRecentFile() { - if (!FileSystem::FileExists(_RecentProjectFilePath)) + if (!FileSystem::FileExists(_RecentProjectFilePath, true)) { FileSystem::BeginWriteFile(_RecentProjectFilePath); FileSystem::WriteLine(_RecentProjectFileDefaultContent); diff --git a/Nuake/src/Core/FileSystem.cpp b/Nuake/src/Core/FileSystem.cpp index f5176395..5bdd90c3 100644 --- a/Nuake/src/Core/FileSystem.cpp +++ b/Nuake/src/Core/FileSystem.cpp @@ -93,9 +93,11 @@ namespace Nuake return false; } - bool FileSystem::FileExists(const std::string path) + bool FileSystem::FileExists(const std::string path, bool absolute) { - std::ifstream f(path.c_str()); + std::string fullPath = absolute ? path : FileSystem::Root + path; + + std::ifstream f(fullPath.c_str()); return f.good(); } diff --git a/Nuake/src/Core/FileSystem.h b/Nuake/src/Core/FileSystem.h index 413773b7..0a4ba20a 100644 --- a/Nuake/src/Core/FileSystem.h +++ b/Nuake/src/Core/FileSystem.h @@ -35,7 +35,7 @@ namespace Nuake static void GetDirectories(); static bool DirectoryExists(const std::string path); - static bool FileExists(const std::string path); + static bool FileExists(const std::string path, bool absolute = false); static std::string ReadFile(const std::string& path, bool absolute = false); @@ -61,6 +61,11 @@ namespace Nuake std::string GetAbsolutePath() { return AbsolutePath; } Ref GetParent() { return Parent; } + std::string Read() + { + return FileSystem::ReadFile(AbsolutePath); + } + File(Ref parentDir, const std::string& absolutePath, const std::string& name, const std::string& type) { AbsolutePath = absolutePath; diff --git a/Nuake/src/Rendering/SceneRenderer.cpp b/Nuake/src/Rendering/SceneRenderer.cpp index 9c5dc151..92155180 100644 --- a/Nuake/src/Rendering/SceneRenderer.cpp +++ b/Nuake/src/Rendering/SceneRenderer.cpp @@ -144,8 +144,11 @@ namespace Nuake { for (auto e : meshView) { auto [transform, mesh] = meshView.get(e); - for (auto& m : mesh.ModelResource->GetMeshes()) - Renderer::SubmitMesh(m, transform.GetGlobalTransform()); + if (mesh.ModelResource != nullptr) + { + for (auto& m : mesh.ModelResource->GetMeshes()) + Renderer::SubmitMesh(m, transform.GetGlobalTransform()); + } } for (auto e : quakeView) diff --git a/Nuake/src/Rendering/Textures/Material.h b/Nuake/src/Rendering/Textures/Material.h index b7a2c71d..29d2f811 100644 --- a/Nuake/src/Rendering/Textures/Material.h +++ b/Nuake/src/Rendering/Textures/Material.h @@ -135,7 +135,7 @@ namespace Nuake if (j.contains("Path")) { this->Path = j["Path"]; - if (FileSystem::FileExists(FileSystem::Root + Path)) + if (FileSystem::FileExists(Path)) { std::string content = FileSystem::ReadFile(Path); Deserialize(content); diff --git a/Nuake/src/Resource/Model.h b/Nuake/src/Resource/Model.h index 89db3d04..c0511fa6 100644 --- a/Nuake/src/Resource/Model.h +++ b/Nuake/src/Resource/Model.h @@ -11,7 +11,6 @@ namespace Nuake { private: std::vector> m_Meshes; - public: Model(); Model(const std::string path); diff --git a/Nuake/src/Resource/ModelLoader.cpp b/Nuake/src/Resource/ModelLoader.cpp index d044a060..cd4e1600 100644 --- a/Nuake/src/Resource/ModelLoader.cpp +++ b/Nuake/src/Resource/ModelLoader.cpp @@ -212,7 +212,7 @@ namespace Nuake } std::string texturePath = FileSystem::Root + modelDir + path; - if (!FileSystem::FileExists(texturePath)) + if (!FileSystem::FileExists(texturePath, true)) { std::string textureNotFoundmsg = "Texture file couldn't be found: " + texturePath; Logger::Log(textureNotFoundmsg, Nuake::LOG_TYPE::WARNING); diff --git a/Nuake/src/Resource/Project.cpp b/Nuake/src/Resource/Project.cpp index 14ad4e25..63f81cb1 100644 --- a/Nuake/src/Resource/Project.cpp +++ b/Nuake/src/Resource/Project.cpp @@ -131,7 +131,7 @@ namespace Nuake if (scenePath == "") // Not set correctly. return true; - if (!FileSystem::FileExists(FileSystem::Root + scenePath)) + if (!FileSystem::FileExists(scenePath)) return true; std::string sceneContent = FileSystem::ReadFile(scenePath, false); diff --git a/Nuake/src/Resource/Resource.h b/Nuake/src/Resource/Resource.h index c72a1b7b..e78f19e6 100644 --- a/Nuake/src/Resource/Resource.h +++ b/Nuake/src/Resource/Resource.h @@ -8,7 +8,7 @@ namespace Nuake class Resource { public: - UUID Id; + UUID ID; bool IsEmbedded = false; std::string Path; // Only if embedded diff --git a/Nuake/src/Resource/ResourceLoader.h b/Nuake/src/Resource/ResourceLoader.h new file mode 100644 index 00000000..ed82010a --- /dev/null +++ b/Nuake/src/Resource/ResourceLoader.h @@ -0,0 +1,70 @@ +#pragma once +#include "src/Core/Core.h" +#include "src/Core/Logger.h" +#include "src/Resource/Resource.h" +#include "src/Resource/ResourceManager.h" +#include "src/Rendering/Textures/Material.h" + +namespace Nuake +{ + class ResourceLoader + { + private: + const std::string FILE_NOT_FOUND = "[Resource Loader] File doesn't exists. \n "; + const std::string WRONG_EXTENSION = "[Resource Loader] Resource type mismatch file extension. \n expected: "; + const std::string MATERIAL_EXT = ".material"; + + public: + ResourceLoader() = default; + ~ResourceLoader() = default; + + + static Ref LoadResource(const std::string& path) + { + const std::string FILE_NOT_FOUND = "[Resource Loader] File doesn't exists. \n "; + const std::string WRONG_EXTENSION = "[Resource Loader] Resource type mismatch file extension. \n expected: "; + const std::string MATERIAL_EXT = ".material"; + if (!FileSystem::FileExists(path)) + { + Logger::Log(FILE_NOT_FOUND + path, LOG_TYPE::WARNING); + return nullptr; + } + + if (!String::EndsWith(path, MATERIAL_EXT)) + { + std::string message = WRONG_EXTENSION + MATERIAL_EXT + " actual: " + path; + Logger::Log(message, LOG_TYPE::WARNING); + } + + std::string content = FileSystem::ReadFile(path); + json j = json::parse(content); + + UUID uuid = ReadUUID(j); + + // Check if resource is already loaded. + if (ResourceManager::IsResourceLoaded(uuid)) + { + return ResourceManager::GetResource(uuid); + } + + Ref material = CreateRef(); + material->ID = uuid; + material->Path = path; + material->Deserialize(j.dump()); + ResourceManager::RegisterResource(material); + + return material; + } + + private: + static UUID ReadUUID(json j) + { + if (j.contains("UUID")) + { + return UUID(j["UUID"]); + } + + return UUID(); + } + }; +} \ No newline at end of file diff --git a/Nuake/src/Resource/ResourceManager.cpp b/Nuake/src/Resource/ResourceManager.cpp new file mode 100644 index 00000000..0d84f60f --- /dev/null +++ b/Nuake/src/Resource/ResourceManager.cpp @@ -0,0 +1,6 @@ +#include "ResourceManager.h" + +namespace Nuake +{ + std::map> ResourceManager::m_Resources; +} diff --git a/Nuake/src/Resource/ResourceManager.h b/Nuake/src/Resource/ResourceManager.h index d286ce35..866999a9 100644 --- a/Nuake/src/Resource/ResourceManager.h +++ b/Nuake/src/Resource/ResourceManager.h @@ -16,15 +16,20 @@ namespace Nuake { template static Ref LoadResource(const std::string& path); - void RegisterResource(Ref resource) + static void RegisterResource(Ref resource) { - m_Resources[resource->Id] = resource; + m_Resources[resource->ID] = resource; + } + + static bool IsResourceLoaded(const UUID& uuid) + { + return m_Resources.find(uuid) != m_Resources.end(); } template static Ref GetResource(const UUID& uuid) { - return reinterpret_pointer_cast>(m_Resources[uuid]); + return std::static_pointer_cast(m_Resources[uuid]); } }; } \ No newline at end of file diff --git a/Nuake/src/Scene/Scene.cpp b/Nuake/src/Scene/Scene.cpp index adf30ea6..8f79c1c7 100644 --- a/Nuake/src/Scene/Scene.cpp +++ b/Nuake/src/Scene/Scene.cpp @@ -157,7 +157,8 @@ namespace Nuake { { std::vector allEntities; auto view = m_Registry.view(); - for (auto e : view) { + for (auto e : view) + { Entity newEntity(e, this); // Check if valid for deleted entities. @@ -177,6 +178,8 @@ namespace Nuake { if (namec.Name == name) return Entity{ e, this }; } + + return Entity(); } Entity Scene::CreateEntity(const std::string& name) @@ -314,6 +317,7 @@ namespace Nuake { sceneCopy->CreateEntity(name, id); } + CopyComponent(sceneCopy->m_Registry, this->m_Registry); CopyComponent(sceneCopy->m_Registry, this->m_Registry); CopyComponent(sceneCopy->m_Registry, this->m_Registry); CopyComponent(sceneCopy->m_Registry, this->m_Registry); diff --git a/Nuake/src/Scene/Scene.h b/Nuake/src/Scene/Scene.h index 695d8f27..7b4febb5 100644 --- a/Nuake/src/Scene/Scene.h +++ b/Nuake/src/Scene/Scene.h @@ -32,7 +32,6 @@ namespace Nuake { // In the scene constructor. std::vector> m_Systems; Ref m_Environement; - std::map> m_Resources; public: Ref m_EditorCamera; diff --git a/Nuake/src/Scripting/Modules/SceneModule.h b/Nuake/src/Scripting/Modules/SceneModule.h index dd982a84..2de9afb4 100644 --- a/Nuake/src/Scripting/Modules/SceneModule.h +++ b/Nuake/src/Scripting/Modules/SceneModule.h @@ -67,49 +67,41 @@ namespace Nuake { std::string name = wrenGetSlotString(vm, 2); Entity ent = Entity((entt::entity)handle, Engine::GetCurrentScene().get()); - if (name == "Transform") + bool result = false; + if (handle == -1) { - bool result = ent.HasComponent(); - wrenSetSlotBool(vm, 0, result); - } - if (name == "Light") - { - bool result = ent.HasComponent(); - wrenSetSlotBool(vm, 0, result); - } - if (name == "QuakeMap") - { - bool result = ent.HasComponent(); wrenSetSlotBool(vm, 0, result); + return; } + + if (name == "Transform") result = ent.HasComponent(); + if (name == "Light") result = ent.HasComponent(); + if (name == "QuakeMap") result = ent.HasComponent(); + if (name == "CharacterController") { - bool result = ent.HasComponent(); - wrenSetSlotBool(vm, 0, result); + result = ent.HasComponent(); } if (name == "Camera") { - bool result = ent.HasComponent(); - wrenSetSlotBool(vm, 0, result); + result = ent.HasComponent(); } if (name == "Script") { - bool result = ent.HasComponent(); - wrenSetSlotBool(vm, 0, result); + result = ent.HasComponent(); } if (name == "Rigidbody") { - bool result = ent.HasComponent(); - wrenSetSlotBool(vm, 0, result); + result = ent.HasComponent(); } if (name == "Script") { - bool result = ent.HasComponent(); - wrenSetSlotBool(vm, 0, result); + result = ent.HasComponent(); } if (name == "Brush") { - bool result = ent.HasComponent(); - wrenSetSlotBool(vm, 0, result); + result = ent.HasComponent(); } + + wrenSetSlotBool(vm, 0, result); } static void GetScript(WrenVM* vm) diff --git a/Nuake/src/Window.cpp b/Nuake/src/Window.cpp index 7c83c667..f1521002 100644 --- a/Nuake/src/Window.cpp +++ b/Nuake/src/Window.cpp @@ -164,7 +164,7 @@ namespace Nuake { colors[ImGuiCol_WindowBg] = ImVec4(0.08f, 0.08f, 0.08f, 1.00f); colors[ImGuiCol_ChildBg] = ImVec4(0.15f, 0.15f, 0.15f, 1.00f); colors[ImGuiCol_PopupBg] = ImVec4(0.08f, 0.08f, 0.08f, 0.94f); - colors[ImGuiCol_Border] = ImVec4(0.08f, 0.10f, 0.12f, 0.00f); + colors[ImGuiCol_Border] = ImVec4(0.11f, 0.11f, 0.11f, 1.00f); colors[ImGuiCol_BorderShadow] = ImVec4(0.00f, 0.00f, 0.00f, 0.00f); colors[ImGuiCol_FrameBg] = ImVec4(0.06f, 0.06f, 0.06f, 1.00f); colors[ImGuiCol_FrameBgHovered] = ImVec4(0.09f, 0.09f, 0.09f, 1.00f);