Skip to content

Commit

Permalink
copy component_change_detection example from @alice-i-cecile's draft
Browse files Browse the repository at this point in the history
  • Loading branch information
xiejiaen committed Oct 31, 2024
1 parent e851496 commit 5cb7832
Show file tree
Hide file tree
Showing 10 changed files with 400 additions and 158 deletions.
11 changes: 11 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2914,6 +2914,17 @@ description = "Demonstrates how to create a node with a border"
category = "UI (User Interface)"
wasm = true

[[example]]
name = "responding_to_changes"
path = "examples/ecs/responding_to_changes.rs"
doc-scrape-examples = true

[package.metadata.example.reactivity]
name = "Responding to Changes"
description = "Demonstrates how and when to use change detection and `OnMutate` hooks and observers"
category = "ECS (Entity Component System)"
wasm = true

[[example]]
name = "box_shadow"
path = "examples/ui/box_shadow.rs"
Expand Down
1 change: 1 addition & 0 deletions crates/bevy_app/src/sub_app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ type ExtractFn = Box<dyn Fn(&mut World, &mut World) + Send>;
///
/// // Create a sub-app with the same resource and a single schedule.
/// let mut sub_app = SubApp::new();
/// sub_app.update_schedule = Some(Main.intern());
/// sub_app.insert_resource(Val(100));
///
/// // Setup an extract function to copy the resource's value in the main world.
Expand Down
1 change: 1 addition & 0 deletions crates/bevy_ecs/macros/src/world_query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ pub(crate) fn world_query_impl(
}

const IS_DENSE: bool = true #(&& <#field_types>::IS_DENSE)*;
const IS_MUTATE: bool = false #(|| <#field_types>::IS_MUTATE)*;

/// SAFETY: we call `set_archetype` for each member that implements `Fetch`
#[inline]
Expand Down
32 changes: 16 additions & 16 deletions crates/bevy_picking/src/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ impl<E: Debug + Clone + Reflect> core::ops::Deref for Pointer<E> {

impl<E: Debug + Clone + Reflect> Pointer<E> {
/// Construct a new `Pointer<E>` event.
pub fn new(target: Entity, id: PointerId, location: Location, event: E) -> Self {
pub fn new(id: PointerId, location: Location, target: Entity, event: E) -> Self {
Self {
target,
pointer_id: id,
Expand Down Expand Up @@ -361,9 +361,9 @@ pub fn pointer_events(
{
state.dragging_over.insert(hovered_entity, hit.clone());
let drag_enter_event = Pointer::new(
hovered_entity,
pointer_id,
location.clone(),
hovered_entity,
DragEnter {
button,
dragged: *drag_target,
Expand All @@ -377,9 +377,9 @@ pub fn pointer_events(

// Always send Over events
let over_event = Pointer::new(
hovered_entity,
pointer_id,
location.clone(),
hovered_entity,
Over { hit: hit.clone() },
);
commands.trigger_targets(over_event.clone(), hovered_entity);
Expand Down Expand Up @@ -409,9 +409,9 @@ pub fn pointer_events(
.flat_map(|h| h.iter().map(|(entity, data)| (*entity, data.clone())))
{
let down_event = Pointer::new(
hovered_entity,
pointer_id,
location.clone(),
hovered_entity,
Down {
button,
hit: hit.clone(),
Expand All @@ -436,9 +436,9 @@ pub fn pointer_events(
if let Some((_, press_instant, _)) = state.pressing.get(&hovered_entity)
{
let click_event = Pointer::new(
hovered_entity,
pointer_id,
location.clone(),
hovered_entity,
Click {
button,
hit: hit.clone(),
Expand All @@ -450,9 +450,9 @@ pub fn pointer_events(
}
// Always send the Up event
let up_event = Pointer::new(
hovered_entity,
pointer_id,
location.clone(),
hovered_entity,
Up {
button,
hit: hit.clone(),
Expand All @@ -467,9 +467,9 @@ pub fn pointer_events(
// Emit DragDrop
for (dragged_over, hit) in state.dragging_over.iter() {
let drag_drop_event = Pointer::new(
*dragged_over,
pointer_id,
location.clone(),
*dragged_over,
DragDrop {
button,
dropped: drag_target,
Expand All @@ -481,9 +481,9 @@ pub fn pointer_events(
}
// Emit DragEnd
let drag_end_event = Pointer::new(
drag_target,
pointer_id,
location.clone(),
drag_target,
DragEnd {
button,
distance: drag.latest_pos - drag.start_pos,
Expand All @@ -494,9 +494,9 @@ pub fn pointer_events(
// Emit DragLeave
for (dragged_over, hit) in state.dragging_over.iter() {
let drag_leave_event = Pointer::new(
*dragged_over,
pointer_id,
location.clone(),
*dragged_over,
DragLeave {
button,
dragged: drag_target,
Expand Down Expand Up @@ -534,9 +534,9 @@ pub fn pointer_events(
},
);
let drag_start_event = Pointer::new(
*press_target,
pointer_id,
location.clone(),
*press_target,
DragStart {
button,
hit: hit.clone(),
Expand All @@ -549,9 +549,9 @@ pub fn pointer_events(
// Emit Drag events to the entities we are dragging
for (drag_target, drag) in state.dragging.iter_mut() {
let drag_event = Pointer::new(
*drag_target,
pointer_id,
location.clone(),
*drag_target,
Drag {
button,
distance: location.position - drag.start_pos,
Expand All @@ -572,9 +572,9 @@ pub fn pointer_events(
.filter(|(hovered_entity, _)| *hovered_entity != *drag_target)
{
let drag_over_event = Pointer::new(
hovered_entity,
pointer_id,
location.clone(),
hovered_entity,
DragOver {
button,
dragged: *drag_target,
Expand All @@ -594,9 +594,9 @@ pub fn pointer_events(
{
// Emit Move events to the entities we are hovering
let move_event = Pointer::new(
hovered_entity,
pointer_id,
location.clone(),
hovered_entity,
Move {
hit: hit.clone(),
delta,
Expand All @@ -615,7 +615,7 @@ pub fn pointer_events(
.flat_map(|h| h.iter().map(|(entity, data)| (*entity, data.to_owned())))
{
let cancel_event =
Pointer::new(hovered_entity, pointer_id, location.clone(), Cancel { hit });
Pointer::new(pointer_id, location.clone(), hovered_entity, Cancel { hit });
commands.trigger_targets(cancel_event.clone(), hovered_entity);
event_writers.cancel_events.send(cancel_event);
}
Expand Down Expand Up @@ -652,9 +652,9 @@ pub fn pointer_events(

// Always send Out events
let out_event = Pointer::new(
hovered_entity,
pointer_id,
location.clone(),
hovered_entity,
Out { hit: hit.clone() },
);
commands.trigger_targets(out_event.clone(), hovered_entity);
Expand All @@ -666,9 +666,9 @@ pub fn pointer_events(
state.dragging_over.remove(&hovered_entity);
for drag_target in state.dragging.keys() {
let drag_leave_event = Pointer::new(
hovered_entity,
pointer_id,
location.clone(),
hovered_entity,
DragLeave {
button,
dragged: *drag_target,
Expand Down
2 changes: 2 additions & 0 deletions crates/bevy_render/src/sync_world.rs
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,7 @@ mod render_entities_world_query_impls {
}

const IS_DENSE: bool = <&'static RenderEntity as WorldQuery>::IS_DENSE;
const IS_MUTATE: bool = <&'static RenderEntity as WorldQuery>::IS_MUTATE;

#[inline]
unsafe fn set_archetype<'w>(
Expand Down Expand Up @@ -393,6 +394,7 @@ mod render_entities_world_query_impls {
}

const IS_DENSE: bool = <&'static MainEntity as WorldQuery>::IS_DENSE;
const IS_MUTATE: bool = <&'static MainEntity as WorldQuery>::IS_MUTATE;

#[inline]
unsafe fn set_archetype<'w>(
Expand Down
4 changes: 2 additions & 2 deletions examples/animation/animated_fox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use std::{f32::consts::PI, time::Duration};

use bevy::{
animation::{animate_targets, AnimationTargetId, RepeatAnimation},
animation::{AnimationTargetId, RepeatAnimation},
color::palettes::css::WHITE,
pbr::CascadeShadowConfigBuilder,
prelude::*,
Expand All @@ -22,7 +22,7 @@ fn main() {
.init_resource::<ParticleAssets>()
.init_resource::<FoxFeetTargets>()
.add_systems(Startup, setup)
.add_systems(Update, setup_scene_once_loaded.before(animate_targets))
.add_systems(Update, setup_scene_once_loaded)
.add_systems(Update, (keyboard_animation_control, simulate_particles))
.add_observer(observe_on_step)
.run();
Expand Down
3 changes: 1 addition & 2 deletions examples/animation/animation_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
//! playing animations by clicking and dragging left or right within the nodes.
use bevy::{
animation::animate_targets,
color::palettes::{
basic::WHITE,
css::{ANTIQUE_WHITE, DARK_GREEN},
Expand Down Expand Up @@ -83,7 +82,7 @@ fn main() {
..default()
}))
.add_systems(Startup, (setup_assets, setup_scene, setup_ui))
.add_systems(Update, init_animations.before(animate_targets))
.add_systems(Update, init_animations)
.add_systems(
Update,
(handle_weight_drag, update_ui, sync_weights).chain(),
Expand Down
Loading

0 comments on commit 5cb7832

Please sign in to comment.