1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
//! Render static and dynamic map elements.

use geom::{Distance, Polygon, Pt2D};
use map_model::{IntersectionID, Map, NORMAL_LANE_THICKNESS, SIDEWALK_THICKNESS};
use sim::{DrawCarInput, PersonID, Sim, VehicleType};
use widgetry::{Color, GfxCtx, Prerender};

use crate::colors::ColorScheme;
pub use crate::render::agents::{AgentCache, UnzoomedAgents};
pub use crate::render::area::DrawArea;
use crate::render::bike::DrawBike;
pub use crate::render::building::DrawBuilding;
use crate::render::car::DrawCar;
pub use crate::render::intersection::{calculate_corners, DrawIntersection};
pub use crate::render::map::DrawMap;
pub use crate::render::pedestrian::{DrawPedCrowd, DrawPedestrian};
pub use crate::render::turn::DrawMovement;
use crate::{AppLike, ID};

mod agents;
mod area;
mod bike;
mod building;
mod car;
mod intersection;
mod lane;
mod map;
mod parking_lot;
mod pedestrian;
mod road;
pub mod traffic_signal;
mod transit_stop;
mod turn;

pub const BIG_ARROW_THICKNESS: Distance = Distance::const_meters(0.5);

pub const OUTLINE_THICKNESS: Distance = Distance::const_meters(0.5);

// Does something belong here or as a method on ID? If it ONLY applies to renderable things, then
// here. For example, trips aren't drawn, so it's meaningless to ask what their bounding box is.
pub trait Renderable {
    // TODO This is expensive for the PedCrowd case. :( Returning a borrow is awkward, because most
    // Renderables are better off storing the inner ID directly.
    fn get_id(&self) -> ID;
    // Only traffic signals need UI. :\
    fn draw(&self, g: &mut GfxCtx, app: &dyn AppLike, opts: &DrawOptions);
    // Higher z-ordered objects are drawn later. Default to low so roads at -1 don't vanish.
    fn get_zorder(&self) -> isize {
        -5
    }
    // This outline is drawn over the base object to show that it's selected. It also represents
    // the boundaries for quadtrees. This isn't called often; don't worry about caching.
    fn get_outline(&self, map: &Map) -> Polygon;
    fn contains_pt(&self, pt: Pt2D, map: &Map) -> bool {
        self.get_outline(map).contains_pt(pt)
    }
}

fn draw_vehicle(
    input: DrawCarInput,
    map: &Map,
    sim: &Sim,
    prerender: &Prerender,
    cs: &ColorScheme,
) -> Box<dyn Renderable> {
    if input.id.vehicle_type == VehicleType::Bike {
        Box::new(DrawBike::new(input, map, sim, prerender, cs))
    } else {
        Box::new(DrawCar::new(input, map, sim, prerender, cs))
    }
}

/// Control how the map is drawn.
pub struct DrawOptions {
    /// Don't draw the current traffic signal state.
    pub suppress_traffic_signal_details: Vec<IntersectionID>,
    /// Label every building.
    pub label_buildings: bool,
}

impl DrawOptions {
    /// Default options for drawing a map.
    pub fn new() -> DrawOptions {
        DrawOptions {
            suppress_traffic_signal_details: Vec::new(),
            label_buildings: false,
        }
    }
}

pub fn unzoomed_agent_radius(vt: Option<VehicleType>) -> Distance {
    // Lane thickness is a little hard to see, so double it. Most of the time, the circles don't
    // leak out of the road too much.
    if vt.is_some() {
        4.0 * NORMAL_LANE_THICKNESS
    } else {
        4.0 * SIDEWALK_THICKNESS
    }
}

/// If the sim has highlighted people, then fade all others out.
fn grey_out_unhighlighted_people(color: Color, person: &Option<PersonID>, sim: &Sim) -> Color {
    if let Some(ref highlighted) = sim.get_highlighted_people() {
        if person
            .as_ref()
            .map(|p| !highlighted.contains(p))
            .unwrap_or(false)
        {
            return color.tint(0.5);
        }
    }
    color
}