From 700c93b8e342d5bf9006f274b9920bdc80d0fc01 Mon Sep 17 00:00:00 2001 From: Emil Ernerfeldt Date: Wed, 22 Apr 2020 19:38:38 +0200 Subject: [PATCH] Add Rect left/right/top/bottom accessors --- emigui/src/collapsing_header.rs | 8 ++++---- emigui/src/layout.rs | 12 ++++++------ emigui/src/math.rs | 29 ++++++++++++++++++++--------- emigui/src/region.rs | 6 +++--- emigui/src/scroll_area.rs | 22 +++++++++++----------- emigui/src/style.rs | 2 +- emigui/src/widgets.rs | 15 ++++++++------- 7 files changed, 53 insertions(+), 41 deletions(-) diff --git a/emigui/src/collapsing_header.rs b/emigui/src/collapsing_header.rs index e3f5077d..f501c6e2 100644 --- a/emigui/src/collapsing_header.rs +++ b/emigui/src/collapsing_header.rs @@ -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, diff --git a/emigui/src/layout.rs b/emigui/src/layout.rs index 643ac1b7..1140754a 100644 --- a/emigui/src/layout.rs +++ b/emigui/src/layout.rs @@ -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()) } diff --git a/emigui/src/math.rs b/emigui/src/math.rs index b5b9ebae..903be399 100644 --- a/emigui/src/math.rs +++ b/emigui/src/math.rs @@ -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()) } } diff --git a/emigui/src/region.rs b/emigui/src/region.rs index 95649de7..86abf7b3 100644 --- a/emigui/src/region.rs +++ b/emigui/src/region.rs @@ -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)), diff --git a/emigui/src/scroll_area.rs b/emigui/src/scroll_area.rs index c1edb16a..206c7891 100644 --- a/emigui/src/scroll_area.rs +++ b/emigui/src/scroll_area.rs @@ -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(); } diff --git a/emigui/src/style.rs b/emigui/src/style.rs index c9b03335..62b34894 100644 --- a/emigui/src/style.rs +++ b/emigui/src/style.rs @@ -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), ); diff --git a/emigui/src/widgets.rs b/emigui/src/widgets.rs index c18313ed..3684cb36 100644 --- a/emigui/src/widgets.rs +++ b/emigui/src/widgets.rs @@ -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, )