mirror of
https://github.com/HarbourMasters/Shipwright
synced 2026-08-21 06:48:39 -04:00
Add Edit buttons for custom presets (#5994)
This commit is contained in:
@@ -282,10 +282,7 @@ void SavePreset(std::string& presetName) {
|
||||
presets[presetName].presetValues["presetName"] = presetName;
|
||||
presets[presetName].presetValues["fileType"] = FILE_TYPE_PRESET;
|
||||
|
||||
std::string safeFilename = SanitizeFilename(presetName);
|
||||
std::ofstream file(
|
||||
fmt::format("{}/{}.json", Ship::Context::GetRawInstance()->LocateFileAcrossAppDirs("presets"), safeFilename));
|
||||
|
||||
std::ofstream file(FormatPresetPath(presetName));
|
||||
if (!file.is_open()) {
|
||||
spdlog::error("Failed to save preset '{}': Could not create file", presetName);
|
||||
return;
|
||||
@@ -296,11 +293,19 @@ void SavePreset(std::string& presetName) {
|
||||
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)
|
||||
@@ -309,7 +314,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]) {
|
||||
@@ -387,10 +392,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();
|
||||
@@ -403,25 +413,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, 400);
|
||||
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();
|
||||
@@ -470,13 +487,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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user