pub struct RectAlign {
pub parent: Align2,
pub child: Align2,
}
Expand description
Position a child Rect
relative to a parent Rect
.
The corner from RectAlign::child
on the new rect will be aligned to
the corner from RectAlign::parent
on the original rect.
There are helper constants for the 12 common menu positions:
┌───────────┐ ┌────────┐ ┌─────────┐
│ TOP_START │ │ TOP │ │ TOP_END │
└───────────┘ └────────┘ └─────────┘
┌──────────┐ ┌────────────────────────────────────┐ ┌───────────┐
│LEFT_START│ │ │ │RIGHT_START│
└──────────┘ │ │ └───────────┘
┌──────────┐ │ │ ┌───────────┐
│ LEFT │ │ some_rect │ │ RIGHT │
└──────────┘ │ │ └───────────┘
┌──────────┐ │ │ ┌───────────┐
│ LEFT_END │ │ │ │ RIGHT_END │
└──────────┘ └────────────────────────────────────┘ └───────────┘
┌────────────┐ ┌──────┐ ┌──────────┐
│BOTTOM_START│ │BOTTOM│ │BOTTOM_END│
└────────────┘ └──────┘ └──────────┘
Fields§
§parent: Align2
The alignment in the parent (original) rect.
child: Align2
The alignment in the child (new) rect.
Implementations§
Source§impl RectAlign
impl RectAlign
Sourcepub const RIGHT_START: Self
pub const RIGHT_START: Self
Along the right edge, topmost.
Sourcepub const BOTTOM_END: Self
pub const BOTTOM_END: Self
Along the bottom edge, rightmost.
Sourcepub const BOTTOM_START: Self
pub const BOTTOM_START: Self
Along the bottom edge, leftmost.
Sourcepub const LEFT_START: Self
pub const LEFT_START: Self
Along the left edge, topmost.
Sourcepub const MENU_ALIGNS: [Self; 12]
pub const MENU_ALIGNS: [Self; 12]
The 12 most common menu positions as an array, for use with RectAlign::find_best_align
.
Sourcepub fn from_align2(align: Align2) -> Self
pub fn from_align2(align: Align2) -> Self
Sourcepub fn over_corner(align: Align2) -> Self
pub fn over_corner(align: Align2) -> Self
The center of the child rect will be aligned to a corner of the parent rect.
Sourcepub fn align_rect(&self, parent_rect: &Rect, size: Vec2, gap: f32) -> Rect
pub fn align_rect(&self, parent_rect: &Rect, size: Vec2, gap: f32) -> Rect
Calculate the child rect based on a size and some optional gap.
Sourcepub fn gap_vector(&self) -> Vec2
pub fn gap_vector(&self) -> Vec2
Returns a sign vector (-1, 0 or 1 in each direction) that can be used as an offset to the child rect, creating a gap between the rects while keeping the edges aligned.
Sourcepub fn anchor(&self, parent_rect: &Rect, gap: f32) -> Pos2
pub fn anchor(&self, parent_rect: &Rect, gap: f32) -> Pos2
Calculator the anchor point for the child rect, based on the parent rect and an optional gap.
Sourcepub fn symmetries(self) -> [Self; 3]
pub fn symmetries(self) -> [Self; 3]
Returns the 3 alternative RectAlign
s that are flipped in various ways, for use
with RectAlign::find_best_align
.
Sourcepub fn find_best_align(
values_to_try: impl Iterator<Item = Self>,
screen_rect: Rect,
parent_rect: Rect,
gap: f32,
expected_size: Vec2,
) -> Option<Self>
pub fn find_best_align( values_to_try: impl Iterator<Item = Self>, screen_rect: Rect, parent_rect: Rect, gap: f32, expected_size: Vec2, ) -> Option<Self>
Look for the first alternative RectAlign
that allows the child rect to fit
inside the screen_rect
.
If no alternative fits, the first is returned.
If no alternatives are given, None
is returned.
See also:
RectAlign::symmetries
to calculate alternativesRectAlign::MENU_ALIGNS
for the 12 common menu positions