added underdeveloped but working image support (no freeing or samplers)

This commit is contained in:
2025-08-22 23:07:31 -04:00
parent bde929b05a
commit 7dbdcbba42
26 changed files with 1256 additions and 155 deletions

View File

@@ -1,7 +1,10 @@
use image::DynamicImage;
use crate::{
ActiveSensors, HashMap, Painter, SensorMap, Widget, WidgetId, WidgetLike,
primitive::Primitives,
util::{IDTracker, Id},
ActiveSensors, HashMap, Painter, SensorMap, TextureHandle, TextureUpdates, Textures, Widget,
WidgetId, WidgetLike,
render::Primitives,
util::{Id, IdTracker},
};
use std::{
any::{Any, TypeId},
@@ -10,20 +13,24 @@ use std::{
};
pub struct Ui<Ctx> {
ids: IDTracker,
base: Option<WidgetId>,
widgets: Widgets<Ctx>,
updates: Vec<WidgetId>,
del_recv: Receiver<Id>,
del_send: Sender<Id>,
primitives: Primitives,
textures: Textures,
full_redraw: bool,
pub(super) active_sensors: ActiveSensors,
pub(super) sensor_map: SensorMap<Ctx>,
primitives: Primitives,
full_redraw: bool,
}
#[derive(Default)]
pub struct Widgets<Ctx>(HashMap<Id, Box<dyn Widget<Ctx>>>);
pub struct Widgets<Ctx> {
ids: IdTracker,
map: HashMap<Id, Box<dyn Widget<Ctx>>>,
}
impl<Ctx> Ui<Ctx> {
pub fn add<W: Widget<Ctx>, Tag>(
@@ -68,7 +75,15 @@ impl<Ctx> Ui<Ctx> {
}
pub fn id<W: Widget<Ctx>>(&mut self) -> WidgetId<W> {
WidgetId::new(self.ids.next(), TypeId::of::<W>(), self.del_send.clone())
WidgetId::new(
self.widgets.reserve(),
TypeId::of::<W>(),
self.del_send.clone(),
)
}
pub fn add_texture(&mut self, image: DynamicImage) -> TextureHandle {
self.textures.add(image)
}
pub fn redraw_all(&mut self, ctx: &mut Ctx)
@@ -83,12 +98,12 @@ impl<Ctx> Ui<Ctx> {
&mut self.active_sensors,
);
if let Some(base) = &self.base {
painter.draw(base);
painter.draw_inner(base);
}
self.primitives = painter.finish();
}
pub fn update(&mut self, ctx: &mut Ctx) -> Option<&Primitives>
pub fn update(&mut self, ctx: &mut Ctx) -> UiRenderUpdates
where
Ctx: 'static,
{
@@ -99,14 +114,24 @@ impl<Ctx> Ui<Ctx> {
if self.full_redraw {
self.redraw_all(ctx);
self.full_redraw = false;
return Some(&self.primitives);
UiRenderUpdates {
primitives: Some(&self.primitives),
textures: self.textures.updates(),
}
} else if self.updates.is_empty() {
UiRenderUpdates {
primitives: None,
textures: self.textures.updates(),
}
} else {
// TODO: partial updates
self.redraw_all(ctx);
self.updates.drain(..);
UiRenderUpdates {
primitives: Some(&self.primitives),
textures: self.textures.updates(),
}
}
if self.updates.is_empty() {
return None;
}
self.redraw_all(ctx);
self.updates.drain(..);
Some(&self.primitives)
}
pub fn needs_redraw(&self) -> bool {
@@ -135,39 +160,51 @@ impl<W: Widget<Ctx>, Ctx> IndexMut<&WidgetId<W>> for Ui<Ctx> {
impl<Ctx> Widgets<Ctx> {
pub fn new() -> Self {
Self(HashMap::new())
Self {
ids: IdTracker::default(),
map: HashMap::new(),
}
}
pub fn get_dyn<W>(&self, id: &WidgetId<W>) -> &dyn Widget<Ctx> {
self.0.get(&id.id).unwrap().as_ref()
self.map.get(&id.id).unwrap().as_ref()
}
pub fn get<W: Widget<Ctx>>(&self, id: &WidgetId<W>) -> Option<&W> {
self.0.get(&id.id).unwrap().as_any().downcast_ref()
self.map.get(&id.id).unwrap().as_any().downcast_ref()
}
pub fn get_mut<W: Widget<Ctx>>(&mut self, id: &WidgetId<W>) -> Option<&mut W> {
self.0.get_mut(&id.id).unwrap().as_any_mut().downcast_mut()
self.map
.get_mut(&id.id)
.unwrap()
.as_any_mut()
.downcast_mut()
}
pub fn insert(&mut self, id: Id, widget: impl Widget<Ctx>) {
self.0.insert(id, Box::new(widget));
self.map.insert(id, Box::new(widget));
}
pub fn insert_any(&mut self, id: Id, widget: Box<dyn Widget<Ctx>>) {
self.0.insert(id, widget);
self.map.insert(id, widget);
}
pub fn delete(&mut self, id: Id) {
self.0.remove(&id);
self.map.remove(&id);
self.ids.free(id);
}
pub fn reserve(&mut self) -> Id {
self.ids.next()
}
pub fn len(&self) -> usize {
self.0.len()
self.map.len()
}
pub fn is_empty(&self) -> bool {
self.0.is_empty()
self.map.is_empty()
}
}
@@ -185,11 +222,11 @@ impl<Ctx: 'static> Default for Ui<Ctx> {
fn default() -> Self {
let (del_send, del_recv) = channel();
Self {
ids: Default::default(),
base: Default::default(),
widgets: Widgets::new(),
updates: Default::default(),
primitives: Default::default(),
textures: Textures::default(),
full_redraw: false,
active_sensors: Default::default(),
sensor_map: Default::default(),
@@ -198,3 +235,8 @@ impl<Ctx: 'static> Default for Ui<Ctx> {
}
}
}
pub struct UiRenderUpdates<'a> {
pub primitives: Option<&'a Primitives>,
pub textures: TextureUpdates<'a>,
}