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,19 +1,21 @@
mod color;
mod id;
mod painter;
mod region;
mod sense;
mod texture;
mod ui;
mod vec2;
mod widget;
mod id;
pub use color::*;
pub use id::*;
pub use painter::*;
pub use region::*;
pub use sense::*;
pub use texture::*;
pub use ui::*;
pub use vec2::*;
pub use widget::*;
pub use id::*;
pub type UiColor = Color<u8>;

View File

@@ -1,6 +1,6 @@
use crate::{
ActiveSensors, SensorMap, UiRegion, WidgetId, Widgets,
primitive::{Primitive, Primitives},
ActiveSensors, SensorMap, TextureHandle, UiRegion, WidgetId, Widgets,
render::{Primitive, Primitives},
};
pub struct Painter<'a, Ctx: 'static> {
@@ -32,7 +32,9 @@ impl<'a, Ctx> Painter<'a, Ctx> {
self.primitives.write(data, self.region);
}
pub fn draw<W>(&mut self, id: &WidgetId<W>)
/// Draws a widget but does NOT maintain the region.
/// Useful if you only have a single widget to draw.
pub fn draw_inner<W>(&mut self, id: &WidgetId<W>)
where
Ctx: 'static,
{
@@ -42,16 +44,34 @@ impl<'a, Ctx> Painter<'a, Ctx> {
self.nodes.get_dyn(id).draw(self);
}
pub fn draw_within(&mut self, node: &WidgetId, region: UiRegion)
/// Draws a widget and maintains the original region.
/// Useful if you need to draw multiple overlapping child widgets.
pub fn draw<W>(&mut self, id: &WidgetId<W>)
where
Ctx: 'static,
{
let old = self.region;
self.draw_inner(id);
self.region = old;
}
/// Draws a widget within an inner region and maintains the original region.
/// Useful if you need to draw multiple child widgets in select areas within the original
/// region.
pub fn draw_within(&mut self, id: &WidgetId, region: UiRegion)
where
Ctx: 'static,
{
let old = self.region;
self.region.select(&region);
self.draw(node);
self.draw_inner(id);
self.region = old;
}
pub fn draw_texture(&mut self, handle: &TextureHandle) {
self.write(handle.inner);
}
pub fn finish(self) -> Primitives {
self.primitives
}

48
src/layout/texture.rs Normal file
View File

@@ -0,0 +1,48 @@
use image::DynamicImage;
use crate::render::TexturePrimitive;
/// TODO: proper resource management
pub struct TextureHandle {
pub inner: TexturePrimitive,
}
/// a texture manager for a ui
/// note that this is heavily oriented towards wgpu's renderer so the primitives don't need mapped
#[derive(Default)]
pub struct Textures {
/// TODO: these are images, not views rn
views: Vec<DynamicImage>,
changed: bool,
}
pub struct TextureUpdates<'a> {
pub images: Option<&'a [DynamicImage]>,
}
impl Textures {
pub fn add(&mut self, image: DynamicImage) -> TextureHandle {
let view_idx = self.views.len() as u32;
self.views.push(image);
// 0 == default in renderer; TODO: actually create samplers here
let sampler_idx = 0;
self.changed = true;
TextureHandle {
inner: TexturePrimitive {
view_idx,
sampler_idx,
},
}
}
pub fn updates(&mut self) -> TextureUpdates<'_> {
if self.changed {
self.changed = false;
TextureUpdates {
images: Some(&self.views),
}
} else {
TextureUpdates { images: None }
}
}
}

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>,
}

View File

@@ -2,7 +2,7 @@ use crate::{util::{impl_op, F32Util}, UiNum};
use std::ops::*;
#[repr(C)]
#[derive(Debug, Clone, Copy, PartialEq, Default, bytemuck::Pod, bytemuck::Zeroable)]
#[derive(Clone, Copy, PartialEq, Default, bytemuck::Pod, bytemuck::Zeroable)]
pub struct Vec2 {
pub x: f32,
pub y: f32,
@@ -60,3 +60,9 @@ impl<T: UiNum, U: UiNum> From<(T, U)> for Vec2 {
}
}
}
impl std::fmt::Debug for Vec2 {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "({}, {})", self.x, self.y)
}
}

View File

@@ -41,8 +41,7 @@ pub(crate) use WidgetFnRet;
impl<W: Widget<Ctx>, Ctx, F: FnOnce(&mut Ui<Ctx>) -> W> WidgetLike<Ctx, FnTag> for F {
type Widget = W;
fn add(self, ui: &mut Ui<Ctx>) -> WidgetId<W> {
let w = self(ui);
ui.add(w)
self(ui).add(ui)
}
}