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
use super::{Pen, Tile, Tiles, WithParams};
use colors;
use pixset;
use pixset::PixLike;
use std::default::Default;
use units;

pub struct Console<P: PixLike> {
    size: units::Size2D,
    tiles: Tiles<P>,
    pen: Pen,
}

impl<P> Console<P>
where
    P: pixset::PixLike,
{
    pub fn new(size: units::Size2D) -> Self {
        Console {
            size: size,
            tiles: Tiles::new(size),
            pen: Pen::new(),
        }
    }

    pub fn get_width(&self) -> i32 {
        self.size.width
    }

    pub fn get_height(&self) -> i32 {
        self.size.height
    }

    pub fn get_tile(&self, x: i32, y: i32) -> Tile<P> {
        self.tiles.tiles[(self.size.width * y + x) as usize]
    }

    pub fn set_loc(&mut self, screen_loc: units::ScreenTile2D) -> &mut Self {
        self.pen.cursor_loc = screen_loc;
        self
    }

    pub fn set_fg(&mut self, color: colors::Rgb) -> &mut Self {
        self.pen.fg = color;
        self
    }

    pub fn set_bg(&mut self, color: colors::Rgb) -> &mut Self {
        self.pen.bg = color;
        self
    }

    #[allow(dead_code)]
    pub fn with_loc(&mut self, screen_loc: units::ScreenTile2D) -> WithParams<P> {
        let fg = self.pen.fg;
        let bg = self.pen.bg;

        // TODO new
        WithParams {
            console: self,
            pen: Pen {
                cursor_loc: screen_loc,
                fg: fg,
                bg: bg,
            },
        }
    }

    #[allow(dead_code)]
    pub fn with_offset(&mut self, offset: units::ScreenTile2D) -> WithParams<P> {
        let fg = self.pen.fg;
        let bg = self.pen.bg;
        let screen_loc = units::ScreenTile2D::new(
            self.pen.cursor_loc.x + offset.x,
            self.pen.cursor_loc.y + offset.y,
        );

        // TODO new
        WithParams {
            console: self,
            pen: Pen {
                cursor_loc: screen_loc,
                fg: fg,
                bg: bg,
            },
        }
    }

    #[allow(dead_code)]
    pub fn with_fg(&mut self, fg: colors::Rgb) -> WithParams<P> {
        let cursor_loc = self.pen.cursor_loc;
        let bg = self.pen.bg;

        // TODO new
        WithParams {
            console: self,
            pen: Pen {
                cursor_loc: cursor_loc,
                fg: fg,
                bg: bg,
            },
        }
    }

    #[allow(dead_code)]
    pub fn with_bg(&mut self, bg: colors::Rgb) -> WithParams<P> {
        let cursor_loc = self.pen.cursor_loc;
        let fg = self.pen.fg;

        // TODO new
        WithParams {
            console: self,
            pen: Pen {
                cursor_loc: cursor_loc,
                fg: fg,
                bg: bg,
            },
        }
    }

    #[allow(dead_code)]
    pub fn set_pix<'a>(&mut self, pix: P) {
        let pen = self.pen.clone();
        self.set_with_pen(pix, pen);
    }

    pub fn set_with_pen<'a>(&mut self, pix: P, pen: Pen) {
        self.tiles.set(pen.cursor_loc, pix, pen.fg, pen.bg);
    }

    // +x is right
    // +y is down
    pub fn set_rect(&mut self, x: u32, y: u32) {
        for d_x in 0..x {
            for d_y in 0..y {
                let offset = units::ScreenTile2D::new(d_x as i32, d_y as i32);
                self.with_offset(offset).set_pix(Default::default());
            }
        }
    }
}