2022-04-25 19:20:38 +00:00
|
|
|
use crate::{Size, UiRect};
|
2021-12-14 03:58:23 +00:00
|
|
|
use bevy_asset::Handle;
|
Enforce type safe usage of Handle::get (#4794)
# Objective
- Sometimes, people might load an asset as one type, then use it with an `Asset`s for a different type.
- See e.g. #4784.
- This is especially likely with the Gltf types, since users may not have a clear conceptual model of what types the assets will be.
- We had an instance of this ourselves, in the `scene_viewer` example
## Solution
- Make `Assets::get` require a type safe handle.
---
## Changelog
### Changed
- `Assets::<T>::get` and `Assets::<T>::get_mut` now require that the passed handles are `Handle<T>`, improving the type safety of handles.
### Added
- `HandleUntyped::typed_weak`, a helper function for creating a weak typed version of an exisitng `HandleUntyped`.
## Migration Guide
`Assets::<T>::get` and `Assets::<T>::get_mut` now require that the passed handles are `Handle<T>`, improving the type safety of handles. If you were previously passing in:
- a `HandleId`, use `&Handle::weak(id)` instead, to create a weak handle. You may have been able to store a type safe `Handle` instead.
- a `HandleUntyped`, use `&handle_untyped.typed_weak()` to create a weak handle of the specified type. This is most likely to be the useful when using [load_folder](https://docs.rs/bevy_asset/latest/bevy_asset/struct.AssetServer.html#method.load_folder)
- a `Handle<U>` of of a different type, consider whether this is the correct handle type to store. If it is (i.e. the same handle id is used for multiple different Asset types) use `Handle::weak(handle.id)` to cast to a different type.
2022-05-30 16:59:44 +00:00
|
|
|
use bevy_derive::{Deref, DerefMut};
|
2021-12-14 03:58:23 +00:00
|
|
|
use bevy_ecs::{prelude::Component, reflect::ReflectComponent};
|
2022-04-25 19:20:38 +00:00
|
|
|
use bevy_math::Vec2;
|
2022-05-03 19:20:13 +00:00
|
|
|
use bevy_reflect::prelude::*;
|
2021-12-14 03:58:23 +00:00
|
|
|
use bevy_render::{
|
|
|
|
color::Color,
|
|
|
|
texture::{Image, DEFAULT_IMAGE_HANDLE},
|
|
|
|
};
|
2020-11-28 00:39:59 +00:00
|
|
|
use serde::{Deserialize, Serialize};
|
2020-07-28 21:24:03 +00:00
|
|
|
use std::ops::{Add, AddAssign};
|
2020-01-13 00:51:21 +00:00
|
|
|
|
2022-01-07 22:20:34 +00:00
|
|
|
/// Describes the size of a UI node
|
2021-12-14 03:58:23 +00:00
|
|
|
#[derive(Component, Debug, Clone, Default, Reflect)]
|
2022-05-03 19:20:13 +00:00
|
|
|
#[reflect(Component, Default)]
|
2020-01-13 00:51:21 +00:00
|
|
|
pub struct Node {
|
2022-01-07 22:20:34 +00:00
|
|
|
/// The size of the node as width and height in pixels
|
2020-06-25 17:13:00 +00:00
|
|
|
pub size: Vec2,
|
2020-01-13 00:51:21 +00:00
|
|
|
}
|
2020-07-26 19:27:09 +00:00
|
|
|
|
2022-01-07 22:20:34 +00:00
|
|
|
/// An enum that describes possible types of value in flexbox layout options
|
2020-11-28 00:39:59 +00:00
|
|
|
#[derive(Copy, Clone, PartialEq, Debug, Serialize, Deserialize, Reflect)]
|
|
|
|
#[reflect_value(PartialEq, Serialize, Deserialize)]
|
2020-07-26 19:27:09 +00:00
|
|
|
pub enum Val {
|
2022-01-07 22:20:34 +00:00
|
|
|
/// No value defined
|
2020-07-26 19:27:09 +00:00
|
|
|
Undefined,
|
2022-01-07 22:20:34 +00:00
|
|
|
/// Automatically determine this value
|
2020-07-26 19:27:09 +00:00
|
|
|
Auto,
|
2022-01-07 22:20:34 +00:00
|
|
|
/// Set this value in pixels
|
2020-07-26 19:27:09 +00:00
|
|
|
Px(f32),
|
2022-01-07 22:20:34 +00:00
|
|
|
/// Set this value in percent
|
2020-07-26 19:27:09 +00:00
|
|
|
Percent(f32),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for Val {
|
|
|
|
fn default() -> Self {
|
|
|
|
Val::Undefined
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Add<f32> for Val {
|
|
|
|
type Output = Val;
|
2020-07-28 21:24:03 +00:00
|
|
|
|
2020-07-26 19:27:09 +00:00
|
|
|
fn add(self, rhs: f32) -> Self::Output {
|
|
|
|
match self {
|
|
|
|
Val::Undefined => Val::Undefined,
|
|
|
|
Val::Auto => Val::Auto,
|
|
|
|
Val::Px(value) => Val::Px(value + rhs),
|
|
|
|
Val::Percent(value) => Val::Percent(value + rhs),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl AddAssign<f32> for Val {
|
|
|
|
fn add_assign(&mut self, rhs: f32) {
|
|
|
|
match self {
|
2020-07-28 21:24:03 +00:00
|
|
|
Val::Undefined | Val::Auto => {}
|
2022-05-31 01:38:07 +00:00
|
|
|
Val::Px(value) | Val::Percent(value) => *value += rhs,
|
2020-07-26 19:27:09 +00:00
|
|
|
}
|
|
|
|
}
|
2020-07-28 04:04:04 +00:00
|
|
|
}
|
|
|
|
|
2022-01-07 22:20:34 +00:00
|
|
|
/// Describes the style of a UI node
|
|
|
|
///
|
|
|
|
/// It uses the [Flexbox](https://cssreference.io/flexbox/) system.
|
|
|
|
///
|
|
|
|
/// **Note:** Bevy's UI is upside down compared to how Flexbox normally works, to stay consistent with engine paradigms about layouting from
|
|
|
|
/// the upper left corner of the display
|
2021-10-03 19:23:44 +00:00
|
|
|
#[derive(Component, Clone, PartialEq, Debug, Reflect)]
|
2022-05-03 19:20:13 +00:00
|
|
|
#[reflect(Component, Default, PartialEq)]
|
2020-07-26 19:27:09 +00:00
|
|
|
pub struct Style {
|
2022-01-07 22:20:34 +00:00
|
|
|
/// Whether to arrange this node and its children with flexbox layout
|
2020-07-26 19:27:09 +00:00
|
|
|
pub display: Display,
|
2022-01-07 22:20:34 +00:00
|
|
|
/// Whether to arrange this node relative to other nodes, or positioned absolutely
|
2020-07-26 19:27:09 +00:00
|
|
|
pub position_type: PositionType,
|
2022-01-07 22:20:34 +00:00
|
|
|
/// Which direction the content of this node should go
|
2020-07-26 19:27:09 +00:00
|
|
|
pub direction: Direction,
|
2022-01-07 22:20:34 +00:00
|
|
|
/// Whether to use column or row layout
|
2020-07-26 19:27:09 +00:00
|
|
|
pub flex_direction: FlexDirection,
|
2022-01-07 22:20:34 +00:00
|
|
|
/// How to wrap nodes
|
2020-07-26 19:27:09 +00:00
|
|
|
pub flex_wrap: FlexWrap,
|
2022-01-07 22:20:34 +00:00
|
|
|
/// How items are aligned according to the cross axis
|
2020-07-26 19:27:09 +00:00
|
|
|
pub align_items: AlignItems,
|
2022-01-07 22:20:34 +00:00
|
|
|
/// Like align_items but for only this item
|
2020-07-26 19:27:09 +00:00
|
|
|
pub align_self: AlignSelf,
|
2022-01-07 22:20:34 +00:00
|
|
|
/// How to align each line, only applies if flex_wrap is set to
|
|
|
|
/// [`FlexWrap::Wrap`] and there are multiple lines of items
|
2020-07-26 19:27:09 +00:00
|
|
|
pub align_content: AlignContent,
|
2022-01-07 22:20:34 +00:00
|
|
|
/// How items align according to the main axis
|
2020-07-26 19:27:09 +00:00
|
|
|
pub justify_content: JustifyContent,
|
2022-01-07 22:20:34 +00:00
|
|
|
/// The position of the node as descrided by its Rect
|
2022-04-25 19:20:38 +00:00
|
|
|
pub position: UiRect<Val>,
|
2022-01-07 22:20:34 +00:00
|
|
|
/// The margin of the node
|
2022-04-25 19:20:38 +00:00
|
|
|
pub margin: UiRect<Val>,
|
2022-01-07 22:20:34 +00:00
|
|
|
/// The padding of the node
|
2022-04-25 19:20:38 +00:00
|
|
|
pub padding: UiRect<Val>,
|
2022-01-07 22:20:34 +00:00
|
|
|
/// The border of the node
|
2022-04-25 19:20:38 +00:00
|
|
|
pub border: UiRect<Val>,
|
2022-01-07 22:20:34 +00:00
|
|
|
/// Defines how much a flexbox item should grow if there's space available
|
2020-07-26 19:27:09 +00:00
|
|
|
pub flex_grow: f32,
|
2022-01-07 22:20:34 +00:00
|
|
|
/// How to shrink if there's not enough space available
|
2020-07-26 19:27:09 +00:00
|
|
|
pub flex_shrink: f32,
|
2022-01-07 22:20:34 +00:00
|
|
|
/// The initial size of the item
|
2020-07-26 19:27:09 +00:00
|
|
|
pub flex_basis: Val,
|
2022-01-07 22:20:34 +00:00
|
|
|
/// The size of the flexbox
|
2020-07-26 19:27:09 +00:00
|
|
|
pub size: Size<Val>,
|
2022-01-07 22:20:34 +00:00
|
|
|
/// The minimum size of the flexbox
|
2020-07-26 19:27:09 +00:00
|
|
|
pub min_size: Size<Val>,
|
2022-01-07 22:20:34 +00:00
|
|
|
/// The maximum size of the flexbox
|
2020-07-26 19:27:09 +00:00
|
|
|
pub max_size: Size<Val>,
|
2022-01-07 22:20:34 +00:00
|
|
|
/// The aspect ratio of the flexbox
|
2020-07-26 19:27:09 +00:00
|
|
|
pub aspect_ratio: Option<f32>,
|
2022-01-07 22:20:34 +00:00
|
|
|
/// How to handle overflow
|
2021-12-19 05:44:28 +00:00
|
|
|
pub overflow: Overflow,
|
2020-07-26 19:27:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
2020-08-25 19:18:43 +00:00
|
|
|
size: Size::new(Val::Auto, Val::Auto),
|
|
|
|
min_size: Size::new(Val::Auto, Val::Auto),
|
|
|
|
max_size: Size::new(Val::Auto, Val::Auto),
|
2020-07-26 19:27:09 +00:00
|
|
|
aspect_ratio: Default::default(),
|
2021-12-19 05:44:28 +00:00
|
|
|
overflow: Default::default(),
|
2020-07-26 19:27:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-07 22:20:34 +00:00
|
|
|
/// How items are aligned according to the cross axis
|
2020-11-28 00:39:59 +00:00
|
|
|
#[derive(Copy, Clone, PartialEq, Debug, Serialize, Deserialize, Reflect)]
|
|
|
|
#[reflect_value(PartialEq, Serialize, Deserialize)]
|
2020-07-26 19:27:09 +00:00
|
|
|
pub enum AlignItems {
|
2022-01-07 22:20:34 +00:00
|
|
|
/// Items are aligned at the start
|
2020-07-26 19:27:09 +00:00
|
|
|
FlexStart,
|
2022-01-07 22:20:34 +00:00
|
|
|
/// Items are aligned at the end
|
2020-07-26 19:27:09 +00:00
|
|
|
FlexEnd,
|
2022-01-07 22:20:34 +00:00
|
|
|
/// Items are aligned at the center
|
2020-07-26 19:27:09 +00:00
|
|
|
Center,
|
2022-01-07 22:20:34 +00:00
|
|
|
/// Items are aligned at the baseline
|
2020-07-26 19:27:09 +00:00
|
|
|
Baseline,
|
2022-01-07 22:20:34 +00:00
|
|
|
/// Items are stretched across the whole cross axis
|
2020-07-26 19:27:09 +00:00
|
|
|
Stretch,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for AlignItems {
|
|
|
|
fn default() -> AlignItems {
|
|
|
|
AlignItems::Stretch
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-07 22:20:34 +00:00
|
|
|
/// Works like [`AlignItems`] but applies only to a single item
|
2020-11-28 00:39:59 +00:00
|
|
|
#[derive(Copy, Clone, PartialEq, Debug, Serialize, Deserialize, Reflect)]
|
|
|
|
#[reflect_value(PartialEq, Serialize, Deserialize)]
|
2020-07-26 19:27:09 +00:00
|
|
|
pub enum AlignSelf {
|
2022-01-07 22:20:34 +00:00
|
|
|
/// Use the value of [`AlignItems`]
|
2020-07-26 19:27:09 +00:00
|
|
|
Auto,
|
2022-01-07 22:20:34 +00:00
|
|
|
/// If the parent has [`AlignItems::Center`] only this item will be at the start
|
2020-07-26 19:27:09 +00:00
|
|
|
FlexStart,
|
2022-01-07 22:20:34 +00:00
|
|
|
/// If the parent has [`AlignItems::Center`] only this item will be at the end
|
2020-07-26 19:27:09 +00:00
|
|
|
FlexEnd,
|
2022-01-07 22:20:34 +00:00
|
|
|
/// If the parent has [`AlignItems::FlexStart`] only this item will be at the center
|
2020-07-26 19:27:09 +00:00
|
|
|
Center,
|
2022-01-07 22:20:34 +00:00
|
|
|
/// If the parent has [`AlignItems::Center`] only this item will be at the baseline
|
2020-07-26 19:27:09 +00:00
|
|
|
Baseline,
|
2022-01-07 22:20:34 +00:00
|
|
|
/// If the parent has [`AlignItems::Center`] only this item will stretch along the whole cross axis
|
2020-07-26 19:27:09 +00:00
|
|
|
Stretch,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for AlignSelf {
|
|
|
|
fn default() -> AlignSelf {
|
|
|
|
AlignSelf::Auto
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-07 22:20:34 +00:00
|
|
|
/// 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.
|
2020-11-28 00:39:59 +00:00
|
|
|
#[derive(Copy, Clone, PartialEq, Debug, Serialize, Deserialize, Reflect)]
|
|
|
|
#[reflect_value(PartialEq, Serialize, Deserialize)]
|
2020-07-26 19:27:09 +00:00
|
|
|
pub enum AlignContent {
|
2022-01-07 22:20:34 +00:00
|
|
|
/// Each line moves towards the start of the cross axis
|
2020-07-26 19:27:09 +00:00
|
|
|
FlexStart,
|
2022-01-07 22:20:34 +00:00
|
|
|
/// Each line moves towards the end of the cross axis
|
2020-07-26 19:27:09 +00:00
|
|
|
FlexEnd,
|
2022-01-07 22:20:34 +00:00
|
|
|
/// Each line moves towards the center of the cross axis
|
2020-07-26 19:27:09 +00:00
|
|
|
Center,
|
2022-01-07 22:20:34 +00:00
|
|
|
/// Each line will stretch to fill the remaining space
|
2020-07-26 19:27:09 +00:00
|
|
|
Stretch,
|
2022-01-07 22:20:34 +00:00
|
|
|
/// Each line fills the space it needs, putting the remaining space, if any
|
|
|
|
/// inbetween the lines
|
2020-07-26 19:27:09 +00:00
|
|
|
SpaceBetween,
|
2022-01-07 22:20:34 +00:00
|
|
|
/// Each line fills the space it needs, putting the remaining space, if any
|
|
|
|
/// around the lines
|
2020-07-26 19:27:09 +00:00
|
|
|
SpaceAround,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for AlignContent {
|
|
|
|
fn default() -> AlignContent {
|
|
|
|
AlignContent::Stretch
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-07 22:20:34 +00:00
|
|
|
/// Defines the text direction
|
|
|
|
///
|
|
|
|
/// For example English is written LTR (left-to-right) while Arabic is written RTL (right-to-left).
|
2020-11-28 00:39:59 +00:00
|
|
|
#[derive(Copy, Clone, PartialEq, Debug, Serialize, Deserialize, Reflect)]
|
|
|
|
#[reflect_value(PartialEq, Serialize, Deserialize)]
|
2020-07-26 19:27:09 +00:00
|
|
|
pub enum Direction {
|
2022-01-07 22:20:34 +00:00
|
|
|
/// Inherit from parent node
|
2020-07-26 19:27:09 +00:00
|
|
|
Inherit,
|
2022-01-07 22:20:34 +00:00
|
|
|
/// Text is written left to right
|
2021-08-24 01:50:21 +00:00
|
|
|
LeftToRight,
|
2022-01-07 22:20:34 +00:00
|
|
|
/// Text is written right to left
|
2021-08-24 01:50:21 +00:00
|
|
|
RightToLeft,
|
2020-07-26 19:27:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for Direction {
|
|
|
|
fn default() -> Direction {
|
|
|
|
Direction::Inherit
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-07 22:20:34 +00:00
|
|
|
/// Whether to use Flexbox layout
|
2020-11-28 00:39:59 +00:00
|
|
|
#[derive(Copy, Clone, PartialEq, Debug, Serialize, Deserialize, Reflect)]
|
|
|
|
#[reflect_value(PartialEq, Serialize, Deserialize)]
|
2020-07-26 19:27:09 +00:00
|
|
|
pub enum Display {
|
2022-01-07 22:20:34 +00:00
|
|
|
/// Use flexbox
|
2020-07-26 19:27:09 +00:00
|
|
|
Flex,
|
2022-01-07 22:20:34 +00:00
|
|
|
/// Use no layout, don't render this node and its children
|
2020-07-26 19:27:09 +00:00
|
|
|
None,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for Display {
|
|
|
|
fn default() -> Display {
|
|
|
|
Display::Flex
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-07 22:20:34 +00:00
|
|
|
/// Defines how flexbox items are ordered within a flexbox
|
2020-11-28 00:39:59 +00:00
|
|
|
#[derive(Copy, Clone, PartialEq, Debug, Serialize, Deserialize, Reflect)]
|
|
|
|
#[reflect_value(PartialEq, Serialize, Deserialize)]
|
2020-07-26 19:27:09 +00:00
|
|
|
pub enum FlexDirection {
|
2022-01-07 22:20:34 +00:00
|
|
|
/// Same way as text direction along the main axis
|
2020-07-26 19:27:09 +00:00
|
|
|
Row,
|
2022-01-07 22:20:34 +00:00
|
|
|
/// Flex from bottom to top
|
2020-07-26 19:27:09 +00:00
|
|
|
Column,
|
2022-01-07 22:20:34 +00:00
|
|
|
/// Opposite way as text direction along the main axis
|
2020-07-26 19:27:09 +00:00
|
|
|
RowReverse,
|
2022-01-07 22:20:34 +00:00
|
|
|
/// Flex from top to bottom
|
2020-07-26 19:27:09 +00:00
|
|
|
ColumnReverse,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for FlexDirection {
|
|
|
|
fn default() -> FlexDirection {
|
|
|
|
FlexDirection::Row
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-07 22:20:34 +00:00
|
|
|
/// Defines how items are aligned according to the main axis
|
2020-11-28 00:39:59 +00:00
|
|
|
#[derive(Copy, Clone, PartialEq, Debug, Serialize, Deserialize, Reflect)]
|
|
|
|
#[reflect_value(PartialEq, Serialize, Deserialize)]
|
2020-07-26 19:27:09 +00:00
|
|
|
pub enum JustifyContent {
|
2022-01-07 22:20:34 +00:00
|
|
|
/// Pushed towards the start
|
2020-07-26 19:27:09 +00:00
|
|
|
FlexStart,
|
2022-01-07 22:20:34 +00:00
|
|
|
/// Pushed towards the end
|
2020-07-26 19:27:09 +00:00
|
|
|
FlexEnd,
|
2022-01-07 22:20:34 +00:00
|
|
|
/// Centered along the main axis
|
2020-07-26 19:27:09 +00:00
|
|
|
Center,
|
2022-01-07 22:20:34 +00:00
|
|
|
/// Remaining space is distributed between the items
|
2020-07-26 19:27:09 +00:00
|
|
|
SpaceBetween,
|
2022-01-07 22:20:34 +00:00
|
|
|
/// Remaining space is distributed around the items
|
2020-07-26 19:27:09 +00:00
|
|
|
SpaceAround,
|
2022-01-07 22:20:34 +00:00
|
|
|
/// Like [`JustifyContent::SpaceAround`] but with even spacing between items
|
2020-07-26 19:27:09 +00:00
|
|
|
SpaceEvenly,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for JustifyContent {
|
|
|
|
fn default() -> JustifyContent {
|
|
|
|
JustifyContent::FlexStart
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-07 22:20:34 +00:00
|
|
|
/// Whether to show or hide overflowing items
|
2021-12-19 05:44:28 +00:00
|
|
|
#[derive(Copy, Clone, PartialEq, Debug, Reflect, Serialize, Deserialize)]
|
|
|
|
#[reflect_value(PartialEq, Serialize, Deserialize)]
|
|
|
|
pub enum Overflow {
|
2022-01-07 22:20:34 +00:00
|
|
|
/// Show overflowing items
|
2021-12-19 05:44:28 +00:00
|
|
|
Visible,
|
2022-01-07 22:20:34 +00:00
|
|
|
/// Hide overflowing items
|
2021-12-19 05:44:28 +00:00
|
|
|
Hidden,
|
|
|
|
}
|
2020-07-26 19:27:09 +00:00
|
|
|
|
2021-12-19 05:44:28 +00:00
|
|
|
impl Default for Overflow {
|
|
|
|
fn default() -> Overflow {
|
|
|
|
Overflow::Visible
|
|
|
|
}
|
|
|
|
}
|
2020-07-26 19:27:09 +00:00
|
|
|
|
2022-01-07 22:20:34 +00:00
|
|
|
/// The strategy used to position this node
|
2020-11-28 00:39:59 +00:00
|
|
|
#[derive(Copy, Clone, PartialEq, Debug, Serialize, Deserialize, Reflect)]
|
|
|
|
#[reflect_value(PartialEq, Serialize, Deserialize)]
|
2020-07-26 19:27:09 +00:00
|
|
|
pub enum PositionType {
|
2022-01-07 22:20:34 +00:00
|
|
|
/// Relative to all other nodes with the [`PositionType::Relative`] value
|
2020-07-26 19:27:09 +00:00
|
|
|
Relative,
|
2022-01-07 22:20:34 +00:00
|
|
|
/// Independent of all other nodes
|
|
|
|
///
|
|
|
|
/// As usual, the `Style.position` field of this node is specified relative to its parent node
|
2020-07-26 19:27:09 +00:00
|
|
|
Absolute,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for PositionType {
|
|
|
|
fn default() -> PositionType {
|
|
|
|
PositionType::Relative
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-07 22:20:34 +00:00
|
|
|
/// Defines if flexbox items appear on a single line or on multiple lines
|
2020-11-28 00:39:59 +00:00
|
|
|
#[derive(Copy, Clone, PartialEq, Debug, Serialize, Deserialize, Reflect)]
|
|
|
|
#[reflect_value(PartialEq, Serialize, Deserialize)]
|
2020-07-26 19:27:09 +00:00
|
|
|
pub enum FlexWrap {
|
2022-01-07 22:20:34 +00:00
|
|
|
/// Single line, will overflow if needed
|
2020-07-26 19:27:09 +00:00
|
|
|
NoWrap,
|
2022-01-07 22:20:34 +00:00
|
|
|
/// Multiple lines, if needed
|
2020-07-26 19:27:09 +00:00
|
|
|
Wrap,
|
2022-01-07 22:20:34 +00:00
|
|
|
/// Same as [`FlexWrap::Wrap`] but new lines will appear before the previous one
|
2020-07-26 19:27:09 +00:00
|
|
|
WrapReverse,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for FlexWrap {
|
|
|
|
fn default() -> FlexWrap {
|
|
|
|
FlexWrap::NoWrap
|
|
|
|
}
|
2020-07-28 21:24:03 +00:00
|
|
|
}
|
2021-03-10 22:37:02 +00:00
|
|
|
|
2022-01-07 22:20:34 +00:00
|
|
|
/// The calculated size of the node
|
2021-12-14 03:58:23 +00:00
|
|
|
#[derive(Component, Default, Copy, Clone, Debug, Reflect)]
|
|
|
|
#[reflect(Component)]
|
2021-03-10 22:37:02 +00:00
|
|
|
pub struct CalculatedSize {
|
2022-01-07 22:20:34 +00:00
|
|
|
/// The size of the node
|
2021-03-10 22:37:02 +00:00
|
|
|
pub size: Size,
|
|
|
|
}
|
2021-12-14 03:58:23 +00:00
|
|
|
|
2022-01-07 22:20:34 +00:00
|
|
|
/// The color of the node
|
2021-12-14 03:58:23 +00:00
|
|
|
#[derive(Component, Default, Copy, Clone, Debug, Reflect)]
|
2022-05-03 19:20:13 +00:00
|
|
|
#[reflect(Component, Default)]
|
2021-12-14 03:58:23 +00:00
|
|
|
pub struct UiColor(pub Color);
|
|
|
|
|
|
|
|
impl From<Color> for UiColor {
|
|
|
|
fn from(color: Color) -> Self {
|
|
|
|
Self(color)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-07 22:20:34 +00:00
|
|
|
/// The image of the node
|
Enforce type safe usage of Handle::get (#4794)
# Objective
- Sometimes, people might load an asset as one type, then use it with an `Asset`s for a different type.
- See e.g. #4784.
- This is especially likely with the Gltf types, since users may not have a clear conceptual model of what types the assets will be.
- We had an instance of this ourselves, in the `scene_viewer` example
## Solution
- Make `Assets::get` require a type safe handle.
---
## Changelog
### Changed
- `Assets::<T>::get` and `Assets::<T>::get_mut` now require that the passed handles are `Handle<T>`, improving the type safety of handles.
### Added
- `HandleUntyped::typed_weak`, a helper function for creating a weak typed version of an exisitng `HandleUntyped`.
## Migration Guide
`Assets::<T>::get` and `Assets::<T>::get_mut` now require that the passed handles are `Handle<T>`, improving the type safety of handles. If you were previously passing in:
- a `HandleId`, use `&Handle::weak(id)` instead, to create a weak handle. You may have been able to store a type safe `Handle` instead.
- a `HandleUntyped`, use `&handle_untyped.typed_weak()` to create a weak handle of the specified type. This is most likely to be the useful when using [load_folder](https://docs.rs/bevy_asset/latest/bevy_asset/struct.AssetServer.html#method.load_folder)
- a `Handle<U>` of of a different type, consider whether this is the correct handle type to store. If it is (i.e. the same handle id is used for multiple different Asset types) use `Handle::weak(handle.id)` to cast to a different type.
2022-05-30 16:59:44 +00:00
|
|
|
#[derive(Component, Clone, Debug, Reflect, Deref, DerefMut)]
|
2022-05-03 19:20:13 +00:00
|
|
|
#[reflect(Component, Default)]
|
2021-12-14 03:58:23 +00:00
|
|
|
pub struct UiImage(pub Handle<Image>);
|
|
|
|
|
|
|
|
impl Default for UiImage {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self(DEFAULT_IMAGE_HANDLE.typed())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<Handle<Image>> for UiImage {
|
|
|
|
fn from(handle: Handle<Image>) -> Self {
|
|
|
|
Self(handle)
|
|
|
|
}
|
|
|
|
}
|
2021-12-19 05:44:28 +00:00
|
|
|
|
2022-01-07 22:20:34 +00:00
|
|
|
/// The calculated clip of the node
|
2021-12-19 05:44:28 +00:00
|
|
|
#[derive(Component, Default, Copy, Clone, Debug, Reflect)]
|
|
|
|
#[reflect(Component)]
|
|
|
|
pub struct CalculatedClip {
|
2022-01-07 22:20:34 +00:00
|
|
|
/// The rect of the clip
|
2021-12-19 05:44:28 +00:00
|
|
|
pub clip: bevy_sprite::Rect,
|
|
|
|
}
|