carbide_core/state/
environment.rs1use std::collections::HashMap;
2use std::fmt::Debug;
3
4use bitflags::_core::fmt::Formatter;
5use serde::Serialize;
6
7use crate::{Color, from_bin, to_bin, text};
8use crate::text::font::{Error, Id};
9use crate::widget::primitive::Widget;
10use crate::widget::types::image_information::ImageInformation;
11use crate::state::global_state::GlobalState;
12use crate::state::state::State;
13use serde::de::DeserializeOwned;
14
15pub struct Environment<GS> where GS: GlobalState {
16 stack: Vec<EnvironmentVariable>,
17 fonts: text::font::Map,
18 images_information: HashMap<crate::image_map::Id, ImageInformation>,
19 overlay_map: HashMap<String, Box<dyn Widget<GS>>>,
20 pub(crate) local_state: HashMap<String, Vec<u8>>,
21}
22
23impl<GS: GlobalState> std::fmt::Debug for Environment<GS> {
24 fn fmt(&self, _f: &mut Formatter<'_>) -> std::fmt::Result {
25 Ok(())
26 }
27}
28
29impl<GS: GlobalState> Environment<GS> {
30
31 pub fn new() -> Self {
32 Environment {
33 stack: vec![],
34 fonts: text::font::Map::new(),
35 images_information: HashMap::new(),
36 overlay_map: HashMap::new(),
37 local_state: HashMap::new()
38 }
39 }
40
41 pub fn get_image_information(&self, id: &crate::image_map::Id) -> Option<&ImageInformation> {
42 self.images_information.get(id)
43 }
44
45 pub fn insert_image(&mut self, id: crate::image_map::Id, image: ImageInformation) {
46 self.images_information.insert(id, image);
47 }
48
49 pub fn get_overlay(&mut self, id: &String) -> Option<Box<dyn Widget<GS>>> {
50 self.overlay_map.remove(id)
51 }
52
53 pub fn add_overlay(&mut self, id: &str, overlay: Box<dyn Widget<GS>>) {
54 self.overlay_map.insert(id.to_string(), overlay);
55 }
56
57 pub fn clear(&mut self) {
58 self.clear_local_state();
59 self.overlay_map.clear();
60 }
61
62 fn clear_local_state(&mut self) {
63 self.local_state.clear()
64 }
65
66 pub fn update_local_state<T: Serialize + Clone + Debug + DeserializeOwned>(&self, local_state: &mut dyn State<T, GS>) {
67 local_state.update_dependent_states(self);
68 if let Some(key) = local_state.get_key() {
69 let local_value: &Vec<u8> = match self.local_state.get(key) {
70 Some(n) => n,
71 None => return,
72 };
73 *local_state.get_latest_value_mut() = from_bin::<T>(&local_value).unwrap();
74 }
75 }
76
77 pub fn insert_local_state<T: Serialize + Clone + Debug>(&mut self, local_state: &dyn State<T, GS>) {
78 if let Some(key) = local_state.get_key() {
79 let value = local_state.get_latest_value();
80 self.local_state.insert(key.clone(), to_bin(value).unwrap());
81 }
82 }
83
84 pub fn insert_local_state_from_key_value<T: Serialize + Clone + Debug>(&mut self, key: &String, value: &T) {
85 self.local_state.insert(key.clone(), to_bin(value).unwrap());
86 }
87
88 pub fn get_fonts_map(&self) -> &text::font::Map {
89 &self.fonts
90 }
91
92 pub fn insert_font_from_file<P>(&mut self, path: P) -> Result<Id, Error>
93 where P: AsRef<std::path::Path>,
94 {
95 self.fonts.insert_from_file(path)
96
97 }
98
99 pub fn get_font(&self, id: Id) -> &rusttype::Font<'static> {
100 self.fonts.get(id).expect("No font was found with the id")
101 }
102
103 pub fn insert_font(&mut self, font: rusttype::Font<'static>) -> Id {
105 self.fonts.insert(font)
106 }
107
108 pub fn push_vec(&mut self, value: Vec<EnvironmentVariable>) {
113 for v in value {
114 self.push(v);
115 }
116 }
117
118 pub fn push(&mut self, value: EnvironmentVariable) {
119 self.stack.push(value);
120 }
121
122 pub fn pop(&mut self) {
123 self.stack.pop();
124 }
125}
126
127
128pub enum EnvironmentVariable {
129 String{key: String, value: String},
130 U32{key: String, value: u32},
131 F64{key: String, value: f64},
132 Color{key: String, value: Color},
133 I32{key: String, value: i32},
134}