Repository navigation
Expand file tree
/
Copy pathmain.c
More file actions
798 lines (689 loc) · 30.6 KB
/
Copy pathmain.c
File metadata and controls
798 lines (689 loc) · 30.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
/* List version of Graph Maker 4/5*/
#include "raylib.h"
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include "my_graph.h"
#define NUM_OF_COLORS 20
#define FAIL_EXIT(msg) do { fprintf(stderr, "ERROR: %s\n", msg); exit(EXIT_FAILURE); } while(0)
//------------------------------------------------------------------------------------
int GetDisplayWidth()
{
if (IsWindowFullscreen())
{
int monitor = GetCurrentMonitor();
return GetMonitorWidth(monitor);
}
else{
return GetScreenWidth();
}
}
int GetDisplayHeight()
{
if (IsWindowFullscreen())
{
int monitor = GetCurrentMonitor();
return GetMonitorHeight(monitor);
}
else{
return GetScreenHeight();
}
}
void ToggleFullScreenWindow(int windowWidth, int windowHeight)
{
if (!IsWindowFullscreen())
{
int monitor = GetCurrentMonitor();
SetWindowSize(GetDisplayWidth(), GetDisplayHeight());
ToggleFullscreen();
}
else{
ToggleFullscreen();
SetWindowSize(windowWidth, windowHeight);
}
}
void cleanup_resources(struct list_item **node_list, struct list_item **line_list) {
// Free all nodes
struct list_item *current = *node_list;
struct list_item *next;
while (current != NULL) {
next = current->next;
free(current->data); // Free the node data
free(current); // Free the list item
current = next;
}
*node_list = NULL;
// Free all lines
current = *line_list;
while (current != NULL) {
next = current->next;
free(current->data); // Free the line data
free(current); // Free the list item
current = next;
}
*line_list = NULL;
}
// New error handling function
void handle_error(const char* msg, struct list_item **node_list, struct list_item **line_list) {
fprintf(stderr, "ERROR: %s\n", msg);
cleanup_resources(node_list, line_list);
CloseWindow();
exit(EXIT_FAILURE);
}
// Global pointers for signal handler
struct list_item *g_node_list = NULL;
struct list_item *g_line_list = NULL;
// Signal handler for cleanup on unexpected termination
void signal_handler(int signal) {
fprintf(stderr, "Caught signal %d, cleaning up resources...\n", signal);
cleanup_resources(&g_node_list, &g_line_list);
CloseWindow();
exit(EXIT_FAILURE);
}
int main(void)
{
// Initialization
//--------------------------------------------------------------------------------------
const int screenWidth = 2560;
const int screenHeight = 1440;
SetTraceLogCallback(CustomLog);
InitWindow(screenWidth, screenHeight, "Graph Maker");
Vector2 mousePosition = { -100.0f, -100.0f };
struct list_item *line_list = NULL;
struct list_item *node_list = NULL;
// Set up global pointers for signal handler
g_node_list = node_list;
g_line_list = line_list;
// Set up signal handlers
signal(SIGINT, signal_handler);
signal(SIGTERM, signal_handler);
signal(SIGSEGV, signal_handler);
struct node *selectedNode = NULL;
struct graph cyclic_graph;
struct graph complete_graph;
int nodeCount = 0;
int nodeDiameter = 10;
bool isNodeDrawn = false;
bool isNodeSelected = false;
bool isNodeLocked = false;
Color nodeColors[NUM_OF_COLORS] = {DARKBLUE, RED, DARKGREEN, ORANGE, PINK,
PURPLE, BLACK, BROWN, GOLD, MAROON,
YELLOW, GRAY, MAGENTA, LIME, SKYBLUE,
VIOLET, BEIGE, DARKBROWN, GREEN, LIGHTGRAY};
int colorIndex = 0;
float selectionDiameter = nodeDiameter / 1.01;
bool isLineStarted = false;
bool isLineEnded = false;
bool createLine = false;
bool endLine = false;
int key_pressed = -1;
bool isCPressed = false;
float cPressedFrame = 0.0;
bool isKPressed = false;
float kPressedFrame = 0.0;
bool isSelecting = false;
bool isMultiSelect = false;
Vector2 startAreaPosition = {0};
Vector2 endAreaPosition = {0};
Rectangle selectionRect = {0};
Color backgroundColor = RAYWHITE;
// Used for multi-select dragging
static bool firstDragFrame = true;
static Vector2 initialMousePosForDrag = {0}; // Shared mouse position for multi-select drag
// Used to prevent re-selection after mouse release
static bool preventSelectionCooldown = false;
static int cooldownFrames = 0;
static int COOLDOWN_DURATION = 5; // Increased cooldown frames
// Used to track whether a node was just created
static bool nodeJustCreated = false;
SetTargetFPS(60);
//---------------------------------------------------------------------------------------
// Main game loop
while (!WindowShouldClose()) // Detect window close button or ESC key
{
// Update
//----------------------------------------------------------------------------------
mousePosition = GetMousePosition();
// Handle cooldown after mouse release
if (preventSelectionCooldown) {
cooldownFrames++;
if (cooldownFrames > COOLDOWN_DURATION) { // Longer cooldown
preventSelectionCooldown = false;
cooldownFrames = 0;
printf("Cooldown ended, selection enabled again\n");
}
}
/* Area Selection */
if (IsMouseButtonDown(MOUSE_BUTTON_MIDDLE)) {
if (!isSelecting) {
startAreaPosition = mousePosition;
isSelecting = true;
// Clear any existing selection when starting a new selection area
if (isMultiSelect) {
struct list_item *currentNode = NULL;
for (currentNode = node_list; currentNode != NULL; currentNode = currentNode->next) {
struct node *currentNodeData = (struct node *)currentNode->data;
currentNodeData->selected = false;
}
}
}
endAreaPosition = mousePosition;
float width = endAreaPosition.x - startAreaPosition.x;
float height = endAreaPosition.y - startAreaPosition.y;
if (width < 0) {
selectionRect.x = endAreaPosition.x;
selectionRect.width = -width;
} else {
selectionRect.x = startAreaPosition.x;
selectionRect.width = width;
}
if (height < 0) {
selectionRect.y = endAreaPosition.y;
selectionRect.height = -height;
} else {
selectionRect.y = startAreaPosition.y;
selectionRect.height = height;
}
} else if (isSelecting) {
isSelecting = false;
isMultiSelect = true;
// Select all nodes in the area
struct list_item *currentNode = NULL;
int nodesSelected = 0; // Track how many nodes are selected
for (currentNode = node_list; currentNode != NULL; currentNode = currentNode->next) {
struct node *currentNodeData = (struct node *)currentNode->data;
if ((currentNodeData->position.x > selectionRect.x) &&
(currentNodeData->position.x < (selectionRect.x + selectionRect.width)) &&
(currentNodeData->position.y > selectionRect.y) &&
(currentNodeData->position.y < (selectionRect.y + selectionRect.height))) {
currentNodeData->selected = true;
nodesSelected++;
// Save original position of each selected node
currentNodeData->originalPosition = currentNodeData->position;
} else if (isMultiSelect) {
// Only deselect nodes if we're not extending a selection with a modifier key
currentNodeData->selected = false;
}
}
// If no nodes were selected, exit multi-select mode
if (nodesSelected == 0) {
isMultiSelect = false;
}
printf("Multi-select activated with %d nodes selected\n", nodesSelected);
}
// Exit multi-select mode when clicking in empty space
else if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT) && !isLineStarted && isMultiSelect && !preventSelectionCooldown) {
// Check if clicking on any node
bool clickedOnNode = false;
struct list_item *currentNode = NULL;
for (currentNode = node_list; currentNode != NULL; currentNode = currentNode->next) {
struct node *currentNodeData = (struct node *)currentNode->data;
if (CheckCollisionPointCircle(mousePosition, currentNodeData->position, selectionDiameter)) {
clickedOnNode = true;
// Store the clicked node for movement but DO NOT deselect other nodes
selectedNode = currentNodeData;
// Make sure this node is also selected
currentNodeData->selected = true;
printf("Clicked on node %d in multi-select mode\n", currentNodeData->id);
break;
}
}
// If clicked outside any node, exit multi-select mode
if (!clickedOnNode) {
printf("Clicked in empty space, exiting multi-select mode\n");
isMultiSelect = false;
// Deselect all nodes
for (currentNode = node_list; currentNode != NULL; currentNode = currentNode->next) {
struct node *currentNodeData = (struct node *)currentNode->data;
currentNodeData->selected = false;
}
}
}
// Draw node with left click - but not during cooldown period
else if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT) && !isLineStarted) {
// Only set isNodeDrawn to true if we're not over an existing node
bool isOverExistingNode = false;
struct list_item *checkNode = NULL;
for (checkNode = node_list; checkNode != NULL; checkNode = checkNode->next) {
if (CheckCollisionPointCircle(mousePosition,
((struct node *)checkNode->data)->position,
selectionDiameter)) {
isOverExistingNode = true;
break;
}
}
if (!isOverExistingNode) {
printf("Setting isNodeDrawn=true for mouse at (%f, %f)\n", mousePosition.x, mousePosition.y);
isNodeDrawn = true;
}
}
// Store original positions when starting a drag operation
if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT) && isMultiSelect) {
printf("Starting multi-select drag operation\n");
// Store original positions of all selected nodes
struct list_item *currentNode = NULL;
for (currentNode = node_list; currentNode != NULL; currentNode = currentNode->next) {
struct node *currentNodeData = (struct node *)currentNode->data;
if (currentNodeData->selected) {
currentNodeData->originalPosition = currentNodeData->position;
}
}
// Store starting mouse position
startAreaPosition = mousePosition;
initialMousePosForDrag = mousePosition;
firstDragFrame = true;
// Clear the selection rectangle so movement isn't constrained
selectionRect.x = 0;
selectionRect.y = 0;
selectionRect.width = 0;
selectionRect.height = 0;
}
// Move node with mouse left button - but not during cooldown
else if (IsMouseButtonDown(MOUSE_BUTTON_LEFT) && !preventSelectionCooldown) {
isNodeSelected = true;
}
else if (IsMouseButtonReleased(MOUSE_BUTTON_LEFT)) {
printf("Mouse released, resetting all selection variables\n");
isNodeSelected = false;
isNodeLocked = false;
isNodeDrawn = false;
nodeJustCreated = false;
// Reset the drag tracking variable
firstDragFrame = true;
// Enable a longer cooldown to prevent immediate re-selection
preventSelectionCooldown = true;
cooldownFrames = 0;
// Exit multi-select mode immediately when releasing mouse
if (isMultiSelect) {
printf("Mouse released, exiting multi-select mode\n");
isMultiSelect = false;
selectedNode = NULL;
// Deselect all nodes
struct list_item *currentNode = NULL;
for (currentNode = node_list; currentNode != NULL; currentNode = currentNode->next) {
struct node *currentNodeData = (struct node *)currentNode->data;
currentNodeData->selected = false;
currentNodeData->locked = false;
}
} else {
// If not in multi-select mode, deselect all nodes as usual
struct list_item *currentNode = NULL;
for (currentNode = node_list; currentNode != NULL; currentNode = currentNode->next) {
struct node *currentNodeData = (struct node *)currentNode->data;
currentNodeData->selected = false;
currentNodeData->locked = false;
}
}
}
// Make line with right click
else if (IsMouseButtonPressed(MOUSE_BUTTON_RIGHT)) {
if (!isLineStarted && !isLineEnded) {
isLineStarted = true;
isLineEnded = false;
createLine = true;
} else {
isLineStarted = false;
isLineEnded = true;
endLine = true;
}
/* Make Cyclic Graph */
} else if (IsKeyPressed(KEY_C)) {
isCPressed = true;
cPressedFrame = GetFrameTime();
} else if (isCPressed) {
isNodeDrawn = false;
key_pressed = GetKeyPressed();
key_pressed -= 48;
if (key_pressed >= 3 && key_pressed <= 9) {
cyclic_graph = cyclic_graph_create(key_pressed,
&nodeCount,
nodeColors,
nodeDiameter,
mousePosition);
if (cyclic_graph.nodes == NULL) {
fprintf(stderr, "Warning: Failed to create cyclic graph\n");
} else {
bool update_success = update_lists(&cyclic_graph, &node_list, &line_list);
if (!update_success) {
fprintf(stderr, "Warning: Failed to update lists with cyclic graph\n");
handle_error("Failed to update lists with cyclic graph", &node_list, &line_list);
}
}
isCPressed = false;
}
/* Make Complete Graph */
} else if (IsKeyPressed(KEY_K)) {
isKPressed = true;
kPressedFrame = GetFrameTime();
} else if (isKPressed) {
isNodeDrawn = false;
key_pressed = GetKeyPressed();
key_pressed -= 48;
if (key_pressed >= 2 && key_pressed <= 9) {
complete_graph = complete_graph_create(key_pressed,
&nodeCount,
nodeColors,
nodeDiameter,
mousePosition);
if (complete_graph.nodes == NULL) {
fprintf(stderr, "Warning: Failed to create complete graph\n");
} else {
bool update_success = update_lists(&complete_graph, &node_list, &line_list);
if (!update_success) {
fprintf(stderr, "Warning: Failed to update lists with complete graph\n");
handle_error("Failed to update lists with complete graph", &node_list, &line_list);
}
}
isKPressed = false;
}
} else if (key_pressed != KEY_C || key_pressed != KEY_K) {
isCPressed = false;
isKPressed = false;
}
/* Toggle full screen */
if (IsKeyPressed(KEY_SPACE)) {
ToggleFullScreenWindow(screenWidth, screenHeight);
}
/* Check node collisions */
bool isSpaceFree = true;
bool isMouseOverNode = false;
struct list_item *currentNode = NULL;
struct list_item *currentNode2 = NULL;
// Skip node collision checks if:
// 1. In multi-select mode
// 2. During cooldown period
// 3. Just created a node
if (!isMultiSelect && !preventSelectionCooldown && !nodeJustCreated) {
for (currentNode = node_list; currentNode != NULL; currentNode = currentNode->next) {
struct node *currentNodeData = (struct node *)currentNode->data;
isMouseOverNode = CheckCollisionPointCircle(
mousePosition,
(currentNodeData)->position,
selectionDiameter);
// Only handle node selection if we're not in multi-select mode
if (isMouseOverNode && !(isLineStarted || isLineEnded) && !isNodeLocked) {
/* Then create node */
isSpaceFree = false;
isNodeDrawn = false; // Important - prevent node creation when over existing node
selectedNode = currentNode->data;
// When not in multi-select mode, clicking a node selects only that node
struct list_item *tempNode = NULL;
for (tempNode = node_list; tempNode != NULL; tempNode = tempNode->next) {
((struct node *)tempNode->data)->selected = false;
}
// Select the clicked node
currentNodeData->selected = true;
printf("Selected node %d at (%f, %f)\n", currentNodeData->id,
currentNodeData->position.x, currentNodeData->position.y);
}
else if (isMouseOverNode && isLineStarted && !isNodeLocked) {
/* Then end line */
isSpaceFree = false;
/* currentNodeData->locked = true; */
isNodeLocked = true;
selectedNode = currentNode->data;
/* currentNodeData->selected = true; */
} else if (isMouseOverNode && isLineEnded && isNodeLocked) {
/* Complete line */
selectedNode = currentNode->data;
/* currentNodeData->selected = true; */
}
/* Area selection handled separately */
for (currentNode2 = node_list; currentNode2 != NULL; currentNode2 = currentNode2->next) {
if (!CheckCollisionCircles(currentNodeData->position,
nodeDiameter / 2.0,
((struct node *)currentNode2->data)->position,
nodeDiameter / 2.0)) {
if (isMouseOverNode && isNodeSelected && !isNodeLocked) {
/* Lock node so when moving, selection is kept. */
selectedNode = currentNode->data;
/* currentNodeData->selected = true; */
/* currentNodeData->locked = true; */
isNodeLocked = true;
break;
}
}
}
}
}
/* Create Node */
if (isNodeDrawn && isSpaceFree) {
if (colorIndex >= NUM_OF_COLORS)
colorIndex = 0;
// Ensure we're creating a new node, not moving an existing one
printf("Creating new node at (%f, %f), selectedNode=%p\n", mousePosition.x, mousePosition.y, selectedNode);
// CRITICAL: Set selectedNode to NULL before creating a new node
selectedNode = NULL;
nodeJustCreated = true;
// Reset node selection flags to prevent segfault
isNodeSelected = false;
isNodeLocked = false;
struct node *new_node = node_create(nodeCount, colorIndex,
nodeColors, nodeDiameter,
mousePosition);
if (new_node == NULL) {
handle_error("Failed to create new node", &node_list, &line_list);
}
struct list_item *new_list = add_to_list(node_list, new_node);
if (new_list == NULL) {
free(new_node); // Clean up allocated node if list addition fails
handle_error("Failed to add node to list", &node_list, &line_list);
}
node_list = new_list;
// Update global pointer for signal handler
g_node_list = node_list;
colorIndex++;
nodeCount++;
isNodeDrawn = false;
}
// LINES---------------------
struct node start_node;
if (createLine) {
if (selectedNode == NULL) {
fprintf(stderr, "Warning: Trying to create line with NULL node\n");
createLine = false;
} else {
struct line *new_line = start_line(selectedNode);
if (new_line == NULL) {
handle_error("Failed to create new line", &node_list, &line_list);
}
struct list_item *new_list = add_to_list(line_list, new_line);
if (new_list == NULL) {
free(new_line); // Clean up allocated line if list addition fails
handle_error("Failed to add line to list", &node_list, &line_list);
}
line_list = new_list;
// Update global pointer for signal handler
g_line_list = line_list;
createLine = false;
}
}
struct node end_node;
if (isLineEnded) {
if (selectedNode == NULL || line_list == NULL) {
fprintf(stderr, "Warning: Cannot end line with NULL node or empty line list\n");
} else {
end_line(line_list, selectedNode);
}
isLineEnded = false;
isNodeLocked = false;
}
//Update line positions when moving node
struct list_item *line_head = NULL;
if (isNodeSelected && selectedNode != NULL) {
for (line_head = line_list; line_head != NULL; line_head = line_head->next) {
if (selectedNode->id == ((struct line *)line_head->data)->startNode.id)
((struct line *)line_head->data)->startNode.position = selectedNode->position;
if (selectedNode->id == ((struct line *)line_head->data)->endNode.id)
((struct line *)line_head->data)->endNode.position = selectedNode->position;
}
}
//----------------------------------------------------------------------------------
// Draw
//----------------------------------------------------------------------------------
BeginDrawing();
ClearBackground(backgroundColor);
// For multi-select, calculate movement constraints to maintain shape
if (isMultiSelect && isNodeSelected) {
// If it's the first frame of dragging, initialize was already done when mouse was pressed
if (firstDragFrame) {
firstDragFrame = false;
printf("First frame of multi-select drag, using saved positions\n");
}
float deltaX = mousePosition.x - initialMousePosForDrag.x;
float deltaY = mousePosition.y - initialMousePosForDrag.y;
// Find leftmost, rightmost, topmost, and bottommost nodes in selection
float leftmostX = screenWidth;
float rightmostX = 0;
float topmostY = screenHeight;
float bottommostY = 0;
// First, calculate the bounds of the selected nodes group
struct list_item *boundNode = NULL;
for (boundNode = node_list; boundNode != NULL; boundNode = boundNode->next) {
struct node *nodeData = (struct node *)boundNode->data;
if (nodeData->selected) {
// Calculate where this node would be after applying the movement delta
float potentialX = nodeData->originalPosition.x + deltaX;
float potentialY = nodeData->originalPosition.y + deltaY;
// Update the extremes
if (potentialX < leftmostX) leftmostX = potentialX;
if (potentialX > rightmostX) rightmostX = potentialX;
if (potentialY < topmostY) topmostY = potentialY;
if (potentialY > bottommostY) bottommostY = potentialY;
}
}
// Calculate adjustments needed to keep all nodes within screen
float adjustX = 0;
float adjustY = 0;
// Check if any node would go out of bounds and calculate adjustment
if (leftmostX < 0) adjustX = -leftmostX;
if (rightmostX > screenWidth) adjustX = screenWidth - rightmostX;
if (topmostY < 0) adjustY = -topmostY;
if (bottommostY > screenHeight) adjustY = screenHeight - bottommostY;
// Adjust the mouse position to apply the corrected movement
// This effectively maintains the shape by moving all nodes together
mousePosition.x += adjustX;
mousePosition.y += adjustY;
// Debug output
if (adjustX != 0 || adjustY != 0) {
printf("Group boundary hit: adjusting by (%f, %f)\n", adjustX, adjustY);
}
}
// Draw Nodes
struct list_item *currentNode3 = NULL;
for (currentNode3 = node_list; currentNode3 != NULL; currentNode3 = currentNode3->next)
{
struct node *currentNode3Data = (struct node *)currentNode3->data;
/* Individual selected node position */
if (isNodeSelected && !(isLineStarted || isLineEnded) && !nodeJustCreated)
if (selectedNode != NULL) {
// Make sure this node is actually selected (additional safety check)
if (!selectedNode->selected) {
printf("WARNING: selectedNode %d is not marked as selected!\n", selectedNode->id);
selectedNode->selected = true;
}
printf("Moving node %d to (%f, %f)\n", selectedNode->id, mousePosition.x, mousePosition.y);
if (mousePosition.x > screenWidth) {
selectedNode->position.x = screenWidth;
} else if (mousePosition.x < 0) {
selectedNode->position.x = 0;
} else {
selectedNode->position.x = mousePosition.x;
}
if (mousePosition.y > screenHeight) {
selectedNode->position.y = screenHeight;
} else if (mousePosition.y < 0) {
selectedNode->position.y = 0;
} else {
selectedNode->position.y = mousePosition.y;
}
} else {
printf("WARNING: Trying to move node but selectedNode is NULL!\n");
}
/* Multi-select move - only executed for nodes that are selected */
if (isMultiSelect && currentNode3Data->selected && isNodeSelected) {
// Calculate the deltas - these will already be adjusted to maintain the shape
float deltaX = mousePosition.x - initialMousePosForDrag.x;
float deltaY = mousePosition.y - initialMousePosForDrag.y;
// Apply the delta to the original position of each node
float newX = currentNode3Data->originalPosition.x + deltaX;
float newY = currentNode3Data->originalPosition.y + deltaY;
// The boundary checks are done separately - in a first pass before this loop
// We'll just use the adjusted deltas here
currentNode3Data->position.x = newX;
currentNode3Data->position.y = newY;
// Update lines connected to this node
struct list_item *line_head = NULL;
for (line_head = line_list; line_head != NULL; line_head = line_head->next) {
if (currentNode3Data->id == ((struct line *)line_head->data)->startNode.id) {
((struct line *)line_head->data)->startNode.position = currentNode3Data->position;
}
if (currentNode3Data->id == ((struct line *)line_head->data)->endNode.id) {
((struct line *)line_head->data)->endNode.position = currentNode3Data->position;
}
}
}
DrawCircleV(((struct node *)currentNode3->data)->position,
((struct node *)currentNode3->data)->diameter,
((struct node *)currentNode3->data)->color);
DrawText(TextFormat("%d", ((struct node *)currentNode3->data)->id + 1),
((struct node *)currentNode3->data)->position.x - 20,
((struct node *)currentNode3->data)->position.y - 15, 8, BLACK);
/* Multi-select draw selection circles*/
if (isMultiSelect && currentNode3Data->selected) {
DrawCircleLines(currentNode3Data->position.x,
currentNode3Data->position.y,
currentNode3Data->diameter + (currentNode3Data->diameter / 2.0),
currentNode3Data->color);
} else if (!isMultiSelect && currentNode3Data->selected) {
currentNode3Data->selected = false;
}
}
//Draw outline around selected node
if (isNodeSelected && selectedNode != NULL)
DrawCircleLines(selectedNode->position.x,
selectedNode->position.y,
selectedNode->diameter + (selectedNode->diameter / 2.0),
selectedNode->color);
//Draw line before it is connected to end node
if (isLineStarted) {
DrawLine(((struct line *)line_list->data)->startNode.position.x,
((struct line *)line_list->data)->startNode.position.y,
mousePosition.x, mousePosition.y, BLACK);
}
// draw lines
if (line_list != NULL) {
struct list_item *line_head2 = NULL;
for (line_head2 = line_list; line_head2 != NULL; line_head2 = line_head2->next) {
DrawLine(((struct line *)line_head2->data)->startNode.position.x,
((struct line *)line_head2->data)->startNode.position.y,
((struct line *)line_head2->data)->endNode.position.x,
((struct line *)line_head2->data)->endNode.position.y,
BLACK);
}
}
if (isSelecting)
{
DrawRectangleLinesEx(selectionRect, 2, BLUE);
}
else
{
/* selectionRect.x = 0; */
/* selectionRect.y = 0; */
/* selectionRect.width = 0; */
/* selectionRect.height = 0; */
}
/* if (line_list != NULL) */
/* DrawText(TextFormat("End node x pos: %f\nEnd node y pos: %f", ((struct line *)line_list->data)->endNode.position.x, ((struct line *)line_list->data)->endNode.position.y), 10, 10, 20, DARKGRAY); */
/* DrawText(TextFormat("isLineStarted: %d", isLineStarted), 10, 50, 20, DARKGRAY); */
EndDrawing();
//----------------------------------------------------------------------------------
}
// De-Initialization
//--------------------------------------------------------------------------------------
cleanup_resources(&node_list, &line_list);
CloseWindow(); // Close window and OpenGL context
//--------------------------------------------------------------------------------------
return 0;
}