Skip to content

Commit

Permalink
Merge pull request #88 from OutpostUniverse/AddGuiObject
Browse files Browse the repository at this point in the history
Add gui object
  • Loading branch information
ldicker83 authored Jun 28, 2023
2 parents d16edc7 + 0e3f2fc commit 8043243
Show file tree
Hide file tree
Showing 5 changed files with 185 additions and 115 deletions.
125 changes: 125 additions & 0 deletions OP2-Landlord/Gui.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
#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(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();

ImGuiIO& io = ImGui::GetIO();
io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard;
io.IniFilename = nullptr;
ImGui::LoadIniSettingsFromDisk(mIniSavePath.c_str());

std::ignore = io.Fonts->AddFontFromFileTTF("data/fonts/opensans.ttf", 24);

ImGui::StyleColorsDark();

ImGui_ImplSDL2_InitForSDLRenderer(mGraphics.window(), mGraphics.renderer());
ImGui_ImplSDLRenderer2_Init(mGraphics.renderer());
}


Gui::~Gui()
{
ImGui::SaveIniSettingsToDisk(mIniSavePath.c_str());
}


void Gui::newFrame()
{
ImGui_ImplSDLRenderer2_NewFrame();
ImGui_ImplSDL2_NewFrame();
ImGui::NewFrame();
}


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;
}
36 changes: 36 additions & 0 deletions OP2-Landlord/Gui.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#pragma once

#include <string>

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


struct SDL_Window;
struct SDL_Renderer;


class Gui
{
public:
Gui() = delete;
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;
};
2 changes: 2 additions & 0 deletions OP2-Landlord/OP2-Landlord.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@
<ClCompile Include="Events.cpp" />
<ClCompile Include="FileIo.cpp" />
<ClCompile Include="Graphics.cpp" />
<ClCompile Include="Gui.cpp" />
<ClCompile Include="main.cpp" />
<ClCompile Include="StringTable.cpp" />
<ClCompile Include="Utility.cpp" />
Expand All @@ -157,6 +158,7 @@
<ClInclude Include="Events.h" />
<ClInclude Include="FileIo.h" />
<ClInclude Include="Graphics.h" />
<ClInclude Include="Gui.h" />
<ClInclude Include="StringTable.h" />
<ClInclude Include="Utility.h" />
</ItemGroup>
Expand Down
6 changes: 6 additions & 0 deletions OP2-Landlord/OP2-Landlord.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@
<ClCompile Include="StringTable.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="Gui.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="Graphics.h">
Expand All @@ -56,5 +59,8 @@
<ClInclude Include="StringTable.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="Gui.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
</Project>
131 changes: 16 additions & 115 deletions OP2-Landlord/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,104 +17,19 @@
#include "Events.h"
#include "FileIo.h"
#include "Graphics.h"
#include "Gui.h"
#include "StringTable.h"
#include "Utility.h"


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 guiEndFrame()
{
ImGui::Render();
ImGui_ImplSDLRenderer2_RenderDrawData(ImGui::GetDrawData());
}


void guiNewFrame()
{
ImGui_ImplSDLRenderer2_NewFrame();
ImGui_ImplSDL2_NewFrame();
ImGui::NewFrame();
}


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()
void mainLoop(Graphics& graphics, Gui& gui)
{
std::string pathToOutpost2{};

Expand All @@ -124,62 +39,48 @@ void mainLoop()

graphics.clear();

guiNewFrame();
gui.newFrame();

if(InitialSetupRequired)
{
doInitialSetup();
InitialSetupRequired = gui.initialSetup();
}
else
{

}

guiEndFrame();
gui.endFrame();

graphics.present();
}
}


void initGui()
{
IMGUI_CHECKVERSION();
ImGui::CreateContext();

ImGuiIO& io = ImGui::GetIO();
io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard;
io.IniFilename = nullptr;
ImGui::LoadIniSettingsFromDisk(Config["UserSavePath"].c_str());

std::ignore = io.Fonts->AddFontFromFileTTF("data/fonts/opensans.ttf", 24);

ImGui::StyleColorsDark();

ImGui_ImplSDL2_InitForSDLRenderer(graphics.window(), graphics.renderer());
ImGui_ImplSDLRenderer2_Init(graphics.renderer());
}


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);
initGui();

checkConfig();
EditorConfig config(getUserPrefPath("OP2-Landlord", "OutpostUniverse"));

Gui gui(MessageStrings, config, graphics, config["UserSavePath"] + "gui.ini");

checkConfig(config);

mainLoop();
mainLoop(graphics, gui);

ImGui::SaveIniSettingsToDisk(std::string{ Config["UserSavePath"] + "gui.ini" }.c_str());
SDL_Quit();
}
catch (std::exception e)
Expand Down

0 comments on commit 8043243

Please sign in to comment.