better & more fine grained redraw system (should allow movement)
This commit is contained in:
@@ -1,185 +1,164 @@
|
||||
use std::ops::Range;
|
||||
|
||||
use crate::{
|
||||
layout::{
|
||||
Active, SensorMap, SizeCtx, TextAttrs, TextBuffer, TextData, TextOffset, TextureHandle,
|
||||
Textures, UiRegion, Vec2, WidgetId, WidgetInstance, Widgets,
|
||||
Textures, UiRegion, Vec2, WidgetId, Widgets,
|
||||
},
|
||||
render::{Primitive, PrimitiveHandle, Primitives},
|
||||
util::Id,
|
||||
util::{HashSet, Id},
|
||||
};
|
||||
|
||||
struct State {
|
||||
pub struct Painter<'a, 'c> {
|
||||
ctx: &'a mut PainterCtx<'c>,
|
||||
region: UiRegion,
|
||||
children: Vec<Id>,
|
||||
parent: Option<Id>,
|
||||
textures: Vec<TextureHandle>,
|
||||
// TODO: there's probably a better way but idc at this point
|
||||
id: Option<Id>,
|
||||
primitives: Vec<PrimitiveHandle>,
|
||||
children: Vec<Id>,
|
||||
id: Id,
|
||||
}
|
||||
|
||||
pub struct Painter<'a> {
|
||||
widgets: &'a Widgets,
|
||||
sensors_map: &'a SensorMap,
|
||||
pub(super) active: &'a mut Active,
|
||||
pub(super) primitives: &'a mut Primitives,
|
||||
textures: &'a mut Textures,
|
||||
text: &'a mut TextData,
|
||||
screen_size: Vec2,
|
||||
/// state of what's currently being drawn
|
||||
state: State,
|
||||
pub struct PainterCtx<'a> {
|
||||
pub widgets: &'a Widgets,
|
||||
pub sensor_map: &'a SensorMap,
|
||||
pub active: &'a mut Active,
|
||||
pub primitives: &'a mut Primitives,
|
||||
pub textures: &'a mut Textures,
|
||||
pub text: &'a mut TextData,
|
||||
pub screen_size: Vec2,
|
||||
drawing: HashSet<Id>,
|
||||
}
|
||||
|
||||
impl<'a> Painter<'a> {
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
pub(super) fn new(
|
||||
pub struct WidgetInstance {
|
||||
pub id: Id,
|
||||
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,
|
||||
primitives: &'a mut Primitives,
|
||||
sensors_map: &'a SensorMap,
|
||||
sensor_map: &'a SensorMap,
|
||||
active: &'a mut Active,
|
||||
text: &'a mut TextData,
|
||||
textures: &'a mut Textures,
|
||||
text: &'a mut TextData,
|
||||
screen_size: Vec2,
|
||||
) -> Self {
|
||||
Self {
|
||||
widgets,
|
||||
sensor_map,
|
||||
active,
|
||||
sensors_map,
|
||||
primitives,
|
||||
text,
|
||||
textures,
|
||||
text,
|
||||
screen_size,
|
||||
state: State {
|
||||
region: UiRegion::full(),
|
||||
children: Vec::new(),
|
||||
parent: None,
|
||||
id: None,
|
||||
textures: Vec::new(),
|
||||
},
|
||||
drawing: HashSet::new(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Writes a primitive to be rendered
|
||||
pub fn write_at<P: Primitive>(&mut self, data: P, region: UiRegion) -> PrimitiveHandle<P> {
|
||||
self.primitives.write(data, region)
|
||||
}
|
||||
|
||||
/// 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(),
|
||||
pub fn redraw(&mut self, id: &Id) {
|
||||
self.drawing.clear();
|
||||
let Some(active) = self.remove(id) else {
|
||||
return;
|
||||
};
|
||||
self.primitives.set_pos(active.span.start);
|
||||
self.draw_inner(id, active.region, active.parent(), Some(active.children));
|
||||
let delta = self.primitives.apply(active.span.clone());
|
||||
if delta != 0
|
||||
&& let Some(parent) = active.parent
|
||||
{
|
||||
self.shift_parent(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();
|
||||
}
|
||||
|
||||
fn draw_inner(
|
||||
&mut self,
|
||||
id: &Id,
|
||||
region: UiRegion,
|
||||
parent: Option<Id>,
|
||||
old_children: Option<Vec<Id>>,
|
||||
) {
|
||||
if self.drawing.contains(id) {
|
||||
panic!("Cannot draw the same widget twice");
|
||||
}
|
||||
let mut old_children = old_children.unwrap_or_default();
|
||||
if let Some(active) = self.active.widgets.get(id) {
|
||||
if active.parent != parent {
|
||||
panic!("Cannot draw the same widget twice");
|
||||
}
|
||||
if active.region == region {
|
||||
self.primitives.skip(&active.span);
|
||||
return;
|
||||
}
|
||||
// TODO:
|
||||
// else if active.region.size() == region.size() { move }
|
||||
let active = self.remove(id).unwrap();
|
||||
old_children = active.children;
|
||||
}
|
||||
|
||||
let mut painter = Painter {
|
||||
region,
|
||||
id: id.duplicate(),
|
||||
textures: Vec::new(),
|
||||
primitives: Vec::new(),
|
||||
ctx: self,
|
||||
children: Vec::new(),
|
||||
};
|
||||
// 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_dynamic(id).draw(self);
|
||||
let end_i = self.primitives.cur_pos();
|
||||
|
||||
// restore state
|
||||
let child_state = std::mem::replace(&mut self.state, self_state);
|
||||
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
|
||||
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) {
|
||||
self.state.textures.push(handle.clone());
|
||||
self.write(handle.primitive());
|
||||
}
|
||||
|
||||
pub fn draw_texture_at(&mut self, handle: &TextureHandle, region: UiRegion) {
|
||||
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,
|
||||
buffer: &mut TextBuffer,
|
||||
content: &str,
|
||||
attrs: &TextAttrs,
|
||||
) -> (TextureHandle, TextOffset) {
|
||||
self.text.draw(buffer, content, attrs, self.textures)
|
||||
}
|
||||
|
||||
pub fn region(&self) -> UiRegion {
|
||||
self.state.region
|
||||
}
|
||||
|
||||
pub fn region_size(&self) -> Vec2 {
|
||||
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,
|
||||
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);
|
||||
}
|
||||
|
||||
pub(crate) fn redraw(&mut self, id: &Id) {
|
||||
if !self.active.widgets.contains_key(id) {
|
||||
return;
|
||||
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);
|
||||
}
|
||||
}
|
||||
let instance = self.free(id);
|
||||
self.state.id = instance.parent;
|
||||
self.primitives.prepare(instance.span.clone());
|
||||
self.draw_raw_at(id, instance.region);
|
||||
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);
|
||||
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
|
||||
@@ -191,7 +170,7 @@ impl<'a> Painter<'a> {
|
||||
*end = end.strict_add_signed(delta);
|
||||
// ids are supposed to be unique so theoretically no cloning is needed
|
||||
// leaving for now for testing
|
||||
let parent = instance.parent.as_ref().map(|p| p.duplicate());
|
||||
let parent = instance.parent();
|
||||
for child in instance
|
||||
.children
|
||||
.iter()
|
||||
@@ -221,12 +200,86 @@ impl<'a> Painter<'a> {
|
||||
self.shift_child(&child, start, delta);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn free(&mut self, id: &Id) -> WidgetInstance {
|
||||
let instance = self.active.remove(id).unwrap();
|
||||
for child in &instance.children {
|
||||
self.free(child);
|
||||
impl<'a, 'c> Painter<'a, 'c> {
|
||||
fn write_at<P: Primitive>(&mut self, data: P, region: UiRegion) {
|
||||
self.primitives
|
||||
.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())
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user