sized spans!
This commit is contained in:
@@ -10,7 +10,7 @@ impl Widget for Image {
|
|||||||
painter.draw_texture(&self.handle);
|
painter.draw_texture(&self.handle);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn size(&mut self, _: SizeCtx) -> Vec2 {
|
fn size(&mut self, _: &mut SizeCtx) -> Vec2 {
|
||||||
self.handle.size()
|
self.handle.size()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ pub struct Span {
|
|||||||
|
|
||||||
impl Widget for Span {
|
impl Widget for Span {
|
||||||
fn draw(&mut self, painter: &mut Painter) {
|
fn draw(&mut self, painter: &mut Painter) {
|
||||||
let total = self.setup(painter);
|
let total = self.setup(&mut painter.size_ctx());
|
||||||
let mut start = UIScalar::min();
|
let mut start = UIScalar::min();
|
||||||
for (child, length) in &self.children {
|
for (child, length) in &self.children {
|
||||||
let mut child_region = UiRegion::full();
|
let mut child_region = UiRegion::full();
|
||||||
@@ -44,6 +44,17 @@ impl Widget for Span {
|
|||||||
painter.draw_within(child, child_region);
|
painter.draw_within(child, child_region);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn size(&mut self, ctx: &mut SizeCtx) -> Vec2 {
|
||||||
|
let total = self.setup(ctx);
|
||||||
|
let axis = self.dir.axis;
|
||||||
|
let dir_len = if total.ratio != 0.0 {
|
||||||
|
ctx.size.axis(axis)
|
||||||
|
} else {
|
||||||
|
total.fixed + total.relative * ctx.screen_size.axis(axis)
|
||||||
|
};
|
||||||
|
Vec2::from_axis(axis, dir_len, total.max_sized)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
@@ -51,6 +62,7 @@ pub struct SpanLenSums {
|
|||||||
pub fixed: f32,
|
pub fixed: f32,
|
||||||
pub ratio: f32,
|
pub ratio: f32,
|
||||||
pub relative: f32,
|
pub relative: f32,
|
||||||
|
pub max_sized: f32,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Span {
|
impl Span {
|
||||||
@@ -61,7 +73,7 @@ impl Span {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn setup(&mut self, painter: &mut Painter) -> SpanLenSums {
|
fn setup(&mut self, ctx: &mut SizeCtx) -> SpanLenSums {
|
||||||
self.children
|
self.children
|
||||||
.iter_mut()
|
.iter_mut()
|
||||||
.fold(SpanLenSums::default(), |mut s, (id, l)| {
|
.fold(SpanLenSums::default(), |mut s, (id, l)| {
|
||||||
@@ -70,8 +82,9 @@ impl Span {
|
|||||||
SpanLen::Ratio(v) => s.ratio += *v,
|
SpanLen::Ratio(v) => s.ratio += *v,
|
||||||
SpanLen::Relative(v) => s.relative += *v,
|
SpanLen::Relative(v) => s.relative += *v,
|
||||||
SpanLen::Sized(v) => {
|
SpanLen::Sized(v) => {
|
||||||
let size = painter.size(id);
|
let size = ctx.size(id);
|
||||||
let len = size.axis(self.dir.axis);
|
let len = size.axis(self.dir.axis);
|
||||||
|
s.max_sized = s.max_sized.max(size.axis(!self.dir.axis));
|
||||||
*v = size;
|
*v = size;
|
||||||
s.fixed += len;
|
s.fixed += len;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -41,7 +41,7 @@ impl Widget for Text {
|
|||||||
// reuse TextureHandle
|
// reuse TextureHandle
|
||||||
}
|
}
|
||||||
|
|
||||||
fn size(&mut self, ctx: SizeCtx) -> Vec2 {
|
fn size(&mut self, ctx: &mut SizeCtx) -> Vec2 {
|
||||||
let (handle, offset) =
|
let (handle, offset) =
|
||||||
ctx.text
|
ctx.text
|
||||||
.draw(&mut self.buf, &self.content, &self.attrs, ctx.textures);
|
.draw(&mut self.buf, &self.content, &self.attrs, ctx.textures);
|
||||||
|
|||||||
@@ -75,4 +75,17 @@ impl Vec2 {
|
|||||||
Axis::Y => &mut self.y,
|
Axis::Y => &mut self.y,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub const fn from_axis(axis: Axis, aligned: f32, ortho: f32) -> Self {
|
||||||
|
Self {
|
||||||
|
x: match axis {
|
||||||
|
Axis::X => aligned,
|
||||||
|
Axis::Y => ortho,
|
||||||
|
},
|
||||||
|
y: match axis {
|
||||||
|
Axis::Y => aligned,
|
||||||
|
Axis::X => ortho,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -151,11 +151,19 @@ impl<'a> Painter<'a> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn size<W>(&mut self, id: &WidgetId<W>) -> Vec2 {
|
pub fn size<W>(&mut self, id: &WidgetId<W>) -> Vec2 {
|
||||||
self.widgets.get_dyn_dynamic(&id.id).size(SizeCtx {
|
self.widgets
|
||||||
|
.get_dyn_dynamic(&id.id)
|
||||||
|
.size(&mut self.size_ctx())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn size_ctx(&mut self) -> SizeCtx {
|
||||||
|
SizeCtx {
|
||||||
|
screen_size: self.screen_size,
|
||||||
size: self.region().in_size(self.screen_size),
|
size: self.region().in_size(self.screen_size),
|
||||||
text: self.text,
|
text: self.text,
|
||||||
textures: self.textures,
|
textures: self.textures,
|
||||||
})
|
widgets: self.widgets,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn redraw(&mut self, id: &Id) {
|
pub(crate) fn redraw(&mut self, id: &Id) {
|
||||||
|
|||||||
@@ -1,18 +1,26 @@
|
|||||||
use crate::layout::{Painter, TextData, Textures, Ui, Vec2, WidgetId, WidgetIdFnRet};
|
use crate::layout::{Painter, TextData, Textures, Ui, Vec2, WidgetId, WidgetIdFnRet, Widgets};
|
||||||
|
|
||||||
use std::{any::Any, marker::PhantomData};
|
use std::{any::Any, marker::PhantomData};
|
||||||
|
|
||||||
pub trait Widget: Any {
|
pub trait Widget: Any {
|
||||||
fn draw(&mut self, painter: &mut Painter);
|
fn draw(&mut self, painter: &mut Painter);
|
||||||
fn size(&mut self, ctx: SizeCtx) -> Vec2 {
|
fn size(&mut self, ctx: &mut SizeCtx) -> Vec2 {
|
||||||
ctx.size
|
ctx.size
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct SizeCtx<'a> {
|
pub struct SizeCtx<'a> {
|
||||||
|
pub screen_size: Vec2,
|
||||||
pub size: Vec2,
|
pub size: Vec2,
|
||||||
pub text: &'a mut TextData,
|
pub text: &'a mut TextData,
|
||||||
pub textures: &'a mut Textures,
|
pub textures: &'a mut Textures,
|
||||||
|
pub(super) widgets: &'a Widgets,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl SizeCtx<'_> {
|
||||||
|
pub fn size<W>(&mut self, id: &WidgetId<W>) -> Vec2 {
|
||||||
|
self.widgets.get_dyn_dynamic(&id.id).size(self)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct WidgetTag;
|
pub struct WidgetTag;
|
||||||
|
|||||||
@@ -96,13 +96,13 @@ impl Client {
|
|||||||
text("okkk\nokkkkkk!").size(30),
|
text("okkk\nokkkkkk!").size(30),
|
||||||
text("hmm").size(30),
|
text("hmm").size(30),
|
||||||
text("a").size(30),
|
text("a").size(30),
|
||||||
text("pretty cool right?").size(30),
|
|
||||||
(
|
(
|
||||||
text("'").size(30).family(Family::Monospace),
|
text("'").size(30).family(Family::Monospace),
|
||||||
text("'").size(30).family(Family::Monospace),
|
text("'").size(30).family(Family::Monospace),
|
||||||
text(":gamer mode").size(30).family(Family::Monospace),
|
text(":gamer mode").size(30).family(Family::Monospace),
|
||||||
)
|
)
|
||||||
.span(Dir::RIGHT, [sized(); _]),
|
.span(Dir::RIGHT, [sized(); _]),
|
||||||
|
text("pretty cool right?").size(30),
|
||||||
)
|
)
|
||||||
.span(Dir::DOWN, [sized(); _]),
|
.span(Dir::DOWN, [sized(); _]),
|
||||||
);
|
);
|
||||||
|
|||||||
Reference in New Issue
Block a user