Skip to content

Commit

Permalink
Merge branch 'main' into fixed-update-cfg
Browse files Browse the repository at this point in the history
  • Loading branch information
Lubba-64 authored Aug 26, 2024
2 parents 7ae3861 + 95ef8f6 commit 1b1c6a8
Show file tree
Hide file tree
Showing 117 changed files with 4,711 additions and 2,668 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ jobs:
steps:
- uses: actions/checkout@v4
- name: Check for typos
uses: crate-ci/typos@v1.23.6
uses: crate-ci/typos@v1.24.1
- name: Typos info
if: failure()
run: |
Expand Down
14 changes: 11 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -180,9 +180,6 @@ trace_tracy_memory = [
# Tracing support
trace = ["bevy_internal/trace"]

# Save a trace of all wgpu calls
wgpu_trace = ["bevy_internal/wgpu_trace"]

# EXR image format support
exr = ["bevy_internal/exr"]

Expand Down Expand Up @@ -3387,6 +3384,17 @@ description = "Demonstrates how to use picking events to spawn simple objects"
category = "Picking"
wasm = true

[[example]]
name = "sprite_picking"
path = "examples/picking/sprite_picking.rs"
doc-scrape-examples = true

[package.metadata.example.sprite_picking]
name = "Sprite Picking"
description = "Demonstrates picking sprites and sprite atlases"
category = "Picking"
wasm = true

[profile.wasm-release]
inherits = "release"
opt-level = "z"
Expand Down
19 changes: 6 additions & 13 deletions benches/benches/bevy_ecs/world/commands.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::mem::size_of;

use bevy_ecs::{
component::Component,
entity::Entity,
Expand Down Expand Up @@ -44,19 +45,11 @@ pub fn spawn_commands(criterion: &mut Criterion) {
bencher.iter(|| {
let mut commands = Commands::new(&mut command_queue, &world);
for i in 0..entity_count {
let mut entity = commands.spawn_empty();

if black_box(i % 2 == 0) {
entity.insert(A);
}

if black_box(i % 3 == 0) {
entity.insert(B);
}

if black_box(i % 4 == 0) {
entity.insert(C);
}
let mut entity = commands
.spawn_empty()
.insert_if(A, || black_box(i % 2 == 0))
.insert_if(B, || black_box(i % 3 == 0))
.insert_if(C, || black_box(i % 4 == 0));

if black_box(i % 5 == 0) {
entity.despawn();
Expand Down
17 changes: 17 additions & 0 deletions crates/bevy_animation/src/graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,23 @@ impl AnimationGraph {
(graph, node_index)
}

/// A convenience method to create an [`AnimationGraph`]s with an iterator
/// of clips.
///
/// All of the animation clips will be direct children of the root with
/// weight 1.0.
///
/// Returns the the graph and indices of the new nodes.
pub fn from_clips<'a, I>(clips: I) -> (Self, Vec<AnimationNodeIndex>)
where
I: IntoIterator<Item = Handle<AnimationClip>>,
<I as IntoIterator>::IntoIter: 'a,
{
let mut graph = Self::new();
let indices = graph.add_clips(clips, 1.0, graph.root).collect();
(graph, indices)
}

/// Adds an [`AnimationClip`] to the animation graph with the given weight
/// and returns its index.
///
Expand Down
8 changes: 6 additions & 2 deletions crates/bevy_animation/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -442,8 +442,9 @@ impl ActiveAnimation {
}

/// Sets the weight of this animation.
pub fn set_weight(&mut self, weight: f32) {
pub fn set_weight(&mut self, weight: f32) -> &mut Self {
self.weight = weight;
self
}

/// Pause the animation.
Expand Down Expand Up @@ -528,7 +529,10 @@ impl ActiveAnimation {
}
}

/// Animation controls
/// Animation controls.
///
/// Automatically added to any root animations of a `SceneBundle` when it is
/// spawned.
#[derive(Component, Default, Reflect)]
#[reflect(Component)]
pub struct AnimationPlayer {
Expand Down
30 changes: 30 additions & 0 deletions crates/bevy_app/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -990,6 +990,36 @@ impl App {
}

/// Spawns an [`Observer`] entity, which will watch for and respond to the given event.
///
/// # Examples
///
/// ```rust
/// # use bevy_app::prelude::*;
/// # use bevy_ecs::prelude::*;
/// # use bevy_utils::default;
/// #
/// # let mut app = App::new();
/// #
/// # #[derive(Event)]
/// # struct Party {
/// # friends_allowed: bool,
/// # };
/// #
/// # #[derive(Event)]
/// # struct Invite;
/// #
/// # #[derive(Component)]
/// # struct Friend;
/// #
/// // An observer system can be any system where the first parameter is a trigger
/// app.observe(|trigger: Trigger<Party>, friends: Query<Entity, With<Friend>>, mut commands: Commands| {
/// if trigger.event().friends_allowed {
/// for friend in friends.iter() {
/// commands.trigger_targets(Invite, friend);
/// }
/// }
/// });
/// ```
pub fn observe<E: Event, B: Bundle, M>(
&mut self,
observer: impl IntoObserverSystem<E, B, M>,
Expand Down
20 changes: 19 additions & 1 deletion crates/bevy_asset/src/io/embedded/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,13 @@ impl EmbeddedAssetRegistry {
self.dir.insert_meta(asset_path, value);
}

/// Removes an asset stored using `full_path` (the full path as [`file`] would return for that file, if it was capable of
/// running in a non-rust file). If no asset is stored with at `full_path` its a no-op.
/// It returning `Option` contains the originally stored `Data` or `None`.
pub fn remove_asset(&self, full_path: &Path) -> Option<super::memory::Data> {
self.dir.remove_asset(full_path)
}

/// Registers a `embedded` [`AssetSource`] that uses this [`EmbeddedAssetRegistry`].
// NOTE: unused_mut because embedded_watcher feature is the only mutable consumer of `let mut source`
#[allow(unused_mut)]
Expand Down Expand Up @@ -300,7 +307,7 @@ macro_rules! load_internal_binary_asset {

#[cfg(test)]
mod tests {
use super::_embedded_asset_path;
use super::{EmbeddedAssetRegistry, _embedded_asset_path};
use std::path::Path;

// Relative paths show up if this macro is being invoked by a local crate.
Expand Down Expand Up @@ -404,4 +411,15 @@ mod tests {
// Really, should be "my_crate/src/the/asset.png"
assert_eq!(asset_path, Path::new("my_crate/the/asset.png"));
}

#[test]
fn remove_embedded_asset() {
let reg = EmbeddedAssetRegistry::default();
let path = std::path::PathBuf::from("a/b/asset.png");
reg.insert_asset(path.clone(), &path, &[]);
assert!(reg.dir.get_asset(&path).is_some());
assert!(reg.remove_asset(&path).is_some());
assert!(reg.dir.get_asset(&path).is_none());
assert!(reg.remove_asset(&path).is_none());
}
}
11 changes: 11 additions & 0 deletions crates/bevy_asset/src/io/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,17 @@ impl Dir {
);
}

/// Removes the stored asset at `path` and returns the `Data` stored if found and otherwise `None`.
pub fn remove_asset(&self, path: &Path) -> Option<Data> {
let mut dir = self.clone();
if let Some(parent) = path.parent() {
dir = self.get_or_insert_dir(parent);
}
let key: Box<str> = path.file_name().unwrap().to_string_lossy().into();
let data = dir.0.write().assets.remove(&key);
data
}

pub fn insert_meta(&self, path: &Path, value: impl Into<Value>) {
let mut dir = self.clone();
if let Some(parent) = path.parent() {
Expand Down
20 changes: 6 additions & 14 deletions crates/bevy_core_pipeline/src/core_3d/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -584,20 +584,12 @@ pub fn extract_camera_prepass_phase(

live_entities.insert(entity);

let mut entity = commands.get_or_spawn(entity);

if depth_prepass {
entity.insert(DepthPrepass);
}
if normal_prepass {
entity.insert(NormalPrepass);
}
if motion_vector_prepass {
entity.insert(MotionVectorPrepass);
}
if deferred_prepass {
entity.insert(DeferredPrepass);
}
commands
.get_or_spawn(entity)
.insert_if(DepthPrepass, || depth_prepass)
.insert_if(NormalPrepass, || normal_prepass)
.insert_if(MotionVectorPrepass, || motion_vector_prepass)
.insert_if(DeferredPrepass, || deferred_prepass);
}

opaque_3d_prepass_phases.retain(|entity, _| live_entities.contains(entity));
Expand Down
5 changes: 4 additions & 1 deletion crates/bevy_dev_tools/src/ci_testing/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ pub use self::config::*;

use bevy_app::prelude::*;
use bevy_ecs::schedule::IntoSystemConfigs;
use bevy_render::view::screenshot::trigger_screenshots;
use bevy_time::TimeUpdateStrategy;
use std::time::Duration;

Expand Down Expand Up @@ -51,7 +52,9 @@ impl Plugin for CiTestingPlugin {
.insert_resource(config)
.add_systems(
Update,
systems::send_events.before(bevy_window::close_when_requested),
systems::send_events
.before(trigger_screenshots)
.before(bevy_window::close_when_requested),
);
}
}
22 changes: 5 additions & 17 deletions crates/bevy_dev_tools/src/ci_testing/systems.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use super::config::*;
use bevy_app::AppExit;
use bevy_ecs::prelude::*;
use bevy_render::view::screenshot::ScreenshotManager;
use bevy_utils::tracing::{debug, info, warn};
use bevy_window::PrimaryWindow;
use bevy_render::view::screenshot::{save_to_disk, Screenshot};
use bevy_utils::tracing::{debug, info};

pub(crate) fn send_events(world: &mut World, mut current_frame: Local<u32>) {
let mut config = world.resource_mut::<CiTestingConfig>();
Expand All @@ -23,21 +22,10 @@ pub(crate) fn send_events(world: &mut World, mut current_frame: Local<u32>) {
info!("Exiting after {} frames. Test successful!", *current_frame);
}
CiTestingEvent::Screenshot => {
let mut primary_window_query =
world.query_filtered::<Entity, With<PrimaryWindow>>();
let Ok(main_window) = primary_window_query.get_single(world) else {
warn!("Requesting screenshot, but PrimaryWindow is not available");
continue;
};
let Some(mut screenshot_manager) = world.get_resource_mut::<ScreenshotManager>()
else {
warn!("Requesting screenshot, but ScreenshotManager is not available");
continue;
};
let path = format!("./screenshot-{}.png", *current_frame);
screenshot_manager
.save_screenshot_to_disk(main_window, path)
.unwrap();
world
.spawn(Screenshot::primary_window())
.observe(save_to_disk(path));
info!("Took a screenshot at frame {}.", *current_frame);
}
// Custom events are forwarded to the world.
Expand Down
13 changes: 12 additions & 1 deletion crates/bevy_ecs/src/observer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use crate::{archetype::ArchetypeFlags, system::IntoObserverSystem, world::*};
use crate::{component::ComponentId, prelude::*, world::DeferredWorld};
use bevy_ptr::Ptr;
use bevy_utils::{EntityHashMap, HashMap};
use std::marker::PhantomData;
use std::{fmt::Debug, marker::PhantomData};

/// Type containing triggered [`Event`] information for a given run of an [`Observer`]. This contains the
/// [`Event`] data itself. If it was triggered for a specific [`Entity`], it includes that as well. It also
Expand Down Expand Up @@ -84,6 +84,17 @@ impl<'w, E, B: Bundle> Trigger<'w, E, B> {
}
}

impl<'w, E: Debug, B: Bundle> Debug for Trigger<'w, E, B> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Trigger")
.field("event", &self.event)
.field("propagate", &self.propagate)
.field("trigger", &self.trigger)
.field("_marker", &self._marker)
.finish()
}
}

/// A description of what an [`Observer`] observes.
#[derive(Default, Clone)]
pub struct ObserverDescriptor {
Expand Down
Loading

0 comments on commit 1b1c6a8

Please sign in to comment.