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
44
45
46
47
48
49
use bevy::{ecs::system::CommandQueue, prelude::*};
use crate::widget_state::WidgetState;
#[derive(Component, Default)]
pub struct PreviousWidget;
#[derive(Default)]
pub(crate) struct EntityCloneSystems(
pub Vec<(
fn(&mut World, Entity, Entity),
fn(&mut World, Entity, Entity, &WidgetState),
)>,
);
pub(crate) fn clone_system<T: Clone + Component>(
world: &mut World,
target: Entity,
reference: Entity,
) {
if let Some(v) = world.entity(reference).get::<T>() {
let v = v.clone();
if let Some(mut entity) = world.get_entity_mut(target) {
entity.insert(v);
}
}
}
pub(crate) fn clone_state<State: Component + PartialEq + Clone>(
world: &mut World,
target: Entity,
reference: Entity,
widget_state: &WidgetState,
) {
if let Some(reference_state_entity) = widget_state.get(reference) {
if let Some(v) = world.entity(reference_state_entity).get::<State>() {
if let Some(target_state_entity) = widget_state.get(target) {
let v = v.clone();
world.entity_mut(target_state_entity).insert(v);
} else {
let mut command_queue = CommandQueue::default();
let mut commands = Commands::new(&mut command_queue, world);
let state_entity = widget_state.add::<State>(&mut commands, target, v.clone());
commands.entity(state_entity).insert(PreviousWidget);
command_queue.apply(world);
}
}
}
}