Skip to content

Commit

Permalink
feat: add an export button for the current config
Browse files Browse the repository at this point in the history
  • Loading branch information
Lazrius committed Mar 16, 2024
1 parent 7c6bbfe commit ac98833
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 10 deletions.
2 changes: 1 addition & 1 deletion ChaosMod/Include/Components/ConfigManager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,6 @@ class ConfigManager : public Component
allowAutoSavesDuringCombat, blockTeleportsDuringMissions, toggledEffects, enablePatchNotes, countDownWhileOnBases,
timeBetweenPatchesInMinutes, changesPerPatchMin, changesPerMinorMin, changesPerMajorMin, progressBarType);

void Save();
void Save(std::string_view path = "");
static std::shared_ptr<ConfigManager> Load();
};
37 changes: 35 additions & 2 deletions ChaosMod/Include/ImGui/Menus/Configurator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ class Configurator final

bool confirmImport = false;

bool showExportModal = false;
std::string exportPath;

void RenderChaosTab() const
{
ImGui::TextWrapped("PLACEHOLDER DESCRIPTION THAT IS SUPER DESCRIPTIVE ABOUT THINGS THAT NEED DESCRIBING FOR THIS DESCRIPTIVE TAB OF DESCRIPTIONS.");
Expand Down Expand Up @@ -263,14 +266,28 @@ class Configurator final
}
else
{
ImGui::BeginDisabled(fileBrowser.IsOpened());
if (ImGui::Button("Save Changes"))
{
config->Save();
}

ImGui::BeginDisabled(fileBrowser.IsOpened());
ImGui::SameLine();
if (ImGui::Button("Load Preset"))
if (ImGui::Button("Export Preset"))
{
exportPath = std::filesystem::current_path().append("presets\\exported.json").string();
config->Save(exportPath);
ImGui::OpenPopup("Exported Config Modal");
showExportModal = true;
}

if (ImGui::IsItemHovered())
{
ImGui::SetTooltip("TODO: Tooltip explaining how exporting presets works");
}

ImGui::SameLine();
if (ImGui::Button("Import Preset"))
{
static const std::vector<std::string> filters = { ".json", ".txt" };
fileBrowser.SetTypeFilters(filters);
Expand All @@ -279,6 +296,7 @@ class Configurator final
fileBrowser.SetPwd(exists(path) ? path : std::filesystem::current_path());
fileBrowser.Open();
}

ImGui::EndDisabled();

if (ImGui::IsItemHovered())
Expand Down Expand Up @@ -330,6 +348,21 @@ class Configurator final
ImGui::EndTabBar();
}

ImVec2 center = ImGui::GetMainViewport()->GetCenter();
ImGui::SetNextWindowPos(center, ImGuiCond_Appearing, ImVec2(0.5f, 0.5f));
if (ImGui::BeginPopupModal("Exported Config Modal", nullptr, ImGuiWindowFlags_AlwaysAutoResize))
{
ImGui::Text("Exported Config To:");
ImGui::Text(exportPath.c_str());

if (ImGui::Button("OK", ImVec2(120, 0)))
{
ImGui::CloseCurrentPopup();
}

ImGui::EndPopup();
}

ImGui::End();
}

Expand Down
20 changes: 13 additions & 7 deletions ChaosMod/Source/Components/ConfigManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,28 @@

#include <magic_enum.hpp>

void ConfigManager::Save()
void ConfigManager::Save(std::string_view path)
{
std::string path;
path.resize(MAX_PATH, '\0');
GetUserDataPath(path.data());
path.erase(std::ranges::find(path, '\0'), path.end());
if (path.empty())
{
static std::string userPath;
userPath.resize(MAX_PATH, '\0');
GetUserDataPath(userPath.data());
userPath.erase(std::ranges::find(userPath, '\0'), userPath.end());
userPath += "\\chaos.json";
path = userPath;
}

std::ofstream file(path + "\\chaos.json");
std::string fileLoc = std::format("{}", path);
std::ofstream file(fileLoc);

const nlohmann::json json = *this;
const std::string jsonStr = json.dump(4);

file << jsonStr << std::endl;
file.close();

Log(std::format("Saving json config: {}", path + "\\chaos.json"));
Log(std::format("Saving json config: {}", fileLoc));
}

std::shared_ptr<ConfigManager> ConfigManager::Load()
Expand Down

0 comments on commit ac98833

Please sign in to comment.