From fea5a0ea5f77482cf19b3684b327f55d74ac1a17 Mon Sep 17 00:00:00 2001 From: Marto Date: Wed, 8 Apr 2026 11:22:19 +0200 Subject: [PATCH] Implemented a few debugging/inspecting tools --- src/infrastructure/node.zig | 6 --- src/infrastructure/node_manager.zig | 6 ++- src/infrastructure/road_manager.zig | 4 +- src/main.zig | 1 + src/simulator.zig | 60 +++++++++++++++++++++++++---- 5 files changed, 59 insertions(+), 18 deletions(-) diff --git a/src/infrastructure/node.zig b/src/infrastructure/node.zig index 42662a6..ce84139 100644 --- a/src/infrastructure/node.zig +++ b/src/infrastructure/node.zig @@ -43,13 +43,7 @@ pub const Node = struct { /// Returns bool whether the road passed is part of the roads list fn roadInList(self: *const Node, road_to_check: *const Road) ?usize { for (self.roads.items, 0..) |road, i| { - std.debug.print("Road id = {d}\n", .{road.id}); - std.debug.print("Road to check id = {d}\n", .{road_to_check.id}); - // TODO fix why doesn't 0 == 0 return i - std.debug.print("{}\n", .{road.id == road_to_check.id}); - const res = road.id == road_to_check.id; if (road.id == road_to_check.id) return i; - if (res) return i; } return null; diff --git a/src/infrastructure/node_manager.zig b/src/infrastructure/node_manager.zig index 8aaf309..b9769ca 100644 --- a/src/infrastructure/node_manager.zig +++ b/src/infrastructure/node_manager.zig @@ -7,11 +7,13 @@ const Node = @import("node.zig").Node; pub const NodeManager = struct { temp_node: ?*Node, nodes: std.ArrayList(Node), + highlighted_node: ?*Node, pub fn init(allocator: std.mem.Allocator) !NodeManager { return .{ .temp_node = null, .nodes = try .initCapacity(allocator, c.ALLOC_SIZE), + .highlighted_node = null, }; } @@ -91,7 +93,7 @@ pub const NodeManager = struct { } /// Removes the node from the list with all appropriate checks - pub fn removeNode(self: *NodeManager, node_to_remove: *Node) !void { + pub fn remove(self: *NodeManager, node_to_remove: *Node) !void { // In case the node has references to the existing roads we can not remove it // This also means we don't have to deinit it, since it has no elements if (node_to_remove.roads.items.len > 0) return; @@ -105,4 +107,6 @@ pub const NodeManager = struct { return error.NodeNotExist; } + + }; \ No newline at end of file diff --git a/src/infrastructure/road_manager.zig b/src/infrastructure/road_manager.zig index 7db2802..e3c8cd2 100644 --- a/src/infrastructure/road_manager.zig +++ b/src/infrastructure/road_manager.zig @@ -21,9 +21,7 @@ pub const RoadManager = struct { self.roads.deinit(allocator); } - pub fn draw(self: *RoadManager, display_highlighted_road: bool) void { - self.highlighted_road = self.getSelectedRoad(rl.getMousePosition()); - + pub fn draw(self: *const RoadManager, display_highlighted_road: bool) void { for (self.roads.items) |road| { var colour: rl.Color = .black; if (self.highlighted_road) |hr| { diff --git a/src/main.zig b/src/main.zig index c6ae73d..7d1a5dd 100644 --- a/src/main.zig +++ b/src/main.zig @@ -28,6 +28,7 @@ pub fn main() !void { rl.beginDrawing(); defer rl.endDrawing(); + sim.update(); sim.handleInput(); sim.draw(); diff --git a/src/simulator.zig b/src/simulator.zig index cbc4d79..e19a91a 100644 --- a/src/simulator.zig +++ b/src/simulator.zig @@ -66,6 +66,7 @@ pub const Simulator = struct { .b => .BUILD, else => self.mode, }; + if (self.mode != .BUILD) self.cancelBuildingRoad(); } /// every mouse event is checked here @@ -76,10 +77,7 @@ pub const Simulator = struct { self.leftClickEvent(pos); } else if (self.mode == .BUILD and rl.isMouseButtonReleased(.right)) { - if (self.node_man.temp_node) |node| { - // todo proper error handling - self.node_man.removeNode(node) catch {}; - } + self.cancelBuildingRoad(); } else if (self.mode == .DELETE and rl.isMouseButtonReleased(.left) and self.road_man.highlighted_road != null) { const nodes = self.road_man.highlighted_road.?.nodes; @@ -87,9 +85,12 @@ pub const Simulator = struct { std.debug.panic("Failed to remove the road: {}\n", .{err}); }; - // todo proper error handling - self.node_man.removeNode(nodes[0]) catch {}; - self.node_man.removeNode(nodes[1]) catch {}; + self.node_man.remove(nodes[0]) catch |err| { + std.debug.panic("Failed to remove the first node of the road to be deleted: {}\n", .{err}); + }; + self.node_man.remove(nodes[1]) catch |err| { + std.debug.panic("Failed to remove the second node of the road to be deleted: {}\n", .{err}); + }; } } @@ -111,8 +112,17 @@ pub const Simulator = struct { } } + fn cancelBuildingRoad(self: *Simulator) void { + if (self.node_man.temp_node == null) return; + + self.node_man.remove(self.node_man.temp_node.?) catch |err| { + std.debug.panic("Node doesn't exist: {}\n", .{err}); + }; + self.node_man.temp_node = null; + } + /// The main drawing function that is exposed upwards - pub fn draw(self: *Simulator) void { + pub fn draw(self: *const Simulator) void { self.road_man.draw(self.mode == .DELETE); self.node_man.draw(self.display_details); self.drawUI(); @@ -147,6 +157,13 @@ pub const Simulator = struct { // Displays the mode the simulation is currently in rl.drawText(@tagName(self.mode), 10, c.HEIGHT - c.TEXT_SIZE, c.TEXT_SIZE, .black); + + // displays id of the element we hover over over + const entityInfo = self.getHighlightedEntityInfo() catch |err| { + std.debug.panic("Failed to capture highlighted entity info: {}\n", .{err}); + }; + + rl.drawText(entityInfo, c.WIDTH - (c.TEXT_SIZE / 2) * @as(i32, @intCast(entityInfo.len)), c.TEXT_SIZE, c.TEXT_SIZE, .black); } /// Gets list of pointers of all roads that 'collide' with the road bounded by the nodes we pass into it @@ -170,4 +187,31 @@ pub const Simulator = struct { if (self.node_man.getNodeWithinRadius(intersection.point) == null) try self.debug_intersection_data.append(self.allocator, intersection); } } + + // TODO verify pointers + // TODO display road/node id + + fn verifyPointerIntegrity() void { + + } + + pub fn update(self: *Simulator) void { + const pos = rl.getMousePosition(); + + self.road_man.highlighted_road = self.road_man.getSelectedRoad(pos); + self.node_man.highlighted_node = self.node_man.getNodeWithinRadius(pos); + } + + /// Get ID info of the highlighted info and returns it + fn getHighlightedEntityInfo(self: *const Simulator) ![:0]u8 { + var buf: [10 * 1024]u8 = undefined; + + if (self.node_man.highlighted_node) |node| + return std.fmt.bufPrintZ(&buf, "Node ID: {d}\nRoad refs: {d}", .{node.id, node.roads.items.len}); + + if (self.road_man.highlighted_road) |road| + return std.fmt.bufPrintZ(&buf, "Road ID: {d}", .{road.id}); + + return std.fmt.bufPrintZ(&buf, "", .{}); + } }; \ No newline at end of file