diff --git a/editor/src/plugins/color_picker.rs b/editor/src/plugins/color_picker.rs index 636251ed81..0d80a81efa 100644 --- a/editor/src/plugins/color_picker.rs +++ b/editor/src/plugins/color_picker.rs @@ -1,6 +1,7 @@ // Copyright 2018 Google LLC, licensed under http://www.apache.org/licenses/LICENSE-2.0 use ezgui::{Canvas, Color, GfxCtx, InputResult, Menu}; +use geom::Polygon; use objects::{Ctx, SETTINGS}; use piston::input::Key; use plugins::{Plugin, PluginCtx}; @@ -102,9 +103,9 @@ impl Plugin for ColorPicker { (x * TILE_DIMS + start_x) as f64, (y * TILE_DIMS + start_y) as f64, )); - g.draw_rectangle( + g.draw_polygon( color, - [corner.x(), corner.y(), TILE_DIMS as f64, TILE_DIMS as f64], + &Polygon::rectangle_topleft(corner, TILE_DIMS as f64, TILE_DIMS as f64), ); } } diff --git a/editor/src/plugins/show_activity.rs b/editor/src/plugins/show_activity.rs index e6eded5167..a9b817a4df 100644 --- a/editor/src/plugins/show_activity.rs +++ b/editor/src/plugins/show_activity.rs @@ -1,5 +1,5 @@ use ezgui::{Color, GfxCtx}; -use geom::{Bounds, Pt2D}; +use geom::{Bounds, Polygon, Pt2D}; use objects::{Ctx, DEBUG}; use piston::input::Key; use plugins::{Plugin, PluginCtx}; @@ -112,14 +112,16 @@ impl Heatmap { let percent = (self.counts[x][y] as f32) / (self.max as f32); // TODO Map percent to hot/cold colors. For now, don't ever become totally opaque. let color = Color::rgba(255, 0, 0, percent * 0.8); - g.draw_rectangle( + g.draw_polygon( color, - [ - self.bounds.min_x + (x as f64) * tile_width, - self.bounds.min_y + (y as f64) * tile_height, + &Polygon::rectangle_topleft( + Pt2D::new( + self.bounds.min_x + (x as f64) * tile_width, + self.bounds.min_y + (y as f64) * tile_height, + ), tile_width, tile_height, - ], + ), ); } } diff --git a/editor/src/render/intersection.rs b/editor/src/render/intersection.rs index ade149308d..e37440689b 100644 --- a/editor/src/render/intersection.rs +++ b/editor/src/render/intersection.rs @@ -52,14 +52,9 @@ impl DrawIntersection { fn draw_traffic_signal(&self, g: &mut GfxCtx, ctx: Ctx) { let radius = 0.5; - g.draw_rectangle( + g.draw_polygon( ctx.cs.get("traffic signal box", Color::BLACK), - [ - self.center.x() - (2.0 * radius), - self.center.y() - (4.0 * radius), - 4.0 * radius, - 8.0 * radius, - ], + &Polygon::rectangle(self.center, 4.0 * radius, 8.0 * radius), ); g.draw_circle( diff --git a/ezgui/src/lib.rs b/ezgui/src/lib.rs index c36c3c7dd9..23b4fd06ba 100644 --- a/ezgui/src/lib.rs +++ b/ezgui/src/lib.rs @@ -138,16 +138,6 @@ impl<'a> GfxCtx<'a> { self.gfx, ); } - - // TODO probably better to have a Polygon::make_rectangle helper or something - pub fn draw_rectangle(&mut self, color: Color, rect: [f64; 4]) { - graphics::Rectangle::new(color.0).draw( - rect, - &self.ctx.draw_state, - self.ctx.transform, - self.gfx, - ); - } } pub struct ToggleableLayer { diff --git a/geom/src/polygon.rs b/geom/src/polygon.rs index e7bce4099c..c65b32a896 100644 --- a/geom/src/polygon.rs +++ b/geom/src/polygon.rs @@ -164,6 +164,28 @@ impl Polygon { .collect(), ) } + + pub fn rectangle(center: Pt2D, width: f64, height: f64) -> Polygon { + let (x, y) = (center.x(), center.y()); + let half_width = width / 2.0; + let half_height = height / 2.0; + Polygon::new(&vec![ + Pt2D::new(x - half_width, y - half_height), + Pt2D::new(x + half_width, y - half_height), + Pt2D::new(x + half_width, y + half_height), + Pt2D::new(x - half_width, y + half_height), + ]) + } + + pub fn rectangle_topleft(top_left: Pt2D, width: f64, height: f64) -> Polygon { + let (x, y) = (top_left.x(), top_left.y()); + Polygon::new(&vec![ + Pt2D::new(x, y), + Pt2D::new(x + width, y), + Pt2D::new(x + width, y + height), + Pt2D::new(x, y + height), + ]) + } } #[derive(Clone, Debug)] diff --git a/synthetic/src/model.rs b/synthetic/src/model.rs index 30eb21de1a..f35db8e2f5 100644 --- a/synthetic/src/model.rs +++ b/synthetic/src/model.rs @@ -42,19 +42,12 @@ impl Road { } pub struct Building { - top_left: Pt2D, + center: Pt2D, } impl Building { fn polygon(&self) -> Polygon { - let (x, y) = (self.top_left.x(), self.top_left.y()); - - Polygon::new(&vec![ - Pt2D::new(x, y), - Pt2D::new(x + BUILDING_LENGTH, y), - Pt2D::new(x + BUILDING_LENGTH, y + BUILDING_LENGTH), - Pt2D::new(x, y + BUILDING_LENGTH), - ]) + Polygon::rectangle(self.center, BUILDING_LENGTH, BUILDING_LENGTH) } } @@ -143,13 +136,13 @@ impl Model { } impl Model { - pub fn create_b(&mut self, top_left: Pt2D) { + pub fn create_b(&mut self, center: Pt2D) { let id = self.buildings.len(); - self.buildings.insert(id, Building { top_left }); + self.buildings.insert(id, Building { center }); } - pub fn move_b(&mut self, id: IntersectionID, top_left: Pt2D) { - self.buildings.get_mut(&id).unwrap().top_left = top_left; + pub fn move_b(&mut self, id: IntersectionID, center: Pt2D) { + self.buildings.get_mut(&id).unwrap().center = center; } pub fn remove_b(&mut self, id: BuildingID) {