remove state generic from a lot of things
This commit is contained in:
1 parent
7e6369029f
commit
30bc55c78e
45 files changed
+740
-856
No files matched your search
+34
-58
@@ -1,6 +1,6 @@
|
||||
use crate::{
|
||||
Event, EventFn, EventLike, EventManager, IdLike, Mask, PixelRegion, PrimitiveLayers, TextData,
|
||||
TextureHandle, Textures, Widget, WidgetHandle, WidgetId, WidgetLike, Widgets,
|
||||
EventsLike, IdLike, Mask, PixelRegion, PrimitiveLayers, TextData, TextureHandle, Textures,
|
||||
Widget, WidgetHandle, WidgetId, Widgets,
|
||||
ui::draw_state::DrawState,
|
||||
util::{HashMap, TrackedArena, Vec2},
|
||||
};
|
||||
@@ -22,10 +22,9 @@ use cache::*;
|
||||
pub use painter::Painter;
|
||||
pub use size::*;
|
||||
|
||||
pub struct Ui<State> {
|
||||
pub struct Ui {
|
||||
// TODO: edit visibilities
|
||||
pub widgets: Widgets<State>,
|
||||
pub events: EventManager<State>,
|
||||
pub widgets: Widgets,
|
||||
|
||||
// retained painter state
|
||||
pub active: HashMap<WidgetId, ActiveData>,
|
||||
@@ -36,56 +35,44 @@ pub struct Ui<State> {
|
||||
pub masks: TrackedArena<Mask, u32>,
|
||||
pub cache: Cache,
|
||||
|
||||
root: Option<WidgetHandle<State>>,
|
||||
pub root: Option<WidgetHandle>,
|
||||
old_root: Option<WidgetId>,
|
||||
recv: Receiver<WidgetId>,
|
||||
send: Sender<WidgetId>,
|
||||
full_redraw: bool,
|
||||
resized: bool,
|
||||
}
|
||||
|
||||
pub trait HasUi: Sized {
|
||||
fn ui_ref(&self) -> &Ui<Self>;
|
||||
fn ui(&mut self) -> &mut Ui<Self>;
|
||||
pub trait HasUi: Sized + 'static {
|
||||
fn ui_ref(&self) -> &Ui;
|
||||
fn ui(&mut self) -> &mut Ui;
|
||||
}
|
||||
|
||||
impl<State: 'static> Ui<State> {
|
||||
pub fn add<W: Widget<State>, Tag>(
|
||||
&mut self,
|
||||
w: impl WidgetLike<State, Tag, Widget = W>,
|
||||
) -> WidgetHandle<State, W> {
|
||||
w.add(self)
|
||||
}
|
||||
|
||||
impl Ui {
|
||||
/// useful for debugging
|
||||
pub fn set_label(&mut self, id: impl IdLike<State>, label: String) {
|
||||
pub fn set_label(&mut self, id: impl IdLike, label: String) {
|
||||
self.widgets.data_mut(id.id()).unwrap().label = label;
|
||||
}
|
||||
|
||||
pub fn label(&self, id: impl IdLike<State>) -> &String {
|
||||
pub fn label(&self, id: impl IdLike) -> &String {
|
||||
&self.widgets.data(id.id()).unwrap().label
|
||||
}
|
||||
|
||||
pub fn add_widget<W: Widget<State>>(&mut self, w: W) -> WidgetHandle<State, W> {
|
||||
pub fn add_widget<W: Widget>(&mut self, w: W) -> WidgetHandle<W> {
|
||||
WidgetHandle::new(self.widgets.add(w), self.send.clone())
|
||||
}
|
||||
|
||||
pub fn set_root<Tag>(&mut self, w: impl WidgetLike<State, Tag>) {
|
||||
self.root = Some(w.add(self));
|
||||
self.full_redraw = true;
|
||||
}
|
||||
|
||||
pub fn new() -> Self {
|
||||
Self::default()
|
||||
}
|
||||
|
||||
pub fn get<I: IdLike<State>>(&self, id: &I) -> Option<&I::Widget>
|
||||
pub fn get<I: IdLike>(&self, id: &I) -> Option<&I::Widget>
|
||||
where
|
||||
I::Widget: Sized,
|
||||
{
|
||||
self.widgets.get(id)
|
||||
}
|
||||
|
||||
pub fn get_mut<I: IdLike<State>>(&mut self, id: &I) -> Option<&mut I::Widget>
|
||||
pub fn get_mut<I: IdLike>(&mut self, id: &I) -> Option<&mut I::Widget>
|
||||
where
|
||||
I::Widget: Sized,
|
||||
{
|
||||
@@ -96,49 +83,39 @@ impl<State: 'static> Ui<State> {
|
||||
self.textures.add(image)
|
||||
}
|
||||
|
||||
pub fn register_event<E: EventLike>(
|
||||
&mut self,
|
||||
id: impl IdLike<State>,
|
||||
event: E,
|
||||
f: impl for<'a> EventFn<State, <E::Event as Event>::Data<'a>>,
|
||||
) {
|
||||
self.events.register(id.id(), event, f);
|
||||
self.widgets
|
||||
.data_mut(id)
|
||||
.unwrap()
|
||||
.event_mgrs
|
||||
.insert(EventManager::<State>::type_key::<E>());
|
||||
}
|
||||
|
||||
pub fn resize(&mut self, size: impl Into<Vec2>) {
|
||||
self.output_size = size.into();
|
||||
self.resized = true;
|
||||
}
|
||||
|
||||
pub fn update(&mut self) {
|
||||
if self.full_redraw {
|
||||
DrawState::new(self).redraw_all();
|
||||
self.full_redraw = false;
|
||||
pub fn update(&mut self, events: &mut dyn EventsLike) {
|
||||
if self.root_changed() {
|
||||
DrawState::new(self, events).redraw_all();
|
||||
self.old_root = self.root.as_ref().map(|r| r.id());
|
||||
} else if self.widgets.has_updates() {
|
||||
DrawState::new(self).redraw_updates();
|
||||
DrawState::new(self, events).redraw_updates();
|
||||
}
|
||||
if self.resized {
|
||||
self.resized = false;
|
||||
DrawState::new(self).redraw_all();
|
||||
DrawState::new(self, events).redraw_all();
|
||||
}
|
||||
}
|
||||
|
||||
/// free any resources that don't have references anymore
|
||||
fn free(&mut self) {
|
||||
fn free(&mut self, events: &mut dyn EventsLike) {
|
||||
for id in self.recv.try_iter() {
|
||||
self.events.remove(id);
|
||||
events.remove(id);
|
||||
self.widgets.delete(id);
|
||||
}
|
||||
self.textures.free();
|
||||
}
|
||||
|
||||
pub fn root_changed(&self) -> bool {
|
||||
self.root.as_ref().map(|r| r.id()) != self.old_root
|
||||
}
|
||||
|
||||
pub fn needs_redraw(&self) -> bool {
|
||||
self.full_redraw || self.widgets.has_updates()
|
||||
self.root_changed() || self.widgets.has_updates()
|
||||
}
|
||||
|
||||
pub fn num_widgets(&self) -> usize {
|
||||
@@ -161,7 +138,7 @@ impl<State: 'static> Ui<State> {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn window_region(&self, id: &impl IdLike<State>) -> Option<PixelRegion> {
|
||||
pub fn window_region(&self, id: &impl IdLike) -> Option<PixelRegion> {
|
||||
let region = self.active.get(&id.id())?.region;
|
||||
Some(region.to_px(self.output_size))
|
||||
}
|
||||
@@ -174,7 +151,7 @@ impl<State: 'static> Ui<State> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<State: 'static, I: IdLike<State>> Index<I> for Ui<State>
|
||||
impl<I: IdLike> Index<I> for Ui
|
||||
where
|
||||
I::Widget: Sized,
|
||||
{
|
||||
@@ -185,7 +162,7 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
impl<State: 'static, I: IdLike<State>> IndexMut<I> for Ui<State>
|
||||
impl<I: IdLike> IndexMut<I> for Ui
|
||||
where
|
||||
I::Widget: Sized,
|
||||
{
|
||||
@@ -194,12 +171,11 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
impl<State: 'static> Default for Ui<State> {
|
||||
impl Default for Ui {
|
||||
fn default() -> Self {
|
||||
let (send, recv) = channel();
|
||||
Self {
|
||||
widgets: Default::default(),
|
||||
events: Default::default(),
|
||||
active: Default::default(),
|
||||
layers: Default::default(),
|
||||
masks: Default::default(),
|
||||
@@ -207,8 +183,8 @@ impl<State: 'static> Default for Ui<State> {
|
||||
textures: Default::default(),
|
||||
cache: Default::default(),
|
||||
output_size: Vec2::ZERO,
|
||||
root: Default::default(),
|
||||
full_redraw: false,
|
||||
root: None,
|
||||
old_root: None,
|
||||
send,
|
||||
recv,
|
||||
resized: false,
|
||||
|
||||
Reference in new issue
Block a user