bilzaa2dutil/
basecounter.rs

1use std::ops::Mul;
2
3// mod attributesenum;
4use super::{AttributesEnum,AnimateResponses,percent_to_value,percentage,Animatable};
5
6#[derive(Copy)]
7#[derive(Clone)]
8#[derive(PartialEq)]
9#[derive(Debug)]
10/// This is one of the Object in my library that implements the 'Animatable' trait. In future there will be more
11pub struct BaseCounter {
12    from_time:u128,
13    to_time:u128,
14    from:u128,
15    to:u128,
16    is_reverse:bool, 
17    animation_duration:u128,
18    animation_distance:u128,
19    attr_to_animate:AttributesEnum,
20}
21//==========================================
22
23impl BaseCounter{
24    pub fn new(from_time:f64,to_time:f64,from:u128,to:u128,attr_to_animate:AttributesEnum)->Option<BaseCounter>{
25    assert!(from_time < to_time,"From time can not be bigger than To time");   
26    assert!(from < 5000,"From value is too large");
27    assert!(to < 5000,"To value is too large");
28        let from_time_millis = from_time.mul(1000_f64);
29        let to_time_millis = to_time.mul(1000_f64);
30            Some(BaseCounter {
31                from_time : from_time_millis as u128,
32                to_time : to_time_millis as u128,
33                from,
34                to,
35                is_reverse: is_reverse(from, to),
36                animation_duration : duration(to_time_millis as u128, from_time_millis as u128), 
37                animation_distance:distance(from, to),
38                attr_to_animate:attr_to_animate,
39            })
40    } 
41    pub fn is_time_valid(&self,time_ms:u128)->Option<bool>{
42        if time_ms > self.to_time || time_ms < self.from_time {
43            return None;
44           }else {
45               Some(true)
46           }       
47    }
48    pub fn time_lapsed(&self,time_ms:u128)->u128{
49        //--time is valid check is seperate.       
50        time_ms - self.from_time
51    }      
52    //----we had animate here but now nothing-!!!!!      
53}//end of impl block
54//----public API
55fn is_reverse(from:u128,to:u128)->bool{
56    if from < to {
57        return false
58    }else {
59        return true
60    }
61}
62fn duration(to_time:u128,from_time:u128)->u128{
63    if from_time > to_time {
64        return from_time - to_time
65    }else {
66        return to_time - from_time
67    }
68}
69fn distance (from:u128,to:u128)->u128{
70    if from > to {
71        return from - to
72    }else {
73        return to - from
74    }
75}
76
77impl Animatable for BaseCounter{
78
79    fn animate(&self, time_ms:u128) ->Option<AnimateResponses> {
80        /////////////////////////////
81        self.is_time_valid(time_ms)?;
82        
83        let time_lapsed = self.time_lapsed(time_ms);
84        
85        let time_perc_lapsed = percent_to_value(self.animation_duration as f64, time_lapsed as f64)?;
86          
87        let per:Option<f64> = percentage(self.animation_distance as f64,time_perc_lapsed);
88            
89            match per {
90                Some(y)=> {
91                    if self.is_reverse == true {
92                        let u = self.from  - y as u128;
93                        let f = self.from as f64  - y ;
94                        return Some(AnimateResponses::U128f64(u,f))
95                    }else {
96                        let u = y  as u128 + self.from ;
97                        let f = y  + self.from as f64;
98                        return Some(AnimateResponses::U128f64(u,f))
99                    }
100                },
101                None=> return None,   
102            }       
103        /////////////////////////////
104    }
105    fn get_attr_to_animate(&self)->AttributesEnum{
106        self.attr_to_animate
107    }
108    
109}//impl Animatable for BaseCounter