From cce048509fc18281733b26322f17b46071b56528 Mon Sep 17 00:00:00 2001 From: Emil Ernerfeldt Date: Sat, 25 Apr 2020 15:45:38 +0200 Subject: [PATCH] Refactor: make Rect members min/max public --- emigui/src/context.rs | 4 ++-- emigui/src/example_app.rs | 2 +- emigui/src/math.rs | 12 +++--------- emigui/src/mesher.rs | 8 ++++---- emigui/src/region.rs | 16 ++++++++-------- emigui/src/texture_atlas.rs | 8 ++++---- emigui/src/widgets.rs | 10 +++++----- emigui/src/window.rs | 2 +- emigui_glium/src/painter.rs | 2 +- emigui_wasm/src/webgl.rs | 4 ++-- 10 files changed, 31 insertions(+), 37 deletions(-) diff --git a/emigui/src/context.rs b/emigui/src/context.rs index ce68b507..ab5a727d 100644 --- a/emigui/src/context.rs +++ b/emigui/src/context.rs @@ -201,7 +201,7 @@ impl Context { rect: rect.expand(2.0), }, ); - self.add_text(layer, rect.min(), text_style, text, Some(color::RED)); + self.add_text(layer, rect.min, text_style, text, Some(color::RED)); } pub fn debug_text(&self, pos: Pos2, text: &str) { @@ -231,7 +231,7 @@ impl Context { let font = &self.fonts[text_style]; let (text, size) = font.layout_multiline(text, f32::INFINITY); let rect = align_rect(&Rect::from_min_size(pos, size), align); - self.add_text(layer, rect.min(), text_style, text, text_color); + self.add_text(layer, rect.min, text_style, text, text_color); size } diff --git a/emigui/src/example_app.rs b/emigui/src/example_app.rs index 1d75dded..857d09d3 100644 --- a/emigui/src/example_app.rs +++ b/emigui/src/example_app.rs @@ -121,7 +121,7 @@ impl ExampleApp { None, ) .rect - .min(); + .min; let mut cmds = vec![]; for i in 0..self.num_boxes { diff --git a/emigui/src/math.rs b/emigui/src/math.rs index c4fedf46..46f21e26 100644 --- a/emigui/src/math.rs +++ b/emigui/src/math.rs @@ -318,8 +318,8 @@ impl std::fmt::Debug for Pos2 { #[derive(Clone, Copy, Default, Eq, PartialEq, Deserialize, Serialize)] pub struct Rect { - min: Pos2, - max: Pos2, + pub min: Pos2, + pub max: Pos2, } impl Rect { @@ -366,7 +366,7 @@ impl Rect { #[must_use] pub fn translate(self, amnt: Vec2) -> Self { - Rect::from_min_size(self.min() + amnt, self.size()) + Rect::from_min_size(self.min + amnt, self.size()) } #[must_use] @@ -405,12 +405,6 @@ impl Rect { y: self.min.y + self.size().y / 2.0, } } - pub fn min(&self) -> Pos2 { - self.min - } - pub fn max(&self) -> Pos2 { - self.max - } pub fn size(&self) -> Vec2 { self.max - self.min } diff --git a/emigui/src/mesher.rs b/emigui/src/mesher.rs index ffcc8533..7315bd8a 100644 --- a/emigui/src/mesher.rs +++ b/emigui/src/mesher.rs @@ -179,8 +179,8 @@ impl Path { } pub fn add_rectangle(&mut self, rect: &Rect) { - let min = rect.min(); - let max = rect.max(); + let min = rect.min; + let max = rect.max; self.add_point(pos2(min.x, min.y), vec2(-1.0, -1.0)); self.add_point(pos2(max.x, min.y), vec2(1.0, -1.0)); self.add_point(pos2(max.x, max.y), vec2(1.0, 1.0)); @@ -188,8 +188,8 @@ impl Path { } pub fn add_rounded_rectangle(&mut self, rect: &Rect, corner_radius: f32) { - let min = rect.min(); - let max = rect.max(); + let min = rect.min; + let max = rect.max; let cr = corner_radius .min(rect.width() * 0.5) diff --git a/emigui/src/region.rs b/emigui/src/region.rs index 94aa915e..38a9a870 100644 --- a/emigui/src/region.rs +++ b/emigui/src/region.rs @@ -49,7 +49,7 @@ pub struct Region { /// Where the next widget will be put. /// Progresses along self.dir. - /// Initially set to rect.min() + /// Initially set to rect.min pub(crate) cursor: Pos2, } @@ -67,7 +67,7 @@ impl Region { desired_rect: rect, bounding_size: Vec2::default(), style, - cursor: rect.min(), + cursor: rect.min, dir: Direction::Vertical, align: Align::Min, } @@ -84,7 +84,7 @@ impl Region { id: self.id, clip_rect, desired_rect: child_rect, - cursor: child_rect.min(), + cursor: child_rect.min, bounding_size: vec2(0.0, 0.0), dir: self.dir, align: self.align, @@ -167,7 +167,7 @@ impl Region { /// This how much more space we can take up without overflowing our parent. /// Shrinks as cursor increments. pub fn available_space(&self) -> Vec2 { - self.desired_rect.max() - self.cursor + self.desired_rect.max - self.cursor } pub fn direction(&self) -> Direction { @@ -209,7 +209,7 @@ impl Region { "You can only indent vertical layouts" ); let indent = vec2(self.style.indent, 0.0); - let child_rect = Rect::from_min_max(self.cursor + indent, self.desired_rect.max()); + let child_rect = Rect::from_min_max(self.cursor + indent, self.desired_rect.max); let mut child_region = Region { id: self.id.with(id_source), align: Align::Min, @@ -219,7 +219,7 @@ impl Region { let size = child_region.bounding_size; // draw a grey line on the left to mark the region - let line_start = child_rect.min() - indent * 0.5; + let line_start = child_rect.min - indent * 0.5; let line_start = line_start.round(); // TODO: round to pixel instead let line_end = pos2(line_start.x, line_start.y + size.y - 8.0); self.add_paint_cmd(PaintCmd::Line { @@ -262,7 +262,7 @@ impl Region { align: Align, add_contents: impl FnOnce(&mut Region), ) { - let child_rect = Rect::from_min_max(self.cursor, self.desired_rect.max()); + let child_rect = Rect::from_min_max(self.cursor, self.desired_rect.max); let mut child_region = Region { dir, align, @@ -443,7 +443,7 @@ impl Region { let font = &self.fonts()[text_style]; let (text, size) = font.layout_multiline(text, f32::INFINITY); let rect = align_rect(&Rect::from_min_size(pos, size), align); - self.add_text(rect.min(), text_style, text, text_color); + self.add_text(rect.min, text_style, text, text_color); size } diff --git a/emigui/src/texture_atlas.rs b/emigui/src/texture_atlas.rs index d25a9321..1c46203b 100644 --- a/emigui/src/texture_atlas.rs +++ b/emigui/src/texture_atlas.rs @@ -109,12 +109,12 @@ impl Texture { let interact = region.reserve_space(size, None); let rect = interact.rect; let top_left = Vertex { - pos: rect.min(), + pos: rect.min, uv: (0, 0), color: WHITE, }; let bottom_right = Vertex { - pos: rect.max(), + pos: rect.max, uv: (self.width as u16 - 1, self.height as u16 - 1), color: WHITE, }; @@ -137,12 +137,12 @@ impl Texture { let v = clamp(v, texel_radius..=self.height as f32 - 1.0 - texel_radius); let top_left = Vertex { - pos: zoom_rect.min(), + pos: zoom_rect.min, uv: ((u - texel_radius) as u16, (v - texel_radius) as u16), color: WHITE, }; let bottom_right = Vertex { - pos: zoom_rect.max(), + pos: zoom_rect.max, uv: ((u + texel_radius) as u16, (v + texel_radius) as u16), color: WHITE, }; diff --git a/emigui/src/widgets.rs b/emigui/src/widgets.rs index de0f8ba3..49cb4748 100644 --- a/emigui/src/widgets.rs +++ b/emigui/src/widgets.rs @@ -54,7 +54,7 @@ impl Widget for Label { let font = ®ion.fonts()[self.text_style]; let (text, text_size) = font.layout_multiline(&self.text, region.available_width()); let interact = region.reserve_space(text_size, None); - region.add_text(interact.rect.min(), self.text_style, text, self.text_color); + region.add_text(interact.rect.min, self.text_style, text, self.text_color); region.response(interact) } } @@ -96,7 +96,7 @@ impl Widget for Hyperlink { if interact.hovered { // Underline: for fragment in &text { - let pos = interact.rect.min(); + let pos = interact.rect.min; let y = pos.y + fragment.y_offset + line_spacing; let y = region.round_to_pixel(y); let min_x = pos.x + fragment.min_x(); @@ -109,7 +109,7 @@ impl Widget for Hyperlink { } } - region.add_text(interact.rect.min(), text_style, text, Some(color)); + region.add_text(interact.rect.min, text_style, text, Some(color)); region.response(interact) } @@ -198,7 +198,7 @@ impl<'a> Widget for Checkbox<'a> { + region.style().button_padding, Some(id), ); - let text_cursor = interact.rect.min() + let text_cursor = interact.rect.min + region.style().button_padding + vec2(region.style().start_icon_width, 0.0); if interact.clicked { @@ -273,7 +273,7 @@ impl Widget for RadioButton { + region.style().button_padding, Some(id), ); - let text_cursor = interact.rect.min() + let text_cursor = interact.rect.min + region.style().button_padding + vec2(region.style().start_icon_width, 0.0); diff --git a/emigui/src/window.rs b/emigui/src/window.rs index 0cb838ca..f04b0a0a 100644 --- a/emigui/src/window.rs +++ b/emigui/src/window.rs @@ -198,7 +198,7 @@ impl Window { let corner_interact = if self.resizeable { // Resize-corner: - let corner_center = outer_rect.max() - Vec2::splat(corner_radius); + let corner_center = outer_rect.max - Vec2::splat(corner_radius); let corner_rect = Rect::from_min_size(corner_center, Vec2::splat(corner_radius)); let corner_interact = ctx.interact(layer, &corner_rect, Some(id.with("corner"))); diff --git a/emigui_glium/src/painter.rs b/emigui_glium/src/painter.rs index 109d78ec..6c7b77de 100644 --- a/emigui_glium/src/painter.rs +++ b/emigui_glium/src/painter.rs @@ -267,7 +267,7 @@ impl Painter { let height_points = height_pixels as f32 / pixels_per_point; let uniforms = uniform! { - u_clip_rect: [clip_rect.min().x, clip_rect.min().y, clip_rect.max().x, clip_rect.max().y], + u_clip_rect: [clip_rect.min.x, clip_rect.min.y, clip_rect.max.x, clip_rect.max.y], u_screen_size: [width_points, height_points], u_tex_size: [texture.width as f32, texture.height as f32], u_sampler: &self.texture, diff --git a/emigui_wasm/src/webgl.rs b/emigui_wasm/src/webgl.rs index 6a74beb2..fe12e262 100644 --- a/emigui_wasm/src/webgl.rs +++ b/emigui_wasm/src/webgl.rs @@ -214,8 +214,8 @@ impl Painter { for (clip_rect, mesh) in batches { // Avoid infinities in shader: - let clip_min = clip_rect.min().max(Pos2::default()); - let clip_max = clip_rect.max().min(Pos2::default() + screen_size_points); + let clip_min = clip_rect.min.max(Pos2::default()); + let clip_max = clip_rect.max.min(Pos2::default() + screen_size_points); gl.uniform4f( Some(&u_clip_rect_loc),