From 22df33f66a98a0f9af5f88bc72b89e29c89eec14 Mon Sep 17 00:00:00 2001 From: Dustin Carlino Date: Wed, 16 Oct 2019 08:58:14 -0700 Subject: [PATCH] try to clean up crashes with showing_pts state --- map_editor/src/main.rs | 4 ++-- map_editor/src/model.rs | 31 +++++++++++++++---------------- 2 files changed, 17 insertions(+), 18 deletions(-) diff --git a/map_editor/src/main.rs b/map_editor/src/main.rs index cd335c2887..19c8811470 100644 --- a/map_editor/src/main.rs +++ b/map_editor/src/main.rs @@ -122,8 +122,8 @@ impl GUI for UI { _ => None, }; if before != after { - if let Some(_) = before { - self.model.stop_showing_pts(); + if let Some(id) = before { + self.model.stop_showing_pts(id); } if let Some(r) = after { self.model.show_r_points(r, ctx.prerender); diff --git a/map_editor/src/model.rs b/map_editor/src/model.rs index 5d58352a20..274409c470 100644 --- a/map_editor/src/model.rs +++ b/map_editor/src/model.rs @@ -564,9 +564,7 @@ impl Model { } pub fn delete_r(&mut self, id: StableRoadID) { - if self.showing_pts == Some(id) { - self.stop_showing_pts(); - } + self.stop_showing_pts(id); match self.map.can_delete_road(id) { Ok(()) => { @@ -674,7 +672,9 @@ impl Model { if self.showing_pts == Some(id) { return; } - assert_eq!(self.showing_pts, None); + if let Some(other) = self.showing_pts { + self.stop_showing_pts(other); + } self.showing_pts = Some(id); let r = &self.map.roads[&id]; @@ -693,20 +693,21 @@ impl Model { } } - // Idempotent - pub fn stop_showing_pts(&mut self) { - if let Some(id) = self.showing_pts.take() { - let r = &self.map.roads[&id]; - for idx in 1..=r.center_points.len() - 2 { - self.world.delete(ID::RoadPoint(id, idx)); - } + pub fn stop_showing_pts(&mut self, id: StableRoadID) { + if self.showing_pts != Some(id) { + return; + } + self.showing_pts = None; + let r = &self.map.roads[&id]; + for idx in 1..=r.center_points.len() - 2 { + self.world.delete(ID::RoadPoint(id, idx)); } } pub fn move_r_pt(&mut self, id: StableRoadID, idx: usize, point: Pt2D, prerender: &Prerender) { assert_eq!(self.showing_pts, Some(id)); - self.stop_showing_pts(); + self.stop_showing_pts(id); self.road_deleted(id); let mut pts = self.map.roads[&id].center_points.clone(); @@ -720,7 +721,7 @@ impl Model { pub fn delete_r_pt(&mut self, id: StableRoadID, idx: usize, prerender: &Prerender) { assert_eq!(self.showing_pts, Some(id)); - self.stop_showing_pts(); + self.stop_showing_pts(id); self.road_deleted(id); let mut pts = self.map.roads[&id].center_points.clone(); @@ -738,9 +739,7 @@ impl Model { return; } - if self.showing_pts == Some(id) { - self.stop_showing_pts(); - } + self.stop_showing_pts(id); // TODO Bit hacky, but we have to do this before doing the mutation, so we know the number // of lanes and can generate all the IDs.