Skip to content

Commit

Permalink
Move remaining GUI code to Gui object
Browse files Browse the repository at this point in the history
  • Loading branch information
ldicker83 committed Jun 28, 2023
1 parent 40841d0 commit 0e3f2fc
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 83 deletions.
85 changes: 81 additions & 4 deletions OP2-Landlord/Gui.cpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,23 @@
#include "Gui.h"

#include <filesystem>

#include <SDL2/SDL.h>

#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();
Expand All @@ -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());
}


Expand All @@ -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<float>(mGraphics.size().x / 2 - 300),
static_cast<float>(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;
}
15 changes: 14 additions & 1 deletion OP2-Landlord/Gui.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

#include <string>

#include "EditorConfig.h"
#include "FileIo.h"
#include "Graphics.h"
#include "StringTable.h"


struct SDL_Window;
struct SDL_Renderer;
Expand All @@ -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;
};
90 changes: 12 additions & 78 deletions OP2-Landlord/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<float>(graphics.size().x / 2 - 300),
static_cast<float>(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{};

Expand All @@ -114,7 +43,7 @@ void mainLoop(Gui& gui)

if(InitialSetupRequired)
{
doInitialSetup();
InitialSetupRequired = gui.initialSetup();
}
else
{
Expand All @@ -128,24 +57,29 @@ void mainLoop(Gui& gui)
}


void checkConfig()
void checkConfig(EditorConfig& config)
{
InitialSetupRequired = !Config.contains("Op2FilePath");
InitialSetupRequired = !config.contains("Op2FilePath");
}


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();
}
Expand Down

0 comments on commit 0e3f2fc

Please sign in to comment.