From 7623f45e6e73a24f1c522749d2cfaeef0db90b64 Mon Sep 17 00:00:00 2001 From: git4rker Date: Tue, 21 Nov 2023 18:25:34 +0300 Subject: [PATCH] Implement stamp renaming --- src/client/Client.cpp | 22 +++++++++++++++++ src/client/Client.h | 1 + .../localbrowser/LocalBrowserController.cpp | 19 +++++++++++++++ src/gui/localbrowser/LocalBrowserController.h | 1 + src/gui/localbrowser/LocalBrowserView.cpp | 24 ++++++++++++------- src/gui/localbrowser/LocalBrowserView.h | 1 + 6 files changed, 60 insertions(+), 8 deletions(-) diff --git a/src/client/Client.cpp b/src/client/Client.cpp index 419c87e0a..d44e5230b 100644 --- a/src/client/Client.cpp +++ b/src/client/Client.cpp @@ -11,6 +11,7 @@ #include "common/platform/Platform.h" #include "common/String.h" #include "graphics/Graphics.h" +#include "gui/dialogues/ErrorMessage.h" #include "prefs/Prefs.h" #include "lua/CommandInterface.h" #include "Config.h" @@ -287,6 +288,27 @@ void Client::DeleteStamp(ByteString stampID) } } +void Client::RenameStamp(ByteString stampID, ByteString newName) +{ + auto oldPath = ByteString::Build(STAMPS_DIR, PATH_SEP_CHAR, stampID, ".stm"); + auto newPath = ByteString::Build(STAMPS_DIR, PATH_SEP_CHAR, newName, ".stm"); + + if (Platform::FileExists(newPath)) + { + new ErrorMessage("Error renaming stamp", "A stamp with this name already exists."); + return; + } + + if (!Platform::RenameFile(oldPath, newPath, false)) + { + new ErrorMessage("Error renaming stamp", "Could not rename the stamp."); + return; + } + + std::replace(stampIDs.begin(), stampIDs.end(), stampID, newName); + WriteStamps(); +} + ByteString Client::AddStamp(std::unique_ptr saveData) { auto now = (uint64_t)time(NULL); diff --git a/src/client/Client.h b/src/client/Client.h index b81a5b14f..365dfa51e 100644 --- a/src/client/Client.h +++ b/src/client/Client.h @@ -91,6 +91,7 @@ public: std::unique_ptr GetStamp(ByteString stampID); void DeleteStamp(ByteString stampID); + void RenameStamp(ByteString stampID, ByteString newName); ByteString AddStamp(std::unique_ptr saveData); void RescanStamps(); const std::vector &GetStamps() const; diff --git a/src/gui/localbrowser/LocalBrowserController.cpp b/src/gui/localbrowser/LocalBrowserController.cpp index 6fd87ce47..a2684a656 100644 --- a/src/gui/localbrowser/LocalBrowserController.cpp +++ b/src/gui/localbrowser/LocalBrowserController.cpp @@ -7,6 +7,8 @@ #include "client/GameSave.h" #include "client/SaveFile.h" #include "gui/dialogues/ConfirmPrompt.h" +#include "gui/dialogues/TextPrompt.h" +#include "gui/dialogues/ErrorMessage.h" #include "tasks/TaskWindow.h" #include "tasks/Task.h" @@ -75,6 +77,23 @@ void LocalBrowserController::removeSelectedC() new TaskWindow("Removing stamps", new RemoveSavesTask(this, selected)); } +void LocalBrowserController::RenameSelected() +{ + ByteString save = browserModel->GetSelected()[0]; + + new TextPrompt("Rename stamp", "Enter a new name for the stamp:", "", "[new name]", false, { [this, save](const String &newName) { + if (newName.length() == 0) + { + new ErrorMessage("Error renaming stamp", "You have to specify the filename."); + return; + } + + Client::Ref().RenameStamp(save, newName.ToUtf8()); + + RefreshSavesList(); + } }); +} + void LocalBrowserController::RescanStamps() { browserModel->RescanStamps(); diff --git a/src/gui/localbrowser/LocalBrowserController.h b/src/gui/localbrowser/LocalBrowserController.h index f6de05a6a..5682685ef 100644 --- a/src/gui/localbrowser/LocalBrowserController.h +++ b/src/gui/localbrowser/LocalBrowserController.h @@ -17,6 +17,7 @@ public: std::unique_ptr TakeSave(); void RemoveSelected(); void removeSelectedC(); + void RenameSelected(); void ClearSelection(); void Selected(ByteString stampID, bool selected); void RescanStamps(); diff --git a/src/gui/localbrowser/LocalBrowserView.cpp b/src/gui/localbrowser/LocalBrowserView.cpp index a08b96ccd..69234d03a 100644 --- a/src/gui/localbrowser/LocalBrowserView.cpp +++ b/src/gui/localbrowser/LocalBrowserView.cpp @@ -48,7 +48,13 @@ LocalBrowserView::LocalBrowserView(): removeSelected = new ui::Button(ui::Point(((WINDOWW-100)/2), WINDOWH-18), ui::Point(100, 16), "Delete"); removeSelected->Visible = false; removeSelected->SetActionCallback({ [this] { c->RemoveSelected(); } }); + + renameSelected = new ui::Button(ui::Point(((WINDOWW - 100) / 2 + 52), WINDOWH - 18), ui::Point(100, 16), "Rename"); + renameSelected->Visible = false; + renameSelected->SetActionCallback({ [this] { c->RenameSelected(); } }); + AddComponent(removeSelected); + AddComponent(renameSelected); } void LocalBrowserView::textChanged() @@ -179,16 +185,18 @@ void LocalBrowserView::NotifySelectedChanged(LocalBrowserModel * sender) } } - if (selected.size()) + removeSelected->Visible = selected.size() > 0; + renameSelected->Visible = selected.size() == 1; + removeSelected->Position.X = (WINDOWW - 100) / 2; + if (renameSelected->Visible) { - removeSelected->Visible = true; - pageLabel->Visible = pageCountLabel->Visible = pageTextbox->Visible = false; - } - else if (removeSelected->Visible) - { - removeSelected->Visible = false; - pageLabel->Visible = pageCountLabel->Visible = pageTextbox->Visible = true; + removeSelected->Position.X -= 52; } + + auto showPagination = !removeSelected->Visible; + pageLabel->Visible = showPagination; + pageCountLabel->Visible = showPagination; + pageTextbox->Visible = showPagination; } void LocalBrowserView::OnMouseWheel(int x, int y, int d) diff --git a/src/gui/localbrowser/LocalBrowserView.h b/src/gui/localbrowser/LocalBrowserView.h index 96c05ce58..efa7f6850 100644 --- a/src/gui/localbrowser/LocalBrowserView.h +++ b/src/gui/localbrowser/LocalBrowserView.h @@ -22,6 +22,7 @@ class LocalBrowserView: public ui::Window { ui::Label * pageCountLabel; ui::Textbox * pageTextbox; ui::Button * removeSelected; + ui::Button *renameSelected; void textChanged(); bool changed;