From d67cb4b5824637d86dd88473182449121cce19ec Mon Sep 17 00:00:00 2001 From: jacob1 Date: Sat, 27 Jun 2015 19:03:41 -0400 Subject: [PATCH] add new s: sign which does a save search also change some searchController stuff to properly queue searches when one is already going on --- src/Misc.cpp | 33 ------------------- src/Misc.h | 2 -- src/graphics/Renderer.cpp | 2 +- src/gui/game/GameController.cpp | 45 ++++++++++++++++---------- src/gui/game/GameController.h | 2 +- src/gui/game/GameView.cpp | 2 +- src/gui/game/SignTool.cpp | 2 +- src/gui/search/SearchController.cpp | 25 +++++++++------ src/gui/search/SearchController.h | 1 + src/gui/search/SearchModel.cpp | 6 ++-- src/gui/search/SearchModel.h | 2 +- src/simulation/Sign.cpp | 49 +++++++++++++++++++++++++++-- src/simulation/Sign.h | 2 ++ 13 files changed, 103 insertions(+), 70 deletions(-) diff --git a/src/Misc.cpp b/src/Misc.cpp index bd102bd00..b60277475 100644 --- a/src/Misc.cpp +++ b/src/Misc.cpp @@ -450,39 +450,6 @@ void membwand(void * destv, void * srcv, size_t destsize, size_t srcsize) } } -int splitsign(const char* str, char * type) -{ - int r; - if (str[0]=='{' && (str[1]=='c' || str[1]=='t' || str[1]=='b')) - { - const char* p=str+2; - if(str[1] != 'b') { - if(str[2]==':' && str[3]>='0' && str[3]<='9') - { - p=str+4; - while (*p>='0' && *p<='9') - p++; - } - else - return 0; - } - - if (*p=='|') - { - r=p-str; - while (*p) - p++; - if (p[-1]=='}') - { - if(type) - *type = str[1]; - return r; - } - } - } - return 0; -} - void millisleep(long int t) { #ifdef WIN diff --git a/src/Misc.h b/src/Misc.h index 418298f08..8c7f2d3eb 100644 --- a/src/Misc.h +++ b/src/Misc.h @@ -82,8 +82,6 @@ void OpenURI(std::string uri); void membwand(void * dest, void * src, size_t destsize, size_t srcsize); -int splitsign(const char* str, char * type = NULL); - void millisleep(long int t); long unsigned int gettime(); diff --git a/src/graphics/Renderer.cpp b/src/graphics/Renderer.cpp index f40383e73..312a73143 100644 --- a/src/graphics/Renderer.cpp +++ b/src/graphics/Renderer.cpp @@ -925,7 +925,7 @@ void Renderer::DrawSigns() { char type = 0; std::string text = signs[i].getText(sim); - splitsign(signs[i].text.c_str(), &type); + sign::splitsign(signs[i].text.c_str(), &type); signs[i].pos(text, x, y, w, h); clearrect(x, y, w+1, h); drawrect(x, y, w+1, h, 192, 192, 192, 255); diff --git a/src/gui/game/GameController.cpp b/src/gui/game/GameController.cpp index c16c2c06a..c76a62f66 100644 --- a/src/gui/game/GameController.cpp +++ b/src/gui/game/GameController.cpp @@ -586,7 +586,7 @@ bool GameController::MouseDown(int x, int y, unsigned button) if (!gameModel->GetActiveTool(0) || gameModel->GetActiveTool(0)->GetIdentifier() != "DEFAULT_UI_SIGN" || button != BUTTON_LEFT) //If it's not a sign tool or you are right/middle clicking { foundSign = GetSignAt(x, y); - if(foundSign && splitsign(foundSign->text.c_str())) + if(foundSign && sign::splitsign(foundSign->text.c_str())) return false; } } @@ -607,27 +607,38 @@ bool GameController::MouseUp(int x, int y, unsigned button) if(foundSign) { const char* str = foundSign->text.c_str(); char type; - int pos = splitsign(str, &type); + int pos = sign::splitsign(str, &type); if (pos) { ret = false; - if(type == 'c' || type == 't') { + if (type == 'c' || type == 't' || type == 's') + { char buff[256]; strcpy(buff, str+3); - buff[pos]=0; - int tempSaveID = format::StringToNumber(std::string(buff)); - if (tempSaveID) + buff[pos-3] = 0; + switch (str[1]) { - if (str[1] == 'c') - OpenSavePreview(tempSaveID, 0, false); - else if (str[1] == 't') - { - char url[256]; - sprintf(url, "http://powdertoy.co.uk/Discussions/Thread/View.html?Thread=%i", tempSaveID); - OpenURI(url); - } + case 'c': + { + int saveID = format::StringToNumber(std::string(buff)); + if (saveID) + OpenSavePreview(saveID, 0, false); + break; } - } else if(type == 'b') { + case 't': + { + // buff is already confirmed to be a number by sign::splitsign + std::stringstream uri; + uri << "http://powdertoy.co.uk/Discussions/Thread/View.html?Thread=" << buff; + OpenURI(uri.str()); + break; + } + case 's': + OpenSearch(buff); + break; + } + } else if (type == 'b') + { Simulation * sim = gameModel->GetSimulation(); sim->create_part(-1, foundSign->x, foundSign->y, PT_SPRK); } @@ -1073,10 +1084,12 @@ void GameController::SetReplaceModeFlags(int flags) gameModel->GetSimulation()->replaceModeFlags = flags; } -void GameController::OpenSearch() +void GameController::OpenSearch(std::string searchText) { if(!search) search = new SearchController(new SearchCallback(this)); + if (searchText.length()) + search->DoSearch2(searchText); ui::Engine::Ref().ShowWindow(search->GetView()); } diff --git a/src/gui/game/GameController.h b/src/gui/game/GameController.h index 7cbab12ee..fa2af8fe3 100644 --- a/src/gui/game/GameController.h +++ b/src/gui/game/GameController.h @@ -114,7 +114,7 @@ public: void SetToolStrength(float value); void LoadSaveFile(SaveFile * file); void LoadSave(SaveInfo * save); - void OpenSearch(); + void OpenSearch(std::string searchText); void OpenLogin(); void OpenProfile(); void OpenTags(); diff --git a/src/gui/game/GameView.cpp b/src/gui/game/GameView.cpp index c4ff27596..f28565dbb 100644 --- a/src/gui/game/GameView.cpp +++ b/src/gui/game/GameView.cpp @@ -211,7 +211,7 @@ GameView::GameView(): if(v->CtrlBehaviour()) v->c->OpenLocalBrowse(); else - v->c->OpenSearch(); + v->c->OpenSearch(""); } }; diff --git a/src/gui/game/SignTool.cpp b/src/gui/game/SignTool.cpp index e17d4d81e..94bb936d2 100644 --- a/src/gui/game/SignTool.cpp +++ b/src/gui/game/SignTool.cpp @@ -182,7 +182,7 @@ void SignWindow::DoDraw() char type = 0; Graphics * g = ui::Engine::Ref().g; std::string text = currentSign.getText(sim); - splitsign(currentSign.text.c_str(), &type); + sign::splitsign(currentSign.text.c_str(), &type); currentSign.pos(text, x, y, w, h); g->clearrect(x, y, w+1, h); g->drawrect(x, y, w+1, h, 192, 192, 192, 255); diff --git a/src/gui/search/SearchController.cpp b/src/gui/search/SearchController.cpp index f45465ac8..05b05a8b7 100644 --- a/src/gui/search/SearchController.cpp +++ b/src/gui/search/SearchController.cpp @@ -63,15 +63,16 @@ void SearchController::Update() { if (doRefresh) { - nextQueryDone = true; - doRefresh = false; - ClearSelection(); - searchModel->UpdateSaveList(searchModel->GetPageNum(), searchModel->GetLastQuery()); + if (searchModel->UpdateSaveList(searchModel->GetPageNum(), searchModel->GetLastQuery())) + { + nextQueryDone = true; + doRefresh = false; + } } else if (!nextQueryDone && nextQueryTime < gettime()) { - nextQueryDone = true; - searchModel->UpdateSaveList(1, nextQuery); + if (searchModel->UpdateSaveList(1, nextQuery)) + nextQueryDone = true; } searchModel->Update(); if(activePreview && activePreview->HasExited) @@ -112,17 +113,21 @@ SearchController::~SearchController() void SearchController::DoSearch(std::string query, bool now) { nextQuery = query; - if(!now) + if (!now) { nextQueryTime = gettime()+600; nextQueryDone = false; } else { - nextQueryDone = true; - searchModel->UpdateSaveList(1, nextQuery); + nextQueryDone = searchModel->UpdateSaveList(1, nextQuery); } - //searchModel->UpdateSaveList(1, query); +} + +void SearchController::DoSearch2(std::string query) +{ + // calls SearchView function to set textbox text, then calls DoSearch + searchView->Search(query); } void SearchController::Refresh() diff --git a/src/gui/search/SearchController.h b/src/gui/search/SearchController.h index 24e982394..094100e70 100644 --- a/src/gui/search/SearchController.h +++ b/src/gui/search/SearchController.h @@ -33,6 +33,7 @@ public: SearchView * GetView() { return searchView; } void Exit(); void DoSearch(std::string query, bool now = false); + void DoSearch2(std::string query); void Refresh(); void NextPage(); void PrevPage(); diff --git a/src/gui/search/SearchModel.cpp b/src/gui/search/SearchModel.cpp index 37e955f07..16dccd06f 100644 --- a/src/gui/search/SearchModel.cpp +++ b/src/gui/search/SearchModel.cpp @@ -61,10 +61,10 @@ void * SearchModel::updateTagListT() return tagList; } -void SearchModel::UpdateSaveList(int pageNumber, std::string query) +bool SearchModel::UpdateSaveList(int pageNumber, std::string query) { //Threading - if(!updateSaveListWorking) + if (!updateSaveListWorking) { lastQuery = query; lastError = ""; @@ -94,7 +94,9 @@ void SearchModel::UpdateSaveList(int pageNumber, std::string query) updateSaveListFinished = false; updateSaveListWorking = true; pthread_create(&updateSaveListThread, 0, &SearchModel::updateSaveListTHelper, this); + return true; } + return false; } void SearchModel::SetLoadedSave(SaveInfo * save) diff --git a/src/gui/search/SearchModel.h b/src/gui/search/SearchModel.h index 412806c45..e644102b6 100644 --- a/src/gui/search/SearchModel.h +++ b/src/gui/search/SearchModel.h @@ -57,7 +57,7 @@ public: void SetShowTags(bool show); bool GetShowTags(); void AddObserver(SearchView * observer); - void UpdateSaveList(int pageNumber, std::string query); + bool UpdateSaveList(int pageNumber, std::string query); vector GetSaveList(); vector > GetTagList(); string GetLastError() { return lastError; } diff --git a/src/simulation/Sign.cpp b/src/simulation/Sign.cpp index 58ca299f7..01b4ff2ec 100644 --- a/src/simulation/Sign.cpp +++ b/src/simulation/Sign.cpp @@ -1,7 +1,6 @@ #include "Sign.h" #include "graphics/Graphics.h" #include "simulation/Simulation.h" -#include "Misc.h" sign::sign(std::string text_, int x_, int y_, Justification justification_): x(x_), @@ -35,7 +34,7 @@ std::string sign::getText(Simulation *sim) } else { - int pos=splitsign(signText); + int pos = splitsign(signText); if (pos) { strcpy(buff, signText+pos+1); @@ -61,3 +60,49 @@ void sign::pos(std::string signText, int & x0, int & y0, int & w, int & h) (ju == 1) ? x - w/2 : x; y0 = (y > 18) ? y - 18 : y + 4; } + +int sign::splitsign(const char* str, char * type) +{ + if (str[0]=='{' && (str[1]=='c' || str[1]=='t' || str[1]=='b' || str[1]=='s')) + { + const char* p = str+2; + // signs with text arguments + if (str[1] == 's') + { + if (str[2]==':') + { + p = str+4; + while (*p && *p!='|') + p++; + } + else + return 0; + } + // signs with number arguments + if (str[1] == 'c' || str[1] == 't') + { + if (str[2]==':' && str[3]>='0' && str[3]<='9') + { + p = str+4; + while (*p>='0' && *p<='9') + p++; + } + else + return 0; + } + + if (*p=='|') + { + int r = p-str; + while (*p) + p++; + if (p[-1] == '}') + { + if (type) + *type = str[1]; + return r; + } + } + } + return 0; +} diff --git a/src/simulation/Sign.h b/src/simulation/Sign.h index 4d15b02d3..b75df1a46 100644 --- a/src/simulation/Sign.h +++ b/src/simulation/Sign.h @@ -16,6 +16,8 @@ public: std::string getText(Simulation *sim); void pos(std::string signText, int & x0, int & y0, int & w, int & h); + + static int splitsign(const char* str, char * type = NULL); }; #endif