From cebd28148d8e6ea77d180ef1e186df7993d9e413 Mon Sep 17 00:00:00 2001 From: Simon Robertshaw Date: Sat, 19 Oct 2013 17:22:43 +0100 Subject: [PATCH 1/5] Minute performance improvement for Sign.getText --- src/simulation/Sign.cpp | 45 ++++++++++++++++++++++++----------------- 1 file changed, 26 insertions(+), 19 deletions(-) diff --git a/src/simulation/Sign.cpp b/src/simulation/Sign.cpp index 57a8a0d24..b6a18cc52 100644 --- a/src/simulation/Sign.cpp +++ b/src/simulation/Sign.cpp @@ -17,30 +17,37 @@ std::string sign::getText(Simulation *sim) char signText[256]; sprintf(signText, "%s", text.substr(0, 255).c_str()); - if (!strcmp(signText,"{p}")) + if(signText[0] && signText[0] == '{') { - float pressure = 0.0f; - if (x>=0 && x=0 && ypv[y/CELL][x/CELL]; - sprintf(buff, "Pressure: %3.2f", pressure); //...pressure - } - else if (!strcmp(signText,"{t}")) - { - if (x>=0 && x=0 && ypmap[y][x]) - sprintf(buff, "Temp: %4.2f", sim->parts[sim->pmap[y][x]>>8].temp-273.15); //...temperature + if (!strcmp(signText,"{p}")) + { + float pressure = 0.0f; + if (x>=0 && x=0 && ypv[y/CELL][x/CELL]; + sprintf(buff, "Pressure: %3.2f", pressure); //...pressure + } + else if (!strcmp(signText,"{t}")) + { + if (x>=0 && x=0 && ypmap[y][x]) + sprintf(buff, "Temp: %4.2f", sim->parts[sim->pmap[y][x]>>8].temp-273.15); //...temperature + else + sprintf(buff, "Temp: 0.00"); //...temperature + } else - sprintf(buff, "Temp: 0.00"); //...temperature + { + int pos=splitsign(signText); + if (pos) + { + strcpy(buff, signText+pos+1); + buff[strlen(signText)-pos-2]=0; + } + else + strcpy(buff, signText); + } } else { - int pos=splitsign(signText); - if (pos) - { - strcpy(buff, signText+pos+1); - buff[strlen(signText)-pos-2]=0; - } - else - strcpy(buff, signText); + strcpy(buff, signText); } return std::string(buff); From 8f4296ac399ba85e79811058744cadec9fd21a0c Mon Sep 17 00:00:00 2001 From: Simon Robertshaw Date: Sat, 19 Oct 2013 18:59:49 +0100 Subject: [PATCH 2/5] Spark sign tool, {b|sometext}, when the sign is clicked, the a spark will be created at the signs position. Also MouseDown events that draw particles from being triggered when clicking on a sign --- src/Misc.cpp | 19 ++++++++++---- src/Misc.h | 2 +- src/graphics/Renderer.cpp | 10 +++++--- src/gui/game/GameController.cpp | 44 ++++++++++++++++++++++++--------- src/gui/game/GameController.h | 1 + src/gui/game/SignTool.cpp | 10 +++++--- 6 files changed, 62 insertions(+), 24 deletions(-) diff --git a/src/Misc.cpp b/src/Misc.cpp index 918e5c4fe..183dfb13a 100644 --- a/src/Misc.cpp +++ b/src/Misc.cpp @@ -622,21 +622,30 @@ void membwand(void * destv, void * srcv, size_t destsize, size_t srcsize) } } -int splitsign(const char* str) +int splitsign(const char* str, char * type) { int match=0,r; - if (str[0]=='{' && (str[1]=='c' || str[1]=='t') && str[2]==':' && str[3]>='0' && str[3]<='9') + if (str[0]=='{' && (str[1]=='c' || str[1]=='t' || str[1]=='b')) { - const char* p=str+4; - while (*p>='0' && *p<='9') - p++; + const char* p=str+2; + if(str[2]==':' && str[3]>='0' && str[3]<='9') + { + p=str+4; + while (*p>='0' && *p<='9') + p++; + } + if (*p=='|') { r=p-str; while (*p) p++; if (p[-1]=='}') + { + if(type) + *type = str[1]; return r; + } } } return 0; diff --git a/src/Misc.h b/src/Misc.h index 0f739504b..ef9ef8b2d 100644 --- a/src/Misc.h +++ b/src/Misc.h @@ -86,7 +86,7 @@ void OpenURI(std::string uri); void membwand(void * dest, void * src, size_t destsize, size_t srcsize); -int splitsign(const char* str); +int splitsign(const char* str, char * type = NULL); // a b // c d diff --git a/src/graphics/Renderer.cpp b/src/graphics/Renderer.cpp index 45bc15e7b..9c99bfac7 100644 --- a/src/graphics/Renderer.cpp +++ b/src/graphics/Renderer.cpp @@ -923,14 +923,18 @@ void Renderer::DrawSigns() for (i=0; i < signs.size(); i++) if (signs[i].text.length()) { + char type = 0; std::string text = signs[i].getText(sim); + 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); - if (splitsign(signs[i].text.c_str())) - drawtext(x+3, y+3, text, 0, 191, 255, 255); - else + if (!type) drawtext(x+3, y+3, text, 255, 255, 255, 255); + else if(type == 'b') + drawtext(x+3, y+3, text, 211, 211, 40, 255); + else + drawtext(x+3, y+3, text, 0, 191, 255, 255); x = signs[i].x; y = signs[i].y; diff --git a/src/gui/game/GameController.cpp b/src/gui/game/GameController.cpp index a114b826e..9f69f02aa 100644 --- a/src/gui/game/GameController.cpp +++ b/src/gui/game/GameController.cpp @@ -253,6 +253,18 @@ GameView * GameController::GetView() return gameView; } +sign * GameController::GetSignAt(int x, int y){ + Simulation * sim = gameModel->GetSimulation(); + for (std::vector::iterator iter = sim->signs.begin(), end = sim->signs.end(); iter != end; ++iter) + { + int signx, signy, signw, signh; + (*iter).pos((*iter).getText(sim), signx, signy, signw, signh); + if (x>=signx && x<=signx+signw && y>=signy && y<=signy+signh) + return &(*iter); + } + return NULL; +} + void GameController::PlaceSave(ui::Point position) { if(gameModel->GetPlaceSave()) @@ -550,7 +562,15 @@ bool GameController::BrushChanged(int brushType, int rx, int ry) bool GameController::MouseDown(int x, int y, unsigned button) { - return commandInterface->OnMouseDown(x, y, button); + bool ret = commandInterface->OnMouseDown(x, y, button); + ui::Point point = PointTranslate(ui::Point(x, y)); + x = point.X; + y = point.Y; + if(ret && yGetActiveTool(0)->GetIdentifier() != "DEFAULT_UI_SIGN" || button != BUTTON_LEFT) //If it's not a sign tool or you are right/middle clicking + if(GetSignAt(x, y)) + return false; + return ret; } bool GameController::MouseUp(int x, int y, unsigned button) @@ -563,17 +583,15 @@ bool GameController::MouseUp(int x, int y, unsigned button) { if (gameModel->GetActiveTool(0)->GetIdentifier() != "DEFAULT_UI_SIGN" || button != BUTTON_LEFT) //If it's not a sign tool or you are right/middle clicking { - Simulation * sim = gameModel->GetSimulation(); - for (std::vector::iterator iter = sim->signs.begin(), end = sim->signs.end(); iter != end; ++iter) - { - int signx, signy, signw, signh; - (*iter).pos((*iter).getText(sim), signx, signy, signw, signh); - if (x>=signx && x<=signx+signw && y>=signy && y<=signy+signh) + sign * foundSign = GetSignAt(x, y); + if(foundSign) { + ret = false; + const char* str=foundSign->text.c_str(); + char type; + int pos=splitsign(str, &type); + if (pos) { - const char* str=(*iter).text.c_str(); - int pos=splitsign(str); - if (pos) - { + if(type == 'c' || type == 't') { char buff[256]; strcpy(buff, str+3); buff[pos]=0; @@ -589,7 +607,9 @@ bool GameController::MouseUp(int x, int y, unsigned button) OpenURI(url); } } - break; + } else if(type == 'b') { + Simulation * sim = gameModel->GetSimulation(); + sim->create_part(-1, foundSign->x, foundSign->y, PT_SPRK); } } } diff --git a/src/gui/game/GameController.h b/src/gui/game/GameController.h index b2a7d99b6..9fe362458 100644 --- a/src/gui/game/GameController.h +++ b/src/gui/game/GameController.h @@ -56,6 +56,7 @@ public: GameController(); ~GameController(); GameView * GetView(); + sign * GetSignAt(int x, int y); bool BrushChanged(int brushType, int rx, int ry); bool MouseMove(int x, int y, int dx, int dy); diff --git a/src/gui/game/SignTool.cpp b/src/gui/game/SignTool.cpp index 9cb389a26..fdc1fb5c0 100644 --- a/src/gui/game/SignTool.cpp +++ b/src/gui/game/SignTool.cpp @@ -178,15 +178,19 @@ void SignWindow::DoDraw() { sign & currentSign = *iter; int x, y, w, h, dx, dy; + char type = 0; Graphics * g = ui::Engine::Ref().g; std::string text = currentSign.getText(sim); + 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); - if (splitsign(currentSign.text.c_str())) - g->drawtext(x+3, y+3, text, 0, 191, 255, 255); - else + if (!type) g->drawtext(x+3, y+3, text, 255, 255, 255, 255); + else if(type == 'b') + g->drawtext(x+3, y+3, text, 211, 211, 40, 255); + else + g->drawtext(x+3, y+3, text, 0, 191, 255, 255); x = currentSign.x; y = currentSign.y; From b59c381177de9fed2c60de5d75ec69f7be9f7134 Mon Sep 17 00:00:00 2001 From: Simon Robertshaw Date: Sat, 19 Oct 2013 22:10:53 +0100 Subject: [PATCH 3/5] Do not allow 't/c' signs to lack an ID --- src/Misc.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Misc.cpp b/src/Misc.cpp index 183dfb13a..ddee10372 100644 --- a/src/Misc.cpp +++ b/src/Misc.cpp @@ -634,6 +634,8 @@ int splitsign(const char* str, char * type) while (*p>='0' && *p<='9') p++; } + else if(str[1] != 'b') + return 0; if (*p=='|') { From aa09c0e7536214f20e978fd4136c418732ae38d9 Mon Sep 17 00:00:00 2001 From: Simon Robertshaw Date: Fri, 25 Oct 2013 20:40:45 +0100 Subject: [PATCH 4/5] Reject {b:1|blah} signs --- src/Misc.cpp | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/src/Misc.cpp b/src/Misc.cpp index ddee10372..39db54b13 100644 --- a/src/Misc.cpp +++ b/src/Misc.cpp @@ -628,14 +628,16 @@ int splitsign(const char* str, char * type) if (str[0]=='{' && (str[1]=='c' || str[1]=='t' || str[1]=='b')) { const char* p=str+2; - if(str[2]==':' && str[3]>='0' && str[3]<='9') - { - p=str+4; - while (*p>='0' && *p<='9') - p++; + 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; } - else if(str[1] != 'b') - return 0; if (*p=='|') { From 30d985ba7716238819185b96f811f7f6e5f4ffdb Mon Sep 17 00:00:00 2001 From: Simon Robertshaw Date: Sat, 26 Oct 2013 01:00:41 +0100 Subject: [PATCH 5/5] Only cancel click events clickable signs (not all signs) --- src/gui/game/GameController.cpp | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/gui/game/GameController.cpp b/src/gui/game/GameController.cpp index 9f69f02aa..cfcb5e6cf 100644 --- a/src/gui/game/GameController.cpp +++ b/src/gui/game/GameController.cpp @@ -568,8 +568,11 @@ bool GameController::MouseDown(int x, int y, unsigned button) y = point.Y; if(ret && yGetActiveTool(0)->GetIdentifier() != "DEFAULT_UI_SIGN" || button != BUTTON_LEFT) //If it's not a sign tool or you are right/middle clicking - if(GetSignAt(x, y)) + { + sign * foundSign = GetSignAt(x, y); + if(foundSign && splitsign(foundSign->text.c_str())) return false; + } return ret; } @@ -585,12 +588,12 @@ bool GameController::MouseUp(int x, int y, unsigned button) { sign * foundSign = GetSignAt(x, y); if(foundSign) { - ret = false; const char* str=foundSign->text.c_str(); char type; int pos=splitsign(str, &type); if (pos) { + ret = false; if(type == 'c' || type == 't') { char buff[256]; strcpy(buff, str+3);