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
//! `term` module provides functions of rendering in terminal

use std::io::{Stdout, Write};

use crossterm::{cursor::MoveTo, queue, style, terminal};
use glam::Vec2;
use rand::{seq::IteratorRandom, thread_rng};

use crate::{
    fireworks::{FireworkManager, FireworkState},
    particle::LifeState,
    utils::distance_squared,
};

/// Wrap a character with color
#[derive(Debug, Clone, Copy)]
pub struct Char {
    pub text: char,
    pub color: style::Color,
}

#[allow(unused)]
impl Char {
    /// Create a new `Char`
    fn new(text: char, color: style::Color) -> Self {
        Self { text, color }
    }
}

/// Struct that represents a terminal
pub struct Terminal {
    pub size: (u16, u16),
    pub screen: Vec<Vec<Char>>,
}

impl Default for Terminal {
    fn default() -> Self {
        let size = terminal::size().expect("Fail to get terminal size.");
        let mut screen = Vec::new();
        (0..size.1).for_each(|_| {
            let mut line = Vec::new();
            (0..size.0).for_each(|_| {
                line.push(Char {
                    text: ' ',
                    color: style::Color::White,
                })
            });
            screen.push(line);
        });
        Self { size, screen }
    }
}

impl Terminal {
    /// Reload terminal to adapt new window size
    pub fn reinit(&mut self) {
        let size = terminal::size().expect("Fail to get terminal size.");
        let mut screen = Vec::new();
        (0..size.1).for_each(|_| {
            let mut line = Vec::new();
            (0..size.0).for_each(|_| {
                line.push(Char {
                    text: ' ',
                    color: style::Color::White,
                })
            });
            screen.push(line);
        });
        self.screen = screen;
        self.size = size;
    }

    /// Clear the terminal screen by setting all the characters in terminal to space
    pub fn clear_screen(&mut self) {
        let size = terminal::size().expect("Fail to get terminal size.");
        let mut s = Vec::new();
        (0..size.1).for_each(|_| {
            let mut line = Vec::new();
            (0..size.0).for_each(|_| {
                line.push(Char {
                    text: ' ',
                    color: style::Color::White,
                })
            });
            s.push(line);
        });
        self.screen = s;
    }

    /// Print the data out to terminal
    pub fn print(&self, w: &mut Stdout) {
        self.screen.iter().enumerate().for_each(|(y, line)| {
            line.iter().enumerate().for_each(|(x, c)| {
                queue!(
                    w,
                    MoveTo(x as u16, y as u16),
                    style::SetForegroundColor(c.color),
                    style::Print(c.text)
                )
                .expect("Std io error.")
            });
        });
        w.flush().expect("Std io error.");
    }

    /// Write the rendering data of all `Fireworks` and `Particles` to `Terminal`
    pub fn render(&mut self, fm: &FireworkManager) {
        self.clear_screen();
        for firework in fm.fireworks.iter().rev() {
            if firework.state == FireworkState::Alive {
                for particle in firework.current_particles.iter().rev() {
                    let grad = if firework.config.enable_gradient {
                        Some((firework.config.gradient_scale)(
                            particle.time_elapsed.as_secs_f32()
                                / particle.config.life_time.as_secs_f32(),
                        ))
                    } else {
                        None
                    };
                    particle
                        .trail
                        .iter()
                        .map(|p| Vec2::new(p.x * 2., p.y))
                        .rev()
                        .collect::<Vec<_>>()
                        .windows(2)
                        .enumerate()
                        .for_each(|(idx, v)| {
                            let density = (particle.config.trail_length - idx - 1) as f32
                                / particle.config.trail_length as f32;
                            construct_line(v[0], v[1]).iter().for_each(|p| {
                                if self.inside(*p)
                                    && self.screen[p.1 as usize][p.0 as usize].text == ' '
                                {
                                    if let Some(c) = match particle.life_state {
                                        LifeState::Alive => Some(get_char_alive(density)),
                                        LifeState::Declining => Some(get_char_declining(density)),
                                        LifeState::Dying => Some(get_char_dying(density)),
                                        LifeState::Dead => None,
                                    } {
                                        self.screen[p.1 as usize][p.0 as usize] = Char {
                                            text: c,
                                            color: {
                                                let color_u8 = if let Some(g) = grad {
                                                    shift_gradient(particle.config.color, g)
                                                } else {
                                                    particle.config.color
                                                };
                                                style::Color::Rgb {
                                                    r: color_u8.0,
                                                    g: color_u8.1,
                                                    b: color_u8.2,
                                                }
                                            },
                                        }
                                    }
                                }
                            });
                        });
                }
            }
        }
    }

    fn inside(&self, (x, y): (isize, isize)) -> bool {
        if x < self.size.0 as isize && y < self.size.1 as isize && x >= 0 && y >= 0 {
            true
        } else {
            false
        }
    }
}

fn construct_line(a: Vec2, b: Vec2) -> Vec<(isize, isize)> {
    const STEP: f32 = 0.2;
    let (x0, y0) = (a.x, a.y);
    let (x1, y1) = (b.x, b.y);
    let mut path = Vec::new();
    let mut x = x0;
    let mut y = y0;
    let slope = (y1 - y0) / (x1 - x0);
    let dx = if x0 == x1 {
        0.
    } else if x1 > x0 {
        1.
    } else {
        -1.
    };
    let dy = if y0 == y1 {
        0.
    } else if y1 > y0 {
        1.
    } else {
        -1.
    };
    let mut ds = distance_squared(a, b) + f32::EPSILON;
    path.push((x0.round() as isize, y0.round() as isize));
    if (x1 - x0).abs() >= (y1 - y0).abs() {
        while distance_squared(Vec2::new(x, y), b) <= ds {
            if *path.last().unwrap() != (x.round() as isize, y.round() as isize) {
                path.push((x.round() as isize, y.round() as isize));
                ds = distance_squared(Vec2::new(x, y), b);
            }
            x += dx * STEP;
            y += dy * (STEP * slope).abs();
        }
    } else {
        while distance_squared(Vec2::new(x, y), b) <= ds {
            if *path.last().unwrap() != (x.round() as isize, y.round() as isize) {
                path.push((x.round() as isize, y.round() as isize));
                ds = distance_squared(Vec2::new(x, y), b);
            }
            y += dy * STEP;
            x += dx * (STEP / slope).abs();
        }
    }
    path
}

fn shift_gradient(color: (u8, u8, u8), scale: f32) -> (u8, u8, u8) {
    (
        (color.0 as f32 * scale) as u8,
        (color.1 as f32 * scale) as u8,
        (color.2 as f32 * scale) as u8,
    )
}

fn get_char_alive(density: f32) -> char {
    let palette = if density < 0.3 {
        "`'. "
    } else if density < 0.5 {
        "/\\|()1{}[]?"
    } else if density < 0.7 {
        "oahkbdpqwmZO0QLCJUYXzcvunxrjft*"
    } else {
        "$@B%8&WM#"
    };
    palette
        .chars()
        .choose(&mut thread_rng())
        .expect("Fail to choose character.")
}

fn get_char_declining(density: f32) -> char {
    let palette = if density < 0.2 {
        "` '. "
    } else if density < 0.6 {
        "-_ +~<> i!lI;:,\"^"
    } else if density < 0.85 {
        "/\\| ()1{}[ ]?"
    } else {
        "xrjft*"
    };
    palette
        .chars()
        .choose(&mut thread_rng())
        .expect("Fail to choose character.")
}

fn get_char_dying(density: f32) -> char {
    let palette = if density < 0.6 {
        ".  ,`.    ^,' . "
    } else {
        " /\\| ( )  1{} [  ]?i !l I;: ,\"^ "
    };
    palette
        .chars()
        .choose(&mut thread_rng())
        .expect("Fail to choose character.")
}