Skip to main content

traaains/editor/track/placing/
input_handlers.rs

1use bevy::{
2    ecs::system::{Res, ResMut},
3    input::{ButtonInput, keyboard::KeyCode},
4    state::state::{NextState, State},
5};
6
7use crate::editor::track::placing::{CurrentStep, states::current_shape::CurrentShape};
8
9/// System that listens for switching shapes keybinds and changes state/resources accordingly.
10/// Any shape/mesh/material changes are handled in the state transition listeners.
11pub fn switch_shape(
12    input: Res<ButtonInput<KeyCode>>,
13    current_shape: Res<State<CurrentShape>>,
14    mut next_shape: ResMut<NextState<CurrentShape>>,
15    current_step: Res<State<CurrentStep>>,
16    mut next_step: ResMut<NextState<CurrentStep>>,
17) {
18    if input.just_pressed(KeyCode::KeyJ) {
19        next_shape.set(current_shape.prev());
20
21        if *current_step.get() != CurrentStep::Zero {
22            next_step.set(CurrentStep::Linear);
23        }
24    }
25
26    if input.just_pressed(KeyCode::KeyL) {
27        next_shape.set(current_shape.next());
28
29        if *current_step.get() != CurrentStep::Zero {
30            next_step.set(CurrentStep::Linear);
31        }
32    }
33}
34
35/// Manages going forward a step on left click while placing track.
36/// Does not handle final step.
37pub fn advance_step(
38    current_step: Res<State<CurrentStep>>,
39    mut next_step: ResMut<NextState<CurrentStep>>,
40) {
41    match current_step.get() {
42        CurrentStep::Zero => next_step.set(CurrentStep::Linear),
43        CurrentStep::Linear => next_step.set(CurrentStep::Quadratic),
44        CurrentStep::Quadratic => next_step.set(CurrentStep::Cubic),
45        _ => {}
46    }
47}
48
49/// Manages going back a step on right click while placing track.
50pub fn back_step(
51    current_step: Res<State<CurrentStep>>,
52    mut next_step: ResMut<NextState<CurrentStep>>,
53) {
54    match current_step.get() {
55        CurrentStep::Zero => {}
56        CurrentStep::Linear => {
57            next_step.set(CurrentStep::Zero);
58        }
59        CurrentStep::Quadratic => {
60            next_step.set(CurrentStep::Linear);
61        }
62        CurrentStep::Cubic => {
63            next_step.set(CurrentStep::Quadratic);
64        }
65    }
66}