Created
January 31, 2025 04:58
-
-
Save kanzwataru/297bb6edbd03aa87f32b560f3f7f63c3 to your computer and use it in GitHub Desktop.
Very small <100LOC Raylib program to visualize a node tree
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <stdbool.h> | |
| #include <raylib.h> | |
| struct Node { | |
| const char *name; | |
| int prev; | |
| Vector2 ui_pos; | |
| }; | |
| typedef struct Node Node; | |
| int main(void) | |
| { | |
| InitWindow(1280, 720, "Raylib: Visualize Nodes"); | |
| SetTargetFPS(60); | |
| Node nodes[256] = {0}; | |
| int nodes_top = 1; | |
| nodes[nodes_top++] = (Node) { | |
| .name = "foo", | |
| .prev = 0, | |
| .ui_pos = { 200, 200 } | |
| }; | |
| nodes[nodes_top++] = (Node) { | |
| .name = "bar", | |
| .prev = 1, | |
| .ui_pos = { 500, 200 } | |
| }; | |
| nodes[nodes_top++] = (Node) { | |
| .name = "baz", | |
| .prev = 1, | |
| .ui_pos = { 500, 500 } | |
| }; | |
| int dragging_node = 0; | |
| const int node_width = 200; | |
| const int node_height = 200; | |
| while(!WindowShouldClose()) { | |
| BeginDrawing(); | |
| ClearBackground(RAYWHITE); | |
| for(int i = 1; i < nodes_top; ++i) { | |
| Node *n = &nodes[i]; | |
| if(!dragging_node && IsMouseButtonDown(0)) { | |
| Vector2 pos = GetMousePosition(); | |
| if(CheckCollisionPointRec(pos, (Rectangle){ n->ui_pos.x, n->ui_pos.y, node_width, node_height })) { | |
| dragging_node = i; | |
| } | |
| } | |
| if(dragging_node == i) { | |
| Vector2 mouse_delta = GetMouseDelta(); | |
| n->ui_pos.x += mouse_delta.x; | |
| n->ui_pos.y += mouse_delta.y; | |
| if(IsMouseButtonUp(0)) { | |
| dragging_node = false; | |
| } | |
| } | |
| DrawRectangle(n->ui_pos.x, n->ui_pos.y, node_width, node_height, SKYBLUE); | |
| if(n->name) { | |
| DrawText(n->name, n->ui_pos.x, n->ui_pos.y - 25, 20, DARKGRAY); | |
| } | |
| DrawCircle(n->ui_pos.x, n->ui_pos.y + 25, 14, RED); | |
| DrawCircle(n->ui_pos.x + node_width, n->ui_pos.y + 25, 14, RED); | |
| if(n->prev) { | |
| Node *prev = &nodes[n->prev]; | |
| Vector2 a = { prev->ui_pos.x + node_width, prev->ui_pos.y + 25 }; | |
| Vector2 b = { n->ui_pos.x, n->ui_pos.y + 25 }; | |
| DrawLineEx(a, b, 4.0f, DARKGRAY); | |
| } | |
| } | |
| EndDrawing(); | |
| } | |
| CloseWindow(); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment