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
use sdl2::render::{Canvas, Texture};
use sdl2::video::Window;
use crate::caches::TextureCache;
use crate::primitives::draw_base;
use crate::properties::{
WidgetProperties, PROPERTY_GRID_COLOR, PROPERTY_GRID_CONNECTED, PROPERTY_GRID_SPACING,
};
use crate::texture_store::TextureStore;
use crate::widget::Widget;
use sdl2::pixels::Color;
use sdl2::rect::Point;
#[derive(Default)]
pub struct GridWidget {
texture_store: TextureStore,
properties: WidgetProperties,
}
impl Widget for GridWidget {
widget_default_impl!();
fn draw(&mut self, c: &mut Canvas<Window>, _t: &mut TextureCache) -> Option<&Texture> {
if self.invalidated() {
let bounds = self.properties.get_bounds();
let grid_connections = self.properties.get_bool(PROPERTY_GRID_CONNECTED);
let grid_size = self.properties.get_value(PROPERTY_GRID_SPACING) as usize;
let grid_draw_color = self.properties.get_color(PROPERTY_GRID_COLOR, Color::GRAY);
self.texture_store
.create_or_resize_texture(c, bounds.0, bounds.1);
let cloned_properties = self.properties.clone();
c.with_texture_canvas(self.texture_store.get_mut_ref(), |texture| {
draw_base(texture, &cloned_properties, None);
texture.set_draw_color(grid_draw_color);
if grid_connections {
for i in (0..bounds.0).step_by(grid_size) {
texture
.draw_line(
Point::new(i as i32, 0),
Point::new(i as i32, bounds.1 as i32),
)
.unwrap();
}
for i in (0..bounds.1).step_by(grid_size) {
texture
.draw_line(
Point::new(0, i as i32),
Point::new(bounds.0 as i32, i as i32),
)
.unwrap();
}
} else {
for x in (0..bounds.0).step_by(grid_size) {
for y in (0..bounds.1).step_by(grid_size) {
texture.draw_point(Point::new(x as i32, y as i32)).unwrap();
}
}
}
})
.unwrap();
}
self.texture_store.get_optional_ref()
}
}