@@ -9,6 +9,7 @@ use crate::views::graph_view::controls::{ControlEvent, Controls};
99use crate :: views:: graph_view:: renderer:: Renderer ;
1010use crate :: views:: resize_observer:: ResizeObserver ;
1111use crate :: views:: utils:: get_element_of_type;
12+ use euclid:: approxeq:: ApproxEq ;
1213use euclid:: { Scale , Size2D , Transform2D , Vector2D } ;
1314use std:: cell:: RefCell ;
1415use std:: rc:: { Rc , Weak } ;
@@ -62,6 +63,12 @@ impl ClipSpace {
6263 }
6364}
6465
66+ /// The decay factor of the drag velocity, per second
67+ const TRANSLATION_DRAG_FACTOR : f32 = 0.5 ;
68+
69+ /// Zero velocity
70+ const TRANSLATION_VELOCITY_ZERO : Vector2D < f32 , ClipSpace > = Vector2D :: new ( 0.0 , 0.0 ) ;
71+
6572/// This represents the Canvas coordinate system, where the canvas is represented in [0, pixel size]
6673struct CanvasSpace ;
6774
@@ -86,6 +93,7 @@ pub struct GraphView {
8693 canvas_to_clip : Transform2D < f32 , CanvasSpace , ClipSpace > ,
8794 zoom : Scale < f32 , ClipSpace , ClipSpace > ,
8895 translation : Vector2D < f32 , ClipSpace > ,
96+ translation_velocity : Vector2D < f32 , ClipSpace > ,
8997 view_transform : [ f32 ; 9 ] ,
9098 renderer : Renderer ,
9199}
@@ -137,6 +145,7 @@ impl GraphView {
137145 canvas_to_clip : Transform2D :: identity ( ) ,
138146 zoom : Scale :: identity ( ) ,
139147 translation : ClipSpace :: CLIP_SPACE_OFFSET ,
148+ translation_velocity : Vector2D :: zero ( ) ,
140149 view_transform : [ 0.0 ; 9 ] ,
141150 renderer,
142151 } )
@@ -159,14 +168,28 @@ impl GraphView {
159168 self . frame_scheduler . schedule ( ) . unwrap ( ) ;
160169 }
161170
162- fn draw ( & mut self , _timestamp : Duration ) {
171+ fn draw ( & mut self , timestamp : Duration ) {
172+ // Update translation in case of flick
173+ self . translation += self . translation_velocity * timestamp. as_secs_f32 ( ) ;
174+ // self.translation_velocity *= TRANSLATION_DRAG_FACTOR * timestamp.as_secs_f32();
175+
176+ // Resize canvas if needed
163177 if self . canvas_needs_size_update {
164178 self . canvas_needs_size_update = false ;
165179 self . canvas . set_width ( self . canvas_size . width as u32 ) ;
166180 self . canvas . set_height ( self . canvas_size . height as u32 ) ;
167181 }
168182
169- self . renderer . draw ( & self . view_transform )
183+ // Draw
184+ self . renderer . draw ( & self . view_transform ) ;
185+
186+ // Schedule next draw if needed
187+ if !self
188+ . translation_velocity
189+ . approx_eq ( & TRANSLATION_VELOCITY_ZERO )
190+ {
191+ self . schedule_draw ( ) ;
192+ }
170193 }
171194
172195 pub fn set_data ( & mut self , graph : & Graph , active_state : BoardId ) {
@@ -188,11 +211,20 @@ impl GraphView {
188211
189212 fn handle_pointer_event ( & mut self , event : ControlEvent ) {
190213 match event {
191- ControlEvent :: Down ( _coordinates) => { }
192- ControlEvent :: Move ( coordinates) => {
193- self . handle_translation ( Vector2D :: new ( coordinates. x as f32 , -coordinates. y as f32 ) )
214+ ControlEvent :: Down ( _coordinates) => {
215+ // Cancel a flick if it was still going
216+ self . translation_velocity = Vector2D :: zero ( ) ;
217+ }
218+ ControlEvent :: Move ( delta_coordinates) => self . handle_translation ( Vector2D :: new (
219+ delta_coordinates. x as f32 ,
220+ -delta_coordinates. y as f32 ,
221+ ) ) ,
222+ ControlEvent :: Up ( velocity) => {
223+ // Store the current drag velocity so that the chart can be flicked
224+ self . translation_velocity = self
225+ . canvas_to_clip
226+ . transform_vector ( Vector2D :: new ( velocity. x as f32 , -velocity. y as f32 ) ) ;
194227 }
195- ControlEvent :: Up ( ) => { }
196228 }
197229 }
198230
0 commit comments