Basic Entity ID display for debugging

This commit is contained in:
2026-05-01 15:44:00 +02:00
parent 2a3064b0fe
commit 643712f529
7 changed files with 64 additions and 31 deletions

View File

@@ -24,3 +24,7 @@ pub const ROAD_SIZE = 20;
pub const ROAD_COLOUR = clr.black;
/// Colour of the road that is highlighted
pub const ROAD_HIGHLIGHTED_COLOUR = clr.green;
pub const TEXT_SIZE = 50;
pub const ENTITY_DATA_TEXT_SIZE = TEXT_SIZE / 2;
pub const ENTITY_DATA_TEXT_COLOUR = clr.orange;

View File

@@ -3,16 +3,13 @@ const Vector2 = @import("raylib").Vector2;
const st = @import("structures.zig");
const Node = @import("../infrastructure/node.zig").Node;
/// Returns distance between two nodes, with the caveat that it doesn't do square root in the end;
/// that's because we are interested in relative distance and don't care for the precise number as long as it is in
/// correct relation to others
///
/// This allows us to avoid the (square) rooting operation which is computationally expensive
/// Returns distance between two nodes, used mostly for road length and possibly for A* heuristics
pub fn calculate_length(start: Vector2, end: Vector2) f32 {
const x_diff = end.x - start.x;
const y_diff = end.y - start.y;
return x_diff * x_diff + y_diff * y_diff;
const squared_solution = x_diff * x_diff + y_diff * y_diff;
return @sqrt(squared_solution);
}
/// Comparator function that compares intersection proximity from the node in order to aid the sorting function
@@ -23,11 +20,10 @@ pub fn compareIntersections(ctx: *const Node, inter_a: st.IntersectionData, inte
return distance_a < distance_b;
}
// felt cute, might delete it later idk
pub fn listContains(comptime T: type, element: *T, list: *[]*T) bool {
for (0..list.len) |i| {
if (list[i] == element) return true;
}
return false;
}
/// Returns vector from P1 to P2
pub fn getVectorP1P2(p1: Vector2, p2: Vector2) Vector2 {
return .{
.x = p2.x - p1.x,
.y = p2.y - p1.y,
};
}

View File

@@ -35,10 +35,22 @@ pub const Node = struct {
}
/// Simple function which draws the node
pub fn draw(self: *const Node, direct_colour: ?rl.Color) void {
pub fn draw(self: *const Node, direct_colour: ?rl.Color, display_info: bool) void {
const colour = if (direct_colour) |clr| clr else c.NODE_COLOUR;
rl.drawCircleV(self.pos, c.NODE_RADIUS, colour);
if (!display_info) return;
var buf: [100]u8 = undefined;
const entity_info = std.fmt.bufPrintZ(&buf, "{d}", .{self.id}) catch |err| {
std.debug.panic("Failed to allocate space for ID???: {}\n", .{err});
};
const info_x = @as(i32, @trunc(self.pos.x)) - c.NODE_RADIUS / 2;
const info_y = @as(i32, @trunc(self.pos.y)) - c.NODE_RADIUS / 2;
rl.drawText(entity_info, info_x, info_y,
c.ENTITY_DATA_TEXT_SIZE, c.ENTITY_DATA_TEXT_COLOUR);
}
/// Determines whether the pos (location) is within the snapping radius of the node

View File

@@ -33,9 +33,9 @@ pub const NodeManager = struct {
}
/// Regular draw function
pub fn draw(self: *const NodeManager, pos: Vector2) void {
pub fn draw(self: *const NodeManager, pos: Vector2, display_info: bool) void {
for (self.nodes.items) |node| {
node.draw(null);
node.draw(null, display_info);
}
if (self.temp_node) |node| {
@@ -43,10 +43,10 @@ pub const NodeManager = struct {
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(false);
road.draw(false, false);
node.draw(c.NODE_TEMP_COLOUR);
cur_node.draw(c.NODE_CURSOR_COLOUR);
node.draw(c.NODE_TEMP_COLOUR, display_info);
cur_node.draw(c.NODE_CURSOR_COLOUR, false);
}
}

View File

@@ -46,9 +46,25 @@ pub const Road = struct {
///
/// In the future as we improve and make roads more complex with multiple lanes and such
/// it will gradually become more complex
pub fn draw(self: *const Road, highlighted: bool) void {
pub fn draw(self: *const Road, highlighted: bool, display_info: bool) void {
const colour = if (highlighted) c.ROAD_HIGHLIGHTED_COLOUR else c.ROAD_COLOUR;
rl.drawLineEx(self.nodes[0].pos, self.nodes[1].pos, c.ROAD_SIZE, colour);
if (!display_info) return;
var buf: [100]u8 = undefined;
const entity = std.fmt.bufPrintZ(&buf, "{d}", .{self.id}) catch |err| {
std.debug.panic("Could not allocate ID into string???: {}\n", .{err});
};
const distance = ut.getVectorP1P2(self.nodes[0].pos, self.nodes[1].pos);
const entity_info_pos: rl.Vector2 = .{
.x = self.nodes[0].pos.x + distance.x / 2 - c.ROAD_SIZE / 2,
.y = self.nodes[0].pos.y + distance.y / 2 - c.ROAD_SIZE / 2,
};
rl.drawText(entity, @trunc(entity_info_pos.x), @trunc(entity_info_pos.y),
c.ENTITY_DATA_TEXT_SIZE, c.ENTITY_DATA_TEXT_COLOUR);
}
/// Important: after this function executes, this road is no longer reachable from its bounding nodes

View File

@@ -27,10 +27,10 @@ pub const RoadManager = struct {
}
/// Draws all the roads in the list, sends the information ahead whether the road drawn should be highlighted
pub fn draw(self: *const RoadManager, highlighted_road: ?*Road) void {
pub fn draw(self: *const RoadManager, highlighted_road: ?*Road, display_info: bool) void {
for (self.roads.items) |road| {
const is_highlighted = if (highlighted_road) |h_road| road == h_road else false;
road.draw(is_highlighted);
road.draw(is_highlighted, display_info);
}
}

View File

@@ -29,6 +29,8 @@ pub const Simulator = struct {
///
/// Note: It only works outside of the delete mode
show_connections: bool,
/// Toggle that tracks whether ID (or possibly something more in the future) of every entity is displayed in GUI
display_entity_info: bool,
highlighted_entity: ?st.Entity,
/// Constructor for convenience
@@ -40,6 +42,7 @@ pub const Simulator = struct {
.auto_continue = false,
.delete_mode = false,
.show_connections = false,
.display_entity_info = false,
.highlighted_entity = null,
};
}
@@ -61,8 +64,8 @@ pub const Simulator = struct {
}
}
self.road_man.draw(highlighted_road);
self.node_man.draw(pos);
self.road_man.draw(highlighted_road, self.display_entity_info);
self.node_man.draw(pos, self.display_entity_info);
self.drawRelatedSelectedEntities();
}
@@ -76,18 +79,18 @@ pub const Simulator = struct {
const node = h_entity.node;
for (node.roads.items) |road| {
road.draw(true);
road.draw(true, self.display_entity_info);
}
node.draw(c.NODE_RELATED_COLOUR);
node.draw(c.NODE_RELATED_COLOUR, self.display_entity_info);
},
.road => {
const road = h_entity.road;
road.draw(true);
road.draw(true, self.display_entity_info);
road.nodes[0].draw(c.NODE_RELATED_COLOUR);
road.nodes[1].draw(c.NODE_RELATED_COLOUR);
road.nodes[0].draw(c.NODE_RELATED_COLOUR, self.display_entity_info);
road.nodes[1].draw(c.NODE_RELATED_COLOUR, self.display_entity_info);
},
}
}
@@ -109,6 +112,8 @@ pub const Simulator = struct {
self.delete_mode = rl.isKeyDown(.left_shift);
self.show_connections = rl.isKeyDown(.left_alt) and !self.delete_mode;
if (rl.isKeyReleased(.tab)) self.display_entity_info = !self.display_entity_info;
if (rl.isKeyReleased(.c)) self.clear() catch |err| {
std.debug.panic("Failed to clear the entities: {}\n", .{err});
};