make painter not stupid (size ctx is kinda tho)
This commit is contained in:
@@ -142,7 +142,7 @@ impl<State: HasUi + 'static> HasEvents for State {
|
||||
id: impl IdLike<Self>,
|
||||
data: &mut <E::Event as Event>::Data<'_>,
|
||||
) {
|
||||
let f = self.ui().data.events.get_type::<E>().run_fn(id);
|
||||
let f = self.ui().events.get_type::<E>().run_fn(id);
|
||||
f(EventCtx { state: self, data })
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,7 +16,6 @@ mod attr;
|
||||
mod event;
|
||||
mod num;
|
||||
mod orientation;
|
||||
mod painter;
|
||||
mod primitive;
|
||||
mod render;
|
||||
mod state;
|
||||
@@ -29,7 +28,6 @@ pub use attr::*;
|
||||
pub use event::*;
|
||||
pub use num::*;
|
||||
pub use orientation::*;
|
||||
pub use painter::*;
|
||||
pub use primitive::*;
|
||||
pub use render::*;
|
||||
pub use ui::*;
|
||||
|
||||
@@ -61,10 +61,10 @@ impl UiRenderNode {
|
||||
|
||||
pub fn update<State>(&mut self, device: &Device, queue: &Queue, ui: &mut Ui<State>) {
|
||||
self.active.clear();
|
||||
for (i, primitives) in ui.data.layers.iter_mut() {
|
||||
for (i, primitives) in ui.layers.iter_mut() {
|
||||
self.active.push(i);
|
||||
for change in primitives.apply_free() {
|
||||
if let Some(inst) = ui.data.active.get_mut(&change.id) {
|
||||
if let Some(inst) = ui.active.get_mut(&change.id) {
|
||||
for h in &mut inst.primitives {
|
||||
if h.layer == i && h.inst_idx == change.old {
|
||||
h.inst_idx = change.new;
|
||||
@@ -101,10 +101,10 @@ impl UiRenderNode {
|
||||
}
|
||||
}
|
||||
let mut changed = false;
|
||||
changed |= self.textures.update(&mut ui.data.textures);
|
||||
if ui.data.masks.changed {
|
||||
ui.data.masks.changed = false;
|
||||
self.masks.update(device, queue, &ui.data.masks[..]);
|
||||
changed |= self.textures.update(&mut ui.textures);
|
||||
if ui.masks.changed {
|
||||
ui.masks.changed = false;
|
||||
self.masks.update(device, queue, &ui.masks[..]);
|
||||
changed = true;
|
||||
}
|
||||
if changed {
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
use crate::{
|
||||
ActiveData, Event, EventFn, EventLike, EventManager, IdLike, PainterData, PixelRegion,
|
||||
TextureHandle, Widget, WidgetHandle, WidgetId, WidgetLike, util::Vec2,
|
||||
Event, EventFn, EventLike, EventManager, IdLike, Mask, PixelRegion, PrimitiveLayers, TextData,
|
||||
TextureHandle, Textures, Widget, WidgetHandle, WidgetId, WidgetLike, Widgets,
|
||||
util::{HashMap, TrackedArena, Vec2},
|
||||
};
|
||||
use image::DynamicImage;
|
||||
use std::{
|
||||
@@ -8,12 +9,25 @@ use std::{
|
||||
sync::mpsc::{Receiver, Sender, channel},
|
||||
};
|
||||
|
||||
mod painter;
|
||||
pub use painter::{ActiveData, Painter, SizeCtx};
|
||||
|
||||
pub struct Ui<State> {
|
||||
// TODO: make this at least pub(super)
|
||||
pub data: PainterData<State>,
|
||||
// TODO: edit visibilities
|
||||
pub widgets: Widgets<State>,
|
||||
pub events: EventManager<State>,
|
||||
|
||||
// retained painter state
|
||||
pub active: HashMap<WidgetId, ActiveData>,
|
||||
pub layers: PrimitiveLayers,
|
||||
pub textures: Textures,
|
||||
pub text: TextData,
|
||||
output_size: Vec2,
|
||||
pub masks: TrackedArena<Mask, u32>,
|
||||
|
||||
root: Option<WidgetHandle<State>>,
|
||||
recv: Receiver<WidgetId>,
|
||||
pub(super) send: Sender<WidgetId>,
|
||||
send: Sender<WidgetId>,
|
||||
full_redraw: bool,
|
||||
resized: bool,
|
||||
}
|
||||
@@ -33,15 +47,15 @@ impl<State: 'static> Ui<State> {
|
||||
|
||||
/// useful for debugging
|
||||
pub fn set_label(&mut self, id: impl IdLike<State>, label: String) {
|
||||
self.data.widgets.data_mut(id.id()).unwrap().label = label;
|
||||
self.widgets.data_mut(id.id()).unwrap().label = label;
|
||||
}
|
||||
|
||||
pub fn label(&self, id: impl IdLike<State>) -> &String {
|
||||
&self.data.widgets.data(id.id()).unwrap().label
|
||||
&self.widgets.data(id.id()).unwrap().label
|
||||
}
|
||||
|
||||
pub fn add_widget<W: Widget<State>>(&mut self, w: W) -> WidgetHandle<State, W> {
|
||||
WidgetHandle::new(self.data.widgets.add(w), self.send.clone())
|
||||
WidgetHandle::new(self.widgets.add(w), self.send.clone())
|
||||
}
|
||||
|
||||
pub fn set_root<Tag>(&mut self, w: impl WidgetLike<State, Tag>) {
|
||||
@@ -57,18 +71,18 @@ impl<State: 'static> Ui<State> {
|
||||
where
|
||||
I::Widget: Sized,
|
||||
{
|
||||
self.data.widgets.get(id)
|
||||
self.widgets.get(id)
|
||||
}
|
||||
|
||||
pub fn get_mut<I: IdLike<State>>(&mut self, id: &I) -> Option<&mut I::Widget>
|
||||
where
|
||||
I::Widget: Sized,
|
||||
{
|
||||
self.data.widgets.get_mut(id)
|
||||
self.widgets.get_mut(id)
|
||||
}
|
||||
|
||||
pub fn add_texture(&mut self, image: DynamicImage) -> TextureHandle {
|
||||
self.data.textures.add(image)
|
||||
self.textures.add(image)
|
||||
}
|
||||
|
||||
pub fn register_event<E: EventLike>(
|
||||
@@ -77,9 +91,8 @@ impl<State: 'static> Ui<State> {
|
||||
event: E,
|
||||
f: impl for<'a> EventFn<State, <E::Event as Event>::Data<'a>>,
|
||||
) {
|
||||
self.data.events.register(id.id(), event, f);
|
||||
self.data
|
||||
.widgets
|
||||
self.events.register(id.id(), event, f);
|
||||
self.widgets
|
||||
.data_mut(id)
|
||||
.unwrap()
|
||||
.event_mgrs
|
||||
@@ -87,27 +100,15 @@ impl<State: 'static> Ui<State> {
|
||||
}
|
||||
|
||||
pub fn resize(&mut self, size: impl Into<Vec2>) {
|
||||
self.data.output_size = size.into();
|
||||
self.output_size = size.into();
|
||||
self.resized = true;
|
||||
}
|
||||
|
||||
pub fn redraw_all(&mut self) {
|
||||
for (id, active) in self.data.active.drain() {
|
||||
let data = self.data.widgets.data(id).unwrap();
|
||||
self.data.events.undraw(data, &active);
|
||||
}
|
||||
// free before bc nothing should exist
|
||||
self.free();
|
||||
if let Some(root) = &self.root {
|
||||
self.data.draw(root.id());
|
||||
}
|
||||
}
|
||||
|
||||
pub fn update(&mut self) {
|
||||
if self.full_redraw {
|
||||
self.redraw_all();
|
||||
self.full_redraw = false;
|
||||
} else if self.data.widgets.has_updates() {
|
||||
} else if self.widgets.has_updates() {
|
||||
self.redraw_updates();
|
||||
}
|
||||
if self.resized {
|
||||
@@ -116,36 +117,29 @@ impl<State: 'static> Ui<State> {
|
||||
}
|
||||
}
|
||||
|
||||
fn redraw_updates(&mut self) {
|
||||
let mut updates = std::mem::take(&mut self.data.widgets.updates);
|
||||
self.data.redraw(&mut updates);
|
||||
self.data.widgets.updates = updates;
|
||||
self.free();
|
||||
}
|
||||
|
||||
/// free any resources that don't have references anymore
|
||||
fn free(&mut self) {
|
||||
for id in self.recv.try_iter() {
|
||||
self.data.events.remove(id);
|
||||
self.data.widgets.delete(id);
|
||||
self.events.remove(id);
|
||||
self.widgets.delete(id);
|
||||
}
|
||||
self.data.textures.free();
|
||||
self.textures.free();
|
||||
}
|
||||
|
||||
pub fn needs_redraw(&self) -> bool {
|
||||
self.full_redraw || self.data.widgets.has_updates()
|
||||
self.full_redraw || self.widgets.has_updates()
|
||||
}
|
||||
|
||||
pub fn num_widgets(&self) -> usize {
|
||||
self.data.widgets.len()
|
||||
self.widgets.len()
|
||||
}
|
||||
|
||||
pub fn active_widgets(&self) -> usize {
|
||||
self.data.active.len()
|
||||
self.active.len()
|
||||
}
|
||||
|
||||
pub fn debug_layers(&self) {
|
||||
for ((idx, depth), primitives) in self.data.layers.iter_depth() {
|
||||
for ((idx, depth), primitives) in self.layers.iter_depth() {
|
||||
let indent = " ".repeat(depth * 2);
|
||||
let len = primitives.instances().len();
|
||||
print!("{indent}{idx}: {len} primitives");
|
||||
@@ -157,13 +151,13 @@ impl<State: 'static> Ui<State> {
|
||||
}
|
||||
|
||||
pub fn window_region(&self, id: &impl IdLike<State>) -> Option<PixelRegion> {
|
||||
let region = self.data.active.get(&id.id())?.region;
|
||||
Some(region.to_px(self.data.output_size))
|
||||
let region = self.active.get(&id.id())?.region;
|
||||
Some(region.to_px(self.output_size))
|
||||
}
|
||||
|
||||
pub fn debug(&self, label: &str) -> impl Iterator<Item = &ActiveData> {
|
||||
self.data.active.iter().filter_map(move |(&id, inst)| {
|
||||
let l = self.data.widgets.label(id);
|
||||
self.active.iter().filter_map(move |(&id, inst)| {
|
||||
let l = self.widgets.label(id);
|
||||
if l == label { Some(inst) } else { None }
|
||||
})
|
||||
}
|
||||
@@ -193,7 +187,14 @@ impl<State: 'static> Default for Ui<State> {
|
||||
fn default() -> Self {
|
||||
let (send, recv) = channel();
|
||||
Self {
|
||||
data: PainterData::default(),
|
||||
widgets: Default::default(),
|
||||
events: Default::default(),
|
||||
active: Default::default(),
|
||||
layers: Default::default(),
|
||||
masks: Default::default(),
|
||||
text: Default::default(),
|
||||
textures: Default::default(),
|
||||
output_size: Vec2::ZERO,
|
||||
root: Default::default(),
|
||||
full_redraw: false,
|
||||
send,
|
||||
@@ -1,13 +1,16 @@
|
||||
use std::ops::{Deref, DerefMut};
|
||||
|
||||
use crate::{
|
||||
Axis, EventManager, Len, PrimitiveLayers, RenderedText, Size, TextAttrs, TextBuffer, TextData,
|
||||
TextureHandle, Textures, UiRegion, UiVec2, WidgetHandle, WidgetId, Widgets,
|
||||
Axis, Len, RenderedText, Size, TextAttrs, TextBuffer, TextData, TextureHandle, Textures, Ui,
|
||||
UiRegion, UiVec2, WidgetHandle, WidgetId, Widgets,
|
||||
render::{Mask, MaskIdx, Primitive, PrimitiveHandle, PrimitiveInst},
|
||||
util::{HashMap, HashSet, TrackedArena, Vec2},
|
||||
util::{HashMap, HashSet, Vec2},
|
||||
};
|
||||
|
||||
/// makes your surfaces look pretty
|
||||
pub struct Painter<'a, 'c, State> {
|
||||
ctx: &'a mut PainterCtx<'c, State>,
|
||||
pub struct Painter<'a, 'b, State> {
|
||||
state: &'a mut DrawState<'b, State>,
|
||||
|
||||
region: UiRegion,
|
||||
mask: MaskIdx,
|
||||
textures: Vec<TextureHandle>,
|
||||
@@ -19,19 +22,11 @@ pub struct Painter<'a, 'c, State> {
|
||||
id: WidgetId,
|
||||
}
|
||||
|
||||
/// context for a painter; lets you draw and redraw widgets
|
||||
struct PainterCtx<'a, State> {
|
||||
pub widgets: &'a Widgets<State>,
|
||||
pub active: &'a mut HashMap<WidgetId, ActiveData>,
|
||||
pub layers: &'a mut PrimitiveLayers,
|
||||
pub textures: &'a mut Textures,
|
||||
pub masks: &'a mut TrackedArena<Mask, u32>,
|
||||
pub text: &'a mut TextData,
|
||||
pub output_size: Vec2,
|
||||
pub events: &'a mut EventManager<State>,
|
||||
pub cache_width: HashMap<WidgetId, (UiVec2, Len)>,
|
||||
pub cache_height: HashMap<WidgetId, (UiVec2, Len)>,
|
||||
pub needs_redraw: &'a mut HashSet<WidgetId>,
|
||||
/// state maintained between widgets during painting
|
||||
pub struct DrawState<'a, State> {
|
||||
ui: &'a mut Ui<State>,
|
||||
cache_width: HashMap<WidgetId, (UiVec2, Len)>,
|
||||
cache_height: HashMap<WidgetId, (UiVec2, Len)>,
|
||||
draw_started: HashSet<WidgetId>,
|
||||
}
|
||||
|
||||
@@ -57,45 +52,25 @@ pub struct ActiveData {
|
||||
pub layer: usize,
|
||||
}
|
||||
|
||||
/// data to be stored in Ui to create PainterCtxs easily
|
||||
pub struct PainterData<State> {
|
||||
pub widgets: Widgets<State>,
|
||||
pub active: HashMap<WidgetId, ActiveData>,
|
||||
pub layers: PrimitiveLayers,
|
||||
pub textures: Textures,
|
||||
pub text: TextData,
|
||||
pub output_size: Vec2,
|
||||
pub events: EventManager<State>,
|
||||
pub px_dependent: HashSet<WidgetId>,
|
||||
pub masks: TrackedArena<Mask, u32>,
|
||||
}
|
||||
|
||||
impl<State> Default for PainterData<State> {
|
||||
fn default() -> Self {
|
||||
impl<'a, State: 'static> DrawState<'a, State> {
|
||||
pub fn new(ui: &'a mut Ui<State>) -> Self {
|
||||
Self {
|
||||
widgets: Default::default(),
|
||||
active: Default::default(),
|
||||
layers: Default::default(),
|
||||
textures: Default::default(),
|
||||
text: Default::default(),
|
||||
output_size: Default::default(),
|
||||
events: Default::default(),
|
||||
px_dependent: Default::default(),
|
||||
masks: Default::default(),
|
||||
ui,
|
||||
cache_width: Default::default(),
|
||||
cache_height: Default::default(),
|
||||
draw_started: Default::default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, State: 'static> PainterCtx<'a, State> {
|
||||
/// redraws a widget that's currently active (drawn)
|
||||
/// can be called on something already drawn or removed,
|
||||
/// will just return if so
|
||||
pub fn redraw(&mut self, id: WidgetId) {
|
||||
self.needs_redraw.remove(&id);
|
||||
self.widgets.needs_redraw.remove(&id);
|
||||
if self.draw_started.contains(&id) {
|
||||
return;
|
||||
}
|
||||
let Some(active) = self.active.get(&id) else {
|
||||
let Some(active) = self.ui.active.get(&id) else {
|
||||
return;
|
||||
};
|
||||
let mut resize = active.resize;
|
||||
@@ -112,20 +87,15 @@ impl<'a, State: 'static> PainterCtx<'a, State> {
|
||||
// TODO: this is stupid having 2 of these, don't ask me what the consequences are
|
||||
let mut ret = false;
|
||||
if let Some((rid, (outer, old_desired))) = &mut resize.x {
|
||||
let new_desired = SizeCtx {
|
||||
source: id,
|
||||
cache_width: &mut self.cache_width,
|
||||
cache_height: &mut self.cache_height,
|
||||
text: self.text,
|
||||
textures: self.textures,
|
||||
widgets: self.widgets,
|
||||
outer: *outer,
|
||||
output_size: self.output_size,
|
||||
checked_width: &mut Default::default(),
|
||||
checked_height: &mut Default::default(),
|
||||
id,
|
||||
}
|
||||
.width_inner(id);
|
||||
let new_desired = self
|
||||
.size_ctx(
|
||||
id,
|
||||
*outer,
|
||||
id,
|
||||
&mut Default::default(),
|
||||
&mut Default::default(),
|
||||
)
|
||||
.width_inner(id);
|
||||
if new_desired != *old_desired {
|
||||
// unsure if I need to walk down the tree here
|
||||
self.redraw(*rid);
|
||||
@@ -138,20 +108,15 @@ impl<'a, State: 'static> PainterCtx<'a, State> {
|
||||
|
||||
if let Some((rid, (outer, old_desired))) = &mut resize.y {
|
||||
// NOTE: might need hack in Span here (or also do it properly here)
|
||||
let new_desired = SizeCtx {
|
||||
source: id,
|
||||
cache_width: &mut self.cache_width,
|
||||
cache_height: &mut self.cache_height,
|
||||
text: self.text,
|
||||
textures: self.textures,
|
||||
widgets: self.widgets,
|
||||
outer: *outer,
|
||||
output_size: self.output_size,
|
||||
checked_width: &mut Default::default(),
|
||||
checked_height: &mut Default::default(),
|
||||
id,
|
||||
}
|
||||
.height_inner(id);
|
||||
let new_desired = self
|
||||
.size_ctx(
|
||||
id,
|
||||
*outer,
|
||||
id,
|
||||
&mut Default::default(),
|
||||
&mut Default::default(),
|
||||
)
|
||||
.height_inner(id);
|
||||
if new_desired != *old_desired {
|
||||
self.redraw(*rid);
|
||||
*old_desired = new_desired;
|
||||
@@ -180,6 +145,29 @@ impl<'a, State: 'static> PainterCtx<'a, State> {
|
||||
finish(self, resize);
|
||||
}
|
||||
|
||||
fn size_ctx<'b>(
|
||||
&'b mut self,
|
||||
source: WidgetId,
|
||||
outer: UiVec2,
|
||||
id: WidgetId,
|
||||
checked_width: &'b mut HashMap<WidgetId, (UiVec2, Len)>,
|
||||
checked_height: &'b mut HashMap<WidgetId, (UiVec2, Len)>,
|
||||
) -> SizeCtx<'b, State> {
|
||||
SizeCtx {
|
||||
source,
|
||||
cache_width: &mut self.cache_width,
|
||||
cache_height: &mut self.cache_height,
|
||||
text: &mut self.ui.text,
|
||||
textures: &mut self.ui.textures,
|
||||
widgets: &self.ui.widgets,
|
||||
outer,
|
||||
output_size: self.ui.output_size,
|
||||
checked_width,
|
||||
checked_height,
|
||||
id,
|
||||
}
|
||||
}
|
||||
|
||||
fn draw_inner(
|
||||
&mut self,
|
||||
layer: usize,
|
||||
@@ -203,8 +191,8 @@ impl<'a, State: 'static> PainterCtx<'a, State> {
|
||||
// }
|
||||
let mut old_children = old_children.unwrap_or_default();
|
||||
let mut resize = ResizeRef::default();
|
||||
if let Some(active) = self.active.get_mut(&id)
|
||||
&& !self.needs_redraw.contains(&id)
|
||||
if let Some(active) = self.ui.active.get_mut(&id)
|
||||
&& !self.ui.widgets.needs_redraw.contains(&id)
|
||||
{
|
||||
// check to see if we can skip drawing first
|
||||
if active.parent != parent {
|
||||
@@ -228,35 +216,48 @@ impl<'a, State: 'static> PainterCtx<'a, State> {
|
||||
self.draw_started.insert(id);
|
||||
|
||||
let mut painter = Painter {
|
||||
state: self,
|
||||
region,
|
||||
mask,
|
||||
layer,
|
||||
id,
|
||||
textures: Vec::new(),
|
||||
primitives: Vec::new(),
|
||||
ctx: self,
|
||||
children: Vec::new(),
|
||||
children_width: Default::default(),
|
||||
children_height: Default::default(),
|
||||
};
|
||||
|
||||
painter.ctx.widgets.get_dyn_dynamic(id).draw(&mut painter);
|
||||
let mut widget = painter.state.widgets.get_dyn_dynamic(id);
|
||||
widget.draw(&mut painter);
|
||||
drop(widget);
|
||||
|
||||
let children_width = painter.children_width;
|
||||
let children_height = painter.children_height;
|
||||
let Painter {
|
||||
state: _,
|
||||
region,
|
||||
mask,
|
||||
textures,
|
||||
primitives,
|
||||
children,
|
||||
children_width,
|
||||
children_height,
|
||||
layer,
|
||||
id,
|
||||
} = painter;
|
||||
|
||||
// add to active
|
||||
let active = ActiveData {
|
||||
id,
|
||||
region,
|
||||
parent,
|
||||
textures: painter.textures,
|
||||
primitives: painter.primitives,
|
||||
children: painter.children,
|
||||
textures,
|
||||
primitives,
|
||||
children,
|
||||
resize,
|
||||
mask: painter.mask,
|
||||
mask,
|
||||
layer,
|
||||
};
|
||||
|
||||
// set resize for children who's size this widget depends on
|
||||
for (cid, outer) in children_width {
|
||||
if let Some(w) = self.active.get_mut(&cid)
|
||||
@@ -281,15 +282,15 @@ impl<'a, State: 'static> PainterCtx<'a, State> {
|
||||
}
|
||||
|
||||
// update modules
|
||||
let data = self.widgets.data(id).unwrap();
|
||||
self.events.draw(data, &active);
|
||||
let data = self.ui.widgets.data(id).unwrap();
|
||||
self.ui.events.draw(data, &active);
|
||||
self.active.insert(id, active);
|
||||
}
|
||||
|
||||
fn mov(&mut self, id: WidgetId, from: UiRegion, to: UiRegion) {
|
||||
let active = self.active.get_mut(&id).unwrap();
|
||||
let active = self.ui.active.get_mut(&id).unwrap();
|
||||
for h in &active.primitives {
|
||||
let region = self.layers[h.layer].region_mut(h);
|
||||
let region = self.ui.layers[h.layer].region_mut(h);
|
||||
*region = region.outside(&from).within(&to);
|
||||
}
|
||||
active.region = active.region.outside(&from).within(&to);
|
||||
@@ -314,8 +315,8 @@ impl<'a, State: 'static> PainterCtx<'a, State> {
|
||||
active.textures.clear();
|
||||
self.textures.free();
|
||||
if undraw {
|
||||
let data = self.widgets.data(id).unwrap();
|
||||
self.events.undraw(data, active);
|
||||
let data = self.ui.widgets.data(id).unwrap();
|
||||
self.ui.events.undraw(data, active);
|
||||
}
|
||||
}
|
||||
active
|
||||
@@ -332,43 +333,38 @@ impl<'a, State: 'static> PainterCtx<'a, State> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<State: 'static> PainterData<State> {
|
||||
fn ctx<'a>(&'a mut self, needs_redraw: &'a mut HashSet<WidgetId>) -> PainterCtx<'a, State> {
|
||||
PainterCtx {
|
||||
widgets: &self.widgets,
|
||||
active: &mut self.active,
|
||||
layers: &mut self.layers,
|
||||
textures: &mut self.textures,
|
||||
text: &mut self.text,
|
||||
output_size: self.output_size,
|
||||
events: &mut self.events,
|
||||
masks: &mut self.masks,
|
||||
cache_width: Default::default(),
|
||||
cache_height: Default::default(),
|
||||
draw_started: Default::default(),
|
||||
needs_redraw,
|
||||
impl<State: 'static> Ui<State> {
|
||||
pub fn redraw_all(&mut self) {
|
||||
for (id, active) in self.active.drain() {
|
||||
let data = self.widgets.data(id).unwrap();
|
||||
self.events.undraw(data, &active);
|
||||
}
|
||||
// free before bc nothing should exist
|
||||
self.free();
|
||||
if let Some(root) = &self.root {
|
||||
self.draw(root.id());
|
||||
}
|
||||
}
|
||||
|
||||
pub fn draw(&mut self, id: WidgetId) {
|
||||
let mut need_redraw = HashSet::default();
|
||||
let mut ctx = self.ctx(&mut need_redraw);
|
||||
ctx.draw_started.clear();
|
||||
ctx.layers.clear();
|
||||
ctx.draw_inner(0, id, UiRegion::FULL, None, MaskIdx::NONE, None);
|
||||
fn draw(&mut self, id: WidgetId) {
|
||||
self.layers.clear();
|
||||
self.widgets.needs_redraw.clear();
|
||||
let mut state = DrawState::new(self);
|
||||
state.draw_inner(0, id, UiRegion::FULL, None, MaskIdx::NONE, None);
|
||||
}
|
||||
|
||||
pub fn redraw(&mut self, ids: &mut HashSet<WidgetId>) {
|
||||
let mut ctx = self.ctx(ids);
|
||||
while let Some(&id) = ctx.needs_redraw.iter().next() {
|
||||
ctx.redraw(id);
|
||||
pub fn redraw_updates(&mut self) {
|
||||
let mut state = DrawState::new(self);
|
||||
while let Some(&id) = state.widgets.needs_redraw.iter().next() {
|
||||
state.redraw(id);
|
||||
}
|
||||
self.free();
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, 'c, State: 'static> Painter<'a, 'c, State> {
|
||||
fn primitive_at<P: Primitive>(&mut self, primitive: P, region: UiRegion) {
|
||||
let h = self.ctx.layers.write(
|
||||
let h = self.state.layers.write(
|
||||
self.layer,
|
||||
PrimitiveInst {
|
||||
id: self.id,
|
||||
@@ -379,7 +375,7 @@ impl<'a, 'c, State: 'static> Painter<'a, 'c, State> {
|
||||
);
|
||||
if self.mask != MaskIdx::NONE {
|
||||
// TODO: I have no clue if this works at all :joy:
|
||||
self.ctx.masks.push_ref(self.mask);
|
||||
self.state.masks.push_ref(self.mask);
|
||||
}
|
||||
self.primitives.push(h);
|
||||
}
|
||||
@@ -395,7 +391,7 @@ impl<'a, 'c, State: 'static> Painter<'a, 'c, State> {
|
||||
|
||||
pub fn set_mask(&mut self, region: UiRegion) {
|
||||
assert!(self.mask == MaskIdx::NONE);
|
||||
self.mask = self.ctx.masks.push(Mask { region });
|
||||
self.mask = self.state.masks.push(Mask { region });
|
||||
}
|
||||
|
||||
/// Draws a widget within this widget's region.
|
||||
@@ -411,7 +407,7 @@ impl<'a, 'c, State: 'static> Painter<'a, 'c, State> {
|
||||
|
||||
fn widget_at<W: ?Sized>(&mut self, id: &WidgetHandle<State, W>, region: UiRegion) {
|
||||
self.children.push(id.id());
|
||||
self.ctx
|
||||
self.state
|
||||
.draw_inner(self.layer, id.id(), region, Some(self.id), self.mask, None);
|
||||
}
|
||||
|
||||
@@ -432,7 +428,10 @@ impl<'a, 'c, State: 'static> Painter<'a, 'c, State> {
|
||||
|
||||
/// returns (handle, offset from top left)
|
||||
pub fn render_text(&mut self, buffer: &mut TextBuffer, attrs: &TextAttrs) -> RenderedText {
|
||||
self.ctx.text.draw(buffer, attrs, self.ctx.textures)
|
||||
self.state
|
||||
.ui
|
||||
.text
|
||||
.draw(buffer, attrs, &mut self.state.ui.textures)
|
||||
}
|
||||
|
||||
pub fn region(&self) -> UiRegion {
|
||||
@@ -450,49 +449,43 @@ impl<'a, 'c, State: 'static> Painter<'a, 'c, State> {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn size_ctx(&mut self) -> SizeCtx<'_, State> {
|
||||
SizeCtx {
|
||||
text: self.ctx.text,
|
||||
textures: self.ctx.textures,
|
||||
widgets: self.ctx.widgets,
|
||||
output_size: self.ctx.output_size,
|
||||
checked_width: &mut self.children_width,
|
||||
checked_height: &mut self.children_height,
|
||||
cache_width: &mut self.ctx.cache_width,
|
||||
cache_height: &mut self.ctx.cache_height,
|
||||
source: self.id,
|
||||
id: self.id,
|
||||
outer: self.region.size(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn output_size(&self) -> Vec2 {
|
||||
self.ctx.output_size
|
||||
self.state.output_size
|
||||
}
|
||||
|
||||
pub fn px_size(&mut self) -> Vec2 {
|
||||
self.region.size().to_abs(self.ctx.output_size)
|
||||
self.region.size().to_abs(self.state.output_size)
|
||||
}
|
||||
|
||||
pub fn text_data(&mut self) -> &mut TextData {
|
||||
self.ctx.text
|
||||
&mut self.state.text
|
||||
}
|
||||
|
||||
pub fn child_layer(&mut self) {
|
||||
self.layer = self.ctx.layers.child(self.layer);
|
||||
self.layer = self.state.layers.child(self.layer);
|
||||
}
|
||||
|
||||
pub fn next_layer(&mut self) {
|
||||
self.layer = self.ctx.layers.next(self.layer);
|
||||
self.layer = self.state.layers.next(self.layer);
|
||||
}
|
||||
|
||||
pub fn label(&self) -> &str {
|
||||
&self.ctx.widgets.data(self.id).unwrap().label
|
||||
&self.state.widgets.data(self.id).unwrap().label
|
||||
}
|
||||
|
||||
pub fn id(&self) -> &WidgetId {
|
||||
&self.id
|
||||
}
|
||||
|
||||
pub fn size_ctx(&mut self) -> SizeCtx<'_, State> {
|
||||
self.state.size_ctx(
|
||||
self.id,
|
||||
self.region.size(),
|
||||
self.id,
|
||||
&mut self.children_width,
|
||||
&mut self.children_height,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
pub struct SizeCtx<'a, State> {
|
||||
@@ -600,3 +593,16 @@ impl<State: 'static> SizeCtx<'_, State> {
|
||||
self.widgets.label(id)
|
||||
}
|
||||
}
|
||||
|
||||
impl<State> Deref for DrawState<'_, State> {
|
||||
type Target = Ui<State>;
|
||||
|
||||
fn deref(&self) -> &Self::Target {
|
||||
self.ui
|
||||
}
|
||||
}
|
||||
impl<State> DerefMut for DrawState<'_, State> {
|
||||
fn deref_mut(&mut self) -> &mut Self::Target {
|
||||
self.ui
|
||||
}
|
||||
}
|
||||
@@ -4,14 +4,14 @@ use crate::{
|
||||
};
|
||||
|
||||
pub struct Widgets<State> {
|
||||
pub updates: HashSet<WidgetId>,
|
||||
pub needs_redraw: HashSet<WidgetId>,
|
||||
vec: SlotVec<WidgetData<State>>,
|
||||
}
|
||||
|
||||
impl<State> Default for Widgets<State> {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
updates: Default::default(),
|
||||
needs_redraw: Default::default(),
|
||||
vec: Default::default(),
|
||||
}
|
||||
}
|
||||
@@ -19,7 +19,7 @@ impl<State> Default for Widgets<State> {
|
||||
|
||||
impl<State: 'static> Widgets<State> {
|
||||
pub fn has_updates(&self) -> bool {
|
||||
!self.updates.is_empty()
|
||||
!self.needs_redraw.is_empty()
|
||||
}
|
||||
|
||||
pub fn get_dyn(&self, id: WidgetId) -> Option<&dyn Widget<State>> {
|
||||
@@ -27,13 +27,13 @@ impl<State: 'static> Widgets<State> {
|
||||
}
|
||||
|
||||
pub fn get_dyn_mut(&mut self, id: WidgetId) -> Option<&mut dyn Widget<State>> {
|
||||
self.updates.insert(id);
|
||||
self.needs_redraw.insert(id);
|
||||
Some(self.vec.get_mut(id)?.widget.as_mut())
|
||||
}
|
||||
|
||||
/// get_dyn but dynamic borrow checking of widgets
|
||||
/// lets you do recursive (tree) operations, like the painter does
|
||||
pub(crate) fn get_dyn_dynamic(&self, id: WidgetId) -> WidgetWrapper<'_, State> {
|
||||
pub(crate) fn get_dyn_dynamic<'a>(&self, id: WidgetId) -> WidgetWrapper<'a, State> {
|
||||
// SAFETY: must guarantee no other mutable references to this widget exist
|
||||
// done through the borrow variable
|
||||
#[allow(mutable_transmutes)]
|
||||
|
||||
@@ -144,13 +144,13 @@ pub trait SensorUi<State> {
|
||||
|
||||
impl<State: 'static + HasUi> SensorUi<State> for State {
|
||||
fn run_sensors(&mut self, cursor: &CursorState, window_size: Vec2) {
|
||||
let layers = std::mem::take(&mut self.ui().data.layers);
|
||||
let layers = std::mem::take(&mut self.ui().layers);
|
||||
let mut active =
|
||||
std::mem::take(&mut self.ui().data.events.get_type::<CursorSense>().active);
|
||||
std::mem::take(&mut self.ui().events.get_type::<CursorSense>().active);
|
||||
for layer in layers.indices().rev() {
|
||||
let mut sensed = false;
|
||||
for (id, sensor) in active.get_mut(&layer).into_iter().flatten() {
|
||||
let shape = self.ui().data.active.get(id).unwrap().region;
|
||||
let shape = self.ui().active.get(id).unwrap().region;
|
||||
let region = shape.to_px(window_size);
|
||||
let in_shape = cursor.exists && region.contains(cursor.pos);
|
||||
sensor.hover.update(in_shape);
|
||||
@@ -175,8 +175,8 @@ impl<State: 'static + HasUi> SensorUi<State> for State {
|
||||
break;
|
||||
}
|
||||
}
|
||||
self.ui().data.events.get_type::<CursorSense>().active = active;
|
||||
self.ui().data.layers = layers;
|
||||
self.ui().events.get_type::<CursorSense>().active = active;
|
||||
self.ui().layers = layers;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -87,7 +87,7 @@ impl<State: 'static> TextBuilderOutput<State> for TextOutput {
|
||||
builder.attrs.line_height,
|
||||
));
|
||||
let hint = builder.hint.get(ui);
|
||||
let font_system = &mut ui.data.text.font_system;
|
||||
let font_system = &mut ui.text.font_system;
|
||||
buf.set_text(font_system, &builder.content, &Attrs::new(), SHAPING, None);
|
||||
let mut text = Text {
|
||||
content: builder.content.into(),
|
||||
@@ -117,7 +117,7 @@ impl<State: 'static> TextBuilderOutput<State> for TextEditOutput {
|
||||
TextView::new(buf, builder.attrs, builder.hint.get(ui)),
|
||||
builder.output.single_line,
|
||||
);
|
||||
let font_system = &mut ui.data.text.font_system;
|
||||
let font_system = &mut ui.text.font_system;
|
||||
text.buf
|
||||
.set_text(font_system, &builder.content, &Attrs::new(), SHAPING, None);
|
||||
builder.attrs.apply(font_system, &mut text.buf, None);
|
||||
|
||||
@@ -617,8 +617,8 @@ pub trait TextEditable<State> {
|
||||
impl<State: 'static, I: IdLike<State, Widget = TextEdit<State>>> TextEditable<State> for I {
|
||||
fn edit<'a>(&self, ui: &'a mut Ui<State>) -> TextEditCtx<'a, State> {
|
||||
TextEditCtx {
|
||||
text: ui.data.widgets.get_mut(self).unwrap(),
|
||||
font_system: &mut ui.data.text.font_system,
|
||||
text: ui.widgets.get_mut(self).unwrap(),
|
||||
font_system: &mut ui.text.font_system,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user