Skip to content

Commit

Permalink
add new s: sign which does a save search
Browse files Browse the repository at this point in the history
also change some searchController stuff to properly queue searches when one is already going on
  • Loading branch information
jacob1 committed Jun 27, 2015
1 parent f95186d commit d67cb4b
Show file tree
Hide file tree
Showing 13 changed files with 103 additions and 70 deletions.
33 changes: 0 additions & 33 deletions src/Misc.cpp
Expand Up @@ -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
Expand Down
2 changes: 0 additions & 2 deletions src/Misc.h
Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion src/graphics/Renderer.cpp
Expand Up @@ -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);
Expand Down
45 changes: 29 additions & 16 deletions src/gui/game/GameController.cpp
Expand Up @@ -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;
}
}
Expand All @@ -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<int>(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<int>(std::string(buff));
if (saveID)
OpenSavePreview(saveID, 0, false);
break;
}
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') {
} else if (type == 'b')
{
Simulation * sim = gameModel->GetSimulation();
sim->create_part(-1, foundSign->x, foundSign->y, PT_SPRK);
}
Expand Down Expand Up @@ -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());
}

Expand Down
2 changes: 1 addition & 1 deletion src/gui/game/GameController.h
Expand Up @@ -114,7 +114,7 @@ class GameController: public ClientListener
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();
Expand Down
2 changes: 1 addition & 1 deletion src/gui/game/GameView.cpp
Expand Up @@ -211,7 +211,7 @@ GameView::GameView():
if(v->CtrlBehaviour())
v->c->OpenLocalBrowse();
else
v->c->OpenSearch();
v->c->OpenSearch("");
}
};

Expand Down
2 changes: 1 addition & 1 deletion src/gui/game/SignTool.cpp
Expand Up @@ -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);
Expand Down
25 changes: 15 additions & 10 deletions src/gui/search/SearchController.cpp
Expand Up @@ -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)
Expand Down Expand Up @@ -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()
Expand Down
1 change: 1 addition & 0 deletions src/gui/search/SearchController.h
Expand Up @@ -33,6 +33,7 @@ class SearchController
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();
Expand Down
6 changes: 4 additions & 2 deletions src/gui/search/SearchModel.cpp
Expand Up @@ -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 = "";
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion src/gui/search/SearchModel.h
Expand Up @@ -57,7 +57,7 @@ class SearchModel
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<SaveInfo*> GetSaveList();
vector<pair<string, int> > GetTagList();
string GetLastError() { return lastError; }
Expand Down
49 changes: 47 additions & 2 deletions 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_),
Expand Down Expand Up @@ -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);
Expand All @@ -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;
}
2 changes: 2 additions & 0 deletions src/simulation/Sign.h
Expand Up @@ -16,6 +16,8 @@ class sign

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

0 comments on commit d67cb4b

Please sign in to comment.