static ids

This commit is contained in:
2025-08-25 16:12:49 -04:00
parent 41103f2732
commit 7b21b0714d
4 changed files with 172 additions and 33 deletions

View File

@@ -2,8 +2,8 @@ use image::DynamicImage;
use crate::{
layout::{
ActiveSensors, Painter, SensorMap, TextData, TextureHandle, Textures, UiRegion, Vec2,
Widget, WidgetId, WidgetLike,
ActiveSensors, Painter, SensorMap, StaticWidgetId, TextData, TextureHandle, Textures,
UiRegion, Vec2, Widget, WidgetId, WidgetLike,
},
render::Primitives,
util::{HashMap, Id, IdTracker},
@@ -18,9 +18,9 @@ pub struct Ui<Ctx> {
base: Option<WidgetId>,
widgets: Widgets<Ctx>,
labels: HashMap<Id, String>,
updates: Vec<WidgetId>,
updates: Vec<Id>,
recv: Receiver<Id>,
send: Sender<Id>,
pub(super) send: Sender<Id>,
size: Vec2,
// TODO: make these non pub(crate)
pub(crate) primitives: Primitives,
@@ -46,6 +46,14 @@ impl<Ctx> Ui<Ctx> {
w.add(self)
}
pub fn add_static<W: Widget<Ctx>, Tag>(
&mut self,
w: impl WidgetLike<Ctx, 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.labels.insert(id.id.duplicate(), label);
@@ -86,7 +94,17 @@ impl<Ctx> Ui<Ctx> {
}
pub fn id<W: Widget<Ctx>>(&mut self) -> WidgetId<W> {
WidgetId::new(self.widgets.reserve(), TypeId::of::<W>(), self.send.clone())
WidgetId::new(
self.widgets.reserve(),
TypeId::of::<W>(),
self.send.clone(),
false,
)
}
pub fn id_static<W: Widget<Ctx>>(&mut self) -> StaticWidgetId<W> {
let id = self.id();
id.into_static()
}
pub fn add_texture(&mut self, image: DynamicImage) -> TextureHandle {
@@ -148,7 +166,7 @@ impl<Ctx> Ui<Ctx> {
self.size,
);
for id in self.updates.drain(..) {
painter.redraw(&id.id);
painter.redraw(&id);
}
self.free();
}
@@ -181,11 +199,26 @@ impl<W: Widget<Ctx>, Ctx> Index<&WidgetId<W>> for Ui<Ctx> {
impl<W: Widget<Ctx>, Ctx> IndexMut<&WidgetId<W>> for Ui<Ctx> {
fn index_mut(&mut self, id: &WidgetId<W>) -> &mut Self::Output {
self.updates.push(id.clone().erase_type());
self.updates.push(id.id.duplicate());
self.get_mut(id).unwrap()
}
}
impl<W: Widget<Ctx>, Ctx> Index<StaticWidgetId<W>> for Ui<Ctx> {
type Output = W;
fn index(&self, id: StaticWidgetId<W>) -> &Self::Output {
self.widgets.get_static(&id).unwrap()
}
}
impl<W: Widget<Ctx>, Ctx> IndexMut<StaticWidgetId<W>> for Ui<Ctx> {
fn index_mut(&mut self, id: StaticWidgetId<W>) -> &mut Self::Output {
self.updates.push(id.id.id());
self.widgets.get_static_mut(&id).unwrap()
}
}
impl<Ctx> Widgets<Ctx> {
pub fn new() -> Self {
Self {
@@ -198,6 +231,18 @@ impl<Ctx> Widgets<Ctx> {
self.map.get(id).unwrap().as_ref()
}
pub fn get_static<W: Widget<Ctx>>(&self, id: &StaticWidgetId<W>) -> Option<&W> {
self.map.get(&id.id.id()).unwrap().as_any().downcast_ref()
}
pub fn get_static_mut<W: Widget<Ctx>>(&mut self, id: &StaticWidgetId<W>) -> Option<&mut W> {
self.map
.get_mut(&id.id.id())
.unwrap()
.as_any_mut()
.downcast_mut()
}
pub fn get<W: Widget<Ctx>>(&self, id: &WidgetId<W>) -> Option<&W> {
self.map.get(&id.id).unwrap().as_any().downcast_ref()
}