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

164
src/layout/layer.rs Normal file
View File

@@ -0,0 +1,164 @@
use std::ops::{Index, IndexMut};
use crate::{
layout::UiRegion,
render::{Primitive, PrimitiveHandle, Primitives},
util::Id,
};
struct LayerNode<T> {
prev: Option<usize>,
next: Next,
child: Option<usize>,
data: T,
}
#[derive(Clone, Copy)]
enum Next {
/// continue on same level
Same(usize),
/// go back to parent
Parent(usize),
/// end
None,
}
pub struct Layers<T> {
vec: Vec<LayerNode<T>>,
}
impl<T: Default> Layers<T> {
pub fn new() -> Layers<T>
where
T: Default,
{
Self {
vec: vec![LayerNode::head()],
}
}
pub fn clear(&mut self)
where
T: Default,
{
self.vec.clear();
self.vec.push(LayerNode::head());
}
fn push(&mut self, node: LayerNode<T>) -> usize {
let i = self.vec.len();
self.vec.push(node);
i
}
pub fn next(&mut self, i: usize) -> usize {
if let Next::Same(i) = self.vec[i].next {
return i;
}
let i_next = self.push(LayerNode::new(T::default(), self.vec[i].next));
self.vec[i].next = Next::Same(i_next);
self.vec[i_next].prev = Some(i);
i_next
}
pub fn child(&mut self, i: usize) -> usize {
if let Some(i) = self.vec[i].child {
return i;
}
let i_next = self.push(LayerNode::new(T::default(), Next::Parent(i)));
self.vec[i].child = Some(i_next);
self.vec[i_next].prev = Some(i);
i_next
}
pub fn iter_mut(&mut self) -> LayerIteratorMut<'_, T> {
LayerIteratorMut {
next: Some(0),
vec: &mut self.vec,
}
}
}
impl<T: Default> Default for Layers<T> {
fn default() -> Self {
Self::new()
}
}
impl<T> Index<usize> for Layers<T> {
type Output = T;
fn index(&self, index: usize) -> &Self::Output {
&self.vec[index].data
}
}
impl<T> IndexMut<usize> for Layers<T> {
fn index_mut(&mut self, index: usize) -> &mut Self::Output {
&mut self.vec[index].data
}
}
impl<T> LayerNode<T> {
pub fn new(data: T, next: Next) -> Self {
Self {
prev: None,
next,
child: None,
data,
}
}
pub fn head() -> Self
where
T: Default,
{
Self::new(T::default(), Next::None)
}
}
pub struct LayerIteratorMut<'a, T> {
next: Option<usize>,
vec: &'a mut Vec<LayerNode<T>>,
}
impl<'a, T> Iterator for LayerIteratorMut<'a, T> {
type Item = (usize, &'a mut T);
fn next(&mut self) -> Option<Self::Item> {
// chat are we cooked? (if it's not set up correctly this could be catastrophic)
let ret_i = self.next?;
let node: &mut LayerNode<T> = unsafe { std::mem::transmute(&mut self.vec[ret_i]) };
self.next = if let Some(i) = node.child {
Some(i)
} else if let Next::Same(i) = node.next {
Some(i)
} else if let Next::Parent(i) = node.next {
let node = &self.vec[i];
if let Next::Same(i) = node.next {
Some(i)
} else {
None
}
} else {
None
};
Some((ret_i, &mut node.data))
}
}
impl Layers<Primitives> {
pub fn write<P: Primitive>(
&mut self,
layer: usize,
id: Id,
primitive: P,
region: UiRegion,
) -> PrimitiveHandle {
self[layer].write(layer, id, primitive, region)
}
pub fn free(&mut self, h: &PrimitiveHandle) {
self[h.layer].free(h)
}
}

View File

@@ -1,5 +1,6 @@
mod color;
mod id;
mod layer;
mod num;
mod orientation;
mod painter;
@@ -13,6 +14,7 @@ mod widget;
pub use color::*;
pub use id::*;
pub use layer::*;
pub use num::*;
pub use orientation::*;
pub use painter::*;

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> {

View File

@@ -3,8 +3,8 @@ use image::DynamicImage;
use crate::{
core::{TextEdit, TextEditCtx},
layout::{
ActiveSensors, PainterCtx, Sensor, SensorMap, StaticWidgetId, TextData, TextureHandle,
Textures, Vec2, Widget, WidgetId, WidgetInstance, WidgetLike,
ActiveSensors, Layers, PainterCtx, Sensor, SensorMap, StaticWidgetId, TextData,
TextureHandle, Textures, Vec2, Widget, WidgetId, WidgetInstance, WidgetLike,
},
render::Primitives,
util::{DynBorrower, HashMap, HashSet, Id, IdTracker},
@@ -23,12 +23,12 @@ pub struct Ui<Ctx> {
pub(super) send: Sender<Id>,
size: Vec2,
// TODO: make these non pub(crate)
pub(crate) primitives: Primitives,
pub(crate) layers: Layers<Primitives>,
pub(crate) textures: Textures,
pub(crate) text: TextData,
full_redraw: bool,
pub(super) active: Active,
pub(crate) active: Active,
pub(super) sensor_map: SensorMap<Ctx>,
}
@@ -129,7 +129,7 @@ impl<Ctx> Ui<Ctx> {
self.free();
let mut ctx = PainterCtx::new(
&self.widgets,
&mut self.primitives,
&mut self.layers,
&mut self.active,
&mut self.textures,
&mut self.text,
@@ -152,7 +152,7 @@ impl<Ctx> Ui<Ctx> {
fn redraw_updates(&mut self) {
let mut ctx = PainterCtx::new(
&self.widgets,
&mut self.primitives,
&mut self.layers,
&mut self.active,
&mut self.textures,
&mut self.text,
@@ -343,7 +343,7 @@ impl<Ctx> Default for Ui<Ctx> {
root: Default::default(),
widgets: Widgets::new(),
updates: Default::default(),
primitives: Default::default(),
layers: Default::default(),
textures: Textures::new(),
text: TextData::default(),
full_redraw: false,