Skip to main content

paiagram_core/trip/
class.rs

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