layers initial impl (no sensors)

This commit is contained in:
2025-09-20 00:50:58 -04:00
parent 7651699743
commit 8ecd8bb171
7 changed files with 312 additions and 198 deletions

View File

@@ -1,9 +1,7 @@
use std::ops::Range;
use crate::{
layout::{
Active, TextAttrs, TextBuffer, TextData, TextOffset, TextureHandle, Textures, UiRegion,
UiVec2, Vec2, WidgetId, Widgets,
Active, Layers, TextAttrs, TextBuffer, TextData, TextOffset, TextureHandle, Textures,
UiRegion, UiVec2, Vec2, WidgetId, Widgets,
},
render::{Primitive, PrimitiveHandle, Primitives},
util::{HashMap, HashSet, Id, IdUtil},
@@ -16,13 +14,14 @@ pub struct Painter<'a, 'c> {
primitives: Vec<PrimitiveHandle>,
children: Vec<Id>,
sized_children: HashMap<Id, UiVec2>,
pub layer: usize,
id: Id,
}
pub struct PainterCtx<'a> {
pub widgets: &'a Widgets,
pub active: &'a mut Active,
pub primitives: &'a mut Primitives,
pub layers: &'a mut Layers<Primitives>,
pub textures: &'a mut Textures,
pub text: &'a mut TextData,
pub screen_size: Vec2,
@@ -37,13 +36,13 @@ pub struct WidgetInstance {
pub primitives: Vec<PrimitiveHandle>,
pub children: Vec<Id>,
pub resize: Option<(Id, UiVec2)>,
pub span: Range<usize>,
pub layer: usize,
}
impl<'a> PainterCtx<'a> {
pub fn new(
widgets: &'a Widgets,
primitives: &'a mut Primitives,
layers: &'a mut Layers<Primitives>,
active: &'a mut Active,
textures: &'a mut Textures,
text: &'a mut TextData,
@@ -52,7 +51,7 @@ impl<'a> PainterCtx<'a> {
Self {
widgets,
active,
primitives,
layers,
textures,
text,
screen_size,
@@ -77,32 +76,25 @@ impl<'a> PainterCtx<'a> {
return;
};
self.primitives.set_pos(active.span.start);
self.draw_inner(
active.layer,
id,
active.region,
active.parent.duplicate(),
Some(active.children),
);
self.active.widgets.get_mut(id).unwrap().resize = active.resize;
let delta = self.primitives.apply(active.span.clone());
if delta != 0
&& let Some(parent) = active.parent
{
self.shift_parent(id, parent, active.span.start, delta);
}
}
pub fn draw(&mut self, id: &Id) {
self.drawing.clear();
self.primitives.clear();
self.draw_inner(id, UiRegion::full(), None, None);
self.primitives.replace();
self.layers.clear();
self.draw_inner(0, id, UiRegion::full(), None, None);
}
fn draw_inner(
&mut self,
layer: usize,
id: &Id,
region: UiRegion,
parent: Option<Id>,
@@ -124,7 +116,6 @@ impl<'a> PainterCtx<'a> {
panic!("Cannot draw the same widget twice (2)");
}
if active.region == region {
self.primitives.skip(&mut active.span);
return;
}
// TODO:
@@ -138,6 +129,7 @@ impl<'a> PainterCtx<'a> {
let mut painter = Painter {
region,
layer,
id: id.duplicate(),
textures: Vec::new(),
primitives: Vec::new(),
@@ -147,9 +139,7 @@ impl<'a> PainterCtx<'a> {
};
// draw widgets
let start_i = painter.ctx.primitives.cur_pos();
painter.ctx.widgets.get_dyn_dynamic(id).draw(&mut painter);
let end_i = painter.ctx.primitives.cur_pos();
let sized_children = painter.sized_children;
@@ -159,10 +149,10 @@ impl<'a> PainterCtx<'a> {
region,
parent,
textures: painter.textures,
span: start_i..end_i,
primitives: painter.primitives,
children: painter.children,
resize,
layer,
};
for (cid, size) in sized_children {
if let Some(w) = self.active.widgets.get_mut(&cid) {
@@ -183,7 +173,7 @@ impl<'a> PainterCtx<'a> {
let mut inst = self.active.remove(id);
if let Some(inst) = &mut inst {
for h in &inst.primitives {
self.primitives.free(h);
self.layers.free(h);
}
inst.textures.clear();
self.textures.free();
@@ -200,56 +190,14 @@ impl<'a> PainterCtx<'a> {
}
inst
}
/// shifts the primitive spans for all widgets above this one in the tree
/// also goes into children of them and modifies those that come after this one
/// should be done after applying primitives to ensure active spans are correct
fn shift_parent(&mut self, original: &Id, parent: Id, start: usize, delta: isize) {
let instance = self.active.widgets.get_mut(&parent).unwrap();
let end = &mut instance.span.end;
*end = end.strict_add_signed(delta);
let parent_parent = instance.parent.duplicate();
for child in instance
.children
.iter()
// skip original
.skip_while(|id| *id != original)
.skip(1)
.map(|id| id.duplicate())
.collect::<Vec<_>>()
{
self.shift_child(&child, start, delta);
}
if let Some(parent_parent) = parent_parent {
self.shift_parent(&parent, parent_parent, start, delta);
}
}
fn shift_child(&mut self, child: &Id, start: usize, delta: isize) {
let instance = self.active.widgets.get_mut(child).unwrap();
// = also prevents the original id from getting shifted
if instance.span.start < start {
return;
}
instance.span.start = instance.span.start.strict_add_signed(delta);
instance.span.end = instance.span.end.strict_add_signed(delta);
for child in instance
.children
.iter()
.map(|id| id.duplicate())
.collect::<Vec<_>>()
{
self.shift_child(&child, start, delta);
}
}
}
impl<'a, 'c> Painter<'a, 'c> {
fn primitive_at<P: Primitive>(&mut self, primitive: P, region: UiRegion) {
let h = self
.ctx
.primitives
.write(self.id.duplicate(), primitive, region);
.layers
.write(self.layer, self.id.duplicate(), primitive, region);
self.primitives.push(h);
}
@@ -276,7 +224,7 @@ impl<'a, 'c> Painter<'a, 'c> {
fn widget_at<W>(&mut self, id: &WidgetId<W>, region: UiRegion) {
self.children.push(id.id.duplicate());
self.ctx
.draw_inner(&id.id, region, Some(self.id.duplicate()), None);
.draw_inner(self.layer, &id.id, region, Some(self.id.duplicate()), None);
}
pub fn texture_within(&mut self, handle: &TextureHandle, region: UiRegion) {
@@ -323,6 +271,14 @@ impl<'a, 'c> Painter<'a, 'c> {
pub fn text_data(&mut self) -> &mut TextData {
self.ctx.text
}
pub fn child_layer(&mut self) -> usize {
self.ctx.layers.child(self.layer)
}
pub fn next_layer(&mut self) -> usize {
self.ctx.layers.next(self.layer)
}
}
pub struct SizeCtx<'a> {