REACTIVITY
This commit is contained in:
@@ -1,17 +1,17 @@
|
||||
use image::DynamicImage;
|
||||
|
||||
use crate::{
|
||||
HashMap,
|
||||
layout::{
|
||||
ActiveSensors, Painter, SensorMap, TextData, TextureHandle, Textures, Vec2, Widget,
|
||||
WidgetId, WidgetLike,
|
||||
ActiveSensors, Painter, SensorMap, TextData, TextureHandle, Textures, UiRegion, Vec2,
|
||||
Widget, WidgetId, WidgetLike,
|
||||
},
|
||||
render::Primitives,
|
||||
util::{Id, IdTracker},
|
||||
util::{HashMap, Id, IdTracker},
|
||||
};
|
||||
use std::{
|
||||
any::{Any, TypeId},
|
||||
ops::{Index, IndexMut},
|
||||
range::Range,
|
||||
sync::mpsc::{Receiver, Sender, channel},
|
||||
};
|
||||
|
||||
@@ -28,7 +28,7 @@ pub struct Ui<Ctx> {
|
||||
pub(crate) text: TextData,
|
||||
full_redraw: bool,
|
||||
|
||||
pub(super) active_sensors: ActiveSensors,
|
||||
pub(super) active: Active,
|
||||
pub(super) sensor_map: SensorMap<Ctx>,
|
||||
}
|
||||
|
||||
@@ -96,7 +96,7 @@ impl<Ctx> Ui<Ctx> {
|
||||
where
|
||||
Ctx: 'static,
|
||||
{
|
||||
self.active_sensors.clear();
|
||||
self.active.clear();
|
||||
self.primitives.clear();
|
||||
self.free();
|
||||
let mut painter = Painter::new(
|
||||
@@ -104,7 +104,7 @@ impl<Ctx> Ui<Ctx> {
|
||||
&mut self.primitives,
|
||||
ctx,
|
||||
&self.sensor_map,
|
||||
&mut self.active_sensors,
|
||||
&mut self.active,
|
||||
&mut self.text,
|
||||
&mut self.textures,
|
||||
self.size,
|
||||
@@ -112,6 +112,7 @@ impl<Ctx> Ui<Ctx> {
|
||||
if let Some(base) = &self.base {
|
||||
painter.draw(base);
|
||||
}
|
||||
self.primitives.replace();
|
||||
}
|
||||
|
||||
pub fn update(&mut self, ctx: &mut Ctx)
|
||||
@@ -122,9 +123,26 @@ impl<Ctx> Ui<Ctx> {
|
||||
self.redraw_all(ctx);
|
||||
self.full_redraw = false;
|
||||
} else if !self.updates.is_empty() {
|
||||
// TODO: partial updates
|
||||
self.redraw_all(ctx);
|
||||
self.updates.drain(..);
|
||||
self.redraw_updates(ctx);
|
||||
}
|
||||
}
|
||||
|
||||
fn redraw_updates(&mut self, ctx: &mut Ctx)
|
||||
where
|
||||
Ctx: 'static,
|
||||
{
|
||||
let mut painter = Painter::new(
|
||||
&self.widgets,
|
||||
&mut self.primitives,
|
||||
ctx,
|
||||
&self.sensor_map,
|
||||
&mut self.active,
|
||||
&mut self.text,
|
||||
&mut self.textures,
|
||||
self.size,
|
||||
);
|
||||
for id in self.updates.drain(..) {
|
||||
painter.redraw(&id.id);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -168,8 +186,8 @@ impl<Ctx> Widgets<Ctx> {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_dyn<W>(&self, id: &WidgetId<W>) -> &dyn Widget<Ctx> {
|
||||
self.map.get(&id.id).unwrap().as_ref()
|
||||
pub fn get_dyn(&self, id: &Id) -> &dyn Widget<Ctx> {
|
||||
self.map.get(id).unwrap().as_ref()
|
||||
}
|
||||
|
||||
pub fn get<W: Widget<Ctx>>(&self, id: &WidgetId<W>) -> Option<&W> {
|
||||
@@ -231,7 +249,7 @@ impl<Ctx: 'static> Default for Ui<Ctx> {
|
||||
textures: Textures::new(),
|
||||
text: TextData::default(),
|
||||
full_redraw: false,
|
||||
active_sensors: Default::default(),
|
||||
active: Default::default(),
|
||||
sensor_map: Default::default(),
|
||||
send,
|
||||
recv,
|
||||
@@ -239,3 +257,37 @@ impl<Ctx: 'static> Default for Ui<Ctx> {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct WidgetInstance {
|
||||
pub region: UiRegion,
|
||||
pub primitives: Range<usize>,
|
||||
pub children: Vec<Id>,
|
||||
pub parent: Option<Id>,
|
||||
}
|
||||
pub type ActiveWidgets = HashMap<Id, WidgetInstance>;
|
||||
|
||||
#[derive(Default)]
|
||||
pub(super) struct Active {
|
||||
pub sensors: ActiveSensors,
|
||||
pub widgets: ActiveWidgets,
|
||||
}
|
||||
|
||||
impl Active {
|
||||
pub fn clear(&mut self) {
|
||||
self.sensors.clear();
|
||||
self.widgets.clear();
|
||||
}
|
||||
|
||||
pub fn remove(&mut self, id: &Id) -> Option<WidgetInstance> {
|
||||
let instance = self.widgets.remove(id);
|
||||
self.sensors.remove(id);
|
||||
instance
|
||||
}
|
||||
|
||||
pub fn add<Ctx>(&mut self, id: &Id, instance: WidgetInstance, sensors_map: &SensorMap<Ctx>) {
|
||||
if sensors_map.get(id).is_some() {
|
||||
self.sensors.insert(id.duplicate(), instance.region);
|
||||
}
|
||||
self.widgets.insert(id.duplicate(), instance);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user