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
extern crate prototty;

use prototty::*;

pub trait DefaultForeground {
    fn default_foreground() -> Self;
}

pub trait DefaultBackground {
    fn default_background() -> Self;
}

#[derive(Debug, Clone)]
pub struct CommonCell<F, B>
    where F: From<Rgb24> + DefaultForeground,
          B: From<Rgb24> + DefaultBackground,
{
    pub character: char,
    pub bold: bool,
    pub underline: bool,
    pub foreground_colour: F,
    pub background_colour: B,
    foreground_depth: i32,
    background_depth: i32,
    access_depth: i32,
}

impl<F, B> ViewCell for CommonCell<F, B>
    where F: From<Rgb24> + DefaultForeground,
          B: From<Rgb24> + DefaultBackground,
{
    fn set_character(&mut self, character: char) {
        if self.access_depth >=  self.foreground_depth {
            self.character = character;
            self.foreground_depth = self.access_depth;
        }
    }
    fn set_bold(&mut self, bold: bool) {
        if self.access_depth >= self.foreground_depth {
            self.bold = bold;
            self.foreground_depth = self.access_depth;
        }
    }
    fn set_underline(&mut self, underline: bool) {
        if self.access_depth >=  self.foreground_depth {
            self.underline = underline;
            self.foreground_depth = self.access_depth;
        }
    }
    fn set_foreground_colour(&mut self, colour: Rgb24) {
        if self.access_depth >=  self.foreground_depth {
            self.foreground_colour = colour.into();
            self.foreground_depth = self.access_depth;
        }
    }
    fn set_background_colour(&mut self, colour: Rgb24) {
        if self.access_depth >=  self.background_depth {
            self.background_colour = colour.into();
            self.background_depth = self.access_depth;
        }
    }
}

impl<F, B> Default for CommonCell<F, B>
    where F: From<Rgb24> + DefaultForeground,
          B: From<Rgb24> + DefaultBackground,
{
    fn default() -> Self {
        CommonCell {
            character: ' ',
            bold: false,
            underline: false,
            foreground_colour: F::default_foreground(),
            background_colour: B::default_background(),
            foreground_depth: 0,
            background_depth: 0,
            access_depth: 0,
        }
    }
}
pub type Iter<'a, C> = grid_2d::Iter<'a, C>;
pub type IterMut<'a, C> = grid_2d::IterMut<'a, C>;
pub type CoordEnumerate<'a, C> = grid_2d::CoordEnumerate<'a, C>;

#[derive(Debug, Clone)]
pub struct Grid<F, B>
    where F: From<Rgb24> + DefaultForeground,
          B: From<Rgb24> + DefaultBackground,
{
    cells: grid_2d::Grid<CommonCell<F, B>>,
}

impl<F, B> Grid<F, B>
    where F: From<Rgb24> + DefaultForeground + Clone,
          B: From<Rgb24> + DefaultBackground + Clone,
{
    pub fn new(size: Size) -> Self {
        let cells = grid_2d::Grid::new_default(size);

        Self {
            cells,
        }
    }

    pub fn size(&self) -> Size {
        self.cells.size()
    }

    pub fn resize(&mut self, size: Size) {
        self.cells.resize_default(size);
    }

    pub fn clear(&mut self) {
        for cell in self.cells.iter_mut() {
            *cell = Default::default();
        }
    }

    pub fn enumerate(&self) -> CoordEnumerate<CommonCell<F, B>> {
        self.cells.enumerate()
    }

    pub fn iter(&self) -> Iter<CommonCell<F, B>> {
        self.cells.iter()
    }

    pub fn iter_mut(&mut self) -> IterMut<CommonCell<F, B>> {
        self.cells.iter_mut()
    }
}

impl<F, B> ViewGrid for Grid<F, B>
    where F: From<Rgb24> + DefaultForeground,
          B: From<Rgb24> + DefaultBackground,
{
    type Cell = CommonCell<F, B>;
    fn get_mut(&mut self, coord: Coord, depth: i32) -> Option<&mut Self::Cell> {
        if let Some(cell) = self.cells.get_mut(coord) {
            if cell.foreground_depth > depth && cell.background_depth > depth {
                None
            } else {
                cell.access_depth = depth;
                Some(cell)
            }
        } else {
            None
        }
    }
}