Compare commits
3 Commits
acd67179b7
...
172e7157be
| Author | SHA1 | Date | |
|---|---|---|---|
| 172e7157be | |||
| e3b1ddc993 | |||
| 5aef8c2201 |
@@ -121,8 +121,10 @@ impl Client {
|
||||
.span(Dir::DOWN)
|
||||
.add_static(&mut ui);
|
||||
|
||||
let texts = Span::empty(Dir::DOWN).gap(10).add_static(&mut ui);
|
||||
let msg_area = (Rect::new(Color::SKY), texts.scroll().masked()).stack();
|
||||
let texts = Span::empty(Dir::DOWN)
|
||||
.gap(10)
|
||||
.add_static(&mut ui);
|
||||
let msg_area = texts.scroll().masked().background(rect(Color::SKY));
|
||||
let add_text = text("add")
|
||||
.editable()
|
||||
.text_align(Align::LEFT)
|
||||
@@ -142,9 +144,8 @@ impl Client {
|
||||
client.ui.text(id).select(ctx.cursor, ctx.size);
|
||||
client.focus = Some(id.clone());
|
||||
});
|
||||
let msg_box = (rect(Color::WHITE.darker(0.5)), text)
|
||||
.stack()
|
||||
.size(StackSize::Child(1))
|
||||
let msg_box = text
|
||||
.background(rect(Color::WHITE.darker(0.5)))
|
||||
.add(&mut client.ui);
|
||||
client.ui[texts].children.push(msg_box.any());
|
||||
})
|
||||
|
||||
@@ -6,7 +6,6 @@ pub struct Masked {
|
||||
|
||||
impl Widget for Masked {
|
||||
fn draw(&mut self, painter: &mut Painter) {
|
||||
println!("yahoo! {}", painter.region());
|
||||
painter.set_mask(painter.region());
|
||||
painter.widget(&self.inner);
|
||||
}
|
||||
|
||||
@@ -9,6 +9,7 @@ mod stack;
|
||||
pub use align::*;
|
||||
pub use offset::*;
|
||||
pub use pad::*;
|
||||
pub use scroll::*;
|
||||
pub use sized::*;
|
||||
pub use span::*;
|
||||
pub use stack::*;
|
||||
|
||||
@@ -3,9 +3,10 @@ use crate::prelude::*;
|
||||
pub struct Scroll {
|
||||
inner: WidgetId,
|
||||
axis: Axis,
|
||||
snap_end: bool,
|
||||
amt: f32,
|
||||
draw_len: f32,
|
||||
snap_end: bool,
|
||||
container_len: f32,
|
||||
content_len: f32,
|
||||
}
|
||||
|
||||
impl Widget for Scroll {
|
||||
@@ -17,10 +18,14 @@ impl Widget for Scroll {
|
||||
.apply_rest()
|
||||
.within_len(container_len)
|
||||
.to_abs(output_len);
|
||||
let container_len = container_len.to_abs(output_len);
|
||||
self.draw_len = content_len;
|
||||
// let region = UiRegion::FULL.offset(self.amt);
|
||||
// painter.widget_within(&self.inner, region);
|
||||
self.container_len = container_len.to_abs(output_len);
|
||||
self.content_len = content_len;
|
||||
if self.snap_end {
|
||||
self.amt = self.content_len - self.container_len;
|
||||
}
|
||||
self.update_amt();
|
||||
let region = UiRegion::FULL.offset(Vec2::from_axis(self.axis, -self.amt, 0.0));
|
||||
painter.widget_within(&self.inner, region);
|
||||
}
|
||||
|
||||
fn desired_width(&mut self, _: &mut SizeCtx) -> Len {
|
||||
@@ -33,7 +38,26 @@ impl Widget for Scroll {
|
||||
}
|
||||
|
||||
impl Scroll {
|
||||
pub fn new(inner: WidgetId, axis: Axis) -> Self {
|
||||
Self {
|
||||
inner,
|
||||
axis,
|
||||
amt: 0.0,
|
||||
snap_end: true,
|
||||
container_len: 0.0,
|
||||
content_len: 0.0,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn update_amt(&mut self) {
|
||||
self.amt = self.amt.max(0.0);
|
||||
let len = (self.content_len - self.container_len).max(0.0);
|
||||
self.amt = self.amt.min(len);
|
||||
self.snap_end = self.amt == len;
|
||||
}
|
||||
|
||||
pub fn scroll(&mut self, amt: f32) {
|
||||
self.amt += amt;
|
||||
self.amt -= amt;
|
||||
self.update_amt();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,9 +11,6 @@ impl Widget for Span {
|
||||
fn draw(&mut self, painter: &mut Painter) {
|
||||
let total = self.len_sum(&mut painter.size_ctx());
|
||||
let mut start = UiScalar::rel_min();
|
||||
if painter.label() == "fricker" {
|
||||
println!("redraw");
|
||||
}
|
||||
for child in &self.children {
|
||||
let mut span = UiSpan::FULL;
|
||||
span.start = start;
|
||||
|
||||
@@ -10,7 +10,7 @@ pub trait CoreWidget<W, Tag> {
|
||||
fn width(self, len: impl Into<Len>) -> impl WidgetFn<Sized>;
|
||||
fn height(self, len: impl Into<Len>) -> impl WidgetFn<Sized>;
|
||||
fn offset(self, amt: impl Into<UiVec2>) -> impl WidgetFn<Offset>;
|
||||
fn scroll(self) -> impl WidgetIdFn<Offset>;
|
||||
fn scroll(self) -> impl WidgetIdFn<Scroll>;
|
||||
fn masked(self) -> impl WidgetFn<Masked>;
|
||||
fn background<T>(self, w: impl WidgetLike<T>) -> impl WidgetFn<Stack>;
|
||||
fn z_offset(self, offset: usize) -> impl WidgetFn<Stack>;
|
||||
@@ -77,11 +77,14 @@ impl<W: WidgetLike<Tag>, Tag> CoreWidget<W::Widget, Tag> for W {
|
||||
}
|
||||
}
|
||||
|
||||
fn scroll(self) -> impl WidgetIdFn<Offset> {
|
||||
self.offset(UiVec2::ZERO)
|
||||
.edit_on(CursorSense::Scroll, |w, data| {
|
||||
w.amt += UiVec2::abs(data.scroll_delta * 50.0);
|
||||
})
|
||||
fn scroll(self) -> impl WidgetIdFn<Scroll> {
|
||||
move |ui| {
|
||||
Scroll::new(self.add(ui).any(), Axis::Y)
|
||||
.edit_on(CursorSense::Scroll, |w, data| {
|
||||
w.scroll(data.scroll_delta.y * 50.0);
|
||||
})
|
||||
.add(ui)
|
||||
}
|
||||
}
|
||||
|
||||
fn masked(self) -> impl WidgetFn<Masked> {
|
||||
|
||||
@@ -7,6 +7,7 @@ use crate::{
|
||||
util::{HashMap, HashSet, Id, TrackedArena},
|
||||
};
|
||||
|
||||
/// makes your surfaces look pretty
|
||||
pub struct Painter<'a, 'c> {
|
||||
ctx: &'a mut PainterCtx<'c>,
|
||||
region: UiRegion,
|
||||
@@ -22,7 +23,8 @@ pub struct Painter<'a, 'c> {
|
||||
id: Id,
|
||||
}
|
||||
|
||||
pub struct PainterCtx<'a> {
|
||||
/// context for a painter; lets you draw and redraw widgets
|
||||
struct PainterCtx<'a> {
|
||||
pub widgets: &'a Widgets,
|
||||
pub active: &'a mut HashMap<Id, WidgetInstance>,
|
||||
pub layers: &'a mut Layers,
|
||||
@@ -33,15 +35,19 @@ pub struct PainterCtx<'a> {
|
||||
pub modules: &'a mut Modules,
|
||||
pub cache_width: HashMap<Id, (UiVec2, Len)>,
|
||||
pub cache_height: HashMap<Id, (UiVec2, Len)>,
|
||||
pub needs_redraw: HashSet<Id>,
|
||||
draw_started: HashSet<Id>,
|
||||
}
|
||||
|
||||
/// stores information for children about the highest level parent that needed their size
|
||||
/// so that they can redraw the parent if their size changes
|
||||
#[derive(Clone, Copy, Debug, Default)]
|
||||
pub struct ResizeRef {
|
||||
x: Option<(Id, (UiVec2, Len))>,
|
||||
y: Option<(Id, (UiVec2, Len))>,
|
||||
}
|
||||
|
||||
/// important non rendering data for retained drawing
|
||||
#[derive(Debug)]
|
||||
pub struct WidgetInstance {
|
||||
pub id: Id,
|
||||
@@ -55,6 +61,7 @@ pub struct WidgetInstance {
|
||||
pub layer: usize,
|
||||
}
|
||||
|
||||
/// data to be stored in Ui to create PainterCtxs easily
|
||||
#[derive(Default)]
|
||||
pub struct PainterData {
|
||||
pub widgets: Widgets,
|
||||
@@ -69,23 +76,11 @@ pub struct PainterData {
|
||||
}
|
||||
|
||||
impl<'a> PainterCtx<'a> {
|
||||
pub fn new(data: &'a mut PainterData) -> Self {
|
||||
Self {
|
||||
widgets: &data.widgets,
|
||||
active: &mut data.active,
|
||||
layers: &mut data.layers,
|
||||
textures: &mut data.textures,
|
||||
text: &mut data.text,
|
||||
output_size: data.output_size,
|
||||
modules: &mut data.modules,
|
||||
masks: &mut data.masks,
|
||||
cache_width: Default::default(),
|
||||
cache_height: Default::default(),
|
||||
draw_started: Default::default(),
|
||||
}
|
||||
}
|
||||
|
||||
/// 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: Id) {
|
||||
self.needs_redraw.remove(&id);
|
||||
if self.draw_started.contains(&id) {
|
||||
return;
|
||||
}
|
||||
@@ -94,13 +89,17 @@ impl<'a> PainterCtx<'a> {
|
||||
};
|
||||
let mut resize = active.resize;
|
||||
|
||||
// set resize back after redrawing
|
||||
let finish = |s: &mut Self, resize| {
|
||||
if let Some(active) = s.active.get_mut(&id) {
|
||||
// might need to get_or_insert here instead of just assuming
|
||||
active.resize = resize;
|
||||
}
|
||||
};
|
||||
|
||||
// check if a parent depends on the desired size of this, if so then redraw it first
|
||||
// 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,
|
||||
@@ -117,15 +116,17 @@ impl<'a> PainterCtx<'a> {
|
||||
}
|
||||
.width_inner(id);
|
||||
if new_desired != *old_desired {
|
||||
// unsure if I need to walk down the tree here
|
||||
self.redraw(*rid);
|
||||
*old_desired = new_desired;
|
||||
if self.draw_started.contains(&id) {
|
||||
return finish(self, resize);
|
||||
ret = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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,
|
||||
@@ -144,11 +145,15 @@ impl<'a> PainterCtx<'a> {
|
||||
self.redraw(*rid);
|
||||
*old_desired = new_desired;
|
||||
if self.draw_started.contains(&id) {
|
||||
return finish(self, resize);
|
||||
ret = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ret {
|
||||
return finish(self, resize);
|
||||
}
|
||||
|
||||
let Some(active) = self.remove(id) else {
|
||||
return;
|
||||
};
|
||||
@@ -164,12 +169,6 @@ impl<'a> PainterCtx<'a> {
|
||||
finish(self, resize);
|
||||
}
|
||||
|
||||
pub fn draw(&mut self, id: Id) {
|
||||
self.draw_started.clear();
|
||||
self.layers.clear();
|
||||
self.draw_inner(0, id, UiRegion::FULL, None, MaskIdx::NONE, None);
|
||||
}
|
||||
|
||||
fn draw_inner(
|
||||
&mut self,
|
||||
layer: usize,
|
||||
@@ -193,7 +192,10 @@ impl<'a> PainterCtx<'a> {
|
||||
}
|
||||
let mut old_children = old_children.unwrap_or_default();
|
||||
let mut resize = ResizeRef::default();
|
||||
if let Some(active) = self.active.get_mut(&id) {
|
||||
if let Some(active) = self.active.get_mut(&id)
|
||||
&& !self.needs_redraw.contains(&id)
|
||||
{
|
||||
// check to see if we can skip drawing first
|
||||
if active.parent != parent {
|
||||
panic!("Cannot draw the same widget twice (2)");
|
||||
}
|
||||
@@ -205,11 +207,13 @@ impl<'a> PainterCtx<'a> {
|
||||
self.mov(id, from, region);
|
||||
return;
|
||||
}
|
||||
// if not, then maintain resize and track old children to remove unneeded
|
||||
let active = self.remove(id).unwrap();
|
||||
old_children = active.children;
|
||||
resize = active.resize;
|
||||
}
|
||||
|
||||
// draw widget
|
||||
self.draw_started.insert(id);
|
||||
|
||||
let mut painter = Painter {
|
||||
@@ -225,7 +229,6 @@ impl<'a> PainterCtx<'a> {
|
||||
children_height: Default::default(),
|
||||
};
|
||||
|
||||
// draw widgets
|
||||
painter.ctx.widgets.get_dyn_dynamic(id).draw(&mut painter);
|
||||
|
||||
let children_width = painter.children_width;
|
||||
@@ -243,6 +246,7 @@ impl<'a> PainterCtx<'a> {
|
||||
mask: painter.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)
|
||||
&& w.resize.x.is_none()
|
||||
@@ -257,12 +261,15 @@ impl<'a> PainterCtx<'a> {
|
||||
w.resize.y = Some((id, outer))
|
||||
}
|
||||
}
|
||||
|
||||
// remove old children that weren't kept
|
||||
for c in &old_children {
|
||||
if !instance.children.contains(c) {
|
||||
self.remove_rec(*c);
|
||||
}
|
||||
}
|
||||
|
||||
// update modules
|
||||
for m in self.modules.iter_mut() {
|
||||
m.on_draw(&instance);
|
||||
}
|
||||
@@ -317,6 +324,39 @@ impl<'a> PainterCtx<'a> {
|
||||
}
|
||||
}
|
||||
|
||||
impl PainterData {
|
||||
fn ctx(&mut self, needs_redraw: HashSet<Id>) -> PainterCtx<'_> {
|
||||
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,
|
||||
modules: &mut self.modules,
|
||||
masks: &mut self.masks,
|
||||
cache_width: Default::default(),
|
||||
cache_height: Default::default(),
|
||||
draw_started: Default::default(),
|
||||
needs_redraw,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn draw(&mut self, id: Id) {
|
||||
let mut ctx = self.ctx(Default::default());
|
||||
ctx.draw_started.clear();
|
||||
ctx.layers.clear();
|
||||
ctx.draw_inner(0, id, UiRegion::FULL, None, MaskIdx::NONE, None);
|
||||
}
|
||||
|
||||
pub fn redraw(&mut self, ids: HashSet<Id>) {
|
||||
let mut ctx = self.ctx(ids);
|
||||
while let Some(&id) = ctx.needs_redraw.iter().next() {
|
||||
ctx.redraw(id);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, 'c> Painter<'a, 'c> {
|
||||
fn primitive_at<P: Primitive>(&mut self, primitive: P, region: UiRegion) {
|
||||
let h = self.ctx.layers.write(
|
||||
@@ -440,6 +480,10 @@ impl<'a, 'c> Painter<'a, 'c> {
|
||||
pub fn label(&self) -> &str {
|
||||
&self.ctx.widgets.data(&self.id).unwrap().label
|
||||
}
|
||||
|
||||
pub fn id(&self) -> &Id {
|
||||
&self.id
|
||||
}
|
||||
}
|
||||
|
||||
pub struct SizeCtx<'a> {
|
||||
@@ -467,16 +511,22 @@ impl SizeCtx<'_> {
|
||||
}
|
||||
|
||||
fn width_inner(&mut self, id: Id) -> Len {
|
||||
if let Some(&(outer, len)) = self.cache_width.get(&id)
|
||||
&& outer == self.outer
|
||||
{
|
||||
self.checked_width.insert(id, (self.outer, len));
|
||||
return len;
|
||||
}
|
||||
// first check cache
|
||||
// TODO: is this needed? broken rn bc does not store children during upper size check,
|
||||
// so if something actually using check_* hits cache it fails to add them
|
||||
// if let Some(&(outer, len)) = self.cache_width.get(&id)
|
||||
// && outer == self.outer
|
||||
// {
|
||||
// self.checked_width.insert(id, (self.outer, len));
|
||||
// return len;
|
||||
// }
|
||||
// store self vars that need to be maintained
|
||||
let self_outer = self.outer;
|
||||
let self_id = self.id;
|
||||
// get size of input id
|
||||
self.id = id;
|
||||
let len = self.widgets.get_dyn_dynamic(id).desired_width(self);
|
||||
// restore vars & update cache + checked
|
||||
self.outer = self_outer;
|
||||
self.id = self_id;
|
||||
self.cache_width.insert(id, (self.outer, len));
|
||||
@@ -486,12 +536,13 @@ impl SizeCtx<'_> {
|
||||
|
||||
// TODO: should be refactored to share code w width_inner
|
||||
fn height_inner(&mut self, id: Id) -> Len {
|
||||
if let Some(&(outer, len)) = self.cache_height.get(&id)
|
||||
&& outer == self.outer
|
||||
{
|
||||
self.checked_height.insert(id, (self.outer, len));
|
||||
return len;
|
||||
}
|
||||
// if let Some(&(outer, len)) = self.cache_height.get(&id)
|
||||
// && outer == self.outer
|
||||
// {
|
||||
// prntln!("FAIL {id:?}");
|
||||
// self.checked_height.insert(id, (self.outer, len));
|
||||
// return len;
|
||||
// }
|
||||
let self_outer = self.outer;
|
||||
let self_id = self.id;
|
||||
self.id = id;
|
||||
|
||||
@@ -3,7 +3,7 @@ use image::DynamicImage;
|
||||
use crate::{
|
||||
core::{TextEdit, TextEditCtx},
|
||||
layout::{
|
||||
IdLike, PainterCtx, PainterData, PixelRegion, StaticWidgetId, TextureHandle, Vec2, Widget,
|
||||
IdLike, PainterData, PixelRegion, StaticWidgetId, TextureHandle, Vec2, Widget,
|
||||
WidgetId, WidgetInstance, WidgetLike,
|
||||
},
|
||||
util::{HashSet, Id},
|
||||
@@ -109,9 +109,8 @@ impl Ui {
|
||||
}
|
||||
// free before bc nothing should exist
|
||||
self.free();
|
||||
let mut ctx = PainterCtx::new(&mut self.data);
|
||||
if let Some(root) = &self.root {
|
||||
ctx.draw(root.id);
|
||||
self.data.draw(root.id);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -138,13 +137,7 @@ impl Ui {
|
||||
}
|
||||
|
||||
fn redraw_updates(&mut self) {
|
||||
// if self.updates.drain(..).next().is_some() {
|
||||
// self.redraw_all();
|
||||
// }
|
||||
let mut ctx = PainterCtx::new(&mut self.data);
|
||||
for id in self.updates.drain() {
|
||||
ctx.redraw(id);
|
||||
}
|
||||
self.data.redraw(std::mem::take(&mut self.updates));
|
||||
self.free();
|
||||
}
|
||||
|
||||
|
||||
@@ -56,7 +56,11 @@ impl Widgets {
|
||||
}
|
||||
|
||||
pub fn insert<W: Widget>(&mut self, id: Id, widget: W) {
|
||||
self.insert_any(id, Box::new(widget), std::any::type_name::<W>().to_string());
|
||||
let mut label = std::any::type_name::<W>().to_string();
|
||||
if let (Some(first), Some(last)) = (label.find(":"), label.rfind(":")) {
|
||||
label = label.split_at(first).0.to_string() + "::" + label.split_at(last + 1).1;
|
||||
}
|
||||
self.insert_any(id, Box::new(widget), label);
|
||||
}
|
||||
|
||||
pub fn data(&self, id: &Id) -> Option<&WidgetData> {
|
||||
|
||||
Reference in New Issue
Block a user