preparation
This commit is contained in:
@@ -1,13 +1,20 @@
|
||||
use crate::prelude::*;
|
||||
|
||||
pub struct Regioned {
|
||||
pub region: UiRegion,
|
||||
pub struct Padded {
|
||||
pub padding: Padding,
|
||||
pub inner: WidgetId,
|
||||
}
|
||||
|
||||
impl Widget for Regioned {
|
||||
impl Widget for Padded {
|
||||
fn draw(&mut self, painter: &mut Painter) {
|
||||
painter.draw_within(&self.inner, self.region);
|
||||
painter.draw_within(&self.inner, self.padding.region());
|
||||
}
|
||||
|
||||
fn get_size(&mut self, ctx: &mut SizeCtx) -> Vec2 {
|
||||
let mut size = ctx.size(&self.inner);
|
||||
size.x += self.padding.left + self.padding.right;
|
||||
size.y += self.padding.top + self.padding.bottom;
|
||||
size
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -15,7 +15,7 @@ impl Widget for Image {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn image(image: impl LoadableImage) -> WidgetFn!(Image) {
|
||||
pub fn image(image: impl LoadableImage) -> impl WidgetFn<Image> {
|
||||
let image = image.get_image().expect("Failed to load image");
|
||||
move |ui| Image {
|
||||
handle: ui.add_texture(image),
|
||||
|
||||
@@ -1,21 +1,25 @@
|
||||
use crate::prelude::*;
|
||||
|
||||
pub trait Sensable<W, Tag> {
|
||||
fn on(self, sense: Senses, f: impl SenseFn) -> WidgetIdFn!(W);
|
||||
fn on(self, sense: Senses, f: impl SenseFn) -> impl WidgetIdFn<W>;
|
||||
fn id_on(
|
||||
self,
|
||||
senses: Senses,
|
||||
f: impl FnMut(&WidgetId<W>, &mut Ui) + 'static + Clone,
|
||||
) -> WidgetIdFn!(W)
|
||||
f: impl FnMut(&WidgetId<W>, &mut Ui, SenseCtx) + 'static,
|
||||
) -> impl WidgetIdFn<W>
|
||||
where
|
||||
W: Widget;
|
||||
fn edit_on(self, senses: Senses, f: impl FnMut(&mut W) + 'static + Clone) -> WidgetIdFn!(W)
|
||||
fn edit_on(
|
||||
self,
|
||||
senses: Senses,
|
||||
f: impl FnMut(&mut W, SenseCtx) + 'static,
|
||||
) -> impl WidgetIdFn<W>
|
||||
where
|
||||
W: Widget;
|
||||
}
|
||||
|
||||
impl<W: WidgetLike<Tag>, Tag> Sensable<W::Widget, Tag> for W {
|
||||
fn on(self, senses: Senses, f: impl SenseFn) -> WidgetIdFn!(W::Widget) {
|
||||
fn on(self, senses: Senses, f: impl SenseFn) -> impl WidgetIdFn<W::Widget> {
|
||||
move |ui| {
|
||||
let id = self.add(ui);
|
||||
ui.add_sensor(
|
||||
@@ -31,24 +35,24 @@ impl<W: WidgetLike<Tag>, Tag> Sensable<W::Widget, Tag> for W {
|
||||
fn id_on(
|
||||
self,
|
||||
senses: Senses,
|
||||
mut f: impl FnMut(&WidgetId<W::Widget>, &mut Ui) + 'static + Clone,
|
||||
) -> WidgetIdFn!(W::Widget)
|
||||
mut f: impl FnMut(&WidgetId<W::Widget>, &mut Ui, SenseCtx) + 'static,
|
||||
) -> impl WidgetIdFn<W::Widget>
|
||||
where
|
||||
W::Widget: Widget,
|
||||
{
|
||||
self.with_id(move |ui, id| {
|
||||
let id2 = id.clone();
|
||||
ui.add(id.on(senses, move |ui| f(&id2, ui)))
|
||||
ui.add(id.on(senses, move |ui, pos| f(&id2, ui, pos)))
|
||||
})
|
||||
}
|
||||
fn edit_on(
|
||||
self,
|
||||
senses: Senses,
|
||||
mut f: impl FnMut(&mut W::Widget) + 'static + Clone,
|
||||
) -> WidgetIdFn!(W::Widget)
|
||||
mut f: impl FnMut(&mut W::Widget, SenseCtx) + 'static,
|
||||
) -> impl WidgetIdFn<W::Widget>
|
||||
where
|
||||
W::Widget: Widget,
|
||||
{
|
||||
self.id_on(senses, move |id, ui| f(&mut ui[id]))
|
||||
self.id_on(senses, move |id, ui, pos| f(&mut ui[id], pos))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,7 +5,10 @@ use crate::prelude::*;
|
||||
pub struct Text {
|
||||
pub content: String,
|
||||
pub attrs: TextAttrs,
|
||||
pub align: Align,
|
||||
buf: TextBuffer,
|
||||
cursor: Cursor,
|
||||
size: Vec2,
|
||||
}
|
||||
|
||||
impl Text {
|
||||
@@ -32,16 +35,34 @@ impl Text {
|
||||
content: content.into(),
|
||||
buf: TextBuffer::new_empty(Metrics::new(attrs.font_size, attrs.line_height)),
|
||||
attrs,
|
||||
align: Align::Center,
|
||||
cursor: Cursor::None,
|
||||
size: Vec2::ZERO,
|
||||
}
|
||||
}
|
||||
pub fn select(&mut self, pos: Vec2, size: Vec2) {
|
||||
let pos = pos - self.region().top_left.to_size(size);
|
||||
let Some(cursor) = self.buf.hit(pos.x, pos.y) else {
|
||||
return;
|
||||
};
|
||||
self.cursor = Cursor::Select {
|
||||
line: cursor.line,
|
||||
idx: cursor.index,
|
||||
};
|
||||
}
|
||||
|
||||
pub fn region(&self) -> UiRegion {
|
||||
UiRegion::from_size_align(self.size, self.align)
|
||||
}
|
||||
}
|
||||
|
||||
impl Widget for Text {
|
||||
fn draw(&mut self, painter: &mut Painter) {
|
||||
let (handle, offset) = painter.render_text(&mut self.buf, &self.content, &self.attrs);
|
||||
let (handle, offset) =
|
||||
painter.render_text(&mut self.buf, &self.content, &self.attrs, &self.cursor);
|
||||
let dims = handle.size();
|
||||
let size = offset.size(&handle);
|
||||
let mut region = Align::Center.pos().expand(size);
|
||||
self.size = offset.size(&handle);
|
||||
let mut region = self.region();
|
||||
region.top_left.offset += offset.top_left;
|
||||
region.bot_right.offset = region.top_left.offset + dims;
|
||||
painter.draw_texture_within(&handle, region);
|
||||
@@ -49,8 +70,7 @@ impl Widget for Text {
|
||||
|
||||
fn get_size(&mut self, ctx: &mut SizeCtx) -> Vec2 {
|
||||
let (handle, offset) =
|
||||
ctx.text
|
||||
.draw(&mut self.buf, &self.content, &self.attrs, ctx.textures);
|
||||
ctx.draw_text(&mut self.buf, &self.content, &self.attrs, &self.cursor);
|
||||
offset.size(&handle)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,41 +2,33 @@ use super::*;
|
||||
use crate::prelude::*;
|
||||
|
||||
pub trait CoreWidget<W, Tag> {
|
||||
fn pad(self, padding: impl Into<Padding>) -> WidgetFn!(Regioned);
|
||||
fn align(self, align: Align) -> WidgetFn!(Aligned);
|
||||
fn center(self) -> WidgetFn!(Aligned);
|
||||
fn region(self, region: UiRegion) -> WidgetFn!(Regioned);
|
||||
fn label(self, label: impl Into<String>) -> WidgetIdFn!(W);
|
||||
fn size(self, size: impl Into<Vec2>) -> WidgetFn!(Sized);
|
||||
fn pad(self, padding: impl Into<Padding>) -> impl WidgetFn<Padded>;
|
||||
fn align(self, align: Align) -> impl WidgetFn<Aligned>;
|
||||
fn center(self) -> impl WidgetFn<Aligned>;
|
||||
fn label(self, label: impl Into<String>) -> impl WidgetIdFn<W>;
|
||||
fn size(self, size: impl Into<Vec2>) -> impl WidgetFn<Sized>;
|
||||
}
|
||||
|
||||
impl<W: WidgetLike<Tag>, Tag> CoreWidget<W::Widget, Tag> for W {
|
||||
fn pad(self, padding: impl Into<Padding>) -> WidgetFn!(Regioned) {
|
||||
|ui| Regioned {
|
||||
region: padding.into().region(),
|
||||
fn pad(self, padding: impl Into<Padding>) -> impl WidgetFn<Padded> {
|
||||
|ui| Padded {
|
||||
padding: padding.into(),
|
||||
inner: self.add(ui).erase_type(),
|
||||
}
|
||||
}
|
||||
|
||||
fn align(self, align: Align) -> WidgetFn!(Aligned) {
|
||||
fn align(self, align: Align) -> impl WidgetFn<Aligned> {
|
||||
move |ui| Aligned {
|
||||
inner: self.add(ui).erase_type(),
|
||||
align,
|
||||
}
|
||||
}
|
||||
|
||||
fn center(self) -> WidgetFn!(Aligned) {
|
||||
fn center(self) -> impl WidgetFn<Aligned> {
|
||||
self.align(Align::Center)
|
||||
}
|
||||
|
||||
fn region(self, region: UiRegion) -> WidgetFn!(Regioned) {
|
||||
move |ui| Regioned {
|
||||
region,
|
||||
inner: self.add(ui).erase_type(),
|
||||
}
|
||||
}
|
||||
|
||||
fn label(self, label: impl Into<String>) -> WidgetIdFn!(W::Widget) {
|
||||
fn label(self, label: impl Into<String>) -> impl WidgetIdFn<W::Widget> {
|
||||
|ui| {
|
||||
let id = self.add(ui);
|
||||
ui.set_label(&id, label.into());
|
||||
@@ -44,7 +36,7 @@ impl<W: WidgetLike<Tag>, Tag> CoreWidget<W::Widget, Tag> for W {
|
||||
}
|
||||
}
|
||||
|
||||
fn size(self, size: impl Into<Vec2>) -> WidgetFn!(Sized) {
|
||||
fn size(self, size: impl Into<Vec2>) -> impl WidgetFn<Sized> {
|
||||
move |ui| Sized {
|
||||
inner: self.add(ui).erase_type(),
|
||||
size: size.into(),
|
||||
@@ -53,43 +45,25 @@ impl<W: WidgetLike<Tag>, Tag> CoreWidget<W::Widget, Tag> for W {
|
||||
}
|
||||
|
||||
pub trait CoreWidgetArr<const LEN: usize, Tag> {
|
||||
fn span(self, dir: Dir, lengths: impl IntoSpanLens<LEN>) -> WidgetFn!(Span);
|
||||
fn stack(self) -> WidgetFn!(Stack);
|
||||
fn span(self, dir: Dir, lengths: impl IntoSpanLens<LEN>) -> impl WidgetFn<Span>;
|
||||
fn stack(self) -> impl WidgetFn<Stack>;
|
||||
}
|
||||
|
||||
impl<const LEN: usize, Wa: WidgetArrLike<LEN, Tag>, Tag> CoreWidgetArr<LEN, Tag> for Wa {
|
||||
fn span(self, dir: Dir, lengths: impl IntoSpanLens<LEN>) -> WidgetFn!(Span) {
|
||||
fn span(self, dir: Dir, lengths: impl IntoSpanLens<LEN>) -> impl WidgetFn<Span> {
|
||||
let lengths = lengths.into_lens();
|
||||
move |ui| Span {
|
||||
children: self.ui(ui).arr.into_iter().zip(lengths).collect(),
|
||||
dir,
|
||||
}
|
||||
}
|
||||
fn stack(self) -> WidgetFn!(Stack) {
|
||||
fn stack(self) -> impl WidgetFn<Stack> {
|
||||
move |ui| Stack {
|
||||
children: self.ui(ui).arr.to_vec(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 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];
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user