From 72c9907f184e014ff4b175dd0359f4374037f69c Mon Sep 17 00:00:00 2001 From: james-j-obrien Date: Mon, 1 Aug 2022 13:18:56 -0700 Subject: [PATCH 1/3] First pass at constifying bevy_ui --- crates/bevy_ui/src/geometry.rs | 34 ++++- crates/bevy_ui/src/ui_node.rs | 219 ++++++++++++++++++++++++++------- 2 files changed, 201 insertions(+), 52 deletions(-) diff --git a/crates/bevy_ui/src/geometry.rs b/crates/bevy_ui/src/geometry.rs index 620e96225def8..b84283cd182f5 100644 --- a/crates/bevy_ui/src/geometry.rs +++ b/crates/bevy_ui/src/geometry.rs @@ -120,7 +120,7 @@ use std::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Sub, SubAssign}; /// bottom: Val::Px(40.0), /// }; /// ``` -#[derive(Copy, Clone, PartialEq, Debug, Default, Reflect)] +#[derive(Copy, Clone, PartialEq, Debug, Reflect)] #[reflect(PartialEq)] pub struct UiRect { /// The value corresponding to the left side of the UI rect. @@ -134,6 +134,13 @@ pub struct UiRect { } impl UiRect { + pub const DEFAULT: Self = Self { + left: Val::DEFAULT, + right: Val::DEFAULT, + top: Val::DEFAULT, + bottom: Val::DEFAULT, + }; + /// Creates a new [`UiRect`] from the values specified. /// /// # Example @@ -153,7 +160,7 @@ impl UiRect { /// assert_eq!(ui_rect.top, Val::Px(30.0)); /// assert_eq!(ui_rect.bottom, Val::Px(40.0)); /// ``` - pub fn new(left: Val, right: Val, top: Val, bottom: Val) -> Self { + pub const fn new(left: Val, right: Val, top: Val, bottom: Val) -> Self { UiRect { left, right, @@ -176,7 +183,7 @@ impl UiRect { /// assert_eq!(ui_rect.top, Val::Px(10.0)); /// assert_eq!(ui_rect.bottom, Val::Px(10.0)); /// ``` - pub fn all(value: Val) -> Self { + pub const fn all(value: Val) -> Self { UiRect { left: value, right: value, @@ -186,10 +193,16 @@ impl UiRect { } } +impl Default for UiRect { + fn default() -> Self { + Self::DEFAULT + } +} + /// A 2-dimensional area defined by a width and height. /// /// It is commonly used to define the size of a text or UI element. -#[derive(Copy, Clone, PartialEq, Debug, Default, Reflect)] +#[derive(Copy, Clone, PartialEq, Debug, Reflect)] #[reflect(PartialEq)] pub struct Size { /// The width of the 2-dimensional area. @@ -199,6 +212,11 @@ pub struct Size { } impl Size { + pub const DEFAULT: Self = Self { + width: Val::DEFAULT, + height: Val::DEFAULT, + }; + /// Creates a new [`Size`] from a width and a height. /// /// # Example @@ -211,11 +229,17 @@ impl Size { /// assert_eq!(size.width, Val::Px(100.0)); /// assert_eq!(size.height, Val::Px(200.0)); /// ``` - pub fn new(width: Val, height: Val) -> Self { + pub const fn new(width: Val, height: Val) -> Self { Size { width, height } } } +impl Default for Size { + fn default() -> Self { + Self::DEFAULT + } +} + impl Add for Size { type Output = Size; diff --git a/crates/bevy_ui/src/ui_node.rs b/crates/bevy_ui/src/ui_node.rs index 4483e03aa7ebf..3a58d8652cd0a 100644 --- a/crates/bevy_ui/src/ui_node.rs +++ b/crates/bevy_ui/src/ui_node.rs @@ -12,19 +12,28 @@ use serde::{Deserialize, Serialize}; use std::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Sub, SubAssign}; /// Describes the size of a UI node -#[derive(Component, Debug, Clone, Default, Reflect)] +#[derive(Component, Debug, Clone, Reflect)] #[reflect(Component, Default)] pub struct Node { /// The size of the node as width and height in pixels pub size: Vec2, } +impl Node { + pub const DEFAULT: Self = Self { size: Vec2::ZERO }; +} + +impl Default for Node { + fn default() -> Self { + Self::DEFAULT + } +} + /// An enum that describes possible types of value in flexbox layout options -#[derive(Copy, Clone, PartialEq, Debug, Default, Serialize, Deserialize, Reflect)] +#[derive(Copy, Clone, PartialEq, Debug, Serialize, Deserialize, Reflect)] #[reflect_value(PartialEq, Serialize, Deserialize)] pub enum Val { /// No value defined - #[default] Undefined, /// Automatically determine this value Auto, @@ -34,6 +43,16 @@ pub enum Val { Percent(f32), } +impl Val { + pub const DEFAULT: Self = Self::Undefined; +} + +impl Default for Val { + fn default() -> Self { + Self::DEFAULT + } +} + impl Add for Val { type Output = Val; @@ -178,36 +197,40 @@ pub struct Style { pub overflow: Overflow, } +impl Style { + pub const DEFAULT: Self = Self { + display: Display::DEFAULT, + position_type: PositionType::DEFAULT, + direction: Direction::DEFAULT, + flex_direction: FlexDirection::DEFAULT, + flex_wrap: FlexWrap::DEFAULT, + align_items: AlignItems::DEFAULT, + align_self: AlignSelf::DEFAULT, + align_content: AlignContent::DEFAULT, + justify_content: JustifyContent::DEFAULT, + position: UiRect::DEFAULT, + margin: UiRect::DEFAULT, + padding: UiRect::DEFAULT, + border: UiRect::DEFAULT, + flex_grow: 0.0, + flex_shrink: 1.0, + flex_basis: Val::Auto, + size: Size::new(Val::Auto, Val::Auto), + min_size: Size::new(Val::Auto, Val::Auto), + max_size: Size::new(Val::Auto, Val::Auto), + aspect_ratio: None, + overflow: Overflow::DEFAULT, + }; +} + impl Default for Style { fn default() -> Self { - Self { - display: Default::default(), - position_type: Default::default(), - direction: Default::default(), - flex_direction: Default::default(), - flex_wrap: Default::default(), - align_items: Default::default(), - align_self: Default::default(), - align_content: Default::default(), - justify_content: Default::default(), - position: Default::default(), - margin: Default::default(), - padding: Default::default(), - border: Default::default(), - flex_grow: 0.0, - flex_shrink: 1.0, - flex_basis: Val::Auto, - size: Size::new(Val::Auto, Val::Auto), - min_size: Size::new(Val::Auto, Val::Auto), - max_size: Size::new(Val::Auto, Val::Auto), - aspect_ratio: Default::default(), - overflow: Default::default(), - } + Self::DEFAULT } } /// How items are aligned according to the cross axis -#[derive(Copy, Clone, PartialEq, Eq, Debug, Default, Serialize, Deserialize, Reflect)] +#[derive(Copy, Clone, PartialEq, Eq, Debug, Serialize, Deserialize, Reflect)] #[reflect_value(PartialEq, Serialize, Deserialize)] pub enum AlignItems { /// Items are aligned at the start @@ -219,16 +242,24 @@ pub enum AlignItems { /// Items are aligned at the baseline Baseline, /// Items are stretched across the whole cross axis - #[default] Stretch, } +impl AlignItems { + pub const DEFAULT: Self = Self::Stretch; +} + +impl Default for AlignItems { + fn default() -> Self { + Self::DEFAULT + } +} + /// Works like [`AlignItems`] but applies only to a single item -#[derive(Copy, Clone, PartialEq, Eq, Debug, Default, Serialize, Deserialize, Reflect)] +#[derive(Copy, Clone, PartialEq, Eq, Debug, Serialize, Deserialize, Reflect)] #[reflect_value(PartialEq, Serialize, Deserialize)] pub enum AlignSelf { /// Use the value of [`AlignItems`] - #[default] Auto, /// If the parent has [`AlignItems::Center`] only this item will be at the start FlexStart, @@ -242,10 +273,20 @@ pub enum AlignSelf { Stretch, } +impl AlignSelf { + pub const DEFAULT: Self = Self::Auto; +} + +impl Default for AlignSelf { + fn default() -> Self { + Self::DEFAULT + } +} + /// Defines how each line is aligned within the flexbox. /// /// It only applies if [`FlexWrap::Wrap`] is present and if there are multiple lines of items. -#[derive(Copy, Clone, PartialEq, Eq, Debug, Default, Serialize, Deserialize, Reflect)] +#[derive(Copy, Clone, PartialEq, Eq, Debug, Serialize, Deserialize, Reflect)] #[reflect_value(PartialEq, Serialize, Deserialize)] pub enum AlignContent { /// Each line moves towards the start of the cross axis @@ -255,7 +296,6 @@ pub enum AlignContent { /// Each line moves towards the center of the cross axis Center, /// Each line will stretch to fill the remaining space - #[default] Stretch, /// Each line fills the space it needs, putting the remaining space, if any /// inbetween the lines @@ -265,14 +305,23 @@ pub enum AlignContent { SpaceAround, } +impl AlignContent { + pub const DEFAULT: Self = Self::Stretch; +} + +impl Default for AlignContent { + fn default() -> Self { + Self::DEFAULT + } +} + /// Defines the text direction /// /// For example English is written LTR (left-to-right) while Arabic is written RTL (right-to-left). -#[derive(Copy, Clone, PartialEq, Eq, Debug, Default, Serialize, Deserialize, Reflect)] +#[derive(Copy, Clone, PartialEq, Eq, Debug, Serialize, Deserialize, Reflect)] #[reflect_value(PartialEq, Serialize, Deserialize)] pub enum Direction { /// Inherit from parent node - #[default] Inherit, /// Text is written left to right LeftToRight, @@ -280,14 +329,23 @@ pub enum Direction { RightToLeft, } +impl Direction { + pub const DEFAULT: Self = Self::Inherit; +} + +impl Default for Direction { + fn default() -> Self { + Self::DEFAULT + } +} + /// Whether to use a Flexbox layout model. /// /// Part of the [`Style`] component. -#[derive(Copy, Clone, PartialEq, Eq, Debug, Default, Serialize, Deserialize, Reflect)] +#[derive(Copy, Clone, PartialEq, Eq, Debug, Serialize, Deserialize, Reflect)] #[reflect_value(PartialEq, Serialize, Deserialize)] pub enum Display { /// Use Flexbox layout model to determine the position of this [`Node`]. - #[default] Flex, /// Use no layout, don't render this node and its children. /// @@ -296,12 +354,21 @@ pub enum Display { None, } +impl Display { + pub const DEFAULT: Self = Self::Flex; +} + +impl Default for Display { + fn default() -> Self { + Self::DEFAULT + } +} + /// Defines how flexbox items are ordered within a flexbox -#[derive(Copy, Clone, PartialEq, Eq, Debug, Default, Serialize, Deserialize, Reflect)] +#[derive(Copy, Clone, PartialEq, Eq, Debug, Serialize, Deserialize, Reflect)] #[reflect_value(PartialEq, Serialize, Deserialize)] pub enum FlexDirection { /// Same way as text direction along the main axis - #[default] Row, /// Flex from bottom to top Column, @@ -311,12 +378,21 @@ pub enum FlexDirection { ColumnReverse, } +impl FlexDirection { + pub const DEFAULT: Self = Self::Row; +} + +impl Default for FlexDirection { + fn default() -> Self { + Self::DEFAULT + } +} + /// Defines how items are aligned according to the main axis -#[derive(Copy, Clone, PartialEq, Eq, Debug, Default, Serialize, Deserialize, Reflect)] +#[derive(Copy, Clone, PartialEq, Eq, Debug, Serialize, Deserialize, Reflect)] #[reflect_value(PartialEq, Serialize, Deserialize)] pub enum JustifyContent { /// Pushed towards the start - #[default] FlexStart, /// Pushed towards the end FlexEnd, @@ -330,23 +406,41 @@ pub enum JustifyContent { SpaceEvenly, } +impl JustifyContent { + pub const DEFAULT: Self = Self::FlexStart; +} + +impl Default for JustifyContent { + fn default() -> Self { + Self::DEFAULT + } +} + /// Whether to show or hide overflowing items -#[derive(Copy, Clone, PartialEq, Eq, Debug, Default, Reflect, Serialize, Deserialize)] +#[derive(Copy, Clone, PartialEq, Eq, Debug, Reflect, Serialize, Deserialize)] #[reflect_value(PartialEq, Serialize, Deserialize)] pub enum Overflow { /// Show overflowing items - #[default] Visible, /// Hide overflowing items Hidden, } +impl Overflow { + pub const DEFAULT: Self = Self::Visible; +} + +impl Default for Overflow { + fn default() -> Self { + Self::DEFAULT + } +} + /// The strategy used to position this node -#[derive(Copy, Clone, PartialEq, Eq, Debug, Default, Serialize, Deserialize, Reflect)] +#[derive(Copy, Clone, PartialEq, Eq, Debug, Serialize, Deserialize, Reflect)] #[reflect_value(PartialEq, Serialize, Deserialize)] pub enum PositionType { /// Relative to all other nodes with the [`PositionType::Relative`] value - #[default] Relative, /// Independent of all other nodes /// @@ -354,12 +448,21 @@ pub enum PositionType { Absolute, } +impl PositionType { + const DEFAULT: Self = Self::Relative; +} + +impl Default for PositionType { + fn default() -> Self { + Self::DEFAULT + } +} + /// Defines if flexbox items appear on a single line or on multiple lines -#[derive(Copy, Clone, PartialEq, Eq, Debug, Default, Serialize, Deserialize, Reflect)] +#[derive(Copy, Clone, PartialEq, Eq, Debug, Serialize, Deserialize, Reflect)] #[reflect_value(PartialEq, Serialize, Deserialize)] pub enum FlexWrap { /// Single line, will overflow if needed - #[default] NoWrap, /// Multiple lines, if needed Wrap, @@ -367,14 +470,36 @@ pub enum FlexWrap { WrapReverse, } +impl FlexWrap { + const DEFAULT: Self = Self::NoWrap; +} + +impl Default for FlexWrap { + fn default() -> Self { + Self::DEFAULT + } +} + /// The calculated size of the node -#[derive(Component, Default, Copy, Clone, Debug, Reflect)] +#[derive(Component, Copy, Clone, Debug, Reflect)] #[reflect(Component)] pub struct CalculatedSize { /// The size of the node pub size: Size, } +impl CalculatedSize { + const DEFAULT: Self = Self { + size: Size::DEFAULT, + }; +} + +impl Default for CalculatedSize { + fn default() -> Self { + Self::DEFAULT + } +} + /// The color of the node #[derive(Component, Default, Copy, Clone, Debug, Reflect)] #[reflect(Component, Default)] From f52543b661fc918ef0647c32778f7eb2201e9926 Mon Sep 17 00:00:00 2001 From: james-j-obrien Date: Mon, 1 Aug 2022 13:35:59 -0700 Subject: [PATCH 2/3] Add missing const for UiColor --- crates/bevy_ui/src/ui_node.rs | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/crates/bevy_ui/src/ui_node.rs b/crates/bevy_ui/src/ui_node.rs index 3a58d8652cd0a..6085c2a5150ce 100644 --- a/crates/bevy_ui/src/ui_node.rs +++ b/crates/bevy_ui/src/ui_node.rs @@ -501,10 +501,20 @@ impl Default for CalculatedSize { } /// The color of the node -#[derive(Component, Default, Copy, Clone, Debug, Reflect)] +#[derive(Component, Copy, Clone, Debug, Reflect)] #[reflect(Component, Default)] pub struct UiColor(pub Color); +impl UiColor { + pub const DEFAULT: Self = Self(Color::WHITE); +} + +impl Default for UiColor { + fn default() -> Self { + Self::DEFAULT + } +} + impl From for UiColor { fn from(color: Color) -> Self { Self(color) From 226ccb56c992ebc44f3bae929d07ae22d48a2f07 Mon Sep 17 00:00:00 2001 From: james-j-obrien Date: Mon, 1 Aug 2022 13:43:08 -0700 Subject: [PATCH 3/3] Add defaults to focus.rs --- crates/bevy_ui/src/focus.rs | 31 +++++++++++++++++++++++-------- 1 file changed, 23 insertions(+), 8 deletions(-) diff --git a/crates/bevy_ui/src/focus.rs b/crates/bevy_ui/src/focus.rs index b9ef20069e9a3..83ade51055455 100644 --- a/crates/bevy_ui/src/focus.rs +++ b/crates/bevy_ui/src/focus.rs @@ -30,9 +30,7 @@ use smallvec::SmallVec; /// /// Note that you can also control the visibility of a node using the [`Display`](crate::ui_node::Display) property, /// which fully collapses it during layout calculations. -#[derive( - Component, Copy, Clone, Default, Eq, PartialEq, Debug, Reflect, Serialize, Deserialize, -)] +#[derive(Component, Copy, Clone, Eq, PartialEq, Debug, Reflect, Serialize, Deserialize)] #[reflect_value(Component, Serialize, Deserialize, PartialEq)] pub enum Interaction { /// The node has been clicked @@ -40,22 +38,39 @@ pub enum Interaction { /// The node has been hovered over Hovered, /// Nothing has happened - #[default] None, } +impl Interaction { + const DEFAULT: Self = Self::None; +} + +impl Default for Interaction { + fn default() -> Self { + Self::DEFAULT + } +} + /// Describes whether the node should block interactions with lower nodes -#[derive( - Component, Copy, Clone, Default, Eq, PartialEq, Debug, Reflect, Serialize, Deserialize, -)] +#[derive(Component, Copy, Clone, Eq, PartialEq, Debug, Reflect, Serialize, Deserialize)] #[reflect_value(Component, Serialize, Deserialize, PartialEq)] pub enum FocusPolicy { /// Blocks interaction - #[default] Block, /// Lets interaction pass through Pass, } + +impl FocusPolicy { + const DEFAULT: Self = Self::Block; +} + +impl Default for FocusPolicy { + fn default() -> Self { + Self::DEFAULT + } +} + /// Contains entities whose Interaction should be set to None #[derive(Default)] pub struct State {