summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMitya Selivanov <automainint@guattari.tech>2024-11-08 06:36:08 +0100
committerMitya Selivanov <automainint@guattari.tech>2024-11-08 06:36:08 +0100
commitdc95a43da475b8ce55f25b670c9848d9dabf1cb7 (patch)
tree924dd43f6b9052bac7405dbd33388386ff03379a
parent23fe91bc8d13c5b1237bffa4293ec0e869378ed2 (diff)
downloadreduced_system_layer-dc95a43da475b8ce55f25b670c9848d9dabf1cb7.zip
Update graph example
-rwxr-xr-xexamples/graph.c48
1 files changed, 32 insertions, 16 deletions
diff --git a/examples/graph.c b/examples/graph.c
index a224a5b..edbb5b3 100755
--- a/examples/graph.c
+++ b/examples/graph.c
@@ -40,6 +40,8 @@ typedef struct {
b8 hover;
b8 highlight;
f64 distance;
+ f64 drag_x;
+ f64 drag_y;
} Node;
typedef struct {
@@ -301,8 +303,11 @@ void highlight_path(i64 src, i64 dst) {
world.nodes[min_index].distance = min_distance;
- // Repeat until there's no connected nodes left
+ // Repeat until we got the destination node
//
+
+ if (min_index == dst)
+ break;
}
// If path not found, stop
@@ -446,8 +451,12 @@ void update_and_render_frame(void) {
if (world.nodes[i].enabled && world.nodes[i].hover) {
drag_on = 1;
drag_node = i;
- drag_x0 = world.nodes[i].x - platform.cursor_x;
- drag_y0 = world.nodes[i].y - platform.cursor_y;
+ drag_x0 = platform.cursor_x;
+ drag_y0 = platform.cursor_y;
+ for (i64 j = 0; j < MAX_NUM_NODES; ++j) {
+ world.nodes[j].drag_x = world.nodes[j].x;
+ world.nodes[j].drag_y = world.nodes[j].y;
+ }
break;
}
@@ -479,25 +488,32 @@ void update_and_render_frame(void) {
if (drag_on) {
if (check_node_index(drag_node)) {
- f64 r = world.nodes[drag_node].radius;
- f64 x = drag_x0 + platform.cursor_x;
- f64 y = drag_y0 + platform.cursor_y;
+ f64 dx = platform.cursor_x - drag_x0;
+ f64 dy = platform.cursor_y - drag_y0;
- b8 collision = 0;
+ world.nodes[drag_node].x = world.nodes[drag_node].drag_x + dx;
+ world.nodes[drag_node].y = world.nodes[drag_node].drag_y + dy;
for (i64 i = 0; i < MAX_NUM_NODES; ++i) {
- Node n = world.nodes[i];
- if (i != drag_node && n.enabled && distance_between(n.x, n.y, x, y) < n.radius + r) {
- collision = 1;
- break;
+ if (i == drag_node) continue;
+
+ b8 is_neighbor = 0;
+
+ for (i64 k = 0; k < MAX_NUM_EDGES; ++k) {
+ Edge e = world.edges[k];
+ if ((e.src == drag_node && e.dst == i) || (e.src == i && e.dst == drag_node)) {
+ is_neighbor = 1;
+ break;
+ }
}
- }
- if (!collision) {
- world.nodes[drag_node].x = x;
- world.nodes[drag_node].y = y;
- path_changed = 1;
+ if (is_neighbor) {
+ world.nodes[i].x = world.nodes[i].drag_x + dx * .3;
+ world.nodes[i].y = world.nodes[i].drag_y + dy * .3;
+ }
}
+
+ path_changed = 1;
}
if (!platform.key_down[BUTTON_LEFT])