REACTIVITY
This commit is contained in:
@@ -2,52 +2,67 @@ use image::GenericImageView;
|
|||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
layout::{
|
layout::{
|
||||||
ActiveSensors, SensorMap, TextAttrs, TextData, TextureHandle, Textures, UiPos, UiRegion,
|
Active, SensorMap, TextAttrs, TextData, TextureHandle, Textures, UiRegion, Vec2, WidgetId,
|
||||||
Vec2, WidgetId, Widgets,
|
WidgetInstance, Widgets,
|
||||||
},
|
},
|
||||||
render::{Primitive, Primitives},
|
render::{Primitive, PrimitiveHandle, Primitives},
|
||||||
|
util::Id,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
struct State {
|
||||||
|
region: UiRegion,
|
||||||
|
children: Vec<Id>,
|
||||||
|
parent: Option<Id>,
|
||||||
|
// TODO: there's probably a better way but idc at this point
|
||||||
|
id: Option<Id>,
|
||||||
|
}
|
||||||
|
|
||||||
pub struct Painter<'a, Ctx: 'static> {
|
pub struct Painter<'a, Ctx: 'static> {
|
||||||
nodes: &'a Widgets<Ctx>,
|
widgets: &'a Widgets<Ctx>,
|
||||||
ctx: &'a mut Ctx,
|
ctx: &'a mut Ctx,
|
||||||
sensors_map: &'a SensorMap<Ctx>,
|
sensors_map: &'a SensorMap<Ctx>,
|
||||||
active_sensors: &'a mut ActiveSensors,
|
pub(super) active: &'a mut Active,
|
||||||
primitives: &'a mut Primitives,
|
pub(super) primitives: &'a mut Primitives,
|
||||||
textures: &'a mut Textures,
|
textures: &'a mut Textures,
|
||||||
text: &'a mut TextData,
|
text: &'a mut TextData,
|
||||||
region: UiRegion,
|
|
||||||
screen_size: Vec2,
|
screen_size: Vec2,
|
||||||
|
/// state of what's currently being drawn
|
||||||
|
state: State,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, Ctx> Painter<'a, Ctx> {
|
impl<'a, Ctx> Painter<'a, Ctx> {
|
||||||
#[allow(clippy::too_many_arguments)]
|
#[allow(clippy::too_many_arguments)]
|
||||||
pub(crate) fn new(
|
pub(super) fn new(
|
||||||
nodes: &'a Widgets<Ctx>,
|
nodes: &'a Widgets<Ctx>,
|
||||||
primitives: &'a mut Primitives,
|
primitives: &'a mut Primitives,
|
||||||
ctx: &'a mut Ctx,
|
ctx: &'a mut Ctx,
|
||||||
sensors_map: &'a SensorMap<Ctx>,
|
sensors_map: &'a SensorMap<Ctx>,
|
||||||
active_sensors: &'a mut ActiveSensors,
|
active: &'a mut Active,
|
||||||
text: &'a mut TextData,
|
text: &'a mut TextData,
|
||||||
textures: &'a mut Textures,
|
textures: &'a mut Textures,
|
||||||
screen_size: Vec2,
|
screen_size: Vec2,
|
||||||
) -> Self {
|
) -> Self {
|
||||||
Self {
|
Self {
|
||||||
nodes,
|
widgets: nodes,
|
||||||
ctx,
|
ctx,
|
||||||
active_sensors,
|
active,
|
||||||
sensors_map,
|
sensors_map,
|
||||||
primitives,
|
primitives,
|
||||||
text,
|
text,
|
||||||
region: UiRegion::full(),
|
|
||||||
textures,
|
textures,
|
||||||
screen_size,
|
screen_size,
|
||||||
|
state: State {
|
||||||
|
region: UiRegion::full(),
|
||||||
|
children: Vec::new(),
|
||||||
|
parent: None,
|
||||||
|
id: None,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Writes a primitive to be rendered
|
/// Writes a primitive to be rendered
|
||||||
pub fn write<P: Primitive>(&mut self, data: P) {
|
pub fn write<P: Primitive>(&mut self, data: P) -> PrimitiveHandle<P> {
|
||||||
self.primitives.write(data, self.region);
|
self.primitives.write(data, self.state.region)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Draws a widget within this widget's region.
|
/// Draws a widget within this widget's region.
|
||||||
@@ -55,7 +70,7 @@ impl<'a, Ctx> Painter<'a, Ctx> {
|
|||||||
where
|
where
|
||||||
Ctx: 'static,
|
Ctx: 'static,
|
||||||
{
|
{
|
||||||
self.draw_at(id, self.region);
|
self.draw_at(id, self.state.region);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Draws a widget somewhere within this one.
|
/// Draws a widget somewhere within this one.
|
||||||
@@ -64,7 +79,7 @@ impl<'a, Ctx> Painter<'a, Ctx> {
|
|||||||
where
|
where
|
||||||
Ctx: 'static,
|
Ctx: 'static,
|
||||||
{
|
{
|
||||||
self.draw_at(id, region.within(&self.region));
|
self.draw_at(id, region.within(&self.state.region));
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Draws a widget in an arbitrary region.
|
/// Draws a widget in an arbitrary region.
|
||||||
@@ -72,15 +87,48 @@ impl<'a, Ctx> Painter<'a, Ctx> {
|
|||||||
where
|
where
|
||||||
Ctx: 'static,
|
Ctx: 'static,
|
||||||
{
|
{
|
||||||
if self.sensors_map.get(&id.id).is_some() {
|
self.draw_raw_at(&id.id, region);
|
||||||
self.active_sensors.push((region, id.id.duplicate()));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn draw_raw_at(&mut self, id: &Id, region: UiRegion)
|
||||||
|
where
|
||||||
|
Ctx: 'static,
|
||||||
|
{
|
||||||
|
if self.active.widgets.contains_key(id) {
|
||||||
|
panic!("widget drawn twice!");
|
||||||
|
}
|
||||||
|
self.state.children.push(id.duplicate());
|
||||||
|
|
||||||
// &mut self is passed to avoid copying all of the "static" pointers in self
|
// &mut self is passed to avoid copying all of the "static" pointers in self
|
||||||
// so need to save non static data here
|
// so need to save non static data here
|
||||||
let old = self.region;
|
let child_state = State {
|
||||||
self.region = region;
|
region,
|
||||||
self.nodes.get_dyn(id).draw(self);
|
children: Vec::new(),
|
||||||
self.region = old;
|
id: Some(id.duplicate()),
|
||||||
|
parent: self.state.id.as_ref().map(|i| i.duplicate()),
|
||||||
|
};
|
||||||
|
// save state
|
||||||
|
let self_state = std::mem::replace(&mut self.state, child_state);
|
||||||
|
|
||||||
|
// draw widgets
|
||||||
|
let start_i = self.primitives.cur_pos();
|
||||||
|
self.widgets.get_dyn(id).draw(self);
|
||||||
|
let end_i = self.primitives.cur_pos();
|
||||||
|
|
||||||
|
// restore state
|
||||||
|
let child_state = std::mem::replace(&mut self.state, self_state);
|
||||||
|
|
||||||
|
// add to active
|
||||||
|
self.active.add(
|
||||||
|
id,
|
||||||
|
WidgetInstance {
|
||||||
|
region,
|
||||||
|
primitives: (start_i..end_i).into(),
|
||||||
|
children: child_state.children,
|
||||||
|
parent: child_state.parent,
|
||||||
|
},
|
||||||
|
self.sensors_map,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn draw_texture(&mut self, handle: &TextureHandle) {
|
pub fn draw_texture(&mut self, handle: &TextureHandle) {
|
||||||
@@ -89,16 +137,16 @@ impl<'a, Ctx> Painter<'a, Ctx> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn draw_texture_at(&mut self, handle: &TextureHandle, region: UiRegion) {
|
pub fn draw_texture_at(&mut self, handle: &TextureHandle, region: UiRegion) {
|
||||||
let old = self.region;
|
let old = self.state.region;
|
||||||
self.region = region;
|
self.state.region = region;
|
||||||
self.draw_texture(handle);
|
self.draw_texture(handle);
|
||||||
self.region = old;
|
self.state.region = old;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn draw_text(&mut self, content: &str, attrs: &TextAttrs) {
|
pub fn draw_text(&mut self, content: &str, attrs: &TextAttrs) {
|
||||||
let handle = self.text.draw(content, attrs, self.textures);
|
let handle = self.text.draw(content, attrs, self.textures);
|
||||||
let dims: Vec2 = self.textures[&handle].dimensions().into();
|
let dims: Vec2 = self.textures[&handle].dimensions().into();
|
||||||
let mut region = self.region.center().expand(dims);
|
let mut region = self.state.region.center().expand(dims);
|
||||||
// TODO: I feel like this shouldn't be needed
|
// TODO: I feel like this shouldn't be needed
|
||||||
// what this does is makes sure the text doesn't get squeezed into one pixel less
|
// what this does is makes sure the text doesn't get squeezed into one pixel less
|
||||||
// I'm unsure exactly why it happens or if this will ever expand it too much
|
// I'm unsure exactly why it happens or if this will ever expand it too much
|
||||||
@@ -107,10 +155,42 @@ impl<'a, Ctx> Painter<'a, Ctx> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn region(&self) -> UiRegion {
|
pub fn region(&self) -> UiRegion {
|
||||||
self.region
|
self.state.region
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn ctx(&mut self) -> &mut Ctx {
|
pub fn ctx(&mut self) -> &mut Ctx {
|
||||||
self.ctx
|
self.ctx
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub(crate) fn redraw(&mut self, id: &Id) {
|
||||||
|
if !self.active.widgets.contains_key(id) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
let instance = self.free(id);
|
||||||
|
self.state.id = instance.parent;
|
||||||
|
self.primitives.prepare(instance.primitives.into());
|
||||||
|
self.draw_raw_at(id, instance.region);
|
||||||
|
let delta = self.primitives.apply(instance.primitives.into());
|
||||||
|
if let Some(parent) = self.state.id.take() {
|
||||||
|
self.shift_end(parent, delta);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn shift_end(&mut self, id: Id, delta: isize) {
|
||||||
|
let instance = self.active.widgets.get_mut(&id).unwrap();
|
||||||
|
let end = &mut instance.primitives.end;
|
||||||
|
*end = end.strict_add_signed(delta);
|
||||||
|
if let Some(parent) = &instance.parent {
|
||||||
|
let parent = parent.duplicate();
|
||||||
|
self.shift_end(parent, delta);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn free(&mut self, id: &Id) -> WidgetInstance {
|
||||||
|
let instance = self.active.remove(id).unwrap();
|
||||||
|
for child in &instance.children {
|
||||||
|
self.free(child);
|
||||||
|
}
|
||||||
|
instance
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
use crate::{
|
use crate::{
|
||||||
HashMap,
|
|
||||||
layout::{Ui, UiRegion, Vec2, WidgetId},
|
layout::{Ui, UiRegion, Vec2, WidgetId},
|
||||||
|
util::HashMap,
|
||||||
util::Id,
|
util::Id,
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -36,7 +36,7 @@ pub struct Sensor<Ctx> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub type SensorMap<Ctx> = HashMap<Id, SensorGroup<Ctx>>;
|
pub type SensorMap<Ctx> = HashMap<Id, SensorGroup<Ctx>>;
|
||||||
pub type ActiveSensors = Vec<(SenseShape, Id)>;
|
pub type ActiveSensors = HashMap<Id, SenseShape>;
|
||||||
pub type SenseShape = UiRegion;
|
pub type SenseShape = UiRegion;
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct SenseTrigger {
|
pub struct SenseTrigger {
|
||||||
@@ -74,9 +74,9 @@ impl<Ctx> Ui<Ctx> {
|
|||||||
where
|
where
|
||||||
Ctx: 'static,
|
Ctx: 'static,
|
||||||
{
|
{
|
||||||
let mut active = std::mem::take(&mut self.active_sensors);
|
let active = std::mem::take(&mut self.active.sensors);
|
||||||
let mut map = std::mem::take(&mut self.sensor_map);
|
let mut map = std::mem::take(&mut self.sensor_map);
|
||||||
for (shape, id) in active.iter_mut().rev() {
|
for (id, shape) in active.iter() {
|
||||||
let group = &mut map.get_mut(id).unwrap();
|
let group = &mut map.get_mut(id).unwrap();
|
||||||
let region = shape.to_screen(window_size);
|
let region = shape.to_screen(window_size);
|
||||||
let in_shape = cursor.exists && region.contains(cursor.pos);
|
let in_shape = cursor.exists && region.contains(cursor.pos);
|
||||||
@@ -90,7 +90,7 @@ impl<Ctx> Ui<Ctx> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
self.sensor_map = map;
|
self.sensor_map = map;
|
||||||
self.active_sensors = active;
|
self.active.sensors = active;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -2,8 +2,8 @@ use cosmic_text::{Attrs, Buffer, FontSystem, Metrics, Shaping, SwashCache};
|
|||||||
use image::{Rgba, RgbaImage};
|
use image::{Rgba, RgbaImage};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
HashMap,
|
|
||||||
layout::{TextureHandle, Textures, UiColor},
|
layout::{TextureHandle, Textures, UiColor},
|
||||||
|
util::HashMap,
|
||||||
};
|
};
|
||||||
|
|
||||||
pub(crate) struct TextData {
|
pub(crate) struct TextData {
|
||||||
|
|||||||
@@ -1,17 +1,17 @@
|
|||||||
use image::DynamicImage;
|
use image::DynamicImage;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
HashMap,
|
|
||||||
layout::{
|
layout::{
|
||||||
ActiveSensors, Painter, SensorMap, TextData, TextureHandle, Textures, Vec2, Widget,
|
ActiveSensors, Painter, SensorMap, TextData, TextureHandle, Textures, UiRegion, Vec2,
|
||||||
WidgetId, WidgetLike,
|
Widget, WidgetId, WidgetLike,
|
||||||
},
|
},
|
||||||
render::Primitives,
|
render::Primitives,
|
||||||
util::{Id, IdTracker},
|
util::{HashMap, Id, IdTracker},
|
||||||
};
|
};
|
||||||
use std::{
|
use std::{
|
||||||
any::{Any, TypeId},
|
any::{Any, TypeId},
|
||||||
ops::{Index, IndexMut},
|
ops::{Index, IndexMut},
|
||||||
|
range::Range,
|
||||||
sync::mpsc::{Receiver, Sender, channel},
|
sync::mpsc::{Receiver, Sender, channel},
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -28,7 +28,7 @@ pub struct Ui<Ctx> {
|
|||||||
pub(crate) text: TextData,
|
pub(crate) text: TextData,
|
||||||
full_redraw: bool,
|
full_redraw: bool,
|
||||||
|
|
||||||
pub(super) active_sensors: ActiveSensors,
|
pub(super) active: Active,
|
||||||
pub(super) sensor_map: SensorMap<Ctx>,
|
pub(super) sensor_map: SensorMap<Ctx>,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -96,7 +96,7 @@ impl<Ctx> Ui<Ctx> {
|
|||||||
where
|
where
|
||||||
Ctx: 'static,
|
Ctx: 'static,
|
||||||
{
|
{
|
||||||
self.active_sensors.clear();
|
self.active.clear();
|
||||||
self.primitives.clear();
|
self.primitives.clear();
|
||||||
self.free();
|
self.free();
|
||||||
let mut painter = Painter::new(
|
let mut painter = Painter::new(
|
||||||
@@ -104,7 +104,7 @@ impl<Ctx> Ui<Ctx> {
|
|||||||
&mut self.primitives,
|
&mut self.primitives,
|
||||||
ctx,
|
ctx,
|
||||||
&self.sensor_map,
|
&self.sensor_map,
|
||||||
&mut self.active_sensors,
|
&mut self.active,
|
||||||
&mut self.text,
|
&mut self.text,
|
||||||
&mut self.textures,
|
&mut self.textures,
|
||||||
self.size,
|
self.size,
|
||||||
@@ -112,6 +112,7 @@ impl<Ctx> Ui<Ctx> {
|
|||||||
if let Some(base) = &self.base {
|
if let Some(base) = &self.base {
|
||||||
painter.draw(base);
|
painter.draw(base);
|
||||||
}
|
}
|
||||||
|
self.primitives.replace();
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn update(&mut self, ctx: &mut Ctx)
|
pub fn update(&mut self, ctx: &mut Ctx)
|
||||||
@@ -122,9 +123,26 @@ impl<Ctx> Ui<Ctx> {
|
|||||||
self.redraw_all(ctx);
|
self.redraw_all(ctx);
|
||||||
self.full_redraw = false;
|
self.full_redraw = false;
|
||||||
} else if !self.updates.is_empty() {
|
} else if !self.updates.is_empty() {
|
||||||
// TODO: partial updates
|
self.redraw_updates(ctx);
|
||||||
self.redraw_all(ctx);
|
}
|
||||||
self.updates.drain(..);
|
}
|
||||||
|
|
||||||
|
fn redraw_updates(&mut self, ctx: &mut Ctx)
|
||||||
|
where
|
||||||
|
Ctx: 'static,
|
||||||
|
{
|
||||||
|
let mut painter = Painter::new(
|
||||||
|
&self.widgets,
|
||||||
|
&mut self.primitives,
|
||||||
|
ctx,
|
||||||
|
&self.sensor_map,
|
||||||
|
&mut self.active,
|
||||||
|
&mut self.text,
|
||||||
|
&mut self.textures,
|
||||||
|
self.size,
|
||||||
|
);
|
||||||
|
for id in self.updates.drain(..) {
|
||||||
|
painter.redraw(&id.id);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -168,8 +186,8 @@ impl<Ctx> Widgets<Ctx> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_dyn<W>(&self, id: &WidgetId<W>) -> &dyn Widget<Ctx> {
|
pub fn get_dyn(&self, id: &Id) -> &dyn Widget<Ctx> {
|
||||||
self.map.get(&id.id).unwrap().as_ref()
|
self.map.get(id).unwrap().as_ref()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get<W: Widget<Ctx>>(&self, id: &WidgetId<W>) -> Option<&W> {
|
pub fn get<W: Widget<Ctx>>(&self, id: &WidgetId<W>) -> Option<&W> {
|
||||||
@@ -231,7 +249,7 @@ impl<Ctx: 'static> Default for Ui<Ctx> {
|
|||||||
textures: Textures::new(),
|
textures: Textures::new(),
|
||||||
text: TextData::default(),
|
text: TextData::default(),
|
||||||
full_redraw: false,
|
full_redraw: false,
|
||||||
active_sensors: Default::default(),
|
active: Default::default(),
|
||||||
sensor_map: Default::default(),
|
sensor_map: Default::default(),
|
||||||
send,
|
send,
|
||||||
recv,
|
recv,
|
||||||
@@ -239,3 +257,37 @@ impl<Ctx: 'static> Default for Ui<Ctx> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub struct WidgetInstance {
|
||||||
|
pub region: UiRegion,
|
||||||
|
pub primitives: Range<usize>,
|
||||||
|
pub children: Vec<Id>,
|
||||||
|
pub parent: Option<Id>,
|
||||||
|
}
|
||||||
|
pub type ActiveWidgets = HashMap<Id, WidgetInstance>;
|
||||||
|
|
||||||
|
#[derive(Default)]
|
||||||
|
pub(super) struct Active {
|
||||||
|
pub sensors: ActiveSensors,
|
||||||
|
pub widgets: ActiveWidgets,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Active {
|
||||||
|
pub fn clear(&mut self) {
|
||||||
|
self.sensors.clear();
|
||||||
|
self.widgets.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn remove(&mut self, id: &Id) -> Option<WidgetInstance> {
|
||||||
|
let instance = self.widgets.remove(id);
|
||||||
|
self.sensors.remove(id);
|
||||||
|
instance
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn add<Ctx>(&mut self, id: &Id, instance: WidgetInstance, sensors_map: &SensorMap<Ctx>) {
|
||||||
|
if sensors_map.get(id).is_some() {
|
||||||
|
self.sensors.insert(id.duplicate(), instance.region);
|
||||||
|
}
|
||||||
|
self.widgets.insert(id.duplicate(), instance);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -2,8 +2,8 @@
|
|||||||
#![feature(const_ops)]
|
#![feature(const_ops)]
|
||||||
#![feature(const_trait_impl)]
|
#![feature(const_trait_impl)]
|
||||||
#![feature(const_from)]
|
#![feature(const_from)]
|
||||||
#![feature(trait_alias)]
|
#![feature(map_try_insert)]
|
||||||
#![feature(negative_impls)]
|
#![feature(new_range_api)]
|
||||||
|
|
||||||
pub mod core;
|
pub mod core;
|
||||||
pub mod layout;
|
pub mod layout;
|
||||||
@@ -15,6 +15,3 @@ pub mod prelude {
|
|||||||
pub use crate::layout::*;
|
pub use crate::layout::*;
|
||||||
pub use crate::render::*;
|
pub use crate::render::*;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub type HashMap<K, V> = std::collections::HashMap<K, V>;
|
|
||||||
pub type HashSet<K> = std::collections::HashSet<K>;
|
|
||||||
|
|||||||
@@ -1,3 +1,8 @@
|
|||||||
|
use std::{
|
||||||
|
marker::PhantomData,
|
||||||
|
ops::{Deref, DerefMut, Range},
|
||||||
|
};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
layout::{Color, TextureHandle, UiRegion},
|
layout::{Color, TextureHandle, UiRegion},
|
||||||
render::{ArrBuf, data::PrimitiveInstance},
|
render::{ArrBuf, data::PrimitiveInstance},
|
||||||
@@ -7,33 +12,37 @@ use wgpu::*;
|
|||||||
|
|
||||||
pub struct Primitives {
|
pub struct Primitives {
|
||||||
pub(super) instances: Vec<PrimitiveInstance>,
|
pub(super) instances: Vec<PrimitiveInstance>,
|
||||||
|
pub(super) run: Vec<PrimitiveInstance>,
|
||||||
pub(super) data: PrimitiveData,
|
pub(super) data: PrimitiveData,
|
||||||
// ensure drawn textures don't get freed
|
// ensure drawn textures don't get freed
|
||||||
pub(crate) drawn_textures: Vec<TextureHandle>,
|
pub(crate) drawn_textures: Vec<TextureHandle>,
|
||||||
pub updated: bool,
|
pub updated: bool,
|
||||||
|
run_start: usize,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for Primitives {
|
impl Default for Primitives {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
Self {
|
Self {
|
||||||
instances: Default::default(),
|
instances: Default::default(),
|
||||||
|
run: Default::default(),
|
||||||
data: Default::default(),
|
data: Default::default(),
|
||||||
drawn_textures: Default::default(),
|
drawn_textures: Default::default(),
|
||||||
updated: true,
|
updated: true,
|
||||||
|
run_start: 0,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub trait Primitive: Pod {
|
pub trait Primitive: Pod {
|
||||||
const BINDING: u32;
|
const BINDING: u32;
|
||||||
fn vec(data: &mut PrimitiveData) -> &mut Vec<Self>;
|
fn vec(data: &mut PrimitiveData) -> &mut PrimitiveVec<Self>;
|
||||||
}
|
}
|
||||||
|
|
||||||
macro_rules! primitives {
|
macro_rules! primitives {
|
||||||
($($name:ident: $ty:ty => $binding:expr,)*) => {
|
($($name:ident: $ty:ty => $binding:expr,)*) => {
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
pub struct PrimitiveData {
|
pub struct PrimitiveData {
|
||||||
$($name: Vec<$ty>,)*
|
$($name: PrimitiveVec<$ty>,)*
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct PrimitiveBuffers {
|
pub struct PrimitiveBuffers {
|
||||||
@@ -68,6 +77,12 @@ macro_rules! primitives {
|
|||||||
pub fn clear(&mut self) {
|
pub fn clear(&mut self) {
|
||||||
$(self.$name.clear();)*
|
$(self.$name.clear();)*
|
||||||
}
|
}
|
||||||
|
pub fn free(&mut self, binding: u32, idx: usize) {
|
||||||
|
match binding {
|
||||||
|
$(<$ty>::BINDING => self.$name.free(idx),)*
|
||||||
|
_ => unreachable!()
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$(
|
$(
|
||||||
@@ -75,7 +90,7 @@ macro_rules! primitives {
|
|||||||
unsafe impl bytemuck::Zeroable for $ty {}
|
unsafe impl bytemuck::Zeroable for $ty {}
|
||||||
impl Primitive for $ty {
|
impl Primitive for $ty {
|
||||||
const BINDING: u32 = $binding;
|
const BINDING: u32 = $binding;
|
||||||
fn vec(data: &mut PrimitiveData) -> &mut Vec<Self> {
|
fn vec(data: &mut PrimitiveData) -> &mut PrimitiveVec<Self> {
|
||||||
&mut data.$name
|
&mut data.$name
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -86,23 +101,72 @@ macro_rules! primitives {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Primitives {
|
impl Primitives {
|
||||||
pub fn write<P: Primitive>(&mut self, data: P, region: UiRegion) {
|
pub fn write<P: Primitive>(&mut self, data: P, region: UiRegion) -> PrimitiveHandle<P> {
|
||||||
let vec = P::vec(&mut self.data);
|
let vec = P::vec(&mut self.data);
|
||||||
let i = vec.len() as u32;
|
let i = vec.add(data);
|
||||||
self.instances.push(PrimitiveInstance {
|
self.run.push(PrimitiveInstance {
|
||||||
region,
|
region,
|
||||||
idx: i,
|
idx: i as u32,
|
||||||
binding: P::BINDING,
|
binding: P::BINDING,
|
||||||
});
|
});
|
||||||
vec.push(data);
|
PrimitiveHandle::new(i)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn clear(&mut self) {
|
pub(crate) fn clear(&mut self) {
|
||||||
self.updated = true;
|
self.updated = true;
|
||||||
self.instances.clear();
|
self.instances.clear();
|
||||||
|
self.run.clear();
|
||||||
self.data.clear();
|
self.data.clear();
|
||||||
self.drawn_textures.clear();
|
self.drawn_textures.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn set<P: Primitive>(&mut self, handle: &PrimitiveHandle<P>, data: P) {
|
||||||
|
P::vec(&mut self.data)[handle.idx] = data;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn prepare(&mut self, span: Range<usize>) {
|
||||||
|
self.run_start = span.start;
|
||||||
|
for instance in &self.instances[span] {
|
||||||
|
self.data.free(instance.binding, instance.idx as usize);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn apply(&mut self, span: Range<usize>) -> isize {
|
||||||
|
let delta = self.run.len() as isize - span.len() as isize;
|
||||||
|
self.instances.splice(span, self.run.drain(..));
|
||||||
|
delta
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn replace(&mut self) {
|
||||||
|
std::mem::swap(&mut self.run, &mut self.instances);
|
||||||
|
self.run.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn cur_pos(&self) -> usize {
|
||||||
|
self.run_start + self.run.len()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// primitives but only exposes set for redrawing
|
||||||
|
pub struct PrimitiveEditor<'a>(&'a mut Primitives);
|
||||||
|
impl PrimitiveEditor<'_> {
|
||||||
|
pub fn set<P: Primitive>(&mut self, handle: &PrimitiveHandle<P>, data: P) {
|
||||||
|
self.0.set(handle, data);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct PrimitiveHandle<P: Primitive> {
|
||||||
|
idx: usize,
|
||||||
|
_pd: PhantomData<P>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<P: Primitive> PrimitiveHandle<P> {
|
||||||
|
fn new(idx: usize) -> Self {
|
||||||
|
Self {
|
||||||
|
idx,
|
||||||
|
_pd: PhantomData,
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
primitives!(
|
primitives!(
|
||||||
@@ -125,3 +189,54 @@ pub struct TexturePrimitive {
|
|||||||
pub view_idx: u32,
|
pub view_idx: u32,
|
||||||
pub sampler_idx: u32,
|
pub sampler_idx: u32,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub struct PrimitiveVec<T> {
|
||||||
|
vec: Vec<T>,
|
||||||
|
free: Vec<usize>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T> PrimitiveVec<T> {
|
||||||
|
pub fn new() -> Self {
|
||||||
|
Self {
|
||||||
|
vec: Vec::new(),
|
||||||
|
free: Vec::new(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pub fn add(&mut self, t: T) -> usize {
|
||||||
|
if let Some(i) = self.free.pop() {
|
||||||
|
self.vec[i] = t;
|
||||||
|
i
|
||||||
|
} else {
|
||||||
|
let i = self.vec.len();
|
||||||
|
self.vec.push(t);
|
||||||
|
i
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pub fn free(&mut self, i: usize) {
|
||||||
|
self.free.push(i);
|
||||||
|
}
|
||||||
|
pub fn clear(&mut self) {
|
||||||
|
self.free.clear();
|
||||||
|
self.vec.clear();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T> Default for PrimitiveVec<T> {
|
||||||
|
fn default() -> Self {
|
||||||
|
Self::new()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T> Deref for PrimitiveVec<T> {
|
||||||
|
type Target = Vec<T>;
|
||||||
|
|
||||||
|
fn deref(&self) -> &Self::Target {
|
||||||
|
&self.vec
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T> DerefMut for PrimitiveVec<T> {
|
||||||
|
fn deref_mut(&mut self) -> &mut Self::Target {
|
||||||
|
&mut self.vec
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -6,3 +6,5 @@ pub use id::*;
|
|||||||
pub use math::*;
|
pub use math::*;
|
||||||
pub use refcount::*;
|
pub use refcount::*;
|
||||||
|
|
||||||
|
pub type HashMap<K, V> = std::collections::HashMap<K, V>;
|
||||||
|
pub type HashSet<K> = std::collections::HashSet<K>;
|
||||||
|
|||||||
Reference in New Issue
Block a user