mirror of
https://github.com/griffi-gh/hUI.git
synced 2024-11-28 18:08:42 -06:00
Compare commits
No commits in common. "92f8975702931f4d0fde40feff8c73945a100601" and "ec4404b26cfe16705bdb873f7ac7b8df6f2c4126" have entirely different histories.
92f8975702
...
ec4404b26c
|
@ -6,16 +6,8 @@ use syn::{parse_macro_input, DeriveInput};
|
|||
|
||||
/// Implements `Signal` trait for the given type
|
||||
#[proc_macro_derive(Signal)]
|
||||
pub fn derive_signal(input: TokenStream) -> TokenStream {
|
||||
pub fn signal(input: TokenStream) -> TokenStream {
|
||||
let input = parse_macro_input!(input as DeriveInput);
|
||||
let name = input.ident;
|
||||
quote!(impl ::hui::signal::Signal for #name {}).into()
|
||||
}
|
||||
|
||||
/// Implements `State` trait for the given type
|
||||
#[proc_macro_derive(State)]
|
||||
pub fn derive_state(input: TokenStream) -> TokenStream {
|
||||
let input = parse_macro_input!(input as DeriveInput);
|
||||
let name = input.ident;
|
||||
quote!(impl ::hui::state::State for #name {}).into()
|
||||
}
|
||||
|
|
|
@ -28,7 +28,6 @@ derive_setters = "0.1"
|
|||
derive_more = "0.99"
|
||||
tinyset = "0.4"
|
||||
image = { version = "0.25", default-features = false, optional = true }
|
||||
rustc-hash = "1.1"
|
||||
|
||||
[features]
|
||||
default = ["el_all", "image", "builtin_font", "pixel_perfect_text", "derive"]
|
||||
|
|
|
@ -18,7 +18,9 @@ use crate::{
|
|||
/// In most cases, you should only have one instance of this struct, but multiple instances are allowed\
|
||||
/// (Please note that it's possible to render multiple UI "roots" using a single instance)
|
||||
pub struct UiInstance {
|
||||
//mouse_position: Vec2,
|
||||
stateful_state: StateRepo,
|
||||
//event_queue: VecDeque<UiEvent>,
|
||||
prev_draw_commands: UiDrawCommandList,
|
||||
draw_commands: UiDrawCommandList,
|
||||
draw_call: UiDrawCall,
|
||||
|
@ -28,7 +30,7 @@ pub struct UiInstance {
|
|||
events: EventQueue,
|
||||
input: UiInputState,
|
||||
signal: SignalStore,
|
||||
/// True if in the middle of a laying out a frame
|
||||
//True if in the middle of a laying out a frame
|
||||
state: bool,
|
||||
}
|
||||
|
||||
|
@ -39,7 +41,7 @@ impl UiInstance {
|
|||
pub fn new() -> Self {
|
||||
UiInstance {
|
||||
//mouse_position: Vec2::ZERO,
|
||||
stateful_state: StateRepo::new(),
|
||||
stateful_state: StateRepo::default(),
|
||||
//event_queue: VecDeque::new(),
|
||||
// root_elements: Vec::new(),
|
||||
prev_draw_commands: UiDrawCommandList::default(),
|
||||
|
|
135
hui/src/state.rs
135
hui/src/state.rs
|
@ -2,141 +2,12 @@
|
|||
|
||||
use hashbrown::{HashMap, HashSet};
|
||||
use nohash_hasher::BuildNoHashHasher;
|
||||
use std::{any::Any, hash::{Hash, Hasher}};
|
||||
use rustc_hash::FxHasher;
|
||||
use std::any::Any;
|
||||
|
||||
//TODO impl StateRepo functions and automatic cleanup of inactive ids
|
||||
|
||||
#[cfg(feature = "derive")]
|
||||
pub use hui_derive::State;
|
||||
|
||||
/// Marker trait for state objects
|
||||
pub trait State: Any {}
|
||||
|
||||
/// Integer type used to identify a state object
|
||||
type StateId = u64;
|
||||
|
||||
fn hash_local(x: impl Hash, g: &[StateId]) -> StateId {
|
||||
let mut hasher = FxHasher::default();
|
||||
0xdeadbeefu64.hash(&mut hasher);
|
||||
for x in g {
|
||||
x.hash(&mut hasher);
|
||||
}
|
||||
x.hash(&mut hasher);
|
||||
hasher.finish()
|
||||
}
|
||||
|
||||
fn hash_global(x: impl Hash) -> StateId {
|
||||
let mut hasher = FxHasher::default();
|
||||
0xcafebabeu64.hash(&mut hasher);
|
||||
x.hash(&mut hasher);
|
||||
hasher.finish()
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct StateRepo {
|
||||
/// Stack of ids used to identify state objects
|
||||
id_stack: Vec<StateId>,
|
||||
|
||||
/// Implementation detail: used to prevent needlessly reallocating the id stack if the `global`` function is used
|
||||
standby: Vec<StateId>,
|
||||
|
||||
/// Actual state objects
|
||||
state: HashMap<StateId, Box<dyn Any>, BuildNoHashHasher<StateId>>,
|
||||
|
||||
/// IDs that were accessed during the current frame, everything else is considered inactive and can be cleaned up
|
||||
active_ids: HashSet<StateId, BuildNoHashHasher<StateId>>
|
||||
}
|
||||
|
||||
impl StateRepo {
|
||||
/// Push an id to the stack
|
||||
pub fn push(&mut self, id: impl Hash) {
|
||||
self.id_stack.push(hash_global(id));
|
||||
}
|
||||
|
||||
/// Pop the last id from the stack
|
||||
///
|
||||
/// ## Panics:
|
||||
/// Panics if the stack is empty
|
||||
pub fn pop(&mut self) {
|
||||
self.id_stack.pop().expect("stack is empty");
|
||||
}
|
||||
|
||||
/// Create a new [`StateRepo`]
|
||||
pub(crate) fn new() -> Self {
|
||||
Self::default()
|
||||
}
|
||||
|
||||
/// Get a reference to a state object by its id
|
||||
pub fn acquire<T: State>(&mut self, id: impl Hash) -> Option<&T> {
|
||||
let id = hash_local(id, &self.id_stack);
|
||||
self.active_ids.insert(id);
|
||||
self.state.get(&id).unwrap().downcast_ref::<T>()
|
||||
}
|
||||
|
||||
/// Get a reference to a state object by its id or insert a new one
|
||||
pub fn acquire_or_insert<T: State>(&mut self, id: impl Hash, state: T) -> &T {
|
||||
let id = hash_local(id, &self.id_stack);
|
||||
self.state.entry(id)
|
||||
.or_insert_with(|| Box::new(state))
|
||||
.downcast_ref::<T>().unwrap()
|
||||
}
|
||||
|
||||
/// Get a reference to a state object by its id or insert a new default one
|
||||
pub fn acquire_or_default<T: State + Default>(&mut self, id: impl Hash) -> &T {
|
||||
let id = hash_local(id, &self.id_stack);
|
||||
self.state.entry(id)
|
||||
.or_insert_with(|| Box::<T>::default())
|
||||
.downcast_ref::<T>().unwrap()
|
||||
}
|
||||
|
||||
/// Get a mutable reference to a state object by its id
|
||||
pub fn acquire_mut<T: State>(&mut self, id: impl Hash) -> Option<&mut T> {
|
||||
let id = hash_local(id, &self.id_stack);
|
||||
self.active_ids.insert(id);
|
||||
self.state.get_mut(&id).unwrap().downcast_mut::<T>()
|
||||
}
|
||||
|
||||
/// Get a mutable reference to a state object by its id or insert a new one
|
||||
pub fn acquire_mut_or_insert<T: State>(&mut self, id: impl Hash, state: T) -> &mut T {
|
||||
let id = hash_local(id, &self.id_stack);
|
||||
self.state.entry(id)
|
||||
.or_insert_with(|| Box::new(state))
|
||||
.downcast_mut::<T>().unwrap()
|
||||
}
|
||||
|
||||
/// Get a mutable reference to a state object by its id or insert a new default one
|
||||
pub fn acquire_mut_or_default<T: State + Default>(&mut self, id: impl Hash) -> &mut T {
|
||||
let id = hash_local(id, &self.id_stack);
|
||||
self.state.entry(id)
|
||||
.or_insert_with(|| Box::<T>::default())
|
||||
.downcast_mut::<T>().unwrap()
|
||||
}
|
||||
|
||||
/// Temporarily forget about current id stack, and use an empty one (within the context of the closure)
|
||||
///
|
||||
/// Can be useful for state management of non-hierarchical objects, e.g. popups
|
||||
pub fn global<R>(&mut self, f: impl FnOnce(&mut Self) -> R) -> R {
|
||||
self.standby.clear();
|
||||
std::mem::swap(&mut self.id_stack, &mut self.standby);
|
||||
let ret = f(self);
|
||||
std::mem::swap(&mut self.id_stack, &mut self.standby);
|
||||
ret
|
||||
}
|
||||
|
||||
/// Scope the state repo
|
||||
///
|
||||
/// Anything pushed or popped will be discarded after the closure,
|
||||
/// and the stack will be restored to its previous state
|
||||
pub fn scope<R>(&mut self, f: impl FnOnce(&mut Self) -> R) -> R {
|
||||
self.standby.clear();
|
||||
self.standby.extend(self.id_stack.iter().copied());
|
||||
let ret = f(self);
|
||||
std::mem::swap(&mut self.id_stack, &mut self.standby);
|
||||
ret
|
||||
//XXX: this is super efficient, but works only for pushes, if anything is popped, it will be lost
|
||||
// let len = self.id_stack.len();
|
||||
// let ret = f(self);
|
||||
// self.id_stack.truncate(len);
|
||||
}
|
||||
state: HashMap<u64, Box<dyn Any>, BuildNoHashHasher<u64>>,
|
||||
active_ids: HashSet<u64, BuildNoHashHasher<u64>>
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue