Skip to content

Commit

Permalink
Merge pull request bevyengine#167 from BD103/linear-rgba-everywhere
Browse files Browse the repository at this point in the history
Migrate `bevy_gizmos`
  • Loading branch information
alice-i-cecile authored Apr 26, 2024
2 parents 978e23c + 00e51c5 commit caebbaa
Show file tree
Hide file tree
Showing 60 changed files with 648 additions and 606 deletions.
3 changes: 1 addition & 2 deletions benches/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
[package]
name = "benches"
version = "0.1.0"
edition = "2021"
description = "Benchmarks for Bevy engine"
description = "Benchmarks that test Bevy's performance"
publish = false
license = "MIT OR Apache-2.0"

Expand Down
6 changes: 3 additions & 3 deletions crates/bevy_dev_tools/src/ui_debug_overlay/inset.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use bevy_color::Color;
use bevy_color::LinearRgba;
use bevy_gizmos::{config::GizmoConfigGroup, prelude::Gizmos};
use bevy_math::{Vec2, Vec2Swizzles};
use bevy_reflect::Reflect;
Expand Down Expand Up @@ -142,7 +142,7 @@ impl<'w, 's> InsetGizmo<'w, 's> {
};
position.xy()
}
fn line_2d(&mut self, mut start: Vec2, mut end: Vec2, color: Color) {
fn line_2d(&mut self, mut start: Vec2, mut end: Vec2, color: LinearRgba) {
if approx_eq(start.x, end.x) {
start.x = self.known_x.inset(start.x);
end.x = start.x;
Expand All @@ -167,7 +167,7 @@ impl<'w, 's> InsetGizmo<'w, 's> {
self.known_y.remove(top, 1);
self.known_y.remove(bottom, -1);
}
pub(super) fn rect_2d(&mut self, rect: LayoutRect, color: Color) {
pub(super) fn rect_2d(&mut self, rect: LayoutRect, color: LinearRgba) {
let (left, right, top, bottom) = rect_border_axis(rect);
if approx_eq(left, right) {
self.line_2d(Vec2::new(left, top), Vec2::new(left, bottom), color);
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_ecs/src/query/par_iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ impl<'w, 's, D: QueryData, F: QueryFilter> QueryParIter<'w, 's, D, F> {
/// Runs `func` on each query result in parallel on a value returned by `init`.
///
/// `init` may be called multiple times per thread, and the values returned may be discarded between tasks on any given thread.
/// Callers should avoid using this function as if it were a a parallel version
/// Callers should avoid using this function as if it were a parallel version
/// of [`Iterator::fold`].
///
/// # Example
Expand Down
5 changes: 1 addition & 4 deletions crates/bevy_ecs/src/system/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -581,10 +581,7 @@ impl<'w, 's> Commands<'w, 's> {
) -> SystemId<I, O> {
let entity = self.spawn_empty().id();
self.queue.push(RegisterSystem::new(system, entity));
SystemId {
entity,
marker: std::marker::PhantomData,
}
SystemId::from_entity(entity)
}

/// Pushes a generic [`Command`] to the command queue.
Expand Down
35 changes: 23 additions & 12 deletions crates/bevy_ecs/src/system/system_registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,29 @@ pub struct SystemId<I = (), O = ()> {
pub(crate) marker: std::marker::PhantomData<fn(I) -> O>,
}

impl<I, O> SystemId<I, O> {
/// Transforms a [`SystemId`] into the [`Entity`] that holds the one-shot system's state.
///
/// It's trivial to convert [`SystemId`] into an [`Entity`] since a one-shot system
/// is really an entity with associated handler function.
///
/// For example, this is useful if you want to assign a name label to a system.
pub fn entity(self) -> Entity {
self.entity
}

/// Create [`SystemId`] from an [`Entity`]. Useful when you only have entity handles to avoid
/// adding extra components that have a [`SystemId`] everywhere. To run a system with this ID
/// - The entity must be a system
/// - The `I` + `O` types must be correct
pub fn from_entity(entity: Entity) -> Self {
Self {
entity,
marker: std::marker::PhantomData,
}
}
}

impl<I, O> Eq for SystemId<I, O> {}

// A manual impl is used because the trait bounds should ignore the `I` and `O` phantom parameters.
Expand Down Expand Up @@ -78,18 +101,6 @@ impl<I, O> std::fmt::Debug for SystemId<I, O> {
}
}

impl<I, O> From<SystemId<I, O>> for Entity {
/// Transforms a [`SystemId`] into the [`Entity`] that holds the one-shot system's state.
///
/// It's trivial to convert [`SystemId`] into an [`Entity`] since a system
/// is really an entity with associated handler function.
///
/// For example, this is useful if you want to assign a name label to a system.
fn from(SystemId { entity, .. }: SystemId<I, O>) -> Self {
entity
}
}

impl World {
/// Registers a system and returns a [`SystemId`] so it can later be called by [`World::run_system`].
///
Expand Down
1 change: 0 additions & 1 deletion crates/bevy_ecs_compile_fail_tests/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
[package]
name = "bevy_ecs_compile_fail_tests"
version = "0.1.0"
edition = "2021"
description = "Compile fail tests for Bevy Engine's entity component system"
homepage = "https://bevyengine.org"
Expand Down
8 changes: 4 additions & 4 deletions crates/bevy_gizmos/src/aabb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use crate as bevy_gizmos;

use bevy_app::{Plugin, PostUpdate};
use bevy_color::{Color, Oklcha};
use bevy_color::{LinearRgba, Oklcha};
use bevy_ecs::{
component::Component,
entity::Entity,
Expand Down Expand Up @@ -58,7 +58,7 @@ pub struct AabbGizmoConfigGroup {
/// A random color is chosen per box if `None`.
///
/// Defaults to `None`.
pub default_color: Option<Color>,
pub default_color: Option<LinearRgba>,
}

/// Add this [`Component`] to an entity to draw its [`Aabb`] component.
Expand All @@ -68,7 +68,7 @@ pub struct ShowAabbGizmo {
/// The color of the box.
///
/// The default color from the [`AabbGizmoConfigGroup`] config is used if `None`,
pub color: Option<Color>,
pub color: Option<LinearRgba>,
}

fn draw_aabbs(
Expand Down Expand Up @@ -97,7 +97,7 @@ fn draw_all_aabbs(
}
}

fn color_from_entity(entity: Entity) -> Color {
fn color_from_entity(entity: Entity) -> LinearRgba {
Oklcha::sequential_dispersed(entity.index()).into()
}

Expand Down
32 changes: 16 additions & 16 deletions crates/bevy_gizmos/src/arcs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

use crate::circles::DEFAULT_CIRCLE_SEGMENTS;
use crate::prelude::{GizmoConfigGroup, Gizmos};
use bevy_color::Color;
use bevy_color::LinearRgba;
use bevy_math::{Quat, Vec2, Vec3};
use std::f32::consts::TAU;

Expand Down Expand Up @@ -36,12 +36,12 @@ where
/// # use std::f32::consts::PI;
/// # use bevy_color::palettes::basic::{GREEN, RED};
/// fn system(mut gizmos: Gizmos) {
/// gizmos.arc_2d(Vec2::ZERO, 0., PI / 4., 1., GREEN);
/// gizmos.arc_2d(Vec2::ZERO, 0., PI / 4., 1., GREEN.into());
///
/// // Arcs have 32 line-segments by default.
/// // You may want to increase this for larger arcs.
/// gizmos
/// .arc_2d(Vec2::ZERO, 0., PI / 4., 5., RED)
/// .arc_2d(Vec2::ZERO, 0., PI / 4., 5., RED.into())
/// .segments(64);
/// }
/// # bevy_ecs::system::assert_is_system(system);
Expand All @@ -53,15 +53,15 @@ where
direction_angle: f32,
arc_angle: f32,
radius: f32,
color: impl Into<Color>,
color: LinearRgba,
) -> Arc2dBuilder<'_, 'w, 's, Config, Clear> {
Arc2dBuilder {
gizmos: self,
position,
direction_angle,
arc_angle,
radius,
color: color.into(),
color,
segments: None,
}
}
Expand All @@ -78,7 +78,7 @@ where
direction_angle: f32,
arc_angle: f32,
radius: f32,
color: Color,
color: LinearRgba,
segments: Option<usize>,
}

Expand Down Expand Up @@ -175,7 +175,7 @@ where
/// 0.25,
/// Vec3::ONE,
/// rotation,
/// ORANGE
/// ORANGE.into(),
/// )
/// .segments(100);
/// }
Expand All @@ -188,7 +188,7 @@ where
radius: f32,
position: Vec3,
rotation: Quat,
color: impl Into<Color>,
color: LinearRgba,
) -> Arc3dBuilder<'_, 'w, 's, Config, Clear> {
Arc3dBuilder {
gizmos: self,
Expand All @@ -197,7 +197,7 @@ where
rotation,
angle,
radius,
color: color.into(),
color,
segments: None,
}
}
Expand Down Expand Up @@ -226,7 +226,7 @@ where
/// Vec3::ONE,
/// Vec3::ONE + Vec3::NEG_ONE,
/// Vec3::ZERO,
/// ORANGE
/// ORANGE.into(),
/// )
/// .segments(100);
/// }
Expand All @@ -245,7 +245,7 @@ where
center: Vec3,
from: Vec3,
to: Vec3,
color: impl Into<Color>,
color: LinearRgba,
) -> Arc3dBuilder<'_, 'w, 's, Config, Clear> {
self.arc_from_to(center, from, to, color, |x| x)
}
Expand Down Expand Up @@ -273,7 +273,7 @@ where
/// Vec3::ONE,
/// Vec3::ONE + Vec3::NEG_ONE,
/// Vec3::ZERO,
/// ORANGE
/// ORANGE.into(),
/// )
/// .segments(100);
/// }
Expand All @@ -292,7 +292,7 @@ where
center: Vec3,
from: Vec3,
to: Vec3,
color: impl Into<Color>,
color: LinearRgba,
) -> Arc3dBuilder<'_, 'w, 's, Config, Clear> {
self.arc_from_to(center, from, to, color, |angle| {
if angle > 0.0 {
Expand All @@ -311,7 +311,7 @@ where
center: Vec3,
from: Vec3,
to: Vec3,
color: impl Into<Color>,
color: LinearRgba,
angle_fn: impl Fn(f32) -> f32,
) -> Arc3dBuilder<'_, 'w, 's, Config, Clear> {
// `from` and `to` can be the same here since in either case nothing gets rendered and the
Expand All @@ -333,7 +333,7 @@ where
rotation,
angle,
radius,
color: color.into(),
color,
segments: None,
}
}
Expand All @@ -360,7 +360,7 @@ where
rotation: Quat,
angle: f32,
radius: f32,
color: Color,
color: LinearRgba,
segments: Option<usize>,
}

Expand Down
22 changes: 11 additions & 11 deletions crates/bevy_gizmos/src/arrows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use crate::prelude::{GizmoConfigGroup, Gizmos};
use bevy_color::{
palettes::basic::{BLUE, GREEN, RED},
Color,
LinearRgba,
};
use bevy_math::{Quat, Vec2, Vec3};
use bevy_transform::TransformPoint;
Expand All @@ -20,7 +20,7 @@ where
gizmos: &'a mut Gizmos<'w, 's, Config, Clear>,
start: Vec3,
end: Vec3,
color: Color,
color: LinearRgba,
double_ended: bool,
tip_length: f32,
}
Expand All @@ -40,7 +40,7 @@ where
/// # use bevy_math::prelude::*;
/// # use bevy_color::palettes::basic::GREEN;
/// fn system(mut gizmos: Gizmos) {
/// gizmos.arrow(Vec3::ZERO, Vec3::ONE, GREEN)
/// gizmos.arrow(Vec3::ZERO, Vec3::ONE, GREEN.into())
/// .with_tip_length(3.);
/// }
/// # bevy_ecs::system::assert_is_system(system);
Expand Down Expand Up @@ -118,22 +118,22 @@ where
/// # use bevy_math::prelude::*;
/// # use bevy_color::palettes::basic::GREEN;
/// fn system(mut gizmos: Gizmos) {
/// gizmos.arrow(Vec3::ZERO, Vec3::ONE, GREEN);
/// gizmos.arrow(Vec3::ZERO, Vec3::ONE, GREEN.into());
/// }
/// # bevy_ecs::system::assert_is_system(system);
/// ```
pub fn arrow(
&mut self,
start: Vec3,
end: Vec3,
color: impl Into<Color>,
color: LinearRgba,
) -> ArrowBuilder<'_, 'w, 's, Config, Clear> {
let length = (end - start).length();
ArrowBuilder {
gizmos: self,
start,
end,
color: color.into(),
color,
double_ended: false,
tip_length: length / 10.,
}
Expand All @@ -150,15 +150,15 @@ where
/// # use bevy_math::prelude::*;
/// # use bevy_color::palettes::basic::GREEN;
/// fn system(mut gizmos: Gizmos) {
/// gizmos.arrow_2d(Vec2::ZERO, Vec2::X, GREEN);
/// gizmos.arrow_2d(Vec2::ZERO, Vec2::X, GREEN.into());
/// }
/// # bevy_ecs::system::assert_is_system(system);
/// ```
pub fn arrow_2d(
&mut self,
start: Vec2,
end: Vec2,
color: impl Into<Color>,
color: LinearRgba,
) -> ArrowBuilder<'_, 'w, 's, Config, Clear> {
self.arrow(start.extend(0.), end.extend(0.), color)
}
Expand Down Expand Up @@ -197,8 +197,8 @@ where
let end_y = transform.transform_point(base_length * Vec3::Y);
let end_z = transform.transform_point(base_length * Vec3::Z);

self.arrow(start, end_x, RED);
self.arrow(start, end_y, GREEN);
self.arrow(start, end_z, BLUE);
self.arrow(start, end_x, RED.into());
self.arrow(start, end_y, GREEN.into());
self.arrow(start, end_z, BLUE.into());
}
}
Loading

0 comments on commit caebbaa

Please sign in to comment.