Skip to main content

traaains/editor/
mod.rs

1//! Manages track editing, placing signals, etc.
2//!
3//! Editors are split into different areas of the simulation, therefore there are [tracks](track), [trains](train) and [signals](signal) editors.
4
5use bevy::{
6    app::{Plugin, Startup},
7    state::{app::AppExtStates, state::States},
8};
9use bevy::{
10    ecs::{component::Component, system::Commands},
11    text::{Justify, TextLayout},
12    ui::{Node, PositionType, px, widget::Text},
13};
14
15pub mod signal;
16pub mod track;
17pub mod train;
18pub mod viewing;
19
20#[derive(Clone, Debug, Default, PartialEq, Eq, Hash, States)]
21pub enum EditorState {
22    #[default]
23    Viewing,
24    Track,
25    Train,
26    Signal,
27}
28
29/// Marker component for the state text, providing the user information about the current editor's state.
30#[derive(Component)]
31pub struct StateText;
32
33pub fn setup_state_text(mut cmd: Commands) {
34    cmd.spawn((
35        // Accepts a `String` or any type that converts into a `String`, such as `&str`
36        Text::new("Viewing\n[0] Edit tracks".to_string()),
37        Node {
38            position_type: PositionType::Absolute,
39            bottom: px(8),
40            right: px(12),
41            ..Default::default()
42        },
43        TextLayout::justify(Justify::End),
44        StateText,
45    ));
46}
47
48pub struct EditorPlugin;
49
50impl Plugin for EditorPlugin {
51    fn build(&self, app: &mut bevy::app::App) {
52        app.init_state::<EditorState>()
53            .add_plugins(viewing::ViewingEditorPlugin)
54            .add_plugins(track::TrackEditorPlugin)
55            .add_plugins(train::TrainEditorPlugin)
56            .add_plugins(signal::SignalEditorPlugin)
57            .add_systems(Startup, setup_state_text);
58    }
59}