matrix-gui 0.1.0

embedded-graphics based GUI framework, use region-based freeform layout.
Documentation
1
2
3
4
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
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
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
//! Animation subsystem for the matrix_gui framework.
//!
//! This module provides a lightweight animation system inspired by LVGL 8,
//! designed for immediate-mode embedded GUI applications.
//!
//! # Features
//!
//! - Multiple easing functions using integer-only math (no floating point)
//! - Support for value animations with callbacks
//! - Animation management with play, pause, stop controls
//! - Memory-efficient design suitable for embedded systems
//! - `no_std` compatible
//!
//! # Core Components
//!
//! - [`Anim`]: Animation definition with start/end values, duration, and easing
//! - [`Easing`]: Easing functions for smooth animations (integer-based)
//! - [`AnimManager`]: Manages multiple active animations
//! - [`AnimCallback`]: Callback trait for animation value updates

use core::cell::Cell;
use core::fmt::Debug;
use core::time::Duration;

/// Scaling factor for fixed-point calculations.
/// Values are scaled to 0..=ANIM_SCALE range for integer math.
pub const ANIM_SCALE: i32 = 1024;

/// Easing functions for animations.
///
/// These functions define how animation progress changes over time,
/// creating smooth and natural-looking motion.
///
/// All calculations use integer-only math with fixed-point arithmetic.
/// The input progress is in range [0, ANIM_SCALE] and output is also
/// in range [0, ANIM_SCALE].
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum Easing {
    /// Linear interpolation - constant speed.
    Linear,
    /// Ease-in - slow start, fast end.
    EaseIn,
    /// Ease-out - fast start, slow end.
    #[default]
    EaseOut,
    /// Ease-in-out - slow start and end.
    EaseInOut,
}

impl Easing {
    /// Calculates the eased value for a given progress using integer math.
    ///
    /// # Arguments
    ///
    /// * `progress` - Animation progress in range [0, ANIM_SCALE]
    ///
    /// # Returns
    ///
    /// The eased progress value in range [0, ANIM_SCALE]
    /// (some easing functions like elastic may slightly exceed this range).
    pub fn calc(&self, progress: i32) -> i32 {
        let t = progress.clamp(0, ANIM_SCALE);
        match self {
            Easing::Linear => t,

            Easing::EaseIn => mul_div(t, t, ANIM_SCALE),

            Easing::EaseOut => {
                let inv_t = ANIM_SCALE - t;
                ANIM_SCALE - mul_div(inv_t, inv_t, ANIM_SCALE)
            }

            Easing::EaseInOut => {
                if t < ANIM_SCALE / 2 {
                    2 * mul_div(t, t, ANIM_SCALE)
                } else {
                    let inv_t = ANIM_SCALE - t;
                    ANIM_SCALE - 2 * mul_div(inv_t, inv_t, ANIM_SCALE)
                }
            }
        }
    }
}

/// Safe multiplication with division, avoiding overflow.
/// Returns (a * b / c) with proper handling.
#[inline]
const fn mul_div(a: i32, b: i32, c: i32) -> i32 {
    (a * b) / c
}

/// Animation playback state.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum AnimState {
    /// Animation is not playing.
    #[default]
    Stopped,
    /// Animation is playing.
    Playing,
    /// Animation is paused.
    Paused,
}

/// Unique identifier for an animation.
pub type AnimId = u16;

/// Animation playback options.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct AnimOptions {
    /// Number of times to repeat the animation (0 = infinite).
    pub repeat_count: u16,
    /// Whether to reverse the animation on each repeat.
    pub reverse: bool,
    /// Delay before starting the animation.
    pub start_delay: Duration,
    /// Whether to play the animation in reverse initially.
    pub play_backward: bool,
}

impl Default for AnimOptions {
    fn default() -> Self {
        Self {
            repeat_count: 1,
            reverse: false,
            start_delay: Duration::ZERO,
            play_backward: false,
        }
    }
}

impl AnimOptions {
    /// Creates new animation options with default values.
    pub const fn new() -> Self {
        Self {
            repeat_count: 1,
            reverse: false,
            start_delay: Duration::ZERO,
            play_backward: false,
        }
    }

    /// Sets the repeat count (0 = infinite).
    pub const fn with_repeat(mut self, count: u16) -> Self {
        self.repeat_count = count;
        self
    }

    /// Enables reverse playback on repeat.
    pub const fn with_reverse(mut self, reverse: bool) -> Self {
        self.reverse = reverse;
        self
    }

    /// Sets the start delay.
    pub const fn with_start_delay(mut self, delay: Duration) -> Self {
        self.start_delay = delay;
        self
    }

    /// Sets whether to play backward initially.
    pub const fn with_play_backward(mut self, backward: bool) -> Self {
        self.play_backward = backward;
        self
    }
}

/// Animation definition.
///
/// This struct defines an animation with start/end values, duration,
/// easing function, and callback.
#[derive(Debug, Clone)]
pub struct Anim {
    /// Starting value of the animation.
    pub start_value: i32,
    /// Ending value of the animation.
    pub end_value: i32,
    /// Duration of the animation.
    pub duration: Duration,
    /// Easing function for the animation.
    pub easing: Easing,
    /// Animation options.
    pub options: AnimOptions,
}

impl Anim {
    /// Creates a new animation with the given parameters.
    ///
    /// # Arguments
    ///
    /// * `start_value` - Starting value
    /// * `end_value` - Ending value
    /// * `duration` - Duration of the animation
    /// * `callback` - Callback for value updates
    pub const fn new(start_value: i32, end_value: i32, duration: Duration) -> Self {
        Self {
            start_value,
            end_value,
            duration,
            easing: Easing::Linear,
            options: AnimOptions::new(),
        }
    }

    /// Sets the easing function.
    pub const fn with_easing(mut self, easing: Easing) -> Self {
        self.easing = easing;
        self
    }

    /// Sets the animation options.
    pub const fn with_options(mut self, options: AnimOptions) -> Self {
        self.options = options;
        self
    }

    /// Sets whether to reverse on repeat.
    pub const fn with_reverse(mut self, reverse: bool) -> Self {
        self.options.reverse = reverse;
        self
    }

    /// Sets the repeat count (0 = infinite).
    pub const fn with_repeat(mut self, count: u16) -> Self {
        self.options.repeat_count = count;
        self
    }

    /// Sets the start delay.
    pub const fn with_start_delay(mut self, delay: Duration) -> Self {
        self.options.start_delay = delay;
        self
    }

    /// Calculates the current value based on progress.
    ///
    /// # Arguments
    ///
    /// * `progress` - Animation progress in range [0, ANIM_SCALE]
    ///
    /// # Returns
    ///
    /// The interpolated value between start and end.
    pub fn calc_value(&self, progress: i32) -> i32 {
        let eased_progress = self.easing.calc(progress);
        let range = self.end_value - self.start_value;
        self.start_value + mul_div(range, eased_progress, ANIM_SCALE)
    }
}

const INVALID_ANIM_ID: AnimId = AnimId::MAX;

/// Internal state for an active animation.
#[derive(Debug, Clone)]
pub struct AnimInstance {
    /// Animation ID
    id: AnimId,
    /// Current playback state.
    state: AnimState,
    /// The animation definition.
    anim: Anim,
    /// Current time elapsed in the animation.
    elapsed: Duration,
    /// Current repeat count.
    current_repeat: u16,
    /// Whether currently playing in reverse.
    is_reversed: bool,
    /// Whether start delay has passed.
    delay_passed: bool,
}

impl AnimInstance {
    const fn new() -> Self {
        Self {
            id: INVALID_ANIM_ID,
            state: AnimState::Playing,
            anim: Anim::new(0, 0, Duration::ZERO),
            elapsed: Duration::ZERO,
            current_repeat: 0,
            is_reversed: false,
            delay_passed: false,
        }
    }
}

#[derive(Debug)]
pub struct AnimStatus(Cell<Option<i32>>);
impl AnimStatus {
    pub fn new() -> Self {
        Self(Cell::new(None))
    }
    pub fn set(&self, value: i32) {
        self.0.set(Some(value));
    }
    pub fn take(&self) -> Option<i32> {
        self.0.take()
    }
    pub fn get(&self) -> Option<i32> {
        self.0.get()
    }
}

pub struct Animations<const N: usize> {
    animations: [AnimInstance; N],
    anim_status: [AnimStatus; N],
}

impl<const N: usize> Animations<N> {
    /// Creates a new animation manager.
    pub fn new() -> Self {
        let animations = core::array::from_fn(|_| AnimInstance::new());
        let anim_status = core::array::from_fn(|_| AnimStatus::new());
        Self {
            animations,
            anim_status,
        }
    }

    pub fn split(self) -> ([AnimInstance; N], [AnimStatus; N]) {
        (self.animations, self.anim_status)
    }
}

/// Animation manager that handles multiple animations.
///
/// This struct manages the lifecycle and playback of multiple animations.
/// It is designed to be memory-efficient for embedded systems.
///
/// # Type Parameters
///
/// * `C` - The callback type that implements `AnimCallback`
/// * `N` - The maximum number of simultaneous animations
pub struct AnimManager<'a> {
    /// Active animation instances.
    animations: &'a mut [AnimInstance],
    anim_status: &'a [AnimStatus],
    /// Next animation ID.
    next_id: AnimId,
}

impl<'a> AnimManager<'a> {
    /// Creates a new animation manager.
    pub const fn new(animations: &'a mut [AnimInstance], anim_status: &'a [AnimStatus]) -> Self {
        Self {
            animations,
            anim_status,
            next_id: 0, //index from 0 to animations.len() - 1
        }
    }

    /// Adds an animation to the manager.
    ///
    /// # Arguments
    ///
    /// * `anim` - The animation to add
    ///
    /// # Returns
    ///
    /// The animation ID, or `None` if the manager is full.
    pub fn add(&mut self, anim: Anim) -> Option<AnimId> {
        if self.next_id as usize >= self.animations.len() {
            return None;
        }
        let id = self.next_id;
        self.next_id = self.next_id.wrapping_add(1);

        let start_value = anim.start_value;
        let anim_instance = AnimInstance {
            id,
            state: AnimState::Stopped,
            anim,
            elapsed: Duration::ZERO,
            current_repeat: 0,
            is_reversed: false,
            delay_passed: false,
        };

        if let Some(instance) = self.animations.get_mut(id as usize) {
            if let Some(status) = self.anim_status.get(id as usize) {
                status.set(start_value);
                *instance = anim_instance;
                return Some(id);
            }
        };

        None
    }

    /// Removes an animation from the manager.
    ///
    /// # Arguments
    ///
    /// * `id` - The animation ID to remove
    ///
    /// # Returns
    ///
    /// `true` if the animation was found and removed.
    pub fn remove(&mut self, id: AnimId) -> bool {
        if let Some(instance) = self.animations.get_mut(id as usize) {
            instance.id = INVALID_ANIM_ID;
            return true;
        }

        false
    }

    /// Starts playing an animation.
    ///
    /// # Arguments
    ///
    /// * `id` - The animation ID to play
    ///
    /// # Returns
    ///
    /// `true` if the animation was found and started.
    pub fn play(&mut self, id: AnimId) -> bool {
        if let Some(instance) = self.animations.get_mut(id as usize) {
            if instance.id == id {
                instance.state = AnimState::Playing;
                instance.elapsed = Duration::ZERO;
                instance.current_repeat = 0;
                instance.is_reversed = instance.anim.options.play_backward;
                instance.delay_passed = instance.anim.options.start_delay.is_zero();
                return true;
            }
        }
        false
    }

    /// Pauses an animation.
    ///
    /// # Arguments
    ///
    /// * `id` - The animation ID to pause
    ///
    /// # Returns
    ///
    /// `true` if the animation was found and paused.
    pub fn pause(&mut self, id: AnimId) -> bool {
        if let Some(instance) = self.animations.get_mut(id as usize) {
            if instance.id == id && instance.state == AnimState::Playing {
                instance.state = AnimState::Paused;
                return true;
            }
        }
        false
    }

    /// Resumes a paused animation.
    ///
    /// # Arguments
    ///
    /// * `id` - The animation ID to resume
    ///
    /// # Returns
    ///
    /// `true` if the animation was found and resumed.
    pub fn resume(&mut self, id: AnimId) -> bool {
        if let Some(instance) = self.animations.get_mut(id as usize) {
            if instance.id == id && instance.state == AnimState::Paused {
                instance.state = AnimState::Playing;
                return true;
            }
        }
        false
    }

    /// Stops an animation.
    ///
    /// # Arguments
    ///
    /// * `id` - The animation ID to stop
    ///
    /// # Returns
    ///
    /// `true` if the animation was found and stopped.
    pub fn stop(&mut self, id: AnimId) -> bool {
        if let Some(instance) = self.animations.get_mut(id as usize) {
            if instance.id == id {
                instance.state = AnimState::Stopped;
                instance.elapsed = Duration::ZERO;
                instance.current_repeat = 0;
                return true;
            }
        }
        false
    }

    /// Gets the state of an animation.
    ///
    /// # Arguments
    ///
    /// * `id` - The animation ID
    ///
    /// # Returns
    ///
    /// The animation state, or `None` if not found.
    pub fn get_state(&self, id: AnimId) -> Option<AnimState> {
        if let Some(instance) = self.animations.get(id as usize) {
            if instance.id == id {
                return Some(instance.state);
            }
        }
        None
    }

    /// Updates all active animations.
    ///
    /// This method should be called regularly (e.g., in the main loop)
    /// with the elapsed time since the last update.
    ///
    /// # Arguments
    ///
    /// * `elapsed` - Time elapsed since the last update
    pub fn tick(&mut self, elapsed: Duration) {
        for (idx, instance) in self.animations.iter_mut().enumerate() {
            if idx >= self.next_id as usize {
                break;
            }
            if instance.id == INVALID_ANIM_ID || instance.state != AnimState::Playing {
                continue;
            }
            let Some(status) = self.anim_status.get(instance.id as usize) else {
                continue;
            };

            // Handle start delay
            if !instance.delay_passed {
                instance.elapsed += elapsed;
                if instance.elapsed >= instance.anim.options.start_delay {
                    instance.delay_passed = true;
                    instance.elapsed = Duration::ZERO;
                } else {
                    continue;
                }
            } else {
                instance.elapsed += elapsed;
            }

            let duration = instance.anim.duration;
            let duration_ms = duration.as_millis() as u64;
            let elapsed_ms = instance.elapsed.as_millis() as u64;

            // Calculate progress
            let progress = if duration_ms == 0 {
                ANIM_SCALE
            } else {
                let effective_elapsed = if instance.is_reversed {
                    duration_ms.saturating_sub(elapsed_ms)
                } else {
                    elapsed_ms.min(duration_ms)
                };
                ((effective_elapsed * ANIM_SCALE as u64) / duration_ms) as i32
            };

            // Calculate and apply value
            let value = instance.anim.calc_value(progress);

            // Check if animation completed
            if instance.elapsed < duration {
                status.set(value);
            } else {
                // Ensure final value is set
                let final_value = if instance.is_reversed {
                    instance.anim.start_value
                } else {
                    instance.anim.end_value
                };
                status.set(final_value);

                // Handle repeat
                let repeat_count = instance.anim.options.repeat_count;
                let should_repeat = repeat_count == 0 || instance.current_repeat < repeat_count - 1;

                if should_repeat {
                    instance.current_repeat += 1;
                    instance.elapsed = Duration::ZERO;

                    // Handle reverse
                    if instance.anim.options.reverse {
                        instance.is_reversed = !instance.is_reversed;
                    }
                } else {
                    instance.state = AnimState::Stopped;
                }
            }
        }
    }

    /// Returns the number of active animations.
    pub fn count(&self) -> usize {
        self.animations
            .iter()
            .filter(|s| s.id != INVALID_ANIM_ID)
            .count()
    }

    /// Returns whether there are any active animations.
    pub fn is_empty(&self) -> bool {
        self.animations.iter().all(|s| s.id == INVALID_ANIM_ID)
    }
}