Merge branch 'master' of github.com:FacialTurd/PowderToypp

This commit is contained in:
Bryan Hoyle 2012-06-22 12:00:17 -04:00
commit 6fa05d1930
9 changed files with 135 additions and 13 deletions

View File

@ -851,6 +851,7 @@ SaveInfo * Client::GetSave(int saveID, int saveDate)
json::Number tempDate = objDocument["Date"]; json::Number tempDate = objDocument["Date"];
json::Boolean tempPublished = objDocument["Published"]; json::Boolean tempPublished = objDocument["Published"];
json::Boolean tempFavourite = objDocument["Favourite"]; json::Boolean tempFavourite = objDocument["Favourite"];
json::Number tempComments = objDocument["Comments"];
json::Array tagsArray = objDocument["Tags"]; json::Array tagsArray = objDocument["Tags"];
vector<string> tempTags; vector<string> tempTags;
@ -873,6 +874,7 @@ SaveInfo * Client::GetSave(int saveID, int saveDate)
tempPublished.Value(), tempPublished.Value(),
tempTags tempTags
); );
tempSave->Comments = tempComments.Value();
tempSave->Favourite = tempFavourite.Value(); tempSave->Favourite = tempFavourite.Value();
return tempSave; return tempSave;
} }
@ -939,7 +941,7 @@ std::vector<SaveComment*> * Client::GetComments(int saveID, int start, int count
std::stringstream urlStream; std::stringstream urlStream;
char * data; char * data;
int dataStatus, dataLength; int dataStatus, dataLength;
urlStream << "http://" << SERVER << "/Browse/View.json?ID=" << saveID << "&Mode=Comments&Start=" << start << "&Count=" << count; urlStream << "http://" << SERVER << "/Browse/Comments.json?ID=" << saveID << "&Start=" << start << "&Count=" << count;
data = http_simple_get((char *)urlStream.str().c_str(), &dataStatus, &dataLength); data = http_simple_get((char *)urlStream.str().c_str(), &dataStatus, &dataLength);
if(dataStatus == 200 && data) if(dataStatus == 200 && data)
{ {

View File

@ -11,7 +11,7 @@
SaveInfo::SaveInfo(SaveInfo & save) : SaveInfo::SaveInfo(SaveInfo & save) :
userName(save.userName), name(save.name), Description(save.Description), date( userName(save.userName), name(save.name), Description(save.Description), date(
save.date), Published(save.Published), id(save.id), votesUp( save.date), Published(save.Published), id(save.id), votesUp(
save.votesUp), votesDown(save.votesDown), gameSave(NULL), vote(save.vote), tags(save.tags) { save.votesUp), votesDown(save.votesDown), gameSave(NULL), vote(save.vote), tags(save.tags), Comments(save.Comments) {
if(save.gameSave) if(save.gameSave)
gameSave = new GameSave(*save.gameSave); gameSave = new GameSave(*save.gameSave);
} }
@ -20,14 +20,14 @@ SaveInfo::SaveInfo(int _id, int _date, int _votesUp, int _votesDown, string _use
string _name) : string _name) :
id(_id), votesUp(_votesUp), votesDown(_votesDown), userName(_userName), name( id(_id), votesUp(_votesUp), votesDown(_votesDown), userName(_userName), name(
_name), Description("No description provided"), date(_date), Published( _name), Description("No description provided"), date(_date), Published(
true), gameSave(NULL), vote(0), tags() { true), gameSave(NULL), vote(0), tags(), Comments(0) {
} }
SaveInfo::SaveInfo(int _id, int date_, int _votesUp, int _votesDown, int _vote, string _userName, SaveInfo::SaveInfo(int _id, int date_, int _votesUp, int _votesDown, int _vote, string _userName,
string _name, string description_, bool published_, vector<string> tags_) : string _name, string description_, bool published_, vector<string> tags_) :
id(_id), votesUp(_votesUp), votesDown(_votesDown), userName(_userName), name( id(_id), votesUp(_votesUp), votesDown(_votesDown), userName(_userName), name(
_name), Description(description_), date(date_), Published( _name), Description(description_), date(date_), Published(
published_), gameSave(NULL), vote(_vote), tags(tags_) { published_), gameSave(NULL), vote(_vote), tags(tags_), Comments(0) {
} }
SaveInfo::~SaveInfo() SaveInfo::~SaveInfo()

View File

@ -18,6 +18,7 @@ public:
int date; int date;
int votesUp, votesDown; int votesUp, votesDown;
bool Favourite; bool Favourite;
int Comments;
GameSave * gameSave; GameSave * gameSave;

View File

@ -86,6 +86,18 @@ void PreviewController::OpenInBrowser()
} }
} }
void PreviewController::NextCommentPage()
{
if(previewModel->GetCommentsPageNum() < previewModel->GetCommentsPageCount() && previewModel->GetCommentsLoaded())
previewModel->UpdateComments(previewModel->GetCommentsPageNum()+1);
}
void PreviewController::PrevCommentPage()
{
if(previewModel->GetCommentsPageNum()>1 && previewModel->GetCommentsLoaded())
previewModel->UpdateComments(previewModel->GetCommentsPageNum()-1);
}
void PreviewController::Exit() void PreviewController::Exit()
{ {
if(ui::Engine::Ref().GetWindow() == previewView) if(ui::Engine::Ref().GetWindow() == previewView)

View File

@ -32,6 +32,10 @@ public:
PreviewView * GetView() { return previewView; } PreviewView * GetView() { return previewView; }
void Update(); void Update();
void FavouriteSave(); void FavouriteSave();
void NextCommentPage();
void PrevCommentPage();
virtual ~PreviewController(); virtual ~PreviewController();
}; };

View File

@ -5,6 +5,7 @@
* Author: Simon * Author: Simon
*/ */
#include <cmath>
#include "PreviewModel.h" #include "PreviewModel.h"
#include "client/Client.h" #include "client/Client.h"
#include "PreviewModelException.h" #include "PreviewModelException.h"
@ -18,7 +19,9 @@ PreviewModel::PreviewModel():
updateSaveInfoWorking(false), updateSaveInfoWorking(false),
updateSaveInfoFinished(false), updateSaveInfoFinished(false),
updateSaveCommentsWorking(false), updateSaveCommentsWorking(false),
updateSaveCommentsFinished(false) updateSaveCommentsFinished(false),
commentsTotal(0),
commentsPageNumber(1)
{ {
// TODO Auto-generated constructor stub // TODO Auto-generated constructor stub
@ -58,7 +61,7 @@ void * PreviewModel::updateSaveDataT()
void * PreviewModel::updateSaveCommentsT() void * PreviewModel::updateSaveCommentsT()
{ {
std::vector<SaveComment*> * tempComments = Client::Ref().GetComments(tSaveID, 0, 10); std::vector<SaveComment*> * tempComments = Client::Ref().GetComments(tSaveID, (commentsPageNumber-1)*10, 10);
updateSaveCommentsFinished = true; updateSaveCommentsFinished = true;
return tempComments; return tempComments;
} }
@ -110,6 +113,7 @@ void PreviewModel::UpdateSave(int saveID, int saveDate)
if(!updateSaveCommentsWorking) if(!updateSaveCommentsWorking)
{ {
commentsLoaded = false;
updateSaveCommentsWorking = true; updateSaveCommentsWorking = true;
updateSaveCommentsFinished = false; updateSaveCommentsFinished = false;
pthread_create(&updateSaveCommentsThread, 0, &PreviewModel::updateSaveCommentsTHelper, this); pthread_create(&updateSaveCommentsThread, 0, &PreviewModel::updateSaveCommentsTHelper, this);
@ -131,6 +135,46 @@ SaveInfo * PreviewModel::GetSave()
return save; return save;
} }
int PreviewModel::GetCommentsPageNum()
{
return commentsPageNumber;
}
int PreviewModel::GetCommentsPageCount()
{
return max(1, (int)(ceil(commentsTotal/10)));
}
bool PreviewModel::GetCommentsLoaded()
{
return commentsLoaded;
}
void PreviewModel::UpdateComments(int pageNumber)
{
commentsLoaded = false;
if(saveComments)
{
for(int i = 0; i < saveComments->size(); i++)
delete saveComments->at(i);
delete saveComments;
saveComments = NULL;
}
//resultCount = 0;
commentsPageNumber = pageNumber;
notifySaveCommentsChanged();
notifyCommentsPageChanged();
//Threading
if(!updateSaveCommentsWorking)
{
updateSaveCommentsFinished = false;
updateSaveCommentsWorking = true;
pthread_create(&updateSaveCommentsThread, 0, &PreviewModel::updateSaveCommentsTHelper, this);
}
}
std::vector<SaveComment*> * PreviewModel::GetComments() std::vector<SaveComment*> * PreviewModel::GetComments()
{ {
return saveComments; return saveComments;
@ -144,6 +188,14 @@ void PreviewModel::notifySaveChanged()
} }
} }
void PreviewModel::notifyCommentsPageChanged()
{
for(int i = 0; i < observers.size(); i++)
{
observers[i]->NotifyCommentsPageChanged(this);
}
}
void PreviewModel::notifySaveCommentsChanged() void PreviewModel::notifySaveCommentsChanged()
{ {
for(int i = 0; i < observers.size(); i++) for(int i = 0; i < observers.size(); i++)
@ -155,6 +207,8 @@ void PreviewModel::notifySaveCommentsChanged()
void PreviewModel::AddObserver(PreviewView * observer) { void PreviewModel::AddObserver(PreviewView * observer) {
observers.push_back(observer); observers.push_back(observer);
observer->NotifySaveChanged(this); observer->NotifySaveChanged(this);
observer->NotifyCommentsChanged(this);
observer->NotifyCommentsPageChanged(this);
} }
void PreviewModel::Update() void PreviewModel::Update()
@ -194,6 +248,7 @@ void PreviewModel::Update()
pthread_join(updateSaveInfoThread, (void**)(&save)); pthread_join(updateSaveInfoThread, (void**)(&save));
if(updateSaveDataFinished && save) if(updateSaveDataFinished && save)
{ {
commentsTotal = save->Comments;
try try
{ {
save->SetGameSave(new GameSave(&saveDataBuffer[0], saveDataBuffer.size())); save->SetGameSave(new GameSave(&saveDataBuffer[0], saveDataBuffer.size()));
@ -204,6 +259,7 @@ void PreviewModel::Update()
} }
} }
notifySaveChanged(); notifySaveChanged();
notifyCommentsPageChanged();
if(!save) if(!save)
throw PreviewModelException("Unable to load save"); throw PreviewModelException("Unable to load save");
} }
@ -220,6 +276,7 @@ void PreviewModel::Update()
delete saveComments; delete saveComments;
saveComments = NULL; saveComments = NULL;
} }
commentsLoaded = true;
updateSaveCommentsWorking = false; updateSaveCommentsWorking = false;
pthread_join(updateSaveCommentsThread, (void**)(&saveComments)); pthread_join(updateSaveCommentsThread, (void**)(&saveComments));
notifySaveCommentsChanged(); notifySaveCommentsChanged();

View File

@ -33,11 +33,17 @@ class PreviewModel {
std::vector<SaveComment*> * saveComments; std::vector<SaveComment*> * saveComments;
void notifySaveChanged(); void notifySaveChanged();
void notifySaveCommentsChanged(); void notifySaveCommentsChanged();
void notifyCommentsPageChanged();
//Background retrieval //Background retrieval
int tSaveID; int tSaveID;
int tSaveDate; int tSaveDate;
//
bool commentsLoaded;
int commentsTotal;
int commentsPageNumber;
bool updateSaveDataWorking; bool updateSaveDataWorking;
volatile bool updateSaveDataFinished; volatile bool updateSaveDataFinished;
pthread_t updateSaveDataThread; pthread_t updateSaveDataThread;
@ -59,6 +65,12 @@ public:
PreviewModel(); PreviewModel();
SaveInfo * GetSave(); SaveInfo * GetSave();
std::vector<SaveComment*> * GetComments(); std::vector<SaveComment*> * GetComments();
bool GetCommentsLoaded();
int GetCommentsPageNum();
int GetCommentsPageCount();
void UpdateComments(int pageNumber);
void AddObserver(PreviewView * observer); void AddObserver(PreviewView * observer);
void UpdateSave(int saveID, int saveDate); void UpdateSave(int saveID, int saveDate);
void SetFavourite(bool favourite); void SetFavourite(bool favourite);

View File

@ -21,7 +21,9 @@ PreviewView::PreviewView():
doOpen(false), doOpen(false),
commentsOffset(0), commentsOffset(0),
commentsVel(0), commentsVel(0),
maxOffset(0) maxOffset(0),
commentsBegin(true),
commentsEnd(false)
{ {
class OpenAction: public ui::ButtonAction class OpenAction: public ui::ButtonAction
{ {
@ -112,6 +114,10 @@ PreviewView::PreviewView():
authorDateLabel = new ui::Label(ui::Point(5, (YRES/2)+15+14), ui::Point(100, 16), ""); authorDateLabel = new ui::Label(ui::Point(5, (YRES/2)+15+14), ui::Point(100, 16), "");
authorDateLabel->Appearance.HorizontalAlign = ui::Appearance::AlignLeft; authorDateLabel->Appearance.VerticalAlign = ui::Appearance::AlignBottom; authorDateLabel->Appearance.HorizontalAlign = ui::Appearance::AlignLeft; authorDateLabel->Appearance.VerticalAlign = ui::Appearance::AlignBottom;
AddComponent(authorDateLabel); AddComponent(authorDateLabel);
pageInfo = new ui::Label(ui::Point((XRES/2) + 5, Size.Y-15), ui::Point(Size.X-((XRES/2) + 10), 15), "Page 1 of 1");
pageInfo->Appearance.HorizontalAlign = ui::Appearance::AlignCentre; authorDateLabel->Appearance.VerticalAlign = ui::Appearance::AlignMiddle;
AddComponent(pageInfo);
} }
void PreviewView::DoDraw() void PreviewView::DoDraw()
@ -185,12 +191,22 @@ void PreviewView::OnTick(float dt)
{ {
commentsOffset = 0; commentsOffset = 0;
commentsVel = 0; commentsVel = 0;
commentsBegin = true;
commentsEnd = false;
} }
if(commentsOffset>maxOffset) else if(commentsOffset>maxOffset)
{ {
commentsOffset = maxOffset; commentsOffset = maxOffset;
commentsVel = 0; commentsVel = 0;
commentsEnd = true;
commentsBegin = false;
} }
else
{
commentsEnd = false;
commentsBegin = false;
}
displayComments(commentsOffset); displayComments(commentsOffset);
} }
@ -303,6 +319,13 @@ void PreviewView::displayComments(int yOffset)
} }
} }
void PreviewView::NotifyCommentsPageChanged(PreviewModel * sender)
{
std::stringstream pageInfoStream;
pageInfoStream << "Page " << sender->GetCommentsPageNum() << " of " << sender->GetCommentsPageCount();
pageInfo->SetText(pageInfoStream.str());
}
void PreviewView::NotifyCommentsChanged(PreviewModel * sender) void PreviewView::NotifyCommentsChanged(PreviewModel * sender)
{ {
if(sender->GetComments()) if(sender->GetComments())
@ -335,6 +358,8 @@ void PreviewView::NotifyCommentsChanged(PreviewModel * sender)
maxOffset = (maxY-Size.Y)+16; maxOffset = (maxY-Size.Y)+16;
commentsBegin = true;
commentsEnd = false;
commentsOffset = 0; commentsOffset = 0;
commentsVel = 0; commentsVel = 0;
displayComments(commentsOffset); displayComments(commentsOffset);
@ -342,13 +367,18 @@ void PreviewView::NotifyCommentsChanged(PreviewModel * sender)
void PreviewView::OnMouseWheel(int x, int y, int d) void PreviewView::OnMouseWheel(int x, int y, int d)
{ {
commentsVel-=d; if(!d)
/*if(!d)
return; return;
commentsVel-=d;
if(d<0) if(d<0)
c->NextPage(); {
else if(commentsEnd)
c->PrevPage();*/ c->NextCommentPage();
} else {
if(commentsBegin)
c->PrevCommentPage();
}
} }
/*void PreviewView::NotifyPreviewChanged(PreviewModel * sender) /*void PreviewView::NotifyPreviewChanged(PreviewModel * sender)

View File

@ -29,6 +29,7 @@ class PreviewView: public ui::Window {
ui::Button * reportButton; ui::Button * reportButton;
ui::Label * saveNameLabel; ui::Label * saveNameLabel;
ui::Label * authorDateLabel; ui::Label * authorDateLabel;
ui::Label * pageInfo;
ui::Textblock * saveDescriptionTextblock; ui::Textblock * saveDescriptionTextblock;
std::vector<SaveComment> comments; std::vector<SaveComment> comments;
std::vector<ui::Component*> commentComponents; std::vector<ui::Component*> commentComponents;
@ -37,6 +38,8 @@ class PreviewView: public ui::Window {
int votesDown; int votesDown;
bool doOpen; bool doOpen;
bool commentsEnd;
bool commentsBegin;
int maxOffset; int maxOffset;
float commentsOffset; float commentsOffset;
float commentsVel; float commentsVel;
@ -47,6 +50,7 @@ public:
PreviewView(); PreviewView();
void NotifySaveChanged(PreviewModel * sender); void NotifySaveChanged(PreviewModel * sender);
void NotifyCommentsChanged(PreviewModel * sender); void NotifyCommentsChanged(PreviewModel * sender);
void NotifyCommentsPageChanged(PreviewModel * sender);
virtual void OnDraw(); virtual void OnDraw();
virtual void DoDraw(); virtual void DoDraw();
virtual void OnTick(float dt); virtual void OnTick(float dt);