micro_gui/widgets/
demo.rs

1
2use crate::core::buffer::Buff;
3
4use crate::types::pixel::*;
5use crate::types::rect::Rect;
6use crate::types::point::Point;
7use crate::graphics::{Graphics, Renderable};
8
9pub struct DemoWidget {
10
11}
12
13impl DemoWidget {
14    pub fn new() -> DemoWidget {
15        return DemoWidget{}
16    }
17}
18
19impl <Pixel>Renderable<Pixel> for DemoWidget 
20where
21    Pixel: BW + RGB,
22{
23    fn render(&mut self, graphics: &mut Graphics<Pixel>, buffer: &mut Buff<Pixel>) {
24        let bounds = graphics.get_bounds();
25
26        // Lines
27        graphics.draw_line(buffer, Point{x: 20, y: 20}, Point{x: bounds.w - 20, y: 20}, &Pixel::black());
28        graphics.draw_line(buffer, Point{x: 0, y: 0}, Point{x: bounds.w, y: bounds.h}, &Pixel::black());
29        graphics.draw_line(buffer, Point{x: 0, y: bounds.h}, Point{x: bounds.w, y: 0}, &Pixel::black());
30
31        let points = [
32                Point{x: bounds.w/6*1, y: bounds.h/8*2}, 
33                Point{x: bounds.w/6*2, y: bounds.h/8*1},
34                Point{x: bounds.w/6*3, y: bounds.h/8*2},
35                Point{x: bounds.w/6*4, y: bounds.h/8*1},
36                Point{x: bounds.w/6*5, y: bounds.h/8*2}];
37        graphics.draw_polyline(buffer, &points, &Pixel::black());
38
39        // Circles
40        let ellipse_r = bounds.w/4;
41        let circles = [
42            Rect{x: (bounds.w-ellipse_r)/2 - ellipse_r/5*3, y: (bounds.h-ellipse_r)/2, w: ellipse_r, h: ellipse_r},
43            Rect{x: (bounds.w-ellipse_r)/2, y: (bounds.h-ellipse_r)/2, w: ellipse_r, h: ellipse_r},
44            Rect{x: (bounds.w-ellipse_r)/2 + ellipse_r/5*3, y: (bounds.h-ellipse_r)/2, w: ellipse_r, h: ellipse_r}
45        ];
46        graphics.draw_ellipse(buffer, circles[0], &Pixel::red());
47        graphics.draw_ellipse(buffer, circles[1], &Pixel::green());
48        graphics.draw_ellipse(buffer, circles[2], &Pixel::blue());
49
50        // Rectangles
51        graphics.draw_rect(buffer, Rect::new(bounds.w/7*1-16, bounds.h/8*6-16, bounds.w/7*5+32, bounds.h/6+32), &Pixel::black());
52        graphics.fill_rect(buffer, Rect::new(bounds.w/7*1, bounds.h/8*6, bounds.w/7, bounds.h/6), &Pixel::red());
53        graphics.fill_rect(buffer, Rect::new(bounds.w/7*3, bounds.h/8*6, bounds.w/7, bounds.h/6), &Pixel::green());
54        graphics.fill_rect(buffer, Rect::new(bounds.w/7*5, bounds.h/8*6, bounds.w/7, bounds.h/6), &Pixel::blue());
55
56    }
57
58}