Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 40 additions & 17 deletions soh/soh/Enhancements/Presets/Presets.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -252,18 +252,25 @@ void SavePreset(std::string& presetName) {
}
presets[presetName].presetValues["presetName"] = presetName;
presets[presetName].presetValues["fileType"] = FILE_TYPE_PRESET;
std::ofstream file(
fmt::format("{}/{}.json", Ship::Context::GetInstance()->LocateFileAcrossAppDirs("presets"), presetName));
std::ofstream file(FormatPresetPath(presetName));
file << presets[presetName].presetValues.dump(4);
file.close();
LoadPresets();
}

static std::string newPresetName;
void DeletePreset(std::string& presetName) {
std::string presetPath = FormatPresetPath(presetName);
if (fs::exists(presetPath)) {
fs::remove(presetPath);
}
presets.erase(presetName);
}

static std::string newPresetName, oldPresetName;
static bool saveSection[PRESET_SECTION_MAX];

void DrawNewPresetPopup() {
bool nameExists = presets.contains(newPresetName);
void DrawEditPresetPopup() {
bool nameExists = presets.contains(newPresetName) && newPresetName != oldPresetName;
UIWidgets::InputString("Preset Name", &newPresetName,
UIWidgets::InputOptions()
.Color(THEME_COLOR)
Expand All @@ -272,7 +279,7 @@ void DrawNewPresetPopup() {
.LabelPosition(UIWidgets::LabelPositions::Near)
.ErrorText("Preset name already exists")
.HasError(nameExists));
nameExists = presets.contains(newPresetName);
nameExists = presets.contains(newPresetName) && newPresetName != oldPresetName;
bool noneSelected = true;
for (int i = PRESET_SECTION_SETTINGS; i < PRESET_SECTION_MAX; i++) {
if (saveSection[i]) {
Expand Down Expand Up @@ -350,10 +357,15 @@ void DrawNewPresetPopup() {
presets[newPresetName].fileName = newPresetName;
std::fill_n(presets[newPresetName].apply, PRESET_SECTION_MAX, true);
SavePreset(newPresetName);
if (newPresetName != oldPresetName) {
DeletePreset(oldPresetName);
}
newPresetName = "";
oldPresetName = "";
ImGui::CloseCurrentPopup();
}
if (UIWidgets::Button("Cancel", UIWidgets::ButtonOptions().Padding({ 6.0f, 6.0f }).Color(THEME_COLOR))) {
oldPresetName = "";
ImGui::CloseCurrentPopup();
}
ImGui::EndPopup();
Expand All @@ -366,25 +378,32 @@ void PresetsCustomWidget(WidgetInfo& info) {
.disabledTooltip = "Disabled because of race lockout" } })
.Size(UIWidgets::Sizes::Inline)
.Color(THEME_COLOR))) {
ImGui::OpenPopup("newPreset");
oldPresetName = "";
newPresetName = "";
std::fill_n(saveSection, PRESET_SECTION_MAX, true);
ImGui::OpenPopup("editPreset");
} else if (oldPresetName != "") {
ImGui::OpenPopup("editPreset");
}
if (ImGui::BeginPopup("newPreset", ImGuiWindowFlags_AlwaysAutoResize | ImGuiWindowFlags_NoResize |
ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoScrollbar |
ImGuiWindowFlags_NoSavedSettings | ImGuiWindowFlags_NoTitleBar)) {
DrawNewPresetPopup();
if (ImGui::BeginPopup("editPreset", ImGuiWindowFlags_AlwaysAutoResize | ImGuiWindowFlags_NoResize |
ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoScrollbar |
ImGuiWindowFlags_NoSavedSettings | ImGuiWindowFlags_NoTitleBar)) {
DrawEditPresetPopup();
}
ImGui::SameLine();
UIWidgets::CVarCheckbox("Hide built-in presets", CVAR_GENERAL("HideBuiltInPresets"),
UIWidgets::CheckboxOptions().Color(THEME_COLOR));
bool hideBuiltIn = CVarGetInteger(CVAR_GENERAL("HideBuiltInPresets"), 0);
UIWidgets::PushStyleTabs(THEME_COLOR);
if (ImGui::BeginTable("PresetWidgetTable", PRESET_SECTION_MAX + 3)) {
if (ImGui::BeginTable("PresetWidgetTable", PRESET_SECTION_MAX + 4)) {
ImGui::TableSetupColumn("Name", ImGuiTableColumnFlags_WidthFixed, 250);
for (int i = PRESET_SECTION_SETTINGS; i < PRESET_SECTION_MAX; i++) {
ImGui::TableSetupColumn(blockInfo[i].names[0].c_str());
}
ImGui::TableSetupColumn("Apply", ImGuiTableColumnFlags_WidthFixed,
ImGui::CalcTextSize("Apply").x + ImGui::GetStyle().FramePadding.x * 2);
ImGui::TableSetupColumn("Edit", ImGuiTableColumnFlags_WidthFixed,
ImGui::CalcTextSize("Edit").x + ImGui::GetStyle().FramePadding.x * 2);
ImGui::TableSetupColumn("Delete", ImGuiTableColumnFlags_WidthFixed,
ImGui::CalcTextSize("Delete").x + ImGui::GetStyle().FramePadding.x * 2);
BlankButton();
Expand Down Expand Up @@ -433,13 +452,17 @@ void PresetsCustomWidget(WidgetInfo& info) {
ImGui::TableNextColumn();
UIWidgets::PushStyleButton(THEME_COLOR);
if (!info.isBuiltIn) {
if (UIWidgets::Button(("Edit##" + name).c_str(), UIWidgets::ButtonOptions().Padding({ 6.0f, 6.0f }))) {
std::copy(info.apply, info.apply + PRESET_SECTION_MAX, saveSection);
newPresetName = name;
oldPresetName = name;
}
UIWidgets::PopStyleButton();
ImGui::TableNextColumn();
UIWidgets::PushStyleButton(THEME_COLOR);
if (UIWidgets::Button(("Delete##" + name).c_str(),
UIWidgets::ButtonOptions().Padding({ 6.0f, 6.0f }))) {
auto path = FormatPresetPath(info.fileName);
if (fs::exists(path)) {
fs::remove(path);
}
presets.erase(name);
DeletePreset(info.fileName);
UIWidgets::PopStyleButton();
break;
}
Expand Down
Loading