Background retrieval of save info and save list.

This commit is contained in:
Simon Robertshaw 2012-01-29 14:44:36 +00:00
parent 7c53ca7799
commit 680a36549a
15 changed files with 271 additions and 29 deletions

View File

@ -18,6 +18,8 @@
#include "game/GameController.h"
#include "game/GameView.h"
#include "dialogues/ErrorMessage.h"
#include "client/HTTP.h"
using namespace std;
@ -73,6 +75,8 @@ int main(int argc, char * argv[])
GameController * gameController = new GameController();
engine->ShowWindow(gameController->GetView());
new ErrorMessage("Error", "This is a test error message");
SDL_Event event;
while(engine->Running())
{

View File

@ -0,0 +1,50 @@
/*
* ErrorMessage.cpp
*
* Created on: Jan 29, 2012
* Author: Simon
*/
#include "ErrorMessage.h"
#include "interface/Button.h"
#include "interface/Label.h"
ErrorMessage::ErrorMessage(std::string title, std::string message):
ui::Window(ui::Point(-1, -1), ui::Point(200, 75))
{
ui::Label * titleLabel = new ui::Label(ui::Point(2, 1), ui::Point(Size.X-4, 16), title);
titleLabel->SetTextColour(ui::Colour(200, 100, 50));
titleLabel->SetAlignment(AlignLeft, AlignBottom);
AddComponent(titleLabel);
ui::Label * messageLabel = new ui::Label(ui::Point(4, 18), ui::Point(Size.X-8, 60), message);
messageLabel->SetAlignment(AlignLeft, AlignTop);
AddComponent(messageLabel);
class DismissAction: public ui::ButtonAction
{
void ActionCallback(ui::Button * sender)
{
ui::Engine::Ref().CloseWindow();
}
};
ui::Button * okayButton = new ui::Button(ui::Point(0, Size.Y-16), ui::Point(Size.X, 16), "Dismiss");
okayButton->SetAlignment(AlignRight, AlignBottom);
okayButton->SetBorderColour(ui::Colour(200, 200, 200));
okayButton->SetActionCallback(new DismissAction());
AddComponent(okayButton);
ui::Engine::Ref().ShowWindow(this);
}
void ErrorMessage::OnDraw()
{
Graphics * g = ui::Engine::Ref().g;
g->clearrect(Position.X-2, Position.Y-2, Size.X+4, Size.Y+4);
g->drawrect(Position.X, Position.Y, Size.X, Size.Y, 200, 200, 200, 255);
}
ErrorMessage::~ErrorMessage() {
}

View File

@ -0,0 +1,20 @@
/*
* ErrorMessage.h
*
* Created on: Jan 29, 2012
* Author: Simon
*/
#ifndef ERRORMESSAGE_H_
#define ERRORMESSAGE_H_
#include "interface/Window.h"
class ErrorMessage: public ui::Window {
public:
ErrorMessage(std::string title, std::string message);
virtual void OnDraw();
virtual ~ErrorMessage();
};
#endif /* ERRORMESSAGE_H_ */

View File

@ -6,7 +6,7 @@
using namespace ui;
Label::Label(Window* parent_state, std::string labelText):
/*Label::Label(Window* parent_state, std::string labelText):
Component(parent_state),
text(labelText),
textPosition(ui::Point(0, 0)),
@ -14,19 +14,20 @@ Label::Label(Window* parent_state, std::string labelText):
textHAlign(AlignCentre)
{
TextPosition();
}
}*/
Label::Label(Point position, Point size, std::string labelText):
Component(position, size),
text(labelText),
textPosition(ui::Point(0, 0)),
textVAlign(AlignMiddle),
textHAlign(AlignCentre)
textHAlign(AlignCentre),
textColour(255, 255, 255)
{
TextPosition();
}
Label::Label(std::string labelText):
/*Label::Label(std::string labelText):
Component(),
text(labelText),
textPosition(ui::Point(0, 0)),
@ -34,7 +35,7 @@ Label::Label(std::string labelText):
textHAlign(AlignCentre)
{
TextPosition();
}
}*/
Label::~Label()
{
@ -80,6 +81,6 @@ void Label::SetText(std::string text)
void Label::Draw(const Point& screenPos)
{
Graphics * g = Engine::Ref().g;
g->drawtext(screenPos.X+textPosition.X, screenPos.Y+textPosition.Y, text, 255, 255, 255, 255);
g->drawtext(screenPos.X+textPosition.X, screenPos.Y+textPosition.Y, text, textColour.Red, textColour.Green, textColour.Blue, 255);
}

View File

@ -5,6 +5,7 @@
#include "Component.h"
#include "Misc.h"
#include "Colour.h"
namespace ui
{
@ -14,10 +15,12 @@ namespace ui
ui::Point textPosition;
HorizontalAlignment textHAlign;
VerticalAlignment textVAlign;
Colour textColour;
public:
Label(Window* parent_state, std::string labelText);
//Label(Window* parent_state, std::string labelText);
Label(Point position, Point size, std::string labelText);
Label(std::string labelText);
//Label(std::string labelText);
virtual ~Label();
void TextPosition();
@ -26,6 +29,8 @@ namespace ui
VerticalAlignment GetVAlignment() { return textVAlign; }
void SetAlignment(HorizontalAlignment hAlign, VerticalAlignment vAlign) { textHAlign = hAlign; textVAlign = vAlign; TextPosition(); }
void SetTextColour(Colour textColour) { this->textColour = textColour; }
virtual void Draw(const Point& screenPos);
};

View File

@ -13,7 +13,6 @@
PreviewController::PreviewController(int saveID, ControllerCallback * callback):
HasExited(false)
{
// TODO Auto-generated constructor stub
previewModel = new PreviewModel();
previewView = new PreviewView();
previewModel->AddObserver(previewView);
@ -24,6 +23,11 @@ PreviewController::PreviewController(int saveID, ControllerCallback * callback):
this->callback = callback;
}
void PreviewController::Update()
{
previewModel->Update();
}
Save * PreviewController::GetSave()
{
return previewModel->GetSave();

View File

@ -27,6 +27,7 @@ public:
bool GetDoOpen();
Save * GetSave();
PreviewView * GetView() { return previewView; }
void Update();
virtual ~PreviewController();
};

View File

@ -11,18 +11,63 @@
PreviewModel::PreviewModel():
save(NULL),
savePreview(NULL),
doOpen(false)
doOpen(false),
updateSavePreviewWorking(false),
updateSavePreviewFinished(false),
updateSaveInfoWorking(false),
updateSaveInfoFinished(false)
{
// TODO Auto-generated constructor stub
}
void * PreviewModel::updateSaveInfoTHelper(void * obj)
{
return ((PreviewModel*)obj)->updateSaveInfoT();
}
void * PreviewModel::updateSavePreviewTHelper(void * obj)
{
return ((PreviewModel*)obj)->updateSavePreviewT();
}
void * PreviewModel::updateSaveInfoT()
{
Save * tempSave = Client::Ref().GetSave(tSaveID, tSaveDate);
updateSaveInfoFinished = true;
return tempSave;
}
void * PreviewModel::updateSavePreviewT()
{
Thumbnail * tempThumb = Client::Ref().GetPreview(tSaveID, tSaveDate);
updateSavePreviewFinished = true;
return tempThumb;
}
void PreviewModel::UpdateSave(int saveID, int saveDate)
{
save = Client::Ref().GetSave(saveID, saveDate);
notifySaveChanged();
savePreview = Client::Ref().GetPreview(saveID, saveDate);
this->tSaveID = saveID;
this->tSaveDate = saveDate;
save = NULL;
savePreview = NULL;
notifyPreviewChanged();
notifySaveChanged();
if(!updateSavePreviewWorking)
{
updateSavePreviewWorking = true;
updateSavePreviewFinished = false;
pthread_create(&updateSavePreviewThread, 0, &PreviewModel::updateSavePreviewTHelper, this);
}
if(!updateSaveInfoWorking)
{
updateSaveInfoWorking = true;
updateSaveInfoFinished = false;
pthread_create(&updateSaveInfoThread, 0, &PreviewModel::updateSaveInfoTHelper, this);
}
}
void PreviewModel::SetDoOpen(bool doOpen)
@ -67,6 +112,27 @@ void PreviewModel::AddObserver(PreviewView * observer) {
observer->NotifySaveChanged(this);
}
void PreviewModel::Update()
{
if(updateSavePreviewWorking)
{
if(updateSavePreviewFinished)
{
pthread_join(updateSavePreviewThread, (void**)(&savePreview));
notifyPreviewChanged();
}
}
if(updateSaveInfoWorking)
{
if(updateSaveInfoFinished)
{
pthread_join(updateSaveInfoThread, (void**)(&save));
notifySaveChanged();
}
}
}
PreviewModel::~PreviewModel() {
if(save)
delete save;

View File

@ -9,6 +9,7 @@
#define PREVIEWMODEL_H_
#include <vector>
#include <pthread.h>
#include "PreviewView.h"
#include "search/Save.h"
#include "search/Thumbnail.h"
@ -23,6 +24,22 @@ class PreviewModel {
Thumbnail * savePreview;
void notifyPreviewChanged();
void notifySaveChanged();
//Background retrieval
int tSaveID;
int tSaveDate;
bool updateSavePreviewWorking;
volatile bool updateSavePreviewFinished;
pthread_t updateSavePreviewThread;
static void * updateSavePreviewTHelper(void * obj);
void * updateSavePreviewT();
bool updateSaveInfoWorking;
volatile bool updateSaveInfoFinished;
pthread_t updateSaveInfoThread;
static void * updateSaveInfoTHelper(void * obj);
void * updateSaveInfoT();
public:
PreviewModel();
Thumbnail * GetPreview();
@ -31,6 +48,7 @@ public:
void UpdateSave(int saveID, int saveDate);
bool GetDoOpen();
void SetDoOpen(bool doOpen);
void Update();
virtual ~PreviewModel();
};

View File

@ -57,6 +57,11 @@ void PreviewView::OnDraw()
g->draw_line(Position.X+XRES/2, Position.Y, Position.X+XRES/2, Position.Y+Size.Y, 255, 255, 255, XRES+BARSIZE);
}
void PreviewView::OnTick(float dt)
{
c->Update();
}
void PreviewView::OnMouseDown(int x, int y, unsigned button)
{
if(!(x > Position.X && y > Position.Y && y < Position.Y+Size.Y && x < Position.X+Size.X)) //Clicked outside window

View File

@ -28,6 +28,7 @@ public:
void NotifyPreviewChanged(PreviewModel * sender);
void NotifySaveChanged(PreviewModel * sender);
virtual void OnDraw();
virtual void OnTick(float dt);
virtual void OnMouseDown(int x, int y, unsigned button);
virtual ~PreviewView();
};

View File

@ -43,6 +43,7 @@ Save * SearchController::GetLoadedSave()
void SearchController::Update()
{
searchModel->Update();
if(activePreview && activePreview->HasExited)
{
delete activePreview;

View File

@ -6,29 +6,43 @@
SearchModel::SearchModel():
currentSort("votes"),
showOwn(false),
loadedSave(NULL)
loadedSave(NULL),
updateSaveListWorking(false),
updateSaveListFinished(false),
saveListLoaded(false)
{
}
void * SearchModel::updateSaveListTHelper(void * obj)
{
return ((SearchModel *)obj)->updateSaveListT();
}
void * SearchModel::updateSaveListT()
{
vector<Save*> * tempSaveList = Client::Ref().SearchSaves((currentPage-1)*20, 20, lastQuery, currentSort, resultCount);
updateSaveListFinished = true;
return tempSaveList;
}
void SearchModel::UpdateSaveList(int pageNumber, std::string query)
{
lastQuery = query;
lastError = "";
saveListLoaded = false;
saveList.clear();
currentPage = 1;
resultCount = 0;
notifySaveListChanged();
notifyPageChanged();
vector<Save*> * tempSaveList = Client::Ref().SearchSaves((pageNumber-1)*20, 20, query, currentSort, resultCount);
saveList = *tempSaveList;
delete tempSaveList;
if(!saveList.size())
{
lastError = Client::Ref().GetLastError();
}
//resultCount = 0;
currentPage = pageNumber;
notifyPageChanged();
notifySaveListChanged();
notifyPageChanged();
//Threading
if(!updateSaveListWorking)
{
updateSaveListFinished = false;
updateSaveListWorking = true;
pthread_create(&updateSaveListThread, 0, &SearchModel::updateSaveListTHelper, this);
}
}
void SearchModel::SetLoadedSave(Save * save)
@ -45,6 +59,30 @@ vector<Save*> SearchModel::GetSaveList()
return saveList;
}
void SearchModel::Update()
{
if(updateSaveListWorking)
{
if(updateSaveListFinished)
{
updateSaveListWorking = false;
lastError = "";
saveListLoaded = true;
vector<Save*> * tempSaveList;
pthread_join(updateSaveListThread, (void**)(&tempSaveList));
saveList = *tempSaveList;
delete tempSaveList;
if(!saveList.size())
{
lastError = Client::Ref().GetLastError();
}
//currentPage = pageNumber;
notifyPageChanged();
notifySaveListChanged();
}
}
}
void SearchModel::AddObserver(SearchView * observer)
{
observers.push_back(observer);

View File

@ -3,6 +3,7 @@
#include <vector>
#include <string>
#include <pthread.h>
#include <math.h>
#include "Save.h"
#include "SearchView.h"
@ -26,6 +27,14 @@ private:
void notifyPageChanged();
void notifySortChanged();
void notifyShowOwnChanged();
//Variables and methods for backgroun save request
bool saveListLoaded;
bool updateSaveListWorking;
volatile bool updateSaveListFinished;
pthread_t updateSaveListThread;
static void * updateSaveListTHelper(void * obj);
void * updateSaveListT();
public:
SearchModel();
virtual ~SearchModel();
@ -42,6 +51,8 @@ public:
bool GetShowOwn() { return showOwn; }
void SetLoadedSave(Save * save);
Save * GetLoadedSave();
bool GetSavesLoaded() { return saveListLoaded; }
void Update();
};
#endif // SEARCHMODEL_H

View File

@ -152,6 +152,16 @@ void SearchView::NotifySaveListChanged(SearchModel * sender)
delete saveButtons[i];
}
saveButtons.clear();
if(!sender->GetSavesLoaded())
{
nextButton->Enabled = false;
previousButton->Enabled = false;
}
else
{
nextButton->Enabled = true;
previousButton->Enabled = true;
}
if(!saves.size())
{
if(!errorLabel)
@ -159,11 +169,18 @@ void SearchView::NotifySaveListChanged(SearchModel * sender)
errorLabel = new ui::Label(ui::Point(((XRES+BARSIZE)/2)-100, ((YRES+MENUSIZE)/2)-6), ui::Point(200, 12), "Error");
AddComponent(errorLabel);
}
if(!sender->GetSavesLoaded())
{
errorLabel->SetText("Loading...");
}
else
{
if(sender->GetLastError().length())
errorLabel->SetText("\bo" + sender->GetLastError());
else
errorLabel->SetText("\boNo saves found");
}
}
else
{
if(errorLabel)