@@ -39,13 +39,15 @@ enum DragState {
39
39
SelectBox ( egui:: Pos2 ) ,
40
40
Feature ( FeatureKey , egui:: Vec2 ) ,
41
41
Constraint ( ConstraintKey , egui:: Vec2 ) ,
42
+ EditingLineLength ( ConstraintKey ) ,
42
43
}
43
44
44
45
#[ derive( Clone , Debug , Copy ) ]
45
46
enum Input {
46
47
Selection ( egui:: Rect ) ,
47
48
FeatureDrag ( FeatureKey , egui:: Pos2 ) ,
48
49
ConstraintDrag ( ConstraintKey , egui:: Pos2 ) ,
50
+ EditingLineLength ( ConstraintKey ) ,
49
51
}
50
52
51
53
/// Widget implements the egui drawing widget.
@@ -113,44 +115,78 @@ impl<'a> Widget<'a> {
113
115
// Handle: selection, dragging
114
116
let current_input = if let Some ( hp) = hp {
115
117
let select_id = ui. make_persistent_id ( "select_box_start" ) ;
116
- let drag_state = if response. drag_started_by ( egui:: PointerButton :: Primary ) {
117
- match hover {
118
- // dragging a box to select
119
- Hover :: None => {
120
- let state = DragState :: SelectBox ( self . drawing . vp . screen_to_point ( hp) ) ;
121
- ui. memory_mut ( |mem| mem. data . insert_temp ( select_id, state) ) ;
122
- Some ( state)
123
- }
124
- // Dragging a point
118
+ let drag_state = match (
119
+ hover,
120
+ response. drag_started_by ( egui:: PointerButton :: Primary ) ,
121
+ response. double_clicked_by ( egui:: PointerButton :: Primary ) ,
122
+ ) {
123
+ // dragging a box to select
124
+ ( Hover :: None , true , false ) => {
125
+ let state = DragState :: SelectBox ( self . drawing . vp . screen_to_point ( hp) ) ;
126
+ ui. memory_mut ( |mem| mem. data . insert_temp ( select_id, state) ) ;
127
+ Some ( state)
128
+ }
129
+ // Dragging a point
130
+ (
125
131
Hover :: Feature {
126
132
k,
127
133
feature : Feature :: Point ( _, px, py) ,
128
- } => {
129
- let offset =
130
- self . drawing . vp . screen_to_point ( hp) - egui:: Pos2 :: new ( * px, * py) ;
131
- let state = DragState :: Feature ( * k, offset) ;
132
- ui. memory_mut ( |mem| mem. data . insert_temp ( select_id, state) ) ;
133
- Some ( state)
134
- }
135
- // TODO: dragging a line
134
+ } ,
135
+ true ,
136
+ false ,
137
+ ) => {
138
+ let offset = self . drawing . vp . screen_to_point ( hp) - egui:: Pos2 :: new ( * px, * py) ;
139
+ let state = DragState :: Feature ( * k, offset) ;
140
+ ui. memory_mut ( |mem| mem. data . insert_temp ( select_id, state) ) ;
141
+ Some ( state)
142
+ }
143
+ // TODO: dragging a line
144
+ (
136
145
Hover :: Feature {
137
146
k : _,
138
147
feature : Feature :: LineSegment ( ..) ,
139
- } => None ,
148
+ } ,
149
+ true ,
150
+ false ,
151
+ ) => None ,
152
+ // Dragging a LineLength constraint reference
153
+ (
140
154
Hover :: Constraint {
141
155
k,
142
156
constraint : Constraint :: LineLength ( _, _, _, dd) ,
143
- } => {
144
- let offset =
145
- self . drawing . vp . screen_to_point ( hp) - egui:: Pos2 :: new ( dd. x , dd. y ) ;
146
- let state = DragState :: Constraint ( * k, offset) ;
147
- ui. memory_mut ( |mem| mem. data . insert_temp ( select_id, state) ) ;
157
+ } ,
158
+ true ,
159
+ false ,
160
+ ) => {
161
+ let offset = self . drawing . vp . screen_to_point ( hp) - egui:: Pos2 :: new ( dd. x , dd. y ) ;
162
+ let state = DragState :: Constraint ( * k, offset) ;
163
+ ui. memory_mut ( |mem| mem. data . insert_temp ( select_id, state) ) ;
164
+ Some ( state)
165
+ }
166
+ // Double-clicking a LineLength constraint reference
167
+ (
168
+ Hover :: Constraint {
169
+ k,
170
+ constraint : Constraint :: LineLength ( _, _, _, dd) ,
171
+ } ,
172
+ false ,
173
+ true ,
174
+ ) => {
175
+ if let Some ( Constraint :: LineLength ( meta, ..) ) = self . drawing . constraint_mut ( * k)
176
+ {
177
+ meta. focus_to = true ;
178
+ let state = DragState :: EditingLineLength ( * k) ;
179
+ ui. memory_mut ( |mem| {
180
+ mem. data . insert_temp ( select_id, state) ;
181
+ } ) ;
148
182
Some ( state)
183
+ } else {
184
+ unreachable ! ( ) ;
149
185
}
150
- Hover :: Constraint { .. } => None ,
151
186
}
152
- } else {
153
- ui. memory ( |mem| mem. data . get_temp ( select_id) )
187
+ ( Hover :: Constraint { .. } , true , false ) => None ,
188
+
189
+ ( _, _, _) => ui. memory ( |mem| mem. data . get_temp ( select_id) ) ,
154
190
} ;
155
191
156
192
let released = response. drag_released_by ( egui:: PointerButton :: Primary ) ;
@@ -196,20 +232,37 @@ impl<'a> Widget<'a> {
196
232
Some ( Input :: ConstraintDrag ( ck, hp) )
197
233
}
198
234
235
+ ( Some ( DragState :: EditingLineLength ( ck) ) , _) => {
236
+ if response. clicked ( ) && matches ! ( hover, Hover :: None ) {
237
+ ui. memory_mut ( |mem| mem. data . remove :: < DragState > ( select_id) ) ;
238
+ }
239
+ Some ( Input :: EditingLineLength ( ck) )
240
+ }
241
+
199
242
( None , _) => None ,
200
243
}
201
244
} else {
202
245
None
203
246
} ;
204
247
248
+ self . drawing . selected_constraint = if let Some ( Input :: EditingLineLength ( ck) ) = current_input
249
+ {
250
+ Some ( ck)
251
+ } else {
252
+ None
253
+ } ;
254
+
205
255
// All clicks get keyboard focus.
206
256
// println!("focus-w: {:?}", response.ctx.memory(|mem| mem.focus()));
207
257
if response. clicked ( ) && !response. lost_focus ( ) {
208
258
ui. memory_mut ( |mem| mem. request_focus ( response. id ) ) ;
209
259
}
210
260
211
261
// Handle: clicks altering selection
212
- if hp. is_some ( ) && response. clicked_by ( egui:: PointerButton :: Primary ) {
262
+ if hp. is_some ( )
263
+ && response. clicked_by ( egui:: PointerButton :: Primary )
264
+ && !matches ! ( current_input, Some ( Input :: EditingLineLength ( ck) ) )
265
+ {
213
266
let shift_held = ui. input ( |i| i. modifiers . shift ) ;
214
267
215
268
// feature clicked: add-to or replace selection
@@ -308,25 +361,66 @@ impl<'a> Widget<'a> {
308
361
v. paint ( self . drawing , k, & pp, painter) ;
309
362
}
310
363
311
- if let Some ( Input :: Selection ( current_drag) ) = current_input {
312
- let screen_rect = self . drawing . vp . translate_rect ( current_drag) ;
313
- painter. rect_filled (
314
- screen_rect. shrink ( 1. ) ,
315
- egui:: Rounding :: ZERO ,
316
- egui:: Color32 :: from_white_alpha ( 20 ) ,
317
- ) ;
318
- painter. rect_stroke (
319
- screen_rect,
320
- egui:: Rounding :: ZERO ,
321
- egui:: Stroke {
322
- width : 1. ,
323
- color : egui:: Color32 :: WHITE ,
324
- } ,
325
- ) ;
326
- }
364
+ match current_input {
365
+ Some ( Input :: Selection ( current_drag) ) => {
366
+ let screen_rect = self . drawing . vp . translate_rect ( current_drag) ;
367
+ painter. rect_filled (
368
+ screen_rect. shrink ( 1. ) ,
369
+ egui:: Rounding :: ZERO ,
370
+ egui:: Color32 :: from_white_alpha ( 20 ) ,
371
+ ) ;
372
+ painter. rect_stroke (
373
+ screen_rect,
374
+ egui:: Rounding :: ZERO ,
375
+ egui:: Stroke {
376
+ width : 1. ,
377
+ color : egui:: Color32 :: WHITE ,
378
+ } ,
379
+ ) ;
380
+ }
381
+
382
+ Some ( Input :: FeatureDrag ( _, _) )
383
+ | Some ( Input :: ConstraintDrag ( _, _) )
384
+ | Some ( Input :: EditingLineLength ( _) )
385
+ | None => { }
386
+ } ;
327
387
328
388
self . tools . paint ( ui, painter, response, hp, & base_params) ;
329
389
390
+ // if let Some(Input::EditingLineLength(ck)) = current_input {
391
+ // if let Some(Constraint::LineLength(_, fk, _, dd)) = self.drawing.constraints.get(ck) {
392
+ // if let Some(Feature::LineSegment(_, f1, f2)) = self.drawing.features.get(*fk) {
393
+ // let (a, b) = match (
394
+ // self.drawing.features.get(*f1).unwrap(),
395
+ // self.drawing.features.get(*f2).unwrap(),
396
+ // ) {
397
+ // (Feature::Point(_, x1, y1), Feature::Point(_, x2, y2)) => {
398
+ // (egui::Pos2 { x: *x1, y: *y1 }, egui::Pos2 { x: *x2, y: *y2 })
399
+ // }
400
+ // _ => panic!("unexpected subkey types: {:?} & {:?}", f1, f2),
401
+ // };
402
+
403
+ // let reference = egui::Vec2::from((dd.x, dd.y));
404
+ // let t = (a - b).angle() + reference.angle();
405
+ // let reference_screen = self.drawing.vp.translate_point(a.lerp(b, 0.5))
406
+ // + egui::Vec2::angled(t) * reference.length();
407
+
408
+ // let area_response = egui::Area::new(popup_id)
409
+ // .order(egui::Order::Foreground)
410
+ // .fixed_pos(reference_screen)
411
+ // .constrain(true)
412
+ // .pivot(egui::Align2::CENTER_CENTER)
413
+ // .show(ui.ctx(), |ui| {
414
+ // ui.vertical(|ui| ui.label("HI"));
415
+ // // egui::Frame::popup(ui.style()).show(ui, |ui| {
416
+ // // ui.label("HI");
417
+ // // });
418
+ // })
419
+ // .response;
420
+ // };
421
+ // };
422
+ // }
423
+
330
424
self . draw_debug ( ui, painter, hp, & base_params) ;
331
425
}
332
426
0 commit comments