@@ -29,11 +29,13 @@ void GraphBased::initVariables() {
2929
3030 message_queue_ = std::make_shared<MessageQueue<bool >>();
3131
32- Algorithm_running_ = false ;
33- Algorithm_initialized_ = false ;
34- Algorithm_reset_ = false ;
35- Algorithm_solved_ = false ;
32+ is_running_ = false ;
33+ is_initialized_ = false ;
34+ is_reset_ = false ;
35+ is_solved_ = false ;
3636 thread_joined_ = true ;
37+ disable_run_ = false ;
38+ disable_gui_parameters_ = false ;
3739}
3840
3941void GraphBased::initColors () {
@@ -50,19 +52,24 @@ void GraphBased::initColors() {
5052 PATH_COL = sf::Color (190 , 242 , 227 , 255 );
5153}
5254
53- void GraphBased::initNodes () {
54- nodes_.clear ();
55+ void GraphBased::initNodes (bool reset) {
56+ if (reset) {
57+ nodes_.clear ();
5558
56- for (int i = 0 ; i < (mapWidth_ / gridSize_) * (mapHeight_ / gridSize_); i++) {
57- nodes_.emplace_back (std::make_shared<Node>());
59+ for (int i = 0 ; i < (mapWidth_ / gridSize_) * (mapHeight_ / gridSize_);
60+ i++) {
61+ nodes_.emplace_back (std::make_shared<Node>());
62+ }
5863 }
5964
6065 // set all nodes to free obsts and respective positions
6166 for (int x = 0 ; x < mapHeight_ / gridSize_; x++) {
6267 for (int y = 0 ; y < mapWidth_ / gridSize_; y++) {
6368 int nodeIndex = (mapWidth_ / gridSize_) * x + y;
64- nodes_[nodeIndex]->setPosition (sf::Vector2i (x, y));
65- nodes_[nodeIndex]->setObstacle (false );
69+ if (reset) {
70+ nodes_[nodeIndex]->setPosition (sf::Vector2i (x, y));
71+ nodes_[nodeIndex]->setObstacle (false );
72+ }
6673 nodes_[nodeIndex]->setVisited (false );
6774 nodes_[nodeIndex]->setFrontier (false );
6875 nodes_[nodeIndex]->setPath (false );
@@ -73,29 +80,31 @@ void GraphBased::initNodes() {
7380 }
7481
7582 // add all neighbours to respective nodes
76- for (int x = 0 ; x < mapHeight_ / gridSize_; x++) {
77- for (int y = 0 ; y < mapWidth_ / gridSize_; y++) {
78- int nodeIndex = (mapWidth_ / gridSize_) * x + y;
79- if (y > 0 )
80- nodes_[nodeIndex]->setNeighbours (
81- nodes_[x * (mapWidth_ / gridSize_) + (y - 1 )]);
82- if (y < ((mapWidth_ / gridSize_) - 1 ))
83- nodes_[nodeIndex]->setNeighbours (
84- nodes_[x * (mapWidth_ / gridSize_) + (y + 1 )]);
85- if (x > 0 )
86- nodes_[nodeIndex]->setNeighbours (
87- nodes_[(x - 1 ) * (mapWidth_ / gridSize_) + y]);
88- if (x < ((mapHeight_ / gridSize_) - 1 ))
89- nodes_[nodeIndex]->setNeighbours (
90- nodes_[(x + 1 ) * (mapWidth_ / gridSize_) + y]);
83+ if (reset) {
84+ for (int x = 0 ; x < mapHeight_ / gridSize_; x++) {
85+ for (int y = 0 ; y < mapWidth_ / gridSize_; y++) {
86+ int nodeIndex = (mapWidth_ / gridSize_) * x + y;
87+ if (y > 0 )
88+ nodes_[nodeIndex]->setNeighbours (
89+ nodes_[x * (mapWidth_ / gridSize_) + (y - 1 )]);
90+ if (y < ((mapWidth_ / gridSize_) - 1 ))
91+ nodes_[nodeIndex]->setNeighbours (
92+ nodes_[x * (mapWidth_ / gridSize_) + (y + 1 )]);
93+ if (x > 0 )
94+ nodes_[nodeIndex]->setNeighbours (
95+ nodes_[(x - 1 ) * (mapWidth_ / gridSize_) + y]);
96+ if (x < ((mapHeight_ / gridSize_) - 1 ))
97+ nodes_[nodeIndex]->setNeighbours (
98+ nodes_[(x + 1 ) * (mapWidth_ / gridSize_) + y]);
99+ }
91100 }
92- }
93101
94- // initialize Start and End nodes ptrs (upper left and lower right corners)
95- nodeStart_ = nodes_[(mapWidth_ / gridSize_) * 0 + 0 ];
96- nodeStart_->setParentNode (nullptr );
97- nodeEnd_ = nodes_[(mapWidth_ / gridSize_) * (mapHeight_ / gridSize_ - 1 ) +
98- (mapWidth_ / gridSize_ - 1 )];
102+ // initialize Start and End nodes ptrs (upper left and lower right corners)
103+ nodeStart_ = nodes_[(mapWidth_ / gridSize_) * 0 + 0 ];
104+ nodeStart_->setParentNode (nullptr );
105+ nodeEnd_ = nodes_[(mapWidth_ / gridSize_) * (mapHeight_ / gridSize_ - 1 ) +
106+ (mapWidth_ / gridSize_ - 1 )];
107+ }
99108}
100109
101110void GraphBased::endState () {}
@@ -135,21 +144,21 @@ void GraphBased::update(const float& dt) {
135144 updateKeybinds ();
136145 // updateButtons();
137146
138- if (Algorithm_reset_ ) {
139- initNodes ();
140- Algorithm_running_ = false ;
141- Algorithm_initialized_ = false ;
142- Algorithm_reset_ = false ;
143- Algorithm_solved_ = false ;
147+ if (is_reset_ ) {
148+ initNodes (false );
149+ is_running_ = false ;
150+ is_initialized_ = false ;
151+ is_reset_ = false ;
152+ is_solved_ = false ;
144153
145154 message_queue_ = std::make_shared<MessageQueue<bool >>();
146155 }
147156
148- if (Algorithm_running_ ) {
149- Algorithm_reset_ = false ;
157+ if (is_running_ ) {
158+ is_reset_ = false ;
150159
151160 // initialize Algorithm
152- if (!Algorithm_initialized_ ) {
161+ if (!is_initialized_ ) {
153162 initAlgorithm ();
154163
155164 // create thread
@@ -158,7 +167,7 @@ void GraphBased::update(const float& dt) {
158167 nodeEnd_, message_queue_);
159168
160169 thread_joined_ = false ;
161- Algorithm_initialized_ = true ;
170+ is_initialized_ = true ;
162171 }
163172
164173 // check the algorithm is solved or not
@@ -167,8 +176,8 @@ void GraphBased::update(const float& dt) {
167176 if (msg) {
168177 t_.join ();
169178 thread_joined_ = true ;
170- Algorithm_running_ = false ;
171- Algorithm_solved_ = true ;
179+ is_running_ = false ;
180+ is_solved_ = true ;
172181 }
173182 } else {
174183 // only allow mouse and key inputs
@@ -191,28 +200,53 @@ void GraphBased::clearObstacles() {
191200void GraphBased::renderGui () {
192201 // buttons
193202 {
194- if (ImGui::Button (" RESET" , ImVec2 (100 .f , 40 .f )) && !Algorithm_running_) {
195- Algorithm_reset_ = true ;
196- std::cout << " RESET" << std::endl;
203+ // RESET button
204+ {
205+ if (!disable_run_ || is_running_) ImGui::BeginDisabled ();
206+ bool clicked = ImGui::Button (" RESET" , ImVec2 (100 .f , 40 .f ));
207+ if (!disable_run_ || is_running_) ImGui::EndDisabled ();
208+ if (clicked && !is_running_) {
209+ is_reset_ = true ;
210+ disable_gui_parameters_ = false ;
211+ disable_run_ = false ;
212+ }
197213 }
214+
198215 ImGui::SameLine ();
199- if (ImGui::Button (" PAUSE" , ImVec2 (100 .f , 40 .f )))
200- std::cout << " PAUSE" << std::endl;
216+
217+ // TODO: PAUSE button
218+ // always disabled (not implemented yet)
219+ {
220+ if (true ) ImGui::BeginDisabled ();
221+ bool clicked = ImGui::Button (" PAUSE" , ImVec2 (100 .f , 40 .f ));
222+ if (true ) ImGui::EndDisabled ();
223+ }
224+
201225 ImGui::SameLine ();
202- if (ImGui::Button (" RUN" , ImVec2 (100 .f , 40 .f )) && !Algorithm_solved_) {
203- std::cout << " RUN" << std::endl;
204- Algorithm_running_ = true ;
226+
227+ // RUN button
228+ {
229+ if (disable_run_) ImGui::BeginDisabled ();
230+ bool clicked = ImGui::Button (" RUN" , ImVec2 (100 .f , 40 .f ));
231+ if (disable_run_) ImGui::EndDisabled ();
232+ if (clicked && !is_solved_) {
233+ is_running_ = true ;
234+ disable_gui_parameters_ = true ;
235+ disable_run_ = true ;
236+ }
205237 }
206238 }
207239
208240 ImGui::Spacing ();
209241 ImGui::Separator ();
210242 ImGui::Spacing ();
211243
244+ if (disable_gui_parameters_) ImGui::BeginDisabled ();
245+
212246 // grid size slider
213247 if (ImGui::SliderInt (" Grid Size" , &slider_grid_size_, 10 , 100 )) {
214- Algorithm_reset_ = true ;
215248 gridSize_ = slider_grid_size_;
249+ initNodes (true );
216250 }
217251
218252 // ImGui::Spacing();
@@ -230,6 +264,8 @@ void GraphBased::renderGui() {
230264 if (ImGui::Button (" RESET PARAMETERS" , ImVec2 (154 .f , 40 .f ))) {
231265 }
232266 }
267+
268+ if (disable_gui_parameters_) ImGui::EndDisabled ();
233269}
234270
235271void GraphBased::render () {
0 commit comments