refactor project structure (start of redoing atomic branch without atomics)
This commit is contained in:
212
core/src/layout/ui.rs
Normal file
212
core/src/layout/ui.rs
Normal file
@@ -0,0 +1,212 @@
|
||||
use crate::{
|
||||
layout::{
|
||||
Event, EventFn, EventModule, IdLike, PainterData, PixelRegion, TextureHandle, Widget,
|
||||
WidgetId, WidgetInstance, WidgetLike,
|
||||
},
|
||||
util::{Id, Vec2},
|
||||
};
|
||||
use image::DynamicImage;
|
||||
use std::{
|
||||
any::{Any, TypeId},
|
||||
ops::{Index, IndexMut},
|
||||
sync::mpsc::{Receiver, Sender, channel},
|
||||
};
|
||||
|
||||
pub struct Ui {
|
||||
// TODO: make this at least pub(super)
|
||||
pub data: PainterData,
|
||||
root: Option<WidgetId>,
|
||||
recv: Receiver<Id>,
|
||||
pub(super) send: Sender<Id>,
|
||||
full_redraw: bool,
|
||||
resized: bool,
|
||||
}
|
||||
|
||||
impl Ui {
|
||||
pub fn add<W: Widget, Tag>(&mut self, w: impl WidgetLike<Tag, Widget = W>) -> WidgetId<W> {
|
||||
w.add(self)
|
||||
}
|
||||
|
||||
/// useful for debugging
|
||||
pub fn set_label<W>(&mut self, id: &WidgetId<W>, label: String) {
|
||||
self.data.widgets.data_mut(&id.id()).unwrap().label = label;
|
||||
}
|
||||
|
||||
pub fn label<W>(&self, id: &WidgetId<W>) -> &String {
|
||||
&self.data.widgets.data(&id.id()).unwrap().label
|
||||
}
|
||||
|
||||
pub fn add_widget<W: Widget>(&mut self, w: W) -> WidgetId<W> {
|
||||
self.push(w)
|
||||
}
|
||||
|
||||
pub fn push<W: Widget>(&mut self, w: W) -> WidgetId<W> {
|
||||
let id = self.new_id();
|
||||
self.data.widgets.insert(id.id(), w);
|
||||
id
|
||||
}
|
||||
|
||||
pub fn set_root<Tag>(&mut self, w: impl WidgetLike<Tag>) {
|
||||
self.root = Some(w.add(self).any());
|
||||
self.full_redraw = true;
|
||||
}
|
||||
|
||||
pub fn new() -> Self {
|
||||
Self::default()
|
||||
}
|
||||
|
||||
pub fn get<I: IdLike>(&self, id: &I) -> Option<&I::Widget> {
|
||||
self.data.widgets.get(id)
|
||||
}
|
||||
|
||||
pub fn get_mut<I: IdLike>(&mut self, id: &I) -> Option<&mut I::Widget> {
|
||||
self.data.widgets.get_mut(id)
|
||||
}
|
||||
|
||||
fn new_id<W: Widget>(&mut self) -> WidgetId<W> {
|
||||
WidgetId::new(
|
||||
self.data.widgets.reserve(),
|
||||
TypeId::of::<W>(),
|
||||
self.send.clone(),
|
||||
)
|
||||
}
|
||||
|
||||
pub fn add_texture(&mut self, image: DynamicImage) -> TextureHandle {
|
||||
self.data.textures.add(image)
|
||||
}
|
||||
|
||||
pub fn register_event<W, E: Event, Ctx: 'static>(
|
||||
&mut self,
|
||||
id: &WidgetId<W>,
|
||||
event: E,
|
||||
f: impl EventFn<Ctx, E::Data>,
|
||||
) {
|
||||
self.data
|
||||
.modules
|
||||
.get_mut::<E::Module<Ctx>>()
|
||||
.register(id.id(), event, f);
|
||||
}
|
||||
|
||||
pub fn resize(&mut self, size: impl Into<Vec2>) {
|
||||
self.data.output_size = size.into();
|
||||
self.resized = true;
|
||||
}
|
||||
|
||||
pub fn redraw_all(&mut self) {
|
||||
for (_, inst) in self.data.active.drain() {
|
||||
for m in self.data.modules.iter_mut() {
|
||||
m.on_undraw(&inst);
|
||||
}
|
||||
}
|
||||
// free before bc nothing should exist
|
||||
self.free();
|
||||
if let Some(root) = &self.root {
|
||||
self.data.draw(root.id());
|
||||
}
|
||||
}
|
||||
|
||||
pub fn update(&mut self) {
|
||||
if self.full_redraw {
|
||||
self.redraw_all();
|
||||
self.full_redraw = false;
|
||||
} else if self.data.widgets.has_updates() {
|
||||
self.redraw_updates();
|
||||
}
|
||||
if self.resized {
|
||||
self.resized = false;
|
||||
self.redraw_all();
|
||||
}
|
||||
}
|
||||
|
||||
fn redraw_updates(&mut self) {
|
||||
let mut updates = std::mem::take(&mut self.data.widgets.updates);
|
||||
self.data.redraw(&mut updates);
|
||||
self.data.widgets.updates = updates;
|
||||
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.data.modules.iter_mut() {
|
||||
m.on_remove(&id);
|
||||
}
|
||||
self.data.widgets.delete(id);
|
||||
}
|
||||
self.data.textures.free();
|
||||
}
|
||||
|
||||
pub fn needs_redraw(&self) -> bool {
|
||||
self.full_redraw || self.data.widgets.has_updates()
|
||||
}
|
||||
|
||||
pub fn num_widgets(&self) -> usize {
|
||||
self.data.widgets.len()
|
||||
}
|
||||
|
||||
pub fn active_widgets(&self) -> usize {
|
||||
self.data.active.len()
|
||||
}
|
||||
|
||||
pub fn debug_layers(&self) {
|
||||
for ((idx, depth), primitives) in self.data.layers.iter_depth() {
|
||||
let indent = " ".repeat(depth * 2);
|
||||
let len = primitives.instances().len();
|
||||
print!("{indent}{idx}: {len} primitives");
|
||||
if len >= 1 {
|
||||
print!(" ({})", primitives.instances()[0].binding);
|
||||
}
|
||||
println!();
|
||||
}
|
||||
}
|
||||
|
||||
pub fn window_region(&self, id: &impl IdLike) -> Option<PixelRegion> {
|
||||
let region = self.data.active.get(&id.id())?.region;
|
||||
Some(region.to_px(self.data.output_size))
|
||||
}
|
||||
|
||||
pub fn debug(&self, label: &str) -> impl Iterator<Item = &WidgetInstance> {
|
||||
self.data.active.iter().filter_map(move |(id, inst)| {
|
||||
let l = &self.data.widgets.label(id);
|
||||
if *l == label { Some(inst) } else { None }
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl<I: IdLike> Index<&I> for Ui {
|
||||
type Output = I::Widget;
|
||||
|
||||
fn index(&self, id: &I) -> &Self::Output {
|
||||
self.get(id).unwrap()
|
||||
}
|
||||
}
|
||||
|
||||
impl<I: IdLike> IndexMut<&I> for Ui {
|
||||
fn index_mut(&mut self, id: &I) -> &mut Self::Output {
|
||||
self.get_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 {
|
||||
data: PainterData::default(),
|
||||
root: Default::default(),
|
||||
full_redraw: false,
|
||||
send,
|
||||
recv,
|
||||
resized: false,
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user