From 4383de7ad535c76d9d48584856c417a28484d206 Mon Sep 17 00:00:00 2001 From: Ian Bastos Date: Wed, 24 Jul 2019 05:04:41 +0100 Subject: [PATCH] Implement file drop handler (#666) --- src/PowderToySDL.cpp | 4 ++++ src/client/Client.cpp | 39 ++++++++++++++++++++---------------- src/client/Client.h | 1 + src/gui/game/GameView.cpp | 24 ++++++++++++++++++++++ src/gui/game/GameView.h | 1 + src/gui/interface/Engine.cpp | 6 ++++++ src/gui/interface/Engine.h | 1 + src/gui/interface/Window.cpp | 5 +++++ src/gui/interface/Window.h | 2 ++ 9 files changed, 66 insertions(+), 17 deletions(-) diff --git a/src/PowderToySDL.cpp b/src/PowderToySDL.cpp index 9b34c5702..a6b3e7eb4 100644 --- a/src/PowderToySDL.cpp +++ b/src/PowderToySDL.cpp @@ -400,6 +400,10 @@ void EventProcess(SDL_Event event) hasMouseMoved = true; break; + case SDL_DROPFILE: + engine->onFileDrop(event.drop.file); + SDL_free(event.drop.file); + break; case SDL_MOUSEBUTTONDOWN: // if mouse hasn't moved yet, sdl will send 0,0. We don't want that if (hasMouseMoved) diff --git a/src/client/Client.cpp b/src/client/Client.cpp index 4163ef3a4..ef36249a5 100644 --- a/src/client/Client.cpp +++ b/src/client/Client.cpp @@ -1025,23 +1025,10 @@ void Client::MoveStampToFront(ByteString stampID) SaveFile * Client::GetStamp(ByteString stampID) { ByteString stampFile = ByteString(STAMPS_DIR PATH_SEP + stampID + ".stm"); - SaveFile * file = new SaveFile(stampID); - if (!FileExists(stampFile)) - stampFile = stampID; - if (FileExists(stampFile)) - { - try - { - GameSave * tempSave = new GameSave(ReadFile(stampFile)); - file->SetGameSave(tempSave); - } - catch (ParseException & e) - { - std::cerr << "Client: Invalid stamp file, " << stampID << " " << e.what() << std::endl; - file->SetLoadingError(ByteString(e.what()).FromUtf8()); - } - } - return file; + SaveFile *saveFile = LoadSaveFile(stampFile); + if (!saveFile) + saveFile = LoadSaveFile(stampID); + return saveFile; } void Client::DeleteStamp(ByteString stampID) @@ -1482,6 +1469,24 @@ SaveInfo * Client::GetSave(int saveID, int saveDate) return NULL; } +SaveFile * Client::LoadSaveFile(ByteString filename) +{ + if (!FileExists(filename)) + return nullptr; + SaveFile * file = new SaveFile(filename); + try + { + GameSave * tempSave = new GameSave(ReadFile(filename)); + file->SetGameSave(tempSave); + } + catch (ParseException & e) + { + std::cerr << "Client: Invalid save file '" << filename << "': " << e.what() << std::endl; + file->SetLoadingError(ByteString(e.what()).FromUtf8()); + } + return file; +} + std::vector > * Client::GetTags(int start, int count, String query, int & resultCount) { lastError = ""; diff --git a/src/client/Client.h b/src/client/Client.h index a958d3b2c..bce8f6670 100644 --- a/src/client/Client.h +++ b/src/client/Client.h @@ -147,6 +147,7 @@ public: std::vector > * GetTags(int start, int count, String query, int & resultCount); SaveInfo * GetSave(int saveID, int saveDate); + SaveFile * LoadSaveFile(ByteString filename); RequestStatus DeleteSave(int saveID); RequestStatus ReportSave(int saveID, String message); diff --git a/src/gui/game/GameView.cpp b/src/gui/game/GameView.cpp index 547823559..a7ad30933 100644 --- a/src/gui/game/GameView.cpp +++ b/src/gui/game/GameView.cpp @@ -25,6 +25,7 @@ #include "gui/Style.h" #include "gui/dialogues/ConfirmPrompt.h" +#include "gui/dialogues/ErrorMessage.h" #include "gui/dialogues/InformationMessage.h" #include "gui/interface/Button.h" #include "gui/interface/Colour.h" @@ -1705,6 +1706,29 @@ void GameView::OnBlur() c->Blur(); } +void GameView::OnFileDrop(ByteString filename) +{ + if (!(filename.EndsWith(".cps") || filename.EndsWith(".stm"))) + { + new ErrorMessage("Error loading save", "Dropped file is not a TPT save file (.cps or .stm format)"); + return; + } + + SaveFile *saveFile = Client::Ref().LoadSaveFile(filename); + if (!saveFile) + return; + if (saveFile->GetError().length()) + { + new ErrorMessage("Error loading save", "Dropped save file could not be loaded: " + saveFile->GetError()); + return; + } + c->LoadSaveFile(saveFile); + delete saveFile; + + // hide the info text if it's not already hidden + introText = 0; +} + void GameView::OnTick(float dt) { if (selectMode == PlaceSave && !placeSaveThumb) diff --git a/src/gui/game/GameView.h b/src/gui/game/GameView.h index 30713c53b..1cf80bc33 100644 --- a/src/gui/game/GameView.h +++ b/src/gui/game/GameView.h @@ -196,6 +196,7 @@ public: void OnTick(float dt) override; void OnDraw() override; void OnBlur() override; + void OnFileDrop(ByteString filename) override; //Top-level handlers, for Lua interface void DoExit() override; diff --git a/src/gui/interface/Engine.cpp b/src/gui/interface/Engine.cpp index e147ed986..f380a1946 100644 --- a/src/gui/interface/Engine.cpp +++ b/src/gui/interface/Engine.cpp @@ -313,3 +313,9 @@ void Engine::onClose() if (state_) state_->DoExit(); } + +void Engine::onFileDrop(ByteString filename) +{ + if (state_) + state_->DoFileDrop(filename); +} diff --git a/src/gui/interface/Engine.h b/src/gui/interface/Engine.h index 6d79a1d79..9ddf9e2b6 100644 --- a/src/gui/interface/Engine.h +++ b/src/gui/interface/Engine.h @@ -34,6 +34,7 @@ namespace ui void onTextInput(String text); void onResize(int newWidth, int newHeight); void onClose(); + void onFileDrop(ByteString filename); void Begin(int width, int height); inline bool Running() { return running_; } diff --git a/src/gui/interface/Window.cpp b/src/gui/interface/Window.cpp index bfab4ebfe..fa47375b9 100644 --- a/src/gui/interface/Window.cpp +++ b/src/gui/interface/Window.cpp @@ -162,6 +162,11 @@ void Window::DoFocus() OnFocus(); } +void Window::DoFileDrop(ByteString filename) +{ + OnFileDrop(filename); +} + void Window::DoDraw() { OnDraw(); diff --git a/src/gui/interface/Window.h b/src/gui/interface/Window.h index 6db939333..25949582e 100644 --- a/src/gui/interface/Window.h +++ b/src/gui/interface/Window.h @@ -59,6 +59,7 @@ namespace ui virtual void DoDraw(); virtual void DoFocus(); virtual void DoBlur(); + virtual void DoFileDrop(ByteString filename); virtual void DoMouseMove(int x, int y, int dx, int dy); virtual void DoMouseDown(int x, int y, unsigned button); @@ -94,6 +95,7 @@ namespace ui virtual void OnDraw() {} virtual void OnFocus() {} virtual void OnBlur() {} + virtual void OnFileDrop(ByteString filename) {} virtual void OnTryExit(ExitMethod); virtual void OnTryOkay(OkayMethod);