Skip to main content

paiagram_core/trip/
class.rs

1use bevy::ecs::query::QueryData;
2use bevy::prelude::*;
3use moonshine_core::prelude::{MapEntities, ReflectMapEntities};
4
5use crate::colors::{DisplayedColor, PredefinedColor};
6
7pub struct ClassPlugin;
8impl Plugin for ClassPlugin {
9    fn build(&self, app: &mut App) {
10        app.init_resource::<ClassResource>();
11    }
12}
13
14#[derive(Debug, Reflect, Component, Clone, Copy)]
15#[reflect(Component)]
16pub struct DisplayedStroke {
17    pub color: DisplayedColor,
18    pub width: f32,
19}
20
21impl Default for DisplayedStroke {
22    fn default() -> Self {
23        Self {
24            color: DisplayedColor::Predefined(PredefinedColor::Emerald),
25            width: 1.0,
26        }
27    }
28}
29
30impl DisplayedStroke {
31    pub fn from_seed(data: impl AsRef<[u8]>) -> Self {
32        Self {
33            color: DisplayedColor::from_seed(data),
34            width: 1.0,
35        }
36    }
37    pub fn egui_stroke(&self, is_dark: bool) -> egui::Stroke {
38        egui::Stroke {
39            color: self.color.get(is_dark),
40            width: self.width,
41        }
42    }
43    pub fn neutral(is_dark: bool) -> egui::Stroke {
44        egui::Stroke {
45            color: DisplayedColor::Predefined(PredefinedColor::Neutral).get(is_dark),
46            width: 1.0,
47        }
48    }
49}
50
51#[derive(Default, Reflect, Component, MapEntities)]
52#[reflect(Component, MapEntities)]
53#[relationship_target(relationship = crate::trip::TripClass)]
54#[require(Name, DisplayedStroke)]
55pub struct Class {
56    #[relationship]
57    #[entities]
58    trips: Vec<Entity>,
59}
60
61impl Class {
62    pub fn as_trips(&self) -> &[Entity] {
63        self.trips.as_slice()
64    }
65}
66
67#[derive(Bundle)]
68pub struct ClassBundle {
69    pub class: Class,
70    pub name: Name,
71    pub stroke: DisplayedStroke,
72}
73
74#[derive(QueryData)]
75pub struct ClassQuery {
76    pub entity: Entity,
77    pub vehicles: &'static Class,
78    pub name: &'static Name,
79    pub stroke: &'static DisplayedStroke,
80}
81
82#[derive(Resource)]
83pub struct ClassResource {
84    // TODO: fix mismatch when reading saves
85    pub default_class: Entity,
86}
87
88impl FromWorld for ClassResource {
89    fn from_world(world: &mut World) -> Self {
90        let name = "Default Class";
91        let e = world
92            .spawn(ClassBundle {
93                class: Class::default(),
94                name: Name::new(name),
95                stroke: DisplayedStroke {
96                    color: DisplayedColor::Predefined(PredefinedColor::Neutral),
97                    width: 1.0,
98                },
99            })
100            .id();
101        Self { default_class: e }
102    }
103}