alignment!!!
This commit is contained in:
17
src/core/align.rs
Normal file
17
src/core/align.rs
Normal file
@@ -0,0 +1,17 @@
|
||||
use crate::prelude::*;
|
||||
|
||||
pub struct Aligned {
|
||||
pub inner: WidgetId,
|
||||
pub align: Align,
|
||||
}
|
||||
|
||||
impl Widget for Aligned {
|
||||
fn draw(&mut self, painter: &mut Painter) {
|
||||
let region = UiRegion::from_size_align(painter.size(&self.inner), self.align);
|
||||
painter.draw_within(&self.inner, region);
|
||||
}
|
||||
|
||||
fn size(&mut self, ctx: &mut SizeCtx) -> Vec2 {
|
||||
ctx.size(&self.inner)
|
||||
}
|
||||
}
|
||||
@@ -1,16 +1,20 @@
|
||||
mod align;
|
||||
mod frame;
|
||||
mod image;
|
||||
mod rect;
|
||||
mod sense;
|
||||
mod sized;
|
||||
mod span;
|
||||
mod stack;
|
||||
mod text;
|
||||
mod trait_fns;
|
||||
|
||||
pub use align::*;
|
||||
pub use frame::*;
|
||||
pub use image::*;
|
||||
pub use rect::*;
|
||||
pub use sense::*;
|
||||
pub use sized::*;
|
||||
pub use span::*;
|
||||
pub use stack::*;
|
||||
pub use text::*;
|
||||
|
||||
16
src/core/sized.rs
Normal file
16
src/core/sized.rs
Normal file
@@ -0,0 +1,16 @@
|
||||
use crate::prelude::*;
|
||||
|
||||
pub struct Sized {
|
||||
pub inner: WidgetId,
|
||||
pub size: Vec2,
|
||||
}
|
||||
|
||||
impl Widget for Sized {
|
||||
fn draw(&mut self, painter: &mut Painter) {
|
||||
painter.draw(&self.inner);
|
||||
}
|
||||
|
||||
fn size(&mut self, _: &mut SizeCtx) -> Vec2 {
|
||||
self.size
|
||||
}
|
||||
}
|
||||
@@ -33,9 +33,7 @@ impl Widget for Span {
|
||||
let offset = size.axis(self.dir.axis);
|
||||
start.offset += offset;
|
||||
*axis.bot_right.offset = start.offset;
|
||||
child_region.bot_right.anchor = child_region.top_left.anchor;
|
||||
let opposite = !self.dir.axis;
|
||||
*child_region.bot_right.offset.axis_mut(opposite) += size.axis(opposite);
|
||||
*axis.bot_right.anchor = *axis.top_left.anchor;
|
||||
}
|
||||
}
|
||||
if self.dir.sign == Sign::Neg {
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
use super::*;
|
||||
use crate::layout::{
|
||||
Dir, UiPos, UiRegion, Vec2, WidgetArrLike, WidgetFnRet, WidgetIdFnRet, WidgetLike,
|
||||
};
|
||||
use crate::prelude::*;
|
||||
|
||||
pub trait CoreWidget<W, Tag> {
|
||||
fn pad(self, padding: impl Into<Padding>) -> WidgetFnRet!(Regioned);
|
||||
fn center(self, size: impl Into<Vec2>) -> WidgetFnRet!(Regioned);
|
||||
fn align(self, align: Align) -> WidgetFnRet!(Aligned);
|
||||
fn center(self) -> WidgetFnRet!(Aligned);
|
||||
fn region(self, region: UiRegion) -> WidgetFnRet!(Regioned);
|
||||
fn label(self, label: impl Into<String>) -> WidgetIdFnRet!(W);
|
||||
fn sized(self, size: impl Into<Vec2>) -> WidgetFnRet!(Sized);
|
||||
}
|
||||
|
||||
impl<W: WidgetLike<Tag>, Tag> CoreWidget<W::Widget, Tag> for W {
|
||||
@@ -18,13 +18,17 @@ impl<W: WidgetLike<Tag>, Tag> CoreWidget<W::Widget, Tag> for W {
|
||||
}
|
||||
}
|
||||
|
||||
fn center(self, size: impl Into<Vec2>) -> WidgetFnRet!(Regioned) {
|
||||
|ui| Regioned {
|
||||
region: UiPos::center().expand(size.into()),
|
||||
fn align(self, align: Align) -> WidgetFnRet!(Aligned) {
|
||||
move |ui| Aligned {
|
||||
inner: self.add(ui).erase_type(),
|
||||
align,
|
||||
}
|
||||
}
|
||||
|
||||
fn center(self) -> WidgetFnRet!(Aligned) {
|
||||
self.align(Align::Center)
|
||||
}
|
||||
|
||||
fn region(self, region: UiRegion) -> WidgetFnRet!(Regioned) {
|
||||
move |ui| Regioned {
|
||||
region,
|
||||
@@ -39,19 +43,26 @@ impl<W: WidgetLike<Tag>, Tag> CoreWidget<W::Widget, Tag> for W {
|
||||
id
|
||||
}
|
||||
}
|
||||
|
||||
fn sized(self, size: impl Into<Vec2>) -> WidgetFnRet!(Sized) {
|
||||
move |ui| Sized {
|
||||
inner: self.add(ui).erase_type(),
|
||||
size: size.into(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub trait CoreWidgetArr<const LEN: usize, Tag> {
|
||||
fn span(self, dir: Dir, lengths: [impl Into<SpanLen>; LEN]) -> WidgetFnRet!(Span);
|
||||
fn span(self, dir: Dir, lengths: impl IntoSpanLens<LEN>) -> WidgetFnRet!(Span);
|
||||
fn stack(self) -> WidgetFnRet!(Stack);
|
||||
}
|
||||
|
||||
impl<const LEN: usize, Wa: WidgetArrLike<LEN, Tag>, Tag> CoreWidgetArr<LEN, Tag> for Wa {
|
||||
fn span(self, dir: Dir, lengths: [impl Into<SpanLen>; LEN]) -> WidgetFnRet!(Span) {
|
||||
let lengths = lengths.map(Into::into);
|
||||
fn span(self, dir: Dir, lengths: impl IntoSpanLens<LEN>) -> WidgetFnRet!(Span) {
|
||||
let lengths = lengths.into_lens();
|
||||
move |ui| Span {
|
||||
dir,
|
||||
children: self.ui(ui).arr.into_iter().zip(lengths).collect(),
|
||||
dir,
|
||||
}
|
||||
}
|
||||
fn stack(self) -> WidgetFnRet!(Stack) {
|
||||
@@ -60,3 +71,37 @@ impl<const LEN: usize, Wa: WidgetArrLike<LEN, Tag>, Tag> CoreWidgetArr<LEN, Tag>
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// pub struct SpanBuilder<const LEN: usize, Tag, Wa: WidgetArrLike<LEN, Tag>> {
|
||||
// children: Wa,
|
||||
// dir: Dir,
|
||||
// align: Align,
|
||||
// }
|
||||
//
|
||||
// impl WidgetLike<FnTag> for SpanBuilder {
|
||||
// type Widget = Span;
|
||||
//
|
||||
// fn add(self, ui: &mut Ui) -> WidgetId<Self::Widget> {
|
||||
// ui.add_widget(Span {
|
||||
// children: self.children,
|
||||
// dir: self.dir,
|
||||
// align: self.align,
|
||||
// })
|
||||
// }
|
||||
// }
|
||||
|
||||
pub trait IntoSpanLens<const LEN: usize> {
|
||||
fn into_lens(self) -> [SpanLen; LEN];
|
||||
}
|
||||
|
||||
impl<const LEN: usize, T: Into<SpanLen>> IntoSpanLens<LEN> for [T; LEN] {
|
||||
fn into_lens(self) -> [SpanLen; LEN] {
|
||||
self.map(Into::into)
|
||||
}
|
||||
}
|
||||
|
||||
impl<const LEN: usize> IntoSpanLens<LEN> for SpanLen {
|
||||
fn into_lens(self) -> [SpanLen; LEN] {
|
||||
[self; LEN]
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user