gui_rect.rs 1.5 KB
Newer Older
1 2 3 4 5 6
#![no_std]
#![no_main]

extern crate user_lib;
extern crate alloc;

Y
Yifan Wu 已提交
7
use user_lib::{VIRTGPU_XRES, VIRTGPU_YRES, Display};
8

9
use embedded_graphics::pixelcolor::Rgb888;
Y
Yifan Wu 已提交
10 11
use embedded_graphics::prelude::{DrawTarget, Drawable, Point, RgbColor, Size};
use embedded_graphics::primitives::{Primitive, PrimitiveStyle, Rectangle};
12 13 14 15 16 17 18 19 20 21 22 23 24

const INIT_X: i32 = 640;
const INIT_Y: i32 = 400;
const RECT_SIZE: u32 = 40;

pub struct DrawingBoard {
    disp: Display,
    latest_pos: Point,
}

impl DrawingBoard {
    pub fn new() -> Self {
        Self {
Y
Yifan Wu 已提交
25
            disp: Display::new(Size::new(VIRTGPU_XRES, VIRTGPU_YRES)),
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
            latest_pos: Point::new(INIT_X, INIT_Y),
        }
    }
    fn paint(&mut self) {
        Rectangle::with_center(self.latest_pos, Size::new(RECT_SIZE, RECT_SIZE))
            .into_styled(PrimitiveStyle::with_stroke(Rgb888::WHITE, 1))
            .draw(&mut self.disp)
            .ok();
    }
    fn unpaint(&mut self) {
        Rectangle::with_center(self.latest_pos, Size::new(RECT_SIZE, RECT_SIZE))
            .into_styled(PrimitiveStyle::with_stroke(Rgb888::BLACK, 1))
            .draw(&mut self.disp)
            .ok();
    }
    pub fn move_rect(&mut self, dx: i32, dy: i32) {
        self.unpaint();
        self.latest_pos.x += dx;
        self.latest_pos.y += dy;
        self.paint();
    }
}

#[no_mangle]
pub fn main() -> i32 {
chyyuu1972's avatar
chyyuu1972 已提交
51
    let mut board = DrawingBoard::new();
52
    let _ = board.disp.clear(Rgb888::BLACK).unwrap();
chyyuu1972's avatar
chyyuu1972 已提交
53
    for i in 0..20 {
54 55
        board.latest_pos.x += i;
        board.latest_pos.y += i;
chyyuu1972's avatar
chyyuu1972 已提交
56
        board.paint();
57 58 59
    }
    0
}