@@ -197,30 +197,54 @@ impl Path {
197
197
self . solver . spline_to ( None , None , p3, smooth) ;
198
198
}
199
199
200
- pub fn remove_last_segment ( & mut self ) -> Option < PointId > {
201
- Arc :: make_mut ( & mut self . points ) . pop ( ) ;
202
- while self
203
- . points ( )
204
- . last ( )
205
- . map ( |pt| pt. type_ . is_control ( ) )
206
- . unwrap_or ( false )
207
- {
208
- Arc :: make_mut ( & mut self . points ) . pop ( ) ;
200
+ pub fn delete ( & mut self , id : PointId ) -> Option < PointId > {
201
+ let pos = self . idx_for_point ( id) . unwrap ( ) ;
202
+ let pt = self . points [ pos] ;
203
+ if pt. is_control ( ) {
204
+ self . points_mut ( ) . remove ( pos) ;
205
+ if self . points . get ( pos) . map ( |pt| pt. is_control ( ) ) == Some ( true ) {
206
+ self . points_mut ( ) . remove ( pos) ;
207
+ } else {
208
+ // if the other point in this segment isn't after us, it must be before:
209
+ self . points_mut ( ) . remove ( pos - 1 ) ;
210
+ }
211
+ let ( el, _) = self . element_containing_idx_mut ( pos) ;
212
+ if let Element :: SplineTo ( _, _, point, smooth) = el {
213
+ * el = Element :: LineTo ( * point, * smooth) ;
214
+ }
215
+ } else {
216
+ let element_idx = self . idx_for_element_containing_point ( pos) ;
217
+ let removed = self . solver . elements_mut ( ) . remove ( element_idx) ;
218
+ if element_idx == 0 {
219
+ if let Some ( el) = self . solver . elements_mut ( ) . get_mut ( 0 ) {
220
+ * el = Element :: MoveTo ( el. endpoint ( ) ) ;
221
+ }
222
+ }
223
+ self . points_mut ( ) . remove ( pos) ;
224
+ if matches ! ( removed, Element :: SplineTo ( ..) ) {
225
+ self . points_mut ( ) . remove ( pos - 1 ) ;
226
+ self . points_mut ( ) . remove ( pos - 2 ) ;
227
+ }
228
+ // if removing the first point, and it is followed by a splineto,
229
+ // remove the control points
230
+ if self . points . get ( 0 ) . map ( |pt| pt. is_control ( ) ) == Some ( true ) {
231
+ self . points_mut ( ) . remove ( 0 ) ;
232
+ self . points_mut ( ) . remove ( 0 ) ;
233
+ }
209
234
}
210
- self . solver . elements_mut ( ) . pop ( ) ;
211
- self . trailing = None ;
212
235
self . after_change ( ) ;
236
+ // select the last point on delete?
213
237
self . points ( ) . last ( ) . map ( |pt| pt. id )
214
238
}
215
239
216
- pub fn close ( & mut self ) {
217
- if !self . closed && self . points . len ( ) > 2 {
218
- let first = self . points . first ( ) . cloned ( ) . unwrap ( ) ;
219
- self . spline_to ( first. point , true ) ;
220
- self . closed = true ;
221
- self . solver . close ( ) ;
222
- }
223
- }
240
+ // pub fn close(&mut self) {
241
+ // if !self.closed && self.points.len() > 2 {
242
+ // let first = self.points.first().cloned().unwrap();
243
+ // self.spline_to(first.point, true);
244
+ // self.closed = true;
245
+ // self.solver.close();
246
+ // }
247
+ // }
224
248
225
249
pub fn update_for_drag ( & mut self , handle : Point ) {
226
250
assert ! ( !self . points. is_empty( ) ) ;
@@ -301,6 +325,19 @@ impl Path {
301
325
unreachable ! ( ) ;
302
326
}
303
327
328
+ fn idx_for_element_containing_point ( & self , idx : usize ) -> usize {
329
+ let mut dist_to_pt = idx;
330
+ for ( i, element) in self . solver . elements ( ) . iter ( ) . enumerate ( ) {
331
+ match element {
332
+ Element :: MoveTo ( ..) | Element :: LineTo ( ..) if dist_to_pt == 0 => return i,
333
+ Element :: SplineTo ( ..) if ( 0 ..=2 ) . contains ( & dist_to_pt) => return i,
334
+ Element :: LineTo ( ..) | Element :: MoveTo ( ..) => dist_to_pt -= 1 ,
335
+ Element :: SplineTo ( ..) => dist_to_pt = dist_to_pt. saturating_sub ( 3 ) ,
336
+ }
337
+ }
338
+ unreachable ! ( ) ;
339
+ }
340
+
304
341
pub fn maybe_convert_line_to_spline ( & mut self , click : Point , max_dist : f64 ) {
305
342
let mut best = ( f64:: MAX , 0 ) ;
306
343
let spline = self . solver . solve ( ) ;
@@ -331,7 +368,8 @@ impl Path {
331
368
let p1 = prev_point. lerp ( pt. point , 1.0 / 3.0 ) ;
332
369
let p2 = prev_point. lerp ( pt. point , 2.0 / 3.0 ) ;
333
370
self . points_mut ( ) . insert ( i, SplinePoint :: control ( p1, true ) ) ;
334
- self . points_mut ( ) . insert ( i + 1 , SplinePoint :: control ( p2, true ) ) ;
371
+ self . points_mut ( )
372
+ . insert ( i + 1 , SplinePoint :: control ( p2, true ) ) ;
335
373
break ;
336
374
}
337
375
segs_seen += 1 ;
0 commit comments