File size: 1,376 Bytes
86c91c8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/// Per-frame damage tracking for partial redraw.
#[derive(Clone, Debug, Default, PartialEq)]
pub enum Damage {
    #[default]
    None,
    Full,
    Lines(Vec<LineDamage>),
    Rects(Vec<RectDamage>),
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct LineDamage {
    pub row: usize,
    pub start_col: usize,
    pub end_col: usize,
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct RectDamage {
    pub x: u32,
    pub y: u32,
    pub w: u32,
    pub h: u32,
}

impl Damage {
    pub fn add_line(&mut self, row: usize, start_col: usize, end_col: usize) {
        match self {
            Damage::None => {
                *self = Damage::Lines(vec![LineDamage { row, start_col, end_col }]);
            }
            Damage::Lines(lines) => {
                lines.push(LineDamage { row, start_col, end_col });
            }
            _ => {}
        }
    }

    pub fn add_rect(&mut self, x: u32, y: u32, w: u32, h: u32) {
        match self {
            Damage::None => {
                *self = Damage::Rects(vec![RectDamage { x, y, w, h }]);
            }
            Damage::Rects(rects) => {
                rects.push(RectDamage { x, y, w, h });
            }
            _ => {}
        }
    }

    pub fn set_full(&mut self) {
        *self = Damage::Full;
    }

    pub fn take(&mut self) -> Damage {
        std::mem::take(self)
    }
}