From e22fa739c187b1b02f129e6412f2d1d81290ea81 Mon Sep 17 00:00:00 2001 From: griffi-gh Date: Sat, 23 Mar 2024 15:50:44 +0100 Subject: [PATCH] use new trigger api for interactable --- hui-examples/examples/ui_test_5_input.rs | 4 +-- hui/src/element/builtin/interactable.rs | 35 ++++++++++++------------ 2 files changed, 20 insertions(+), 19 deletions(-) diff --git a/hui-examples/examples/ui_test_5_input.rs b/hui-examples/examples/ui_test_5_input.rs index 619012f..9161af4 100644 --- a/hui-examples/examples/ui_test_5_input.rs +++ b/hui-examples/examples/ui_test_5_input.rs @@ -53,7 +53,7 @@ ui_main!( .with_text_size(32) .add_child(ui); }) - .on_click(CounterSignal::Decrement) + .on_click(|| CounterSignal::Decrement) .add_child(ui); Container::default() .with_size(size!(60, auto)) @@ -72,7 +72,7 @@ ui_main!( .with_text_size(32) .add_child(ui); }) - .on_click(CounterSignal::Increment) + .on_click(|| CounterSignal::Increment) .add_child(ui); Br.add_child(ui); for _ in 0..*counter { diff --git a/hui/src/element/builtin/interactable.rs b/hui/src/element/builtin/interactable.rs index c2e63a3..f86c0b8 100644 --- a/hui/src/element/builtin/interactable.rs +++ b/hui/src/element/builtin/interactable.rs @@ -5,9 +5,8 @@ use crate::{ element::{MeasureContext, ProcessContext, UiElement}, - signal::Signal, + signal::{trigger::SignalTrigger, Signal}, }; -use std::cell::RefCell; #[non_exhaustive] #[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)] @@ -18,7 +17,7 @@ pub enum InteractableEvent { } /// Wrapper that allows adding click and hover events to any element -pub struct Interactable { +pub struct Interactable { /// The wrapped element that will be interactable pub element: Box, @@ -26,20 +25,24 @@ pub struct Interactable { pub event: InteractableEvent, /// Signal that will be called if the element was clicked in the current frame - pub signal: RefCell>, + pub signal: SignalTrigger, } -impl Interactable { - pub fn new(element: Box, event: InteractableEvent, signal: C) -> Self { +impl Interactable { + pub fn new S + 'static>( + element: Box, + event: InteractableEvent, + signal: F + ) -> Self { Self { element, event, - signal: RefCell::new(Some(signal)), + signal: SignalTrigger::new(signal), } } } -impl UiElement for Interactable { +impl UiElement for Interactable { fn name(&self) -> &'static str { "Interactable" } @@ -59,9 +62,7 @@ impl UiElement for Interactable { }; if event_happened { - if let Some(sig) = self.signal.take() { - ctx.signal.add(sig); - } + self.signal.fire(ctx.signal); } self.element.process(ctx) @@ -71,25 +72,25 @@ impl UiElement for Interactable { /// Extension trait for [`UiElement`] that adds methods to wrap the element in an [`Interactable`] pub trait ElementInteractableExt: UiElement { /// Wrap the element in an [`Interactable`] that will call the given signal when the specified event occurs - fn into_interactable(self, event: InteractableEvent, signal: C) -> Interactable; + fn into_interactable S + 'static>(self, event: InteractableEvent, signal: F) -> Interactable; /// Wrap the element in an [`Interactable`] that will call the given signal when clicked - fn on_click(self, signal: C) -> Interactable; + fn on_click S + 'static>(self, signal: F) -> Interactable; /// Wrap the element in an [`Interactable`] that will call the given signal while hovered - fn on_hover(self, signal: C) -> Interactable; + fn on_hover S + 'static>(self, signal: F) -> Interactable; } impl ElementInteractableExt for T { - fn into_interactable(self, event: InteractableEvent, signal: C) -> Interactable { + fn into_interactable S + 'static>(self, event: InteractableEvent, signal: F) -> Interactable { Interactable::new(Box::new(self), event, signal) } - fn on_click(self, signal: C) -> Interactable { + fn on_click S + 'static>(self, signal: F) -> Interactable { self.into_interactable(InteractableEvent::Click, signal) } - fn on_hover(self, signal: C) -> Interactable { + fn on_hover S + 'static>(self, signal: F) -> Interactable { self.into_interactable(InteractableEvent::Hover, signal) } }