From 04ebfd41e89a3cf2d8bfb6f3ff46547aefa80137 Mon Sep 17 00:00:00 2001 From: griffi-gh Date: Sun, 24 Mar 2024 02:05:28 +0100 Subject: [PATCH] `frame_rect` macro --- hui-examples/examples/ui_test_2_loading.rs | 21 +++++-- hui/src/macros.rs | 68 ++++++++++++++++++++++ 2 files changed, 84 insertions(+), 5 deletions(-) diff --git a/hui-examples/examples/ui_test_2_loading.rs b/hui-examples/examples/ui_test_2_loading.rs index dec5cd3..73c911c 100644 --- a/hui-examples/examples/ui_test_2_loading.rs +++ b/hui-examples/examples/ui_test_2_loading.rs @@ -1,6 +1,17 @@ use glam::vec4; use hui::{ - color, element::{container::Container, progress_bar::ProgressBar, text::Text, UiElementExt}, frame::FrameRect, layout::Alignment, rect::Corners, size, text::FontHandle + size, frame_rect, + color, + element::{ + container::Container, + progress_bar::ProgressBar, + text::Text, + UiElementExt + }, + frame::FrameRect, + layout::Alignment, + rect::Corners, + text::FontHandle }; #[path = "../boilerplate.rs"] @@ -59,10 +70,10 @@ ui_main!( .with_align((Alignment::Center, Alignment::Begin)) .with_padding(15.) .with_gap(10.) - .with_background( - FrameRect::color((0., 0., 0., 0.5)) - .with_corner_radius(8.) - ) + .with_background(frame_rect! { + color: (0., 0., 0., 0.5), + corner_radius: 8., + }) .with_children(|ui| { Text::default() .with_text("Did you know?") diff --git a/hui/src/macros.rs b/hui/src/macros.rs index 434e248..95962c6 100644 --- a/hui/src/macros.rs +++ b/hui/src/macros.rs @@ -60,3 +60,71 @@ macro_rules! size { } }; } + +/// Helper macro for constructing a `FrameRect` +#[macro_export] +macro_rules! frame_rect { + () => { + $crate::frame::FrameRect::default() + }; + + ($expr:expr) => { + { + let _frame_rect: $crate::frame::FrameRect = $crate::frame::FrameRect::from($expr); + _frame_rect + } + }; + + ($image:expr, $color:expr) => { + $crate::frame::FrameRect::color_image($color, $image) + }; + + {$($ident:ident : $expr:expr),+$(,)?} => { + { + // ensure all identifiers are unique + #[allow(non_upper_case_globals)] + {$(const $ident: () = ();)+} + + // construct the FrameRect + { + let mut frame_rect = $crate::frame::FrameRect::default(); + $( + let $ident = ($expr).into(); + frame_rect.$ident = $ident; + )+ + frame_rect + } + } + }; + + // {$from:expr, $($ident:ident : $expr:expr),+$(,)?} => { + // { + // // ensure all identifiers are unique + // #[allow(non_upper_case_globals)] + // { + // $( + // const $ident: () = (); + // )+ + // } + // // construct the FrameRect + // { + // let mut _frame_rect: $crate::frame::FrameRect = ($from).into(); + // $( + // let $ident = ($expr).into(); + // _frame_rect.$ident = $ident; + // )+ + // _frame_rect + // } + // } + // }; +} + +// #[allow(unused)] +// fn test() { +// // let _ = frame_rect!(5, 6); + +// let _ = frame_rect! { +// color: (0.2, 0.2, 0.3, 1.), +// corner_radius: 5., +// }; +// }