@@ -2,63 +2,47 @@ use egui::{
22 containers:: Frame ,
33 emath,
44 epaint:: { self , PathStroke } ,
5- pos2, vec2, Color32 , Pos2 , Rect ,
5+ pos2, vec2, Color32 , Pos2 , Rect , Ui ,
66} ;
77
88use crate :: { app_state:: AppState , stats:: FPS_SAMPLES } ;
99
1010const WIDTH : f32 = 1.0 ;
1111
12- #[ derive( Default ) ]
13- pub struct FpsGraph {
14- pub open : bool ,
15- }
16-
17- impl FpsGraph {
18- pub fn ui ( & mut self , ctx : & egui:: Context , app_state : & mut AppState ) {
19- egui:: Window :: new ( "FPS" )
20- . open ( & mut self . open )
21- . default_pos ( [ 1390.0 , 40.0 ] )
22- . default_size ( vec2 ( 500.0 , 20.0 ) )
23- . vscroll ( false )
24- . show ( ctx, |ui| {
25- Frame :: canvas ( ui. style ( ) ) . show ( ui, |ui| {
26- ui. ctx ( ) . request_repaint ( ) ;
27-
28- let desired_size = vec2 ( ui. available_width ( ) , ui. available_height ( ) ) ;
29- let ( _id, rect) = ui. allocate_space ( desired_size) ;
30-
31- let to_screen = emath:: RectTransform :: from_to (
32- Rect :: from_x_y_ranges ( 0.0 ..=1.0 , -1.0 ..=1.0 ) ,
33- rect,
34- ) ;
35-
36- let mut shapes = vec ! [ ] ;
37-
38- let points: Vec < Pos2 > = app_state
39- . stats
40- . get_times ( )
41- . skip ( FPS_SAMPLES - rect. width ( ) as usize )
42- . enumerate ( )
43- . map ( |( x, y) | {
44- to_screen
45- * pos2 (
46- // Draw all the {ui.available_width()} last frame timings.
47- x as f32 / rect. width ( ) ,
48- // Draw y in relation to a maximum of 20 ms.
49- -y. as_secs_f32 ( ) * 1000.0 / 20.0 ,
50- )
51- + vec2 ( 0.0 , rect. height ( ) / 2.0 )
52- } )
53- . collect ( ) ;
54-
55- shapes. push ( epaint:: Shape :: line (
56- points,
57- PathStroke :: new ( WIDTH , Color32 :: WHITE ) ,
58- ) ) ;
59-
60- ui. painter ( ) . extend ( shapes) ;
61- } ) ;
62- } ) ;
63- }
12+ pub fn view_fps ( ui : & mut Ui , app_state : & mut AppState ) {
13+ Frame :: canvas ( ui. style ( ) ) . show ( ui, |ui| {
14+ ui. ctx ( ) . request_repaint ( ) ;
15+
16+ let desired_size = vec2 ( ui. available_width ( ) , ui. available_height ( ) ) ;
17+ let ( _id, rect) = ui. allocate_space ( desired_size) ;
18+
19+ let to_screen =
20+ emath:: RectTransform :: from_to ( Rect :: from_x_y_ranges ( 0.0 ..=1.0 , -1.0 ..=1.0 ) , rect) ;
21+
22+ let mut shapes = vec ! [ ] ;
23+
24+ let points: Vec < Pos2 > = app_state
25+ . stats
26+ . get_times ( )
27+ . skip ( FPS_SAMPLES - rect. width ( ) as usize )
28+ . enumerate ( )
29+ . map ( |( x, y) | {
30+ to_screen
31+ * pos2 (
32+ // Draw all the {ui.available_width()} last frame timings.
33+ x as f32 / rect. width ( ) ,
34+ // Draw y in relation to a maximum of 20 ms.
35+ -y. as_secs_f32 ( ) * 1000.0 / 20.0 ,
36+ )
37+ + vec2 ( 0.0 , rect. height ( ) / 2.0 )
38+ } )
39+ . collect ( ) ;
40+
41+ shapes. push ( epaint:: Shape :: line (
42+ points,
43+ PathStroke :: new ( WIDTH , Color32 :: WHITE ) ,
44+ ) ) ;
45+
46+ ui. painter ( ) . extend ( shapes) ;
47+ } ) ;
6448}
0 commit comments