ing prefix gives off bad vibes
This commit is contained in:
63
src/core/position/pad.rs
Normal file
63
src/core/position/pad.rs
Normal file
@@ -0,0 +1,63 @@
|
||||
use crate::prelude::*;
|
||||
|
||||
pub struct Padded {
|
||||
pub padding: Padding,
|
||||
pub inner: WidgetId,
|
||||
}
|
||||
|
||||
impl Widget for Padded {
|
||||
fn draw(&mut self, painter: &mut Painter) {
|
||||
painter.widget_within(&self.inner, self.padding.region());
|
||||
}
|
||||
|
||||
fn desired_size(&mut self, ctx: &mut SizeCtx) -> UiVec2 {
|
||||
let mut size = ctx.size(&self.inner);
|
||||
// TODO: this is currently a hack
|
||||
// the correct solution is that the position is not linear
|
||||
// becauase if you have 0.5 rel + 100 abs, it's linear
|
||||
// until you fill up parent rel (in abs units) and then 1.0 rel (piecewise linear)
|
||||
// so I guess yet another detection system is needed
|
||||
// to determine when abs translated to rel + rel > 1.0
|
||||
// hopefully find a way to get working on gpu so no cpu changing needed
|
||||
// eg by sending size to calculate above and then doing... something?
|
||||
if size.rel.x != 1.0 {
|
||||
size.abs.x += self.padding.left + self.padding.right;
|
||||
}
|
||||
if size.rel.y != 1.0 {
|
||||
size.abs.y += self.padding.top + self.padding.bottom;
|
||||
}
|
||||
size
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Padding {
|
||||
pub left: f32,
|
||||
pub right: f32,
|
||||
pub top: f32,
|
||||
pub bottom: f32,
|
||||
}
|
||||
|
||||
impl Padding {
|
||||
pub fn uniform(amt: f32) -> Self {
|
||||
Self {
|
||||
left: amt,
|
||||
right: amt,
|
||||
top: amt,
|
||||
bottom: amt,
|
||||
}
|
||||
}
|
||||
pub fn region(&self) -> UiRegion {
|
||||
let mut region = UiRegion::full();
|
||||
region.top_left.abs.x += self.left;
|
||||
region.top_left.abs.y += self.top;
|
||||
region.bot_right.abs.x -= self.right;
|
||||
region.bot_right.abs.y -= self.bottom;
|
||||
region
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: UiNum> From<T> for Padding {
|
||||
fn from(amt: T) -> Self {
|
||||
Self::uniform(amt.to_f32())
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user