sensors are now normal

This commit is contained in:
2025-09-20 01:46:55 -04:00
parent 8ecd8bb171
commit fee03fddc8
7 changed files with 109 additions and 202 deletions

View File

@@ -1,16 +1,16 @@
use std::ops::{Index, IndexMut};
use crate::{
layout::UiRegion,
layout::{SenseShape, UiRegion},
render::{Primitive, PrimitiveHandle, Primitives},
util::Id,
util::{HashMap, Id},
};
struct LayerNode<T> {
struct LayerNode {
prev: Option<usize>,
next: Next,
child: Option<usize>,
data: T,
data: Layer,
}
#[derive(Clone, Copy)]
@@ -23,29 +23,29 @@ enum Next {
None,
}
pub struct Layers<T> {
vec: Vec<LayerNode<T>>,
pub struct Layers {
vec: Vec<LayerNode>,
}
impl<T: Default> Layers<T> {
pub fn new() -> Layers<T>
where
T: Default,
{
#[derive(Default)]
pub struct Layer {
pub primitives: Primitives,
pub sensors: HashMap<Id, SenseShape>,
}
impl Layers {
pub fn new() -> Layers {
Self {
vec: vec![LayerNode::head()],
}
}
pub fn clear(&mut self)
where
T: Default,
{
pub fn clear(&mut self) {
self.vec.clear();
self.vec.push(LayerNode::head());
}
fn push(&mut self, node: LayerNode<T>) -> usize {
fn push(&mut self, node: LayerNode) -> usize {
let i = self.vec.len();
self.vec.push(node);
i
@@ -55,7 +55,7 @@ impl<T: Default> Layers<T> {
if let Next::Same(i) = self.vec[i].next {
return i;
}
let i_next = self.push(LayerNode::new(T::default(), self.vec[i].next));
let i_next = self.push(LayerNode::new(Layer::default(), self.vec[i].next));
self.vec[i].next = Next::Same(i_next);
self.vec[i_next].prev = Some(i);
i_next
@@ -65,42 +65,56 @@ impl<T: Default> Layers<T> {
if let Some(i) = self.vec[i].child {
return i;
}
let i_next = self.push(LayerNode::new(T::default(), Next::Parent(i)));
let i_next = self.push(LayerNode::new(Layer::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> {
pub fn iter_mut(&mut self) -> LayerIteratorMut<'_> {
LayerIteratorMut {
next: Some(0),
vec: &mut self.vec,
}
}
pub fn write<P: Primitive>(
&mut self,
layer: usize,
id: Id,
primitive: P,
region: UiRegion,
) -> PrimitiveHandle {
self[layer].primitives.write(layer, id, primitive, region)
}
pub fn free(&mut self, h: &PrimitiveHandle) {
self[h.layer].primitives.free(h)
}
}
impl<T: Default> Default for Layers<T> {
impl Default for Layers {
fn default() -> Self {
Self::new()
}
}
impl<T> Index<usize> for Layers<T> {
type Output = T;
impl Index<usize> for Layers {
type Output = Layer;
fn index(&self, index: usize) -> &Self::Output {
&self.vec[index].data
}
}
impl<T> IndexMut<usize> for Layers<T> {
impl IndexMut<usize> for Layers {
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 {
impl LayerNode {
pub fn new(data: Layer, next: Next) -> Self {
Self {
prev: None,
next,
@@ -109,26 +123,23 @@ impl<T> LayerNode<T> {
}
}
pub fn head() -> Self
where
T: Default,
{
Self::new(T::default(), Next::None)
pub fn head() -> Self {
Self::new(Layer::default(), Next::None)
}
}
pub struct LayerIteratorMut<'a, T> {
pub struct LayerIteratorMut<'a> {
next: Option<usize>,
vec: &'a mut Vec<LayerNode<T>>,
vec: &'a mut Vec<LayerNode>,
}
impl<'a, T> Iterator for LayerIteratorMut<'a, T> {
type Item = (usize, &'a mut T);
impl<'a> Iterator for LayerIteratorMut<'a> {
type Item = (usize, &'a mut Layer);
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]) };
let node: &mut LayerNode = 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 {
@@ -146,19 +157,3 @@ impl<'a, T> Iterator for LayerIteratorMut<'a, T> {
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)
}
}