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
use serde::{de, Deserialize, Deserializer, Serialize, Serializer};
use std::fmt;
use std::str::FromStr;
use tiny_skia::{BlendMode, FilterQuality, Pixmap, PixmapPaint, Transform};

/// Fit mode when drawing the animation frame
#[derive(Debug, Copy, Clone)]
pub enum DivoomDrawFitMode {
    /// Draw the image in the center
    Center,

    /// Stretch both width and height of the image and doesn't maintain the radio
    Stretch,

    /// Maintaining the ratio and stretch the image to make the width the same as the canvas
    FitX,

    /// Maintaining the ratio and stretch the image to make the height the same as the canvas
    FitY,
}

impl_divoom_dto_enum_traits_without_raw!(DivoomDrawFitMode, Center: "center", Stretch: "stretch", FitX: "fitX", FitY: "fixY");

/// Builder of each animation frame
pub struct DivoomAnimationFrameBuilder<'a> {
    frame: &'a mut Pixmap,
}

impl DivoomAnimationFrameBuilder<'_> {
    /// Create an new builder on top of a mutable Pixmap reference.
    pub fn new(frame: &mut Pixmap) -> DivoomAnimationFrameBuilder {
        DivoomAnimationFrameBuilder { frame }
    }

    /// Return the internal Pixmap
    pub fn canvas(&self) -> &Pixmap {
        self.frame
    }

    /// Return the mutable reference of the internal Pixmap. This allow us to directly use tiny_skia APIs to do more complicated things.
    pub fn canvas_mut(&mut self) -> &mut Pixmap {
        self.frame
    }

    /// Draw an Pixmap onto the current canvas.
    pub fn draw_frame(self, frame: &Pixmap) -> Self {
        self.draw_frame_fit(
            frame,
            DivoomDrawFitMode::Center,
            0.0,
            1.0,
            BlendMode::default(),
        )
    }

    /// Draw an Pixmap onto the current canvas with fit options and other options, like rotation, opacity and blend mode.
    pub fn draw_frame_fit(
        self,
        frame: &Pixmap,
        fit: DivoomDrawFitMode,
        rotation: f32,
        opacity: f32,
        blend: BlendMode,
    ) -> Self {
        let (mut x, mut y, mut draw_width, mut draw_height) =
            (0i32, 0i32, frame.width(), frame.height());
        let frame_ratio: f32 = frame.width() as f32 / frame.height() as f32;

        match fit {
            DivoomDrawFitMode::Center => {
                x = ((self.frame.width() - draw_width) / 2) as i32;
                y = ((self.frame.height() - draw_height) / 2) as i32;
            }

            DivoomDrawFitMode::FitX => {
                draw_width = self.frame.width();
                draw_height = (draw_width as f32 / frame_ratio).round() as u32;
                y = ((self.frame.height() - draw_height) / 2) as i32;
            }

            DivoomDrawFitMode::FitY => {
                draw_height = self.frame.height();
                draw_width = (draw_height as f32 * frame_ratio).round() as u32;
                x = ((self.frame.width() - draw_width) / 2) as i32;
            }

            DivoomDrawFitMode::Stretch => {
                draw_width = self.frame.width();
                draw_height = self.frame.height();
            }
        }

        self.draw_frame_sized(
            frame,
            x,
            y,
            draw_width,
            draw_height,
            rotation,
            opacity,
            blend,
        )
    }

    /// Draw an Pixmap onto the current canvas with position and size specified.
    pub fn draw_frame_sized(
        self,
        frame: &Pixmap,
        x: i32,
        y: i32,
        width: u32,
        height: u32,
        rotation: f32,
        opacity: f32,
        blend: BlendMode,
    ) -> Self {
        let scale_x = if width == frame.width() {
            1.0
        } else {
            width as f32 / frame.width() as f32
        };
        let scale_y = if height == frame.height() {
            1.0
        } else {
            height as f32 / frame.height() as f32
        };

        self.draw_frame_scaled(frame, x, y, scale_x, scale_y, rotation, opacity, blend)
    }

    /// Draw an Pixmap onto the current canvas with position and scale on X and Y axis specified.
    pub fn draw_frame_scaled(
        self,
        frame: &Pixmap,
        x: i32,
        y: i32,
        scale_x: f32,
        scale_y: f32,
        rotation: f32,
        opacity: f32,
        blend: BlendMode,
    ) -> Self {
        let transform = Transform::from_rotate_at(
            rotation,
            x as f32 + (frame.width() as f32 / 2.0),
            y as f32 + (frame.height() as f32 / 2.0),
        )
        .post_concat(Transform::from_scale(scale_x, scale_y));

        let paint = PixmapPaint {
            opacity,
            blend_mode: blend,
            quality: FilterQuality::Bicubic,
        };

        self.frame
            .draw_pixmap(x, y, frame.as_ref(), &paint, transform, None);

        self
    }
}