REACTIVITY
This commit is contained in:
@@ -2,52 +2,67 @@ use image::GenericImageView;
|
||||
|
||||
use crate::{
|
||||
layout::{
|
||||
ActiveSensors, SensorMap, TextAttrs, TextData, TextureHandle, Textures, UiPos, UiRegion,
|
||||
Vec2, WidgetId, Widgets,
|
||||
Active, SensorMap, TextAttrs, TextData, TextureHandle, Textures, UiRegion, Vec2, WidgetId,
|
||||
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> {
|
||||
nodes: &'a Widgets<Ctx>,
|
||||
widgets: &'a Widgets<Ctx>,
|
||||
ctx: &'a mut Ctx,
|
||||
sensors_map: &'a SensorMap<Ctx>,
|
||||
active_sensors: &'a mut ActiveSensors,
|
||||
primitives: &'a mut Primitives,
|
||||
pub(super) active: &'a mut Active,
|
||||
pub(super) primitives: &'a mut Primitives,
|
||||
textures: &'a mut Textures,
|
||||
text: &'a mut TextData,
|
||||
region: UiRegion,
|
||||
screen_size: Vec2,
|
||||
/// state of what's currently being drawn
|
||||
state: State,
|
||||
}
|
||||
|
||||
impl<'a, Ctx> Painter<'a, Ctx> {
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
pub(crate) fn new(
|
||||
pub(super) fn new(
|
||||
nodes: &'a Widgets<Ctx>,
|
||||
primitives: &'a mut Primitives,
|
||||
ctx: &'a mut Ctx,
|
||||
sensors_map: &'a SensorMap<Ctx>,
|
||||
active_sensors: &'a mut ActiveSensors,
|
||||
active: &'a mut Active,
|
||||
text: &'a mut TextData,
|
||||
textures: &'a mut Textures,
|
||||
screen_size: Vec2,
|
||||
) -> Self {
|
||||
Self {
|
||||
nodes,
|
||||
widgets: nodes,
|
||||
ctx,
|
||||
active_sensors,
|
||||
active,
|
||||
sensors_map,
|
||||
primitives,
|
||||
text,
|
||||
region: UiRegion::full(),
|
||||
textures,
|
||||
screen_size,
|
||||
state: State {
|
||||
region: UiRegion::full(),
|
||||
children: Vec::new(),
|
||||
parent: None,
|
||||
id: None,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
/// Writes a primitive to be rendered
|
||||
pub fn write<P: Primitive>(&mut self, data: P) {
|
||||
self.primitives.write(data, self.region);
|
||||
pub fn write<P: Primitive>(&mut self, data: P) -> PrimitiveHandle<P> {
|
||||
self.primitives.write(data, self.state.region)
|
||||
}
|
||||
|
||||
/// Draws a widget within this widget's region.
|
||||
@@ -55,7 +70,7 @@ impl<'a, Ctx> Painter<'a, Ctx> {
|
||||
where
|
||||
Ctx: 'static,
|
||||
{
|
||||
self.draw_at(id, self.region);
|
||||
self.draw_at(id, self.state.region);
|
||||
}
|
||||
|
||||
/// Draws a widget somewhere within this one.
|
||||
@@ -64,7 +79,7 @@ impl<'a, Ctx> Painter<'a, Ctx> {
|
||||
where
|
||||
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.
|
||||
@@ -72,15 +87,48 @@ impl<'a, Ctx> Painter<'a, Ctx> {
|
||||
where
|
||||
Ctx: 'static,
|
||||
{
|
||||
if self.sensors_map.get(&id.id).is_some() {
|
||||
self.active_sensors.push((region, id.id.duplicate()));
|
||||
self.draw_raw_at(&id.id, region);
|
||||
}
|
||||
|
||||
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
|
||||
// so need to save non static data here
|
||||
let old = self.region;
|
||||
self.region = region;
|
||||
self.nodes.get_dyn(id).draw(self);
|
||||
self.region = old;
|
||||
let child_state = State {
|
||||
region,
|
||||
children: Vec::new(),
|
||||
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) {
|
||||
@@ -89,16 +137,16 @@ impl<'a, Ctx> Painter<'a, Ctx> {
|
||||
}
|
||||
|
||||
pub fn draw_texture_at(&mut self, handle: &TextureHandle, region: UiRegion) {
|
||||
let old = self.region;
|
||||
self.region = region;
|
||||
let old = self.state.region;
|
||||
self.state.region = region;
|
||||
self.draw_texture(handle);
|
||||
self.region = old;
|
||||
self.state.region = old;
|
||||
}
|
||||
|
||||
pub fn draw_text(&mut self, content: &str, attrs: &TextAttrs) {
|
||||
let handle = self.text.draw(content, attrs, self.textures);
|
||||
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
|
||||
// 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
|
||||
@@ -107,10 +155,42 @@ impl<'a, Ctx> Painter<'a, Ctx> {
|
||||
}
|
||||
|
||||
pub fn region(&self) -> UiRegion {
|
||||
self.region
|
||||
self.state.region
|
||||
}
|
||||
|
||||
pub fn ctx(&mut self) -> &mut 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
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user