Expand description
A type which is commonly used to define positions, margins, paddings and borders.
Examples
Position
A position is used to determine where to place a UI element.
let position = UiRect {
left: Val::Px(100.0),
top: Val::Px(50.0),
..default()
};
If you define opposite sides of the position, the size of the UI element will automatically be calculated
if not explicitly specified. This means that if you have a Size
that uses Val::Undefined
as a width and height, the size would be determined by the window size and the values specified in the position.
let position = UiRect {
left: Val::Px(100.0),
right: Val::Px(200.0),
top: Val::Px(300.0),
bottom: Val::Px(400.0),
};
To determine the width of the UI element you have to take the width of the window and subtract it by the left and right values of the position. To determine the height of the UI element you have to take the height of the window and subtract it by the top and bottom values of the position. If we had a window with a width and height of 1000px, the UI element declared above would have a width of 700px and a height of 300px.
// Size of the window
let window_width = 1000.0;
let window_height = 1000.0;
// Values of the position
let left = 100.0;
let right = 200.0;
let top = 300.0;
let bottom = 400.0;
// Calculation to get the size of the UI element
let ui_element_width = window_width - left - right;
let ui_element_height = window_height - top - bottom;
assert_eq!(ui_element_width, 700.0);
assert_eq!(ui_element_height, 300.0);
If you define a Size
and also all four sides of the position, the top and left values of the position
are used to determine where to place the UI element. The size will not be calculated using the bottom and
right values of the position because the size of the UI element is already explicitly specified.
let style = Style {
position: UiRect { // Defining all four sides
left: Val::Px(100.0),
right: Val::Px(200.0),
top: Val::Px(300.0),
bottom: Val::Px(400.0),
},
size: Size::new(Val::Percent(100.0), Val::Percent(50.0)), // but also explicitly specifying a size
..default()
};
Margin
A margin is used to create space around UI elements, outside of any defined borders.
let margin = UiRect::all(Val::Auto); // Centers the UI element
Padding
A padding is used to create space around UI elements, inside of any defined borders.
let padding = UiRect {
left: Val::Px(10.0),
right: Val::Px(20.0),
top: Val::Px(30.0),
bottom: Val::Px(40.0),
};
Borders
A border is used to define the width of the border of a UI element.
let border = UiRect {
left: Val::Px(10.0),
right: Val::Px(20.0),
top: Val::Px(30.0),
bottom: Val::Px(40.0),
};
Fields§
§left: Val
The value corresponding to the left side of the UI rect.
right: Val
The value corresponding to the right side of the UI rect.
top: Val
The value corresponding to the top side of the UI rect.
bottom: Val
The value corresponding to the bottom side of the UI rect.
Implementations§
§impl UiRect
impl UiRect
pub fn new(left: Val, right: Val, top: Val, bottom: Val) -> UiRect
pub fn new(left: Val, right: Val, top: Val, bottom: Val) -> UiRect
Creates a new UiRect
from the values specified.
Example
let ui_rect = UiRect::new(
Val::Px(10.0),
Val::Px(20.0),
Val::Px(30.0),
Val::Px(40.0),
);
assert_eq!(ui_rect.left, Val::Px(10.0));
assert_eq!(ui_rect.right, Val::Px(20.0));
assert_eq!(ui_rect.top, Val::Px(30.0));
assert_eq!(ui_rect.bottom, Val::Px(40.0));
pub fn horizontal(value: Val) -> UiRect
pub fn horizontal(value: Val) -> UiRect
Trait Implementations§
§impl GetTypeRegistration for UiRect
impl GetTypeRegistration for UiRect
§impl Reflect for UiRect
impl Reflect for UiRect
§fn get_type_info(&self) -> &'static TypeInfo
fn get_type_info(&self) -> &'static TypeInfo
§fn into_any(self: Box<UiRect, Global>) -> Box<dyn Any + 'static, Global>
fn into_any(self: Box<UiRect, Global>) -> Box<dyn Any + 'static, Global>
Box<dyn Any>
.§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut dyn Any
.§fn into_reflect(self: Box<UiRect, Global>) -> Box<dyn Reflect + 'static, Global>
fn into_reflect(self: Box<UiRect, Global>) -> Box<dyn Reflect + 'static, Global>
§fn as_reflect(&self) -> &(dyn Reflect + 'static)
fn as_reflect(&self) -> &(dyn Reflect + 'static)
§fn as_reflect_mut(&mut self) -> &mut (dyn Reflect + 'static)
fn as_reflect_mut(&mut self) -> &mut (dyn Reflect + 'static)
§fn clone_value(&self) -> Box<dyn Reflect + 'static, Global>
fn clone_value(&self) -> Box<dyn Reflect + 'static, Global>
Reflect
trait object. Read more§fn set(
&mut self,
value: Box<dyn Reflect + 'static, Global>
) -> Result<(), Box<dyn Reflect + 'static, Global>>
fn set(
&mut self,
value: Box<dyn Reflect + 'static, Global>
) -> Result<(), Box<dyn Reflect + 'static, Global>>
§fn apply(&mut self, value: &(dyn Reflect + 'static))
fn apply(&mut self, value: &(dyn Reflect + 'static))
§fn reflect_ref(&self) -> ReflectRef<'_>
fn reflect_ref(&self) -> ReflectRef<'_>
§fn reflect_mut(&mut self) -> ReflectMut<'_>
fn reflect_mut(&mut self) -> ReflectMut<'_>
§fn reflect_owned(self: Box<UiRect, Global>) -> ReflectOwned
fn reflect_owned(self: Box<UiRect, Global>) -> ReflectOwned
§fn reflect_partial_eq(&self, value: &(dyn Reflect + 'static)) -> Option<bool>
fn reflect_partial_eq(&self, value: &(dyn Reflect + 'static)) -> Option<bool>
§fn reflect_hash(&self) -> Option<u64>
fn reflect_hash(&self) -> Option<u64>
§fn debug(&self, f: &mut Formatter<'_>) -> Result<(), Error>
fn debug(&self, f: &mut Formatter<'_>) -> Result<(), Error>
§fn serializable(&self) -> Option<Serializable<'_>>
fn serializable(&self) -> Option<Serializable<'_>>
§impl Struct for UiRect
impl Struct for UiRect
§fn field_at_mut(&mut self, index: usize) -> Option<&mut (dyn Reflect + 'static)>
fn field_at_mut(&mut self, index: usize) -> Option<&mut (dyn Reflect + 'static)>
index
as a &mut dyn Reflect
. Read more§fn iter_fields(&self) -> FieldIter<'_> ⓘ
fn iter_fields(&self) -> FieldIter<'_> ⓘ
§fn clone_dynamic(&self) -> DynamicStruct
fn clone_dynamic(&self) -> DynamicStruct
DynamicStruct
.impl Copy for UiRect
impl StructuralPartialEq for UiRect
Auto Trait Implementations§
impl RefUnwindSafe for UiRect
impl Send for UiRect
impl Sync for UiRect
impl Unpin for UiRect
impl UnwindSafe for UiRect
Blanket Implementations§
§impl<T, U> AsBindGroupShaderType<U> for Twhere
U: ShaderType,
&'a T: for<'a> Into<U>,
impl<T, U> AsBindGroupShaderType<U> for Twhere
U: ShaderType,
&'a T: for<'a> Into<U>,
§fn as_bind_group_shader_type(&self, _images: &RenderAssets<Image>) -> U
fn as_bind_group_shader_type(&self, _images: &RenderAssets<Image>) -> U
T
ShaderType
for self
. When used in AsBindGroup
derives, it is safe to assume that all images in self
exist. Read more§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
§fn into_any(self: Box<T, Global>) -> Box<dyn Any + 'static, Global>
fn into_any(self: Box<T, Global>) -> Box<dyn Any + 'static, Global>
Box<dyn Trait>
(where Trait: Downcast
) to Box<dyn Any>
. Box<dyn Any>
can
then be further downcast
into Box<ConcreteType>
where ConcreteType
implements Trait
. Read more§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any + 'static>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any + 'static>
Rc<Trait>
(where Trait: Downcast
) to Rc<Any>
. Rc<Any>
can then be
further downcast
into Rc<ConcreteType>
where ConcreteType
implements Trait
. Read more§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
&Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &Any
’s vtable from &Trait
’s. Read more§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &mut Any
’s vtable from &mut Trait
’s. Read more§impl<T> FromWorld for Twhere
T: Default,
impl<T> FromWorld for Twhere
T: Default,
§fn from_world(_world: &mut World) -> T
fn from_world(_world: &mut World) -> T
Self
using data from the given World§impl<S> GetField for Swhere
S: Struct,
impl<S> GetField for Swhere
S: Struct,
§impl<T> GetPath for Twhere
T: Reflect,
impl<T> GetPath for Twhere
T: Reflect,
§fn path<'r, 'p>(
&'r self,
path: &'p str
) -> Result<&'r (dyn Reflect + 'static), ReflectPathError<'p>>
fn path<'r, 'p>(
&'r self,
path: &'p str
) -> Result<&'r (dyn Reflect + 'static), ReflectPathError<'p>>
path
. Read more§fn path_mut<'r, 'p>(
&'r mut self,
path: &'p str
) -> Result<&'r mut (dyn Reflect + 'static), ReflectPathError<'p>>
fn path_mut<'r, 'p>(
&'r mut self,
path: &'p str
) -> Result<&'r mut (dyn Reflect + 'static), ReflectPathError<'p>>
path
. Read more§fn get_path<T, 'r, 'p>(
&'r self,
path: &'p str
) -> Result<&'r T, ReflectPathError<'p>>where
T: Reflect,
fn get_path<T, 'r, 'p>(
&'r self,
path: &'p str
) -> Result<&'r T, ReflectPathError<'p>>where
T: Reflect,
path
.§fn get_path_mut<T, 'r, 'p>(
&'r mut self,
path: &'p str
) -> Result<&'r mut T, ReflectPathError<'p>>where
T: Reflect,
fn get_path_mut<T, 'r, 'p>(
&'r mut self,
path: &'p str
) -> Result<&'r mut T, ReflectPathError<'p>>where
T: Reflect,
path
. Read more