summaryrefslogtreecommitdiff
path: root/src/search
diff options
context:
space:
mode:
authorSimon Robertshaw <simon@hardwired.org.uk>2012-08-04 19:55:59 (GMT)
committer Simon Robertshaw <simon@hardwired.org.uk>2012-08-04 19:55:59 (GMT)
commit5a2da01a5b1d59bae3d1f00132230835e6803301 (patch)
treea580fe32daa8bac4acb002d32f51552a00c30e72 /src/search
parent82d2bcc7c2fe7ae5ba40b915ce22422a36d68639 (diff)
downloadpowder-5a2da01a5b1d59bae3d1f00132230835e6803301.zip
powder-5a2da01a5b1d59bae3d1f00132230835e6803301.tar.gz
Tags, fixes #55
Diffstat (limited to 'src/search')
-rw-r--r--src/search/SearchController.cpp14
-rw-r--r--src/search/SearchController.h2
-rw-r--r--src/search/SearchModel.cpp58
-rw-r--r--src/search/SearchModel.h4
-rw-r--r--src/search/SearchView.cpp99
-rw-r--r--src/search/SearchView.h3
6 files changed, 168 insertions, 12 deletions
diff --git a/src/search/SearchController.cpp b/src/search/SearchController.cpp
index f127354..cd1e5f0 100644
--- a/src/search/SearchController.cpp
+++ b/src/search/SearchController.cpp
@@ -96,11 +96,19 @@ SearchController::~SearchController()
delete searchView;
}
-void SearchController::DoSearch(std::string query)
+void SearchController::DoSearch(std::string query, bool now)
{
nextQuery = query;
- nextQueryTime = clock()+(0.6 * CLOCKS_PER_SEC);
- nextQueryDone = false;
+ if(!now)
+ {
+ nextQueryTime = clock()+(0.6 * CLOCKS_PER_SEC);
+ nextQueryDone = false;
+ }
+ else
+ {
+ nextQueryDone = true;
+ searchModel->UpdateSaveList(1, nextQuery);
+ }
//searchModel->UpdateSaveList(1, query);
}
diff --git a/src/search/SearchController.h b/src/search/SearchController.h
index 16b4039..32ca8b3 100644
--- a/src/search/SearchController.h
+++ b/src/search/SearchController.h
@@ -30,7 +30,7 @@ public:
~SearchController();
SearchView * GetView() { return searchView; }
void Exit();
- void DoSearch(std::string query);
+ void DoSearch(std::string query, bool now = false);
void NextPage();
void PrevPage();
void ChangeSort();
diff --git a/src/search/SearchModel.cpp b/src/search/SearchModel.cpp
index 9449673..63231d7 100644
--- a/src/search/SearchModel.cpp
+++ b/src/search/SearchModel.cpp
@@ -12,10 +12,16 @@ SearchModel::SearchModel():
updateSaveListFinished(false),
saveListLoaded(false),
currentPage(1),
- resultCount(0)
+ resultCount(0),
+ showTags(true)
{
}
+void SearchModel::SetShowTags(bool show)
+{
+ showTags = show;
+}
+
void * SearchModel::updateSaveListTHelper(void * obj)
{
return ((SearchModel *)obj)->updateSaveListT();
@@ -23,14 +29,27 @@ void * SearchModel::updateSaveListTHelper(void * obj)
void * SearchModel::updateSaveListT()
{
+ void ** information = new void*[2];
+
std::string category = "";
if(showFavourite)
category = "Favourites";
if(showOwn && Client::Ref().GetAuthUser().ID)
category = "by:"+Client::Ref().GetAuthUser().Username;
- vector<SaveInfo*> * tempSaveList = Client::Ref().SearchSaves((currentPage-1)*20, 20, lastQuery, currentSort=="new"?"date":"votes", category, resultCount);
+ information[0] = Client::Ref().SearchSaves((currentPage-1)*20, 20, lastQuery, currentSort=="new"?"date":"votes", category, resultCount);
+
+ if(showTags)
+ {
+ int tagResultCount;
+ information[1] = Client::Ref().GetTags(0, 24, "", tagResultCount);
+ }
+ else
+ {
+ information[1] = NULL;
+ }
+
updateSaveListFinished = true;
- return tempSaveList;
+ return information;
}
void SearchModel::UpdateSaveList(int pageNumber, std::string query)
@@ -46,6 +65,11 @@ void SearchModel::UpdateSaveList(int pageNumber, std::string query)
selected.clear();
notifySelectedChanged();
+ if(pageNumber == 1 && !showOwn && !showFavourite && currentSort == "best" && query == "")
+ SetShowTags(true);
+ else
+ SetShowTags(false);
+
//Threading
if(!updateSaveListWorking)
{
@@ -78,6 +102,11 @@ vector<SaveInfo*> SearchModel::GetSaveList()
return saveList;
}
+vector<pair<string, int> > SearchModel::GetTagList()
+{
+ return tagList;
+}
+
void SearchModel::Update()
{
if(updateSaveListWorking)
@@ -87,10 +116,25 @@ void SearchModel::Update()
updateSaveListWorking = false;
lastError = "";
saveListLoaded = true;
- vector<SaveInfo*> * tempSaveList;
- pthread_join(updateSaveListThread, (void**)(&tempSaveList));
- saveList = *tempSaveList;
- delete tempSaveList;
+ void ** tempInformation;
+ //vector<SaveInfo*> * tempSaveList;
+ pthread_join(updateSaveListThread, (void**)(&tempInformation));
+ saveList = *(vector<SaveInfo*>*)tempInformation[0];
+
+ delete (vector<SaveInfo*>*)tempInformation[0];
+
+ if(tempInformation[1])
+ {
+ tagList = *(vector<pair<string, int> >*)tempInformation[1];
+ delete (vector<pair<string, int> >*)tempInformation[1];
+ }
+ else
+ {
+ tagList = vector<pair<string, int> >();
+ }
+
+ delete[] tempInformation;
+
if(!saveList.size())
{
lastError = Client::Ref().GetLastError();
diff --git a/src/search/SearchModel.h b/src/search/SearchModel.h
index 831e141..385ff05 100644
--- a/src/search/SearchModel.h
+++ b/src/search/SearchModel.h
@@ -21,10 +21,12 @@ private:
vector<int> selected;
vector<SearchView*> observers;
vector<SaveInfo*> saveList;
+ vector<pair<string, int> > tagList;
int currentPage;
int resultCount;
bool showOwn;
bool showFavourite;
+ bool showTags;
void notifySaveListChanged();
void notifySelectedChanged();
void notifyPageChanged();
@@ -43,9 +45,11 @@ public:
SearchModel();
virtual ~SearchModel();
+ void SetShowTags(bool show);
void AddObserver(SearchView * observer);
void UpdateSaveList(int pageNumber, std::string query);
vector<SaveInfo*> GetSaveList();
+ vector<pair<string, int> > GetTagList();
string GetLastError() { return lastError; }
int GetPageCount() { return max(1, (int)(ceil(resultCount/16))); }
int GetPageNum() { return currentPage; }
diff --git a/src/search/SearchView.cpp b/src/search/SearchView.cpp
index 16d03ed..cf02985 100644
--- a/src/search/SearchView.cpp
+++ b/src/search/SearchView.cpp
@@ -16,6 +16,7 @@ SearchView::SearchView():
nextButton = new ui::Button(ui::Point(XRES+BARSIZE-52, YRES+MENUSIZE-18), ui::Point(50, 16), "Next \x95");
previousButton = new ui::Button(ui::Point(1, YRES+MENUSIZE-18), ui::Point(50, 16), "\x96 Prev");
infoLabel = new ui::Label(ui::Point(51, YRES+MENUSIZE-18), ui::Point(XRES+BARSIZE-102, 16), "Loading...");
+ tagsLabel = new ui::Label(ui::Point(51, YRES+MENUSIZE-18), ui::Point(XRES+BARSIZE-102, 16), "Popular Tags:");
class SearchAction : public ui::TextboxAction
{
@@ -219,6 +220,12 @@ SearchView::~SearchView()
delete infoLabel;
}
+void SearchView::Search(std::string query)
+{
+ searchField->SetText(query);
+ c->DoSearch(query, true);
+}
+
void SearchView::NotifySortChanged(SearchModel * sender)
{
sortButton->SetText("Show "+sender->GetSort());
@@ -293,14 +300,29 @@ void SearchView::NotifySaveListChanged(SearchModel * sender)
int buttonWidth, buttonHeight, saveX = 0, saveY = 0, savesX = 5, savesY = 4, buttonPadding = 1;
int buttonAreaWidth, buttonAreaHeight, buttonXOffset, buttonYOffset;
+ int tagWidth, tagHeight, tagX = 0, tagY = 0, tagsX = 6, tagsY = 4, tagPadding = 1;
+ int tagAreaWidth, tagAreaHeight, tagXOffset, tagYOffset;
+
vector<SaveInfo*> saves = sender->GetSaveList();
+ vector<pair<string, int> > tags = sender->GetTagList();
+ //string messageOfTheDay = sender->GetMessageOfTheDay();
+
+ RemoveComponent(tagsLabel);
+ tagsLabel->SetParentWindow(NULL);
+
Client::Ref().ClearThumbnailRequests();
for(i = 0; i < saveButtons.size(); i++)
{
RemoveComponent(saveButtons[i]);
delete saveButtons[i];
}
+ for(i = 0; i < tagButtons.size(); i++)
+ {
+ RemoveComponent(tagButtons[i]);
+ delete tagButtons[i];
+ }
saveButtons.clear();
+ tagButtons.clear();
if(!sender->GetSavesLoaded())
{
nextButton->Enabled = false;
@@ -341,12 +363,34 @@ void SearchView::NotifySaveListChanged(SearchModel * sender)
delete errorLabel;
errorLabel = NULL;
}
- buttonXOffset = buttonPadding;
+
buttonYOffset = 28;
+ buttonXOffset = buttonPadding;
buttonAreaWidth = Size.X;
buttonAreaHeight = Size.Y - buttonYOffset - 18;
+
+ if(tags.size())
+ {
+ buttonYOffset += (buttonAreaHeight/savesY) - buttonPadding*2;
+ buttonAreaHeight = Size.Y - buttonYOffset - 18;
+ savesY--;
+
+ tagXOffset = tagPadding;
+ tagYOffset = 60;
+ tagAreaWidth = Size.X;
+ tagAreaHeight = ((buttonAreaHeight/savesY) - buttonPadding*2)-(tagYOffset-28)-5;
+ tagWidth = (tagAreaWidth/tagsX) - tagPadding*2;
+ tagHeight = (tagAreaHeight/tagsY) - tagPadding*2;
+
+ AddComponent(tagsLabel);
+ tagsLabel->Position.Y = tagYOffset-16;
+ }
+
buttonWidth = (buttonAreaWidth/savesX) - buttonPadding*2;
buttonHeight = (buttonAreaHeight/savesY) - buttonPadding*2;
+
+
+
class SaveOpenAction: public ui::SaveButtonAction
{
SearchView * v;
@@ -361,6 +405,59 @@ void SearchView::NotifySaveListChanged(SearchModel * sender)
v->c->Selected(sender->GetSave()->GetID(), sender->GetSelected());
}
};
+
+ class TagAction: public ui::ButtonAction
+ {
+ SearchView * v;
+ std::string tag;
+ public:
+ TagAction(SearchView * v, std::string tag) : v(v), tag(tag) {}
+ virtual void ActionCallback(ui::Button * sender)
+ {
+ v->Search(tag);
+ }
+ };
+
+ for(i = 0; i < tags.size(); i++)
+ {
+ int maxTagVotes = tags[0].second;
+
+ pair<string, int> tag = tags[i];
+
+ if(tagX == tagsX)
+ {
+ if(tagY == tagsY-1)
+ break;
+ tagX = 0;
+ tagY++;
+ }
+
+ int tagAlpha = 192;
+ if (maxTagVotes)
+ tagAlpha = 127+(128*tag.second)/maxTagVotes;
+
+ ui::Button * tagButton;
+ tagButton = new ui::Button(
+ ui::Point(
+ tagXOffset + tagPadding + tagX*(tagWidth+tagPadding*2),
+ tagYOffset + tagPadding + tagY*(tagHeight+tagPadding*2)
+ ),
+ ui::Point(tagWidth, tagHeight),
+ tag.first
+ );
+ tagButton->SetActionCallback(new TagAction(this, tag.first));
+ tagButton->Appearance.BorderInactive = ui::Colour(0, 0, 0);
+ tagButton->Appearance.BorderHover = ui::Colour(0, 0, 0);
+ tagButton->Appearance.BorderActive = ui::Colour(0, 0, 0);
+ tagButton->Appearance.BackgroundHover = ui::Colour(0, 0, 0);
+
+ tagButton->Appearance.TextInactive = ui::Colour(tagAlpha, tagAlpha, tagAlpha);
+ tagButton->Appearance.TextHover = ui::Colour((tagAlpha*5)/6, (tagAlpha*5)/6, tagAlpha);
+ AddComponent(tagButton);
+ tagButtons.push_back(tagButton);
+ tagX++;
+
+ }
for(i = 0; i < saves.size(); i++)
{
if(saveX == savesX)
diff --git a/src/search/SearchView.h b/src/search/SearchView.h
index 6e27f06..abded85 100644
--- a/src/search/SearchView.h
+++ b/src/search/SearchView.h
@@ -19,12 +19,14 @@ class SearchView: public ui::Window
private:
SearchController * c;
vector<ui::SaveButton*> saveButtons;
+ vector<ui::Button*> tagButtons;
ui::Button * favButton;
ui::Button * nextButton;
ui::Button * previousButton;
ui::Label * errorLabel;
ui::Textbox * searchField;
ui::Label * infoLabel;
+ ui::Label * tagsLabel;
ui::Button * sortButton;
ui::Button * ownButton;
ui::Spinner * loadingSpinner;
@@ -44,6 +46,7 @@ public:
SearchView();
virtual ~SearchView();
void AttachController(SearchController * _c) { c = _c; }
+ virtual void Search(std::string);
virtual void OnTick(float dt);
virtual void OnMouseWheel(int x, int y, int d);
virtual void OnKeyPress(int key, Uint16 character, bool shift, bool ctrl, bool alt);