use image::DynamicImage; use crate::{ core::{TextEdit, TextEditCtx}, layout::{ Layers, Modules, PainterCtx, StaticWidgetId, TextData, TextureHandle, Textures, Vec2, Widget, WidgetId, WidgetInstance, WidgetLike, Widgets, }, util::{HashMap, Id}, }; use std::{ any::{Any, TypeId}, ops::{Index, IndexMut}, sync::mpsc::{Receiver, Sender, channel}, }; pub struct Ui { root: Option, pub(super) widgets: Widgets, updates: Vec, recv: Receiver, pub(super) send: Sender, size: Vec2, // TODO: make these non pub(crate) pub(crate) layers: Layers, pub(crate) textures: Textures, pub(crate) text: TextData, full_redraw: bool, pub(crate) active: ActiveWidgets, pub modules: Modules, } impl Ui { pub fn add(&mut self, w: impl WidgetLike) -> WidgetId { w.add(self) } pub fn add_static( &mut self, w: impl WidgetLike, ) -> StaticWidgetId { let id = w.add(self); id.into_static() } /// useful for debugging pub fn set_label(&mut self, id: &WidgetId, label: String) { self.widgets.data_mut(&id.id).unwrap().label = label; } pub fn label(&self, id: &WidgetId) -> &String { &self.widgets.data(&id.id).unwrap().label } pub fn add_widget(&mut self, w: W) -> WidgetId { self.push(w) } pub fn push(&mut self, w: W) -> WidgetId { let id = self.id(); self.widgets.insert(id.id.duplicate(), w); id } pub fn set(&mut self, id: &WidgetId, w: W) { self.widgets.insert(id.id.duplicate(), w); } pub fn set_root(&mut self, w: impl WidgetLike) { self.root = Some(w.add(self).any()); self.full_redraw = true; } pub fn new() -> Self { Self::default() } pub fn get(&self, id: &WidgetId) -> Option<&W> { self.widgets.get(id) } pub fn get_mut(&mut self, id: &WidgetId) -> Option<&mut W> { self.widgets.get_mut(id) } pub fn id(&mut self) -> WidgetId { WidgetId::new( self.widgets.reserve(), TypeId::of::(), self.send.clone(), false, ) } pub fn id_static(&mut self) -> StaticWidgetId { let id = self.id(); id.into_static() } pub fn add_texture(&mut self, image: DynamicImage) -> TextureHandle { self.textures.add(image) } pub fn resize(&mut self, size: impl Into) { self.size = size.into(); } pub fn redraw_all(&mut self) { self.active.clear(); // free before bc nothing should exist self.free(); let mut ctx = PainterCtx::new( &self.widgets, &mut self.layers, &mut self.active, &mut self.modules, &mut self.textures, &mut self.text, self.size, ); if let Some(root) = &self.root { ctx.draw(&root.id); } } pub fn update(&mut self) { if self.full_redraw { self.redraw_all(); self.full_redraw = false; } else if !self.updates.is_empty() { self.redraw_updates(); } } fn redraw_updates(&mut self) { let mut ctx = PainterCtx::new( &self.widgets, &mut self.layers, &mut self.active, &mut self.modules, &mut self.textures, &mut self.text, self.size, ); for id in self.updates.drain(..) { ctx.redraw(&id); } self.free(); } /// free any resources that don't have references anymore fn free(&mut self) { for id in self.recv.try_iter() { for m in self.modules.iter_mut() { m.on_remove(&id); } self.widgets.delete(id); } self.textures.free(); } pub fn needs_redraw(&self) -> bool { self.full_redraw || !self.updates.is_empty() } pub fn num_widgets(&self) -> usize { self.widgets.len() } pub fn active_widgets(&self) -> usize { self.active.len() } pub fn text(&mut self, id: &WidgetId) -> TextEditCtx<'_> { self.updates.push(id.id.duplicate()); TextEditCtx { text: self.widgets.get_mut(id).unwrap(), font_system: &mut self.text.font_system, } } } impl Index<&WidgetId> for Ui { type Output = W; fn index(&self, id: &WidgetId) -> &Self::Output { self.get(id).unwrap() } } impl IndexMut<&WidgetId> for Ui { fn index_mut(&mut self, id: &WidgetId) -> &mut Self::Output { self.updates.push(id.id.duplicate()); self.get_mut(id).unwrap() } } impl Index> for Ui { type Output = W; fn index(&self, id: StaticWidgetId) -> &Self::Output { self.widgets.get_static(&id).unwrap() } } impl IndexMut> for Ui { fn index_mut(&mut self, id: StaticWidgetId) -> &mut Self::Output { self.updates.push(id.id.id()); self.widgets.get_static_mut(&id).unwrap() } } impl dyn Widget { pub fn as_any(&self) -> &dyn Any { self } pub fn as_any_mut(&mut self) -> &mut dyn Any { self } } impl Default for Ui { fn default() -> Self { let (send, recv) = channel(); Self { root: Default::default(), widgets: Widgets::new(), updates: Default::default(), layers: Default::default(), textures: Textures::new(), text: TextData::default(), full_redraw: false, active: Default::default(), send, recv, size: Vec2::ZERO, modules: Modules::default(), } } } pub type ActiveWidgets = HashMap;