bevy_math/
aspect_ratio.rs1use crate::Vec2;
4use derive_more::derive::Into;
5use thiserror::Error;
6
7#[cfg(feature = "bevy_reflect")]
8use bevy_reflect::Reflect;
9
10#[derive(#[automatically_derived]
impl ::core::marker::Copy for AspectRatio { }Copy, #[automatically_derived]
impl ::core::clone::Clone for AspectRatio {
#[inline]
fn clone(&self) -> AspectRatio {
let _: ::core::clone::AssertParamIsClone<f32>;
*self
}
}Clone, #[automatically_derived]
impl ::core::fmt::Debug for AspectRatio {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::debug_tuple_field1_finish(f, "AspectRatio",
&&self.0)
}
}Debug, #[automatically_derived]
impl ::core::cmp::PartialEq for AspectRatio {
#[inline]
fn eq(&self, other: &AspectRatio) -> bool { self.0 == other.0 }
}PartialEq, #[automatically_derived]
impl ::core::cmp::PartialOrd for AspectRatio {
#[inline]
fn partial_cmp(&self, other: &AspectRatio)
-> ::core::option::Option<::core::cmp::Ordering> {
::core::cmp::PartialOrd::partial_cmp(&self.0, &other.0)
}
}PartialOrd, #[allow(clippy :: unused_unit)]
#[allow(deprecated)]
#[automatically_derived]
impl derive_more::core::convert::From<AspectRatio> for (f32) {
#[inline]
fn from(value: AspectRatio) -> Self {
(<f32 as derive_more::core::convert::From<_>>::from(value.0))
}
}Into)]
12#[cfg_attr(
13 feature = "bevy_reflect",
14 derive(const _: () =
{
impl bevy_reflect::GetTypeRegistration for AspectRatio where {
fn get_type_registration() -> bevy_reflect::TypeRegistration {
let mut registration =
bevy_reflect::TypeRegistration::of::<Self>();
registration.insert::<bevy_reflect::ReflectFromPtr>(bevy_reflect::FromType::<Self>::from_type());
registration.insert::<bevy_reflect::ReflectFromReflect>(bevy_reflect::FromType::<Self>::from_type());
registration
}
#[inline(never)]
fn register_type_dependencies(registry:
&mut bevy_reflect::TypeRegistry) {
<f32 as
bevy_reflect::__macro_exports::RegisterForReflection>::__register(registry);
}
}
impl bevy_reflect::Typed for AspectRatio where {
#[inline]
fn type_info() -> &'static bevy_reflect::TypeInfo {
static CELL: bevy_reflect::utility::NonGenericTypeInfoCell =
bevy_reflect::utility::NonGenericTypeInfoCell::new();
CELL.get_or_set(||
{
bevy_reflect::TypeInfo::TupleStruct(bevy_reflect::tuple_struct::TupleStructInfo::new::<Self>(&[bevy_reflect::UnnamedField::new::<f32>(0usize)]))
})
}
}
#[allow(deprecated, reason =
"derives on a deprecated type shouldn't be considered a usage")]
impl bevy_reflect::TypePath for AspectRatio where {
fn type_path() -> &'static str {
"bevy_math::aspect_ratio::AspectRatio"
}
fn short_type_path() -> &'static str { "AspectRatio" }
fn type_ident() -> ::core::option::Option<&'static str> {
::core::option::Option::Some("AspectRatio")
}
fn crate_name() -> ::core::option::Option<&'static str> {
::core::option::Option::Some("bevy_math::aspect_ratio".split(':').next().unwrap())
}
fn module_path() -> ::core::option::Option<&'static str> {
::core::option::Option::Some("bevy_math::aspect_ratio")
}
}
impl bevy_reflect::Reflect for AspectRatio where {
#[inline]
fn into_any(self:
bevy_reflect::__macro_exports::alloc_utils::Box<Self>)
->
bevy_reflect::__macro_exports::alloc_utils::Box<dyn ::core::any::Any> {
self
}
#[inline]
fn as_any(&self) -> &dyn ::core::any::Any { self }
#[inline]
fn as_any_mut(&mut self) -> &mut dyn ::core::any::Any { self }
#[inline]
fn into_reflect(self:
bevy_reflect::__macro_exports::alloc_utils::Box<Self>)
->
bevy_reflect::__macro_exports::alloc_utils::Box<dyn bevy_reflect::Reflect> {
self
}
#[inline]
fn as_reflect(&self) -> &dyn bevy_reflect::Reflect { self }
#[inline]
fn as_reflect_mut(&mut self) -> &mut dyn bevy_reflect::Reflect {
self
}
#[inline]
fn set(&mut self,
value:
bevy_reflect::__macro_exports::alloc_utils::Box<dyn bevy_reflect::Reflect>)
->
::core::result::Result<(),
bevy_reflect::__macro_exports::alloc_utils::Box<dyn bevy_reflect::Reflect>> {
*self = <dyn bevy_reflect::Reflect>::take(value)?;
::core::result::Result::Ok(())
}
}
impl bevy_reflect::tuple_struct::TupleStruct for AspectRatio where {
fn field(&self, index: usize)
-> ::core::option::Option<&dyn bevy_reflect::PartialReflect> {
match index {
0usize => ::core::option::Option::Some(&self.0),
_ => ::core::option::Option::None,
}
}
fn field_mut(&mut self, index: usize)
->
::core::option::Option<&mut dyn bevy_reflect::PartialReflect> {
match index {
0usize => ::core::option::Option::Some(&mut self.0),
_ => ::core::option::Option::None,
}
}
#[inline]
fn field_len(&self) -> usize { 1usize }
#[inline]
fn iter_fields(&self)
-> bevy_reflect::tuple_struct::TupleStructFieldIter {
bevy_reflect::tuple_struct::TupleStructFieldIter::new(self)
}
fn to_dynamic_tuple_struct(&self)
-> bevy_reflect::tuple_struct::DynamicTupleStruct {
let mut dynamic:
bevy_reflect::tuple_struct::DynamicTupleStruct =
::core::default::Default::default();
dynamic.set_represented_type(bevy_reflect::PartialReflect::get_represented_type_info(self));
dynamic.insert_boxed(bevy_reflect::PartialReflect::to_dynamic(&self.0));
dynamic
}
}
impl bevy_reflect::PartialReflect for AspectRatio where {
#[inline]
fn get_represented_type_info(&self)
-> ::core::option::Option<&'static bevy_reflect::TypeInfo> {
::core::option::Option::Some(<Self as
bevy_reflect::Typed>::type_info())
}
#[inline]
fn try_apply(&mut self, value: &dyn bevy_reflect::PartialReflect)
-> ::core::result::Result<(), bevy_reflect::ApplyError> {
if let bevy_reflect::ReflectRef::TupleStruct(struct_value) =
bevy_reflect::PartialReflect::reflect_ref(value) {
for (i, value) in
::core::iter::Iterator::enumerate(bevy_reflect::tuple_struct::TupleStruct::iter_fields(struct_value))
{
if let ::core::option::Option::Some(v) =
bevy_reflect::tuple_struct::TupleStruct::field_mut(self, i)
{
bevy_reflect::PartialReflect::try_apply(v, value)?;
}
}
} else {
return ::core::result::Result::Err(bevy_reflect::ApplyError::MismatchedKinds {
from_kind: bevy_reflect::PartialReflect::reflect_kind(value),
to_kind: bevy_reflect::ReflectKind::TupleStruct,
});
}
::core::result::Result::Ok(())
}
#[inline]
fn reflect_kind(&self) -> bevy_reflect::ReflectKind {
bevy_reflect::ReflectKind::TupleStruct
}
#[inline]
fn reflect_ref(&self) -> bevy_reflect::ReflectRef {
bevy_reflect::ReflectRef::TupleStruct(self)
}
#[inline]
fn reflect_mut(&mut self) -> bevy_reflect::ReflectMut {
bevy_reflect::ReflectMut::TupleStruct(self)
}
#[inline]
fn reflect_owned(self:
bevy_reflect::__macro_exports::alloc_utils::Box<Self>)
-> bevy_reflect::ReflectOwned {
bevy_reflect::ReflectOwned::TupleStruct(self)
}
#[inline]
fn try_into_reflect(self:
bevy_reflect::__macro_exports::alloc_utils::Box<Self>)
->
::core::result::Result<bevy_reflect::__macro_exports::alloc_utils::Box<dyn bevy_reflect::Reflect>,
bevy_reflect::__macro_exports::alloc_utils::Box<dyn bevy_reflect::PartialReflect>> {
::core::result::Result::Ok(self)
}
#[inline]
fn try_as_reflect(&self)
-> ::core::option::Option<&dyn bevy_reflect::Reflect> {
::core::option::Option::Some(self)
}
#[inline]
fn try_as_reflect_mut(&mut self)
-> ::core::option::Option<&mut dyn bevy_reflect::Reflect> {
::core::option::Option::Some(self)
}
#[inline]
fn into_partial_reflect(self:
bevy_reflect::__macro_exports::alloc_utils::Box<Self>)
->
bevy_reflect::__macro_exports::alloc_utils::Box<dyn bevy_reflect::PartialReflect> {
self
}
#[inline]
fn as_partial_reflect(&self)
-> &dyn bevy_reflect::PartialReflect {
self
}
#[inline]
fn as_partial_reflect_mut(&mut self)
-> &mut dyn bevy_reflect::PartialReflect {
self
}
fn reflect_partial_eq(&self,
value: &dyn bevy_reflect::PartialReflect)
-> ::core::option::Option<bool> {
let value =
<dyn bevy_reflect::PartialReflect>::try_downcast_ref::<Self>(value);
if let ::core::option::Option::Some(value) = value {
::core::option::Option::Some(::core::cmp::PartialEq::eq(self,
value))
} else { ::core::option::Option::Some(false) }
}
fn reflect_partial_cmp(&self,
value: &dyn bevy_reflect::PartialReflect)
-> ::core::option::Option<::core::cmp::Ordering> {
(bevy_reflect::tuple_struct::tuple_struct_partial_cmp)(self,
value)
}
fn debug(&self, f: &mut ::core::fmt::Formatter<'_>)
-> ::core::fmt::Result {
::core::fmt::Debug::fmt(self, f)
}
#[inline]
fn reflect_clone(&self)
->
::core::result::Result<bevy_reflect::__macro_exports::alloc_utils::Box<dyn bevy_reflect::Reflect>,
bevy_reflect::ReflectCloneError> {
::core::result::Result::Ok(bevy_reflect::__macro_exports::alloc_utils::Box::new(::core::clone::Clone::clone(self)))
}
}
impl bevy_reflect::FromReflect for AspectRatio where {
fn from_reflect(reflect: &dyn bevy_reflect::PartialReflect)
-> ::core::option::Option<Self> {
if let bevy_reflect::ReflectRef::TupleStruct(__ref_struct) =
bevy_reflect::PartialReflect::reflect_ref(reflect) {
let __this =
Self {
0: <f32 as
bevy_reflect::FromReflect>::from_reflect(bevy_reflect::tuple_struct::TupleStruct::field(__ref_struct,
0)?)?,
};
::core::option::Option::Some(__this)
} else { ::core::option::Option::None }
}
}
};Reflect),
15 reflect(Debug, PartialEq, Clone)
16)]
17pub struct AspectRatio(f32);
18
19impl AspectRatio {
20 pub const SIXTEEN_NINE: Self = Self(16.0 / 9.0);
22 pub const FOUR_THREE: Self = Self(4.0 / 3.0);
24 pub const ULTRAWIDE: Self = Self(21.0 / 9.0);
26
27 #[inline]
36 pub const fn try_new(width: f32, height: f32) -> Result<Self, AspectRatioError> {
37 match (width, height) {
38 (w, h) if w == 0.0 || h == 0.0 => Err(AspectRatioError::Zero),
39 (w, h) if w.is_infinite() || h.is_infinite() => Err(AspectRatioError::Infinite),
40 (w, h) if w.is_nan() || h.is_nan() => Err(AspectRatioError::NaN),
41 _ => Ok(Self(width / height)),
42 }
43 }
44
45 #[inline]
47 pub const fn try_from_pixels(x: u32, y: u32) -> Result<Self, AspectRatioError> {
48 Self::try_new(x as f32, y as f32)
49 }
50
51 #[inline]
53 pub const fn ratio(&self) -> f32 {
54 self.0
55 }
56
57 #[inline]
59 pub const fn inverse(&self) -> Self {
60 Self(1.0 / self.0)
61 }
62
63 #[inline]
65 pub const fn is_landscape(&self) -> bool {
66 self.0 > 1.0
67 }
68
69 #[inline]
71 pub const fn is_portrait(&self) -> bool {
72 self.0 < 1.0
73 }
74
75 #[inline]
77 pub const fn is_square(&self) -> bool {
78 self.0 == 1.0
79 }
80}
81
82impl TryFrom<Vec2> for AspectRatio {
83 type Error = AspectRatioError;
84
85 #[inline]
86 fn try_from(value: Vec2) -> Result<Self, Self::Error> {
87 Self::try_new(value.x, value.y)
88 }
89}
90
91#[derive(#[allow(unused_qualifications)]
#[automatically_derived]
impl ::core::fmt::Display for AspectRatioError {
fn fmt(&self, __formatter: &mut ::core::fmt::Formatter)
-> ::core::fmt::Result {
#[allow(unused_variables, deprecated, clippy ::
used_underscore_binding)]
match self {
AspectRatioError::Zero {} =>
__formatter.write_str("AspectRatio error: width or height is zero"),
AspectRatioError::Infinite {} =>
__formatter.write_str("AspectRatio error: width or height is infinite"),
AspectRatioError::NaN {} =>
__formatter.write_str("AspectRatio error: width or height is NaN"),
}
}
}Error, #[automatically_derived]
impl ::core::fmt::Debug for AspectRatioError {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::write_str(f,
match self {
AspectRatioError::Zero => "Zero",
AspectRatioError::Infinite => "Infinite",
AspectRatioError::NaN => "NaN",
})
}
}Debug, #[automatically_derived]
impl ::core::cmp::PartialEq for AspectRatioError {
#[inline]
fn eq(&self, other: &AspectRatioError) -> bool {
let __self_discr = ::core::intrinsics::discriminant_value(self);
let __arg1_discr = ::core::intrinsics::discriminant_value(other);
__self_discr == __arg1_discr
}
}PartialEq, #[automatically_derived]
impl ::core::cmp::Eq for AspectRatioError {
#[inline]
#[doc(hidden)]
#[coverage(off)]
fn assert_fields_are_eq(&self) {}
}Eq, #[automatically_derived]
impl ::core::clone::Clone for AspectRatioError {
#[inline]
fn clone(&self) -> AspectRatioError { *self }
}Clone, #[automatically_derived]
impl ::core::marker::Copy for AspectRatioError { }Copy)]
93pub enum AspectRatioError {
94 #[error("AspectRatio error: width or height is zero")]
96 Zero,
97 #[error("AspectRatio error: width or height is infinite")]
99 Infinite,
100 #[error("AspectRatio error: width or height is NaN")]
102 NaN,
103}