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
pub mod graph;
pub mod handler;
pub mod manager;
mod modifier_value;
pub mod parsers;
pub mod scopes;

use std::collections::{HashMap, HashSet};
use std::fmt::Debug;

use bevy_app::App;
use bevy_ecs::prelude::{Resource, SystemSet};
pub use manager::{CommandExecutionEvent, CommandProcessedEvent};
pub use modifier_value::ModifierValue;
use petgraph::prelude::NodeIndex;
pub use scopes::CommandScopeRegistry;

use crate::graph::{CommandGraph, CommandGraphBuilder};
use crate::handler::CommandHandlerPlugin;
use crate::parsers::ParseInput;

#[derive(SystemSet, Clone, PartialEq, Eq, Hash, Debug)]
pub struct CommandSystemSet;

#[derive(Resource, Default)]
#[allow(clippy::type_complexity)]
pub struct CommandRegistry {
    pub graph: CommandGraph,
    pub parsers: HashMap<NodeIndex, fn(&mut ParseInput) -> bool>,
    pub modifiers: HashMap<NodeIndex, fn(String, &mut HashMap<ModifierValue, ModifierValue>)>,
    pub executables: HashSet<NodeIndex>,
}

pub trait Command {
    fn assemble_graph(graph: &mut CommandGraphBuilder<Self>)
    where
        Self: Sized;
}

pub trait AddCommand {
    fn add_command<T: Command + Send + Sync + 'static>(&mut self) -> &mut Self;
}

impl AddCommand for App {
    fn add_command<T: Command + Send + Sync + 'static>(&mut self) -> &mut Self {
        self.add_plugins(CommandHandlerPlugin::<T>::new())
    }
}