Skip to main content

traaains/editor/track/
mod.rs

1//! Provides functionality for editing tracks (bezier curves)
2
3use bevy::{
4    app::{Plugin, Update},
5    ecs::system::{Res, ResMut},
6    input::{ButtonInput, keyboard::KeyCode},
7    state::{
8        app::AppExtStates,
9        state::{NextState, SubStates},
10    },
11};
12
13use crate::editor::{
14    self, EditorState,
15    track::{placing::PlacingPlugin, removing::RemovingPlugin},
16};
17
18mod placing;
19mod removing;
20
21#[derive(Clone, Debug, Default, PartialEq, Eq, Hash, SubStates)]
22#[source(EditorState = EditorState::Track)]
23pub enum TrackEditorState {
24    #[default]
25    Placing,
26    Removing,
27}
28
29/// System for switching between track editor states.
30/// Will not run when in other editors due to `TrackEditorState` dependency on EditorState being TrackEditor,
31/// and thus does not require a run condition.
32fn input_handler(
33    input: Res<ButtonInput<KeyCode>>,
34    mut next_editor_state: ResMut<NextState<editor::EditorState>>,
35    mut next_track_state: ResMut<NextState<TrackEditorState>>,
36) {
37    if input.just_pressed(KeyCode::Escape) {
38        next_editor_state.set(editor::EditorState::Viewing);
39    }
40
41    if input.just_pressed(KeyCode::Digit0) {
42        next_track_state.set(TrackEditorState::Placing);
43    }
44
45    if input.just_pressed(KeyCode::Digit9) {
46        next_track_state.set(TrackEditorState::Removing);
47    }
48}
49
50/// Provides functionality for editing tracks (bezier curves)
51pub struct TrackEditorPlugin;
52
53impl Plugin for TrackEditorPlugin {
54    fn build(&self, app: &mut bevy::app::App) {
55        app.add_sub_state::<TrackEditorState>()
56            .add_plugins(PlacingPlugin)
57            .add_plugins(RemovingPlugin)
58            .add_systems(Update, input_handler);
59    }
60}