Skip to content

Commit

Permalink
some changes to sign handling (attempt to fix crash that I can't repr…
Browse files Browse the repository at this point in the history
…oduce)
  • Loading branch information
jacob1 committed Jul 29, 2017
1 parent adb39b9 commit b38b204
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 20 deletions.
41 changes: 26 additions & 15 deletions src/gui/game/GameController.cpp
Expand Up @@ -128,7 +128,7 @@ class GameController::StampsCallback: public ControllerCallback

GameController::GameController():
firstTick(true),
foundSign(NULL),
foundSignID(-1),
activePreview(NULL),
search(NULL),
renderOptions(NULL),
Expand Down Expand Up @@ -308,17 +308,23 @@ GameView * GameController::GetView()
return gameView;
}

sign * GameController::GetSignAt(int x, int y)
int GameController::GetSignAt(int x, int y)
{
Simulation * sim = gameModel->GetSimulation();
for (std::vector<sign>::reverse_iterator iter = sim->signs.rbegin(), end = sim->signs.rend(); iter != end; ++iter)
for (int i = sim->signs.size()-1; i >= 0; i--)
{
int signx, signy, signw, signh;
(*iter).pos((*iter).getText(sim), signx, signy, signw, signh);
sim->signs[i].pos(sim->signs[i].getText(sim), signx, signy, signw, signh);
if (x>=signx && x<=signx+signw && y>=signy && y<=signy+signh)
return &(*iter);
return i;
}
return NULL;
return -1;
}

// assumed to already be a valid sign
std::string GameController::GetSignText(int signID)
{
return gameModel->GetSimulation()->signs[signID].text;
}

void GameController::PlaceSave(ui::Point position)
Expand Down Expand Up @@ -619,9 +625,13 @@ bool GameController::MouseDown(int x, int y, unsigned button)
y = point.Y;
if (!gameModel->GetActiveTool(0) || gameModel->GetActiveTool(0)->GetIdentifier() != "DEFAULT_UI_SIGN" || button != SDL_BUTTON_LEFT) //If it's not a sign tool or you are right/middle clicking
{
foundSign = GetSignAt(x, y);
if(foundSign && sign::splitsign(foundSign->text.c_str()))
return false;
foundSignID = GetSignAt(x, y);
if (foundSignID != -1)
{
sign foundSign = gameModel->GetSimulation()->signs[foundSignID];
if (sign::splitsign(foundSign.text.c_str()))
return false;
}
}
}
return ret;
Expand All @@ -632,17 +642,18 @@ bool GameController::MouseUp(int x, int y, unsigned button, char type)
bool ret = commandInterface->OnMouseUp(x, y, button, type);
if (type)
return ret;
if (ret && foundSign && y<YRES && x<XRES && !gameView->GetPlacingSave())
if (ret && foundSignID != -1 && y<YRES && x<XRES && !gameView->GetPlacingSave())
{
ui::Point point = gameModel->AdjustZoomCoords(ui::Point(x, y));
x = point.X;
y = point.Y;
if (!gameModel->GetActiveTool(0) || gameModel->GetActiveTool(0)->GetIdentifier() != "DEFAULT_UI_SIGN" || button != SDL_BUTTON_LEFT) //If it's not a sign tool or you are right/middle clicking
{
sign * foundSign = GetSignAt(x, y);
if (foundSign)
int foundSignID = GetSignAt(x, y);
if (foundSignID != -1)
{
const char* str = foundSign->text.c_str();
sign foundSign = gameModel->GetSimulation()->signs[foundSignID];
const char* str = foundSign.text.c_str();
char type;
int pos = sign::splitsign(str, &type);
if (pos)
Expand Down Expand Up @@ -678,13 +689,13 @@ bool GameController::MouseUp(int x, int y, unsigned button, char type)
else if (type == 'b')
{
Simulation * sim = gameModel->GetSimulation();
sim->create_part(-1, foundSign->x, foundSign->y, PT_SPRK);
sim->create_part(-1, foundSign.x, foundSign.y, PT_SPRK);
}
}
}
}
}
foundSign = NULL;
foundSignID = -1;
return ret;
}

Expand Down
5 changes: 3 additions & 2 deletions src/gui/game/GameController.h
Expand Up @@ -30,7 +30,7 @@ class GameController: public ClientListener
{
private:
bool firstTick;
sign * foundSign;
int foundSignID;

PreviewController * activePreview;
GameView * gameView;
Expand All @@ -57,7 +57,8 @@ class GameController: public ClientListener
GameController();
~GameController();
GameView * GetView();
sign * GetSignAt(int x, int y);
int GetSignAt(int x, int y);
std::string GetSignText(int signID);

bool MouseMove(int x, int y, int dx, int dy);
bool MouseDown(int x, int y, unsigned button);
Expand Down
6 changes: 3 additions & 3 deletions src/gui/game/GameView.cpp
Expand Up @@ -1731,10 +1731,10 @@ void GameView::OnTick(float dt)
}
}

sign * foundSign = c->GetSignAt(mousePosition.X, mousePosition.Y);
if (foundSign)
int foundSignID = c->GetSignAt(mousePosition.X, mousePosition.Y);
if (foundSignID != -1)
{
const char* str = foundSign->text.c_str();
const char* str = c->GetSignText(foundSignID).c_str();;
char type = '\0';
int pos = sign::splitsign(str, &type);
if (type == 'c' || type == 't' || type == 's')
Expand Down

0 comments on commit b38b204

Please sign in to comment.