diff --git a/src/infrastructure/node_manager.zig b/src/infrastructure/node_manager.zig index 5147190..c5dac55 100644 --- a/src/infrastructure/node_manager.zig +++ b/src/infrastructure/node_manager.zig @@ -22,13 +22,13 @@ pub const NodeManager = struct { pub fn draw(self: *const NodeManager, display_details: bool) void { for (self.nodes.items) |node| { + if (display_details) rl.drawCircleV(node.pos, c.NODE_SNAP_RADIUS * c.NODE_RADIUS, .pink); rl.drawCircleV(node.pos, c.NODE_RADIUS, .brown); } const pos = rl.getMousePosition(); if (self.temp_node) |node| { - if (display_details) rl.drawCircleV(node.pos, c.NODE_SNAP_RADIUS * c.NODE_RADIUS, .pink); rl.drawLineEx(node.pos, pos, c.ROAD_SIZE, .black); rl.drawCircleV(node.pos, c.NODE_RADIUS, .brown); @@ -41,10 +41,7 @@ pub const NodeManager = struct { pub fn add(self: *NodeManager, pos: rl.Vector2) ?*Node { const pos_node = self.getSelectedNode(pos); - if (self.temp_node) |_| { - // TODO add road length check before creating the second node / returning - return pos_node; - } + if (self.temp_node != null) return pos_node; self.temp_node = pos_node; return null; @@ -83,9 +80,23 @@ pub const NodeManager = struct { return if (last_ref) |ref| ref.*.id + 1 else 0; } + /// Iterates through all nodes and runs the deinit procedure on each of them pub fn clear(self: *NodeManager, allocator: std.mem.Allocator) void { for (self.nodes.items) |*node| { node.deinit(allocator); } } + + /// Removes the node from the list with all appropriate checks + pub fn removeNode(self: *NodeManager, node_to_remove: *Node) void { + // In case the node has references to the existing roads we can not remove it + if (node_to_remove.roads.items.len > 0) return; + + for (self.nodes.items, 0..) |*node, i| { + if (node.id != node_to_remove.id) continue; + + _ = self.nodes.swapRemove(i); + return; + } + } }; \ No newline at end of file diff --git a/src/simulator.zig b/src/simulator.zig index a01779c..e7b2899 100644 --- a/src/simulator.zig +++ b/src/simulator.zig @@ -53,9 +53,18 @@ pub const Simulator = struct { /// every mouse event is checked here fn handleMouseInput(self: *Simulator) void { const pos = rl.getMousePosition(); - if (!rl.isMouseButtonReleased(.left)) return; + if (rl.isMouseButtonReleased(.left)) { + self.leftClickEvent(pos); + } + else if (rl.isMouseButtonReleased(.right)) self.rightClickEvent(); + + } + + fn leftClickEvent(self: *Simulator, pos: rl.Vector2) void { if (self.node_man.add(pos)) |node| { + if (self.node_man.temp_node.?.id == node.id) return; + self.road_man.add(self.allocator, self.node_man.temp_node.?, node) catch |err| { std.debug.panic("Error while attempting to add a road: {}\n", .{err}); }; @@ -64,6 +73,13 @@ pub const Simulator = struct { } } + fn rightClickEvent(self: *Simulator) void { + if (self.node_man.temp_node) |node| { + self.node_man.removeNode(node); + self.node_man.temp_node = null; + } + } + /// The main drawing function that is exposed upwards pub fn draw(self: *const Simulator) void { self.road_man.draw(); @@ -72,7 +88,8 @@ pub const Simulator = struct { rl.clearBackground(.light_gray); } - + + /// Draws UI elements like text fn drawUI(self: *const Simulator) void { var buf: [1024]u8 = undefined;