From d33c4f33df8aa9ce2409249c91f6a689a8b65487 Mon Sep 17 00:00:00 2001 From: Antoine Pilote Date: Sun, 28 Jul 2024 14:53:12 -0400 Subject: [PATCH] Started quake map importer --- Editor/src/Windows/EditorInterface.cpp | 10 +++ Editor/src/Windows/EditorInterface.h | 3 + Editor/src/Windows/MapImporterWindow.cpp | 107 +++++++++++++++++++++++ Editor/src/Windows/MapImporterWindow.h | 19 ++++ Nuake/src/Core/FileSystem.cpp | 2 +- Nuake/src/Core/FileSystem.h | 3 +- 6 files changed, 142 insertions(+), 2 deletions(-) create mode 100644 Editor/src/Windows/MapImporterWindow.cpp create mode 100644 Editor/src/Windows/MapImporterWindow.h diff --git a/Editor/src/Windows/EditorInterface.cpp b/Editor/src/Windows/EditorInterface.cpp index 6d0b4a66..ad4ecd0a 100644 --- a/Editor/src/Windows/EditorInterface.cpp +++ b/Editor/src/Windows/EditorInterface.cpp @@ -2231,6 +2231,11 @@ namespace Nuake { { m_ShowTrenchbroomConfigurator = !m_ShowTrenchbroomConfigurator; } + + if (ImGui::MenuItem("Map Importer", NULL, m_ShowMapImporter)) + { + m_ShowMapImporter = !m_ShowMapImporter; + } ImGui::EndMenu(); } if (ImGui::BeginMenu("View")) @@ -2421,6 +2426,11 @@ namespace Nuake { m_TrenchhbroomConfigurator.Draw(); } + if (m_ShowMapImporter) + { + m_MapImporter.Draw(); + } + DrawMenuBar(); DrawMenuBars(); DrawStatusBar(); diff --git a/Editor/src/Windows/EditorInterface.h b/Editor/src/Windows/EditorInterface.h index 0b17269a..07e81cd0 100644 --- a/Editor/src/Windows/EditorInterface.h +++ b/Editor/src/Windows/EditorInterface.h @@ -13,6 +13,7 @@ #include "../Windows/TrenchbroomConfiguratorWindow.h" #include "../Commands/CommandBuffer.h" #include "../Commands/ICommand.h" +#include "MapImporterWindow.h" using namespace NuakeEditor; @@ -36,6 +37,7 @@ namespace Nuake bool m_IsHoveringViewport = false; bool m_IsViewportFocused = false; bool m_ShowTrenchbroomConfigurator = false; + bool m_ShowMapImporter = false; Vector2 m_ViewportPos = {0, 0}; Vector2 m_ViewportSize = {}; @@ -56,6 +58,7 @@ namespace Nuake EditorSelection Selection; EditorSelectionPanel SelectionPanel; TrenchbroomConfiguratorWindow m_TrenchhbroomConfigurator; + MapImporterWindow m_MapImporter; EditorInterface(CommandBuffer& commandBuffer); diff --git a/Editor/src/Windows/MapImporterWindow.cpp b/Editor/src/Windows/MapImporterWindow.cpp new file mode 100644 index 00000000..1d89947a --- /dev/null +++ b/Editor/src/Windows/MapImporterWindow.cpp @@ -0,0 +1,107 @@ +#include "MapImporterWindow.h" +#include +#include "../Misc/InterfaceFonts.h" +#include +#include +#include + +void MapImporterWindow::Draw() +{ + auto& io = ImGui::GetIO(); + ImGui::SetNextWindowPos(ImVec2(io.DisplaySize.x * 0.5f, io.DisplaySize.y * 0.5f), ImGuiCond_FirstUseEver, ImVec2(0.5f, 0.5f)); + ImGui::SetNextWindowSizeConstraints(ImVec2(600, 400), ImVec2(1920, 1080)); + if (ImGui::Begin("Map Importer")) + { + ImGui::PushFont(FontManager::GetFont(Bold)); + ImGui::Text("What is this?"); + ImGui::PopFont(); + + ImGui::TextWrapped("This imported will convert any Quake .map file into a .map that uses Nuake Material system."); + + ImGui::PushFont(FontManager::GetFont(Bold)); + ImGui::Text("1. Select a quake .map to import"); + ImGui::PopFont(); + + std::string buttonLbl = "Empty"; + + if (m_MapToImport) + { + buttonLbl = m_MapToImport->GetRelativePath(); + } + + using namespace Nuake; + if (ImGui::Button(buttonLbl.c_str())) + { + std::string selectedFile = FileDialog::OpenFile("*.map"); + std::string relativePath = FileSystem::AbsoluteToRelative(selectedFile); + if (FileSystem::FileExists(relativePath)) + { + if (Ref file = FileSystem::GetFile(relativePath); file) + { + m_MapToImport = file; + } + } + } + + if (ImGui::BeginDragDropTarget()) + { + if (const ImGuiPayload* payload = ImGui::AcceptDragDropPayload("_Map")) + { + std::string fullPath = std::string(reinterpret_cast(payload->Data), 256); + std::string relPath = FileSystem::AbsoluteToRelative(fullPath); + if (FileSystem::FileExists(relPath)) + { + if (Ref file = FileSystem::GetFile(relPath); file) + { + m_MapToImport = file; + } + } + else + { + Logger::Log("Failed to import map file. File doesn't exists", "map importer", CRITICAL); + } + } + ImGui::EndDragDropTarget(); + } + + auto wads = ScanUsedWads(); + + if (Nuake::UI::PrimaryButton("Export")) + { + m_MapToImport = nullptr; + } + } + ImGui::End(); +} + +std::vector MapImporterWindow::ScanUsedWads() +{ + if (!m_MapToImport) + { + return std::vector(); + } + + using namespace Nuake; + + const std::string mapContent = m_MapToImport->Read(); + + std::vector usedWads; + std::regex pattern("\"wad\"\\s*\"([^\"]+)\""); + std::smatch match; + + // Search the input string using the regular expression + if (std::regex_search(mapContent, match, pattern)) + { + const std::string fullString = match[1].str(); + + // Split multiple wads in case. + auto separatedWads = String::Split(fullString, ';'); + for (auto& wad : separatedWads) + { + usedWads.push_back(wad); + } + } + + // If no match is found, return an empty string + return usedWads; +} \ No newline at end of file diff --git a/Editor/src/Windows/MapImporterWindow.h b/Editor/src/Windows/MapImporterWindow.h new file mode 100644 index 00000000..a000dbe1 --- /dev/null +++ b/Editor/src/Windows/MapImporterWindow.h @@ -0,0 +1,19 @@ +#pragma once + +#include +#include + +class MapImporterWindow +{ +public: + MapImporterWindow() = default; + ~MapImporterWindow() = default; + + void Update(); + void Draw(); + +private: + Ref m_MapToImport = nullptr; + + std::vector ScanUsedWads(); +}; \ No newline at end of file diff --git a/Nuake/src/Core/FileSystem.cpp b/Nuake/src/Core/FileSystem.cpp index 648722b0..03448dd6 100644 --- a/Nuake/src/Core/FileSystem.cpp +++ b/Nuake/src/Core/FileSystem.cpp @@ -180,7 +180,7 @@ namespace Nuake bool FileSystem::MakeDirectory(const std::string& path, bool absolute) { - return std::filesystem::create_directory(absolute ? path : FileSystem::Root + path); + return std::filesystem::create_directories(absolute ? path : FileSystem::Root + path); } bool FileSystem::FileExists(const std::string& path, bool absolute) diff --git a/Nuake/src/Core/FileSystem.h b/Nuake/src/Core/FileSystem.h index 96c4f9f2..0acb0d25 100644 --- a/Nuake/src/Core/FileSystem.h +++ b/Nuake/src/Core/FileSystem.h @@ -203,9 +203,10 @@ namespace Nuake return "File"; } + std::string Read() { - return FileSystem::ReadFile(AbsolutePath); + return FileSystem::ReadFile(AbsolutePath, true); } bool IsValid()