243 lines
6.0 KiB
Rust
243 lines
6.0 KiB
Rust
use image::DynamicImage;
|
|
|
|
use crate::{
|
|
core::{TextEdit, TextEditCtx},
|
|
layout::{
|
|
IdLike, PainterCtx, PainterData, StaticWidgetId, TextureHandle, Vec2, Widget, WidgetId,
|
|
WidgetLike,
|
|
},
|
|
util::Id,
|
|
};
|
|
use std::{
|
|
any::{Any, TypeId},
|
|
ops::{Index, IndexMut},
|
|
sync::mpsc::{Receiver, Sender, channel},
|
|
};
|
|
|
|
pub struct Ui {
|
|
// TODO: make this at least pub(super)
|
|
pub(crate) data: PainterData,
|
|
root: Option<WidgetId>,
|
|
updates: Vec<Id>,
|
|
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)
|
|
}
|
|
|
|
pub fn add_static<W: Widget, Tag>(
|
|
&mut self,
|
|
w: impl WidgetLike<Tag, Widget = W>,
|
|
) -> StaticWidgetId<W> {
|
|
let id = w.add(self);
|
|
id.into_static()
|
|
}
|
|
|
|
/// 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.id();
|
|
self.data.widgets.insert(id.id, w);
|
|
id
|
|
}
|
|
|
|
pub fn set<W: Widget>(&mut self, id: &WidgetId<W>, w: W) {
|
|
self.data.widgets.insert(id.id, w);
|
|
}
|
|
|
|
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<W: Widget>(&self, id: &impl IdLike<W>) -> Option<&W> {
|
|
self.data.widgets.get(id)
|
|
}
|
|
|
|
pub fn get_mut<W: Widget>(&mut self, id: &impl IdLike<W>) -> Option<&mut W> {
|
|
self.data.widgets.get_mut(id)
|
|
}
|
|
|
|
pub fn id<W: Widget>(&mut self) -> WidgetId<W> {
|
|
WidgetId::new(
|
|
self.data.widgets.reserve(),
|
|
TypeId::of::<W>(),
|
|
self.send.clone(),
|
|
false,
|
|
)
|
|
}
|
|
|
|
pub fn id_static<W: Widget>(&mut self) -> StaticWidgetId<W> {
|
|
let id = self.id();
|
|
id.into_static()
|
|
}
|
|
|
|
pub fn add_texture(&mut self, image: DynamicImage) -> TextureHandle {
|
|
self.data.textures.add(image)
|
|
}
|
|
|
|
pub fn resize(&mut self, size: impl Into<Vec2>) {
|
|
self.data.output_size = size.into();
|
|
self.resized = true;
|
|
}
|
|
|
|
pub fn redraw_all(&mut self) {
|
|
self.data.active.clear();
|
|
// free before bc nothing should exist
|
|
self.free();
|
|
let mut ctx = PainterCtx::new(&mut self.data);
|
|
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();
|
|
}
|
|
if self.resized {
|
|
self.resized = false;
|
|
self.redraw_size();
|
|
}
|
|
}
|
|
|
|
fn redraw_size(&mut self) {
|
|
// let mut ctx = PainterCtx::new(&mut self.data);
|
|
// let dep = ctx.px_dependent.clone();
|
|
// for id in dep {
|
|
// ctx.redraw(id);
|
|
// }
|
|
self.redraw_all();
|
|
}
|
|
|
|
fn redraw_updates(&mut self) {
|
|
let mut ctx = PainterCtx::new(&mut self.data);
|
|
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.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.updates.is_empty()
|
|
}
|
|
|
|
pub fn num_widgets(&self) -> usize {
|
|
self.data.widgets.len()
|
|
}
|
|
|
|
pub fn active_widgets(&self) -> usize {
|
|
self.data.active.len()
|
|
}
|
|
|
|
pub fn text(&mut self, id: &impl IdLike<TextEdit>) -> TextEditCtx<'_> {
|
|
self.updates.push(id.id());
|
|
TextEditCtx {
|
|
text: self.data.widgets.get_mut(id).unwrap(),
|
|
font_system: &mut self.data.text.font_system,
|
|
}
|
|
}
|
|
|
|
pub fn debug(&self, label: &str) {
|
|
for (id, inst) in &self.data.active {
|
|
let l = &self.data.widgets.data(id).unwrap().label;
|
|
if l != label {
|
|
continue;
|
|
}
|
|
println!("\"{label}\" {{");
|
|
println!(" region: {}", inst.region);
|
|
println!(" desired_size: {}", inst.desired_size);
|
|
println!("}}");
|
|
}
|
|
}
|
|
}
|
|
|
|
impl<W: Widget> Index<&WidgetId<W>> for Ui {
|
|
type Output = W;
|
|
|
|
fn index(&self, id: &WidgetId<W>) -> &Self::Output {
|
|
self.get(id).unwrap()
|
|
}
|
|
}
|
|
|
|
impl<W: Widget> IndexMut<&WidgetId<W>> for Ui {
|
|
fn index_mut(&mut self, id: &WidgetId<W>) -> &mut Self::Output {
|
|
self.updates.push(id.id);
|
|
self.get_mut(id).unwrap()
|
|
}
|
|
}
|
|
|
|
impl<W: Widget> Index<StaticWidgetId<W>> for Ui {
|
|
type Output = W;
|
|
|
|
fn index(&self, id: StaticWidgetId<W>) -> &Self::Output {
|
|
self.data.widgets.get(&id).unwrap()
|
|
}
|
|
}
|
|
|
|
impl<W: Widget> IndexMut<StaticWidgetId<W>> for Ui {
|
|
fn index_mut(&mut self, id: StaticWidgetId<W>) -> &mut Self::Output {
|
|
self.updates.push(id.id);
|
|
self.data.widgets.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(),
|
|
updates: Default::default(),
|
|
full_redraw: false,
|
|
send,
|
|
recv,
|
|
resized: false,
|
|
}
|
|
}
|
|
}
|