Skip to content

Commit 39377ed

Browse files
committed
Add back button
1 parent c3119f6 commit 39377ed

File tree

6 files changed

+43
-27
lines changed

6 files changed

+43
-27
lines changed

include/Gui.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,10 @@ class Button {
4949
class DropDownList {
5050
private:
5151
sf::Font* font_;
52-
gui::Button* mainButton_;
52+
std::unique_ptr<gui::Button> mainButton_;
5353
gui::Button* activeButton_;
54-
std::vector<gui::Button*> list_;
54+
// std::vector<gui::Button*> list_;
55+
std::vector<std::unique_ptr<gui::Button>> list_;
5556
bool showList_;
5657
bool activeMode_;
5758

@@ -72,6 +73,9 @@ class DropDownList {
7273
gui::Button* getActiveButton();
7374
const bool hasActiveButton() const;
7475

76+
// Mutators
77+
void makeButtonInActive();
78+
7579
// Functions
7680
void updateKeyTime(const float& dt);
7781
void update(const sf::Vector2f mousePos, const float& dt);

include/States/MainMenu_State.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class MainMenu_State : public State {
2121
std::vector<std::string> algo_vec_;
2222

2323
std::map<std::string, std::unique_ptr<gui::Button>> buttons_;
24-
std::unique_ptr<gui::DropDownList> testDDL_;
24+
std::unique_ptr<gui::DropDownList> ddl_;
2525

2626
// Functions
2727
void initFonts();

src/Gui.cpp

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -144,24 +144,20 @@ gui::DropDownList::DropDownList(float x, float y, float width, float height,
144144
keyTime_{0.f},
145145
activeButton_{nullptr},
146146
activeMode_{false} {
147-
mainButton_ = new gui::Button(
147+
mainButton_ = std::make_unique<gui::Button>(
148148
x, y, width, height, font_, text, 20, sf::Color(251, 244, 249, 255),
149149
sf::Color(245, 238, 243, 255), sf::Color(214, 214, 215, 200));
150+
150151
for (size_t i = 0; i < numOfElements; i++) {
151-
list_.push_back(new gui::Button(
152+
list_.emplace_back(std::make_unique<gui::Button>(
152153
x, y + ((i + 1) * height), width, height, font_, algo_vec[i], 20,
153154
sf::Color(251, 244, 249, 255), sf::Color(245, 238, 243, 255),
154155
sf::Color(240, 240, 240, 200)));
155156
}
156157
}
157158

158159
// Destructor
159-
gui::DropDownList::~DropDownList() {
160-
delete mainButton_;
161-
for (auto i : list_) {
162-
delete i;
163-
}
164-
}
160+
gui::DropDownList::~DropDownList() {}
165161

166162
/**
167163
* Getter function for DropDownList ACTIVE state.
@@ -184,6 +180,8 @@ const bool gui::DropDownList::hasActiveButton() const {
184180
*/
185181
gui::Button* gui::DropDownList::getActiveButton() { return activeButton_; }
186182

183+
void gui::DropDownList::makeButtonInActive() { activeButton_ = nullptr; }
184+
187185
/**
188186
* Getter function for DropDownList key timer.
189187
*
@@ -238,7 +236,7 @@ void gui::DropDownList::update(const sf::Vector2f mousePos, const float& dt) {
238236
for (auto& i : list_) {
239237
i->update(mousePos);
240238
if (i->isPressed()) {
241-
activeButton_ = i;
239+
activeButton_ = i.get();
242240
}
243241
}
244242

src/States/Algorithms/Algorithm.cpp

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@ Algorithm::Algorithm(sf::RenderWindow* window,
1515

1616
// Destructor
1717
Algorithm::~Algorithm() {
18-
if (!thread_joined_) t_.join();
18+
if (!thread_joined_) {
19+
// std::cout << "thread joined" << '\n';
20+
t_.join();
21+
}
1922
}
2023

2124
void Algorithm::initVariables() {
@@ -25,11 +28,8 @@ void Algorithm::initVariables() {
2528
mapWidth_ = 900;
2629
mapHeight_ = 640;
2730

28-
// old array initialization
29-
// nodes_ = std::make_unique<Node[]>((mapWidth_ / gridSize_) * (mapHeight_ /
30-
// gridSize_));
3131
for (int i = 0; i < (mapWidth_ / gridSize_) * (mapHeight_ / gridSize_); i++) {
32-
nodes_.push_back(std::make_shared<Node>());
32+
nodes_.emplace_back(std::make_shared<Node>());
3333
}
3434

3535
message_queue_ = std::make_shared<MessageQueue<bool>>();
@@ -87,6 +87,9 @@ void Algorithm::initButtons() {
8787

8888
buttons_["RESET"] = std::make_unique<gui::Button>(
8989
150, 210, 150, 40, &font2_, "RESET", 18, IDLE_COL, HOVER_COL, ACTIVE_COL);
90+
91+
buttons_["MENU"] = std::make_unique<gui::Button>(
92+
150, 570, 150, 40, &font2_, "MENU", 18, IDLE_COL, HOVER_COL, ACTIVE_COL);
9093
}
9194

9295
void Algorithm::initNodes() {
@@ -131,7 +134,9 @@ void Algorithm::initNodes() {
131134
(mapWidth_ / gridSize_ - 1)];
132135
}
133136

134-
void Algorithm::endState() { std::cout << "Ending Algorithm State" << '\n'; }
137+
void Algorithm::endState() {
138+
// std::cout << "Ending Algorithm State" << '\n';
139+
}
135140

136141
void Algorithm::updateKeybinds() { checkForQuit(); }
137142

@@ -149,6 +154,11 @@ void Algorithm::updateButtons() {
149154
if (buttons_["RESET"]->isPressed() && getKeyTime()) {
150155
Algorithm_reset_ = true;
151156
}
157+
158+
// Back to MainMenu
159+
if (buttons_["MENU"]->isPressed() && getKeyTime()) {
160+
quit_ = true;
161+
}
152162
}
153163

154164
/**

src/States/Algorithms/BFS/BFS.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ BFS::BFS(sf::RenderWindow *window, std::stack<std::unique_ptr<State>> &states)
55
: Algorithm(window, states, "BREADTH FIRST SEARCH") {}
66

77
// Destructor
8-
BFS::~BFS() {}
8+
BFS::~BFS() { std::cout << "BFS Destructor called" << '\n'; }
99

1010
// override initAlgorithm() function
1111
void BFS::initAlgorithm() {

src/States/MainMenu_State.cpp

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ MainMenu_State::MainMenu_State(sf::RenderWindow *window,
1212
}
1313

1414
// Destructor
15-
MainMenu_State::~MainMenu_State() {}
15+
MainMenu_State::~MainMenu_State() {
16+
// std::cout << "MainMenu Destructor called" << '\n';
17+
}
1618

1719
void MainMenu_State::initColors() {
1820
BGN_COL = sf::Color(246, 229, 245, 255);
@@ -74,8 +76,8 @@ void MainMenu_State::initButtons() {
7476
int x = window_->getSize().x / 2;
7577
int y = window_->getSize().y / 2 - 150;
7678

77-
testDDL_ = std::make_unique<gui::DropDownList>(
78-
x, y, 250, 50, &font2_, "SELECT ALGORITHM", algo_vec_, 4);
79+
ddl_ = std::make_unique<gui::DropDownList>(x, y, 250, 50, &font2_,
80+
"SELECT ALGORITHM", algo_vec_, 4);
7981

8082
buttons_["EXIT"] = std::make_unique<gui::Button>(
8183
x, y + 410, 150, 50, &font2_, "EXIT", 20, IDLE_COL, HOVER_COL,
@@ -87,10 +89,10 @@ void MainMenu_State::updateButtons(const float &dt) {
8789
it.second->update(sf::Vector2f(mousePositionWindow_));
8890
}
8991

90-
testDDL_->update(sf::Vector2f(mousePositionWindow_), dt);
92+
ddl_->update(sf::Vector2f(mousePositionWindow_), dt);
9193

92-
if (testDDL_->hasActiveButton()) {
93-
std::string algo = testDDL_->getActiveButton()->getText();
94+
if (ddl_->hasActiveButton()) {
95+
std::string algo = ddl_->getActiveButton()->getText();
9496

9597
int index = -1;
9698
for (auto i = 0; i < algo_vec_.size(); i++) {
@@ -120,6 +122,8 @@ void MainMenu_State::updateButtons(const float &dt) {
120122
default:
121123
break;
122124
}
125+
126+
ddl_->makeButtonInActive();
123127
}
124128

125129
// Quit the game
@@ -133,7 +137,7 @@ void MainMenu_State::renderButtons() {
133137
it.second->render(window_);
134138
}
135139

136-
testDDL_->render(window_);
140+
ddl_->render(window_);
137141
}
138142

139143
void MainMenu_State::renderBackground() {
@@ -147,7 +151,7 @@ void MainMenu_State::renderBackground() {
147151
}
148152

149153
void MainMenu_State::endState() {
150-
std::cout << "Ending MainMenu State" << '\n';
154+
// std::cout << "Ending MainMenu State" << '\n';
151155
}
152156

153157
void MainMenu_State::updateKeybinds() { checkForQuit(); }

0 commit comments

Comments
 (0)