Skip to content

Commit

Permalink
Merge branch 'main' into example-game_menu-DRY
Browse files Browse the repository at this point in the history
  • Loading branch information
awwsmm authored Mar 22, 2024
2 parents bae4b7b + 67cc605 commit 2ac488f
Show file tree
Hide file tree
Showing 39 changed files with 506 additions and 169 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,7 @@ bevy_internal = { path = "crates/bevy_internal", version = "0.14.0-dev", default

[dev-dependencies]
rand = "0.8.0"
rand_chacha = "0.3.1"
ron = "0.8.0"
flate2 = "1.0"
serde = { version = "1", features = ["derive"] }
Expand Down
3 changes: 2 additions & 1 deletion crates/bevy_animation/src/animatable.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::util;
use bevy_color::{ClampColor, Laba, LinearRgba, Oklaba, Xyza};
use bevy_color::{ClampColor, Laba, LinearRgba, Oklaba, Srgba, Xyza};
use bevy_ecs::world::World;
use bevy_math::*;
use bevy_reflect::Reflect;
Expand Down Expand Up @@ -96,6 +96,7 @@ impl_float_animatable!(DVec4, f64);
impl_color_animatable!(LinearRgba);
impl_color_animatable!(Laba);
impl_color_animatable!(Oklaba);
impl_color_animatable!(Srgba);
impl_color_animatable!(Xyza);

// Vec3 is special cased to use Vec3A internally for blending
Expand Down
17 changes: 17 additions & 0 deletions crates/bevy_app/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use bevy_ecs::{
common_conditions::run_once as run_once_condition, run_enter_schedule,
InternedScheduleLabel, ScheduleBuildSettings, ScheduleLabel,
},
system::SystemId,
};
use bevy_utils::{intern::Interned, tracing::debug, HashMap, HashSet};
use std::{
Expand Down Expand Up @@ -453,6 +454,22 @@ impl App {
self
}

/// Registers a system and returns a [`SystemId`] so it can later be called by [`World::run_system`].
///
/// It's possible to register the same systems more than once, they'll be stored separately.
///
/// This is different from adding systems to a [`Schedule`] with [`App::add_systems`],
/// because the [`SystemId`] that is returned can be used anywhere in the [`World`] to run the associated system.
/// This allows for running systems in a push-based fashion.
/// Using a [`Schedule`] is still preferred for most cases
/// due to its better performance and ability to run non-conflicting systems simultaneously.
pub fn register_system<I: 'static, O: 'static, M, S: IntoSystem<I, O, M> + 'static>(
&mut self,
system: S,
) -> SystemId<I, O> {
self.world.register_system(system)
}

/// Configures a collection of system sets in the provided schedule, adding any sets that do not exist.
#[track_caller]
pub fn configure_sets(
Expand Down
21 changes: 0 additions & 21 deletions crates/bevy_asset/src/handle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,27 +249,13 @@ impl<A: Asset> PartialEq for Handle<A> {

impl<A: Asset> Eq for Handle<A> {}

impl<A: Asset> From<Handle<A>> for AssetId<A> {
#[inline]
fn from(value: Handle<A>) -> Self {
value.id()
}
}

impl<A: Asset> From<&Handle<A>> for AssetId<A> {
#[inline]
fn from(value: &Handle<A>) -> Self {
value.id()
}
}

impl<A: Asset> From<Handle<A>> for UntypedAssetId {
#[inline]
fn from(value: Handle<A>) -> Self {
value.id().into()
}
}

impl<A: Asset> From<&Handle<A>> for UntypedAssetId {
#[inline]
fn from(value: &Handle<A>) -> Self {
Expand Down Expand Up @@ -429,13 +415,6 @@ impl PartialOrd for UntypedHandle {
}
}

impl From<UntypedHandle> for UntypedAssetId {
#[inline]
fn from(value: UntypedHandle) -> Self {
value.id()
}
}

impl From<&UntypedHandle> for UntypedAssetId {
#[inline]
fn from(value: &UntypedHandle) -> Self {
Expand Down
6 changes: 3 additions & 3 deletions crates/bevy_asset/src/io/embedded/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ pub fn watched_path(_source_file_path: &'static str, _asset_path: &'static str)
macro_rules! load_internal_asset {
($app: ident, $handle: expr, $path_str: expr, $loader: expr) => {{
let mut assets = $app.world.resource_mut::<$crate::Assets<_>>();
assets.insert($handle, ($loader)(
assets.insert($handle.id(), ($loader)(
include_str!($path_str),
std::path::Path::new(file!())
.parent()
Expand All @@ -266,7 +266,7 @@ macro_rules! load_internal_asset {
// we can't support params without variadic arguments, so internal assets with additional params can't be hot-reloaded
($app: ident, $handle: ident, $path_str: expr, $loader: expr $(, $param:expr)+) => {{
let mut assets = $app.world.resource_mut::<$crate::Assets<_>>();
assets.insert($handle, ($loader)(
assets.insert($handle.id(), ($loader)(
include_str!($path_str),
std::path::Path::new(file!())
.parent()
Expand All @@ -284,7 +284,7 @@ macro_rules! load_internal_binary_asset {
($app: ident, $handle: expr, $path_str: expr, $loader: expr) => {{
let mut assets = $app.world.resource_mut::<$crate::Assets<_>>();
assets.insert(
$handle,
$handle.id(),
($loader)(
include_bytes!($path_str).as_ref(),
std::path::Path::new(file!())
Expand Down
4 changes: 2 additions & 2 deletions crates/bevy_asset/src/reflect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ impl<A: Asset + FromReflect> FromType<A> for ReflectAsset {
let mut assets = world.resource_mut::<Assets<A>>();
let value: A = FromReflect::from_reflect(value)
.expect("could not call `FromReflect::from_reflect` in `ReflectAsset::set`");
assets.insert(handle.typed_debug_checked(), value);
assets.insert(&handle.typed_debug_checked(), value);
},
len: |world| {
let assets = world.resource::<Assets<A>>();
Expand All @@ -161,7 +161,7 @@ impl<A: Asset + FromReflect> FromType<A> for ReflectAsset {
},
remove: |world, handle| {
let mut assets = world.resource_mut::<Assets<A>>();
let value = assets.remove(handle.typed_debug_checked());
let value = assets.remove(&handle.typed_debug_checked());
value.map(|value| Box::new(value) as Box<dyn Reflect>)
},
}
Expand Down
29 changes: 28 additions & 1 deletion crates/bevy_color/src/color_ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,18 @@ pub trait ClampColor: Sized {
fn is_within_bounds(&self) -> bool;
}

/// Utility function for interpolating hue values. This ensures that the interpolation
/// takes the shortest path around the color wheel, and that the result is always between
/// 0 and 360.
pub(crate) fn lerp_hue(a: f32, b: f32, t: f32) -> f32 {
let diff = (b - a + 180.0).rem_euclid(360.) - 180.;
(a + diff * t).rem_euclid(360.0)
}

#[cfg(test)]
mod tests {
use super::*;
use crate::Hsla;
use crate::{testing::assert_approx_eq, Hsla};

#[test]
fn test_rotate_hue() {
Expand All @@ -113,4 +121,23 @@ mod tests {
assert_eq!(hsla.rotate_hue(360.0), hsla);
assert_eq!(hsla.rotate_hue(-360.0), hsla);
}

#[test]
fn test_hue_wrap() {
assert_approx_eq!(lerp_hue(10., 20., 0.25), 12.5, 0.001);
assert_approx_eq!(lerp_hue(10., 20., 0.5), 15., 0.001);
assert_approx_eq!(lerp_hue(10., 20., 0.75), 17.5, 0.001);

assert_approx_eq!(lerp_hue(20., 10., 0.25), 17.5, 0.001);
assert_approx_eq!(lerp_hue(20., 10., 0.5), 15., 0.001);
assert_approx_eq!(lerp_hue(20., 10., 0.75), 12.5, 0.001);

assert_approx_eq!(lerp_hue(10., 350., 0.25), 5., 0.001);
assert_approx_eq!(lerp_hue(10., 350., 0.5), 0., 0.001);
assert_approx_eq!(lerp_hue(10., 350., 0.75), 355., 0.001);

assert_approx_eq!(lerp_hue(350., 10., 0.25), 355., 0.001);
assert_approx_eq!(lerp_hue(350., 10., 0.5), 0., 0.001);
assert_approx_eq!(lerp_hue(350., 10., 0.75), 5., 0.001);
}
}
10 changes: 1 addition & 9 deletions crates/bevy_color/src/hsla.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,16 +105,8 @@ impl Mix for Hsla {
#[inline]
fn mix(&self, other: &Self, factor: f32) -> Self {
let n_factor = 1.0 - factor;
// TODO: Refactor this into EuclideanModulo::lerp_modulo
let shortest_angle = ((((other.hue - self.hue) % 360.) + 540.) % 360.) - 180.;
let mut hue = self.hue + shortest_angle * factor;
if hue < 0. {
hue += 360.;
} else if hue >= 360. {
hue -= 360.;
}
Self {
hue,
hue: crate::color_ops::lerp_hue(self.hue, other.hue, factor),
saturation: self.saturation * n_factor + other.saturation * factor,
lightness: self.lightness * n_factor + other.lightness * factor,
alpha: self.alpha * n_factor + other.alpha * factor,
Expand Down
15 changes: 14 additions & 1 deletion crates/bevy_color/src/hsva.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{Alpha, ClampColor, Hue, Hwba, Lcha, LinearRgba, Srgba, StandardColor, Xyza};
use crate::{Alpha, ClampColor, Hue, Hwba, Lcha, LinearRgba, Mix, Srgba, StandardColor, Xyza};
use bevy_reflect::prelude::*;
use serde::{Deserialize, Serialize};

Expand Down Expand Up @@ -69,6 +69,19 @@ impl Default for Hsva {
}
}

impl Mix for Hsva {
#[inline]
fn mix(&self, other: &Self, factor: f32) -> Self {
let n_factor = 1.0 - factor;
Self {
hue: crate::color_ops::lerp_hue(self.hue, other.hue, factor),
saturation: self.saturation * n_factor + other.saturation * factor,
value: self.value * n_factor + other.value * factor,
alpha: self.alpha * n_factor + other.alpha * factor,
}
}
}

impl Alpha for Hsva {
#[inline]
fn with_alpha(&self, alpha: f32) -> Self {
Expand Down
15 changes: 14 additions & 1 deletion crates/bevy_color/src/hwba.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//! in [_HWB - A More Intuitive Hue-Based Color Model_] by _Smith et al_.
//!
//! [_HWB - A More Intuitive Hue-Based Color Model_]: https://web.archive.org/web/20240226005220/http://alvyray.com/Papers/CG/HWB_JGTv208.pdf
use crate::{Alpha, ClampColor, Hue, Lcha, LinearRgba, Srgba, StandardColor, Xyza};
use crate::{Alpha, ClampColor, Hue, Lcha, LinearRgba, Mix, Srgba, StandardColor, Xyza};
use bevy_reflect::prelude::*;
use serde::{Deserialize, Serialize};

Expand Down Expand Up @@ -73,6 +73,19 @@ impl Default for Hwba {
}
}

impl Mix for Hwba {
#[inline]
fn mix(&self, other: &Self, factor: f32) -> Self {
let n_factor = 1.0 - factor;
Self {
hue: crate::color_ops::lerp_hue(self.hue, other.hue, factor),
whiteness: self.whiteness * n_factor + other.whiteness * factor,
blackness: self.blackness * n_factor + other.blackness * factor,
alpha: self.alpha * n_factor + other.alpha * factor,
}
}
}

impl Alpha for Hwba {
#[inline]
fn with_alpha(&self, alpha: f32) -> Self {
Expand Down
50 changes: 5 additions & 45 deletions crates/bevy_color/src/srgba.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::ops::{Div, Mul};

use crate::color_difference::EuclideanDistance;
use crate::{Alpha, ClampColor, LinearRgba, Luminance, Mix, StandardColor, Xyza};
use crate::{
impl_componentwise_point, Alpha, ClampColor, LinearRgba, Luminance, Mix, StandardColor, Xyza,
};
use bevy_math::Vec4;
use bevy_reflect::prelude::*;
use serde::{Deserialize, Serialize};
Expand All @@ -27,6 +27,8 @@ pub struct Srgba {

impl StandardColor for Srgba {}

impl_componentwise_point!(Srgba, [red, green, blue, alpha]);

impl Srgba {
// The standard VGA colors, with alpha set to 1.0.
// https://en.wikipedia.org/wiki/Web_colors#Basic_colors
Expand Down Expand Up @@ -389,48 +391,6 @@ pub enum HexColorError {
Char(char),
}

/// All color channels are scaled directly,
/// but alpha is unchanged.
///
/// Values are not clamped.
impl Mul<f32> for Srgba {
type Output = Self;

fn mul(self, rhs: f32) -> Self {
Self {
red: self.red * rhs,
green: self.green * rhs,
blue: self.blue * rhs,
alpha: self.alpha,
}
}
}

impl Mul<Srgba> for f32 {
type Output = Srgba;

fn mul(self, rhs: Srgba) -> Srgba {
rhs * self
}
}

/// All color channels are scaled directly,
/// but alpha is unchanged.
///
/// Values are not clamped.
impl Div<f32> for Srgba {
type Output = Self;

fn div(self, rhs: f32) -> Self {
Self {
red: self.red / rhs,
green: self.green / rhs,
blue: self.blue / rhs,
alpha: self.alpha,
}
}
}

#[cfg(test)]
mod tests {
use crate::testing::assert_approx_eq;
Expand Down
12 changes: 9 additions & 3 deletions crates/bevy_ecs/src/query/filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,12 @@ pub trait QueryFilter: WorldQuery {
/// many elements are being iterated (such as `Iterator::collect()`).
const IS_ARCHETYPAL: bool;

/// Returns true if the provided [`Entity`] and [`TableRow`] should be included in the query results.
/// If false, the entity will be skipped.
///
/// Note that this is called after already restricting the matched [`Table`]s and [`Archetype`]s to the
/// ones that are compatible with the Filter's access.
///
/// # Safety
///
/// Must always be called _after_ [`WorldQuery::set_table`] or [`WorldQuery::set_archetype`]. `entity` and
Expand Down Expand Up @@ -357,7 +363,7 @@ impl<T: WorldQuery> Clone for OrFetch<'_, T> {
}
}

macro_rules! impl_query_filter_tuple {
macro_rules! impl_or_query_filter {
($(($filter: ident, $state: ident)),*) => {
#[allow(unused_variables)]
#[allow(non_snake_case)]
Expand Down Expand Up @@ -506,7 +512,7 @@ macro_rules! impl_tuple_query_filter {
}

all_tuples!(impl_tuple_query_filter, 0, 15, F);
all_tuples!(impl_query_filter_tuple, 0, 15, F, S);
all_tuples!(impl_or_query_filter, 0, 15, F, S);

/// A filter on a component that only retains results added after the system last ran.
///
Expand All @@ -524,7 +530,7 @@ all_tuples!(impl_query_filter_tuple, 0, 15, F, S);
/// # Time complexity
///
/// `Added` is not [`ArchetypeFilter`], which practically means that
/// if query (with `T` component filter) matches million entities,
/// if the query (with `T` component filter) matches a million entities,
/// `Added<T>` filter will iterate over all of them even if none of them were just added.
///
/// For example, these two systems are roughly equivalent in terms of performance:
Expand Down
Loading

0 comments on commit 2ac488f

Please sign in to comment.