Skip to content

Commit 575ab78

Browse files
committed
fix(sidebar): Node cannot be placed in the sidebar domain
1 parent 0bd06e8 commit 575ab78

File tree

5 files changed

+48
-9
lines changed

5 files changed

+48
-9
lines changed

src/board.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ Vector2 Board::isInNodeDomain(Vector2 mousePosition) {
4949
return {0.0f, 0.0f};
5050
}
5151

52-
void Board::addNode(Vector2 mousePosition) {
52+
void Board::addNode(Vector2 mousePosition, float currentRadius) {
5353
for (const Node& node : nodes) {
5454
float minDistance = node.getNodeRadius() * 3;
5555

@@ -65,7 +65,7 @@ void Board::addNode(Vector2 mousePosition) {
6565
}
6666
}
6767

68-
Node currentNode = Node(lastNodeIndex, mousePosition, 50.0f);
68+
Node currentNode = Node(lastNodeIndex, mousePosition, currentRadius);
6969
nodes[lastNodeIndex++] = currentNode;
7070
}
7171

src/board.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class Board {
3333

3434
Vector2 isInNodeDomain(Vector2 mousePosition);
3535

36-
void addNode(Vector2 mousePosition);
36+
void addNode(Vector2 mousePosition, float currentRadius);
3737
void addEdge(Vector2 firstNodePosition, Vector2 secondNodePosition);
3838
void removeEdge(Vector2 firstNodePosition, Vector2 secondNodePosition);
3939
void removeNode(Vector2 mousePosition);

src/main.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
#include "board.h"
22
#include <iostream>
33
#include "raylib.h"
4+
#include "sidebar.h"
5+
6+
const float radius = 50.0f;
47

58
int main() {
69
int monitor = GetCurrentMonitor();
@@ -9,12 +12,16 @@ int main() {
912
SetTargetFPS(60);
1013

1114
Board board;
15+
Sidebar sidebar(GetScreenHeight());
1216
Vector2 selectedNode = {-1, -1};
1317

1418
while (!WindowShouldClose()) {
1519
Vector2 mouse = GetMousePosition();
1620
if (IsMouseButtonPressed(MOUSE_LEFT_BUTTON)) {
17-
board.addNode(mouse);
21+
sidebar.handleMouse(mouse);
22+
if (!sidebar.isInSidebarDomain(mouse, radius)) {
23+
board.addNode(mouse, radius);
24+
}
1825
}
1926

2027
if (IsMouseButtonPressed(MOUSE_RIGHT_BUTTON)) {
@@ -34,6 +41,7 @@ int main() {
3441

3542
BeginDrawing();
3643
ClearBackground(WHITE);
44+
sidebar.draw();
3745
board.drawNodes();
3846
board.drawEdges();
3947

@@ -42,3 +50,4 @@ int main() {
4250

4351
CloseWindow();
4452
}
53+

src/sidebar.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "raylib.h"
22
#include "sidebar.h"
33
#include <vector>
4+
#include <iostream>
45
#include <string>
56

67
const float margin = 10.0f;
@@ -29,3 +30,35 @@ void Sidebar::draw() {
2930
DrawText(button.label.c_str(), button.domain.x + margin, button.domain.y + margin, 18, BLACK);
3031
}
3132
}
33+
34+
void Sidebar::handleMouse(Vector2 mousePosition) {
35+
if (mousePosition.x > width || mousePosition.y > height) {
36+
return;
37+
}
38+
39+
for (const auto& button : buttons) {
40+
const Rectangle& rect = button.domain;
41+
if (mousePosition.x > rect.x && mousePosition.x < rect.x + rect.width &&
42+
mousePosition.y > rect.y && mousePosition.y < rect.y + rect.height) {
43+
std::cout << "Is In Domain of " << button.label << '\n';
44+
}
45+
}
46+
}
47+
48+
bool Sidebar::isInSidebarDomain(Vector2 mousePosition, float currentRadius) {
49+
if (mousePosition.x - currentRadius < width && mousePosition.y - currentRadius < height) {
50+
return true;
51+
}
52+
return false;
53+
}
54+
55+
/*
56+
struct Button {
57+
Rectangle domain;
58+
std::string label;
59+
bool clicked;
60+
61+
Button(Rectangle rect, const std::string& str)
62+
: domain(rect), label(str), clicked(false) {}
63+
};
64+
*/

src/sidebar.h

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,11 @@ class Sidebar {
2525
std::vector<Button> buttons;
2626

2727
public:
28-
Sidebar(int screenHeight);
29-
30-
int getSidebarX() const;
31-
int getSidebarWidth() const;
32-
int getSidebarHeight() const;
28+
Sidebar(int screenHeight);
3329

3430
void draw();
3531
void handleMouse(Vector2 mousePosition);
32+
bool isInSidebarDomain(Vector2 mousePosition, float currentRadius);
3633

3734
bool isButtonClicked(const std::string& label);
3835
void resetClick();

0 commit comments

Comments
 (0)