texture freeing + render updates done a bit nicer
This commit is contained in:
@@ -1,8 +1,8 @@
|
||||
use image::DynamicImage;
|
||||
|
||||
use crate::{
|
||||
ActiveSensors, HashMap, Painter, SensorMap, TextureHandle, TextureUpdates, Textures, Widget,
|
||||
WidgetId, WidgetLike,
|
||||
ActiveSensors, HashMap, Painter, SensorMap, TextureHandle, Textures, Widget, WidgetId,
|
||||
WidgetLike,
|
||||
render::Primitives,
|
||||
util::{Id, IdTracker},
|
||||
};
|
||||
@@ -16,16 +16,22 @@ pub struct Ui<Ctx> {
|
||||
base: Option<WidgetId>,
|
||||
widgets: Widgets<Ctx>,
|
||||
updates: Vec<WidgetId>,
|
||||
del_recv: Receiver<Id>,
|
||||
del_send: Sender<Id>,
|
||||
primitives: Primitives,
|
||||
textures: Textures,
|
||||
recv: Receiver<UiMsg>,
|
||||
send: UiMsgSender,
|
||||
pub(crate) primitives: Primitives,
|
||||
pub(crate) textures: Textures,
|
||||
full_redraw: bool,
|
||||
|
||||
pub(super) active_sensors: ActiveSensors,
|
||||
pub(super) sensor_map: SensorMap<Ctx>,
|
||||
}
|
||||
|
||||
pub enum UiMsg {
|
||||
FreeWidget(Id),
|
||||
FreeTexture(u32),
|
||||
}
|
||||
pub type UiMsgSender = Sender<UiMsg>;
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct Widgets<Ctx> {
|
||||
ids: IdTracker,
|
||||
@@ -75,15 +81,11 @@ impl<Ctx> Ui<Ctx> {
|
||||
}
|
||||
|
||||
pub fn id<W: Widget<Ctx>>(&mut self) -> WidgetId<W> {
|
||||
WidgetId::new(
|
||||
self.widgets.reserve(),
|
||||
TypeId::of::<W>(),
|
||||
self.del_send.clone(),
|
||||
)
|
||||
WidgetId::new(self.widgets.reserve(), TypeId::of::<W>(), self.send.clone())
|
||||
}
|
||||
|
||||
pub fn add_texture(&mut self, image: DynamicImage) -> TextureHandle {
|
||||
self.textures.add(image)
|
||||
self.textures.add(image, self.send.clone())
|
||||
}
|
||||
|
||||
pub fn redraw_all(&mut self, ctx: &mut Ctx)
|
||||
@@ -103,33 +105,27 @@ impl<Ctx> Ui<Ctx> {
|
||||
self.primitives = painter.finish();
|
||||
}
|
||||
|
||||
pub fn update(&mut self, ctx: &mut Ctx) -> UiRenderUpdates
|
||||
pub fn update(&mut self, ctx: &mut Ctx)
|
||||
where
|
||||
Ctx: 'static,
|
||||
{
|
||||
while let Ok(id) = self.del_recv.try_recv() {
|
||||
self.widgets.delete(id);
|
||||
}
|
||||
self.recv_msgs();
|
||||
|
||||
if self.full_redraw {
|
||||
self.redraw_all(ctx);
|
||||
self.full_redraw = false;
|
||||
UiRenderUpdates {
|
||||
primitives: Some(&self.primitives),
|
||||
textures: self.textures.updates(),
|
||||
}
|
||||
} else if self.updates.is_empty() {
|
||||
UiRenderUpdates {
|
||||
primitives: None,
|
||||
textures: self.textures.updates(),
|
||||
}
|
||||
} else {
|
||||
} else if !self.updates.is_empty() {
|
||||
// TODO: partial updates
|
||||
self.redraw_all(ctx);
|
||||
self.updates.drain(..);
|
||||
UiRenderUpdates {
|
||||
primitives: Some(&self.primitives),
|
||||
textures: self.textures.updates(),
|
||||
}
|
||||
}
|
||||
|
||||
fn recv_msgs(&mut self) {
|
||||
while let Ok(msg) = self.recv.try_recv() {
|
||||
match msg {
|
||||
UiMsg::FreeWidget(id) => self.widgets.delete(id),
|
||||
UiMsg::FreeTexture(id) => self.textures.free(id),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -230,13 +226,8 @@ impl<Ctx: 'static> Default for Ui<Ctx> {
|
||||
full_redraw: false,
|
||||
active_sensors: Default::default(),
|
||||
sensor_map: Default::default(),
|
||||
del_send,
|
||||
del_recv,
|
||||
send: del_send,
|
||||
recv: del_recv,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct UiRenderUpdates<'a> {
|
||||
pub primitives: Option<&'a Primitives>,
|
||||
pub textures: TextureUpdates<'a>,
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user