Implement file drop handler (#666)

This commit is contained in:
Ian Bastos 2019-07-24 05:04:41 +01:00 committed by jacob1
parent 9faf95a858
commit 4383de7ad5
9 changed files with 66 additions and 17 deletions

View File

@ -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)

View File

@ -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<std::pair<ByteString, int> > * Client::GetTags(int start, int count, String query, int & resultCount)
{
lastError = "";

View File

@ -147,6 +147,7 @@ public:
std::vector<std::pair<ByteString, int> > * 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);

View File

@ -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)

View File

@ -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;

View File

@ -313,3 +313,9 @@ void Engine::onClose()
if (state_)
state_->DoExit();
}
void Engine::onFileDrop(ByteString filename)
{
if (state_)
state_->DoFileDrop(filename);
}

View File

@ -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_; }

View File

@ -162,6 +162,11 @@ void Window::DoFocus()
OnFocus();
}
void Window::DoFileDrop(ByteString filename)
{
OnFileDrop(filename);
}
void Window::DoDraw()
{
OnDraw();

View File

@ -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);