Add Rect left/right/top/bottom accessors

This commit is contained in:
Emil Ernerfeldt 2020-04-22 19:38:38 +02:00
parent 2a4828670e
commit 700c93b8e3
7 changed files with 53 additions and 41 deletions

View File

@ -135,8 +135,8 @@ fn paint_icon(region: &mut Region, state: &State, interact: &InteractInfo) {
// Draw a minus:
region.add_paint_cmd(PaintCmd::Line {
points: vec![
pos2(small_icon_rect.min().x, small_icon_rect.center().y),
pos2(small_icon_rect.max().x, small_icon_rect.center().y),
pos2(small_icon_rect.left(), small_icon_rect.center().y),
pos2(small_icon_rect.right(), small_icon_rect.center().y),
],
color: stroke_color,
width: stroke_width,
@ -146,8 +146,8 @@ fn paint_icon(region: &mut Region, state: &State, interact: &InteractInfo) {
// Draw it as a plus:
region.add_paint_cmd(PaintCmd::Line {
points: vec![
pos2(small_icon_rect.center().x, small_icon_rect.min().y),
pos2(small_icon_rect.center().x, small_icon_rect.max().y),
pos2(small_icon_rect.center().x, small_icon_rect.top()),
pos2(small_icon_rect.center().x, small_icon_rect.bottom()),
],
color: stroke_color,
width: stroke_width,

View File

@ -81,14 +81,14 @@ impl Default for Align {
pub fn align_rect(rect: Rect, align: (Align, Align)) -> Rect {
let x = match align.0 {
Align::Min => rect.min().x,
Align::Center => rect.min().x - 0.5 * rect.size().x,
Align::Max => rect.min().x - rect.size().x,
Align::Min => rect.left(),
Align::Center => rect.left() - 0.5 * rect.size().x,
Align::Max => rect.left() - rect.size().x,
};
let y = match align.1 {
Align::Min => rect.min().y,
Align::Center => rect.min().y - 0.5 * rect.size().y,
Align::Max => rect.min().y - rect.size().y,
Align::Min => rect.top(),
Align::Center => rect.top() - 0.5 * rect.size().y,
Align::Max => rect.top() - rect.size().y,
};
Rect::from_min_size(pos2(x, y), rect.size())
}

View File

@ -399,30 +399,41 @@ impl Rect {
}
// Convenience functions (assumes origin is towards left top):
pub fn left(&self) -> f32 {
self.min.x
}
pub fn right(&self) -> f32 {
self.max.x
}
pub fn top(&self) -> f32 {
self.min.y
}
pub fn bottom(&self) -> f32 {
self.max.y
}
pub fn left_top(&self) -> Pos2 {
pos2(self.min().x, self.min().y)
pos2(self.left(), self.top())
}
pub fn center_top(&self) -> Pos2 {
pos2(self.center().x, self.min().y)
pos2(self.center().x, self.top())
}
pub fn right_top(&self) -> Pos2 {
pos2(self.max().x, self.min().y)
pos2(self.right(), self.top())
}
pub fn left_center(&self) -> Pos2 {
pos2(self.min().x, self.center().y)
pos2(self.left(), self.center().y)
}
pub fn right_center(&self) -> Pos2 {
pos2(self.max().x, self.center().y)
pos2(self.right(), self.center().y)
}
pub fn left_bottom(&self) -> Pos2 {
pos2(self.min().x, self.max().y)
pos2(self.left(), self.bottom())
}
pub fn center_bottom(&self) -> Pos2 {
pos2(self.center().x, self.max().y)
pos2(self.center().x, self.bottom())
}
pub fn right_bottom(&self) -> Pos2 {
pos2(self.max().x, self.max().y)
pos2(self.right(), self.bottom())
}
}

View File

@ -130,11 +130,11 @@ impl Region {
}
pub fn available_width(&self) -> f32 {
self.desired_rect.max().x - self.cursor.x
self.desired_rect.right() - self.cursor.x
}
pub fn available_height(&self) -> f32 {
self.desired_rect.max().y - self.cursor.y
self.desired_rect.bottom() - self.cursor.y
}
/// This how much more space we can take up without overflowing our parent.
@ -265,7 +265,7 @@ impl Region {
.map(|col_idx| {
let pos = self.cursor + vec2((col_idx as f32) * (column_width + padding), 0.0);
let child_rect =
Rect::from_min_max(pos, pos2(pos.x + column_width, self.desired_rect.max().y));
Rect::from_min_max(pos, pos2(pos.x + column_width, self.desired_rect.bottom()));
Region {
id: self.make_child_region_id(&("column", col_idx)),

View File

@ -72,28 +72,28 @@ impl ScrollArea {
if show_scroll {
let corner_radius = scroll_bar_width / 2.0;
let left = inner_rect.max().x;
let right = outer_rect.max().x;
let left = inner_rect.right();
let right = outer_rect.right();
let outer_scroll_rect = Rect::from_min_max(
pos2(left, inner_rect.min().y),
pos2(right, inner_rect.max().y),
pos2(left, inner_rect.top()),
pos2(right, inner_rect.bottom()),
);
let scroll_handle_min_y = remap_clamp(
state.offset.y,
0.0,
content_size.y,
inner_rect.min().y,
inner_rect.max().y,
inner_rect.top(),
inner_rect.bottom(),
);
let scroll_handle_max_y = remap_clamp(
state.offset.y + inner_rect.height(),
0.0,
content_size.y,
inner_rect.min().y,
inner_rect.max().y,
inner_rect.top(),
inner_rect.bottom(),
);
let scroll_handle_rect = Rect::from_min_max(
@ -111,13 +111,13 @@ impl ScrollArea {
if handle_interact.active {
// state.offset.y = remap_clamp(
// ctx.input.mouse_pos.y,
// inner_rect.min().y,
// inner_rect.max().y,
// inner_rect.top(),
// inner_rect.bottom(),
// 0.0,
// content_size.y,
// );
if let Some(mouse_pos) = ctx.input.mouse_pos {
if inner_rect.min().y <= mouse_pos.y && mouse_pos.y <= inner_rect.max().y {
if inner_rect.top() <= mouse_pos.y && mouse_pos.y <= inner_rect.bottom() {
state.offset.y +=
ctx.input.mouse_move.y * content_size.y / inner_rect.height();
}

View File

@ -134,7 +134,7 @@ impl Style {
pub fn icon_rectangles(&self, rect: &Rect) -> (Rect, Rect) {
let box_side = 16.0;
let big_icon_rect = Rect::from_center_size(
pos2(rect.min().x + 4.0 + box_side * 0.5, rect.center().y),
pos2(rect.left() + 4.0 + box_side * 0.5, rect.center().y),
vec2(box_side, box_side),
);

View File

@ -159,9 +159,9 @@ impl<'a> Widget for Checkbox<'a> {
if *self.checked {
region.add_paint_cmd(PaintCmd::Line {
points: vec![
pos2(small_icon_rect.min().x, small_icon_rect.center().y),
pos2(small_icon_rect.center().x, small_icon_rect.max().y),
pos2(small_icon_rect.max().x, small_icon_rect.min().y),
pos2(small_icon_rect.left(), small_icon_rect.center().y),
pos2(small_icon_rect.center().x, small_icon_rect.bottom()),
pos2(small_icon_rect.right(), small_icon_rect.top()),
],
color: stroke_color,
width: region.style().line_width,
@ -479,13 +479,14 @@ impl Separator {
impl Widget for Separator {
fn add_to(self, region: &mut Region) -> GuiResponse {
let available_space = region.available_space();
let extra = self.extra;
let (points, interact) = match region.direction() {
Direction::Horizontal => {
let interact = region.reserve_space(vec2(self.min_length, available_space.y), None);
(
vec![
pos2(interact.rect.center().x, interact.rect.min().y - self.extra),
pos2(interact.rect.center().x, interact.rect.max().y + self.extra),
pos2(interact.rect.center().x, interact.rect.top() - extra),
pos2(interact.rect.center().x, interact.rect.bottom() + extra),
],
interact,
)
@ -494,8 +495,8 @@ impl Widget for Separator {
let interact = region.reserve_space(vec2(available_space.x, self.min_length), None);
(
vec![
pos2(interact.rect.min().x - self.extra, interact.rect.center().y),
pos2(interact.rect.max().x + self.extra, interact.rect.center().y),
pos2(interact.rect.left() - extra, interact.rect.center().y),
pos2(interact.rect.right() + extra, interact.rect.center().y),
],
interact,
)