update px dependent on resize + move painter data into struct in ui
This commit is contained in:
@@ -3,10 +3,9 @@ use image::DynamicImage;
|
||||
use crate::{
|
||||
core::{TextEdit, TextEditCtx},
|
||||
layout::{
|
||||
Layers, Modules, PainterCtx, StaticWidgetId, TextData, TextureHandle, Textures, Vec2,
|
||||
Widget, WidgetId, WidgetInstance, WidgetLike, Widgets,
|
||||
PainterCtx, PainterData, StaticWidgetId, TextureHandle, Vec2, Widget, WidgetId, WidgetLike,
|
||||
},
|
||||
util::{HashMap, Id},
|
||||
util::Id,
|
||||
};
|
||||
use std::{
|
||||
any::{Any, TypeId},
|
||||
@@ -15,20 +14,13 @@ use std::{
|
||||
};
|
||||
|
||||
pub struct Ui {
|
||||
pub(crate) data: PainterData,
|
||||
root: Option<WidgetId>,
|
||||
pub(super) widgets: Widgets,
|
||||
updates: Vec<Id>,
|
||||
recv: Receiver<Id>,
|
||||
pub(super) send: Sender<Id>,
|
||||
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,
|
||||
resized: bool,
|
||||
}
|
||||
|
||||
impl Ui {
|
||||
@@ -46,11 +38,11 @@ impl Ui {
|
||||
|
||||
/// useful for debugging
|
||||
pub fn set_label<W>(&mut self, id: &WidgetId<W>, label: String) {
|
||||
self.widgets.data_mut(&id.id).unwrap().label = label;
|
||||
self.data.widgets.data_mut(&id.id).unwrap().label = label;
|
||||
}
|
||||
|
||||
pub fn label<W>(&self, id: &WidgetId<W>) -> &String {
|
||||
&self.widgets.data(&id.id).unwrap().label
|
||||
&self.data.widgets.data(&id.id).unwrap().label
|
||||
}
|
||||
|
||||
pub fn add_widget<W: Widget>(&mut self, w: W) -> WidgetId<W> {
|
||||
@@ -59,12 +51,12 @@ impl Ui {
|
||||
|
||||
pub fn push<W: Widget>(&mut self, w: W) -> WidgetId<W> {
|
||||
let id = self.id();
|
||||
self.widgets.insert(id.id, w);
|
||||
self.data.widgets.insert(id.id, w);
|
||||
id
|
||||
}
|
||||
|
||||
pub fn set<W: Widget>(&mut self, id: &WidgetId<W>, w: W) {
|
||||
self.widgets.insert(id.id, w);
|
||||
self.data.widgets.insert(id.id, w);
|
||||
}
|
||||
|
||||
pub fn set_root<Tag>(&mut self, w: impl WidgetLike<Tag>) {
|
||||
@@ -77,16 +69,16 @@ impl Ui {
|
||||
}
|
||||
|
||||
pub fn get<W: Widget>(&self, id: &WidgetId<W>) -> Option<&W> {
|
||||
self.widgets.get(id)
|
||||
self.data.widgets.get(id)
|
||||
}
|
||||
|
||||
pub fn get_mut<W: Widget>(&mut self, id: &WidgetId<W>) -> Option<&mut W> {
|
||||
self.widgets.get_mut(id)
|
||||
self.data.widgets.get_mut(id)
|
||||
}
|
||||
|
||||
pub fn id<W: Widget>(&mut self) -> WidgetId<W> {
|
||||
WidgetId::new(
|
||||
self.widgets.reserve(),
|
||||
self.data.widgets.reserve(),
|
||||
TypeId::of::<W>(),
|
||||
self.send.clone(),
|
||||
false,
|
||||
@@ -99,26 +91,19 @@ impl Ui {
|
||||
}
|
||||
|
||||
pub fn add_texture(&mut self, image: DynamicImage) -> TextureHandle {
|
||||
self.textures.add(image)
|
||||
self.data.textures.add(image)
|
||||
}
|
||||
|
||||
pub fn resize(&mut self, size: impl Into<Vec2>) {
|
||||
self.size = size.into();
|
||||
self.data.output_size = size.into();
|
||||
self.resized = true;
|
||||
}
|
||||
|
||||
pub fn redraw_all(&mut self) {
|
||||
self.active.clear();
|
||||
self.data.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,
|
||||
);
|
||||
let mut ctx = PainterCtx::new(&mut self.data);
|
||||
if let Some(root) = &self.root {
|
||||
ctx.draw(root.id);
|
||||
}
|
||||
@@ -131,18 +116,22 @@ impl Ui {
|
||||
} 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);
|
||||
}
|
||||
}
|
||||
|
||||
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,
|
||||
);
|
||||
let mut ctx = PainterCtx::new(&mut self.data);
|
||||
for id in self.updates.drain(..) {
|
||||
ctx.redraw(id);
|
||||
}
|
||||
@@ -152,12 +141,12 @@ impl Ui {
|
||||
/// 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() {
|
||||
for m in self.data.modules.iter_mut() {
|
||||
m.on_remove(&id);
|
||||
}
|
||||
self.widgets.delete(id);
|
||||
self.data.widgets.delete(id);
|
||||
}
|
||||
self.textures.free();
|
||||
self.data.textures.free();
|
||||
}
|
||||
|
||||
pub fn needs_redraw(&self) -> bool {
|
||||
@@ -165,18 +154,18 @@ impl Ui {
|
||||
}
|
||||
|
||||
pub fn num_widgets(&self) -> usize {
|
||||
self.widgets.len()
|
||||
self.data.widgets.len()
|
||||
}
|
||||
|
||||
pub fn active_widgets(&self) -> usize {
|
||||
self.active.len()
|
||||
self.data.active.len()
|
||||
}
|
||||
|
||||
pub fn text(&mut self, id: &WidgetId<TextEdit>) -> TextEditCtx<'_> {
|
||||
self.updates.push(id.id);
|
||||
TextEditCtx {
|
||||
text: self.widgets.get_mut(id).unwrap(),
|
||||
font_system: &mut self.text.font_system,
|
||||
text: self.data.widgets.get_mut(id).unwrap(),
|
||||
font_system: &mut self.data.text.font_system,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -200,14 +189,14 @@ impl<W: Widget> Index<StaticWidgetId<W>> for Ui {
|
||||
type Output = W;
|
||||
|
||||
fn index(&self, id: StaticWidgetId<W>) -> &Self::Output {
|
||||
self.widgets.get_static(&id).unwrap()
|
||||
self.data.widgets.get_static(&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.widgets.get_static_mut(&id).unwrap()
|
||||
self.data.widgets.get_static_mut(&id).unwrap()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -225,20 +214,13 @@ impl Default for Ui {
|
||||
fn default() -> Self {
|
||||
let (send, recv) = channel();
|
||||
Self {
|
||||
data: PainterData::default(),
|
||||
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(),
|
||||
resized: false,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub type ActiveWidgets = HashMap<Id, WidgetInstance>;
|
||||
|
||||
Reference in New Issue
Block a user