Consistency between Wireframe2d and Wireframe (#14720)

# Objective

- Wireframe plugins have inconsistencies between 3D and 2D versions.
This PR addresses the following
  - 2d version uses `Srgba` for colors, 3d version uses `Color`.

  
## Solution

- This PR brings consistency by doing the following change
  - `Wireframe2d` now uses `Color` instead of `Srgba`

## Testing

- `wireframe_2d` and `wireframe` examples were verified and they work as
before.

---

## Migration Guide

- `Wireframe2dConfig`.`default_color` type is now `Color` instead of
`Srgba`. Use `.into()` to convert between them.
- `Wireframe2dColor`.`color` type is now `Color` instead of `Srgba`. Use
`.into()` to convert between them.
This commit is contained in:
eckz 2024-08-13 20:57:47 +02:00 committed by GitHub
parent 882973a528
commit 46e8c6b662
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 18 additions and 11 deletions

View file

@ -1,7 +1,7 @@
use crate::{Material2d, Material2dKey, Material2dPlugin, Mesh2dHandle}; use crate::{Material2d, Material2dKey, Material2dPlugin, Mesh2dHandle};
use bevy_app::{Plugin, Startup, Update}; use bevy_app::{Plugin, Startup, Update};
use bevy_asset::{load_internal_asset, Asset, Assets, Handle}; use bevy_asset::{load_internal_asset, Asset, Assets, Handle};
use bevy_color::{LinearRgba, Srgba}; use bevy_color::{Color, LinearRgba};
use bevy_ecs::prelude::*; use bevy_ecs::prelude::*;
use bevy_reflect::{std_traits::ReflectDefault, Reflect, TypePath}; use bevy_reflect::{std_traits::ReflectDefault, Reflect, TypePath};
use bevy_render::{ use bevy_render::{
@ -67,7 +67,7 @@ pub struct Wireframe2d;
#[derive(Component, Debug, Clone, Default, Reflect)] #[derive(Component, Debug, Clone, Default, Reflect)]
#[reflect(Component, Default)] #[reflect(Component, Default)]
pub struct Wireframe2dColor { pub struct Wireframe2dColor {
pub color: Srgba, pub color: Color,
} }
/// Disables wireframe rendering for any entity it is attached to. /// Disables wireframe rendering for any entity it is attached to.
@ -81,13 +81,13 @@ pub struct NoWireframe2d;
#[derive(Resource, Debug, Clone, Default, ExtractResource, Reflect)] #[derive(Resource, Debug, Clone, Default, ExtractResource, Reflect)]
#[reflect(Resource)] #[reflect(Resource)]
pub struct Wireframe2dConfig { pub struct Wireframe2dConfig {
/// Whether to show wireframes for all meshes. /// Whether to show wireframes for all 2D meshes.
/// Can be overridden for individual meshes by adding a [`Wireframe2d`] or [`NoWireframe2d`] component. /// Can be overridden for individual meshes by adding a [`Wireframe2d`] or [`NoWireframe2d`] component.
pub global: bool, pub global: bool,
/// If [`Self::global`] is set, any [`Entity`] that does not have a [`Wireframe2d`] component attached to it will have /// If [`Self::global`] is set, any [`Entity`] that does not have a [`Wireframe2d`] component attached to it will have
/// wireframes using this color. Otherwise, this will be the fallback color for any entity that has a [`Wireframe2d`], /// wireframes using this color. Otherwise, this will be the fallback color for any entity that has a [`Wireframe2d`],
/// but no [`Wireframe2dColor`]. /// but no [`Wireframe2dColor`].
pub default_color: Srgba, pub default_color: Color,
} }
#[derive(Resource)] #[derive(Resource)]

View file

@ -44,7 +44,7 @@ fn main() {
global: true, global: true,
// Controls the default color of all wireframes. Used as the default color for global wireframes. // Controls the default color of all wireframes. Used as the default color for global wireframes.
// Can be changed per mesh using the `Wireframe2dColor` component. // Can be changed per mesh using the `Wireframe2dColor` component.
default_color: WHITE, default_color: WHITE.into(),
}) })
.add_systems(Startup, setup) .add_systems(Startup, setup)
.add_systems(Update, update_colors) .add_systems(Update, update_colors)
@ -91,7 +91,9 @@ fn setup(
Wireframe2d, Wireframe2d,
// This lets you configure the wireframe color of this entity. // This lets you configure the wireframe color of this entity.
// If not set, this will use the color in `WireframeConfig` // If not set, this will use the color in `WireframeConfig`
Wireframe2dColor { color: GREEN }, Wireframe2dColor {
color: GREEN.into(),
},
)); ));
// Camera // Camera
@ -126,7 +128,8 @@ Wireframe2dConfig
------------- -------------
Global: {} Global: {}
Color: {:?}", Color: {:?}",
config.global, config.default_color, config.global,
config.default_color.to_srgba(),
); );
// Toggle showing a wireframe on all meshes // Toggle showing a wireframe on all meshes
@ -136,17 +139,21 @@ Color: {:?}",
// Toggle the global wireframe color // Toggle the global wireframe color
if keyboard_input.just_pressed(KeyCode::KeyX) { if keyboard_input.just_pressed(KeyCode::KeyX) {
config.default_color = if config.default_color == WHITE { config.default_color = if config.default_color == WHITE.into() {
RED RED.into()
} else { } else {
WHITE WHITE.into()
}; };
} }
// Toggle the color of a wireframe using `Wireframe2dColor` and not the global color // Toggle the color of a wireframe using `Wireframe2dColor` and not the global color
if keyboard_input.just_pressed(KeyCode::KeyC) { if keyboard_input.just_pressed(KeyCode::KeyC) {
for mut color in &mut wireframe_colors { for mut color in &mut wireframe_colors {
color.color = if color.color == GREEN { RED } else { GREEN }; color.color = if color.color == GREEN.into() {
RED.into()
} else {
GREEN.into()
};
} }
} }
} }