Rename a length's abs component to px
`dp` is coming, and then `abs` says which of the two it is not. The component has always been a pixel count, so the name that admits it is the one that leaves room for a second unit beside it. Mechanical: the field on `Len` and `UiScalar`, their constructors, `to_abs`/`get_abs`, the matching WGSL struct member and the locals composing it. Field order and types are unchanged, so the `Pod` layout the shader reads is the same bytes. `f32::abs` is untouched. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
1 parent
3f7cd8251b
commit
7c50a3e51b
22 files changed
+120
-124
No files matched your search
@@ -144,10 +144,10 @@ impl UiScalar {
|
||||
pub const fn align(&self, align: AxisAlign) -> UiSpan {
|
||||
let rel = align.rel();
|
||||
let mut start = UiScalar::rel(rel);
|
||||
start.abs -= self.abs * rel;
|
||||
start.px -= self.px * rel;
|
||||
start.rel -= self.rel * rel;
|
||||
let mut end = UiScalar::rel(rel);
|
||||
end.abs += self.abs * (1.0 - rel);
|
||||
end.px += self.px * (1.0 - rel);
|
||||
end.rel += self.rel * (1.0 - rel);
|
||||
UiSpan { start, end }
|
||||
}
|
||||
|
||||
+20
-20
@@ -9,14 +9,14 @@ pub struct Size {
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq)]
|
||||
pub struct Len {
|
||||
pub abs: f32,
|
||||
pub px: f32,
|
||||
pub rel: f32,
|
||||
pub rest: f32,
|
||||
}
|
||||
|
||||
impl<N: UiNum> From<N> for Len {
|
||||
fn from(value: N) -> Self {
|
||||
Len::abs(value.to_f32())
|
||||
Len::px(value.to_f32())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -46,10 +46,10 @@ impl Size {
|
||||
y: Len::REST,
|
||||
};
|
||||
|
||||
pub fn abs(v: Vec2) -> Self {
|
||||
pub fn px(v: Vec2) -> Self {
|
||||
Self {
|
||||
x: Len::abs(v.x),
|
||||
y: Len::abs(v.y),
|
||||
x: Len::px(v.x),
|
||||
y: Len::px(v.y),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -97,13 +97,13 @@ impl Size {
|
||||
|
||||
impl Len {
|
||||
pub const ZERO: Self = Self {
|
||||
abs: 0.0,
|
||||
px: 0.0,
|
||||
rel: 0.0,
|
||||
rest: 0.0,
|
||||
};
|
||||
|
||||
pub const REST: Self = Self {
|
||||
abs: 0.0,
|
||||
px: 0.0,
|
||||
rel: 0.0,
|
||||
rest: 1.0,
|
||||
};
|
||||
@@ -111,27 +111,27 @@ impl Len {
|
||||
pub fn apply_rest(&self) -> UiScalar {
|
||||
UiScalar {
|
||||
rel: self.rel + if self.rest > 0.0 { 1.0 } else { 0.0 },
|
||||
abs: self.abs,
|
||||
px: self.px,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn abs(abs: impl UiNum) -> Self {
|
||||
pub fn px(px: impl UiNum) -> Self {
|
||||
Self {
|
||||
abs: abs.to_f32(),
|
||||
px: px.to_f32(),
|
||||
rel: 0.0,
|
||||
rest: 0.0,
|
||||
}
|
||||
}
|
||||
pub fn rel(rel: impl UiNum) -> Self {
|
||||
Self {
|
||||
abs: 0.0,
|
||||
px: 0.0,
|
||||
rel: rel.to_f32(),
|
||||
rest: 0.0,
|
||||
}
|
||||
}
|
||||
pub fn rest(ratio: impl UiNum) -> Self {
|
||||
Self {
|
||||
abs: 0.0,
|
||||
px: 0.0,
|
||||
rel: 0.0,
|
||||
rest: ratio.to_f32(),
|
||||
}
|
||||
@@ -141,31 +141,31 @@ impl Len {
|
||||
pub mod len_fns {
|
||||
use super::*;
|
||||
|
||||
pub fn abs(abs: impl UiNum) -> Len {
|
||||
pub fn px(px: impl UiNum) -> Len {
|
||||
Len {
|
||||
abs: abs.to_f32(),
|
||||
px: px.to_f32(),
|
||||
rel: 0.0,
|
||||
rest: 0.0,
|
||||
}
|
||||
}
|
||||
pub fn rel(rel: impl UiNum) -> Len {
|
||||
Len {
|
||||
abs: 0.0,
|
||||
px: 0.0,
|
||||
rel: rel.to_f32(),
|
||||
rest: 0.0,
|
||||
}
|
||||
}
|
||||
pub fn rest(ratio: impl UiNum) -> Len {
|
||||
Len {
|
||||
abs: 0.0,
|
||||
px: 0.0,
|
||||
rel: 0.0,
|
||||
rest: ratio.to_f32(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl_op!(Len Add add; abs rel rest);
|
||||
impl_op!(Len Sub sub; abs rel rest);
|
||||
impl_op!(Len Add add; px rel rest);
|
||||
impl_op!(Len Sub sub; px rel rest);
|
||||
|
||||
impl_op!(Size Add add; x y);
|
||||
impl_op!(Size Sub sub; x y);
|
||||
@@ -184,8 +184,8 @@ impl std::fmt::Display for Size {
|
||||
|
||||
impl std::fmt::Display for Len {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
if self.abs != 0.0 {
|
||||
write!(f, "{} abs;", self.abs)?;
|
||||
if self.px != 0.0 {
|
||||
write!(f, "{} px;", self.px)?;
|
||||
}
|
||||
if self.rel != 0.0 {
|
||||
write!(f, "{} rel;", self.rel)?;
|
||||
|
||||
+38
-38
@@ -23,11 +23,11 @@ impl UiVec2 {
|
||||
Self { x, y }
|
||||
}
|
||||
|
||||
pub const fn abs(abs: impl const Into<Vec2>) -> Self {
|
||||
let abs = abs.into();
|
||||
pub const fn px(px: impl const Into<Vec2>) -> Self {
|
||||
let px = px.into();
|
||||
Self {
|
||||
x: UiScalar::abs(abs.x),
|
||||
y: UiScalar::abs(abs.y),
|
||||
x: UiScalar::px(px.x),
|
||||
y: UiScalar::px(px.y),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -70,10 +70,10 @@ impl UiVec2 {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn to_abs(&self, rel: Vec2) -> Vec2 {
|
||||
pub fn to_px(&self, rel: Vec2) -> Vec2 {
|
||||
Vec2 {
|
||||
x: self.x.to_abs(rel.x),
|
||||
y: self.y.to_abs(rel.y),
|
||||
x: self.x.to_px(rel.x),
|
||||
y: self.y.to_px(rel.y),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -92,8 +92,8 @@ impl UiVec2 {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_abs(&self) -> Vec2 {
|
||||
(self.x.abs, self.y.abs).into()
|
||||
pub fn get_px(&self) -> Vec2 {
|
||||
(self.x.px, self.y.px).into()
|
||||
}
|
||||
|
||||
pub fn get_rel(&self) -> Vec2 {
|
||||
@@ -102,15 +102,15 @@ impl UiVec2 {
|
||||
|
||||
pub fn abs_mut(&mut self) -> Vec2View<'_> {
|
||||
Vec2View {
|
||||
x: &mut self.x.abs,
|
||||
y: &mut self.y.abs,
|
||||
x: &mut self.x.px,
|
||||
y: &mut self.y.px,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Display for UiVec2 {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
write!(f, "rel{};abs{}", self.get_rel(), self.get_abs())
|
||||
write!(f, "rel{};px{}", self.get_rel(), self.get_px())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -118,8 +118,8 @@ impl_op!(UiVec2 Add add; x y);
|
||||
impl_op!(UiVec2 Sub sub; x y);
|
||||
|
||||
const impl From<Vec2> for UiVec2 {
|
||||
fn from(abs: Vec2) -> Self {
|
||||
Self::abs(abs)
|
||||
fn from(px: Vec2) -> Self {
|
||||
Self::px(px)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -127,8 +127,8 @@ const impl<T: const UiNum, U: const UiNum> From<(T, U)> for UiVec2
|
||||
where
|
||||
(T, U): const Destruct,
|
||||
{
|
||||
fn from(abs: (T, U)) -> Self {
|
||||
Self::abs(abs)
|
||||
fn from(px: (T, U)) -> Self {
|
||||
Self::px(px)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -136,34 +136,34 @@ where
|
||||
#[derive(Debug, Copy, Clone, PartialEq, bytemuck::Pod, Default, bytemuck::Zeroable)]
|
||||
pub struct UiScalar {
|
||||
pub rel: f32,
|
||||
pub abs: f32,
|
||||
pub px: f32,
|
||||
}
|
||||
|
||||
impl Eq for UiScalar {}
|
||||
impl Hash for UiScalar {
|
||||
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
|
||||
state.write_u32(self.rel.to_bits());
|
||||
state.write_u32(self.abs.to_bits());
|
||||
state.write_u32(self.px.to_bits());
|
||||
}
|
||||
}
|
||||
|
||||
impl_op!(UiScalar Add add; rel abs);
|
||||
impl_op!(UiScalar Sub sub; rel abs);
|
||||
impl_op!(UiScalar Add add; rel px);
|
||||
impl_op!(UiScalar Sub sub; rel px);
|
||||
|
||||
impl UiScalar {
|
||||
pub const ZERO: Self = Self { rel: 0.0, abs: 0.0 };
|
||||
pub const FULL: Self = Self { rel: 1.0, abs: 0.0 };
|
||||
pub const ZERO: Self = Self { rel: 0.0, px: 0.0 };
|
||||
pub const FULL: Self = Self { rel: 1.0, px: 0.0 };
|
||||
|
||||
pub const fn new(rel: f32, abs: f32) -> Self {
|
||||
Self { rel, abs }
|
||||
pub const fn new(rel: f32, px: f32) -> Self {
|
||||
Self { rel, px }
|
||||
}
|
||||
|
||||
pub const fn rel(rel: f32) -> Self {
|
||||
Self { rel, abs: 0.0 }
|
||||
Self { rel, px: 0.0 }
|
||||
}
|
||||
|
||||
pub const fn abs(abs: f32) -> Self {
|
||||
Self { rel: 0.0, abs }
|
||||
pub const fn px(px: f32) -> Self {
|
||||
Self { rel: 0.0, px }
|
||||
}
|
||||
|
||||
pub const fn rel_min() -> Self {
|
||||
@@ -177,28 +177,28 @@ impl UiScalar {
|
||||
pub const fn max(&self, other: Self) -> Self {
|
||||
Self {
|
||||
rel: self.rel.max(other.rel),
|
||||
abs: self.abs.max(other.abs),
|
||||
px: self.px.max(other.px),
|
||||
}
|
||||
}
|
||||
|
||||
pub const fn min(&self, other: Self) -> Self {
|
||||
Self {
|
||||
rel: self.rel.min(other.rel),
|
||||
abs: self.abs.min(other.abs),
|
||||
px: self.px.min(other.px),
|
||||
}
|
||||
}
|
||||
|
||||
pub const fn offset(mut self, amt: f32) -> Self {
|
||||
self.abs += amt;
|
||||
self.px += amt;
|
||||
self
|
||||
}
|
||||
|
||||
pub const fn within(&self, span: &UiSpan) -> Self {
|
||||
let anchor = self.rel.lerp(span.start.rel, span.end.rel);
|
||||
let offset = self.abs + self.rel.lerp(span.start.abs, span.end.abs);
|
||||
let offset = self.px + self.rel.lerp(span.start.px, span.end.px);
|
||||
Self {
|
||||
rel: anchor,
|
||||
abs: offset,
|
||||
px: offset,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -215,15 +215,15 @@ impl UiScalar {
|
||||
|
||||
pub const fn flip(&mut self) {
|
||||
self.rel = 1.0 - self.rel;
|
||||
self.abs = -self.abs;
|
||||
self.px = -self.px;
|
||||
}
|
||||
|
||||
pub const fn to(&self, end: Self) -> UiSpan {
|
||||
UiSpan { start: *self, end }
|
||||
}
|
||||
|
||||
pub const fn to_abs(&self, rel: f32) -> f32 {
|
||||
self.rel * rel + self.abs
|
||||
pub const fn to_px(&self, rel: f32) -> f32 {
|
||||
self.rel * rel + self.px
|
||||
}
|
||||
}
|
||||
|
||||
@@ -255,7 +255,7 @@ impl UiSpan {
|
||||
self.start.flip();
|
||||
self.end.flip();
|
||||
std::mem::swap(&mut self.start.rel, &mut self.end.rel);
|
||||
std::mem::swap(&mut self.start.abs, &mut self.end.abs);
|
||||
std::mem::swap(&mut self.start.px, &mut self.end.px);
|
||||
}
|
||||
|
||||
pub const fn shift(&mut self, offset: UiScalar) {
|
||||
@@ -338,8 +338,8 @@ impl UiRegion {
|
||||
|
||||
pub fn to_px(&self, size: Vec2) -> PixelRegion {
|
||||
PixelRegion {
|
||||
top_left: self.top_left().get_rel() * size + self.top_left().get_abs(),
|
||||
bot_right: self.bot_right().get_rel() * size + self.bot_right().get_abs(),
|
||||
top_left: self.top_left().get_rel() * size + self.top_left().get_px(),
|
||||
bot_right: self.bot_right().get_rel() * size + self.bot_right().get_px(),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -40,7 +40,7 @@ const CHAIN_LIMIT: u32 = 64u;
|
||||
fn scalar_within(s: UiScalar, p: UiSpan) -> UiScalar {
|
||||
return UiScalar(
|
||||
mix(p.start.rel, p.end.rel, s.rel),
|
||||
s.abs + mix(p.start.abs, p.end.abs, s.rel),
|
||||
s.px + mix(p.start.px, p.end.px, s.rel),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -69,7 +69,7 @@ struct UiSpan {
|
||||
|
||||
struct UiScalar {
|
||||
rel: f32,
|
||||
abs: f32,
|
||||
px: f32,
|
||||
}
|
||||
|
||||
struct InstanceInput {
|
||||
@@ -104,12 +104,12 @@ fn vs_main(
|
||||
);
|
||||
let r = resolve_move(in.move_idx, local);
|
||||
let top_left_rel = vec2(r.x.start.rel, r.y.start.rel);
|
||||
let top_left_abs = vec2(r.x.start.abs, r.y.start.abs);
|
||||
let top_left_px = vec2(r.x.start.px, r.y.start.px);
|
||||
let bot_right_rel = vec2(r.x.end.rel, r.y.end.rel);
|
||||
let bot_right_abs = vec2(r.x.end.abs, r.y.end.abs);
|
||||
let bot_right_px = vec2(r.x.end.px, r.y.end.px);
|
||||
|
||||
let top_left = floor(top_left_rel * window.dim) + floor(top_left_abs);
|
||||
let bot_right = floor(bot_right_rel * window.dim) + floor(bot_right_abs);
|
||||
let top_left = floor(top_left_rel * window.dim) + floor(top_left_px);
|
||||
let bot_right = floor(bot_right_rel * window.dim) + floor(bot_right_px);
|
||||
let size = bot_right - top_left;
|
||||
|
||||
let uv = vec2<f32>(
|
||||
@@ -136,12 +136,12 @@ fn masked(in: VertexOutput, color: vec4<f32>) -> vec4<f32> {
|
||||
// clips content that moves inside it.
|
||||
let m = resolve_move(mask.move_idx, Region(mask.x, mask.y));
|
||||
let tl = vec2(m.x.start.rel, m.y.start.rel);
|
||||
let tl_abs = vec2(m.x.start.abs, m.y.start.abs);
|
||||
let tl_px = vec2(m.x.start.px, m.y.start.px);
|
||||
let br = vec2(m.x.end.rel, m.y.end.rel);
|
||||
let br_abs = vec2(m.x.end.abs, m.y.end.abs);
|
||||
let br_px = vec2(m.x.end.px, m.y.end.px);
|
||||
|
||||
let top_left = floor(tl * window.dim) + floor(tl_abs);
|
||||
let bot_right = floor(br * window.dim) + floor(br_abs);
|
||||
let top_left = floor(tl * window.dim) + floor(tl_px);
|
||||
let bot_right = floor(br * window.dim) + floor(br_px);
|
||||
let pos = in.clip_position.xy;
|
||||
if pos.x < top_left.x || pos.x > bot_right.x || pos.y < top_left.y || pos.y > bot_right.y {
|
||||
return color * 0.0;
|
||||
|
||||
@@ -34,6 +34,6 @@ fn distance_from_rect(pixel_pos: vec2<f32>, rect_center: vec2<f32>, rect_corner:
|
||||
// vec from center to pixel
|
||||
let p = pixel_pos - rect_center;
|
||||
// vec from inner rect corner to pixel
|
||||
let q = abs(p) - (rect_corner - radius);
|
||||
let q = px(p) - (rect_corner - radius);
|
||||
return length(max(q, vec2(0.0))) - radius;
|
||||
}
|
||||
@@ -260,9 +260,9 @@ impl<'a> Painter<'a> {
|
||||
let mut region = origin;
|
||||
region.x.end = region.x.start;
|
||||
region.y.end = region.y.start;
|
||||
let mut region = region.offset(UiVec2::abs(glyph.offset));
|
||||
region.x.end = region.x.start + UiScalar::abs(glyph.entry.width as f32);
|
||||
region.y.end = region.y.start + UiScalar::abs(glyph.entry.height as f32);
|
||||
let mut region = region.offset(UiVec2::px(glyph.offset));
|
||||
region.x.end = region.x.start + UiScalar::px(glyph.entry.width as f32);
|
||||
region.y.end = region.y.start + UiScalar::px(glyph.entry.height as f32);
|
||||
self.write(
|
||||
kind,
|
||||
GlyphPrimitive {
|
||||
@@ -306,7 +306,7 @@ impl<'a> Painter<'a> {
|
||||
self.reads_output = [true; 2];
|
||||
self.size_box_inputs = [true; 2];
|
||||
let region = self.state.moves.resolve(self.move_idx, self.region);
|
||||
region.size().to_abs(self.state.output_size)
|
||||
region.size().to_px(self.state.output_size)
|
||||
}
|
||||
|
||||
/// One axis of this widget's box in pixels. Prefer this to
|
||||
@@ -318,7 +318,7 @@ impl<'a> Painter<'a> {
|
||||
region
|
||||
.size()
|
||||
.axis(axis)
|
||||
.to_abs(self.state.output_size.axis(axis))
|
||||
.to_px(self.state.output_size.axis(axis))
|
||||
}
|
||||
|
||||
pub fn text_data(&mut self) -> &mut TextData {
|
||||
|
||||
@@ -325,7 +325,7 @@ impl UiRenderState {
|
||||
self.moves
|
||||
.resolve(slot, region)
|
||||
.size()
|
||||
.to_abs(self.output_size)
|
||||
.to_px(self.output_size)
|
||||
}
|
||||
|
||||
/// A clean widget's retained size, when the offered pixel axes which
|
||||
|
||||
Reference in new issue
Block a user