Save history and new multiline formatter

This commit is contained in:
Simon Robertshaw 2012-09-13 22:39:01 +01:00
parent a11cd592cb
commit 5b52ac3675
12 changed files with 111 additions and 23 deletions

View File

@ -1412,6 +1412,7 @@ SaveInfo * Client::GetSave(int saveID, int saveDate)
json::Boolean tempFavourite = objDocument["Favourite"];
json::Number tempComments = objDocument["Comments"];
json::Number tempViews = objDocument["Views"];
json::Number tempVersion = objDocument["Version"];
json::Array tagsArray = objDocument["Tags"];
std::vector<std::string> tempTags;
@ -1437,6 +1438,7 @@ SaveInfo * Client::GetSave(int saveID, int saveDate)
tempSave->Comments = tempComments.Value();
tempSave->Favourite = tempFavourite.Value();
tempSave->Views = tempViews.Value();
tempSave->Version = tempVersion.Value();
return tempSave;
}
catch (json::Exception &e)
@ -1643,16 +1645,17 @@ std::vector<SaveInfo*> * Client::SearchSaves(int start, int count, std::string q
json::Number tempScoreDown = savesArray[j]["ScoreDown"];
json::String tempUsername = savesArray[j]["Username"];
json::String tempName = savesArray[j]["Name"];
saveArray->push_back(
new SaveInfo(
json::Number tempVersion = savesArray[j]["Version"];
SaveInfo * tempSaveInfo = new SaveInfo(
tempID.Value(),
tempDate.Value(),
tempScoreUp.Value(),
tempScoreDown.Value(),
tempUsername.Value(),
tempName.Value()
)
);
);
tempSaveInfo->Version = tempVersion.Value();
saveArray->push_back(tempSaveInfo);
}
}
catch (json::Exception &e)

View File

@ -12,7 +12,7 @@
SaveInfo::SaveInfo(SaveInfo & save) :
userName(save.userName), name(save.name), Description(save.Description), date(
save.date), Published(save.Published), id(save.id), votesUp(
save.votesUp), votesDown(save.votesDown), gameSave(NULL), vote(save.vote), tags(save.tags), Comments(save.Comments), Views(save.Views) {
save.votesUp), votesDown(save.votesDown), gameSave(NULL), vote(save.vote), tags(save.tags), Comments(save.Comments), Views(save.Views), Version(save.Version) {
if(save.gameSave)
gameSave = new GameSave(*save.gameSave);
}
@ -21,14 +21,14 @@ SaveInfo::SaveInfo(int _id, int _date, int _votesUp, int _votesDown, std::string
std::string _name) :
id(_id), votesUp(_votesUp), votesDown(_votesDown), userName(_userName), name(
_name), Description(""), date(_date), Published(
true), gameSave(NULL), vote(0), tags(), Comments(0), Views(0) {
true), gameSave(NULL), vote(0), tags(), Comments(0), Views(0), Version(0) {
}
SaveInfo::SaveInfo(int _id, int date_, int _votesUp, int _votesDown, int _vote, std::string _userName,
std::string _name, std::string description_, bool published_, std::vector<std::string> tags_) :
id(_id), votesUp(_votesUp), votesDown(_votesDown), userName(_userName), name(
_name), Description(description_), date(date_), Published(
published_), gameSave(NULL), vote(_vote), tags(tags_), Views(0), Comments(0) {
published_), gameSave(NULL), vote(_vote), tags(tags_), Views(0), Comments(0), Version(0) {
}
SaveInfo::~SaveInfo()
@ -99,6 +99,13 @@ int SaveInfo::GetVotesDown() {
return votesDown;
}
void SaveInfo::SetVersion(int version) {
this->Version = version;
}
int SaveInfo::GetVersion() {
return Version;
}
void SaveInfo::SetTags(std::vector<std::string> tags)
{
this->tags = tags;

View File

@ -18,6 +18,7 @@ public:
bool Favourite;
int Comments;
int Views;
int Version;
GameSave * gameSave;
@ -64,6 +65,9 @@ public:
void SetVotesDown(int votesDown);
int GetVotesDown();
void SetVersion(int version);
int GetVersion();
void SetTags(std::vector<std::string> tags);
std::vector<std::string> GetTags();

View File

@ -489,6 +489,11 @@ int Graphics::textwidth(const char *s)
return x-1;
}
int Graphics::CharWidth(char c)
{
return font_data[font_ptrs[(int)c]];
}
int Graphics::textnwidth(char *s, int n)
{
int x = 0;

View File

@ -129,6 +129,7 @@ public:
//Font/text metrics
static int CharIndexAtPosition(char *s, int positionX, int positionY);
static int PositionAtCharIndex(char *s, int charIndex, int & positionX, int & positionY);
static int CharWidth(char c);
static int textnwidth(char *s, int n);
static void textnpos(char *s, int n, int w, int *cx, int *cy);
static int textwidthx(char *s, int w);

View File

@ -76,21 +76,59 @@ void Label::updateMultiline()
std::copy(text.begin(), text.end(), rawText);
rawText[text.length()] = 0;
int currentWidth = 0;
char c, pc = 0;
int charIndex = 0;
int wordWidth = 0;
int lineWidth = 0;
char * wordStart = NULL;
while(c = rawText[charIndex++])
{
switch(c)
{
case ' ':
lineWidth += Graphics::CharWidth(c);
lineWidth += wordWidth;
wordWidth = 0;
break;
case '\n':
lineWidth = wordWidth = 0;
lines++;
break;
default:
if(pc == ' ')
{
wordStart = &rawText[charIndex-2];
}
wordWidth += Graphics::CharWidth(c);
if(lineWidth + wordWidth >= Size.X-(Appearance.Margin.Left+Appearance.Margin.Right))
{
if(wordStart && *wordStart)
*wordStart = '\n';
else if(!wordStart)
rawText[charIndex-1] = '\n';
lineWidth = wordWidth = 0;
lines++;
}
break;
}
pc = c;
}
if(autoHeight)
{
Size.Y = lines*12;
}
textLines = std::string(rawText);
delete[] rawText;
/*int currentWidth = 0;
char * lastSpace = NULL;
char * currentWord = rawText;
char * nextSpace;
char oldChar;
while(true)
{
nextSpace = strchr(currentWord+1, ' ');
if(nextSpace > strchr(currentWord+1, '\n'))
nextSpace = strchr(currentWord+1, '\n');
if(nextSpace)
{
oldChar = nextSpace[0];
nextSpace[0] = 0;
}
int width = Graphics::textwidth(currentWord);
if(width+currentWidth >= Size.X-(Appearance.Margin.Left+Appearance.Margin.Right))
{
@ -101,15 +139,10 @@ void Label::updateMultiline()
lines++;
}
}
else if(oldChar == '\n')
{
currentWidth = width;
lines++;
}
else
currentWidth += width;
if(nextSpace)
nextSpace[0] = oldChar;
nextSpace[0] = ' ';
if(!currentWord[0] || !currentWord[1] || !(currentWord = strchr(currentWord+1, ' ')))
break;
}
@ -118,7 +151,7 @@ void Label::updateMultiline()
Size.Y = lines*12;
}
textLines = std::string(rawText);
delete[] rawText;
delete[] rawText;*/
}
else
{

View File

@ -120,7 +120,7 @@ void SaveButton::Tick(float dt)
else if(save->GetID())
{
waitingForThumb = true;
ThumbnailBroker::Ref().RetrieveThumbnail(save->GetID() , Size.X-3, Size.Y-25, this);
ThumbnailBroker::Ref().RetrieveThumbnail(save->GetID(), save->GetVersion(), Size.X-3, Size.Y-25, this);
}
}
else if(file && file->GetGameSave())

View File

@ -15,9 +15,33 @@
#include "login/LoginController.h"
#include "Controller.h"
PreviewController::PreviewController(int saveID, int saveDate, ControllerCallback * callback):
HasExited(false),
saveId(saveID),
saveDate(saveDate),
loginWindow(NULL)
{
previewModel = new PreviewModel();
previewView = new PreviewView();
previewModel->AddObserver(previewView);
previewView->AttachController(this);
previewModel->UpdateSave(saveID, saveDate);
if(Client::Ref().GetAuthUser().ID)
{
previewModel->SetCommentBoxEnabled(true);
}
Client::Ref().AddListener(this);
this->callback = callback;
}
PreviewController::PreviewController(int saveID, ControllerCallback * callback):
HasExited(false),
saveId(saveID),
saveDate(0),
loginWindow(NULL)
{
previewModel = new PreviewModel();

View File

@ -19,6 +19,7 @@ class PreviewModel;
class PreviewView;
class PreviewController: public ClientListener {
int saveId;
int saveDate;
PreviewModel * previewModel;
PreviewView * previewView;
LoginController * loginWindow;
@ -29,6 +30,7 @@ public:
bool HasExited;
PreviewController(int saveID, ControllerCallback * callback);
PreviewController(int saveID, int saveDate, ControllerCallback * callback);
void Exit();
void DoOpen();
void OpenInBrowser();

View File

@ -181,6 +181,14 @@ void SearchController::OpenSave(int saveID)
ui::Engine::Ref().ShowWindow(activePreview->GetView());
}
void SearchController::OpenSave(int saveID, int saveDate)
{
if(activePreview)
delete activePreview;
activePreview = new PreviewController(saveID, saveDate, new OpenCallback(this));
ui::Engine::Ref().ShowWindow(activePreview->GetView());
}
void SearchController::ClearSelection()
{
searchModel->ClearSelected();

View File

@ -38,6 +38,7 @@ public:
void ShowFavourite(bool show);
void Selected(int saveID, bool selected);
void OpenSave(int saveID);
void OpenSave(int saveID, int saveDate);
void Update();
void ClearSelection();
void RemoveSelected();

View File

@ -586,7 +586,7 @@ void SearchView::NotifySaveListChanged(SearchModel * sender)
SaveOpenAction(SearchView * _v) { v = _v; }
virtual void ActionCallback(ui::SaveButton * sender)
{
v->c->OpenSave(sender->GetSave()->GetID());
v->c->OpenSave(sender->GetSave()->GetID(), sender->GetSave()->GetVersion());
}
virtual void SelectedCallback(ui::SaveButton * sender)
{