alignment!!!
This commit is contained in:
@@ -1,20 +1,21 @@
|
||||
#[const_trait]
|
||||
pub trait UiNum {
|
||||
fn to_f32(self) -> f32;
|
||||
}
|
||||
|
||||
impl UiNum for f32 {
|
||||
impl const UiNum for f32 {
|
||||
fn to_f32(self) -> f32 {
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
impl UiNum for u32 {
|
||||
impl const UiNum for u32 {
|
||||
fn to_f32(self) -> f32 {
|
||||
self as f32
|
||||
}
|
||||
}
|
||||
|
||||
impl UiNum for i32 {
|
||||
impl const UiNum for i32 {
|
||||
fn to_f32(self) -> f32 {
|
||||
self as f32
|
||||
}
|
||||
|
||||
@@ -2,25 +2,6 @@ use std::ops::Not;
|
||||
|
||||
use crate::layout::{Vec2, vec2};
|
||||
|
||||
#[derive(Clone, Copy, Debug)]
|
||||
pub enum Corner {
|
||||
TopLeft,
|
||||
TopRight,
|
||||
BotLeft,
|
||||
BotRight,
|
||||
}
|
||||
|
||||
impl Corner {
|
||||
pub const fn anchor(&self) -> Vec2 {
|
||||
match self {
|
||||
Corner::TopLeft => vec2(0.0, 0.0),
|
||||
Corner::TopRight => vec2(1.0, 0.0),
|
||||
Corner::BotLeft => vec2(0.0, 1.0),
|
||||
Corner::BotRight => vec2(1.0, 1.0),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, Eq, PartialEq)]
|
||||
pub enum Axis {
|
||||
X,
|
||||
@@ -89,3 +70,33 @@ impl Vec2 {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, PartialEq, Eq)]
|
||||
pub enum Align {
|
||||
TopLeft,
|
||||
Top,
|
||||
TopRight,
|
||||
Left,
|
||||
Center,
|
||||
Right,
|
||||
BotLeft,
|
||||
Bot,
|
||||
BotRight,
|
||||
}
|
||||
|
||||
impl Align {
|
||||
pub const fn anchor(&self) -> Vec2 {
|
||||
match self {
|
||||
Self::TopLeft => vec2(0.0, 0.0),
|
||||
Self::Top => vec2(0.5, 0.0),
|
||||
Self::TopRight => vec2(1.0, 0.0),
|
||||
Self::Left => vec2(0.0, 0.5),
|
||||
Self::Center => vec2(0.5, 0.5),
|
||||
Self::Right => vec2(1.0, 0.5),
|
||||
Self::BotLeft => vec2(0.0, 1.0),
|
||||
Self::Bot => vec2(0.5, 1.0),
|
||||
Self::BotRight => vec2(1.0, 1.0),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -156,7 +156,7 @@ impl<'a> Painter<'a> {
|
||||
.size(&mut self.size_ctx())
|
||||
}
|
||||
|
||||
pub fn size_ctx(&mut self) -> SizeCtx {
|
||||
pub fn size_ctx(&mut self) -> SizeCtx<'_> {
|
||||
SizeCtx {
|
||||
size: self.region().in_size(self.screen_size),
|
||||
text: self.text,
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
use crate::{
|
||||
layout::{Axis, Corner, Vec2, vec2},
|
||||
layout::{Align, Axis, Vec2},
|
||||
util::{F32Util, impl_op},
|
||||
};
|
||||
|
||||
@@ -20,10 +20,6 @@ impl UiPos {
|
||||
}
|
||||
}
|
||||
|
||||
pub const fn center() -> Self {
|
||||
Self::anchor(vec2(0.5, 0.5))
|
||||
}
|
||||
|
||||
pub const fn anchor(anchor: Vec2) -> Self {
|
||||
Self {
|
||||
anchor,
|
||||
@@ -38,10 +34,6 @@ impl UiPos {
|
||||
}
|
||||
}
|
||||
|
||||
pub const fn corner(corner: Corner) -> Self {
|
||||
Self::anchor(corner.anchor())
|
||||
}
|
||||
|
||||
pub const fn shift(&mut self, offset: impl const Into<Vec2>) {
|
||||
self.offset += offset.into();
|
||||
}
|
||||
@@ -90,6 +82,18 @@ impl UiPos {
|
||||
}
|
||||
}
|
||||
|
||||
impl const From<Align> for UiPos {
|
||||
fn from(align: Align) -> Self {
|
||||
Self::anchor(align.anchor())
|
||||
}
|
||||
}
|
||||
|
||||
impl Align {
|
||||
pub fn pos(self) -> UiPos {
|
||||
UiPos::from(self)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug)]
|
||||
pub struct UIScalar {
|
||||
pub anchor: f32,
|
||||
@@ -137,8 +141,8 @@ pub struct UiRegion {
|
||||
impl UiRegion {
|
||||
pub const fn full() -> Self {
|
||||
Self {
|
||||
top_left: UiPos::corner(Corner::TopLeft),
|
||||
bot_right: UiPos::corner(Corner::BotRight),
|
||||
top_left: Align::TopLeft.into(),
|
||||
bot_right: Align::BotRight.into(),
|
||||
}
|
||||
}
|
||||
pub fn anchor(anchor: Vec2) -> Self {
|
||||
@@ -153,9 +157,6 @@ impl UiRegion {
|
||||
bot_right: self.bot_right.within(parent),
|
||||
}
|
||||
}
|
||||
pub fn select(&mut self, inner: &Self) {
|
||||
*self = inner.within(self);
|
||||
}
|
||||
pub fn axis_mut(&mut self, axis: Axis) -> UIRegionAxisView<'_> {
|
||||
UIRegionAxisView {
|
||||
top_left: self.top_left.axis_mut(axis),
|
||||
@@ -188,12 +189,27 @@ impl UiRegion {
|
||||
}
|
||||
|
||||
pub fn center(&self) -> UiPos {
|
||||
UiPos::center().within(self)
|
||||
Align::Center.pos().within(self)
|
||||
}
|
||||
|
||||
pub fn in_size(&self, size: Vec2) -> Vec2 {
|
||||
self.bot_right.to_size(size) - self.top_left.to_size(size)
|
||||
}
|
||||
|
||||
pub fn select_aligned(&self, size: Vec2, align: Align) -> Self {
|
||||
Self::from_size_align(size, align).within(self)
|
||||
}
|
||||
|
||||
pub fn from_size_align(size: Vec2, align: Align) -> Self {
|
||||
let mut top_left = UiPos::from(align);
|
||||
top_left.offset -= size * align.anchor();
|
||||
let mut bot_right = UiPos::from(align);
|
||||
bot_right.offset += size * (Vec2::ONE - align.anchor());
|
||||
Self {
|
||||
top_left,
|
||||
bot_right,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
|
||||
@@ -54,9 +54,12 @@ impl Vec2 {
|
||||
}
|
||||
}
|
||||
|
||||
impl const From<f32> for Vec2 {
|
||||
fn from(v: f32) -> Self {
|
||||
Self { x: v, y: v }
|
||||
impl<T: const UiNum + Copy> const From<T> for Vec2 {
|
||||
fn from(v: T) -> Self {
|
||||
Self {
|
||||
x: v.to_f32(),
|
||||
y: v.to_f32(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user