@@ -7,11 +7,13 @@ using namespace sf;
7
7
// kinda wack to hardcode these values but whatever
8
8
int windowWidth = 1000 ;
9
9
int windowHeight = 1000 ;
10
+ int extraUIHeight = 100 ;
10
11
int gridSize = 25 ;
11
12
12
13
RenderWindow *window;
13
14
PathFinder *pathFinder;
14
15
Grid<PathFinder::GridNode>* grid;
16
+ bool includeDiagonals = true ;
15
17
16
18
Vector2f selectedGridPos = Vector2f(0 , 0 );
17
19
@@ -24,12 +26,16 @@ void playerCursor()
24
26
Vector2i mousePos = Mouse::getPosition (*window);
25
27
26
28
// 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
+ }
33
39
}
34
40
35
41
// / <summary>
@@ -69,18 +75,66 @@ void playerController()
69
75
}
70
76
else if (Keyboard::isKeyPressed (Keyboard::Key::Tab))
71
77
{
72
- if (!pathFinder->drawShortestPath (window, false ))
78
+ if (!pathFinder->drawShortestPath (window, includeDiagonals ))
73
79
{
74
80
cout << " missing start/end" << endl;
75
81
}
76
82
}
77
83
}
78
84
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
+
79
133
int main ()
80
134
{
81
135
// make the window
82
136
window = new RenderWindow (
83
- VideoMode (windowWidth, windowHeight), " Pathfinding" );
137
+ VideoMode (windowWidth, windowHeight + extraUIHeight ), " Pathfinding" );
84
138
// instantiate grid
85
139
pathFinder = new PathFinder (windowWidth / gridSize, windowHeight / gridSize, gridSize);
86
140
grid = pathFinder->getGrid ();
@@ -106,6 +160,7 @@ int main()
106
160
playerController ();
107
161
108
162
// draw objects
163
+ drawUI ();
109
164
pathFinder->drawGrid (window);
110
165
playerCursor ();
111
166
0 commit comments