Skip to main content

Animation

Struct Animation 

Source
pub struct Animation {
    pub running: bool,
    /* private fields */
}
Expand description

This structure contains information about Animation progress with time. It does not contain any frames, making it possible to reuse those frames in multiple animations belonging to the same graphic. Also the same animation structure can be reused by multiple graphics with different frames making them appear as if they are running in sync.

Fields§

§running: bool

Implementations§

Source§

impl Animation

Source

pub fn new( running: bool, looping: bool, ordering: Vec<(usize, Timestamp)>, start_time: Timestamp, ) -> Animation

This method creates a new Animation instance. One can decide whether it should be running immediately or only after defined moment in time. One can also decide if it should run only once, or start over again and again.

Examples found in repository?
examples/example_5.rs (lines 435-440)
408fn build_graphic(cols: usize, rows: usize) -> (Graphic, usize) {
409    let start_frame = 0;
410    let mut library = HashMap::with_capacity(2);
411    library.insert(
412        start_frame,
413        vec![
414            Glyph::new(
415                '\u{2580}',
416                animaterm::Color::new_8bit(0, 5, 0),
417                animaterm::Color::new_8bit(0, 0, 5),
418                false,
419                true,
420                false,
421                false,
422                false,
423                false,
424                false,
425                false,
426                false,
427                false,
428            );
429            rows * cols
430        ],
431    );
432    let mut animations = HashMap::new();
433    animations.insert(
434        0,
435        Animation::new(
436            false,
437            true,
438            vec![(1, Timestamp::new(0, 500)), (0, Timestamp::new(0, 500))],
439            Timestamp::now(),
440        ),
441    );
442
443    let mut gr = Graphic::new(cols, rows, start_frame, library, Some(animations));
444    let pid = gr
445        .add_to_library(vec![
446            Glyph::new(
447                '\u{2580}',
448                animaterm::Color::new_truecolor(0, 255, 255),
449                animaterm::Color::new_truecolor(0, 0, 255),
450                false,
451                true,
452                false,
453                false,
454                false,
455                false,
456                false,
457                false,
458                false,
459                false,
460            );
461            rows * cols
462        ])
463        .unwrap();
464    (gr, pid)
465}
More examples
Hide additional examples
examples/example_2.rs (line 52)
5fn main() {
6    let mut mgr = Manager::new(true, None, None, None, None, None);
7
8    let mut library = HashMap::with_capacity(2);
9    let cols = 10;
10    let rows = 5;
11    let start_frame = 0;
12    let glyph_1 = Glyph::new(
13        '\u{2580}',
14        Color::new_8bit(0, 0, 5),
15        Color::new_8bit(5, 5, 0),
16        false,
17        true,
18        false,
19        false,
20        false,
21        false,
22        false,
23        false,
24        false,
25        false,
26    );
27    let glyph_2 = Glyph::new(
28        '\u{258C}',
29        Color::new_truecolor(255, 255, 255),
30        Color::new_truecolor(255, 0, 0),
31        false,
32        true,
33        false,
34        false,
35        false,
36        false,
37        false,
38        false,
39        false,
40        false,
41    );
42
43    library.insert(start_frame, vec![glyph_1; rows * cols]);
44    library.insert(start_frame + 1, vec![glyph_2; rows * cols]);
45    let ordering = vec![
46        (start_frame, Timestamp::new(0, 500)),
47        (start_frame + 1, Timestamp::new(0, 500)),
48    ];
49    let running = false;
50    let looping = true;
51    let start_time = Timestamp::now();
52    let animation = Animation::new(running, looping, ordering, start_time);
53    let mut animations = HashMap::new();
54    let anim_id = 0;
55    animations.insert(anim_id, animation);
56    let mut gr = Graphic::new(cols, rows, start_frame, library, Some(animations));
57    let fast_ordering = vec![
58        (start_frame, Timestamp::new(0, 200)),
59        (start_frame + 1, Timestamp::new(0, 200)),
60    ];
61    let mut fast_anim_id = anim_id;
62    if let Some(id) = gr.add_animation(Animation::new(running, looping, fast_ordering, start_time))
63    {
64        fast_anim_id = id;
65    };
66
67    let layer = 0;
68    let offset = (15, 5);
69    let graphic_id = mgr.add_graphic(gr, layer, offset).unwrap();
70    let screen_size = mgr.screen_size();
71    let title = "Navigation help".to_string();
72    let text = "Press 0 to set current frame to 0\n Press 1 to set current frame to 1\n Press a|Shift+a|Ctrl+a start anim \n Press s or Shift+s stop animation\n\n Press q or Shift+q to quit\n".to_string();
73    let mbox = message_box(Some(title), text, Glyph::default(), 80, 17);
74    let mbid = mgr
75        .add_graphic(mbox, 1, (1, screen_size.1 as isize - 8))
76        .unwrap();
77    mgr.set_graphic(mbid, 0, true);
78
79    let var_ordering = vec![
80        (start_frame, Timestamp::new(0, 400)),
81        (start_frame + 1, Timestamp::new(0, 400)),
82        (start_frame, Timestamp::new(0, 300)),
83        (start_frame + 1, Timestamp::new(0, 300)),
84        (start_frame, Timestamp::new(0, 200)),
85        (start_frame + 1, Timestamp::new(0, 200)),
86        (start_frame, Timestamp::new(0, 100)),
87        (start_frame + 1, Timestamp::new(0, 100)),
88        (start_frame, Timestamp::new(0, 200)),
89        (start_frame + 1, Timestamp::new(0, 200)),
90        (start_frame, Timestamp::new(0, 300)),
91        (start_frame + 1, Timestamp::new(0, 300)),
92    ];
93    // let mut all_results_read = false;
94    // while !all_results_read {
95    //     let result = mgr.read_result();
96    //     match result {
97    //         Ok(AnimOk::AllResultsRead) => all_results_read = true,
98    //         _ => continue,
99    //     }
100    // }
101    mgr.add_animation(
102        graphic_id,
103        Animation::new(false, true, var_ordering, Timestamp::now()),
104    );
105    let mut var_anim_id = 0;
106
107    if let Ok(AnimOk::AnimationAdded(anim_id)) = mgr.read_result() {
108        var_anim_id = anim_id;
109    }
110
111    let mut keep_running = true;
112    while keep_running {
113        if let Some(key) = mgr.read_key() {
114            match key {
115                Key::Zero => mgr.set_graphic(graphic_id, start_frame, true),
116                Key::One => mgr.set_graphic(graphic_id, start_frame + 1, true),
117                Key::A => mgr.start_animation(graphic_id, anim_id),
118                Key::ShiftA => mgr.start_animation(graphic_id, fast_anim_id),
119                Key::P => mgr.pause_animation(graphic_id),
120                Key::CtrlP => mgr.pause_animation(graphic_id),
121                Key::ShiftP => mgr.pause_animation_on_frame(graphic_id, start_frame),
122                Key::CtrlA => mgr.start_animation(graphic_id, var_anim_id),
123                Key::S | Key::ShiftS => mgr.stop_animation(graphic_id),
124                Key::Q | Key::ShiftQ => {
125                    keep_running = false;
126                }
127                _ => continue,
128            }
129        }
130    }
131
132    mgr.terminate();
133}
Source

pub fn start(&mut self, t: Timestamp)

This method is used to start an Animation if it is not already running.

Source

pub fn restart(&mut self, t: Timestamp)

Use this method to start an Animation from beginning frame.

Source

pub fn pause(&mut self, t: Timestamp)

In order to pause an Animation, call this method.

Source

pub fn freeze(&mut self, t: Timestamp)

Prevent an Animation from switching to the next frame for given amount of time.

Source

pub fn pause_on_frame(&mut self, frame_id: usize)

Pause an Animation when given frame is being displayed.

Source

pub fn stop(&mut self)

Stop an Animation.

Source

pub fn update(&mut self, dtime: Timestamp) -> Option<(usize, bool)>

This method is being called internally to check if an Animation should be updated on screen.

Trait Implementations§

Source§

impl Debug for Animation

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.