bevy_reflect: Update enum derives (#5473)

> In draft until #4761 is merged. See the relevant commits [here](a85fe94a18).

---

# Objective

Update enums across Bevy to use the new enum reflection and get rid of `#[reflect_value(...)]` usages.

## Solution

Find and replace all[^1] instances of `#[reflect_value(...)]` on enum types.

---

## Changelog

- Updated all[^1] reflected enums to implement `Enum` (i.e. they are no longer `ReflectRef::Value`)

## Migration Guide
Bevy-defined enums have been updated to implement `Enum` and are not considered value types (`ReflectRef::Value`) anymore. This means that their serialized representations will need to be updated. For example, given the Bevy enum:

```rust
pub enum ScalingMode {
  None,
  WindowSize,
  Auto { min_width: f32, min_height: f32 },
  FixedVertical(f32),
  FixedHorizontal(f32),
}
```

You will need to update the serialized versions accordingly.

```js
// OLD FORMAT
{
  "type": "bevy_render:📷:projection::ScalingMode",
  "value": FixedHorizontal(720),
},

// NEW FORMAT
{
  "type": "bevy_render:📷:projection::ScalingMode",
  "enum": {
    "variant": "FixedHorizontal",
    "tuple": [
      {
        "type": "f32",
        "value": 720,
      },
    ],
  },
},
```

This may also have other smaller implications (such as `Debug` representation), but serialization is probably the most prominent.

[^1]: All enums except `HandleId` as neither `Uuid` nor `AssetPathId` implement the reflection traits
This commit is contained in:
Gino Valente 2022-08-02 22:40:29 +00:00
parent 15826d6019
commit bd008589f3
8 changed files with 21 additions and 21 deletions

View File

@ -5,7 +5,7 @@ use bevy_render::{color::Color, extract_resource::ExtractResource};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
#[derive(Reflect, Serialize, Deserialize, Clone, Debug, Default)] #[derive(Reflect, Serialize, Deserialize, Clone, Debug, Default)]
#[reflect_value(Serialize, Deserialize)] #[reflect(Serialize, Deserialize)]
pub enum ClearColorConfig { pub enum ClearColorConfig {
#[default] #[default]
Default, Default,

View File

@ -23,7 +23,7 @@ pub struct Camera3d {
/// The depth clear operation to perform for the main 3d pass. /// The depth clear operation to perform for the main 3d pass.
#[derive(Reflect, Serialize, Deserialize, Clone, Debug)] #[derive(Reflect, Serialize, Deserialize, Clone, Debug)]
#[reflect_value(Serialize, Deserialize)] #[reflect(Serialize, Deserialize)]
pub enum Camera3dDepthLoadOp { pub enum Camera3dDepthLoadOp {
/// Clear with a specified value. /// Clear with a specified value.
/// Note that 0.0 is the far plane due to bevy's use of reverse-z projections. /// Note that 0.0 is the far plane due to bevy's use of reverse-z projections.

View File

@ -311,7 +311,7 @@ impl RenderTarget {
} }
#[derive(Debug, Clone, Copy, Default, Reflect, FromReflect, Serialize, Deserialize)] #[derive(Debug, Clone, Copy, Default, Reflect, FromReflect, Serialize, Deserialize)]
#[reflect_value(Serialize, Deserialize)] #[reflect(Serialize, Deserialize)]
pub enum DepthCalculation { pub enum DepthCalculation {
/// Pythagorean distance; works everywhere, more expensive to compute. /// Pythagorean distance; works everywhere, more expensive to compute.
#[default] #[default]

View File

@ -142,14 +142,14 @@ impl Default for PerspectiveProjection {
// TODO: make this a component instead of a property // TODO: make this a component instead of a property
#[derive(Debug, Clone, Reflect, FromReflect, Serialize, Deserialize)] #[derive(Debug, Clone, Reflect, FromReflect, Serialize, Deserialize)]
#[reflect_value(Serialize, Deserialize)] #[reflect(Serialize, Deserialize)]
pub enum WindowOrigin { pub enum WindowOrigin {
Center, Center,
BottomLeft, BottomLeft,
} }
#[derive(Debug, Clone, Reflect, FromReflect, Serialize, Deserialize)] #[derive(Debug, Clone, Reflect, FromReflect, Serialize, Deserialize)]
#[reflect_value(Serialize, Deserialize)] #[reflect(Serialize, Deserialize)]
pub enum ScalingMode { pub enum ScalingMode {
/// Manually specify left/right/top/bottom values. /// Manually specify left/right/top/bottom values.
/// Ignore window resizing; the image will stretch. /// Ignore window resizing; the image will stretch.

View File

@ -187,7 +187,7 @@ impl Default for TextAlignment {
/// Describes horizontal alignment preference for positioning & bounds. /// Describes horizontal alignment preference for positioning & bounds.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Reflect, Serialize, Deserialize)] #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Reflect, Serialize, Deserialize)]
#[reflect_value(Serialize, Deserialize)] #[reflect(Serialize, Deserialize)]
pub enum HorizontalAlign { pub enum HorizontalAlign {
/// Leftmost character is immediately to the right of the render position.<br/> /// Leftmost character is immediately to the right of the render position.<br/>
/// Bounds start from the render position and advance rightwards. /// Bounds start from the render position and advance rightwards.
@ -213,7 +213,7 @@ impl From<HorizontalAlign> for glyph_brush_layout::HorizontalAlign {
/// Describes vertical alignment preference for positioning & bounds. Currently a placeholder /// Describes vertical alignment preference for positioning & bounds. Currently a placeholder
/// for future functionality. /// for future functionality.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Reflect, Serialize, Deserialize)] #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Reflect, Serialize, Deserialize)]
#[reflect_value(Serialize, Deserialize)] #[reflect(Serialize, Deserialize)]
pub enum VerticalAlign { pub enum VerticalAlign {
/// Characters/bounds start underneath the render position and progress downwards. /// Characters/bounds start underneath the render position and progress downwards.
Top, Top,

View File

@ -33,7 +33,7 @@ use smallvec::SmallVec;
#[derive( #[derive(
Component, Copy, Clone, Default, Eq, PartialEq, Debug, Reflect, Serialize, Deserialize, Component, Copy, Clone, Default, Eq, PartialEq, Debug, Reflect, Serialize, Deserialize,
)] )]
#[reflect_value(Component, Serialize, Deserialize, PartialEq)] #[reflect(Component, Serialize, Deserialize, PartialEq)]
pub enum Interaction { pub enum Interaction {
/// The node has been clicked /// The node has been clicked
Clicked, Clicked,
@ -48,7 +48,7 @@ pub enum Interaction {
#[derive( #[derive(
Component, Copy, Clone, Default, Eq, PartialEq, Debug, Reflect, Serialize, Deserialize, Component, Copy, Clone, Default, Eq, PartialEq, Debug, Reflect, Serialize, Deserialize,
)] )]
#[reflect_value(Component, Serialize, Deserialize, PartialEq)] #[reflect(Component, Serialize, Deserialize, PartialEq)]
pub enum FocusPolicy { pub enum FocusPolicy {
/// Blocks interaction /// Blocks interaction
#[default] #[default]

View File

@ -21,7 +21,7 @@ pub struct Node {
/// An enum that describes possible types of value in flexbox layout options /// 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, Default, Serialize, Deserialize, Reflect)]
#[reflect_value(PartialEq, Serialize, Deserialize)] #[reflect(PartialEq, Serialize, Deserialize)]
pub enum Val { pub enum Val {
/// No value defined /// No value defined
#[default] #[default]
@ -208,7 +208,7 @@ impl Default for Style {
/// How items are aligned according to the cross axis /// 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, Default, Serialize, Deserialize, Reflect)]
#[reflect_value(PartialEq, Serialize, Deserialize)] #[reflect(PartialEq, Serialize, Deserialize)]
pub enum AlignItems { pub enum AlignItems {
/// Items are aligned at the start /// Items are aligned at the start
FlexStart, FlexStart,
@ -225,7 +225,7 @@ pub enum AlignItems {
/// Works like [`AlignItems`] but applies only to a single item /// 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, Default, Serialize, Deserialize, Reflect)]
#[reflect_value(PartialEq, Serialize, Deserialize)] #[reflect(PartialEq, Serialize, Deserialize)]
pub enum AlignSelf { pub enum AlignSelf {
/// Use the value of [`AlignItems`] /// Use the value of [`AlignItems`]
#[default] #[default]
@ -246,7 +246,7 @@ pub enum AlignSelf {
/// ///
/// It only applies if [`FlexWrap::Wrap`] is present and if there are multiple lines of items. /// 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, Default, Serialize, Deserialize, Reflect)]
#[reflect_value(PartialEq, Serialize, Deserialize)] #[reflect(PartialEq, Serialize, Deserialize)]
pub enum AlignContent { pub enum AlignContent {
/// Each line moves towards the start of the cross axis /// Each line moves towards the start of the cross axis
FlexStart, FlexStart,
@ -269,7 +269,7 @@ pub enum AlignContent {
/// ///
/// For example English is written LTR (left-to-right) while Arabic is written RTL (right-to-left). /// 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, Default, Serialize, Deserialize, Reflect)]
#[reflect_value(PartialEq, Serialize, Deserialize)] #[reflect(PartialEq, Serialize, Deserialize)]
pub enum Direction { pub enum Direction {
/// Inherit from parent node /// Inherit from parent node
#[default] #[default]
@ -284,7 +284,7 @@ pub enum Direction {
/// ///
/// Part of the [`Style`] component. /// Part of the [`Style`] component.
#[derive(Copy, Clone, PartialEq, Eq, Debug, Default, Serialize, Deserialize, Reflect)] #[derive(Copy, Clone, PartialEq, Eq, Debug, Default, Serialize, Deserialize, Reflect)]
#[reflect_value(PartialEq, Serialize, Deserialize)] #[reflect(PartialEq, Serialize, Deserialize)]
pub enum Display { pub enum Display {
/// Use Flexbox layout model to determine the position of this [`Node`]. /// Use Flexbox layout model to determine the position of this [`Node`].
#[default] #[default]
@ -298,7 +298,7 @@ pub enum Display {
/// Defines how flexbox items are ordered within a flexbox /// 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, Default, Serialize, Deserialize, Reflect)]
#[reflect_value(PartialEq, Serialize, Deserialize)] #[reflect(PartialEq, Serialize, Deserialize)]
pub enum FlexDirection { pub enum FlexDirection {
/// Same way as text direction along the main axis /// Same way as text direction along the main axis
#[default] #[default]
@ -313,7 +313,7 @@ pub enum FlexDirection {
/// Defines how items are aligned according to the main axis /// 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, Default, Serialize, Deserialize, Reflect)]
#[reflect_value(PartialEq, Serialize, Deserialize)] #[reflect(PartialEq, Serialize, Deserialize)]
pub enum JustifyContent { pub enum JustifyContent {
/// Pushed towards the start /// Pushed towards the start
#[default] #[default]
@ -332,7 +332,7 @@ pub enum JustifyContent {
/// Whether to show or hide overflowing items /// Whether to show or hide overflowing items
#[derive(Copy, Clone, PartialEq, Eq, Debug, Default, Reflect, Serialize, Deserialize)] #[derive(Copy, Clone, PartialEq, Eq, Debug, Default, Reflect, Serialize, Deserialize)]
#[reflect_value(PartialEq, Serialize, Deserialize)] #[reflect(PartialEq, Serialize, Deserialize)]
pub enum Overflow { pub enum Overflow {
/// Show overflowing items /// Show overflowing items
#[default] #[default]
@ -343,7 +343,7 @@ pub enum Overflow {
/// The strategy used to position this node /// The strategy used to position this node
#[derive(Copy, Clone, PartialEq, Eq, Debug, Default, Serialize, Deserialize, Reflect)] #[derive(Copy, Clone, PartialEq, Eq, Debug, Default, Serialize, Deserialize, Reflect)]
#[reflect_value(PartialEq, Serialize, Deserialize)] #[reflect(PartialEq, Serialize, Deserialize)]
pub enum PositionType { pub enum PositionType {
/// Relative to all other nodes with the [`PositionType::Relative`] value /// Relative to all other nodes with the [`PositionType::Relative`] value
#[default] #[default]
@ -356,7 +356,7 @@ pub enum PositionType {
/// Defines if flexbox items appear on a single line or on multiple lines /// 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, Default, Serialize, Deserialize, Reflect)]
#[reflect_value(PartialEq, Serialize, Deserialize)] #[reflect(PartialEq, Serialize, Deserialize)]
pub enum FlexWrap { pub enum FlexWrap {
/// Single line, will overflow if needed /// Single line, will overflow if needed
#[default] #[default]

View File

@ -12,7 +12,7 @@ use serde::{Deserialize, Serialize};
/// Describes how to resize the Image node /// Describes how to resize the Image node
#[derive(Component, Debug, Default, Clone, Reflect, Serialize, Deserialize)] #[derive(Component, Debug, Default, Clone, Reflect, Serialize, Deserialize)]
#[reflect_value(Component, Serialize, Deserialize)] #[reflect(Component, Serialize, Deserialize)]
pub enum ImageMode { pub enum ImageMode {
/// Keep the aspect ratio of the image /// Keep the aspect ratio of the image
#[default] #[default]