Save as current name complete, resolves #5

This commit is contained in:
Simon Robertshaw 2012-08-07 01:58:46 +01:00
parent 51657b8575
commit 46b2def193
5 changed files with 84 additions and 16 deletions

View File

@ -19,6 +19,7 @@
#include "filebrowser/FileBrowserActivity.h"
#include "save/LocalSaveActivity.h"
#include "save/ServerSaveActivity.h"
#include "interface/Keys.h"
using namespace std;
@ -433,26 +434,26 @@ bool GameController::KeyRelease(int key, Uint16 character, bool shift, bool ctrl
if(ret)
{
Simulation * sim = gameModel->GetSimulation();
if (key == SDLK_RIGHT || key == SDLK_LEFT)
if (key == KEY_RIGHT || key == KEY_LEFT)
{
sim->player.pcomm = sim->player.comm; //Saving last movement
sim->player.comm = (int)(sim->player.comm)&12; //Stop command
}
if (key == SDLK_UP)
if (key == KEY_UP)
{
sim->player.comm = (int)(sim->player.comm)&11;
}
if (key == SDLK_DOWN)
if (key == KEY_DOWN)
{
sim->player.comm = (int)(sim->player.comm)&7;
}
if (key == SDLK_d || key == SDLK_a)
if (key == KEY_d || key == KEY_a)
{
sim->player2.pcomm = sim->player2.comm; //Saving last movement
sim->player2.comm = (int)(sim->player2.comm)&12; //Stop command
}
if (key == SDLK_w)
if (key == KEY_w)
{
sim->player2.comm = (int)(sim->player2.comm)&11;
}
@ -852,11 +853,8 @@ void GameController::SaveAsCurrent()
//c->LoadSave(&save);
}
};
if(!gameModel->GetSave() || gameModel->GetUser().Username != gameModel->GetSave()->GetUserName())
{
OpenSaveWindow();
}
if(gameModel->GetUser().ID)
if(gameModel->GetSave() && gameModel->GetUser().ID && gameModel->GetUser().Username == gameModel->GetSave()->GetUserName())
{
Simulation * sim = gameModel->GetSimulation();
GameSave * gameSave = sim->Save();
@ -886,6 +884,10 @@ void GameController::SaveAsCurrent()
}
}
}
else if(gameModel->GetUser().ID)
{
OpenSaveWindow();
}
else
{
new ErrorMessage("Error", "You need to login to upload saves.");

View File

@ -54,7 +54,7 @@ public:
{
if(leftDown)
DoLeftAction();
if(rightDown)
else if(rightDown)
DoRightAction();
}
ui::Button::OnMouseUp(x, y, button);

View File

@ -8,6 +8,7 @@
#include "dialogues/ErrorMessage.h"
#include "dialogues/ConfirmPrompt.h"
#include "client/Client.h"
#include "tasks/Task.h"
#include "Style.h"
class ServerSaveActivity::CancelAction: public ui::ButtonAction
@ -32,11 +33,46 @@ public:
}
};
//Currently, reading is done on another thread, we can't render outside the main thread due to some bullshit with OpenGL
class SaveUploadTask: public Task
{
SaveInfo save;
virtual void before()
{
}
virtual void after()
{
}
virtual bool doWork()
{
notifyProgress(-1);
return Client::Ref().UploadSave(save) == RequestOkay;
}
public:
SaveInfo GetSave()
{
return save;
}
SaveUploadTask(SaveInfo save):
save(save)
{
}
};
ServerSaveActivity::ServerSaveActivity(SaveInfo save, ServerSaveActivity::SaveUploadedCallback * callback) :
WindowActivity(ui::Point(-1, -1), ui::Point(440, 200)),
thumbnail(NULL),
save(save),
callback(callback)
callback(callback),
saveUploadTask(NULL)
{
ui::Label * titleLabel = new ui::Label(ui::Point(4, 5), ui::Point((Size.X/2)-8, 16), "Save to server:");
titleLabel->SetTextColour(style::Colour::InformationTitle);
@ -98,13 +134,27 @@ ServerSaveActivity::ServerSaveActivity(SaveInfo save, bool saveNow, ServerSaveAc
WindowActivity(ui::Point(-1, -1), ui::Point(200, 50)),
thumbnail(NULL),
save(save),
callback(callback)
callback(callback),
saveUploadTask(NULL)
{
ui::Label * titleLabel = new ui::Label(ui::Point(0, 0), Size, "Saving to server...");
titleLabel->SetTextColour(style::Colour::InformationTitle);
titleLabel->Appearance.HorizontalAlign = ui::Appearance::AlignCentre;
titleLabel->Appearance.VerticalAlign = ui::Appearance::AlignMiddle;
AddComponent(titleLabel);
saveUploadTask = new SaveUploadTask(save);
saveUploadTask->AddTaskListener(this);
saveUploadTask->Start();
}
void ServerSaveActivity::NotifyDone(Task * task)
{
Exit();
if(!task->GetSuccess())
{
new ErrorMessage("Error", "Error while saving");
}
}
void ServerSaveActivity::Save()
@ -169,6 +219,12 @@ void ServerSaveActivity::Exit()
WindowActivity::Exit();
}
void ServerSaveActivity::OnTick(float dt)
{
if(saveUploadTask)
saveUploadTask->Poll();
}
void ServerSaveActivity::OnDraw()
{
Graphics * g = ui::Engine::Ref().g;
@ -192,5 +248,6 @@ void ServerSaveActivity::OnThumbnailReady(Thumbnail * thumbnail)
ServerSaveActivity::~ServerSaveActivity()
{
if(saveUploadTask)
delete saveUploadTask;
}

View File

@ -3,6 +3,7 @@
#include "Activity.h"
#include "client/SaveInfo.h"
#include "client/ThumbnailListener.h"
#include "tasks/TaskListener.h"
namespace ui
{
@ -10,8 +11,9 @@ namespace ui
class Checkbox;
}
class Task;
class Thumbnail;
class ServerSaveActivity: public WindowActivity, public ThumbnailListener
class ServerSaveActivity: public WindowActivity, public ThumbnailListener, public TaskListener
{
public:
class SaveUploadedCallback
@ -28,8 +30,11 @@ public:
virtual void Exit();
virtual void OnDraw();
virtual void OnThumbnailReady(Thumbnail * thumbnail);
virtual void OnTick(float dt);
virtual ~ServerSaveActivity();
protected:
virtual void NotifyDone(Task * task);
Task * saveUploadTask;
SaveUploadedCallback * callback;
SaveInfo save;
Thumbnail * thumbnail;

View File

@ -102,7 +102,11 @@ void Task::Poll()
Task::~Task()
{
if(!done)
{
pthread_join(doWorkThread, NULL);
pthread_mutex_destroy(&taskMutex);
}
}
void Task::before()