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
use crate::render::callbacks::CallbackRegistry;
use crate::render::widget::*;
use crate::render::widget_cache::WidgetContainer;
use crate::render::widget_config::*;
use crate::render::{Points, Size, SIZE_HEIGHT, SIZE_WIDTH};
use sdl2::render::Canvas;
use sdl2::video::Window;
use crate::render::layout_cache::LayoutContainer;
use sdl2::pixels::Color;
use sdl2::rect::{Point, Rect};
use std::any::Any;
use std::collections::HashMap;
pub struct GridWidget {
config: WidgetConfig,
system_properties: HashMap<i32, String>,
callback_registry: CallbackRegistry,
grid_size: u32,
}
impl GridWidget {
pub fn new(points: Points, size: Size, grid_size: u32) -> Self {
Self {
config: WidgetConfig::new(points.clone(), size.clone()),
system_properties: HashMap::new(),
callback_registry: CallbackRegistry::new(),
grid_size,
}
}
fn draw_grid(&mut self, c: &mut Canvas<Window>) {
let size = self.get_config().get_size(CONFIG_SIZE);
c.set_draw_color(Color::RGB(192, 192, 192));
for i in (0..size[SIZE_WIDTH]).step_by(self.grid_size as usize) {
c.draw_line(
Point::new(self.get_config().to_x(i as i32), self.get_config().to_y(0)),
Point::new(
self.get_config().to_x(i as i32),
self.get_config().to_y(size[SIZE_HEIGHT] as i32),
),
)
.unwrap();
}
for i in (0..size[SIZE_HEIGHT]).step_by(self.grid_size as usize) {
c.draw_line(
Point::new(self.get_config().to_x(0), self.get_config().to_y(i as i32)),
Point::new(
self.get_config().to_x(size[SIZE_WIDTH] as i32),
self.get_config().to_y(i as i32),
),
)
.unwrap();
}
}
pub fn set_grid_size(&mut self, grid_size: u32) {
self.grid_size = grid_size;
self.get_config().set_invalidated(true);
}
}
impl Widget for GridWidget {
fn draw(&mut self, c: &mut Canvas<Window>) {
let base_color = self.get_color(CONFIG_COLOR_BASE);
c.set_draw_color(base_color);
c.fill_rect(self.get_drawing_area()).unwrap();
self.draw_grid(c);
let border_color = self.get_config().get_color(CONFIG_COLOR_BORDER);
if self.get_config().get_numeric(CONFIG_BORDER_WIDTH) > 0 && base_color != border_color {
c.set_draw_color(border_color);
for border in 0..self.get_config().get_numeric(CONFIG_BORDER_WIDTH) {
c.draw_rect(Rect::new(
self.config.to_x(border),
self.config.to_y(border),
self.get_config().get_size(CONFIG_SIZE)[0] - (border as u32 * 2),
self.get_config().get_size(CONFIG_SIZE)[1] - (border as u32 * 2),
))
.unwrap();
}
}
}
default_widget_functions!();
default_widget_properties!();
default_widget_callbacks!();
}