From 222ddf6a964137ce46ba43a778923d576284761d Mon Sep 17 00:00:00 2001 From: Dustin Carlino Date: Fri, 17 May 2019 12:34:04 -0700 Subject: [PATCH] use Distance, not f64, for some params in geom/ layer, simplifying other code --- editor/src/debug/color_picker.rs | 8 ++++++-- editor/src/mission/scenario.rs | 4 ++-- editor/src/render/intersection.rs | 17 ++++++++--------- editor/src/sandbox/show_activity.rs | 6 +++--- ezgui/src/text.rs | 22 +++++++++++++++------- geom/src/polygon.rs | 13 ++++++------- geom/src/pt.rs | 4 ++-- 7 files changed, 42 insertions(+), 32 deletions(-) diff --git a/editor/src/debug/color_picker.rs b/editor/src/debug/color_picker.rs index b34b4ca491..9755167a04 100644 --- a/editor/src/debug/color_picker.rs +++ b/editor/src/debug/color_picker.rs @@ -2,7 +2,7 @@ use crate::ui::UI; use ezgui::{ Canvas, Color, EventCtx, GfxCtx, InputResult, Key, ModalMenu, ScreenPt, ScrollingMenu, }; -use geom::Polygon; +use geom::{Distance, Polygon}; // TODO assumes minimum screen size const WIDTH: u32 = 255; @@ -81,7 +81,11 @@ impl ColorPicker { )); g.draw_polygon( color, - &Polygon::rectangle_topleft(corner, TILE_DIMS, TILE_DIMS), + &Polygon::rectangle_topleft( + corner, + Distance::meters(TILE_DIMS), + Distance::meters(TILE_DIMS), + ), ); } } diff --git a/editor/src/mission/scenario.rs b/editor/src/mission/scenario.rs index 245cfe1302..6f3f9ff844 100644 --- a/editor/src/mission/scenario.rs +++ b/editor/src/mission/scenario.rs @@ -167,12 +167,12 @@ impl ScenarioEditor { // TODO Draw text label in neighborhood, then src is left and dst is right let src = mapping[&s.start_from_neighborhood] .center - .offset(-50.0, 0.0); + .offset(Distance::meters(-50.0), Distance::ZERO); let dst = match s.goal { OriginDestination::Neighborhood(ref n) => mapping[n].center, OriginDestination::Border(i) => ui.primary.map.get_i(i).point, } - .offset(50.0, 0.0); + .offset(Distance::meters(50.0), Distance::ZERO); // TODO Draw a self-loop or something if src == dst { continue; diff --git a/editor/src/render/intersection.rs b/editor/src/render/intersection.rs index f151cea01f..173bd13342 100644 --- a/editor/src/render/intersection.rs +++ b/editor/src/render/intersection.rs @@ -242,10 +242,9 @@ pub fn draw_signal_cycle( return; } - // TODO argh, fix these types to use Distance let radius = Distance::meters(0.5); - let box_width = (2.5 * radius).inner_meters(); - let box_height = (6.5 * radius).inner_meters(); + let box_width = 2.5 * radius; + let box_height = 6.5 * radius; let center = ctx.map.get_i(cycle.parent).point; let top_left = center.offset(-box_width / 2.0, -box_height / 2.0); let percent = time_left.unwrap() / cycle.duration; @@ -256,7 +255,7 @@ pub fn draw_signal_cycle( ); batch.push( Color::RED, - Circle::new(center.offset(0.0, (-2.0 * radius).inner_meters()), radius).to_polygon(), + Circle::new(center.offset(Distance::ZERO, -2.0 * radius), radius).to_polygon(), ); batch.push(Color::grey(0.4), Circle::new(center, radius).to_polygon()); batch.push( @@ -265,7 +264,7 @@ pub fn draw_signal_cycle( ); batch.push( Color::GREEN, - Circle::new(center.offset(0.0, (2.0 * radius).inner_meters()), radius).to_polygon(), + Circle::new(center.offset(Distance::ZERO, 2.0 * radius), radius).to_polygon(), ); batch.draw(g); } @@ -404,8 +403,8 @@ pub fn draw_signal_diagram( .get_def("signal editor panel", Color::BLACK.alpha(0.95)), &Polygon::rectangle_topleft( Pt2D::new(x1_screen, y1_screen), - total_screen_width, - (padding + intersection_height) * (cycles.len() as f64) * zoom, + Distance::meters(total_screen_width), + Distance::meters((padding + intersection_height) * (cycles.len() as f64) * zoom), ), ); g.draw_polygon( @@ -418,8 +417,8 @@ pub fn draw_signal_diagram( x1_screen, y1_screen + (padding + intersection_height) * (current_cycle as f64) * zoom, ), - total_screen_width, - (padding + intersection_height) * zoom, + Distance::meters(total_screen_width), + Distance::meters((padding + intersection_height) * zoom), ), ); diff --git a/editor/src/sandbox/show_activity.rs b/editor/src/sandbox/show_activity.rs index 1cf4eee364..dec9ca9ca4 100644 --- a/editor/src/sandbox/show_activity.rs +++ b/editor/src/sandbox/show_activity.rs @@ -1,7 +1,7 @@ use crate::render::MIN_ZOOM_FOR_DETAIL; use crate::ui::UI; use ezgui::{Color, EventCtx, GfxCtx, ModalMenu}; -use geom::{Bounds, Duration, Polygon, Pt2D}; +use geom::{Bounds, Distance, Duration, Polygon, Pt2D}; use map_model::{RoadID, Traversable}; use std::collections::HashMap; @@ -120,8 +120,8 @@ impl Heatmap { self.bounds.min_x + (x as f64) * tile_width, self.bounds.min_y + (y as f64) * tile_height, ), - tile_width, - tile_height, + Distance::meters(tile_width), + Distance::meters(tile_height), ), ); } diff --git a/ezgui/src/text.rs b/ezgui/src/text.rs index 8925f1f9e8..1818c2d399 100644 --- a/ezgui/src/text.rs +++ b/ezgui/src/text.rs @@ -1,6 +1,6 @@ use crate::screen_geom::ScreenRectangle; use crate::{Canvas, Color, GfxCtx, ScreenPt}; -use geom::{Polygon, Pt2D}; +use geom::{Distance, Polygon, Pt2D}; use glium_glyph::glyph_brush::rusttype::Scale; use glium_glyph::glyph_brush::GlyphCruncher; use glium_glyph::glyph_brush::{Section, SectionText, VariedSection}; @@ -180,8 +180,8 @@ pub fn draw_text_bubble( c, &Polygon::rectangle_topleft( Pt2D::new(top_left.x, top_left.y), - total_width, - total_height, + Distance::meters(total_width), + Distance::meters(total_height), ), ); } @@ -210,7 +210,11 @@ pub fn draw_text_bubble( if let Some(c) = line_color { g.draw_polygon( *c, - &Polygon::rectangle_topleft(Pt2D::new(top_left.x, y), total_width, height), + &Polygon::rectangle_topleft( + Pt2D::new(top_left.x, y), + Distance::meters(total_width), + Distance::meters(height), + ), ); } @@ -244,7 +248,11 @@ pub fn draw_text_bubble_mapspace( if let Some(c) = txt.bg_color { g.draw_polygon( c, - &Polygon::rectangle_topleft(top_left, total_width, total_height), + &Polygon::rectangle_topleft( + top_left, + Distance::meters(total_width), + Distance::meters(total_height), + ), ); } @@ -277,8 +285,8 @@ pub fn draw_text_bubble_mapspace( *c, &Polygon::rectangle_topleft( Pt2D::new(top_left.x(), top_left.y() + y), - total_width, - height, + Distance::meters(total_width), + Distance::meters(height), ), ); } diff --git a/geom/src/polygon.rs b/geom/src/polygon.rs index 5c8ebec82c..c967b88980 100644 --- a/geom/src/polygon.rs +++ b/geom/src/polygon.rs @@ -1,6 +1,5 @@ -use crate::{Bounds, HashablePt2D, Pt2D}; +use crate::{Bounds, Distance, HashablePt2D, Pt2D}; use serde_derive::{Deserialize, Serialize}; -use std::f64; #[derive(Serialize, Deserialize, Clone, Debug)] pub struct Polygon { @@ -114,7 +113,7 @@ impl Polygon { Bounds::from(&self.points) } - pub fn translate(&self, dx: f64, dy: f64) -> Polygon { + pub fn translate(&self, dx: Distance, dy: Distance) -> Polygon { Polygon { points: self.points.iter().map(|pt| pt.offset(dx, dy)).collect(), indices: self.indices.clone(), @@ -133,17 +132,17 @@ impl Polygon { Pt2D::center(&pts.iter().map(|pt| Pt2D::from(*pt)).collect()) } - pub fn rectangle(center: Pt2D, width: f64, height: f64) -> Polygon { + pub fn rectangle(center: Pt2D, width: Distance, height: Distance) -> Polygon { Polygon::rectangle_topleft(center.offset(-width / 2.0, -height / 2.0), width, height) } - pub fn rectangle_topleft(top_left: Pt2D, width: f64, height: f64) -> Polygon { + pub fn rectangle_topleft(top_left: Pt2D, width: Distance, height: Distance) -> Polygon { Polygon { points: vec![ top_left, - top_left.offset(width, 0.0), + top_left.offset(width, Distance::ZERO), top_left.offset(width, height), - top_left.offset(0.0, height), + top_left.offset(Distance::ZERO, height), ], indices: vec![0, 1, 2, 2, 3, 0], } diff --git a/geom/src/pt.rs b/geom/src/pt.rs index a6d2baa3a0..20405fb354 100644 --- a/geom/src/pt.rs +++ b/geom/src/pt.rs @@ -115,8 +115,8 @@ impl Pt2D { Angle::new((to.y() - self.y()).atan2(to.x() - self.x())) } - pub fn offset(self, dx: f64, dy: f64) -> Pt2D { - Pt2D::new(self.x() + dx, self.y() + dy) + pub fn offset(self, dx: Distance, dy: Distance) -> Pt2D { + Pt2D::new(self.x() + dx.inner_meters(), self.y() + dy.inner_meters()) } pub fn center(pts: &Vec) -> Pt2D {