Implement stamp renaming

This commit is contained in:
git4rker 2023-11-21 18:25:34 +03:00 committed by Tamás Bálint Misius
parent d84e0a0c3e
commit 7623f45e6e
6 changed files with 60 additions and 8 deletions

View File

@ -11,6 +11,7 @@
#include "common/platform/Platform.h" #include "common/platform/Platform.h"
#include "common/String.h" #include "common/String.h"
#include "graphics/Graphics.h" #include "graphics/Graphics.h"
#include "gui/dialogues/ErrorMessage.h"
#include "prefs/Prefs.h" #include "prefs/Prefs.h"
#include "lua/CommandInterface.h" #include "lua/CommandInterface.h"
#include "Config.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<GameSave> saveData) ByteString Client::AddStamp(std::unique_ptr<GameSave> saveData)
{ {
auto now = (uint64_t)time(NULL); auto now = (uint64_t)time(NULL);

View File

@ -91,6 +91,7 @@ public:
std::unique_ptr<SaveFile> GetStamp(ByteString stampID); std::unique_ptr<SaveFile> GetStamp(ByteString stampID);
void DeleteStamp(ByteString stampID); void DeleteStamp(ByteString stampID);
void RenameStamp(ByteString stampID, ByteString newName);
ByteString AddStamp(std::unique_ptr<GameSave> saveData); ByteString AddStamp(std::unique_ptr<GameSave> saveData);
void RescanStamps(); void RescanStamps();
const std::vector<ByteString> &GetStamps() const; const std::vector<ByteString> &GetStamps() const;

View File

@ -7,6 +7,8 @@
#include "client/GameSave.h" #include "client/GameSave.h"
#include "client/SaveFile.h" #include "client/SaveFile.h"
#include "gui/dialogues/ConfirmPrompt.h" #include "gui/dialogues/ConfirmPrompt.h"
#include "gui/dialogues/TextPrompt.h"
#include "gui/dialogues/ErrorMessage.h"
#include "tasks/TaskWindow.h" #include "tasks/TaskWindow.h"
#include "tasks/Task.h" #include "tasks/Task.h"
@ -75,6 +77,23 @@ void LocalBrowserController::removeSelectedC()
new TaskWindow("Removing stamps", new RemoveSavesTask(this, selected)); 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() void LocalBrowserController::RescanStamps()
{ {
browserModel->RescanStamps(); browserModel->RescanStamps();

View File

@ -17,6 +17,7 @@ public:
std::unique_ptr<SaveFile> TakeSave(); std::unique_ptr<SaveFile> TakeSave();
void RemoveSelected(); void RemoveSelected();
void removeSelectedC(); void removeSelectedC();
void RenameSelected();
void ClearSelection(); void ClearSelection();
void Selected(ByteString stampID, bool selected); void Selected(ByteString stampID, bool selected);
void RescanStamps(); void RescanStamps();

View File

@ -48,7 +48,13 @@ LocalBrowserView::LocalBrowserView():
removeSelected = new ui::Button(ui::Point(((WINDOWW-100)/2), WINDOWH-18), ui::Point(100, 16), "Delete"); removeSelected = new ui::Button(ui::Point(((WINDOWW-100)/2), WINDOWH-18), ui::Point(100, 16), "Delete");
removeSelected->Visible = false; removeSelected->Visible = false;
removeSelected->SetActionCallback({ [this] { c->RemoveSelected(); } }); 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(removeSelected);
AddComponent(renameSelected);
} }
void LocalBrowserView::textChanged() 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; removeSelected->Position.X -= 52;
pageLabel->Visible = pageCountLabel->Visible = pageTextbox->Visible = false;
}
else if (removeSelected->Visible)
{
removeSelected->Visible = false;
pageLabel->Visible = pageCountLabel->Visible = pageTextbox->Visible = true;
} }
auto showPagination = !removeSelected->Visible;
pageLabel->Visible = showPagination;
pageCountLabel->Visible = showPagination;
pageTextbox->Visible = showPagination;
} }
void LocalBrowserView::OnMouseWheel(int x, int y, int d) void LocalBrowserView::OnMouseWheel(int x, int y, int d)

View File

@ -22,6 +22,7 @@ class LocalBrowserView: public ui::Window {
ui::Label * pageCountLabel; ui::Label * pageCountLabel;
ui::Textbox * pageTextbox; ui::Textbox * pageTextbox;
ui::Button * removeSelected; ui::Button * removeSelected;
ui::Button *renameSelected;
void textChanged(); void textChanged();
bool changed; bool changed;