Skip to content

Commit af2268b

Browse files
committed
feat(sidebar): Highlight for active buttons
1 parent 4773eea commit af2268b

File tree

4 files changed

+84
-41
lines changed

4 files changed

+84
-41
lines changed

src/board.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -405,12 +405,15 @@ void Board::askForWeight() {
405405
DrawRectangleRec(box, color);
406406
DrawRectangleLinesEx(box, 2, BLACK);
407407
DrawText(weightInput, box.x + 10, box.y + 15, 30, DARKGRAY);
408-
int key = GetCharPressed();
409408

409+
int key = GetCharPressed();
410410
while (key > 0) {
411411
if (isdigit(key) && weightDigitCount < 9) {
412412
weightInput[weightDigitCount++] = (char)key;
413413
weightInput[weightDigitCount] = '\0';
414+
} else if (key == '-' && !weightDigitCount) {
415+
weightInput[weightDigitCount++] = '-';
416+
weightInput[weightDigitCount] = '\0';
414417
}
415418

416419
key = GetCharPressed();

src/main.cpp

Lines changed: 6 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,7 @@
44
#include "raylib.h"
55
#include "sidebar.h"
66

7-
#define SMALL 30.0f
8-
#define MEDIUM 50.0f
9-
#define LARGE 70.0f
10-
11-
float radius = MEDIUM;
7+
float radius;
128
static std::optional<Vector2> firstNode = std::nullopt;
139
static std::optional<Vector2> lastNode = std::nullopt;
1410
static Vector2 startNode;
@@ -37,48 +33,27 @@ void handleLeftClick(Board& board, Sidebar& sidebar, Vector2 mouse) {
3733

3834
if (sidebar.isButtonClicked("Clear")) {
3935
board.clear();
40-
return;
41-
}
42-
43-
if (sidebar.isButtonClicked("Small")) {
44-
radius = SMALL;
45-
sidebar.resetClicks();
46-
return;
47-
}
48-
49-
if (sidebar.isButtonClicked("Medium")) {
50-
radius = MEDIUM;
51-
sidebar.resetClicks();
52-
return;
53-
}
54-
55-
if (sidebar.isButtonClicked("Large")) {
56-
radius = LARGE;
5736
sidebar.resetClicks();
5837
return;
5938
}
6039

6140
if (sidebar.isButtonClicked("BFS")) {
6241
board.runBFS(startNode);
63-
sidebar.resetClicks();
6442
return;
6543
}
6644

6745
if (sidebar.isButtonClicked("DFS")) {
6846
board.runDFS(startNode);
69-
sidebar.resetClicks();
7047
return;
7148
}
7249

7350
if (sidebar.isButtonClicked("Dijkstra")) {
7451
board.runDijkstra(startNode);
75-
sidebar.resetClicks();
7652
return;
7753
}
7854

7955
if (sidebar.isButtonClicked("Bellman-Ford")) {
8056
board.runBellmanFord(startNode);
81-
sidebar.resetClicks();
8257
return;
8358
}
8459

@@ -90,7 +65,6 @@ void handleLeftClick(Board& board, Sidebar& sidebar, Vector2 mouse) {
9065
std::cout << "karoche graph weight is now set to " << (board.isGraphWeighted() ? "true\n" : "false\n");
9166
}
9267

93-
sidebar.resetClicks();
9468
return;
9569
}
9670
}
@@ -147,6 +121,10 @@ int main() {
147121
handleLeftClick(board, sidebar, mouse);
148122
handleRightClick(board, sidebar, mouse);
149123

124+
// Sync radius with currently selected sidebar option
125+
radius = sidebar.getSelectedRadius();
126+
127+
150128
if (IsKeyPressed(KEY_RIGHT)) {
151129
board.stepForward();
152130
}
@@ -186,3 +164,4 @@ int main() {
186164

187165
CloseWindow();
188166
}
167+

src/sidebar.cpp

Lines changed: 62 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,28 @@
33
#include <vector>
44
#include <iostream>
55
#include <string>
6+
#include <map>
7+
8+
inline constexpr float SMALL = 30.0f;
9+
inline constexpr float MEDIUM = 50.0f;
10+
inline constexpr float LARGE = 70.0f;
611

712
const float margin = 10.0f;
813
float topButtons;
914

15+
static const std::map<RadiusSize, float> RadiusValues = {
16+
{RadiusSize::Small, SMALL},
17+
{RadiusSize::Medium, MEDIUM},
18+
{RadiusSize::Large, LARGE}
19+
};
20+
21+
static RadiusSize labelToRadiusEnum(const std::string& label) {
22+
if (label == "S") return RadiusSize::Small;
23+
if (label == "M") return RadiusSize::Medium;
24+
if (label == "L") return RadiusSize::Large;
25+
return RadiusSize::None;
26+
}
27+
1028
Sidebar::Sidebar(int screenHeight) :
1129
x(0),
1230
width(250),
@@ -37,6 +55,10 @@ Sidebar::Sidebar(int screenHeight) :
3755
for (int i = 0; i < radiusLabels.size(); ++i) {
3856
float buttonX = baseX + i * (buttonWidth + margin);
3957
radiuses.emplace_back(Rectangle{buttonX, radiusY, buttonWidth, buttonHeight}, radiusLabels[i]);
58+
if (radiusLabels[i] == "M") {
59+
// default value
60+
radiuses[i].isClicked = true;
61+
}
4062
}
4163

4264
yOffset += buttonHeight + margin;
@@ -51,7 +73,7 @@ void Sidebar::draw() {
5173
DrawRectangleRoundedLinesEx(temp, 0.2f, 1, 5, DARKGRAY);
5274

5375
for (const auto& button : buttons) {
54-
DrawRectangleRec(button.domain, LIGHTGRAY);
76+
DrawRectangleRec(button.domain, isButtonClicked(button.getButtonLabel()) ? GREEN : LIGHTGRAY);
5577
DrawRectangleLinesEx(button.domain, 2, DARKGRAY);
5678
int textWidth = MeasureText(button.label.c_str(), 18);
5779
DrawText(button.label.c_str(),
@@ -63,41 +85,47 @@ void Sidebar::draw() {
6385
DrawLineEx({x + 3 * margin, topButtons + margin}, {(float)width - margin - margin / 2, topButtons + margin}, 5.0f, DARKGRAY);
6486

6587
for (const auto& button : radiuses) {
66-
DrawRectangleRec(button.domain, LIGHTGRAY);
88+
DrawRectangleRec(button.domain, isButtonClicked(button.getButtonLabel()) ? GREEN : LIGHTGRAY);
6789
DrawRectangleLinesEx(button.domain, 2, DARKGRAY);
6890
int textWidth = MeasureText(button.label.c_str(), 18);
6991
DrawText(button.label.c_str(),
7092
button.domain.x + (button.domain.width - textWidth) / 2,
7193
button.domain.y + (button.domain.height - 18) / 2,
7294
18, BLACK);
73-
}
95+
}
7496
}
7597

7698
void Sidebar::handleMouse(Vector2 mousePosition) {
7799
if (mousePosition.x > width || mousePosition.y > height) {
78100
return;
79101
}
80102

81-
for (auto& button : buttons) {
82-
button.isClicked = false;
83-
}
84-
85103
for (auto& button : buttons) {
86104
const Rectangle& rect = button.domain;
87-
button.isClicked = false;
88-
if (mousePosition.x > rect.x && mousePosition.x < rect.x + rect.width &&
89-
mousePosition.y > rect.y && mousePosition.y < rect.y + rect.height) {
105+
106+
if (button.getButtonLabel() != "Weighted") {
107+
button.isClicked = false;
108+
}
109+
110+
if (CheckCollisionPointRec(mousePosition, rect)) {
90111
std::cout << "Is In Domain of " << button.label << '\n';
91112
button.isClicked = true;
113+
return;
92114
}
93-
}
115+
}
94116

95117
for (auto& button : radiuses) {
96118
const Rectangle& rect = button.domain;
97-
if (mousePosition.x > rect.x && mousePosition.x < rect.x + rect.width &&
98-
mousePosition.y > rect.y && mousePosition.y < rect.y + rect.height) {
119+
button.isClicked = false;
120+
if (CheckCollisionPointRec(mousePosition, rect)) {
99121
std::cout << "Is In Domain of " << button.label << '\n';
122+
123+
for (auto& b : radiuses) {
124+
b.isClicked = false;
125+
}
126+
100127
button.isClicked = true;
128+
return;
101129
}
102130
}
103131
}
@@ -125,8 +153,29 @@ bool Sidebar::isButtonClicked(const std::string& label) {
125153
return false;
126154
}
127155

156+
RadiusSize Sidebar::getSelectedRadiusSize() const {
157+
for (const auto& button : radiuses) {
158+
if (button.isClicked) {
159+
return labelToRadiusEnum(button.getButtonLabel());
160+
}
161+
}
162+
163+
return RadiusSize::None;
164+
}
165+
166+
float Sidebar::getSelectedRadius() const {
167+
RadiusSize selected = getSelectedRadiusSize();
168+
auto it = RadiusValues.find(selected);
169+
return (it != RadiusValues.end() ? it->second : 0.0f);
170+
}
171+
128172
void Sidebar::resetClicks() {
129173
for (auto& button : radiuses) {
174+
if (button.getButtonLabel() == "M") {
175+
button.isClicked = true;
176+
continue;
177+
}
178+
130179
button.isClicked = false;
131180
}
132181

src/sidebar.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,18 @@ struct Button {
1313

1414
Button(Rectangle rect, const std::string& str)
1515
: domain(rect), label(str), isClicked(false) {}
16+
17+
std::string getButtonLabel() const {
18+
return label;
19+
}
1620
};
1721

22+
enum class RadiusSize {
23+
Small,
24+
Medium,
25+
Large,
26+
None
27+
};
1828

1929
class Sidebar {
2030
private:
@@ -34,6 +44,8 @@ class Sidebar {
3444
bool isInSidebarDomain(Vector2 mousePosition, float currentRadius);
3545

3646
bool isButtonClicked(const std::string& label);
47+
RadiusSize getSelectedRadiusSize() const;
48+
float getSelectedRadius() const;
3749
void resetClicks();
3850
};
3951

0 commit comments

Comments
 (0)