Skip to main content

fantasy_craft/gui/
alignment.rs

1use hecs::Entity;
2
3use crate::prelude::ComponentLoader;
4
5#[derive(Debug, Clone, Copy)]
6pub enum HorizontalAlignmentType {
7    Left,
8    Center,
9    Right
10}
11
12impl HorizontalAlignmentType {
13    pub fn to_str(&self) -> &'static str {
14        match self {
15            HorizontalAlignmentType::Left => "left",
16            HorizontalAlignmentType::Center => "center",
17            HorizontalAlignmentType::Right => "right"
18        }
19    }
20
21    pub fn from_str(value: &str) -> HorizontalAlignmentType {
22        match value {
23            "left" => HorizontalAlignmentType::Left,
24            "center" => HorizontalAlignmentType::Center,
25            "right" => HorizontalAlignmentType::Right,
26            _ => HorizontalAlignmentType::Left
27        }
28    }
29}
30
31#[derive(Debug, Clone)]
32pub struct HorizontalAlignment(pub HorizontalAlignmentType);
33
34pub struct HorizontalAlignmentLoader;
35
36impl ComponentLoader for HorizontalAlignmentLoader {
37    fn load(&self, ctx: &mut crate::prelude::Context, entity: Entity, data: &serde_json::Value) {
38        let loader_data: String = serde_json::from_value(data.clone())
39            .unwrap_or_default();
40
41        let component = HorizontalAlignment(HorizontalAlignmentType::from_str(&loader_data));
42
43        ctx.world.insert_one(entity, component).expect("Failed to insert HorizontalAlignment");
44    }
45}
46
47#[derive(Debug, Clone, Copy)]
48pub enum VerticalAlignmentType {
49    Top,
50    Center,
51    Bottom
52}
53
54impl VerticalAlignmentType {
55    pub fn to_str(&self) -> &'static str {
56        match self {
57            VerticalAlignmentType::Top => "top",
58            VerticalAlignmentType::Center => "center",
59            VerticalAlignmentType::Bottom => "bottom"
60        }
61    }
62
63    pub fn from_str(value: &str) -> VerticalAlignmentType {
64        match value {
65            "top" => VerticalAlignmentType::Top,
66            "center" => VerticalAlignmentType::Center,
67            "bottom" => VerticalAlignmentType::Bottom,
68            _ => VerticalAlignmentType::Top
69        }
70    }
71}
72
73#[derive(Debug, Clone)]
74pub struct VerticalAlignment(pub VerticalAlignmentType);
75
76pub struct VerticalAlignmentLoader;
77
78impl ComponentLoader for VerticalAlignmentLoader {
79    fn load(&self, ctx: &mut crate::prelude::Context, entity: Entity, data: &serde_json::Value) {
80        let loader_data: String = serde_json::from_value(data.clone())
81            .unwrap_or_default();
82
83        let component = VerticalAlignment(VerticalAlignmentType::from_str(&loader_data));
84
85        ctx.world.insert_one(entity, component).expect("Failed to insert VerticalAlignment");
86    }
87}