Basic drawing

This commit is contained in:
2026-04-28 22:11:35 +02:00
parent edfc54b927
commit ccbf7344b5
6 changed files with 121 additions and 3 deletions

View File

@@ -6,28 +6,33 @@ const Node = @import("node.zig").Node;
const Road = @import("road.zig").Road;
pub const NodeManager = struct {
next_id: usize,
nodes: std.ArrayList(Node),
temp_node: ?*Node,
pub fn init() NodeManager {
return .{
.next_id = 0,
.nodes = .empty,
.temp_node = null,
};
}
pub fn deinit(self: *NodeManager, allocator: std.mem.Allocator) void {
for (self.nodes.items) |*node| {
node.deinit(allocator);
}
self.nodes.deinit(allocator);
}
pub fn draw(self: *const NodeManager, pos: Vector2) void {
for (self.nodes.items) |node| {
node.draw();
node.draw(null);
}
if (self.temp_node) |node| {
// Temporary node that points at the cursor
const cur_node = Node.init(0, pos);
var cur_node = Node.init(0, pos);
// Temporary road that is to be drawn as one in the making
const road: Road = .init(0, node, &cur_node);
road.draw();
@@ -36,4 +41,32 @@ pub const NodeManager = struct {
cur_node.draw(c.NODE_CURSOR_COLOUR);
}
}
pub fn getSelectedNode(self: *NodeManager, allocator: std.mem.Allocator, pos: Vector2) !*Node {
for (self.nodes.items) |*node| {
if (node.posWithinRadius(pos)) return node;
}
// No node is within that position, so we must create a new one
const node: Node = .init(self.getNextID(), pos);
try self.nodes.append(allocator, node);
return &self.nodes.items[self.nodes.items.len - 1];
}
fn getNextID(self: *NodeManager) usize {
const id = self.next_id;
self.next_id += 1;
return id;
}
pub fn clear(self: *NodeManager, allocator: std.mem.Allocator) void {
self.temp_node = null;
for (self.nodes.items) |*node| {
node.deinit(allocator);
}
self.nodes.clearRetainingCapacity();
self.next_id = 0;
}
};