Newer
Older
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
use std::sync::{Arc, RwLock};
use bevy::{
prelude::{Commands, Component, Entity},
utils::HashMap,
};
/// Stores mappings between widget entities and their corresponding state entities.
#[derive(Default, Debug, Clone)]
pub struct WidgetState {
mapping: Arc<RwLock<HashMap<Entity, Entity>>>,
}
impl WidgetState {
/// Attempts to create a state entity or return the existing entity.
pub fn add<State: Component + PartialEq + Clone>(
&self,
commands: &mut Commands,
widget_entity: Entity,
initial_state: State,
) -> Entity {
if let Ok(mut mapping) = self.mapping.try_write() {
if mapping.contains_key(&widget_entity) {
*mapping.get(&widget_entity).unwrap()
} else {
let state_entity = commands.spawn(initial_state).id();
mapping.insert(widget_entity, state_entity);
state_entity
}
} else {
panic!("Couldn't get mapping lock!");
}
}
/// Attempts to get a state entity
pub fn get(&self, widget_entity: Entity) -> Option<Entity> {
if let Ok(mapping) = self.mapping.try_read() {
return mapping.get(&widget_entity).cloned();
}
None
}
}