somewhat less hacky...

This commit is contained in:
griffi-gh 2024-03-12 01:26:48 +01:00
parent 3a4b0eea66
commit 7f28aebb97
5 changed files with 15 additions and 30 deletions

View file

@ -6,7 +6,7 @@ use crate::{
input::InputCtx,
layout::LayoutInfo,
measure::Response,
signal::SignalCtx,
signal::SignalStore,
state::StateRepo,
text::{FontHandle, TextMeasure},
UiInstance,
@ -36,7 +36,7 @@ pub struct ProcessContext<'a> {
pub current_font: FontHandle,
pub images: ImageCtx<'a>,
pub input: InputCtx<'a>,
pub signal: SignalCtx<'a>,
pub signal: &'a mut SignalStore,
}
pub trait UiElement {

View file

@ -9,7 +9,6 @@ use crate::{
layout::{Alignment, Alignment2d, Direction, LayoutInfo, Size, Size2d},
measure::{Hints, Response},
rectangle::{Corners, Sides},
signal::SignalCtx,
};
// pub struct Border {
@ -456,7 +455,7 @@ impl UiElement for Container {
input: ctx.input,
//HACK: i have no idea what to do with this
//this sucks
signal: SignalCtx(ctx.signal.0),
signal: ctx.signal,
});
//layout

View file

@ -56,8 +56,7 @@ impl<C: UiSignal + 'static> UiElement for Interactable<C> {
//XXX: should we do this AFTER normal process call of wrapped element?
if ctx.input.check_click(rect) {
if let Some(sig) = self.signal.take() {
//HACK: Fucking whatever
ctx.signal.0.add(sig);
ctx.signal.add(sig);
}
}

View file

@ -8,7 +8,7 @@ use crate::{
event::{EventQueue, UiEvent},
input::UiInputState,
layout::{Direction, LayoutInfo},
signal::{SigIntStore, UiSignal},
signal::{SignalStore, UiSignal},
state::StateRepo,
text::{FontHandle, TextRenderer}
};
@ -29,7 +29,7 @@ pub struct UiInstance {
atlas: TextureAtlasManager,
events: EventQueue,
input: UiInputState,
signal: SigIntStore,
signal: SignalStore,
//True if in the middle of a laying out a frame
state: bool,
}
@ -57,7 +57,7 @@ impl UiInstance {
},
events: EventQueue::new(),
input: UiInputState::new(),
signal: SigIntStore::new(),
signal: SignalStore::new(),
state: false,
}
}
@ -135,7 +135,7 @@ impl UiInstance {
current_font: self.text_renderer.current_font(),
images: self.atlas.context(),
input: self.input.ctx(),
signal: self.signal.ctx(),
signal: &mut self.signal,
});
}

View file

@ -5,18 +5,18 @@ use nohash_hasher::BuildNoHashHasher;
/// A marker trait for signals
pub trait UiSignal: Any {}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, Default)]
pub(crate) struct DummySignal;
impl UiSignal for DummySignal {}
// #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, Default)]
// pub(crate) struct DummySignal;
// impl UiSignal for DummySignal {}
pub(crate) struct SigIntStore {
pub struct SignalStore {
///XXX: is this truly the most efficient structure?
sig: HashMap<TypeId, Vec<Box<dyn Any>>, BuildNoHashHasher<u64>>
}
impl SigIntStore {
impl SignalStore {
/// Create a new [`SigIntStore`]
pub fn new() -> Self {
pub(crate) fn new() -> Self {
Self {
sig: Default::default(),
}
@ -41,22 +41,9 @@ impl SigIntStore {
}
/// Drain all signals of a given type
pub fn drain<T: UiSignal + 'static>(&mut self) -> impl Iterator<Item = T> + '_ {
pub(crate) fn drain<T: UiSignal + 'static>(&mut self) -> impl Iterator<Item = T> + '_ {
self.internal_store::<T>()
.drain(..)
.map(|x| *x.downcast::<T>().unwrap()) //unchecked?
}
pub fn ctx(&mut self) -> SignalCtx {
SignalCtx(self)
}
}
pub struct SignalCtx<'a>(pub(crate) &'a mut SigIntStore);
impl<'a> SignalCtx<'a> {
/// Add a signal to the store
pub fn push<T: UiSignal + 'static>(&mut self, sig: T) {
self.0.add(sig);
}
}