starting new halloween experiment... just rendering thick roads

This commit is contained in:
Dustin Carlino 2018-10-06 13:26:27 -07:00
parent 661d784b6e
commit b14df8a090
5 changed files with 130 additions and 0 deletions

View File

@ -9,6 +9,7 @@ members = [
"ezgui",
"geom",
"gtfs",
"halloween",
"headless",
"kml",
"map_model",

View File

@ -277,3 +277,10 @@ Alright, I think this is the sequence of things to do:
- but wait, then road editor kind of cant work, because mut borrow edits from map while holding immutable lane/road refs. theyre really indep things, so cant store together.
2) make it possible to completely reload UI and everything from scratch, from a plugin. rationale: it'd be nice to switch maps from inside the editor anyway. not necessary, but useful.
3) make road edits propogate correctly, and somehow have a strategy for ensuring nothing is forgotten. impl today is VERY incomplete.
## Rendering a map differently
For "Project Halloween", I want to draw the map model in a very different
visual style. Stuff like intersections are ignored, rendering roads instead of
lanes, and making animated buildings. Let's just start, and see what kind of
common code makes sense to keep.

11
halloween/Cargo.toml Normal file
View File

@ -0,0 +1,11 @@
[package]
name = "halloween"
version = "0.1.0"
authors = ["Dustin Carlino <dabreegster@gmail.com>"]
[dependencies]
ezgui = { path = "../ezgui" }
geom = { path = "../geom" }
map_model = { path = "../map_model" }
piston = "*"
structopt = "0.2"

67
halloween/src/main.rs Normal file
View File

@ -0,0 +1,67 @@
extern crate ezgui;
extern crate geom;
extern crate map_model;
extern crate piston;
#[macro_use]
extern crate structopt;
mod render;
use ezgui::{Canvas, EventLoopMode, GfxCtx, Text, UserInput, GUI};
use map_model::{Map, RoadEdits};
use piston::input::Key;
use render::DrawMap;
use std::process;
use structopt::StructOpt;
#[derive(StructOpt)]
#[structopt(name = "halloween")]
struct Flags {
/// Map to render
#[structopt(name = "load_map")]
load_map: String,
}
const KEY_CATEGORY: &str = "";
struct UI {
canvas: Canvas,
draw_map: DrawMap,
}
impl UI {
fn new(flags: Flags) -> UI {
let map = Map::new(&flags.load_map, RoadEdits::new()).unwrap();
UI {
canvas: Canvas::new(),
draw_map: DrawMap::new(map),
}
}
}
impl GUI for UI {
fn event(&mut self, mut input: UserInput, _osd: &mut Text) -> EventLoopMode {
if input.unimportant_key_pressed(Key::Escape, KEY_CATEGORY, "quit") {
process::exit(0);
}
self.canvas.handle_event(&mut input);
EventLoopMode::InputOnly
}
fn get_mut_canvas(&mut self) -> &mut Canvas {
&mut self.canvas
}
fn draw(&self, g: &mut GfxCtx, _osd: Text) {
g.clear([1.0; 4]);
self.draw_map.draw(g);
}
}
fn main() {
let flags = Flags::from_args();
ezgui::run(UI::new(flags), "Halloween tech demo", 1024, 768);
}

44
halloween/src/render.rs Normal file
View File

@ -0,0 +1,44 @@
use ezgui::GfxCtx;
use geom::Polygon;
use map_model::{Map, Road, RoadID, LANE_THICKNESS};
struct DrawRoad {
id: RoadID,
polygon: Polygon,
}
impl DrawRoad {
fn new(r: &Road) -> DrawRoad {
// TODO Should shift if the number of children is uneven
let num_lanes = r.children_forwards.len() + r.children_backwards.len();
DrawRoad {
id: r.id,
polygon: r
.center_pts
.make_polygons_blindly(LANE_THICKNESS * (num_lanes as f64)),
}
}
fn draw(&self, g: &mut GfxCtx) {
g.draw_polygon([0.0, 0.0, 0.0, 1.0], &self.polygon);
}
}
pub struct DrawMap {
roads: Vec<DrawRoad>,
}
impl DrawMap {
pub fn new(map: Map) -> DrawMap {
DrawMap {
roads: map.all_roads().iter().map(|r| DrawRoad::new(r)).collect(),
}
}
pub fn draw(&self, g: &mut GfxCtx) {
// TODO no pruning yet
for r in &self.roads {
r.draw(g);
}
}
}