From 45bbe8900067c686d561d8fe3632bb5dcb7f90a1 Mon Sep 17 00:00:00 2001 From: pablo-lua Date: Tue, 2 Jan 2024 22:19:15 -0300 Subject: [PATCH 1/2] [cargo] Added Serialize feature --- crates/bevy_internal/Cargo.toml | 1 + crates/bevy_ui/Cargo.toml | 5 ++++- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/crates/bevy_internal/Cargo.toml b/crates/bevy_internal/Cargo.toml index 0563bb6a2122f..709d78179e8a2 100644 --- a/crates/bevy_internal/Cargo.toml +++ b/crates/bevy_internal/Cargo.toml @@ -69,6 +69,7 @@ serialize = [ "bevy_transform/serialize", "bevy_math/serialize", "bevy_scene?/serialize", + "bevy_ui?/serialize", ] multi-threaded = [ "bevy_asset/multi-threaded", diff --git a/crates/bevy_ui/Cargo.toml b/crates/bevy_ui/Cargo.toml index 40ff1a78efdd6..92c985fa8103c 100644 --- a/crates/bevy_ui/Cargo.toml +++ b/crates/bevy_ui/Cargo.toml @@ -32,9 +32,12 @@ bevy_utils = { path = "../bevy_utils", version = "0.12.0" } # other taffy = { version = "0.3.10" } -serde = { version = "1", features = ["derive"] } +serde = { version = "1", features = ["derive"], optional = true } bytemuck = { version = "1.5", features = ["derive"] } thiserror = "1.0.0" +[features] +serialize = ["serde"] + [lints] workspace = true From 94c123c9105e0249d85589bac23ec138a74f0dbb Mon Sep 17 00:00:00 2001 From: pablo-lua Date: Tue, 2 Jan 2024 22:20:29 -0300 Subject: [PATCH 2/2] [alt] Changed so it only implement serialize on feature --- crates/bevy_ui/src/focus.rs | 33 +++-- crates/bevy_ui/src/geometry.rs | 25 ++-- crates/bevy_ui/src/ui_node.rs | 217 +++++++++++++++++++++++++-------- 3 files changed, 210 insertions(+), 65 deletions(-) diff --git a/crates/bevy_ui/src/focus.rs b/crates/bevy_ui/src/focus.rs index c578be4cb6cd2..36ebfc18022ad 100644 --- a/crates/bevy_ui/src/focus.rs +++ b/crates/bevy_ui/src/focus.rs @@ -9,13 +9,15 @@ use bevy_ecs::{ }; use bevy_input::{mouse::MouseButton, touch::Touches, ButtonInput}; use bevy_math::{Rect, Vec2}; -use bevy_reflect::{Reflect, ReflectDeserialize, ReflectSerialize}; +use bevy_reflect::Reflect; use bevy_render::{camera::NormalizedRenderTarget, prelude::Camera, view::ViewVisibility}; use bevy_transform::components::GlobalTransform; use bevy_utils::smallvec::SmallVec; use bevy_window::{PrimaryWindow, Window}; -use serde::{Deserialize, Serialize}; + +#[cfg(feature = "serialize")] +use bevy_reflect::{ReflectDeserialize, ReflectSerialize}; /// Describes what type of input interaction has occurred for a UI node. /// @@ -31,8 +33,13 @@ use serde::{Deserialize, Serialize}; /// /// 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, Eq, PartialEq, Debug, Reflect, Serialize, Deserialize)] -#[reflect(Component, Serialize, Deserialize, PartialEq)] +#[derive(Component, Copy, Clone, Eq, PartialEq, Debug, Reflect)] +#[reflect(Component, PartialEq)] +#[cfg_attr( + feature = "serialize", + derive(serde::Serialize, serde::Deserialize), + reflect(Serialize, Deserialize) +)] pub enum Interaction { /// The node has been pressed. /// @@ -59,8 +66,13 @@ impl Default for Interaction { /// /// It can be used alongside interaction to get the position of the press. -#[derive(Component, Copy, Clone, Default, PartialEq, Debug, Reflect, Serialize, Deserialize)] -#[reflect(Component, Serialize, Deserialize, PartialEq)] +#[derive(Component, Copy, Clone, Default, PartialEq, Debug, Reflect)] +#[reflect(Component, PartialEq)] +#[cfg_attr( + feature = "serialize", + derive(serde::Serialize, serde::Deserialize), + reflect(Serialize, Deserialize) +)] pub struct RelativeCursorPosition { /// Visible area of the Node relative to the size of the entire Node. pub normalized_visible_node_rect: Rect, @@ -79,8 +91,13 @@ impl RelativeCursorPosition { } /// Describes whether the node should block interactions with lower nodes -#[derive(Component, Copy, Clone, Eq, PartialEq, Debug, Reflect, Serialize, Deserialize)] -#[reflect(Component, Serialize, Deserialize, PartialEq)] +#[derive(Component, Copy, Clone, Eq, PartialEq, Debug, Reflect)] +#[reflect(Component, PartialEq)] +#[cfg_attr( + feature = "serialize", + derive(serde::Serialize, serde::Deserialize), + reflect(Serialize, Deserialize) +)] pub enum FocusPolicy { /// Blocks interaction Block, diff --git a/crates/bevy_ui/src/geometry.rs b/crates/bevy_ui/src/geometry.rs index 9462ef4e770d5..bf970f4cceb7c 100644 --- a/crates/bevy_ui/src/geometry.rs +++ b/crates/bevy_ui/src/geometry.rs @@ -1,19 +1,23 @@ use bevy_math::Vec2; use bevy_reflect::Reflect; -use bevy_reflect::ReflectDeserialize; -use bevy_reflect::ReflectSerialize; -use serde::Deserialize; -use serde::Serialize; use std::ops::Neg; use std::ops::{Div, DivAssign, Mul, MulAssign}; use thiserror::Error; +#[cfg(feature = "serialize")] +use bevy_reflect::{ReflectDeserialize, ReflectSerialize}; + /// Represents the possible value types for layout properties. /// /// This enum allows specifying values for various [`Style`](crate::Style) properties in different units, /// such as logical pixels, percentages, or automatically determined values. -#[derive(Copy, Clone, Debug, Serialize, Deserialize, Reflect)] -#[reflect(PartialEq, Serialize, Deserialize)] +#[derive(Copy, Clone, Debug, Reflect)] +#[reflect(PartialEq)] +#[cfg_attr( + feature = "serialize", + derive(serde::Serialize, serde::Deserialize), + reflect(Serialize, Deserialize) +)] pub enum Val { /// Automatically determine the value based on the context and other [`Style`](crate::Style) properties. Auto, @@ -241,8 +245,13 @@ impl Val { /// bottom: Val::Px(40.0), /// }; /// ``` -#[derive(Copy, Clone, PartialEq, Debug, Serialize, Deserialize, Reflect)] -#[reflect(PartialEq, Serialize, Deserialize)] +#[derive(Copy, Clone, PartialEq, Debug, Reflect)] +#[reflect(PartialEq)] +#[cfg_attr( + feature = "serialize", + derive(serde::Serialize, serde::Deserialize), + reflect(Serialize, Deserialize) +)] pub struct UiRect { /// The value corresponding to the left side of the UI rect. pub left: Val, diff --git a/crates/bevy_ui/src/ui_node.rs b/crates/bevy_ui/src/ui_node.rs index aa4661f4ab97b..773da577a5b2e 100644 --- a/crates/bevy_ui/src/ui_node.rs +++ b/crates/bevy_ui/src/ui_node.rs @@ -6,7 +6,6 @@ use bevy_reflect::prelude::*; use bevy_render::{color::Color, texture::Image}; use bevy_transform::prelude::GlobalTransform; use bevy_utils::smallvec::SmallVec; -use serde::{Deserialize, Serialize}; use std::num::{NonZeroI16, NonZeroU16}; use thiserror::Error; @@ -133,8 +132,13 @@ impl Default for Node { /// - [A Complete Guide To CSS Grid](https://css-tricks.com/snippets/css/complete-guide-grid/) by CSS Tricks. This is detailed guide with illustrations and comprehensive written explanation of the different CSS Grid properties and how they work. /// - [CSS Grid Garden](https://cssgridgarden.com/). An interactive tutorial/game that teaches the essential parts of CSS Grid in a fun engaging way. -#[derive(Component, Clone, PartialEq, Debug, Deserialize, Serialize, Reflect)] -#[reflect(Component, Default, PartialEq, Deserialize, Serialize)] +#[derive(Component, Clone, PartialEq, Debug, Reflect)] +#[reflect(Component, Default, PartialEq)] +#[cfg_attr( + feature = "serialize", + derive(serde::Serialize, serde::Deserialize), + reflect(Serialize, Deserialize) +)] pub struct Style { /// Which layout algorithm to use when laying out this node's contents: /// - [`Display::Flex`]: Use the Flexbox layout algorithm @@ -464,8 +468,13 @@ impl Default for Style { /// - For CSS Grid containers, controls block (vertical) axis alignment of children of this grid container within their grid areas. /// /// -#[derive(Copy, Clone, PartialEq, Eq, Debug, Serialize, Deserialize, Reflect)] -#[reflect(PartialEq, Serialize, Deserialize)] +#[derive(Copy, Clone, PartialEq, Eq, Debug, Reflect)] +#[reflect(PartialEq)] +#[cfg_attr( + feature = "serialize", + derive(serde::Serialize, serde::Deserialize), + reflect(Serialize, Deserialize) +)] pub enum AlignItems { /// The items are packed in their default position as if no alignment was applied. Default, @@ -502,8 +511,13 @@ impl Default for AlignItems { /// - For CSS Grid containers, sets default inline (horizontal) axis alignment of child items within their grid areas. /// /// -#[derive(Copy, Clone, PartialEq, Eq, Debug, Serialize, Deserialize, Reflect)] -#[reflect(PartialEq, Serialize, Deserialize)] +#[derive(Copy, Clone, PartialEq, Eq, Debug, Reflect)] +#[reflect(PartialEq)] +#[cfg_attr( + feature = "serialize", + derive(serde::Serialize, serde::Deserialize), + reflect(Serialize, Deserialize) +)] pub enum JustifyItems { /// The items are packed in their default position as if no alignment was applied. Default, @@ -534,8 +548,13 @@ impl Default for JustifyItems { /// - For CSS Grid items, controls block (vertical) axis alignment of a grid item within its grid area. /// /// -#[derive(Copy, Clone, PartialEq, Eq, Debug, Serialize, Deserialize, Reflect)] -#[reflect(PartialEq, Serialize, Deserialize)] +#[derive(Copy, Clone, PartialEq, Eq, Debug, Reflect)] +#[reflect(PartialEq)] +#[cfg_attr( + feature = "serialize", + derive(serde::Serialize, serde::Deserialize), + reflect(Serialize, Deserialize) +)] pub enum AlignSelf { /// Use the parent node's [`AlignItems`] value to determine how this item should be aligned. Auto, @@ -572,8 +591,13 @@ impl Default for AlignSelf { /// - For CSS Grid items, controls inline (horizontal) axis alignment of a grid item within its grid area. /// /// -#[derive(Copy, Clone, PartialEq, Eq, Debug, Serialize, Deserialize, Reflect)] -#[reflect(PartialEq, Serialize, Deserialize)] +#[derive(Copy, Clone, PartialEq, Eq, Debug, Reflect)] +#[reflect(PartialEq)] +#[cfg_attr( + feature = "serialize", + derive(serde::Serialize, serde::Deserialize), + reflect(Serialize, Deserialize) +)] pub enum JustifySelf { /// Use the parent node's [`JustifyItems`] value to determine how this item should be aligned. Auto, @@ -604,8 +628,13 @@ impl Default for JustifySelf { /// - For CSS Grid containers, controls alignment of grid rows. /// /// -#[derive(Copy, Clone, PartialEq, Eq, Debug, Serialize, Deserialize, Reflect)] -#[reflect(PartialEq, Serialize, Deserialize)] +#[derive(Copy, Clone, PartialEq, Eq, Debug, Reflect)] +#[reflect(PartialEq)] +#[cfg_attr( + feature = "serialize", + derive(serde::Serialize, serde::Deserialize), + reflect(Serialize, Deserialize) +)] pub enum AlignContent { /// The items are packed in their default position as if no alignment was applied. Default, @@ -646,8 +675,13 @@ impl Default for AlignContent { /// - For CSS Grid containers, controls alignment of grid columns. /// /// -#[derive(Copy, Clone, PartialEq, Eq, Debug, Serialize, Deserialize, Reflect)] -#[reflect(PartialEq, Serialize, Deserialize)] +#[derive(Copy, Clone, PartialEq, Eq, Debug, Reflect)] +#[reflect(PartialEq)] +#[cfg_attr( + feature = "serialize", + derive(serde::Serialize, serde::Deserialize), + reflect(Serialize, Deserialize) +)] pub enum JustifyContent { /// The items are packed in their default position as if no alignment was applied. Default, @@ -686,8 +720,13 @@ impl Default for JustifyContent { /// 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, Serialize, Deserialize, Reflect)] -#[reflect(PartialEq, Serialize, Deserialize)] +#[derive(Copy, Clone, PartialEq, Eq, Debug, Reflect)] +#[reflect(PartialEq)] +#[cfg_attr( + feature = "serialize", + derive(serde::Serialize, serde::Deserialize), + reflect(Serialize, Deserialize) +)] pub enum Direction { /// Inherit from parent node. Inherit, @@ -710,8 +749,13 @@ impl Default for Direction { /// Defines the layout model used by this node. /// /// Part of the [`Style`] component. -#[derive(Copy, Clone, PartialEq, Eq, Debug, Serialize, Deserialize, Reflect)] -#[reflect(PartialEq, Serialize, Deserialize)] +#[derive(Copy, Clone, PartialEq, Eq, Debug, Reflect)] +#[reflect(PartialEq)] +#[cfg_attr( + feature = "serialize", + derive(serde::Serialize, serde::Deserialize), + reflect(Serialize, Deserialize) +)] pub enum Display { /// Use Flexbox layout model to determine the position of this [`Node`]. Flex, @@ -735,8 +779,13 @@ impl Default for Display { } /// Defines how flexbox items are ordered within a flexbox -#[derive(Copy, Clone, PartialEq, Eq, Debug, Serialize, Deserialize, Reflect)] -#[reflect(PartialEq, Serialize, Deserialize)] +#[derive(Copy, Clone, PartialEq, Eq, Debug, Reflect)] +#[reflect(PartialEq)] +#[cfg_attr( + feature = "serialize", + derive(serde::Serialize, serde::Deserialize), + reflect(Serialize, Deserialize) +)] pub enum FlexDirection { /// Same way as text direction along the main axis. Row, @@ -759,8 +808,13 @@ impl Default for FlexDirection { } /// Whether to show or hide overflowing items -#[derive(Copy, Clone, PartialEq, Eq, Debug, Reflect, Serialize, Deserialize)] -#[reflect(PartialEq, Serialize, Deserialize)] +#[derive(Copy, Clone, PartialEq, Eq, Debug, Reflect)] +#[reflect(PartialEq)] +#[cfg_attr( + feature = "serialize", + derive(serde::Serialize, serde::Deserialize), + reflect(Serialize, Deserialize) +)] pub struct Overflow { /// Whether to show or clip overflowing items on the x axis pub x: OverflowAxis, @@ -819,8 +873,13 @@ impl Default for Overflow { } /// Whether to show or hide overflowing items -#[derive(Copy, Clone, PartialEq, Eq, Debug, Reflect, Serialize, Deserialize)] -#[reflect(PartialEq, Serialize, Deserialize)] +#[derive(Copy, Clone, PartialEq, Eq, Debug, Reflect)] +#[reflect(PartialEq)] +#[cfg_attr( + feature = "serialize", + derive(serde::Serialize, serde::Deserialize), + reflect(Serialize, Deserialize) +)] pub enum OverflowAxis { /// Show overflowing items. Visible, @@ -844,8 +903,13 @@ impl Default for OverflowAxis { } /// The strategy used to position this node -#[derive(Copy, Clone, PartialEq, Eq, Debug, Serialize, Deserialize, Reflect)] -#[reflect(PartialEq, Serialize, Deserialize)] +#[derive(Copy, Clone, PartialEq, Eq, Debug, Reflect)] +#[reflect(PartialEq)] +#[cfg_attr( + feature = "serialize", + derive(serde::Serialize, serde::Deserialize), + reflect(Serialize, Deserialize) +)] pub enum PositionType { /// Relative to all other nodes with the [`PositionType::Relative`] value. Relative, @@ -864,8 +928,13 @@ impl Default for PositionType { } /// Defines if flexbox items appear on a single line or on multiple lines -#[derive(Copy, Clone, PartialEq, Eq, Debug, Serialize, Deserialize, Reflect)] -#[reflect(PartialEq, Serialize, Deserialize)] +#[derive(Copy, Clone, PartialEq, Eq, Debug, Reflect)] +#[reflect(PartialEq)] +#[cfg_attr( + feature = "serialize", + derive(serde::Serialize, serde::Deserialize), + reflect(Serialize, Deserialize) +)] pub enum FlexWrap { /// Single line, will overflow if needed. NoWrap, @@ -893,8 +962,13 @@ impl Default for FlexWrap { /// Defaults to [`GridAutoFlow::Row`]. /// /// -#[derive(Copy, Clone, PartialEq, Eq, Debug, Serialize, Deserialize, Reflect)] -#[reflect(PartialEq, Serialize, Deserialize)] +#[derive(Copy, Clone, PartialEq, Eq, Debug, Reflect)] +#[reflect(PartialEq)] +#[cfg_attr( + feature = "serialize", + derive(serde::Serialize, serde::Deserialize), + reflect(Serialize, Deserialize) +)] pub enum GridAutoFlow { /// Items are placed by filling each row in turn, adding new rows as necessary. Row, @@ -916,8 +990,13 @@ impl Default for GridAutoFlow { } } -#[derive(Copy, Clone, PartialEq, Debug, Serialize, Deserialize, Reflect)] -#[reflect_value(PartialEq, Serialize, Deserialize)] +#[derive(Copy, Clone, PartialEq, Debug, Reflect)] +#[reflect_value(PartialEq)] +#[cfg_attr( + feature = "serialize", + derive(serde::Serialize, serde::Deserialize), + reflect_value(Serialize, Deserialize) +)] pub enum MinTrackSizingFunction { /// Track minimum size should be a fixed pixel value Px(f32), @@ -931,8 +1010,13 @@ pub enum MinTrackSizingFunction { Auto, } -#[derive(Copy, Clone, PartialEq, Debug, Serialize, Deserialize, Reflect)] -#[reflect_value(PartialEq, Serialize, Deserialize)] +#[derive(Copy, Clone, PartialEq, Debug, Reflect)] +#[reflect_value(PartialEq)] +#[cfg_attr( + feature = "serialize", + derive(serde::Serialize, serde::Deserialize), + reflect_value(Serialize, Deserialize) +)] pub enum MaxTrackSizingFunction { /// Track maximum size should be a fixed pixel value Px(f32), @@ -957,8 +1041,13 @@ pub enum MaxTrackSizingFunction { /// A [`GridTrack`] is a Row or Column of a CSS Grid. This struct specifies what size the track should be. /// See below for the different "track sizing functions" you can specify. -#[derive(Copy, Clone, PartialEq, Debug, Serialize, Deserialize, Reflect)] -#[reflect(PartialEq, Serialize, Deserialize)] +#[derive(Copy, Clone, PartialEq, Debug, Reflect)] +#[reflect(PartialEq)] +#[cfg_attr( + feature = "serialize", + derive(serde::Serialize, serde::Deserialize), + reflect(Serialize, Deserialize) +)] pub struct GridTrack { pub(crate) min_sizing_function: MinTrackSizingFunction, pub(crate) max_sizing_function: MaxTrackSizingFunction, @@ -1075,8 +1164,13 @@ impl Default for GridTrack { } } -#[derive(Copy, Clone, PartialEq, Debug, Serialize, Deserialize, Reflect)] -#[reflect(PartialEq, Serialize, Deserialize)] +#[derive(Copy, Clone, PartialEq, Debug, Reflect)] +#[reflect(PartialEq)] +#[cfg_attr( + feature = "serialize", + derive(serde::Serialize, serde::Deserialize), + reflect(Serialize, Deserialize) +)] /// How many times to repeat a repeated grid track /// /// @@ -1125,8 +1219,13 @@ impl From for GridTrackRepetition { /// You may only use one auto-repetition per track list. And if your track list contains an auto repetition /// then all tracks (in and outside of the repetition) must be fixed size (px or percent). Integer repetitions are just shorthand for writing out /// N tracks longhand and are not subject to the same limitations. -#[derive(Clone, PartialEq, Debug, Serialize, Deserialize, Reflect)] -#[reflect(PartialEq, Serialize, Deserialize)] +#[derive(Clone, PartialEq, Debug, Reflect)] +#[reflect(PartialEq)] +#[cfg_attr( + feature = "serialize", + derive(serde::Serialize, serde::Deserialize), + reflect(Serialize, Deserialize) +)] pub struct RepeatedGridTrack { pub(crate) repetition: GridTrackRepetition, pub(crate) tracks: SmallVec<[GridTrack; 1]>, @@ -1275,8 +1374,13 @@ impl From for Vec { } } -#[derive(Copy, Clone, PartialEq, Eq, Debug, Serialize, Deserialize, Reflect)] -#[reflect(PartialEq, Serialize, Deserialize)] +#[derive(Copy, Clone, PartialEq, Eq, Debug, Reflect)] +#[reflect(PartialEq)] +#[cfg_attr( + feature = "serialize", + derive(serde::Serialize, serde::Deserialize), + reflect(Serialize, Deserialize) +)] /// Represents the position of a grid item in a single axis. /// /// There are 3 fields which may be set: @@ -1474,8 +1578,13 @@ pub enum GridPlacementError { /// /// This serves as the "fill" color. /// When combined with [`UiImage`], tints the provided texture. -#[derive(Component, Copy, Clone, Debug, Deserialize, Serialize, Reflect)] -#[reflect(Component, Default, Deserialize, Serialize)] +#[derive(Component, Copy, Clone, Debug, Reflect)] +#[reflect(Component, Default)] +#[cfg_attr( + feature = "serialize", + derive(serde::Serialize, serde::Deserialize), + reflect(Serialize, Deserialize) +)] pub struct BackgroundColor(pub Color); impl BackgroundColor { @@ -1507,8 +1616,13 @@ pub struct UiTextureAtlasImage { } /// The border color of the UI node. -#[derive(Component, Copy, Clone, Debug, Deserialize, Serialize, Reflect)] -#[reflect(Component, Default, Deserialize, Serialize)] +#[derive(Component, Copy, Clone, Debug, Reflect)] +#[reflect(Component, Default)] +#[cfg_attr( + feature = "serialize", + derive(serde::Serialize, serde::Deserialize), + reflect(Serialize, Deserialize) +)] pub struct BorderColor(pub Color); impl From for BorderColor { @@ -1527,8 +1641,13 @@ impl Default for BorderColor { } } -#[derive(Component, Copy, Clone, Default, Debug, Deserialize, Serialize, Reflect)] -#[reflect(Component, Default, Deserialize, Serialize)] +#[derive(Component, Copy, Clone, Default, Debug, Reflect)] +#[reflect(Component, Default)] +#[cfg_attr( + feature = "serialize", + derive(serde::Serialize, serde::Deserialize), + reflect(Serialize, Deserialize) +)] /// The [`Outline`] component adds an outline outside the edge of a UI node. /// Outlines do not take up space in the layout. ///