diff options
author | Akshay <[email protected]> | 2019-11-29 17:35:54 +0000 |
---|---|---|
committer | Akshay <[email protected]> | 2019-11-29 17:35:54 +0000 |
commit | 9a8fd65cb0f110e0e851315d24e7e25d55a6fe85 (patch) | |
tree | 38ae8255894e38a75fbaabffe6c57ea9d695a531 /src | |
parent | 7a0ec1aef055fc7522dab43abcf2ef4376778484 (diff) |
add; configurable animations
Diffstat (limited to 'src')
-rw-r--r-- | src/animations.rs | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/src/animations.rs b/src/animations.rs new file mode 100644 index 0000000..634986b --- /dev/null +++ b/src/animations.rs | |||
@@ -0,0 +1,48 @@ | |||
1 | use crate::IndicatorConfig; | ||
2 | use std::thread; | ||
3 | use std::time::Duration; | ||
4 | |||
5 | pub struct Animation { | ||
6 | duration_per_frame: Duration, | ||
7 | frames: Vec<xcb::Arc>, | ||
8 | clear: bool, | ||
9 | } | ||
10 | |||
11 | impl Animation { | ||
12 | pub fn circles( | ||
13 | config: IndicatorConfig, | ||
14 | function: Box<dyn DoubleEndedIterator<Item = u16>>, | ||
15 | clear: bool, | ||
16 | ) -> Animation { | ||
17 | let no_of_frames = config.framerate; | ||
18 | let duration_per_frame = | ||
19 | Duration::from_millis(config.duration).div_f64(no_of_frames as f64); | ||
20 | let frames = function | ||
21 | .map(|i| { | ||
22 | xcb::Arc::new( | ||
23 | ((config.max_size as f32) / (2. * no_of_frames as f32) * i as f32) as i16, // x | ||
24 | ((config.max_size as f32) / (2. * no_of_frames as f32) * i as f32) as i16, // y | ||
25 | (config.max_size) - (config.max_size / no_of_frames) * i as u16, // width | ||
26 | (config.max_size) - (config.max_size / no_of_frames) * i as u16, // height | ||
27 | 0, // start angle | ||
28 | 360 << 6, // end angle | ||
29 | ) | ||
30 | }) | ||
31 | .collect::<Vec<xcb::Arc>>(); | ||
32 | return Animation { | ||
33 | duration_per_frame, | ||
34 | frames, | ||
35 | clear, | ||
36 | }; | ||
37 | } | ||
38 | pub fn play(&self, conn: &xcb::Connection, win: u32, gfx_ctx: u32) { | ||
39 | for frame in &self.frames { | ||
40 | xcb::poly_arc(&conn, win, gfx_ctx, &[*frame]); | ||
41 | conn.flush(); | ||
42 | if self.clear { | ||
43 | xcb::clear_area(&conn, true, win, frame.x(), frame.y(), 2000, 2000); | ||
44 | } | ||
45 | thread::sleep(self.duration_per_frame); | ||
46 | } | ||
47 | } | ||
48 | } | ||