Implemented relations displaying mode
This commit is contained in:
@@ -16,10 +16,11 @@ pub const NODE_COLOUR = clr.brown;
|
|||||||
pub const NODE_TEMP_COLOUR = clr.orange;
|
pub const NODE_TEMP_COLOUR = clr.orange;
|
||||||
/// The colour of the node being at the cursor
|
/// The colour of the node being at the cursor
|
||||||
pub const NODE_CURSOR_COLOUR = clr.blue;
|
pub const NODE_CURSOR_COLOUR = clr.blue;
|
||||||
|
pub const NODE_RELATED_COLOUR = clr.purple;
|
||||||
|
|
||||||
/// Road (line) size
|
/// Road (line) size
|
||||||
pub const ROAD_SIZE = 20;
|
pub const ROAD_SIZE = 20;
|
||||||
/// Regular road colour
|
/// Regular road colour
|
||||||
pub const ROAD_COLOUR = clr.black;
|
pub const ROAD_COLOUR = clr.black;
|
||||||
/// Colour of the road that is highlighted
|
/// Colour of the road that is highlighted
|
||||||
pub const ROAD_HIGHLIGHTED_COLOUR = clr.green;
|
pub const ROAD_HIGHLIGHTED_COLOUR = clr.green;
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ const rl = @import("raylib");
|
|||||||
|
|
||||||
const c = @import("../common/constants.zig");
|
const c = @import("../common/constants.zig");
|
||||||
const e = @import("../errors.zig");
|
const e = @import("../errors.zig");
|
||||||
|
const st = @import("../common/structures.zig");
|
||||||
const Road = @import("road.zig").Road;
|
const Road = @import("road.zig").Road;
|
||||||
|
|
||||||
pub const Node = struct {
|
pub const Node = struct {
|
||||||
|
|||||||
@@ -26,6 +26,7 @@ pub const NodeManager = struct {
|
|||||||
/// Deinitialises every node (pointer) within the list and then deinits the list containing the nodes itself
|
/// Deinitialises every node (pointer) within the list and then deinits the list containing the nodes itself
|
||||||
pub fn deinit(self: *NodeManager, allocator: std.mem.Allocator) !void {
|
pub fn deinit(self: *NodeManager, allocator: std.mem.Allocator) !void {
|
||||||
for (self.nodes.items) |node| {
|
for (self.nodes.items) |node| {
|
||||||
|
node.roads.clearRetainingCapacity();
|
||||||
try node.deinit(allocator);
|
try node.deinit(allocator);
|
||||||
}
|
}
|
||||||
self.nodes.deinit(allocator);
|
self.nodes.deinit(allocator);
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
const rl = @import("raylib");
|
const rl = @import("raylib");
|
||||||
|
|
||||||
const c = @import("../common/constants.zig");
|
const c = @import("../common/constants.zig");
|
||||||
|
const st = @import("../common/structures.zig");
|
||||||
const Node = @import("node.zig").Node;
|
const Node = @import("node.zig").Node;
|
||||||
|
|
||||||
pub const Road = struct {
|
pub const Road = struct {
|
||||||
|
|||||||
@@ -24,6 +24,8 @@ pub const Simulator = struct {
|
|||||||
///
|
///
|
||||||
/// For example, if I hover over a node it will highlight all roads that are connected to it;
|
/// For example, if I hover over a node it will highlight all roads that are connected to it;
|
||||||
/// Same goes for hovering over a road or in the future, a car (might show destination and path to it)
|
/// Same goes for hovering over a road or in the future, a car (might show destination and path to it)
|
||||||
|
///
|
||||||
|
/// Note: It only works outside of the delete mode
|
||||||
show_connections: bool,
|
show_connections: bool,
|
||||||
highlighted_entity: ?st.Entity,
|
highlighted_entity: ?st.Entity,
|
||||||
|
|
||||||
@@ -51,7 +53,6 @@ pub const Simulator = struct {
|
|||||||
rl.clearBackground(c.BACKGROUND_COLOR);
|
rl.clearBackground(c.BACKGROUND_COLOR);
|
||||||
|
|
||||||
var highlighted_road: ?*Road = null;
|
var highlighted_road: ?*Road = null;
|
||||||
|
|
||||||
if (self.delete_mode) {
|
if (self.delete_mode) {
|
||||||
if (self.highlighted_entity) |entity| {
|
if (self.highlighted_entity) |entity| {
|
||||||
if (entity == .road) highlighted_road = entity.road;
|
if (entity == .road) highlighted_road = entity.road;
|
||||||
@@ -60,6 +61,33 @@ pub const Simulator = struct {
|
|||||||
|
|
||||||
self.road_man.draw(highlighted_road);
|
self.road_man.draw(highlighted_road);
|
||||||
self.node_man.draw(pos);
|
self.node_man.draw(pos);
|
||||||
|
|
||||||
|
self.drawRelatedSelectedEntities();
|
||||||
|
}
|
||||||
|
|
||||||
|
fn drawRelatedSelectedEntities(self: *const Simulator) void {
|
||||||
|
if (!self.show_connections or self.highlighted_entity == null) return;
|
||||||
|
const h_entity = self.highlighted_entity.?;
|
||||||
|
|
||||||
|
switch (h_entity) {
|
||||||
|
.node => {
|
||||||
|
const node = h_entity.node;
|
||||||
|
|
||||||
|
for (node.roads.items) |road| {
|
||||||
|
road.draw(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
node.draw(c.NODE_RELATED_COLOUR);
|
||||||
|
},
|
||||||
|
.road => {
|
||||||
|
const road = h_entity.road;
|
||||||
|
|
||||||
|
road.draw(true);
|
||||||
|
|
||||||
|
road.nodes[0].draw(c.NODE_RELATED_COLOUR);
|
||||||
|
road.nodes[1].draw(c.NODE_RELATED_COLOUR);
|
||||||
|
},
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Update tick
|
/// Update tick
|
||||||
@@ -77,7 +105,7 @@ pub const Simulator = struct {
|
|||||||
fn handleKeyboardInput(self: *Simulator) void {
|
fn handleKeyboardInput(self: *Simulator) void {
|
||||||
self.auto_continue = rl.isKeyDown(.left_control);
|
self.auto_continue = rl.isKeyDown(.left_control);
|
||||||
self.delete_mode = rl.isKeyDown(.left_shift);
|
self.delete_mode = rl.isKeyDown(.left_shift);
|
||||||
self.show_connections = rl.isKeyDown(.left_alt);
|
self.show_connections = rl.isKeyDown(.left_alt) and !self.delete_mode;
|
||||||
|
|
||||||
if (rl.isKeyReleased(.c)) self.clear() catch |err| {
|
if (rl.isKeyReleased(.c)) self.clear() catch |err| {
|
||||||
std.debug.panic("Failed to clear the entities: {}\n", .{err});
|
std.debug.panic("Failed to clear the entities: {}\n", .{err});
|
||||||
@@ -102,6 +130,7 @@ pub const Simulator = struct {
|
|||||||
|
|
||||||
/// User initiated road building functionality
|
/// User initiated road building functionality
|
||||||
fn new_road(self: *Simulator, pos: rl.Vector2) void {
|
fn new_road(self: *Simulator, pos: rl.Vector2) void {
|
||||||
|
if (self.show_connections) return;
|
||||||
const cur_node = self.node_man.getSelectedNode(self.allocator, pos) catch |err| {
|
const cur_node = self.node_man.getSelectedNode(self.allocator, pos) catch |err| {
|
||||||
std.debug.panic("Failed to append the newly created node at pos ({d}, {d}) to node list: {}\n", .{
|
std.debug.panic("Failed to append the newly created node at pos ({d}, {d}) to node list: {}\n", .{
|
||||||
pos.x, pos.y, err
|
pos.x, pos.y, err
|
||||||
@@ -143,6 +172,7 @@ pub const Simulator = struct {
|
|||||||
|
|
||||||
/// Clearing node and road lists without deinitialising them (only the children)
|
/// Clearing node and road lists without deinitialising them (only the children)
|
||||||
fn clear(self: *Simulator) !void {
|
fn clear(self: *Simulator) !void {
|
||||||
|
self.highlighted_entity = null;
|
||||||
self.road_man.clear(self.allocator);
|
self.road_man.clear(self.allocator);
|
||||||
try self.node_man.clear(self.allocator);
|
try self.node_man.clear(self.allocator);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user