From fda226425142b122d90586b1fd678bc4e8136873 Mon Sep 17 00:00:00 2001 From: Joeyrp Date: Tue, 9 Nov 2021 15:29:22 -0500 Subject: [PATCH] New project generation working --- docs/core.todo | 2 +- docs/editor.todo | 5 ++ docs/game.todo | 19 ++++-- src/gui/fileBrowser.cpp | 30 ++++++---- src/gui/fileBrowser.h | 4 ++ src/run_modes/editor/CMakeLists.txt | 3 +- src/run_modes/editor/editor.cpp | 10 +++- src/run_modes/editor/editor.h | 4 ++ src/run_modes/editor/panels/mainPanel.cpp | 18 ------ src/run_modes/editor/project/project.cpp | 72 +++++++++++++++++++++++ src/run_modes/editor/project/project.h | 40 +++++++++++++ 11 files changed, 169 insertions(+), 38 deletions(-) create mode 100644 src/run_modes/editor/project/project.cpp diff --git a/docs/core.todo b/docs/core.todo index 4c9c36e..042708d 100644 --- a/docs/core.todo +++ b/docs/core.todo @@ -36,7 +36,7 @@ Core: ✔ Allow opening of listed directories @done (11/8/2021, 3:16:26 PM) ✔ Add indication that an item is directory @done (11/8/2021, 6:19:20 PM) ✔ Sort items by type (Directories should come first) @done (11/8/2021, 6:26:01 PM) - ☐ Allow the user to type in a filename + ✔ Allow the user to type in a filename @done (11/9/2021, 3:26:16 PM) ✔ Add a "New Directory" button @done (11/8/2021, 7:15:51 PM) Input: diff --git a/docs/editor.todo b/docs/editor.todo index 5f3453f..dae5b32 100644 --- a/docs/editor.todo +++ b/docs/editor.todo @@ -14,6 +14,11 @@ Editor: ☐ Raw Sound importer class ☐ Raw font file importer class + Project (Class for loading and tracking project data): + ✔ Generate new project at given location @done (11/9/2021, 3:26:03 PM) + ☐ Save project data + ☐ Open existing project + GUI Panels: Project Overview (Tree view): diff --git a/docs/game.todo b/docs/game.todo index 5e372cc..d919b81 100644 --- a/docs/game.todo +++ b/docs/game.todo @@ -8,14 +8,21 @@ Game: Scene: ☐ Manage scene scripts - ☐ Manage game objects in scene + ☐ Manage the master list of game objects in scene + ☐ Contains a World (World System) - Manage list of Regions: - ☐ Track which regions should be loaded + + World System: + ☐ Track/manage loaded regions + ☐ Render loaded Regions + Camera: + ☐ Current Region + ☐ Current Position within Region - Region: - ☐ List of renderable images for each layer + Region: + ☐ List of renderable images for each layer + ☐ List of game objects (by reference) in this Region Game Object: ☐ List of components @@ -30,4 +37,4 @@ Game: ☐ Audio Listener Animations: - ☐ Animated Sprite clas \ No newline at end of file + ☐ Animated Sprite class \ No newline at end of file diff --git a/src/gui/fileBrowser.cpp b/src/gui/fileBrowser.cpp index 627a6df..60244cf 100644 --- a/src/gui/fileBrowser.cpp +++ b/src/gui/fileBrowser.cpp @@ -21,6 +21,10 @@ namespace lunarium : mIsOpen(false), mSelectionMode(SelectionMode::FILES_ONLY), mWarnOnExisting(false), mpFolderIcon(nullptr), mResult(Result::CANCEL), mpNewFolderIcon(nullptr), mpUpFolderIcon(nullptr) { + mPrompt = "Select an item"; + memset(mDirNameBuffer, 0, mBufferSize); + memset(mInputBuffer, 0, mBufferSize); + int x,y,n; unsigned char* pData = stbi_load("dir_icon.png", &x, &y, &n, 0); mpFolderIcon = new Image(pData, x, y, ImageFormat::RGBA); @@ -72,18 +76,21 @@ namespace lunarium return false; } + ImGui::TextUnformatted(mPrompt.c_str()); + ImGui::Separator(); + ImVec2 iconSize(mpNewFolderIcon->GetWidth(), mpNewFolderIcon->GetHeight()); if (ImGui::ImageButton((ImTextureID) mpNewFolderIcon->GetGLTextureID(), iconSize)) { ImGui::OpenPopup("New Folder Name"); - memset(mInputBuffer, 0, mBufferSize); + memset(mDirNameBuffer, 0, mBufferSize); } if (ImGui::BeginPopup("New Folder Name")) { - if (ImGui::InputText("Folder Name", mInputBuffer, mBufferSize, ImGuiInputTextFlags_EnterReturnsTrue)) + if (ImGui::InputText("Folder Name", mDirNameBuffer, mBufferSize, ImGuiInputTextFlags_EnterReturnsTrue)) { - std::filesystem::create_directory(mInputBuffer); + std::filesystem::create_directory(mDirNameBuffer); ImGui::CloseCurrentPopup(); ReloadItems(); } @@ -104,13 +111,11 @@ namespace lunarium DoListBox(); ImGui::Separator(); - if (mpSelectedItem) - { - ImGui::Text("Selected: %s", mpSelectedItem->filename().string().c_str()); - } - else + + if (ImGui::InputText("selection", mInputBuffer, mBufferSize)) { - ImGui::Text("Selected: "); + mInputSelection = mCurrentDirectory / mInputBuffer; + mpSelectedItem = &mInputSelection; } ImGui::Separator(); @@ -135,6 +140,11 @@ namespace lunarium { mWarnOnExisting = warn; } + + void FileBrowser::SetPrompt(std::string prompt) + { + mPrompt = prompt; + } /// returns nullptr if the dialog has not been opened yet @@ -184,7 +194,7 @@ namespace lunarium //////////////////////////////////////////////////////////// void FileBrowser::DoListBox() { - if (ImGui::BeginListBox("", ImVec2(ImGui::GetWindowWidth(), ImGui::GetWindowHeight() * .65f))) + if (ImGui::BeginListBox("", ImVec2(ImGui::GetWindowWidth(), ImGui::GetWindowHeight() * .575f))) { for (int i = 0; i < mItemsInDir.size(); i++) { diff --git a/src/gui/fileBrowser.h b/src/gui/fileBrowser.h index 5f6e7d4..1c49e92 100644 --- a/src/gui/fileBrowser.h +++ b/src/gui/fileBrowser.h @@ -45,6 +45,7 @@ namespace lunarium bool DoFrame(); bool IsOpen() const; + void SetPrompt(std::string prompt); void AddExtensionFilter(std::string ext); void SetSelectionMode(SelectionMode mode); void WarnOnExistingFileSelection(bool warn); @@ -59,15 +60,18 @@ namespace lunarium SelectionMode mSelectionMode; bool mWarnOnExisting; Result mResult; + std::string mPrompt; std::filesystem::path mCurrentDirectory; std::vector mItemsInDir; std::vector mExtensionsFilter; // Show only these extensions. If empty show all files. std::filesystem::path* mpSelectedItem; + std::filesystem::path mInputSelection; Image* mpFolderIcon; Image* mpNewFolderIcon; Image* mpUpFolderIcon; static const int mBufferSize = 256; + char mDirNameBuffer[mBufferSize]; char mInputBuffer[mBufferSize]; private: diff --git a/src/run_modes/editor/CMakeLists.txt b/src/run_modes/editor/CMakeLists.txt index 786c7b0..c9c21f5 100644 --- a/src/run_modes/editor/CMakeLists.txt +++ b/src/run_modes/editor/CMakeLists.txt @@ -1,4 +1,4 @@ -add_library(editor editor.cpp panels/iPanel.cpp panels/mainPanel.cpp panels/about.cpp) +add_library(editor editor.cpp project/project.cpp panels/iPanel.cpp panels/mainPanel.cpp panels/about.cpp) target_include_directories(editor PUBLIC "${PROJECT_BINARY_DIR}" @@ -8,4 +8,5 @@ target_include_directories(editor PUBLIC ../../../external/glad/include PUBLIC ../../../external/glfw/include PUBLIC ../../../external/box2d/include + PUBLIC ../../../external/pugixml/src ) \ No newline at end of file diff --git a/src/run_modes/editor/editor.cpp b/src/run_modes/editor/editor.cpp index c3528f0..835299d 100644 --- a/src/run_modes/editor/editor.cpp +++ b/src/run_modes/editor/editor.cpp @@ -90,7 +90,8 @@ namespace lunarium { mpFileBrowser = new FileBrowser; // mpFileBrowser->WarnOnExistingFileSelection(true); - mpFileBrowser->SetSelectionMode(FileBrowser::SelectionMode::DIRECTORIES_ONLY); + mpFileBrowser->SetSelectionMode(FileBrowser::SelectionMode::FILES_ONLY); + mpFileBrowser->SetPrompt("Pick a location and name for the project"); if (!mpFileBrowser->OpenInDirectory("")) { delete mpFileBrowser; @@ -106,7 +107,12 @@ namespace lunarium { Logger::Log(mLogCat, LogLevel::INFO, "Generating new project at %s", mpPath->string().c_str()); - // TODO: Generate new project at mpPath + // Generate new project at mpPath + OpRes result = mProject.GenerateProject(mpPath->filename().string(), mpPath->parent_path()); + if (Failed(result)) + { + Logger::Log(mLogCat, LogLevel::ERROR, "Could not create a new project: %s", result.Description); + } } else { diff --git a/src/run_modes/editor/editor.h b/src/run_modes/editor/editor.h index 80958b8..919f6eb 100644 --- a/src/run_modes/editor/editor.h +++ b/src/run_modes/editor/editor.h @@ -13,6 +13,8 @@ #include #include "panels/iPanel.h" +#include "project/project.h" + #include #include @@ -50,6 +52,8 @@ namespace lunarium MainPanel* mpMainPanel; std::map mPanels; + Project mProject; + FileBrowser* mpFileBrowser; const std::filesystem::path* mpPath; diff --git a/src/run_modes/editor/panels/mainPanel.cpp b/src/run_modes/editor/panels/mainPanel.cpp index 21b8b9a..25b145d 100644 --- a/src/run_modes/editor/panels/mainPanel.cpp +++ b/src/run_modes/editor/panels/mainPanel.cpp @@ -66,25 +66,7 @@ namespace lunarium ImGui::End(); return false; } - - // const ImGuiViewport* vp = ImGui::GetWindowViewport(); - // ImGuiID dockspace_id = ImGui::GetID("MyDockSpace"); - // ImGui::DockSpace(dockspace_id, ImVec2(vp->WorkSize.x, vp->WorkSize.y - ImGui::GetFrameHeight() * 2)); - - // float status_bar_pos = (vp->WorkSize.y - ImGui::GetFrameHeight() * 2); - // ImGui::SetCursorPosY(status_bar_pos); - // ImGui::Separator(); - // if (ImGui::BeginTable("status", 3)) - // { - // ImGui::TableNextColumn(); - // ImGui::Text("Lunarium"); - // ImGui::TableNextColumn(); - // ImGui::Text("Testing"); - // ImGui::TableNextColumn(); - // ImGui::Text("Status Bar"); - // ImGui::EndTable(); - // } ImGui::PopStyleVar(); ImGui::End(); diff --git a/src/run_modes/editor/project/project.cpp b/src/run_modes/editor/project/project.cpp new file mode 100644 index 0000000..7a3afc7 --- /dev/null +++ b/src/run_modes/editor/project/project.cpp @@ -0,0 +1,72 @@ +/****************************************************************************** +* File - project.cpp +* Author - Joey Pollack +* Date - 2021/11/09 (y/m/d) +* Mod Date - 2021/11/09 (y/m/d) +* Description - Manage data for a game project +******************************************************************************/ + +#include "project.h" + +#include + +namespace lunarium +{ + Project::Project() + : mIsLoaded(false) + { + + } + + OpRes Project::GenerateProject(std::string name, std::filesystem::path location) + { + if (mIsLoaded) + { + return OpRes::Fail("A project is already loaded. Unload the current project to generate a new one."); + } + + mName = name; + mLocation = location; + mLocation /= mName; + + if (std::filesystem::exists(mLocation)) + { + return OpRes::Fail("A project with that name (%s) already exists in this location: %s", mName.c_str(), mLocation.string().c_str()); + } + + std::filesystem::create_directory(mLocation); + + std::filesystem::path proj_doc = mLocation; + proj_doc /= std::filesystem::path(mName + ".lproj"); + + pugi::xml_document doc; + pugi::xml_node proj_node = doc.append_child("Lunarium_Project"); + proj_node.append_attribute("name").set_value(mName.c_str()); + //pugi::xml_node name_node = proj_node.append_child("Name"); + doc.save_file(proj_doc.string().c_str()); + + std::filesystem::create_directory(mLocation / std::filesystem::path("engine")); + std::filesystem::create_directory(mLocation / std::filesystem::path("contents")); + std::filesystem::create_directory(mLocation / std::filesystem::path("contents/assets")); + + mIsLoaded = true; + return OpRes::OK(); + } + + bool Project::IsLoaded() const + { + return mIsLoaded; + } + + OpRes Project::LoadProject(std::filesystem::path location) + { + return OpRes::Fail("Project::LoadProject not implemented yet"); + } + + void Project::UnloadCurrentProject() + { + mName = ""; + mLocation = ""; + mIsLoaded = false; + } +} \ No newline at end of file diff --git a/src/run_modes/editor/project/project.h b/src/run_modes/editor/project/project.h index e69de29..faf8a5a 100644 --- a/src/run_modes/editor/project/project.h +++ b/src/run_modes/editor/project/project.h @@ -0,0 +1,40 @@ +/****************************************************************************** +* File - project.h +* Author - Joey Pollack +* Date - 2021/11/09 (y/m/d) +* Mod Date - 2021/11/09 (y/m/d) +* Description - Manage data for a game project +******************************************************************************/ + +#ifndef PROJECT_H_ +#define PROJECT_H_ + +#include + +#include +#include +#include + +namespace lunarium +{ + class Scene; + class Project + { + public: + Project(); + OpRes GenerateProject(std::string name, std::filesystem::path location); + OpRes LoadProject(std::filesystem::path location); + void UnloadCurrentProject(); + + bool IsLoaded() const; + + private: + bool mIsLoaded; + std::string mName; + std::filesystem::path mLocation; + + std::vector mScenes; + }; +} + +#endif // PROJECT_H_ \ No newline at end of file