From 7b25df4c4dc3ead44dc02015fcc46c1ccb283b8e Mon Sep 17 00:00:00 2001 From: HampusM Date: Fri, 17 Jul 2026 01:23:25 +0200 Subject: feat(engine): add Reflection derives to various types --- engine/src/windowing/dpi.rs | 87 +++++++++++++++++++++++++++------------------ 1 file changed, 52 insertions(+), 35 deletions(-) (limited to 'engine/src/windowing/dpi.rs') diff --git a/engine/src/windowing/dpi.rs b/engine/src/windowing/dpi.rs index e11fa25..d628709 100644 --- a/engine/src/windowing/dpi.rs +++ b/engine/src/windowing/dpi.rs @@ -1,3 +1,5 @@ +use crate::reflection::Reflection; + macro_rules! gen_struct_from_to_impls { ($struct: ident, fields=($($field: ident),*)) => { impl From> for $struct @@ -46,78 +48,88 @@ macro_rules! gen_enum_from_to_impls { }; } -#[derive(Debug, Default, Clone, PartialEq)] +#[derive(Debug, Default, Clone, PartialEq, Reflection)] +#[reflection(impl_with_generics(, , , ))] pub struct PhysicalPosition { pub x: Pixel, - pub y: Pixel + pub y: Pixel, } impl PhysicalPosition { - pub fn to_logical(&self, scale_factor: f64) -> LogicalPosition + pub fn to_logical( + &self, + scale_factor: f64, + ) -> LogicalPosition where Pixel: Into + Clone, - LogicalPixel: From + LogicalPixel: From, { let x = self.x.clone().into(); let y = self.y.clone().into(); LogicalPosition { x: (x / scale_factor).into(), - y: (y / scale_factor).into() + y: (y / scale_factor).into(), } } - pub fn try_convert_from(source: PhysicalPosition) -> Result + pub fn try_convert_from( + source: PhysicalPosition, + ) -> Result where - Pixel: TryFrom + Pixel: TryFrom, { Ok(Self { x: source.x.try_into()?, - y: source.y.try_into()? + y: source.y.try_into()?, }) } } -gen_struct_from_to_impls!(PhysicalPosition, fields=(x, y)); +gen_struct_from_to_impls!(PhysicalPosition, fields = (x, y)); -#[derive(Debug, Default, Clone, PartialEq)] +#[derive(Debug, Default, Clone, PartialEq, Reflection)] +#[reflection(impl_with_generics())] pub struct LogicalPosition { pub x: Pixel, - pub y: Pixel + pub y: Pixel, } impl LogicalPosition { - pub fn try_convert_from(source: LogicalPosition) -> Result + pub fn try_convert_from( + source: LogicalPosition, + ) -> Result where - Pixel: TryFrom + Pixel: TryFrom, { Ok(Self { x: source.x.try_into()?, - y: source.y.try_into()? + y: source.y.try_into()?, }) } } -gen_struct_from_to_impls!(LogicalPosition, fields=(x, y)); +gen_struct_from_to_impls!(LogicalPosition, fields = (x, y)); -#[derive(Debug, Clone, PartialEq)] +#[derive(Debug, Clone, PartialEq, Reflection)] pub enum Position { Physical(PhysicalPosition), - Logical(LogicalPosition) + Logical(LogicalPosition), } -gen_enum_from_to_impls!(Position, variants=(Physical, Logical)); +gen_enum_from_to_impls!(Position, variants = (Physical, Logical)); -#[derive(Debug, Default, Clone, PartialEq)] +#[derive(Debug, Default, Clone, PartialEq, Reflection)] +#[reflection(impl_with_generics())] pub struct PhysicalSize { pub width: Pixel, - pub height: Pixel + pub height: Pixel, } impl PhysicalSize @@ -125,60 +137,65 @@ impl PhysicalSize pub fn to_logical(&self, scale_factor: f64) -> LogicalSize where Pixel: Into + Clone, - LogicalPixel: From + LogicalPixel: From, { let width = self.width.clone().into(); let height = self.height.clone().into(); LogicalSize { width: (width / scale_factor).into(), - height: (height / scale_factor).into() + height: (height / scale_factor).into(), } } } impl PhysicalSize { - pub fn try_convert_from(source: PhysicalSize) -> Result + pub fn try_convert_from( + source: PhysicalSize, + ) -> Result where - Pixel: TryFrom + Pixel: TryFrom, { Ok(Self { width: source.width.try_into()?, - height: source.height.try_into()? + height: source.height.try_into()?, }) } } -gen_struct_from_to_impls!(PhysicalSize, fields=(width, height)); +gen_struct_from_to_impls!(PhysicalSize, fields = (width, height)); -#[derive(Debug, Default, Clone, PartialEq)] +#[derive(Debug, Default, Clone, PartialEq, Reflection)] +#[reflection(impl_with_generics())] pub struct LogicalSize { pub width: Pixel, - pub height: Pixel + pub height: Pixel, } impl LogicalSize { - pub fn try_convert_from(source: LogicalSize) -> Result + pub fn try_convert_from( + source: LogicalSize, + ) -> Result where - Pixel: TryFrom + Pixel: TryFrom, { Ok(Self { width: source.width.try_into()?, - height: source.height.try_into()? + height: source.height.try_into()?, }) } } -gen_struct_from_to_impls!(LogicalSize, fields=(width, height)); +gen_struct_from_to_impls!(LogicalSize, fields = (width, height)); -#[derive(Debug, Clone, PartialEq)] +#[derive(Debug, Clone, PartialEq, Reflection)] pub enum Size { Physical(PhysicalSize), - Logical(LogicalSize) + Logical(LogicalSize), } -gen_enum_from_to_impls!(Size, variants=(Physical, Logical)); +gen_enum_from_to_impls!(Size, variants = (Physical, Logical)); -- cgit v1.2.3-18-g5258