From 7a64a6b750443cd158984fe1b4a039fe6b82a2ef Mon Sep 17 00:00:00 2001 From: griffi-gh Date: Sun, 24 Mar 2024 02:19:26 +0100 Subject: [PATCH] make hacky macro even hackier --- hui-examples/examples/mom_downloader.rs | 10 ++++----- hui-examples/examples/ui_test_3_transform.rs | 10 ++++----- hui/src/macros.rs | 22 +++++++++++++++++--- 3 files changed, 29 insertions(+), 13 deletions(-) diff --git a/hui-examples/examples/mom_downloader.rs b/hui-examples/examples/mom_downloader.rs index 2964457..5e66bb7 100644 --- a/hui-examples/examples/mom_downloader.rs +++ b/hui-examples/examples/mom_downloader.rs @@ -5,7 +5,7 @@ use hui::{ progress_bar::ProgressBar, text::Text, UiElementExt, - }, frame::FrameRect, layout::{Alignment, Direction}, size + }, frame::FrameRect, frame_rect, layout::{Alignment, Direction}, size }; #[path = "../boilerplate.rs"] @@ -31,10 +31,10 @@ ui_main!{ .with_gap(5.) .with_padding(10.) .with_size(size!(450, auto)) - .with_background( - FrameRect::color((0.2, 0.2, 0.5)) - .with_corner_radius(8.) - ) + .with_background(frame_rect! { + color: (0.2, 0.2, 0.5), + corner_radius: 8. + }) .with_children(|ui| { if instant.elapsed().as_secs_f32() < 5. { Text::default() diff --git a/hui-examples/examples/ui_test_3_transform.rs b/hui-examples/examples/ui_test_3_transform.rs index 178d4e2..ea0a52d 100644 --- a/hui-examples/examples/ui_test_3_transform.rs +++ b/hui-examples/examples/ui_test_3_transform.rs @@ -7,7 +7,7 @@ use hui::{ text::Text, transformer::ElementTransformExt, UiElementExt - }, frame::FrameRect, layout::Alignment, rect::Corners, size, text::FontHandle + }, frame::FrameRect, frame_rect, layout::Alignment, rect::Corners, size, text::FontHandle }; #[path = "../boilerplate.rs"] @@ -37,10 +37,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 95962c6..8de75d9 100644 --- a/hui/src/macros.rs +++ b/hui/src/macros.rs @@ -64,10 +64,14 @@ macro_rules! size { /// Helper macro for constructing a `FrameRect` #[macro_export] macro_rules! frame_rect { - () => { + {} => { $crate::frame::FrameRect::default() }; + // () => { + // $crate::frame::FrameRect::default() + // }; + ($expr:expr) => { { let _frame_rect: $crate::frame::FrameRect = $crate::frame::FrameRect::from($expr); @@ -88,10 +92,22 @@ macro_rules! frame_rect { // construct the FrameRect { let mut frame_rect = $crate::frame::FrameRect::default(); + let mut _color_is_set = false; + let mut _image_is_set = false; $( - let $ident = ($expr).into(); - frame_rect.$ident = $ident; + { + frame_rect.$ident = ($expr).into(); + if stringify!($ident) == "image" { + _image_is_set = true; + } + if stringify!($ident) == "color" { + _color_is_set = true; + } + } )+ + if frame_rect.image.is_some() && _image_is_set && !_color_is_set { + frame_rect.color = (1., 1., 1., 1.).into(); + } frame_rect } }