bevy_ecs/observer/
trigger_event.rs

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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
use crate::{
    component::ComponentId,
    entity::Entity,
    event::Event,
    world::{Command, DeferredWorld, World},
};

/// A [`Command`] that emits a given trigger for a given set of targets.
pub struct TriggerEvent<E, Targets: TriggerTargets = ()> {
    /// The event to trigger.
    pub event: E,

    /// The targets to trigger the event for.
    pub targets: Targets,
}

impl<E: Event, Targets: TriggerTargets> TriggerEvent<E, Targets> {
    pub(super) fn trigger(mut self, world: &mut World) {
        let event_type = world.register_component::<E>();
        trigger_event(world, event_type, &mut self.event, self.targets);
    }
}

impl<E: Event, Targets: TriggerTargets> TriggerEvent<&mut E, Targets> {
    pub(super) fn trigger_ref(self, world: &mut World) {
        let event_type = world.register_component::<E>();
        trigger_event(world, event_type, self.event, self.targets);
    }
}

impl<E: Event, Targets: TriggerTargets + Send + Sync + 'static> Command
    for TriggerEvent<E, Targets>
{
    fn apply(self, world: &mut World) {
        self.trigger(world);
    }
}

/// Emit a trigger for a dynamic component id. This is unsafe and must be verified manually.
pub struct EmitDynamicTrigger<T, Targets: TriggerTargets = ()> {
    event_type: ComponentId,
    event_data: T,
    targets: Targets,
}

impl<E, Targets: TriggerTargets> EmitDynamicTrigger<E, Targets> {
    /// Sets the event type of the resulting trigger, used for dynamic triggers
    /// # Safety
    /// Caller must ensure that the component associated with `event_type` is accessible as E
    pub unsafe fn new_with_id(event_type: ComponentId, event_data: E, targets: Targets) -> Self {
        Self {
            event_type,
            event_data,
            targets,
        }
    }
}

impl<E: Event, Targets: TriggerTargets + Send + Sync + 'static> Command
    for EmitDynamicTrigger<E, Targets>
{
    fn apply(mut self, world: &mut World) {
        trigger_event(world, self.event_type, &mut self.event_data, self.targets);
    }
}

#[inline]
fn trigger_event<E: Event, Targets: TriggerTargets>(
    world: &mut World,
    event_type: ComponentId,
    event_data: &mut E,
    targets: Targets,
) {
    let mut world = DeferredWorld::from(world);
    if targets.entities().is_empty() {
        // SAFETY: T is accessible as the type represented by self.trigger, ensured in `Self::new`
        unsafe {
            world.trigger_observers_with_data::<_, E::Traversal>(
                event_type,
                Entity::PLACEHOLDER,
                targets.components(),
                event_data,
                false,
            );
        };
    } else {
        for target in targets.entities() {
            // SAFETY: T is accessible as the type represented by self.trigger, ensured in `Self::new`
            unsafe {
                world.trigger_observers_with_data::<_, E::Traversal>(
                    event_type,
                    *target,
                    targets.components(),
                    event_data,
                    E::AUTO_PROPAGATE,
                );
            };
        }
    }
}

/// Represents a collection of targets for a specific [`Trigger`] of an [`Event`]. Targets can be of type [`Entity`] or [`ComponentId`].
///
/// When a trigger occurs for a given event and [`TriggerTargets`], any [`Observer`] that watches for that specific event-target combination
/// will run.
///
/// [`Trigger`]: crate::observer::Trigger
/// [`Observer`]: crate::observer::Observer
pub trait TriggerTargets {
    /// The components the trigger should target.
    fn components(&self) -> &[ComponentId];

    /// The entities the trigger should target.
    fn entities(&self) -> &[Entity];
}

impl TriggerTargets for () {
    fn components(&self) -> &[ComponentId] {
        &[]
    }

    fn entities(&self) -> &[Entity] {
        &[]
    }
}

impl TriggerTargets for Entity {
    fn components(&self) -> &[ComponentId] {
        &[]
    }

    fn entities(&self) -> &[Entity] {
        core::slice::from_ref(self)
    }
}

impl TriggerTargets for Vec<Entity> {
    fn components(&self) -> &[ComponentId] {
        &[]
    }

    fn entities(&self) -> &[Entity] {
        self.as_slice()
    }
}

impl<const N: usize> TriggerTargets for [Entity; N] {
    fn components(&self) -> &[ComponentId] {
        &[]
    }

    fn entities(&self) -> &[Entity] {
        self.as_slice()
    }
}

impl TriggerTargets for ComponentId {
    fn components(&self) -> &[ComponentId] {
        core::slice::from_ref(self)
    }

    fn entities(&self) -> &[Entity] {
        &[]
    }
}

impl TriggerTargets for Vec<ComponentId> {
    fn components(&self) -> &[ComponentId] {
        self.as_slice()
    }

    fn entities(&self) -> &[Entity] {
        &[]
    }
}

impl<const N: usize> TriggerTargets for [ComponentId; N] {
    fn components(&self) -> &[ComponentId] {
        self.as_slice()
    }

    fn entities(&self) -> &[Entity] {
        &[]
    }
}

impl TriggerTargets for &Vec<Entity> {
    fn components(&self) -> &[ComponentId] {
        &[]
    }

    fn entities(&self) -> &[Entity] {
        self.as_slice()
    }
}