use crate::{Align2, Pos2, Rect, Vec2};
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub struct RectAlign {
pub parent: Align2,
pub child: Align2,
}
impl Default for RectAlign {
fn default() -> Self {
Self::BOTTOM_START
}
}
impl RectAlign {
pub const TOP_START: Self = Self {
parent: Align2::LEFT_TOP,
child: Align2::LEFT_BOTTOM,
};
pub const TOP: Self = Self {
parent: Align2::CENTER_TOP,
child: Align2::CENTER_BOTTOM,
};
pub const TOP_END: Self = Self {
parent: Align2::RIGHT_TOP,
child: Align2::RIGHT_BOTTOM,
};
pub const RIGHT_START: Self = Self {
parent: Align2::RIGHT_TOP,
child: Align2::LEFT_TOP,
};
pub const RIGHT: Self = Self {
parent: Align2::RIGHT_CENTER,
child: Align2::LEFT_CENTER,
};
pub const RIGHT_END: Self = Self {
parent: Align2::RIGHT_BOTTOM,
child: Align2::LEFT_BOTTOM,
};
pub const BOTTOM_END: Self = Self {
parent: Align2::RIGHT_BOTTOM,
child: Align2::RIGHT_TOP,
};
pub const BOTTOM: Self = Self {
parent: Align2::CENTER_BOTTOM,
child: Align2::CENTER_TOP,
};
pub const BOTTOM_START: Self = Self {
parent: Align2::LEFT_BOTTOM,
child: Align2::LEFT_TOP,
};
pub const LEFT_END: Self = Self {
parent: Align2::LEFT_BOTTOM,
child: Align2::RIGHT_BOTTOM,
};
pub const LEFT: Self = Self {
parent: Align2::LEFT_CENTER,
child: Align2::RIGHT_CENTER,
};
pub const LEFT_START: Self = Self {
parent: Align2::LEFT_TOP,
child: Align2::RIGHT_TOP,
};
pub const MENU_ALIGNS: [Self; 12] = [
Self::BOTTOM_START,
Self::BOTTOM_END,
Self::TOP_START,
Self::TOP_END,
Self::RIGHT_END,
Self::RIGHT_START,
Self::LEFT_END,
Self::LEFT_START,
Self::TOP,
Self::RIGHT,
Self::BOTTOM,
Self::LEFT,
];
pub fn parent(&self) -> Align2 {
self.parent
}
pub fn child(&self) -> Align2 {
self.child
}
pub fn from_align2(align: Align2) -> Self {
Self {
parent: align,
child: align,
}
}
pub fn over_corner(align: Align2) -> Self {
Self {
parent: align,
child: Align2::CENTER_CENTER,
}
}
pub fn outside(align: Align2) -> Self {
Self {
parent: align,
child: align.flip(),
}
}
pub fn align_rect(&self, parent_rect: &Rect, size: Vec2, gap: f32) -> Rect {
let (pivot, anchor) = self.pivot_pos(parent_rect, gap);
pivot.anchor_size(anchor, size)
}
pub fn pivot_pos(&self, parent_rect: &Rect, gap: f32) -> (Align2, Pos2) {
(self.child(), self.anchor(parent_rect, gap))
}
pub fn gap_vector(&self) -> Vec2 {
let mut gap = -self.child.to_sign();
match *self {
Self::TOP_START | Self::TOP_END | Self::BOTTOM_START | Self::BOTTOM_END => {
gap.x = 0.0;
}
Self::LEFT_START | Self::LEFT_END | Self::RIGHT_START | Self::RIGHT_END => {
gap.y = 0.0;
}
_ => {}
}
gap
}
pub fn anchor(&self, parent_rect: &Rect, gap: f32) -> Pos2 {
let pos = self.parent.pos_in_rect(parent_rect);
let offset = self.gap_vector() * gap;
pos + offset
}
pub fn flip_x(self) -> Self {
Self {
parent: self.parent.flip_x(),
child: self.child.flip_x(),
}
}
pub fn flip_y(self) -> Self {
Self {
parent: self.parent.flip_y(),
child: self.child.flip_y(),
}
}
pub fn flip(self) -> Self {
Self {
parent: self.parent.flip(),
child: self.child.flip(),
}
}
pub fn symmetries(self) -> [Self; 3] {
[self.flip_x(), self.flip_y(), self.flip()]
}
pub fn find_best_align(
values_to_try: impl Iterator<Item = Self>,
content_rect: Rect,
parent_rect: Rect,
gap: f32,
expected_size: Vec2,
) -> Option<Self> {
let mut first_choice = None;
for align in values_to_try {
first_choice = first_choice.or(Some(align));
let suggested_popup_rect = align.align_rect(&parent_rect, expected_size, gap);
if content_rect.contains_rect(suggested_popup_rect) {
return Some(align);
}
}
first_choice
}
}