add offset / scrolling + clipboard support
This commit is contained in:
@@ -11,23 +11,20 @@ pub struct UiVec2 {
|
||||
}
|
||||
|
||||
impl UiVec2 {
|
||||
pub const ZERO: Self = Self {
|
||||
rel: Vec2::ZERO,
|
||||
abs: Vec2::ZERO,
|
||||
};
|
||||
|
||||
/// expands this position into a sized region centered at self
|
||||
pub fn expand(&self, size: impl Into<Vec2>) -> UiRegion {
|
||||
let size = size.into();
|
||||
UiRegion {
|
||||
top_left: self.shifted(-size / 2.0),
|
||||
bot_right: self.shifted(size / 2.0),
|
||||
top_left: self.offset(-size / 2.0),
|
||||
bot_right: self.offset(size / 2.0),
|
||||
}
|
||||
}
|
||||
|
||||
pub const fn anchor(anchor: Vec2) -> Self {
|
||||
Self::rel(anchor)
|
||||
}
|
||||
|
||||
pub const fn offset(offset: Vec2) -> Self {
|
||||
Self::abs(offset)
|
||||
}
|
||||
|
||||
pub const fn abs(abs: Vec2) -> Self {
|
||||
Self {
|
||||
rel: Vec2::ZERO,
|
||||
@@ -42,11 +39,12 @@ impl UiVec2 {
|
||||
}
|
||||
}
|
||||
|
||||
pub const fn shift(&mut self, offset: impl const Into<Vec2>) {
|
||||
self.abs += offset.into();
|
||||
pub const fn shift(&mut self, offset: impl const Into<UiVec2>) {
|
||||
let offset = offset.into();
|
||||
*self += offset;
|
||||
}
|
||||
|
||||
pub const fn shifted(mut self, offset: Vec2) -> Self {
|
||||
pub const fn offset(mut self, offset: impl const Into<UiVec2>) -> Self {
|
||||
self.shift(offset);
|
||||
self
|
||||
}
|
||||
@@ -118,7 +116,7 @@ impl_op!(UiVec2 Sub sub; rel abs);
|
||||
|
||||
impl const From<Align> for UiVec2 {
|
||||
fn from(align: Align) -> Self {
|
||||
Self::anchor(align.anchor())
|
||||
Self::rel(align.rel())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -128,6 +126,12 @@ impl Align {
|
||||
}
|
||||
}
|
||||
|
||||
impl const From<Vec2> for UiVec2 {
|
||||
fn from(abs: Vec2) -> Self {
|
||||
Self::abs(abs)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, Default)]
|
||||
pub struct UiScalar {
|
||||
pub rel: f32,
|
||||
@@ -198,10 +202,10 @@ impl UiRegion {
|
||||
bot_right: Align::BotRight.into(),
|
||||
}
|
||||
}
|
||||
pub fn anchor(anchor: Vec2) -> Self {
|
||||
pub fn rel(anchor: Vec2) -> Self {
|
||||
Self {
|
||||
top_left: UiVec2::anchor(anchor),
|
||||
bot_right: UiVec2::anchor(anchor),
|
||||
top_left: UiVec2::rel(anchor),
|
||||
bot_right: UiVec2::rel(anchor),
|
||||
}
|
||||
}
|
||||
pub fn within(&self, parent: &Self) -> Self {
|
||||
@@ -229,13 +233,13 @@ impl UiRegion {
|
||||
std::mem::swap(&mut self.top_left, &mut self.bot_right);
|
||||
}
|
||||
|
||||
pub fn shift(&mut self, offset: impl Into<Vec2>) {
|
||||
pub fn shift(&mut self, offset: impl Into<UiVec2>) {
|
||||
let offset = offset.into();
|
||||
self.top_left.shift(offset);
|
||||
self.bot_right.shift(offset);
|
||||
}
|
||||
|
||||
pub fn shifted(mut self, offset: impl Into<Vec2>) -> Self {
|
||||
pub fn offset(mut self, offset: impl Into<UiVec2>) -> Self {
|
||||
self.shift(offset);
|
||||
self
|
||||
}
|
||||
@@ -265,9 +269,9 @@ impl UiRegion {
|
||||
|
||||
pub fn from_size_align(size: Vec2, align: Align) -> Self {
|
||||
let mut top_left = UiVec2::from(align);
|
||||
top_left.abs -= size * align.anchor();
|
||||
top_left.abs -= size * align.rel();
|
||||
let mut bot_right = UiVec2::from(align);
|
||||
bot_right.abs += size * (Vec2::ONE - align.anchor());
|
||||
bot_right.abs += size * (Vec2::ONE - align.rel());
|
||||
Self {
|
||||
top_left,
|
||||
bot_right,
|
||||
@@ -276,11 +280,11 @@ impl UiRegion {
|
||||
|
||||
pub fn from_ui_size_align(size: UiVec2, align: Align) -> Self {
|
||||
let mut top_left = UiVec2::from(align);
|
||||
top_left.abs -= size.abs * align.anchor();
|
||||
top_left.rel -= size.rel * align.anchor();
|
||||
top_left.abs -= size.abs * align.rel();
|
||||
top_left.rel -= size.rel * align.rel();
|
||||
let mut bot_right = UiVec2::from(align);
|
||||
bot_right.abs += size.abs * (Vec2::ONE - align.anchor());
|
||||
bot_right.rel += size.rel * (Vec2::ONE - align.anchor());
|
||||
bot_right.abs += size.abs * (Vec2::ONE - align.rel());
|
||||
bot_right.rel += size.rel * (Vec2::ONE - align.rel());
|
||||
Self {
|
||||
top_left,
|
||||
bot_right,
|
||||
|
||||
Reference in New Issue
Block a user