From 0e3f2fc583a0c2f8433252c7804f543a30e41e63 Mon Sep 17 00:00:00 2001 From: Leeor Dicker Date: Tue, 27 Jun 2023 22:47:27 -0400 Subject: [PATCH] Move remaining GUI code to Gui object --- OP2-Landlord/Gui.cpp | 85 ++++++++++++++++++++++++++++++++++++++-- OP2-Landlord/Gui.h | 15 +++++++- OP2-Landlord/main.cpp | 90 ++++++------------------------------------- 3 files changed, 107 insertions(+), 83 deletions(-) diff --git a/OP2-Landlord/Gui.cpp b/OP2-Landlord/Gui.cpp index b4e32f0..386c373 100644 --- a/OP2-Landlord/Gui.cpp +++ b/OP2-Landlord/Gui.cpp @@ -1,14 +1,23 @@ #include "Gui.h" +#include + #include #include "imgui.h" #include "imgui_impl_sdl2.h" #include "imgui_impl_sdlrenderer2.h" +#include "FileIo.h" +#include "Utility.h" + -Gui::Gui(const std::string& settingsPath, SDL_Window* window, SDL_Renderer* renderer) : - mIniSavePath{ settingsPath } +Gui::Gui(StringTable& table, EditorConfig& config, Graphics& graphics, const std::string& settingsPath) : + mStringTable{ table }, + mIniSavePath{ settingsPath }, + mGraphics{ graphics }, + mConfig{ config }, + mFileIo{ *graphics.window() } { IMGUI_CHECKVERSION(); ImGui::CreateContext(); @@ -22,8 +31,8 @@ Gui::Gui(const std::string& settingsPath, SDL_Window* window, SDL_Renderer* rend ImGui::StyleColorsDark(); - ImGui_ImplSDL2_InitForSDLRenderer(window, renderer); - ImGui_ImplSDLRenderer2_Init(renderer); + ImGui_ImplSDL2_InitForSDLRenderer(mGraphics.window(), mGraphics.renderer()); + ImGui_ImplSDLRenderer2_Init(mGraphics.renderer()); } @@ -46,3 +55,71 @@ void Gui::endFrame() ImGui::Render(); ImGui_ImplSDLRenderer2_RenderDrawData(ImGui::GetDrawData()); } + + +bool Gui::initialSetup() +{ + static char op2Path[1000] = { '\0' }; + + ImGui::SetNextWindowSize({ 550, 170 }); + const ImVec2 position{ + static_cast(mGraphics.size().x / 2 - 300), + static_cast(mGraphics.size().y / 2 - 85) + }; + + ImGui::SetNextWindowPos(position); + + ImGuiIO& io = ImGui::GetIO(); + + ImGui::Begin("Initial Setup", nullptr, ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoCollapse); + + ImGui::Text("Location of Outpost 2"); + + ImGui::SetNextItemWidth(ImGui::GetContentRegionAvail().x * 0.90f); + ImGui::InputText("##op2_path", op2Path, 500, ImGuiInputTextFlags_AutoSelectAll); + ImGui::SameLine(); + if (ImGui::Button("...", { ImGui::GetContentRegionAvail().x, 0 })) + { + if (mFileIo.pickFolder()) + { + std::ignore = strcpy_s(op2Path, mFileIo.folderPath().c_str()); + } + } + + ImGui::Dummy({ 0, 20 }); + + bool retValue = true; + + if (ImGui::Button("Continue", { ImGui::GetContentRegionAvail().x, 0 })) + { + const auto exepath = std::string(op2Path) + mFileIo.pathSeparator() + "Outpost2.exe"; + const auto artpath = std::string(op2Path) + mFileIo.pathSeparator() + "art.vol"; + const std::string path(trimWhitespace(op2Path)); + + if (path.empty()) + { + SDL_ShowSimpleMessageBox( + SDL_MESSAGEBOX_ERROR, + mStringTable[StringTable::StringName::EmptyDir].c_str(), + mStringTable[StringTable::StringName::EmptyDirMsg].c_str(), + mGraphics.window()); + } + else if (!std::filesystem::exists(exepath) || !std::filesystem::exists(artpath)) + { + SDL_ShowSimpleMessageBox( + SDL_MESSAGEBOX_ERROR, + mStringTable[StringTable::StringName::MissingAssets].c_str(), + mStringTable[StringTable::StringName::MissingAssetsMsg].c_str(), + mGraphics.window()); + } + else + { + mConfig["Op2FilePath"] = op2Path; + retValue = false; + } + } + + ImGui::End(); + + return retValue; +} \ No newline at end of file diff --git a/OP2-Landlord/Gui.h b/OP2-Landlord/Gui.h index 1eb9c3d..f5abe82 100644 --- a/OP2-Landlord/Gui.h +++ b/OP2-Landlord/Gui.h @@ -2,6 +2,11 @@ #include +#include "EditorConfig.h" +#include "FileIo.h" +#include "Graphics.h" +#include "StringTable.h" + struct SDL_Window; struct SDL_Renderer; @@ -11,13 +16,21 @@ class Gui { public: Gui() = delete; - Gui(const std::string& settingsPath, SDL_Window* window, SDL_Renderer* renderer); + Gui(StringTable& table, EditorConfig& config, Graphics& graphics, const std::string& settingsPath); ~Gui(); void newFrame(); void endFrame(); + bool initialSetup(); + private: + const StringTable& mStringTable; const std::string mIniSavePath; + + Graphics& mGraphics; + EditorConfig& mConfig; + + FileIo mFileIo; }; diff --git a/OP2-Landlord/main.cpp b/OP2-Landlord/main.cpp index ed0bcfd..2c470b0 100644 --- a/OP2-Landlord/main.cpp +++ b/OP2-Landlord/main.cpp @@ -24,83 +24,12 @@ namespace { - Graphics graphics({ 800, 600 }); - FileIo fileIo(*graphics.window()); - constexpr auto ClearColor = ImColor{ 0.117f, 0.117f, 0.117f, 1.0f }; - - EditorConfig Config(getUserPrefPath("OP2-Landlord", "OutpostUniverse")); - StringTable MessageStrings; - bool InitialSetupRequired = false; }; -void doInitialSetup() -{ - static char op2Path[1000] = { '\0' }; - - ImGui::SetNextWindowSize({ 550, 170 }); - const ImVec2 position{ - static_cast(graphics.size().x / 2 - 300), - static_cast(graphics.size().y / 2 - 85) - }; - - ImGui::SetNextWindowPos(position); - - ImGuiIO& io = ImGui::GetIO(); - - ImGui::Begin("Initial Setup", nullptr, ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoCollapse); - - ImGui::Text("Location of Outpost 2"); - - ImGui::SetNextItemWidth(ImGui::GetContentRegionAvail().x * 0.90f); - ImGui::InputText("##op2_path", op2Path, 500, ImGuiInputTextFlags_AutoSelectAll); - ImGui::SameLine(); - if (ImGui::Button("...", { ImGui::GetContentRegionAvail().x, 0 })) - { - if (fileIo.pickFolder()) - { - std::ignore = strcpy_s(op2Path, fileIo.folderPath().c_str()); - } - } - - ImGui::Dummy({ 0, 20 }); - - if (ImGui::Button("Continue", { ImGui::GetContentRegionAvail().x, 0 })) - { - const auto exepath = std::string(op2Path) + fileIo.pathSeparator() + "Outpost2.exe"; - const auto artpath = std::string(op2Path) + fileIo.pathSeparator() + "art.vol"; - const std::string path(trimWhitespace(op2Path)); - - if (path.empty()) - { - SDL_ShowSimpleMessageBox( - SDL_MESSAGEBOX_ERROR, - MessageStrings[StringTable::StringName::EmptyDir].c_str(), - MessageStrings[StringTable::StringName::EmptyDirMsg].c_str(), - graphics.window()); - } - else if (!std::filesystem::exists(exepath) || !std::filesystem::exists(artpath)) - { - SDL_ShowSimpleMessageBox( - SDL_MESSAGEBOX_ERROR, - MessageStrings[StringTable::StringName::MissingAssets].c_str(), - MessageStrings[StringTable::StringName::MissingAssetsMsg].c_str(), - graphics.window()); - } - else - { - Config["Op2FilePath"] = op2Path; - InitialSetupRequired = false; - } - } - - ImGui::End(); -} - - -void mainLoop(Gui& gui) +void mainLoop(Graphics& graphics, Gui& gui) { std::string pathToOutpost2{}; @@ -114,7 +43,7 @@ void mainLoop(Gui& gui) if(InitialSetupRequired) { - doInitialSetup(); + InitialSetupRequired = gui.initialSetup(); } else { @@ -128,9 +57,9 @@ void mainLoop(Gui& gui) } -void checkConfig() +void checkConfig(EditorConfig& config) { - InitialSetupRequired = !Config.contains("Op2FilePath"); + InitialSetupRequired = !config.contains("Op2FilePath"); } @@ -138,14 +67,19 @@ int main(int argc, char* argv[]) { try { + StringTable MessageStrings; MessageStrings.load("data/en.json"); + + Graphics graphics({ 800, 600 }); graphics.drawColor(ClearColor); - Gui gui(Config["UserSavePath"] + "gui.ini", graphics.window(), graphics.renderer()); + EditorConfig config(getUserPrefPath("OP2-Landlord", "OutpostUniverse")); + + Gui gui(MessageStrings, config, graphics, config["UserSavePath"] + "gui.ini"); - checkConfig(); + checkConfig(config); - mainLoop(gui); + mainLoop(graphics, gui); SDL_Quit(); }