better & more fine grained redraw system (should allow movement)
This commit is contained in:
@@ -41,12 +41,10 @@ impl Widget for Text {
|
|||||||
let (handle, offset) = painter.render_text(&mut self.buf, &self.content, &self.attrs);
|
let (handle, offset) = painter.render_text(&mut self.buf, &self.content, &self.attrs);
|
||||||
let dims = handle.size();
|
let dims = handle.size();
|
||||||
let size = offset.size(&handle);
|
let size = offset.size(&handle);
|
||||||
let mut region = painter.region().center().expand(size);
|
let mut region = Align::Center.pos().expand(size);
|
||||||
region.top_left.offset += offset.top_left;
|
region.top_left.offset += offset.top_left;
|
||||||
region.bot_right.offset = region.top_left.offset + dims;
|
region.bot_right.offset = region.top_left.offset + dims;
|
||||||
painter.draw_texture_at(&handle, region);
|
painter.draw_texture_within(&handle, region);
|
||||||
// TODO: when on_update is added to painter,
|
|
||||||
// reuse TextureHandle
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_size(&mut self, ctx: &mut SizeCtx) -> Vec2 {
|
fn get_size(&mut self, ctx: &mut SizeCtx) -> Vec2 {
|
||||||
|
|||||||
@@ -1,185 +1,164 @@
|
|||||||
|
use std::ops::Range;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
layout::{
|
layout::{
|
||||||
Active, SensorMap, SizeCtx, TextAttrs, TextBuffer, TextData, TextOffset, TextureHandle,
|
Active, SensorMap, SizeCtx, TextAttrs, TextBuffer, TextData, TextOffset, TextureHandle,
|
||||||
Textures, UiRegion, Vec2, WidgetId, WidgetInstance, Widgets,
|
Textures, UiRegion, Vec2, WidgetId, Widgets,
|
||||||
},
|
},
|
||||||
render::{Primitive, PrimitiveHandle, Primitives},
|
render::{Primitive, PrimitiveHandle, Primitives},
|
||||||
util::Id,
|
util::{HashSet, Id},
|
||||||
};
|
};
|
||||||
|
|
||||||
struct State {
|
pub struct Painter<'a, 'c> {
|
||||||
|
ctx: &'a mut PainterCtx<'c>,
|
||||||
region: UiRegion,
|
region: UiRegion,
|
||||||
children: Vec<Id>,
|
|
||||||
parent: Option<Id>,
|
|
||||||
textures: Vec<TextureHandle>,
|
textures: Vec<TextureHandle>,
|
||||||
// TODO: there's probably a better way but idc at this point
|
primitives: Vec<PrimitiveHandle>,
|
||||||
id: Option<Id>,
|
children: Vec<Id>,
|
||||||
|
id: Id,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct Painter<'a> {
|
pub struct PainterCtx<'a> {
|
||||||
widgets: &'a Widgets,
|
pub widgets: &'a Widgets,
|
||||||
sensors_map: &'a SensorMap,
|
pub sensor_map: &'a SensorMap,
|
||||||
pub(super) active: &'a mut Active,
|
pub active: &'a mut Active,
|
||||||
pub(super) primitives: &'a mut Primitives,
|
pub primitives: &'a mut Primitives,
|
||||||
textures: &'a mut Textures,
|
pub textures: &'a mut Textures,
|
||||||
text: &'a mut TextData,
|
pub text: &'a mut TextData,
|
||||||
screen_size: Vec2,
|
pub screen_size: Vec2,
|
||||||
/// state of what's currently being drawn
|
drawing: HashSet<Id>,
|
||||||
state: State,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> Painter<'a> {
|
pub struct WidgetInstance {
|
||||||
#[allow(clippy::too_many_arguments)]
|
pub id: Id,
|
||||||
pub(super) fn new(
|
pub region: UiRegion,
|
||||||
|
pub parent: Option<Id>,
|
||||||
|
pub textures: Vec<TextureHandle>,
|
||||||
|
pub primitives: Vec<PrimitiveHandle>,
|
||||||
|
pub children: Vec<Id>,
|
||||||
|
pub span: Range<usize>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> PainterCtx<'a> {
|
||||||
|
pub fn new(
|
||||||
widgets: &'a Widgets,
|
widgets: &'a Widgets,
|
||||||
primitives: &'a mut Primitives,
|
primitives: &'a mut Primitives,
|
||||||
sensors_map: &'a SensorMap,
|
sensor_map: &'a SensorMap,
|
||||||
active: &'a mut Active,
|
active: &'a mut Active,
|
||||||
text: &'a mut TextData,
|
|
||||||
textures: &'a mut Textures,
|
textures: &'a mut Textures,
|
||||||
|
text: &'a mut TextData,
|
||||||
screen_size: Vec2,
|
screen_size: Vec2,
|
||||||
) -> Self {
|
) -> Self {
|
||||||
Self {
|
Self {
|
||||||
widgets,
|
widgets,
|
||||||
|
sensor_map,
|
||||||
active,
|
active,
|
||||||
sensors_map,
|
|
||||||
primitives,
|
primitives,
|
||||||
text,
|
|
||||||
textures,
|
textures,
|
||||||
|
text,
|
||||||
screen_size,
|
screen_size,
|
||||||
state: State {
|
drawing: HashSet::new(),
|
||||||
region: UiRegion::full(),
|
|
||||||
children: Vec::new(),
|
|
||||||
parent: None,
|
|
||||||
id: None,
|
|
||||||
textures: Vec::new(),
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Writes a primitive to be rendered
|
pub fn redraw(&mut self, id: &Id) {
|
||||||
pub fn write_at<P: Primitive>(&mut self, data: P, region: UiRegion) -> PrimitiveHandle<P> {
|
self.drawing.clear();
|
||||||
self.primitives.write(data, region)
|
let Some(active) = self.remove(id) else {
|
||||||
}
|
return;
|
||||||
|
|
||||||
/// Writes a primitive to be rendered
|
|
||||||
pub fn write<P: Primitive>(&mut self, data: P) -> PrimitiveHandle<P> {
|
|
||||||
self.write_at(data, self.state.region)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Draws a widget within this widget's region.
|
|
||||||
pub fn draw<W>(&mut self, id: &WidgetId<W>) {
|
|
||||||
self.draw_at(id, self.state.region);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Draws a widget somewhere within this one.
|
|
||||||
/// Useful for drawing child widgets in select areas.
|
|
||||||
pub fn draw_within<W>(&mut self, id: &WidgetId<W>, region: UiRegion) {
|
|
||||||
self.draw_at(id, region.within(&self.state.region));
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Draws a widget in an arbitrary region.
|
|
||||||
pub fn draw_at<W>(&mut self, id: &WidgetId<W>, region: UiRegion) {
|
|
||||||
self.draw_raw_at(&id.id, region);
|
|
||||||
}
|
|
||||||
|
|
||||||
fn draw_raw_at(&mut self, id: &Id, region: UiRegion) {
|
|
||||||
self.state.children.push(id.duplicate());
|
|
||||||
|
|
||||||
// &mut self is passed to avoid copying all of the "static" pointers in self
|
|
||||||
// so need to save non static data here
|
|
||||||
let child_state = State {
|
|
||||||
region,
|
|
||||||
children: Vec::new(),
|
|
||||||
id: Some(id.duplicate()),
|
|
||||||
parent: self.state.id.as_ref().map(|i| i.duplicate()),
|
|
||||||
textures: Vec::new(),
|
|
||||||
};
|
};
|
||||||
// save state
|
self.primitives.set_pos(active.span.start);
|
||||||
let self_state = std::mem::replace(&mut self.state, child_state);
|
self.draw_inner(id, active.region, active.parent(), Some(active.children));
|
||||||
|
let delta = self.primitives.apply(active.span.clone());
|
||||||
// draw widgets
|
if delta != 0
|
||||||
let start_i = self.primitives.cur_pos();
|
&& let Some(parent) = active.parent
|
||||||
self.widgets.get_dyn_dynamic(id).draw(self);
|
{
|
||||||
let end_i = self.primitives.cur_pos();
|
self.shift_parent(parent, active.span.start, delta);
|
||||||
|
}
|
||||||
// restore state
|
|
||||||
let child_state = std::mem::replace(&mut self.state, self_state);
|
|
||||||
|
|
||||||
// add to active
|
|
||||||
self.active.add(
|
|
||||||
id,
|
|
||||||
WidgetInstance {
|
|
||||||
region,
|
|
||||||
span: start_i..end_i,
|
|
||||||
children: child_state.children,
|
|
||||||
parent: child_state.parent,
|
|
||||||
textures: child_state.textures,
|
|
||||||
},
|
|
||||||
self.sensors_map,
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn draw_texture(&mut self, handle: &TextureHandle) {
|
pub fn draw(&mut self, id: &Id) {
|
||||||
self.state.textures.push(handle.clone());
|
self.drawing.clear();
|
||||||
self.write(handle.primitive());
|
self.primitives.clear();
|
||||||
|
self.draw_inner(id, UiRegion::full(), None, None);
|
||||||
|
self.primitives.replace();
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn draw_texture_at(&mut self, handle: &TextureHandle, region: UiRegion) {
|
fn draw_inner(
|
||||||
let old = self.state.region;
|
|
||||||
self.state.region = region;
|
|
||||||
self.draw_texture(handle);
|
|
||||||
self.state.region = old;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// returns (handle, offset from top left)
|
|
||||||
pub fn render_text(
|
|
||||||
&mut self,
|
&mut self,
|
||||||
buffer: &mut TextBuffer,
|
id: &Id,
|
||||||
content: &str,
|
region: UiRegion,
|
||||||
attrs: &TextAttrs,
|
parent: Option<Id>,
|
||||||
) -> (TextureHandle, TextOffset) {
|
old_children: Option<Vec<Id>>,
|
||||||
self.text.draw(buffer, content, attrs, self.textures)
|
) {
|
||||||
|
if self.drawing.contains(id) {
|
||||||
|
panic!("Cannot draw the same widget twice");
|
||||||
}
|
}
|
||||||
|
let mut old_children = old_children.unwrap_or_default();
|
||||||
pub fn region(&self) -> UiRegion {
|
if let Some(active) = self.active.widgets.get(id) {
|
||||||
self.state.region
|
if active.parent != parent {
|
||||||
|
panic!("Cannot draw the same widget twice");
|
||||||
}
|
}
|
||||||
|
if active.region == region {
|
||||||
pub fn region_size(&self) -> Vec2 {
|
self.primitives.skip(&active.span);
|
||||||
self.state.region.in_size(self.screen_size)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn size<W>(&mut self, id: &WidgetId<W>) -> Vec2 {
|
|
||||||
self.widgets
|
|
||||||
.get_dyn_dynamic(&id.id)
|
|
||||||
.get_size(&mut self.size_ctx())
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn size_ctx(&mut self) -> SizeCtx<'_> {
|
|
||||||
SizeCtx {
|
|
||||||
size: self.region().in_size(self.screen_size),
|
|
||||||
text: self.text,
|
|
||||||
textures: self.textures,
|
|
||||||
widgets: self.widgets,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) fn redraw(&mut self, id: &Id) {
|
|
||||||
if !self.active.widgets.contains_key(id) {
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
let instance = self.free(id);
|
// TODO:
|
||||||
self.state.id = instance.parent;
|
// else if active.region.size() == region.size() { move }
|
||||||
self.primitives.prepare(instance.span.clone());
|
let active = self.remove(id).unwrap();
|
||||||
self.draw_raw_at(id, instance.region);
|
old_children = active.children;
|
||||||
let start = instance.span.start;
|
|
||||||
let delta = self.primitives.apply(instance.span);
|
|
||||||
if delta != 0
|
|
||||||
&& let Some(parent) = self.state.id.take()
|
|
||||||
{
|
|
||||||
self.shift_parent(parent, start, delta);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let mut painter = Painter {
|
||||||
|
region,
|
||||||
|
id: id.duplicate(),
|
||||||
|
textures: Vec::new(),
|
||||||
|
primitives: Vec::new(),
|
||||||
|
ctx: self,
|
||||||
|
children: Vec::new(),
|
||||||
|
};
|
||||||
|
|
||||||
|
// 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();
|
||||||
|
|
||||||
|
// add to active
|
||||||
|
let instance = WidgetInstance {
|
||||||
|
id: id.duplicate(),
|
||||||
|
region,
|
||||||
|
parent,
|
||||||
|
textures: painter.textures,
|
||||||
|
span: start_i..end_i,
|
||||||
|
primitives: painter.primitives,
|
||||||
|
children: painter.children,
|
||||||
|
};
|
||||||
|
for c in &old_children {
|
||||||
|
if !instance.children.contains(c) {
|
||||||
|
self.remove_rec(c);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
self.active.add(id, instance, self.sensor_map);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn remove(&mut self, id: &Id) -> Option<WidgetInstance> {
|
||||||
|
let inst = self.active.remove(id);
|
||||||
|
if let Some(inst) = &inst {
|
||||||
|
for h in &inst.primitives {
|
||||||
|
self.primitives.free(h);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
inst
|
||||||
|
}
|
||||||
|
|
||||||
|
fn remove_rec(&mut self, id: &Id) -> Option<WidgetInstance> {
|
||||||
|
let inst = self.remove(id);
|
||||||
|
if let Some(inst) = &inst {
|
||||||
|
for c in &inst.children {
|
||||||
|
self.remove_rec(c);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
inst
|
||||||
}
|
}
|
||||||
|
|
||||||
/// shifts the primitive spans for all widgets above this one in the tree
|
/// shifts the primitive spans for all widgets above this one in the tree
|
||||||
@@ -191,7 +170,7 @@ impl<'a> Painter<'a> {
|
|||||||
*end = end.strict_add_signed(delta);
|
*end = end.strict_add_signed(delta);
|
||||||
// ids are supposed to be unique so theoretically no cloning is needed
|
// ids are supposed to be unique so theoretically no cloning is needed
|
||||||
// leaving for now for testing
|
// leaving for now for testing
|
||||||
let parent = instance.parent.as_ref().map(|p| p.duplicate());
|
let parent = instance.parent();
|
||||||
for child in instance
|
for child in instance
|
||||||
.children
|
.children
|
||||||
.iter()
|
.iter()
|
||||||
@@ -221,12 +200,86 @@ impl<'a> Painter<'a> {
|
|||||||
self.shift_child(&child, start, delta);
|
self.shift_child(&child, start, delta);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn free(&mut self, id: &Id) -> WidgetInstance {
|
impl<'a, 'c> Painter<'a, 'c> {
|
||||||
let instance = self.active.remove(id).unwrap();
|
fn write_at<P: Primitive>(&mut self, data: P, region: UiRegion) {
|
||||||
for child in &instance.children {
|
self.primitives
|
||||||
self.free(child);
|
.push(self.ctx.primitives.write(self.id.duplicate(), data, region));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Writes a primitive to be rendered
|
||||||
|
pub fn write<P: Primitive>(&mut self, data: P) {
|
||||||
|
self.write_at(data, self.region)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Draws a widget within this widget's region.
|
||||||
|
pub fn draw<W>(&mut self, id: &WidgetId<W>) {
|
||||||
|
self.draw_at(id, self.region);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Draws a widget somewhere within this one.
|
||||||
|
/// Useful for drawing child widgets in select areas.
|
||||||
|
pub fn draw_within<W>(&mut self, id: &WidgetId<W>, region: UiRegion) {
|
||||||
|
self.draw_at(id, region.within(&self.region));
|
||||||
|
}
|
||||||
|
|
||||||
|
fn draw_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);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn draw_texture_within(&mut self, handle: &TextureHandle, region: UiRegion) {
|
||||||
|
self.textures.push(handle.clone());
|
||||||
|
self.write_at(handle.primitive(), region.within(&self.region));
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn draw_texture(&mut self, handle: &TextureHandle) {
|
||||||
|
self.textures.push(handle.clone());
|
||||||
|
self.write(handle.primitive());
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn draw_texture_at(&mut self, handle: &TextureHandle, region: UiRegion) {
|
||||||
|
self.textures.push(handle.clone());
|
||||||
|
self.write_at(handle.primitive(), region);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// returns (handle, offset from top left)
|
||||||
|
pub fn render_text(
|
||||||
|
&mut self,
|
||||||
|
buffer: &mut TextBuffer,
|
||||||
|
content: &str,
|
||||||
|
attrs: &TextAttrs,
|
||||||
|
) -> (TextureHandle, TextOffset) {
|
||||||
|
self.ctx
|
||||||
|
.text
|
||||||
|
.draw(buffer, content, attrs, self.ctx.textures)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn region_size(&self) -> Vec2 {
|
||||||
|
self.region.in_size(self.ctx.screen_size)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn size<W>(&mut self, id: &WidgetId<W>) -> Vec2 {
|
||||||
|
self.ctx
|
||||||
|
.widgets
|
||||||
|
.get_dyn_dynamic(&id.id)
|
||||||
|
.get_size(&mut self.size_ctx())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn size_ctx(&mut self) -> SizeCtx<'_> {
|
||||||
|
SizeCtx {
|
||||||
|
size: self.region.in_size(self.ctx.screen_size),
|
||||||
|
text: self.ctx.text,
|
||||||
|
textures: self.ctx.textures,
|
||||||
|
widgets: self.ctx.widgets,
|
||||||
}
|
}
|
||||||
instance
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl WidgetInstance {
|
||||||
|
pub fn parent(&self) -> Option<Id> {
|
||||||
|
self.parent.as_ref().map(|p| p.duplicate())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ use crate::{
|
|||||||
};
|
};
|
||||||
|
|
||||||
#[repr(C)]
|
#[repr(C)]
|
||||||
#[derive(Debug, Copy, Clone, bytemuck::Pod, bytemuck::Zeroable, Default)]
|
#[derive(Debug, Copy, Clone, PartialEq, bytemuck::Pod, bytemuck::Zeroable, Default)]
|
||||||
pub struct UiPos {
|
pub struct UiPos {
|
||||||
pub anchor: Vec2,
|
pub anchor: Vec2,
|
||||||
pub offset: Vec2,
|
pub offset: Vec2,
|
||||||
@@ -132,7 +132,7 @@ impl UIScalar {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[repr(C)]
|
#[repr(C)]
|
||||||
#[derive(Debug, Copy, Clone, bytemuck::Pod, bytemuck::Zeroable)]
|
#[derive(Debug, Copy, Clone, PartialEq, bytemuck::Pod, bytemuck::Zeroable)]
|
||||||
pub struct UiRegion {
|
pub struct UiRegion {
|
||||||
pub top_left: UiPos,
|
pub top_left: UiPos,
|
||||||
pub bot_right: UiPos,
|
pub bot_right: UiPos,
|
||||||
|
|||||||
@@ -2,15 +2,15 @@ use image::DynamicImage;
|
|||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
layout::{
|
layout::{
|
||||||
ActiveSensors, Painter, SensorMap, StaticWidgetId, TextData, TextureHandle, Textures,
|
ActiveSensors, PainterCtx, SensorMap, StaticWidgetId, TextData, TextureHandle, Textures,
|
||||||
UiRegion, Vec2, Widget, WidgetId, WidgetLike,
|
Vec2, Widget, WidgetId, WidgetInstance, WidgetLike,
|
||||||
},
|
},
|
||||||
render::Primitives,
|
render::Primitives,
|
||||||
util::{HashMap, Id, IdTracker},
|
util::{HashMap, Id, IdTracker},
|
||||||
};
|
};
|
||||||
use std::{
|
use std::{
|
||||||
any::{Any, TypeId},
|
any::{Any, TypeId},
|
||||||
ops::{Deref, DerefMut, Index, IndexMut, Range},
|
ops::{Deref, DerefMut, Index, IndexMut},
|
||||||
sync::mpsc::{Receiver, Sender, channel},
|
sync::mpsc::{Receiver, Sender, channel},
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -118,22 +118,20 @@ impl Ui {
|
|||||||
|
|
||||||
pub fn redraw_all(&mut self) {
|
pub fn redraw_all(&mut self) {
|
||||||
self.active.clear();
|
self.active.clear();
|
||||||
self.primitives.clear();
|
|
||||||
// free before bc nothing should exist
|
// free before bc nothing should exist
|
||||||
self.free();
|
self.free();
|
||||||
let mut painter = Painter::new(
|
let mut ctx = PainterCtx::new(
|
||||||
&self.widgets,
|
&self.widgets,
|
||||||
&mut self.primitives,
|
&mut self.primitives,
|
||||||
&self.sensor_map,
|
&self.sensor_map,
|
||||||
&mut self.active,
|
&mut self.active,
|
||||||
&mut self.text,
|
|
||||||
&mut self.textures,
|
&mut self.textures,
|
||||||
|
&mut self.text,
|
||||||
self.size,
|
self.size,
|
||||||
);
|
);
|
||||||
if let Some(base) = &self.base {
|
if let Some(base) = &self.base {
|
||||||
painter.draw(base);
|
ctx.draw(&base.id);
|
||||||
}
|
}
|
||||||
self.primitives.replace();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn update(&mut self) {
|
pub fn update(&mut self) {
|
||||||
@@ -146,17 +144,17 @@ impl Ui {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn redraw_updates(&mut self) {
|
fn redraw_updates(&mut self) {
|
||||||
let mut painter = Painter::new(
|
let mut ctx = PainterCtx::new(
|
||||||
&self.widgets,
|
&self.widgets,
|
||||||
&mut self.primitives,
|
&mut self.primitives,
|
||||||
&self.sensor_map,
|
&self.sensor_map,
|
||||||
&mut self.active,
|
&mut self.active,
|
||||||
&mut self.text,
|
|
||||||
&mut self.textures,
|
&mut self.textures,
|
||||||
|
&mut self.text,
|
||||||
self.size,
|
self.size,
|
||||||
);
|
);
|
||||||
for id in self.updates.drain(..) {
|
for id in self.updates.drain(..) {
|
||||||
painter.redraw(&id);
|
ctx.redraw(&id);
|
||||||
}
|
}
|
||||||
self.free();
|
self.free();
|
||||||
}
|
}
|
||||||
@@ -350,17 +348,10 @@ impl Default for Ui {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct WidgetInstance {
|
|
||||||
pub region: UiRegion,
|
|
||||||
pub span: Range<usize>,
|
|
||||||
pub children: Vec<Id>,
|
|
||||||
pub parent: Option<Id>,
|
|
||||||
pub textures: Vec<TextureHandle>,
|
|
||||||
}
|
|
||||||
pub type ActiveWidgets = HashMap<Id, WidgetInstance>;
|
pub type ActiveWidgets = HashMap<Id, WidgetInstance>;
|
||||||
|
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
pub(super) struct Active {
|
pub struct Active {
|
||||||
pub sensors: ActiveSensors,
|
pub sensors: ActiveSensors,
|
||||||
pub widgets: ActiveWidgets,
|
pub widgets: ActiveWidgets,
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -51,8 +51,8 @@ impl UiRenderer {
|
|||||||
pub fn update(&mut self, device: &Device, queue: &Queue, ui: &mut Ui) {
|
pub fn update(&mut self, device: &Device, queue: &Queue, ui: &mut Ui) {
|
||||||
if ui.primitives.updated {
|
if ui.primitives.updated {
|
||||||
self.instance
|
self.instance
|
||||||
.update(device, queue, &ui.primitives.instances);
|
.update(device, queue, ui.primitives.instances());
|
||||||
self.primitives.update(device, queue, &ui.primitives.data);
|
self.primitives.update(device, queue, ui.primitives.data());
|
||||||
self.primitive_group =
|
self.primitive_group =
|
||||||
Self::primitive_group(device, &self.primitive_layout, self.primitives.buffers())
|
Self::primitive_group(device, &self.primitive_layout, self.primitives.buffers())
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,19 +1,24 @@
|
|||||||
use std::{
|
use std::ops::{Deref, DerefMut, Range};
|
||||||
marker::PhantomData,
|
|
||||||
ops::{Deref, DerefMut, Range},
|
|
||||||
};
|
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
layout::{Color, UiRegion},
|
layout::{Color, UiRegion},
|
||||||
render::{ArrBuf, data::PrimitiveInstance},
|
render::{ArrBuf, data::PrimitiveInstance},
|
||||||
|
util::Id,
|
||||||
};
|
};
|
||||||
use bytemuck::Pod;
|
use bytemuck::Pod;
|
||||||
use wgpu::*;
|
use wgpu::*;
|
||||||
|
|
||||||
|
#[derive(Default)]
|
||||||
|
struct Instances {
|
||||||
|
data: Vec<PrimitiveInstance>,
|
||||||
|
assoc: Vec<Id>,
|
||||||
|
}
|
||||||
|
|
||||||
pub struct Primitives {
|
pub struct Primitives {
|
||||||
pub(super) instances: Vec<PrimitiveInstance>,
|
instances: Instances,
|
||||||
pub(super) run: Vec<PrimitiveInstance>,
|
run: Instances,
|
||||||
pub(super) data: PrimitiveData,
|
pos: usize,
|
||||||
|
data: PrimitiveData,
|
||||||
pub updated: bool,
|
pub updated: bool,
|
||||||
run_start: usize,
|
run_start: usize,
|
||||||
}
|
}
|
||||||
@@ -23,6 +28,7 @@ impl Default for Primitives {
|
|||||||
Self {
|
Self {
|
||||||
instances: Default::default(),
|
instances: Default::default(),
|
||||||
run: Default::default(),
|
run: Default::default(),
|
||||||
|
pos: 0,
|
||||||
data: Default::default(),
|
data: Default::default(),
|
||||||
updated: true,
|
updated: true,
|
||||||
run_start: 0,
|
run_start: 0,
|
||||||
@@ -98,15 +104,40 @@ macro_rules! primitives {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Primitives {
|
impl Primitives {
|
||||||
pub fn write<P: Primitive>(&mut self, data: P, region: UiRegion) -> PrimitiveHandle<P> {
|
pub fn write<P: Primitive>(&mut self, id: Id, data: P, region: UiRegion) -> PrimitiveHandle {
|
||||||
let vec = P::vec(&mut self.data);
|
let vec = P::vec(&mut self.data);
|
||||||
let i = vec.add(data);
|
let i = vec.add(data);
|
||||||
self.run.push(PrimitiveInstance {
|
let inst = PrimitiveInstance {
|
||||||
region,
|
region,
|
||||||
idx: i as u32,
|
idx: i as u32,
|
||||||
binding: P::BINDING,
|
binding: P::BINDING,
|
||||||
});
|
};
|
||||||
PrimitiveHandle::new(i)
|
if self.running() {
|
||||||
|
self.run.push(id, inst);
|
||||||
|
} else if self.instances.assoc.get(self.pos).is_some_and(|i| *i == id) {
|
||||||
|
self.instances.data[self.pos] = inst;
|
||||||
|
} else {
|
||||||
|
self.run_start = self.pos;
|
||||||
|
self.run.push(id, inst);
|
||||||
|
}
|
||||||
|
self.pos += 1;
|
||||||
|
PrimitiveHandle::new::<P>(i)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn running(&self) -> bool {
|
||||||
|
self.run.len() != 0
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn skip(&mut self, range: &Range<usize>) {
|
||||||
|
if self.running() {
|
||||||
|
self.run.data.extend(&self.instances.data[range.clone()]);
|
||||||
|
self.run.assoc.extend(
|
||||||
|
self.instances.assoc[range.clone()]
|
||||||
|
.iter()
|
||||||
|
.map(|i| i.duplicate()),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
self.pos += range.len();
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn clear(&mut self) {
|
pub(crate) fn clear(&mut self) {
|
||||||
@@ -114,53 +145,55 @@ impl Primitives {
|
|||||||
self.instances.clear();
|
self.instances.clear();
|
||||||
self.run.clear();
|
self.run.clear();
|
||||||
self.data.clear();
|
self.data.clear();
|
||||||
}
|
self.pos = 0;
|
||||||
|
|
||||||
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 {
|
pub fn apply(&mut self, span: Range<usize>) -> isize {
|
||||||
let delta = self.run.len() as isize - span.len() as isize;
|
let delta = self.pos as isize - span.end as isize;
|
||||||
self.instances.splice(span, self.run.drain(..));
|
if self.run.len() == 0 {
|
||||||
|
self.run_start = self.pos;
|
||||||
|
}
|
||||||
|
let span = self.run_start..span.end;
|
||||||
|
self.instances.splice(span, &mut self.run);
|
||||||
delta
|
delta
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn set_pos(&mut self, p: usize) {
|
||||||
|
self.pos = p;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn free(&mut self, h: &PrimitiveHandle) {
|
||||||
|
self.data.free(h.binding, h.idx);
|
||||||
|
}
|
||||||
|
|
||||||
pub fn replace(&mut self) {
|
pub fn replace(&mut self) {
|
||||||
std::mem::swap(&mut self.run, &mut self.instances);
|
std::mem::swap(&mut self.run, &mut self.instances);
|
||||||
self.run.clear();
|
self.run.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn cur_pos(&self) -> usize {
|
pub fn cur_pos(&self) -> usize {
|
||||||
self.run_start + self.run.len()
|
self.pos
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn data(&self) -> &PrimitiveData {
|
||||||
|
&self.data
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn instances(&self) -> &Vec<PrimitiveInstance> {
|
||||||
|
&self.instances.data
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// primitives but only exposes set for redrawing
|
pub struct PrimitiveHandle {
|
||||||
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,
|
idx: usize,
|
||||||
_pd: PhantomData<P>,
|
binding: u32,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<P: Primitive> PrimitiveHandle<P> {
|
impl PrimitiveHandle {
|
||||||
fn new(idx: usize) -> Self {
|
fn new<P: Primitive>(idx: usize) -> Self {
|
||||||
Self {
|
Self {
|
||||||
idx,
|
idx,
|
||||||
_pd: PhantomData,
|
binding: P::BINDING,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -236,3 +269,21 @@ impl<T> DerefMut for PrimitiveVec<T> {
|
|||||||
&mut self.vec
|
&mut self.vec
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Instances {
|
||||||
|
pub fn clear(&mut self) {
|
||||||
|
self.data.clear();
|
||||||
|
self.assoc.clear();
|
||||||
|
}
|
||||||
|
pub fn len(&self) -> usize {
|
||||||
|
self.data.len()
|
||||||
|
}
|
||||||
|
pub fn push(&mut self, id: Id, inst: PrimitiveInstance) {
|
||||||
|
self.assoc.push(id);
|
||||||
|
self.data.push(inst);
|
||||||
|
}
|
||||||
|
pub fn splice(&mut self, span: Range<usize>, other: &mut Instances) {
|
||||||
|
self.data.splice(span.clone(), other.data.drain(..));
|
||||||
|
self.assoc.splice(span, other.assoc.drain(..));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user