const std = @import("std"); const rl = @import("raylib"); const c = @import("../constants.zig"); 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, }; } pub fn deinit(self: *NodeManager, allocator: std.mem.Allocator) void { self.clear(allocator); self.nodes.deinit(allocator); } 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, .pink); rl.drawCircleV(node.pos, c.NODE_RADIUS, .brown); } const pos = rl.getMousePosition(); if (self.temp_node) |node| { rl.drawLineEx(node.pos, pos, c.ROAD_SIZE, .black); rl.drawCircleV(node.pos, c.NODE_RADIUS, .brown); rl.drawCircleV(pos, c.NODE_RADIUS, .blue); } } /// Adds a new node to the list, and if the temp_node already has a value it returns the pointer to the 2nd one pub fn add(self: *NodeManager, pos: rl.Vector2) ?*Node { const pos_node = self.getSelectedNode(pos); if (self.temp_node != null) return pos_node; self.temp_node = pos_node; return null; } pub fn getNodeWithinRadius(self: *const NodeManager, pos: rl.Vector2) ?*Node { for (self.nodes.items) |*node| { if (!node.withinSnappingRadius(pos)) continue; return node; } return null; } /// Returns the pointer to the node that is in the snapping node radius of the pos /// or creates a new node and returns pointer to it fn getSelectedNode(self: *NodeManager, pos: rl.Vector2) *Node { if (self.getNodeWithinRadius(pos)) |node| return node; // We couldn't find the existing node and as such must create a new one const node: Node = .init(self.getNewID(), pos); self.nodes.appendBounded(node) catch |err| { std.debug.panic("This is because preallocated size got exceeded, TODO fix: {}\n", .{err}); }; return self.getLastRef().?; } /// Returns the reference to the last element in the list; returns null if there are no elements in the list fn getLastRef(self: *const NodeManager) ?*Node { const nlen = self.nodes.items.len; return if (nlen == 0) null else &self.nodes.items[nlen - 1]; } /// generates finds the last element in the list and returns id + 1 or 0 if there are no elements in the list fn getNewID(self: *const NodeManager) usize { return if (self.getLastRef()) |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 remove(self: *NodeManager, allocator: std.mem.Allocator, 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; for (self.nodes.items, 0..) |*node, i| { if (node.id != node_to_remove.id) continue; node.deinit(allocator); _ = self.nodes.swapRemove(i); return; } return error.NodeNotExist; } };