animaterm

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)
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
fn build_graphic(cols: usize, rows: usize) -> (Graphic, usize) {
    let start_frame = 0;
    let mut library = HashMap::with_capacity(2);
    library.insert(
        start_frame,
        vec![
            Glyph::new(
                '\u{2580}',
                animaterm::Color::new_8bit(0, 5, 0),
                animaterm::Color::new_8bit(0, 0, 5),
                false,
                true,
                false,
                false,
                false,
                false,
                false,
                false,
                false,
                false,
            );
            rows * cols
        ],
    );
    let mut animations = HashMap::new();
    animations.insert(
        0,
        Animation::new(
            false,
            true,
            vec![(1, Timestamp::new(0, 500)), (0, Timestamp::new(0, 500))],
            Timestamp::now(),
        ),
    );

    let mut gr = Graphic::new(cols, rows, start_frame, library, Some(animations));
    let pid = gr
        .add_to_library(vec![
            Glyph::new(
                '\u{2580}',
                animaterm::Color::new_truecolor(0, 255, 255),
                animaterm::Color::new_truecolor(0, 0, 255),
                false,
                true,
                false,
                false,
                false,
                false,
                false,
                false,
                false,
                false,
            );
            rows * cols
        ])
        .unwrap();
    (gr, pid)
}
More examples
Hide additional examples
examples/example_2.rs (line 52)
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
fn main() {
    let mut mgr = Manager::new(true, None, None, None, None, None);

    let mut library = HashMap::with_capacity(2);
    let cols = 10;
    let rows = 5;
    let start_frame = 0;
    let glyph_1 = Glyph::new(
        '\u{2580}',
        Color::new_8bit(0, 0, 5),
        Color::new_8bit(5, 5, 0),
        false,
        true,
        false,
        false,
        false,
        false,
        false,
        false,
        false,
        false,
    );
    let glyph_2 = Glyph::new(
        '\u{258C}',
        Color::new_truecolor(255, 255, 255),
        Color::new_truecolor(255, 0, 0),
        false,
        true,
        false,
        false,
        false,
        false,
        false,
        false,
        false,
        false,
    );

    library.insert(start_frame, vec![glyph_1; rows * cols]);
    library.insert(start_frame + 1, vec![glyph_2; rows * cols]);
    let ordering = vec![
        (start_frame, Timestamp::new(0, 500)),
        (start_frame + 1, Timestamp::new(0, 500)),
    ];
    let running = false;
    let looping = true;
    let start_time = Timestamp::now();
    let animation = Animation::new(running, looping, ordering, start_time);
    let mut animations = HashMap::new();
    let anim_id = 0;
    animations.insert(anim_id, animation);
    let mut gr = Graphic::new(cols, rows, start_frame, library, Some(animations));
    let fast_ordering = vec![
        (start_frame, Timestamp::new(0, 200)),
        (start_frame + 1, Timestamp::new(0, 200)),
    ];
    let mut fast_anim_id = anim_id;
    if let Some(id) = gr.add_animation(Animation::new(running, looping, fast_ordering, start_time))
    {
        fast_anim_id = id;
    };

    let layer = 0;
    let offset = (15, 5);
    let graphic_id = mgr.add_graphic(gr, layer, offset).unwrap();
    let screen_size = mgr.screen_size();
    let title = "Navigation help".to_string();
    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();
    let mbox = message_box(Some(title), text, Glyph::default(), 80, 17);
    let mbid = mgr
        .add_graphic(mbox, 1, (1, screen_size.1 as isize - 8))
        .unwrap();
    mgr.set_graphic(mbid, 0, true);

    let var_ordering = vec![
        (start_frame, Timestamp::new(0, 400)),
        (start_frame + 1, Timestamp::new(0, 400)),
        (start_frame, Timestamp::new(0, 300)),
        (start_frame + 1, Timestamp::new(0, 300)),
        (start_frame, Timestamp::new(0, 200)),
        (start_frame + 1, Timestamp::new(0, 200)),
        (start_frame, Timestamp::new(0, 100)),
        (start_frame + 1, Timestamp::new(0, 100)),
        (start_frame, Timestamp::new(0, 200)),
        (start_frame + 1, Timestamp::new(0, 200)),
        (start_frame, Timestamp::new(0, 300)),
        (start_frame + 1, Timestamp::new(0, 300)),
    ];
    // let mut all_results_read = false;
    // while !all_results_read {
    //     let result = mgr.read_result();
    //     match result {
    //         Ok(AnimOk::AllResultsRead) => all_results_read = true,
    //         _ => continue,
    //     }
    // }
    mgr.add_animation(
        graphic_id,
        Animation::new(false, true, var_ordering, Timestamp::now()),
    );
    let mut var_anim_id = 0;

    if let Ok(AnimOk::AnimationAdded(anim_id)) = mgr.read_result() {
        var_anim_id = anim_id;
    }

    let mut keep_running = true;
    while keep_running {
        if let Some(key) = mgr.read_key() {
            match key {
                Key::Zero => mgr.set_graphic(graphic_id, start_frame, true),
                Key::One => mgr.set_graphic(graphic_id, start_frame + 1, true),
                Key::A => mgr.start_animation(graphic_id, anim_id),
                Key::ShiftA => mgr.start_animation(graphic_id, fast_anim_id),
                Key::P => mgr.pause_animation(graphic_id),
                Key::CtrlP => mgr.pause_animation(graphic_id),
                Key::ShiftP => mgr.pause_animation_on_frame(graphic_id, start_frame),
                Key::CtrlA => mgr.start_animation(graphic_id, var_anim_id),
                Key::S | Key::ShiftS => mgr.stop_animation(graphic_id),
                Key::Q | Key::ShiftQ => {
                    keep_running = false;
                }
                _ => continue,
            }
        }
    }

    mgr.terminate();
}
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.