Skip to content

Commit 766aca7

Browse files
added some buttons
1 parent a8cac96 commit 766aca7

File tree

2 files changed

+64
-9
lines changed

2 files changed

+64
-9
lines changed

Grid.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ vector<T*> *Grid<T>::getNeighbours(int x, int y, bool includeDiagonals)
7979
}
8080

8181
// skip diagonals if needed
82-
if (includeDiagonals)
82+
if (!includeDiagonals)
8383
{
8484
if ((relativeX == -1 && relativeY == -1)
8585
|| (relativeX == -1 && relativeY == 1)

Main.cpp

+63-8
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@ using namespace sf;
77
// kinda wack to hardcode these values but whatever
88
int windowWidth = 1000;
99
int windowHeight = 1000;
10+
int extraUIHeight = 100;
1011
int gridSize = 25;
1112

1213
RenderWindow *window;
1314
PathFinder *pathFinder;
1415
Grid<PathFinder::GridNode>* grid;
16+
bool includeDiagonals = true;
1517

1618
Vector2f selectedGridPos = Vector2f(0, 0);
1719

@@ -24,12 +26,16 @@ void playerCursor()
2426
Vector2i mousePos = Mouse::getPosition(*window);
2527

2628
// set selected cell
27-
selectedGridPos = grid->centerScreenCoord(mousePos);
28-
Vector2f cellSize(grid->getCellSize(), grid->getCellSize());
29-
RectangleShape selectedSquare(cellSize);
30-
selectedSquare.setFillColor(Color((unsigned long)valToColor(GridValue::SELECTED)));
31-
selectedSquare.setPosition(selectedGridPos);
32-
window->draw(selectedSquare);
29+
Vector2i gridPos = grid->screenToGrid(mousePos);
30+
if (grid->validCoords(gridPos.x, gridPos.y))
31+
{
32+
selectedGridPos = grid->centerScreenCoord(mousePos);
33+
Vector2f cellSize(grid->getCellSize(), grid->getCellSize());
34+
RectangleShape selectedSquare(cellSize);
35+
selectedSquare.setFillColor(Color((unsigned long)valToColor(GridValue::SELECTED)));
36+
selectedSquare.setPosition(selectedGridPos);
37+
window->draw(selectedSquare);
38+
}
3339
}
3440

3541
/// <summary>
@@ -69,18 +75,66 @@ void playerController()
6975
}
7076
else if (Keyboard::isKeyPressed(Keyboard::Key::Tab))
7177
{
72-
if (!pathFinder->drawShortestPath(window, false))
78+
if (!pathFinder->drawShortestPath(window, includeDiagonals))
7379
{
7480
cout << "missing start/end" << endl;
7581
}
7682
}
7783
}
7884

85+
bool isShapePressed(RectangleShape shape)
86+
{
87+
if(Mouse::isButtonPressed(Mouse::Button::Left))
88+
{
89+
Vector2i mousePos = Mouse::getPosition(*window);
90+
Vector2f mouseWorldPos(mousePos);
91+
if (shape.getGlobalBounds().contains(mouseWorldPos))
92+
{
93+
return true;
94+
}
95+
}
96+
return false;
97+
}
98+
99+
void drawUI()
100+
{
101+
// draw button to start shortest path
102+
// draw button to turn on and off diagonal search
103+
const int NUM_OF_BUTTONS = 4;
104+
105+
// size of all buttons
106+
Vector2f buttonSize(windowWidth / NUM_OF_BUTTONS, extraUIHeight);
107+
// the leftmost position for UI buttons
108+
Vector2f btnPosLeft = grid->gridToScreen(0, grid->getGridHeight());
109+
110+
RectangleShape pathFindingButton(buttonSize);
111+
pathFindingButton.setFillColor(Color::Cyan);
112+
pathFindingButton.setPosition(btnPosLeft);
113+
window->draw(pathFindingButton);
114+
115+
if (isShapePressed(pathFindingButton))
116+
{
117+
pathFinder->drawShortestPath(window, includeDiagonals);
118+
}
119+
120+
RectangleShape diagonalToggle(buttonSize);
121+
Color diagonalToggleColor = (includeDiagonals) ? Color::Green : Color::Red;
122+
diagonalToggle.setFillColor(diagonalToggleColor);
123+
diagonalToggle.setPosition(btnPosLeft + Vector2f(buttonSize.x, 0));
124+
window->draw(diagonalToggle);
125+
126+
if (isShapePressed(diagonalToggle))
127+
{
128+
includeDiagonals = !includeDiagonals;
129+
}
130+
131+
}
132+
79133
int main()
80134
{
81135
// make the window
82136
window = new RenderWindow(
83-
VideoMode(windowWidth, windowHeight), "Pathfinding");
137+
VideoMode(windowWidth, windowHeight + extraUIHeight), "Pathfinding");
84138
// instantiate grid
85139
pathFinder = new PathFinder(windowWidth / gridSize, windowHeight / gridSize, gridSize);
86140
grid = pathFinder->getGrid();
@@ -106,6 +160,7 @@ int main()
106160
playerController();
107161

108162
// draw objects
163+
drawUI();
109164
pathFinder->drawGrid(window);
110165
playerCursor();
111166

0 commit comments

Comments
 (0)