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
use super::{Color, Event, Placeable, Point, Rect, Renderer, Widget, WidgetCore};
use super::callback::{Click};

use std::cell::{Cell, RefCell};
use std::sync::Arc;

pub struct Canvas {
    pub core: WidgetCore,
    data: RefCell<Vec<Color>>,
    click_callback: Option<Arc<Fn(&Canvas, Point)>>,
}

impl Canvas {
    pub fn new() -> Self {
        Canvas {
            core: WidgetCore::new().bg(Color::white()).fg(Color::black()),
            data: RefCell::new(vec![]),
            click_callback: None
        }
    }

    pub fn clear(&self, color: Color) {
        let rect = self.rect().get();

        *(self.data.borrow_mut()) = vec![color; (rect.width*rect.height) as usize];
    }

    // TODO(ca1ek): implement the commented out functions,
    // I would keep them uncommented, but the lint
    // level disallows this.
    /*pub fn char(&self, pos: Point, c: char, color: Color) {
        unimplemented!()
    }*/

    /*pub fn draw_rect(&self, rect: Rect, color: Color) {
        unimplemented!()
    }*/

    pub fn pixel(&self, point: Point, color: Color) {
        let rect = self.rect().get();
        if let Some(color_ref) = self.data.borrow_mut().get_mut((point.y*rect.width as i32 + point.x) as usize) {
            *color_ref = color;
        } 
    }

    pub fn line(&self, start: Point, end: Point, color: Color) {
        let x0 = start.x;
        let y0 = start.y;
        let x1 = end.x;
        let y1 = end.y;

        let dx = (x1 - x0).abs();
        let dy = (y1 - y0).abs();

        let (mut x, mut y) = (x0, y0);

        let sx = if x0 > x1 { -1 } else { 1 };
        let sy = if y0 > y1 { -1 } else { 1 };

        if dx > dy {
            let mut err = dx as f32 / 2.0;
            while x != x1 {
                self.pixel(Point::new(x, y), color);
                err -= dy as f32;
                if err < 0.0 {
                    y += sy;
                    err += dx as f32;
                }
                x += sx;
            }
        } else {
            let mut err = dy as f32 / 2.0;
            while y != y1 {
                self.pixel(Point::new(x, y), color);
                err -= dx as f32;
                if err < 0.0 {
                    x += sx;
                    err += dy as f32;
                }
                y += sy;
            }
        }

        self.pixel(Point::new(x, y), color);
    }
}

impl Click for Canvas {
    fn emit_click(&self, point: Point) {
        if let Some(ref click_callback) = self.click_callback {
            click_callback(self, point);
        }
    }

    fn on_click<T: Fn(&Self, Point) + 'static>(mut self, func: T) -> Self {
        self.click_callback = Some(Arc::new(func));
        self
    }
}

impl Placeable for Canvas {
    // override default implementation because data 
    // must be initialized to proper width and height.
    fn size(self, width: u32, height: u32) -> Self {
        *(self.data.borrow_mut()) = vec![self.core.bg; (width*height) as usize];

        // code below should be the same as default 
        // Place::size() implementation
        let mut rect = self.rect().get();
        rect.width = width;
        rect.height = height;
        self.rect().set(rect);

        self
    }
}

impl Widget for Canvas {
    fn rect(&self) -> &Cell<Rect> {
        &self.core.rect
    }

    fn draw(&self, renderer: &mut Renderer, _focused: bool) {
        let rect = self.core.rect.get();
        renderer.rect(rect, self.core.bg);

        let data = self.data.borrow();

        for x in 0..rect.width {
            for y in 0..rect.height {
                if let Some(pixel) = data.get((y*rect.width + x) as usize) {
                    renderer.pixel(Point::new(rect.x + x as i32, rect.y + y as i32), pixel.clone());
                }
            }
        }
    }

    fn event(&self, event: Event, focused: bool, redraw: &mut bool) -> bool {
        match event {
            Event::Mouse { point, left_button, .. } => {
                let rect = self.core.rect.get();

                // if mouse is in canvas and LMB is pressed
                if rect.contains(point) && left_button {
                    // point is a position inside the window,
                    // subtracting the position of the canvas
                    // makes it a position inside the canvas.
                    let click_point: Point = point - rect.point();
                    self.emit_click(click_point);
                    *redraw = true;
                }
            }
            _ => (),
        }

        focused
    }
}