From fc4716f56c0a5ace207e81e0add956e1452e2f2a Mon Sep 17 00:00:00 2001 From: Andrew Date: Sun, 17 Mar 2024 23:41:50 -0400 Subject: [PATCH] feat: derive some common traits on some UI types (#12532) # Objective - working with UI components in Bevy, I found myself wanting some of these common traits, like `PartialEq` for comparing simple types ## Solution - I added only (hopefully) uncontroversial `derive`s for some common UI types Note that many types, unfortunately, can't have `PartialEq` `derive`d for them, because they contain `f32`s and / or `Vec`s. --- crates/bevy_text/src/text.rs | 15 +++------------ crates/bevy_ui/src/ui_node.rs | 8 ++++---- crates/bevy_ui/src/widget/button.rs | 2 +- 3 files changed, 8 insertions(+), 17 deletions(-) diff --git a/crates/bevy_text/src/text.rs b/crates/bevy_text/src/text.rs index b0a3dc7604..34384e312b 100644 --- a/crates/bevy_text/src/text.rs +++ b/crates/bevy_text/src/text.rs @@ -7,7 +7,7 @@ use serde::{Deserialize, Serialize}; use crate::Font; -#[derive(Component, Debug, Clone, Reflect)] +#[derive(Component, Debug, Clone, Default, Reflect)] #[reflect(Component, Default)] pub struct Text { pub sections: Vec, @@ -18,16 +18,6 @@ pub struct Text { pub linebreak_behavior: BreakLineOn, } -impl Default for Text { - fn default() -> Self { - Self { - sections: Default::default(), - justify: JustifyText::Left, - linebreak_behavior: BreakLineOn::WordBoundary, - } - } -} - impl Text { /// Constructs a [`Text`] with a single section. /// @@ -219,12 +209,13 @@ impl Default for TextStyle { } /// Determines how lines will be broken when preventing text from running out of bounds. -#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Reflect, Serialize, Deserialize)] +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default, Reflect, Serialize, Deserialize)] #[reflect(Serialize, Deserialize)] pub enum BreakLineOn { /// Uses the [Unicode Line Breaking Algorithm](https://www.unicode.org/reports/tr14/). /// Lines will be broken up at the nearest suitable word boundary, usually a space. /// This behavior suits most cases, as it keeps words intact across linebreaks. + #[default] WordBoundary, /// Lines will be broken without discrimination on any character that would leave bounds. /// This is closer to the behavior one might expect from text in a terminal. diff --git a/crates/bevy_ui/src/ui_node.rs b/crates/bevy_ui/src/ui_node.rs index 36db16ca60..f7b77149e6 100644 --- a/crates/bevy_ui/src/ui_node.rs +++ b/crates/bevy_ui/src/ui_node.rs @@ -23,7 +23,7 @@ use thiserror::Error; /// - [`RelativeCursorPosition`](crate::RelativeCursorPosition) /// to obtain the cursor position relative to this node /// - [`Interaction`](crate::Interaction) to obtain the interaction state of this node -#[derive(Component, Debug, Copy, Clone, Reflect)] +#[derive(Component, Debug, Copy, Clone, PartialEq, Reflect)] #[reflect(Component, Default)] pub struct Node { /// The order of the node in the UI layout. @@ -1590,7 +1590,7 @@ pub enum GridPlacementError { /// The background color of the node /// /// This serves as the "fill" color. -#[derive(Component, Copy, Clone, Debug, Reflect)] +#[derive(Component, Copy, Clone, Debug, PartialEq, Reflect)] #[reflect(Component, Default)] #[cfg_attr( feature = "serialize", @@ -1616,7 +1616,7 @@ impl> From for BackgroundColor { } /// The border color of the UI node. -#[derive(Component, Copy, Clone, Debug, Reflect)] +#[derive(Component, Copy, Clone, Debug, PartialEq, Reflect)] #[reflect(Component, Default)] #[cfg_attr( feature = "serialize", @@ -1796,7 +1796,7 @@ pub struct CalculatedClip { /// `ZIndex::Local(n)` and `ZIndex::Global(n)` for root nodes. /// /// Nodes without this component will be treated as if they had a value of `ZIndex::Local(0)`. -#[derive(Component, Copy, Clone, Debug, Reflect)] +#[derive(Component, Copy, Clone, Debug, PartialEq, Eq, Reflect)] #[reflect(Component, Default)] pub enum ZIndex { /// Indicates the order in which this node should be rendered relative to its siblings. diff --git a/crates/bevy_ui/src/widget/button.rs b/crates/bevy_ui/src/widget/button.rs index 6c7dced0f3..d28c9ce5cd 100644 --- a/crates/bevy_ui/src/widget/button.rs +++ b/crates/bevy_ui/src/widget/button.rs @@ -4,6 +4,6 @@ use bevy_reflect::std_traits::ReflectDefault; use bevy_reflect::Reflect; /// Marker struct for buttons -#[derive(Component, Debug, Default, Clone, Copy, Reflect)] +#[derive(Component, Debug, Default, Clone, Copy, PartialEq, Eq, Reflect)] #[reflect(Component, Default)] pub struct Button;