1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
//! The `app3d` module provides functionality to visualize railway graphs in 3D.
//!
//! It includes functions to initialize a Bevy application with a given `RailwayGraph` or a default one,
//! and provides systems for displaying the graph, interacting with the user interface, and updating the camera.
//!
#![cfg_attr(target_arch = "wasm32", allow(dead_code, unused_imports))]

use std::sync::{Arc, RwLock};

use crate::prelude::{RailwayGraph, RailwayGraphExt};
use crate::simulation::Simulation;
use bevy::input::Input;
use bevy::prelude::*;
use bevy_egui::EguiPlugin;
use bevy_mod_picking::prelude::{On, Pointer};
use bevy_mod_picking::{
    prelude::{Click, RaycastPickTarget},
    DefaultPickingPlugins, PickableBundle,
};
use bevy_obj::ObjPlugin;
use petgraph::visit::IntoNodeReferences;
use petgraph::visit::NodeRef;

mod camera;
mod edges;
mod nodes;
mod train_agent;
mod ui;

use nodes::Node;

#[cfg(target_arch = "wasm32")]
use wasm_bindgen::prelude::*;

mod console;
mod projection;

pub use projection::Projection;

use self::train_agent::SelectedTrain;

/// Holds application state, including the area name, railway graph, and camera look-at position.
#[derive(Default, Resource)]
pub struct AppResource {
    area_name: String,
    graph: Option<RailwayGraph>,
    simulation: Option<Arc<RwLock<Simulation>>>,
    look_at_position: Option<Vec3>,
}

/// Stores configuration of current debug
#[derive(Default, Resource)]
pub struct DebugResource {
    show_train_target: bool,
}

/// Defines the different interaction modes for the application.
#[derive(Debug, PartialEq, Clone, Default)]
pub enum InteractionMode {
    /// Default mode: selecting nodes to display information or find the shortest path.
    #[default]
    SelectMode,
    /// Mode for placing trains on the railway network.
    PlaceTrain,
}

/// Stores the current interaction mode.
#[derive(Default, Resource)]
pub struct InteractionModeResource {
    mode: InteractionMode,
}

/// Configures a Bevy application with the necessary plugins, resources, and systems.
///
/// This function configures a Bevy application with the required plugins,
/// resources, and systems based on the given `AppResource`. The caller is
/// responsible for inserting any additional resources and running the application.
///
/// # Arguments
///
/// * `app` - A mutable reference to a `bevy::prelude::App` instance to configure.
/// * `app_resource` - An `AppResource` to configure the application.
///
pub fn setup_app(app: &mut App, app_resource: AppResource) {
    app.add_plugins(camera::CameraPlugins)
        .add_plugins(EguiPlugin)
        .add_plugins(DefaultPickingPlugins)
        .add_plugins(ObjPlugin)
        .insert_resource(app_resource)
        .insert_resource(nodes::SelectedNode::default())
        .insert_resource(SelectedTrain::default())
        .insert_resource(InteractionModeResource::default())
        .insert_resource(DebugResource::default())
        .add_systems(Startup, (setup, camera::setup_camera))
        .add_systems(
            Update,
            (
                update_look_at_position_system,
                nodes::select_node_system,
                edges::show_edges,
                train_agent::update_train_position_system,
                train_agent::update_train_agent_line_system,
                train_agent::select_train_system,
                update_simulation_system,
            ),
        )
        .add_event::<train_agent::TrainSelectedEvent>()
        .add_event::<nodes::NodeSelectedEvent>();
    ui::add_ui_systems_to_app(app);
    console::add_console_to_app(app);
}

/// Initializes the Bevy application with a given `RailwayGraph`.
///
/// This function sets up the Bevy application with the required plugins,
/// resources, and systems. It inserts the provided `RailwayGraph` into the
/// `AppResource` and starts the Bevy application.
///
/// # Arguments
///
/// * `graph` - A `RailwayGraph` to display in the application.
///
pub fn init_with_graph(graph: RailwayGraph) {
    let mut projection = Projection::new(5000.0, 5000.0);
    let (min_coord, max_coord) = graph.bounding_box();
    projection.set_bounding_box(min_coord, max_coord);

    let app_resource = AppResource {
        area_name: "".to_string(),
        graph: Some(graph.clone()),
        look_at_position: None,
        simulation: Some(Arc::new(RwLock::new(Simulation::new(graph)))),
    };
    let mut app = App::new();
    app.add_plugins(DefaultPlugins.set(WindowPlugin {
        primary_window: Some(Window {
            title: "Railwaymap".into(),
            resolution: (1000., 1000.).into(),
            fit_canvas_to_parent: true,
            prevent_default_event_handling: true,
            canvas: Some("#bevy".to_string()),
            ..default()
        }),
        ..default()
    }));
    setup_app(&mut app, app_resource);
    app.insert_resource(projection)
        .add_systems(Startup, (display_graph,))
        .run()
}

/// Initializes the Bevy application with a default `RailwayGraph`.
///
/// This function sets up the Bevy application with the required plugins,
/// resources, and systems. It inserts a default `AppResource` and starts the
/// Bevy application. The user can load a `RailwayGraph` through the UI.
///
/// # Note
///
/// This function is not available when compiling for the `wasm32` target.
#[cfg(not(target_arch = "wasm32"))]
pub fn init() {
    let projection = Projection::new(1000.0, 1000.0);
    let app_resource = AppResource::default();

    let mut app = App::new();

    app.add_plugins(DefaultPlugins.set(WindowPlugin {
        primary_window: Some(Window {
            title: "Railwaymap".into(),
            resolution: (1000., 1000.).into(),
            fit_canvas_to_parent: true,
            prevent_default_event_handling: false,
            ..default()
        }),
        ..default()
    }));
    setup_app(&mut app, app_resource);
    app.insert_resource(projection).run()
}

fn setup(mut commands: Commands) {
    commands.insert_resource(AmbientLight {
        color: Color::WHITE,
        brightness: 0.7,
    });
    commands.spawn(PointLightBundle {
        transform: Transform::from_xyz(0.0, 500.0, 100.0),
        point_light: PointLight {
            intensity: 1000.0,
            range: 1000.0,
            ..Default::default()
        },
        ..Default::default()
    });
}

fn update_simulation_system(app_resource: Res<AppResource>, time: Res<Time>) {
    if let Some(simulation) = &app_resource.simulation {
        simulation.write().unwrap().update(time.delta())
    }
}

fn update_look_at_position_system(
    keyboard_input: Res<Input<KeyCode>>,
    mut camera_query: Query<&mut Transform, With<Camera>>,
    mut app_resource: ResMut<AppResource>,
    projection: Res<Projection>,
) {
    if keyboard_input.just_pressed(KeyCode::N) {
        if let Some(graph) = &app_resource.graph {
            let mut nodes = graph.physical_graph.graph.node_references();
            let current_position = app_resource.look_at_position.unwrap_or(Vec3::ZERO);

            for transform in camera_query.iter() {
                println!("{:?}", transform);
            }

            let next_node = nodes
                .find(|(_, node_data)| {
                    let position = projection.project(node_data.location).unwrap_or_default();
                    position != current_position
                })
                .map(|(_, node_data)| projection.project(node_data.location).unwrap_or_default());

            if let Some(next_position) = next_node {
                app_resource.look_at_position = Some(next_position);
            }
            if let Some(look_at_position) = app_resource.look_at_position {
                for mut transform in camera_query.iter_mut() {
                    *transform = Transform::from_translation(transform.translation)
                        .looking_at(look_at_position, Vec3::Z);
                }
            }
        }
    }
}

fn display_graph(
    mut commands: Commands,
    app_resource: Res<AppResource>,
    node_query: Query<Entity, With<nodes::Node>>,
    projection: Res<Projection>,
    mut meshes: ResMut<Assets<Mesh>>,
    mut materials: ResMut<Assets<StandardMaterial>>,
) {
    if let Some(graph) = &app_resource.graph {
        // Clear previous edges and nodes
        for entity in node_query.iter() {
            commands.entity(entity).despawn();
        }

        // Display nodes
        for node in graph.physical_graph.graph.node_references() {
            let node_data = node.weight();
            let position = projection.project(node_data.location);

            if let Some(position) = position {
                commands
                    .spawn((
                        PbrBundle {
                            mesh: meshes.add(
                                Mesh::try_from(shape::Icosphere {
                                    radius: 1.0,
                                    subdivisions: 2,
                                })
                                .unwrap(),
                            ),
                            material: materials.add(StandardMaterial {
                                base_color: Color::RED,
                                ..Default::default()
                            }),
                            transform: Transform::from_translation(position),
                            ..Default::default()
                        },
                        PickableBundle::default(),
                        RaycastPickTarget::default(),
                        On::<Pointer<Click>>::send_event::<nodes::NodeSelectedEvent>(),
                    ))
                    .insert(nodes::Node { id: node_data.id });
            }
        }
    }
}