1use crate::{Area, Bitmap, Color, Handler, PixelabError, Point};
2
3pub struct Backend {
4 pub bitmap: Option<Box<dyn Bitmap + 'static>>,
5 pub bitmap_type: Option<Box<dyn Bitmap + 'static>>,
6 pub position: Point,
7 pub area: Area,
8 pub enable: bool,
9 pub visible: bool,
10 pub background_color: Color,
11 pub handle_all_event: bool,
12 pub on_press: Option<Box<dyn FnMut(Point, &mut Handler, &mut Backend)>>,
13 pub on_motion: Option<Box<dyn FnMut(Point, &mut Handler, &mut Backend)>>,
14 pub on_release: Option<Box<dyn FnMut(Point, &mut Handler, &mut Backend)>>,
15 pub on_message: Option<Box<dyn FnMut(&'static str, &mut Handler, &mut Backend)>>,
16}
17impl Backend {
18 pub fn new() -> Self {
19 Self {
20 bitmap: None,
21 bitmap_type: None,
22 position: Point::new(0, 0),
23 area: Area::default(),
24 enable: true,
25 visible: true,
26 background_color: Color::white(),
27 handle_all_event: true,
28 on_press: None,
29 on_motion: None,
30 on_release: None,
31 on_message: None,
32 }
33 }
34
35 pub fn set_size(&mut self, width: u16, height: u16) {
36 self.area.width = width;
37 self.area.height = height;
38 }
39
40 pub fn create_buffer(&mut self) -> Result<(), PixelabError> {
41 match self.bitmap_type.take() {
42 Some(bitmap_type) => {
43 self.bitmap = Some(bitmap_type.create(self.area));
44 self.bitmap_type = Some(bitmap_type);
45 Ok(())
46 }
47 None => Err(PixelabError::BitmapTypeIsNone),
48 }
49 }
50
51 pub fn destroy_buffer(&mut self) {
52 self.bitmap = None
53 }
54
55 pub fn overlay(&mut self, fb: &mut Box<dyn Bitmap + 'static>) -> Result<(), PixelabError> {
56 match self.bitmap.take() {
57 Some(mut buffer) => {
58 fb.overlay(&mut buffer, self.position)?;
59 self.bitmap = Some(buffer);
60 Ok(())
61 }
62 None => Err(PixelabError::BitmapBufferNotCreated),
63 }
64 }
65
66 pub fn set_pixel(&mut self, point: Point, color: Color) -> Result<(), PixelabError> {
67 match self.bitmap.take() {
68 Some(mut buffer) => {
69 buffer.set_pixel(point, color)?;
70 self.bitmap = Some(buffer);
71 Ok(())
72 }
73 None => Err(PixelabError::BitmapBufferNotCreated),
74 }
75 }
76
77 pub fn clear(&mut self, color: Color) -> Result<(), PixelabError> {
78 match self.bitmap.take() {
79 Some(mut buffer) => {
80 buffer.fill(color)?;
81 self.bitmap = Some(buffer);
82 Ok(())
83 }
84 None => Err(PixelabError::BitmapBufferNotCreated),
85 }
86 }
87
88 pub fn set_position(&mut self, x: u16, y: u16) {
89 self.position.x = x;
90 self.position.y = y;
91 }
92 pub fn check_scope(&mut self, point: Point) -> bool {
93 if point.x > self.position.x && point.x < self.position.x + self.area.width {
94 if point.y > self.position.y && point.y < self.position.y + self.area.height {
95 return true;
96 }
97 }
98 false
99 }
100 pub fn on_press(&mut self, f: impl 'static + FnMut(Point, &mut Handler, &mut Backend)) {
101 self.on_press = Some(Box::new(f));
102 }
103 pub fn on_motion(&mut self, f: impl 'static + FnMut(Point, &mut Handler, &mut Backend)) {
104 self.on_motion = Some(Box::new(f));
105 }
106 pub fn on_release(&mut self, f: impl 'static + FnMut(Point, &mut Handler, &mut Backend)) {
107 self.on_release = Some(Box::new(f));
108 }
109 pub fn on_message<F>(
110 &mut self,
111 f: impl 'static + FnMut(&'static str, &mut Handler, &mut Backend),
112 ) {
113 self.on_message = Some(Box::new(f));
114 }
115}