Skip to content

Commit

Permalink
page textbox in browser (go to any page)
Browse files Browse the repository at this point in the history
also fix page count (add one for front page), and make page count invisible until the saves actually load
  • Loading branch information
jacob1 committed Nov 11, 2014
1 parent b3b8522 commit efaa323
Show file tree
Hide file tree
Showing 8 changed files with 96 additions and 21 deletions.
1 change: 1 addition & 0 deletions src/gui/game/GameModel.cpp
Expand Up @@ -12,6 +12,7 @@
#include "BitmapBrush.h"
#include "client/Client.h"
#include "client/GameSave.h"
#include "client/SaveFile.h"
#include "gui/game/DecorationTool.h"
#include "QuickOptions.h"
#include "GameModelException.h"
Expand Down
1 change: 1 addition & 0 deletions src/gui/game/GameView.cpp
Expand Up @@ -14,6 +14,7 @@
#include "simulation/SaveRenderer.h"
#include "simulation/SimulationData.h"
#include "gui/dialogues/ConfirmPrompt.h"
#include "client/SaveFile.h"
#include "Format.h"
#include "QuickOptions.h"
#include "IntroText.h"
Expand Down
10 changes: 8 additions & 2 deletions src/gui/search/SearchController.cpp
Expand Up @@ -121,16 +121,22 @@ void SearchController::DoSearch(std::string query, bool now)

void SearchController::PrevPage()
{
if(searchModel->GetPageNum()>1)
if (searchModel->GetPageNum()>1)
searchModel->UpdateSaveList(searchModel->GetPageNum()-1, searchModel->GetLastQuery());
}

void SearchController::NextPage()
{
if(searchModel->GetPageNum() < searchModel->GetPageCount())
if (searchModel->GetPageNum() < searchModel->GetPageCount())
searchModel->UpdateSaveList(searchModel->GetPageNum()+1, searchModel->GetLastQuery());
}

void SearchController::SetPage(int page)
{
if (page != searchModel->GetPageNum() && page > 0 && page <= searchModel->GetPageCount())
searchModel->UpdateSaveList(page, searchModel->GetLastQuery());
}

void SearchController::ChangeSort()
{
if(searchModel->GetSort() == "new")
Expand Down
1 change: 1 addition & 0 deletions src/gui/search/SearchController.h
Expand Up @@ -34,6 +34,7 @@ class SearchController
void DoSearch(std::string query, bool now = false);
void NextPage();
void PrevPage();
void SetPage(int page);
void ChangeSort();
void ShowOwn(bool show);
void ShowFavourite(bool show);
Expand Down
2 changes: 1 addition & 1 deletion src/gui/search/SearchModel.h
Expand Up @@ -64,7 +64,7 @@ class SearchModel
int GetPageCount()
{
if (!showOwn && !showFavourite && currentSort == "best" && lastQuery == "")
return max(1, (int)(ceil((resultCount+5)/20.0f)));
return max(1, (int)(ceil((resultCount+5)/20.0f))+1); //add one for front page (front page saves are repeated twice)
else
return max(1, (int)(ceil(resultCount/20.0f)));
}
Expand Down
88 changes: 76 additions & 12 deletions src/gui/search/SearchView.cpp
Expand Up @@ -4,26 +4,52 @@
#include "client/Client.h"
#include "gui/interface/Keys.h"
#include "gui/interface/SaveButton.h"
#include "gui/interface/Button.h"
#include "gui/interface/Label.h"
#include "gui/interface/RichLabel.h"
#include "gui/interface/Textbox.h"
#include "gui/interface/Spinner.h"
#include "Misc.h"
#include "Format.h"

SearchView::SearchView():
ui::Window(ui::Point(0, 0), ui::Point(WINDOWW, WINDOWH)),
saveButtons(vector<ui::SaveButton*>()),
errorLabel(NULL),
c(NULL)
c(NULL),
changed(true),
lastChanged(0),
pageCount(0)
{

Client::Ref().AddListener(this);

nextButton = new ui::Button(ui::Point(WINDOWW-52, WINDOWH-18), ui::Point(50, 16), "Next \x95");
previousButton = new ui::Button(ui::Point(1, WINDOWH-18), ui::Point(50, 16), "\x96 Prev");
infoLabel = new ui::Label(ui::Point(260, WINDOWH-18), ui::Point(WINDOWW-520, 16), "Page 1 of 1");
tagsLabel = new ui::Label(ui::Point(270, WINDOWH-18), ui::Point(WINDOWW-540, 16), "\boPopular Tags:");
motdLabel = new ui::RichLabel(ui::Point(51, WINDOWH-18), ui::Point(WINDOWW-102, 16), Client::Ref().GetMessageOfTheDay());

class PageNumAction : public ui::TextboxAction
{
SearchView * v;
public:
PageNumAction(SearchView * _v) { v = _v; }
void TextChangedCallback(ui::Textbox * sender)
{
v->textChanged();
}
};
pageTextbox = new ui::Textbox(ui::Point(283, WINDOWH-18), ui::Point(41, 16), "");
pageTextbox->SetActionCallback(new PageNumAction(this));
pageTextbox->SetInputType(ui::Textbox::Number);
pageLabel = new ui::Label(ui::Point(0, WINDOWH-18), ui::Point(30, 16), "Page"); //page [TEXTBOX] of y
pageLabel->Appearance.HorizontalAlign = ui::Appearance::AlignRight;
pageCountLabel = new ui::Label(ui::Point(WINDOWW/2+6, WINDOWH-18), ui::Point(50, 16), "");
pageCountLabel->Appearance.HorizontalAlign = ui::Appearance::AlignLeft;
AddComponent(pageLabel);
AddComponent(pageCountLabel);
AddComponent(pageTextbox);

class SearchAction : public ui::TextboxAction
{
SearchView * v;
Expand Down Expand Up @@ -131,6 +157,7 @@ SearchView::SearchView():
nextButton->SetActionCallback(new NextPageAction(this));
nextButton->Appearance.HorizontalAlign = ui::Appearance::AlignRight;
nextButton->Appearance.VerticalAlign = ui::Appearance::AlignMiddle;
nextButton->Visible = false;
class PrevPageAction : public ui::ButtonAction
{
SearchView * v;
Expand All @@ -144,10 +171,10 @@ SearchView::SearchView():
previousButton->SetActionCallback(new PrevPageAction(this));
previousButton->Appearance.HorizontalAlign = ui::Appearance::AlignLeft;
previousButton->Appearance.VerticalAlign = ui::Appearance::AlignMiddle;
previousButton->Visible = false;
AddComponent(nextButton);
AddComponent(previousButton);
AddComponent(searchField);
AddComponent(infoLabel);

loadingSpinner = new ui::Spinner(ui::Point((WINDOWW/2)-12, (WINDOWH/2)+12), ui::Point(24, 24));
AddComponent(loadingSpinner);
Expand Down Expand Up @@ -235,13 +262,23 @@ void SearchView::doSearch()
c->DoSearch(searchField->GetText());
}


void SearchView::clearSearch()
{
searchField->SetText("");
c->DoSearch(searchField->GetText(), true);
}

void SearchView::textChanged()
{
int num = format::StringToNumber<int>(pageTextbox->GetText());
if (num < 0) //0 is allowed so that you can backspace the 1
pageTextbox->SetText("1");
else if (num > pageCount)
pageTextbox->SetText(format::NumberToString(pageCount));
changed = true;
lastChanged = SDL_GetTicks()+600;
}

void SearchView::OnTryOkay(OkayMethod method)
{
c->DoSearch(searchField->GetText(), true);
Expand All @@ -252,18 +289,21 @@ SearchView::~SearchView()
Client::Ref().RemoveListener(this);
RemoveComponent(nextButton);
RemoveComponent(previousButton);
RemoveComponent(infoLabel);
RemoveComponent(pageTextbox);
RemoveComponent(pageLabel);
RemoveComponent(pageCountLabel);
delete nextButton;
delete previousButton;
delete pageTextbox;
delete pageLabel;
delete pageCountLabel;

for(int i = 0; i < saveButtons.size(); i++)
{
RemoveComponent(saveButtons[i]);
delete saveButtons[i];
}
saveButtons.clear();

delete nextButton;
delete previousButton;
delete infoLabel;
}

void SearchView::Search(std::string query)
Expand Down Expand Up @@ -330,9 +370,28 @@ void SearchView::NotifyShowFavouriteChanged(SearchModel * sender)

void SearchView::NotifyPageChanged(SearchModel * sender)
{
std::stringstream pageInfo;
pageInfo << "Page " << sender->GetPageNum() << " of " << sender->GetPageCount();
infoLabel->SetText(pageInfo.str());
pageCount = sender->GetPageCount();
if (!sender->GetSaveList().size()) //no saves
{
pageLabel->Visible = pageCountLabel->Visible = pageTextbox->Visible = false;
}
else
{
std::stringstream pageInfo;
pageInfo << "of " << pageCount;
pageCountLabel->SetText(pageInfo.str());
int width = Graphics::textwidth(pageInfo.str().c_str());

pageLabel->Position.X = WINDOWW/2-width-20;
pageTextbox->Position.X = WINDOWW/2-width+11;
pageTextbox->Size.X = width-4;
//pageCountLabel->Position.X = WINDOWW/2+6;
pageLabel->Visible = pageCountLabel->Visible = pageTextbox->Visible = true;

pageInfo.str("");
pageInfo << sender->GetPageNum();
pageTextbox->SetText(pageInfo.str());
}
if(sender->GetPageNum() == 1)
{
previousButton->Visible = false;
Expand Down Expand Up @@ -700,6 +759,11 @@ void SearchView::NotifySelectedChanged(SearchModel * sender)
void SearchView::OnTick(float dt)
{
c->Update();
if (changed && lastChanged < SDL_GetTicks())
{
changed = false;
c->SetPage(std::max(format::StringToNumber<int>(pageTextbox->GetText()), 0));
}
}

void SearchView::OnMouseWheel(int x, int y, int d)
Expand Down
13 changes: 7 additions & 6 deletions src/gui/search/SearchView.h
Expand Up @@ -3,11 +3,6 @@

#include <vector>
#include "SearchController.h"
#include "gui/interface/SaveButton.h"
#include "gui/interface/Button.h"
#include "gui/interface/Label.h"
#include "gui/interface/Spinner.h"
#include "gui/interface/Textbox.h"
#include "client/ClientListener.h"

using namespace std;
Expand Down Expand Up @@ -36,7 +31,9 @@ class SearchView: public ui::Window, public ClientListener
ui::Button * previousButton;
ui::Label * errorLabel;
ui::Textbox * searchField;
ui::Label * infoLabel;
ui::Textbox * pageTextbox;
ui::Label * pageLabel;
ui::Label * pageCountLabel;
ui::Label * tagsLabel;
ui::RichLabel * motdLabel;
ui::Button * sortButton;
Expand All @@ -49,6 +46,10 @@ class SearchView: public ui::Window, public ClientListener
ui::Button * clearSelection;
void clearSearch();
void doSearch();
void textChanged();
bool changed;
int lastChanged;
int pageCount;
public:
void NotifyTagListChanged(SearchModel * sender);
void NotifySaveListChanged(SearchModel * sender);
Expand Down
1 change: 1 addition & 0 deletions src/lua/LuaScriptInterface.cpp
Expand Up @@ -21,6 +21,7 @@
#include "gui/game/Tool.h"
#include "LuaScriptHelper.h"
#include "client/HTTP.h"
#include "client/SaveFile.h"
#include "Misc.h"
#include "PowderToy.h"

Expand Down

0 comments on commit efaa323

Please sign in to comment.