Added left and right click events, fixed display details option/functionality, added function that safely removes the node and prevented addition of new roads if they are bounded by the same nodes (distance/length check)
This commit is contained in:
@@ -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;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user