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