refactoring a proper way to draw/use rectangles

This commit is contained in:
Dustin Carlino 2018-11-14 09:50:42 -08:00
parent 99fbde1d4d
commit 7b6f1c4624
6 changed files with 41 additions and 38 deletions

View File

@ -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),
);
}
}

View File

@ -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,
[
&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,
],
),
);
}
}

View File

@ -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(

View File

@ -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 {

View File

@ -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)]

View File

@ -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) {