@@ -2,7 +2,7 @@ use std::sync::Arc;
2
2
3
3
use { ahash:: AHashMap , parking_lot:: Mutex } ;
4
4
5
- use crate :: { layout :: align_rect , paint:: * , * } ;
5
+ use crate :: { paint:: * , * } ;
6
6
7
7
#[ derive( Clone , Copy , Default ) ]
8
8
struct PaintStats {
@@ -234,7 +234,7 @@ impl Context {
234
234
235
235
/// Generate a id from the given source.
236
236
/// If it is not unique, an error will be printed at the given position.
237
- pub fn make_unique_id < IdSource > ( & self , source : IdSource , pos : Pos2 ) -> Id
237
+ pub fn make_unique_id < IdSource > ( self : & Arc < Self > , source : IdSource , pos : Pos2 ) -> Id
238
238
where
239
239
IdSource : std:: hash:: Hash + std:: fmt:: Debug + Copy ,
240
240
{
@@ -246,19 +246,25 @@ impl Context {
246
246
}
247
247
248
248
/// If the given Id is not unique, an error will be printed at the given position.
249
- pub fn register_unique_id ( & self , id : Id , source_name : impl std:: fmt:: Debug , pos : Pos2 ) -> Id {
249
+ pub fn register_unique_id (
250
+ self : & Arc < Self > ,
251
+ id : Id ,
252
+ source_name : impl std:: fmt:: Debug ,
253
+ pos : Pos2 ,
254
+ ) -> Id {
250
255
if let Some ( clash_pos) = self . used_ids . lock ( ) . insert ( id, pos) {
256
+ let painter = self . debug_painter ( ) ;
251
257
if clash_pos. distance ( pos) < 4.0 {
252
- self . show_error (
258
+ painter . error (
253
259
pos,
254
260
& format ! ( "use of non-unique ID {:?} (name clash?)" , source_name) ,
255
261
) ;
256
262
} else {
257
- self . show_error (
263
+ painter . error (
258
264
clash_pos,
259
265
& format ! ( "first use of non-unique ID {:?} (name clash?)" , source_name) ,
260
266
) ;
261
- self . show_error (
267
+ painter . error (
262
268
pos,
263
269
& format ! (
264
270
"second use of non-unique ID {:?} (name clash?)" ,
@@ -422,103 +428,12 @@ impl Context {
422
428
}
423
429
}
424
430
}
431
+ }
425
432
426
- // ---------------------------------------------------------------------
427
-
428
- pub fn show_error ( & self , pos : Pos2 , text : impl Into < String > ) {
429
- let text = text. into ( ) ;
430
- let align = ( Align :: Min , Align :: Min ) ;
431
- let layer = Layer :: debug ( ) ;
432
- let text_style = TextStyle :: Monospace ;
433
- let font = & self . fonts ( ) [ text_style] ;
434
- let galley = font. layout_multiline ( text, f32:: INFINITY ) ;
435
- let rect = align_rect ( Rect :: from_min_size ( pos, galley. size ) , align) ;
436
- self . add_paint_cmd (
437
- layer,
438
- PaintCmd :: Rect {
439
- corner_radius : 0.0 ,
440
- fill : Some ( color:: gray ( 0 , 240 ) ) ,
441
- outline : Some ( LineStyle :: new ( 1.0 , color:: RED ) ) ,
442
- rect : rect. expand ( 2.0 ) ,
443
- } ,
444
- ) ;
445
- self . add_galley ( layer, rect. min , galley, text_style, Some ( color:: RED ) ) ;
446
- }
447
-
448
- pub fn debug_text ( & self , pos : Pos2 , text : impl Into < String > ) {
449
- let text = text. into ( ) ;
450
- let layer = Layer :: debug ( ) ;
451
- let align = ( Align :: Min , Align :: Min ) ;
452
- self . floating_text (
453
- layer,
454
- pos,
455
- text,
456
- TextStyle :: Monospace ,
457
- align,
458
- Some ( color:: YELLOW ) ,
459
- ) ;
460
- }
461
-
462
- pub fn debug_rect ( & self , rect : Rect , color : Color , name : impl Into < String > ) {
463
- let text = format ! ( "{} {:?}" , name. into( ) , rect) ;
464
- let layer = Layer :: debug ( ) ;
465
- self . add_paint_cmd (
466
- layer,
467
- PaintCmd :: Rect {
468
- corner_radius : 0.0 ,
469
- fill : None ,
470
- outline : Some ( LineStyle :: new ( 2.0 , color) ) ,
471
- rect,
472
- } ,
473
- ) ;
474
- let align = ( Align :: Min , Align :: Min ) ;
475
- let text_style = TextStyle :: Monospace ;
476
- self . floating_text ( layer, rect. min , text, text_style, align, Some ( color) ) ;
477
- }
478
-
479
- /// Show some text anywhere on screen.
480
- /// To center the text at the given position, use `align: (Center, Center)`.
481
- pub fn floating_text (
482
- & self ,
483
- layer : Layer ,
484
- pos : Pos2 ,
485
- text : String ,
486
- text_style : TextStyle ,
487
- align : ( Align , Align ) ,
488
- text_color : Option < Color > ,
489
- ) -> Rect {
490
- let font = & self . fonts ( ) [ text_style] ;
491
- let galley = font. layout_multiline ( text, f32:: INFINITY ) ;
492
- let rect = align_rect ( Rect :: from_min_size ( pos, galley. size ) , align) ;
493
- self . add_galley ( layer, rect. min , galley, text_style, text_color) ;
494
- rect
495
- }
496
-
497
- /// Already layed out text.
498
- pub fn add_galley (
499
- & self ,
500
- layer : Layer ,
501
- pos : Pos2 ,
502
- galley : font:: Galley ,
503
- text_style : TextStyle ,
504
- color : Option < Color > ,
505
- ) {
506
- let color = color. unwrap_or_else ( || self . style ( ) . text_color ) ;
507
- self . add_paint_cmd (
508
- layer,
509
- PaintCmd :: Text {
510
- pos,
511
- galley,
512
- text_style,
513
- color,
514
- } ,
515
- ) ;
516
- }
517
-
518
- pub fn add_paint_cmd ( & self , layer : Layer , paint_cmd : PaintCmd ) {
519
- self . graphics ( )
520
- . layer ( layer)
521
- . push ( ( Rect :: everything ( ) , paint_cmd) )
433
+ /// ## Painting
434
+ impl Context {
435
+ pub fn debug_painter ( self : & Arc < Self > ) -> Painter {
436
+ Painter :: new ( self . clone ( ) , Layer :: debug ( ) , self . rect ( ) )
522
437
}
523
438
}
524
439
0 commit comments