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/GameController.h"
#include "game/GameView.h" #include "game/GameView.h"
#include "dialogues/ErrorMessage.h"
#include "client/HTTP.h" #include "client/HTTP.h"
using namespace std; using namespace std;
@ -73,6 +75,8 @@ int main(int argc, char * argv[])
GameController * gameController = new GameController(); GameController * gameController = new GameController();
engine->ShowWindow(gameController->GetView()); engine->ShowWindow(gameController->GetView());
new ErrorMessage("Error", "This is a test error message");
SDL_Event event; SDL_Event event;
while(engine->Running()) 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; using namespace ui;
Label::Label(Window* parent_state, std::string labelText): /*Label::Label(Window* parent_state, std::string labelText):
Component(parent_state), Component(parent_state),
text(labelText), text(labelText),
textPosition(ui::Point(0, 0)), textPosition(ui::Point(0, 0)),
@ -14,19 +14,20 @@ Label::Label(Window* parent_state, std::string labelText):
textHAlign(AlignCentre) textHAlign(AlignCentre)
{ {
TextPosition(); TextPosition();
} }*/
Label::Label(Point position, Point size, std::string labelText): Label::Label(Point position, Point size, std::string labelText):
Component(position, size), Component(position, size),
text(labelText), text(labelText),
textPosition(ui::Point(0, 0)), textPosition(ui::Point(0, 0)),
textVAlign(AlignMiddle), textVAlign(AlignMiddle),
textHAlign(AlignCentre) textHAlign(AlignCentre),
textColour(255, 255, 255)
{ {
TextPosition(); TextPosition();
} }
Label::Label(std::string labelText): /*Label::Label(std::string labelText):
Component(), Component(),
text(labelText), text(labelText),
textPosition(ui::Point(0, 0)), textPosition(ui::Point(0, 0)),
@ -34,7 +35,7 @@ Label::Label(std::string labelText):
textHAlign(AlignCentre) textHAlign(AlignCentre)
{ {
TextPosition(); TextPosition();
} }*/
Label::~Label() Label::~Label()
{ {
@ -80,6 +81,6 @@ void Label::SetText(std::string text)
void Label::Draw(const Point& screenPos) void Label::Draw(const Point& screenPos)
{ {
Graphics * g = Engine::Ref().g; 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 "Component.h"
#include "Misc.h" #include "Misc.h"
#include "Colour.h"
namespace ui namespace ui
{ {
@ -14,10 +15,12 @@ namespace ui
ui::Point textPosition; ui::Point textPosition;
HorizontalAlignment textHAlign; HorizontalAlignment textHAlign;
VerticalAlignment textVAlign; VerticalAlignment textVAlign;
Colour textColour;
public: public:
Label(Window* parent_state, std::string labelText); //Label(Window* parent_state, std::string labelText);
Label(Point position, Point size, std::string labelText); Label(Point position, Point size, std::string labelText);
Label(std::string labelText); //Label(std::string labelText);
virtual ~Label(); virtual ~Label();
void TextPosition(); void TextPosition();
@ -26,6 +29,8 @@ namespace ui
VerticalAlignment GetVAlignment() { return textVAlign; } VerticalAlignment GetVAlignment() { return textVAlign; }
void SetAlignment(HorizontalAlignment hAlign, VerticalAlignment vAlign) { textHAlign = hAlign; textVAlign = vAlign; TextPosition(); } void SetAlignment(HorizontalAlignment hAlign, VerticalAlignment vAlign) { textHAlign = hAlign; textVAlign = vAlign; TextPosition(); }
void SetTextColour(Colour textColour) { this->textColour = textColour; }
virtual void Draw(const Point& screenPos); virtual void Draw(const Point& screenPos);
}; };

View File

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

View File

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

View File

@ -11,18 +11,63 @@
PreviewModel::PreviewModel(): PreviewModel::PreviewModel():
save(NULL), save(NULL),
savePreview(NULL), savePreview(NULL),
doOpen(false) doOpen(false),
updateSavePreviewWorking(false),
updateSavePreviewFinished(false),
updateSaveInfoWorking(false),
updateSaveInfoFinished(false)
{ {
// TODO Auto-generated constructor stub // 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) void PreviewModel::UpdateSave(int saveID, int saveDate)
{ {
save = Client::Ref().GetSave(saveID, saveDate); this->tSaveID = saveID;
notifySaveChanged(); this->tSaveDate = saveDate;
savePreview = Client::Ref().GetPreview(saveID, saveDate);
save = NULL;
savePreview = NULL;
notifyPreviewChanged(); 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) void PreviewModel::SetDoOpen(bool doOpen)
@ -67,6 +112,27 @@ void PreviewModel::AddObserver(PreviewView * observer) {
observer->NotifySaveChanged(this); 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() { PreviewModel::~PreviewModel() {
if(save) if(save)
delete save; delete save;

View File

@ -9,6 +9,7 @@
#define PREVIEWMODEL_H_ #define PREVIEWMODEL_H_
#include <vector> #include <vector>
#include <pthread.h>
#include "PreviewView.h" #include "PreviewView.h"
#include "search/Save.h" #include "search/Save.h"
#include "search/Thumbnail.h" #include "search/Thumbnail.h"
@ -23,6 +24,22 @@ class PreviewModel {
Thumbnail * savePreview; Thumbnail * savePreview;
void notifyPreviewChanged(); void notifyPreviewChanged();
void notifySaveChanged(); 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: public:
PreviewModel(); PreviewModel();
Thumbnail * GetPreview(); Thumbnail * GetPreview();
@ -31,6 +48,7 @@ public:
void UpdateSave(int saveID, int saveDate); void UpdateSave(int saveID, int saveDate);
bool GetDoOpen(); bool GetDoOpen();
void SetDoOpen(bool doOpen); void SetDoOpen(bool doOpen);
void Update();
virtual ~PreviewModel(); 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); 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) 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 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 NotifyPreviewChanged(PreviewModel * sender);
void NotifySaveChanged(PreviewModel * sender); void NotifySaveChanged(PreviewModel * sender);
virtual void OnDraw(); virtual void OnDraw();
virtual void OnTick(float dt);
virtual void OnMouseDown(int x, int y, unsigned button); virtual void OnMouseDown(int x, int y, unsigned button);
virtual ~PreviewView(); virtual ~PreviewView();
}; };

View File

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

View File

@ -6,29 +6,43 @@
SearchModel::SearchModel(): SearchModel::SearchModel():
currentSort("votes"), currentSort("votes"),
showOwn(false), 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) void SearchModel::UpdateSaveList(int pageNumber, std::string query)
{ {
lastQuery = query; lastQuery = query;
lastError = ""; lastError = "";
saveListLoaded = false;
saveList.clear(); saveList.clear();
currentPage = 1; //resultCount = 0;
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();
}
currentPage = pageNumber; currentPage = pageNumber;
notifyPageChanged();
notifySaveListChanged(); notifySaveListChanged();
notifyPageChanged();
//Threading
if(!updateSaveListWorking)
{
updateSaveListFinished = false;
updateSaveListWorking = true;
pthread_create(&updateSaveListThread, 0, &SearchModel::updateSaveListTHelper, this);
}
} }
void SearchModel::SetLoadedSave(Save * save) void SearchModel::SetLoadedSave(Save * save)
@ -45,6 +59,30 @@ vector<Save*> SearchModel::GetSaveList()
return saveList; 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) void SearchModel::AddObserver(SearchView * observer)
{ {
observers.push_back(observer); observers.push_back(observer);

View File

@ -3,6 +3,7 @@
#include <vector> #include <vector>
#include <string> #include <string>
#include <pthread.h>
#include <math.h> #include <math.h>
#include "Save.h" #include "Save.h"
#include "SearchView.h" #include "SearchView.h"
@ -26,6 +27,14 @@ private:
void notifyPageChanged(); void notifyPageChanged();
void notifySortChanged(); void notifySortChanged();
void notifyShowOwnChanged(); 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: public:
SearchModel(); SearchModel();
virtual ~SearchModel(); virtual ~SearchModel();
@ -42,6 +51,8 @@ public:
bool GetShowOwn() { return showOwn; } bool GetShowOwn() { return showOwn; }
void SetLoadedSave(Save * save); void SetLoadedSave(Save * save);
Save * GetLoadedSave(); Save * GetLoadedSave();
bool GetSavesLoaded() { return saveListLoaded; }
void Update();
}; };
#endif // SEARCHMODEL_H #endif // SEARCHMODEL_H

View File

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