span builder
This commit is contained in:
@@ -1,3 +1,5 @@
|
|||||||
|
use std::marker::PhantomData;
|
||||||
|
|
||||||
use crate::prelude::*;
|
use crate::prelude::*;
|
||||||
|
|
||||||
pub struct Span {
|
pub struct Span {
|
||||||
@@ -15,13 +17,13 @@ impl Widget for Span {
|
|||||||
let mut axis = child_region.axis_mut(self.dir.axis);
|
let mut axis = child_region.axis_mut(self.dir.axis);
|
||||||
axis.top_left.set(start);
|
axis.top_left.set(start);
|
||||||
let len = painter.size(child).axis(self.dir.axis);
|
let len = painter.size(child).axis(self.dir.axis);
|
||||||
start.abs += len.abs;
|
|
||||||
start.rel += len.rel;
|
|
||||||
if len.rest > 0.0 {
|
if len.rest > 0.0 {
|
||||||
let offset = UiScalar::new(total.rel, total.abs);
|
let offset = UiScalar::new(total.rel, total.abs);
|
||||||
let rel_end = UiScalar::from_anchor(len.rest / total.rest);
|
let rel_end = UiScalar::from_anchor(len.rest / total.rest);
|
||||||
start = rel_end.within(start, (UiScalar::rel_max() + start) - offset);
|
start = rel_end.within(start, (UiScalar::rel_max() + start) - offset);
|
||||||
}
|
}
|
||||||
|
start.abs += len.abs;
|
||||||
|
start.rel += len.rel;
|
||||||
axis.bot_right.set(start);
|
axis.bot_right.set(start);
|
||||||
if self.dir.sign == Sign::Neg {
|
if self.dir.sign == Sign::Neg {
|
||||||
child_region.flip(self.dir.axis);
|
child_region.flip(self.dir.axis);
|
||||||
@@ -75,3 +77,40 @@ impl Span {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub struct SpanBuilder<const LEN: usize, Wa: WidgetArrLike<LEN, Tag>, Tag> {
|
||||||
|
pub children: Wa,
|
||||||
|
pub dir: Dir,
|
||||||
|
pub gap: f32,
|
||||||
|
_pd: PhantomData<Tag>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<const LEN: usize, Wa: WidgetArrLike<LEN, Tag>, Tag> FnOnce<(&mut Ui,)>
|
||||||
|
for SpanBuilder<LEN, Wa, Tag>
|
||||||
|
{
|
||||||
|
type Output = Span;
|
||||||
|
|
||||||
|
extern "rust-call" fn call_once(self, args: (&mut Ui,)) -> Self::Output {
|
||||||
|
Span {
|
||||||
|
children: self.children.ui(args.0).arr.to_vec(),
|
||||||
|
dir: self.dir,
|
||||||
|
gap: self.gap,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<const LEN: usize, Wa: WidgetArrLike<LEN, Tag>, Tag> SpanBuilder<LEN, Wa, Tag> {
|
||||||
|
pub fn new(children: Wa, dir: Dir) -> Self {
|
||||||
|
Self {
|
||||||
|
children,
|
||||||
|
dir,
|
||||||
|
gap: 0.0,
|
||||||
|
_pd: PhantomData,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn gap(mut self, gap: impl UiNum) -> Self {
|
||||||
|
self.gap = gap.to_f32();
|
||||||
|
self
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ pub trait CoreWidget<W, Tag> {
|
|||||||
fn offset(self, amt: impl Into<UiVec2>) -> impl WidgetFn<Offset>;
|
fn offset(self, amt: impl Into<UiVec2>) -> impl WidgetFn<Offset>;
|
||||||
fn scroll(self) -> impl WidgetIdFn<Offset>;
|
fn scroll(self) -> impl WidgetIdFn<Offset>;
|
||||||
fn masked(self) -> impl WidgetFn<Masked>;
|
fn masked(self) -> impl WidgetFn<Masked>;
|
||||||
|
fn background<T>(self, w: impl WidgetLike<T>) -> impl WidgetFn<Stack>;
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<W: WidgetLike<Tag>, Tag> CoreWidget<W::Widget, Tag> for W {
|
impl<W: WidgetLike<Tag>, Tag> CoreWidget<W::Widget, Tag> for W {
|
||||||
@@ -87,20 +88,24 @@ impl<W: WidgetLike<Tag>, Tag> CoreWidget<W::Widget, Tag> for W {
|
|||||||
inner: self.add(ui).any(),
|
inner: self.add(ui).any(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn background<T>(self, w: impl WidgetLike<T>) -> impl WidgetFn<Stack> {
|
||||||
|
move |ui| Stack {
|
||||||
|
children: vec![w.add(ui).any(), self.add(ui).any()],
|
||||||
|
size: StackSize::Child(1),
|
||||||
|
offset: 0,
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub trait CoreWidgetArr<const LEN: usize, Wa: WidgetArrLike<LEN, Tag>, Tag> {
|
pub trait CoreWidgetArr<const LEN: usize, Wa: WidgetArrLike<LEN, Tag>, Tag> {
|
||||||
fn span(self, dir: Dir) -> impl WidgetFn<Span>;
|
fn span(self, dir: Dir) -> SpanBuilder<LEN, Wa, Tag>;
|
||||||
fn stack(self) -> StackBuilder<LEN, Wa, Tag>;
|
fn stack(self) -> StackBuilder<LEN, Wa, Tag>;
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<const LEN: usize, Wa: WidgetArrLike<LEN, Tag>, Tag> CoreWidgetArr<LEN, Wa, Tag> for Wa {
|
impl<const LEN: usize, Wa: WidgetArrLike<LEN, Tag>, Tag> CoreWidgetArr<LEN, Wa, Tag> for Wa {
|
||||||
fn span(self, dir: Dir) -> impl WidgetFn<Span> {
|
fn span(self, dir: Dir) -> SpanBuilder<LEN, Wa, Tag> {
|
||||||
move |ui| Span {
|
SpanBuilder::new(self, dir)
|
||||||
children: self.ui(ui).arr.to_vec(),
|
|
||||||
dir,
|
|
||||||
gap: 0.0,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
fn stack(self) -> StackBuilder<LEN, Wa, Tag> {
|
fn stack(self) -> StackBuilder<LEN, Wa, Tag> {
|
||||||
StackBuilder::new(self)
|
StackBuilder::new(self)
|
||||||
|
|||||||
Reference in New Issue
Block a user