From add122549f230c8398dd42670fbd5b13b6ff43f8 Mon Sep 17 00:00:00 2001 From: Dustin Carlino Date: Sat, 20 Feb 2021 10:03:06 -0800 Subject: [PATCH] Modernize map_editor code: EventCtx should always be the first param --- map_editor/src/edit.rs | 2 +- map_editor/src/main.rs | 24 +++++------ map_editor/src/model.rs | 96 ++++++++++++++++++++--------------------- 3 files changed, 61 insertions(+), 61 deletions(-) diff --git a/map_editor/src/edit.rs b/map_editor/src/edit.rs index 56590fd7b2..9002d58a8e 100644 --- a/map_editor/src/edit.rs +++ b/map_editor/src/edit.rs @@ -178,7 +178,7 @@ impl SimpleState for EditRoad { _ => unreachable!(), } - app.model.road_added(self.r, ctx); + app.model.road_added(ctx, self.r); Transition::Pop } _ => unreachable!(), diff --git a/map_editor/src/main.rs b/map_editor/src/main.rs index 9d97eead91..8473ebcc2d 100644 --- a/map_editor/src/main.rs +++ b/map_editor/src/main.rs @@ -64,7 +64,7 @@ impl MainState { args.done(); let model = if let Some(path) = load { - Model::import(path, include_bldgs, ctx) + Model::import(ctx, path, include_bldgs) } else { Model::blank() }; @@ -160,7 +160,7 @@ impl State for MainState { app.model.stop_showing_pts(id); } if let Some(r) = after { - app.model.show_r_points(r, ctx); + app.model.show_r_points(ctx, r); app.model.world.handle_mouseover(ctx); } } @@ -230,13 +230,13 @@ impl State for MainState { app.model.delete_r(r); app.model.world.handle_mouseover(ctx); } else if cursor.is_some() && ctx.input.pressed(Key::P) { - if let Some(id) = app.model.insert_r_pt(r, cursor.unwrap(), ctx) { + if let Some(id) = app.model.insert_r_pt(ctx, r, cursor.unwrap()) { app.model.world.force_set_selection(id); } } else if ctx.input.pressed(Key::X) { - app.model.clear_r_pts(r, ctx); + app.model.clear_r_pts(ctx, r); } else if ctx.input.pressed(Key::M) { - app.model.merge_r(r, ctx); + app.model.merge_r(ctx, r); app.model.world.handle_mouseover(ctx); } else if ctx.normal_left_click() { return Transition::Push(edit::EditRoad::new(ctx, app, r)); @@ -274,7 +274,7 @@ impl State for MainState { if ctx.input.pressed(Key::LeftControl) { self.mode = Mode::MovingRoadPoint(r, idx); } else if ctx.input.pressed(Key::Backspace) { - app.model.delete_r_pt(r, idx, ctx); + app.model.delete_r_pt(ctx, r, idx); app.model.world.handle_mouseover(ctx); } @@ -315,7 +315,7 @@ impl State for MainState { _ => { if ctx.input.pressed(Key::I) { if let Some(pt) = cursor { - app.model.create_i(pt, ctx); + app.model.create_i(ctx, pt); app.model.world.handle_mouseover(ctx); } // TODO Silly bug: Mouseover doesn't actually work! I think the @@ -323,7 +323,7 @@ impl State for MainState { // up the precomputed triangles. } else if ctx.input.pressed(Key::B) { if let Some(pt) = cursor { - let id = app.model.create_b(pt, ctx); + let id = app.model.create_b(ctx, pt); app.model.world.force_set_selection(id); } } @@ -348,7 +348,7 @@ impl State for MainState { } Mode::MovingIntersection(id) => { if let Some(pt) = cursor { - app.model.move_i(id, pt, ctx); + app.model.move_i(ctx, id, pt); if ctx.input.key_released(Key::LeftControl) { self.mode = Mode::Viewing; } @@ -356,7 +356,7 @@ impl State for MainState { } Mode::MovingBuilding(id) => { if let Some(pt) = cursor { - app.model.move_b(id, pt, ctx); + app.model.move_b(ctx, id, pt); if ctx.input.key_released(Key::LeftControl) { self.mode = Mode::Viewing; } @@ -364,7 +364,7 @@ impl State for MainState { } Mode::MovingRoadPoint(r, idx) => { if let Some(pt) = cursor { - app.model.move_r_pt(r, idx, pt, ctx); + app.model.move_r_pt(ctx, r, idx, pt); if ctx.input.key_released(Key::LeftControl) { self.mode = Mode::Viewing; } @@ -376,7 +376,7 @@ impl State for MainState { app.model.world.handle_mouseover(ctx); } else if let Some(ID::Intersection(i2)) = app.model.world.get_selection() { if i1 != i2 && ctx.input.pressed(Key::R) { - app.model.create_r(i1, i2, ctx); + app.model.create_r(ctx, i1, i2); self.mode = Mode::Viewing; app.model.world.handle_mouseover(ctx); } diff --git a/map_editor/src/model.rs b/map_editor/src/model.rs index 2f1ff51bd1..5b72b10626 100644 --- a/map_editor/src/model.rs +++ b/map_editor/src/model.rs @@ -43,7 +43,7 @@ impl Model { } } - pub fn import(path: String, include_bldgs: bool, ctx: &EventCtx) -> Model { + pub fn import(ctx: &EventCtx, path: String, include_bldgs: bool) -> Model { let mut timer = Timer::new("import map"); let mut model = Model::blank(); model.include_bldgs = include_bldgs; @@ -78,7 +78,7 @@ impl Model { if model.include_bldgs { for id in model.map.buildings.keys().cloned().collect::>() { - model.bldg_added(id, ctx); + model.bldg_added(ctx, id); } } timer.start_iter( @@ -87,12 +87,12 @@ impl Model { ); for id in model.map.intersections.keys().cloned().collect::>() { timer.next(); - model.intersection_added(id, ctx); + model.intersection_added(ctx, id); } timer.start_iter("fill out world with roads", model.map.roads.len()); for id in model.map.roads.keys().cloned().collect::>() { timer.next(); - model.road_added(id, ctx); + model.road_added(ctx, id); } model @@ -135,13 +135,13 @@ impl Model { // Re-add everything to the world, since we just shifted coordinates around self.world = World::new(); for id in self.map.buildings.keys().cloned().collect::>() { - self.bldg_added(id, ctx); + self.bldg_added(ctx, id); } for id in self.map.intersections.keys().cloned().collect::>() { - self.intersection_added(id, ctx); + self.intersection_added(ctx, id); } for id in self.map.roads.keys().cloned().collect::>() { - self.road_added(id, ctx); + self.road_added(ctx, id); } } @@ -166,7 +166,7 @@ impl Model { // Intersections impl Model { - fn intersection_added(&mut self, id: osm::NodeID, ctx: &EventCtx) { + fn intersection_added(&mut self, ctx: &EventCtx, id: osm::NodeID) { let i = &self.map.intersections[&id]; let color = match i.intersection_type { IntersectionType::TrafficSignal => Color::GREEN, @@ -186,7 +186,7 @@ impl Model { .add(ctx, Object::new(ID::Intersection(id), color, poly)); } - pub fn create_i(&mut self, point: Pt2D, ctx: &EventCtx) { + pub fn create_i(&mut self, ctx: &EventCtx, point: Pt2D) { let id = self.map.new_osm_node_id(time_to_id()); self.map.intersections.insert( id, @@ -198,16 +198,16 @@ impl Model { elevation: Distance::ZERO, }, ); - self.intersection_added(id, ctx); + self.intersection_added(ctx, id); } - pub fn move_i(&mut self, id: osm::NodeID, point: Pt2D, ctx: &EventCtx) { + pub fn move_i(&mut self, ctx: &EventCtx, id: osm::NodeID, point: Pt2D) { self.world.delete(ID::Intersection(id)); for r in self.map.move_intersection(id, point).unwrap() { self.road_deleted(r); - self.road_added(r, ctx); + self.road_added(ctx, r); } - self.intersection_added(id, ctx); + self.intersection_added(ctx, id); } pub fn delete_i(&mut self, id: osm::NodeID) { @@ -229,7 +229,7 @@ impl Model { i.intersection_type = IntersectionType::TrafficSignal; } - self.intersection_added(id, ctx); + self.intersection_added(ctx, id); } pub fn show_intersection_geometry(&mut self, ctx: &EventCtx, show: bool) { @@ -237,14 +237,14 @@ impl Model { for id in self.map.intersections.keys().cloned().collect::>() { self.world.delete(ID::Intersection(id)); - self.intersection_added(id, ctx); + self.intersection_added(ctx, id); } } } // Roads impl Model { - pub fn road_added(&mut self, id: OriginalRoad, ctx: &EventCtx) { + pub fn road_added(&mut self, ctx: &EventCtx, id: OriginalRoad) { self.world.add(ctx, self.road_object(id)); } @@ -252,7 +252,7 @@ impl Model { self.world.delete(ID::Road(id)); } - pub fn create_r(&mut self, i1: osm::NodeID, i2: osm::NodeID, ctx: &EventCtx) { + pub fn create_r(&mut self, ctx: &EventCtx, i1: osm::NodeID, i2: osm::NodeID) { // Ban cul-de-sacs, since they get stripped out later anyway. if self .map @@ -293,7 +293,7 @@ impl Model { complicated_turn_restrictions: Vec::new(), }, ); - self.road_added(id, ctx); + self.road_added(ctx, id); } pub fn delete_r(&mut self, id: OriginalRoad) { @@ -313,7 +313,7 @@ impl Model { ) } - pub fn show_r_points(&mut self, id: OriginalRoad, ctx: &EventCtx) { + pub fn show_r_points(&mut self, ctx: &EventCtx, id: OriginalRoad) { if self.showing_pts == Some(id) { return; } @@ -349,7 +349,7 @@ impl Model { } } - pub fn move_r_pt(&mut self, id: OriginalRoad, idx: usize, point: Pt2D, ctx: &EventCtx) { + pub fn move_r_pt(&mut self, ctx: &EventCtx, id: OriginalRoad, idx: usize, point: Pt2D) { assert_eq!(self.showing_pts, Some(id)); self.stop_showing_pts(id); @@ -360,13 +360,13 @@ impl Model { let pts = &mut self.map.roads.get_mut(&id).unwrap().center_points; pts[idx] = point; - self.road_added(id, ctx); - self.intersection_added(id.i1, ctx); - self.intersection_added(id.i2, ctx); - self.show_r_points(id, ctx); + self.road_added(ctx, id); + self.intersection_added(ctx, id.i1); + self.intersection_added(ctx, id.i2); + self.show_r_points(ctx, id); } - pub fn delete_r_pt(&mut self, id: OriginalRoad, idx: usize, ctx: &EventCtx) { + pub fn delete_r_pt(&mut self, ctx: &EventCtx, id: OriginalRoad, idx: usize) { assert_eq!(self.showing_pts, Some(id)); self.stop_showing_pts(id); @@ -377,13 +377,13 @@ impl Model { let pts = &mut self.map.roads.get_mut(&id).unwrap().center_points; pts.remove(idx); - self.road_added(id, ctx); - self.intersection_added(id.i1, ctx); - self.intersection_added(id.i2, ctx); - self.show_r_points(id, ctx); + self.road_added(ctx, id); + self.intersection_added(ctx, id.i1); + self.intersection_added(ctx, id.i2); + self.show_r_points(ctx, id); } - pub fn insert_r_pt(&mut self, id: OriginalRoad, pt: Pt2D, ctx: &EventCtx) -> Option { + pub fn insert_r_pt(&mut self, ctx: &EventCtx, id: OriginalRoad, pt: Pt2D) -> Option { assert_eq!(self.showing_pts, Some(id)); self.stop_showing_pts(id); @@ -404,15 +404,15 @@ impl Model { None }; - self.road_added(id, ctx); - self.intersection_added(id.i1, ctx); - self.intersection_added(id.i2, ctx); - self.show_r_points(id, ctx); + self.road_added(ctx, id); + self.intersection_added(ctx, id.i1); + self.intersection_added(ctx, id.i2); + self.show_r_points(ctx, id); new_id } - pub fn clear_r_pts(&mut self, id: OriginalRoad, ctx: &EventCtx) { + pub fn clear_r_pts(&mut self, ctx: &EventCtx, id: OriginalRoad) { assert_eq!(self.showing_pts, Some(id)); self.stop_showing_pts(id); @@ -423,14 +423,14 @@ impl Model { let r = &mut self.map.roads.get_mut(&id).unwrap(); r.center_points = vec![r.center_points[0], *r.center_points.last().unwrap()]; - self.road_added(id, ctx); - self.intersection_added(id.i1, ctx); - self.intersection_added(id.i2, ctx); - self.show_r_points(id, ctx); + self.road_added(ctx, id); + self.intersection_added(ctx, id.i1); + self.intersection_added(ctx, id.i2); + self.show_r_points(ctx, id); } // TODO Need to show_r_points of the thing we wind up selecting after this. - pub fn merge_r(&mut self, id: OriginalRoad, ctx: &EventCtx) { + pub fn merge_r(&mut self, ctx: &EventCtx, id: OriginalRoad) { self.stop_showing_pts(id); let (retained_i, deleted_i, deleted_roads, created_roads) = @@ -440,13 +440,13 @@ impl Model { } Err(err) => { warn!("Can't merge this road: {}", err); - self.show_r_points(id, ctx); + self.show_r_points(ctx, id); return; } }; self.world.delete(ID::Intersection(retained_i)); - self.intersection_added(retained_i, ctx); + self.intersection_added(ctx, retained_i); self.world.delete(ID::Intersection(deleted_i)); @@ -454,7 +454,7 @@ impl Model { self.world.delete(ID::Road(r)); } for r in created_roads { - self.road_added(r, ctx); + self.road_added(ctx, r); } info!("Merged {}", id.to_string_code()); @@ -463,7 +463,7 @@ impl Model { // Buildings impl Model { - fn bldg_added(&mut self, id: osm::OsmID, ctx: &EventCtx) { + fn bldg_added(&mut self, ctx: &EventCtx, id: osm::OsmID) { let b = &self.map.buildings[&id]; self.world.add( ctx, @@ -471,7 +471,7 @@ impl Model { ); } - pub fn create_b(&mut self, center: Pt2D, ctx: &EventCtx) -> ID { + pub fn create_b(&mut self, ctx: &EventCtx, center: Pt2D) -> ID { let id = osm::OsmID::Way(self.map.new_osm_way_id(time_to_id())); self.map.buildings.insert( id, @@ -483,11 +483,11 @@ impl Model { amenities: Vec::new(), }, ); - self.bldg_added(id, ctx); + self.bldg_added(ctx, id); ID::Building(id) } - pub fn move_b(&mut self, id: osm::OsmID, new_center: Pt2D, ctx: &EventCtx) { + pub fn move_b(&mut self, ctx: &EventCtx, id: osm::OsmID, new_center: Pt2D) { self.world.delete(ID::Building(id)); let b = self.map.buildings.get_mut(&id).unwrap(); @@ -497,7 +497,7 @@ impl Model { new_center.y() - old_center.y(), ); - self.bldg_added(id, ctx); + self.bldg_added(ctx, id); } pub fn delete_b(&mut self, id: osm::OsmID) {