teng/components/incremental/
boundschecker.rs1use crate::components::incremental::planarvec::Bounds;
2use crate::{Component, Pixel, Renderer, SharedState, UpdateInfo};
3
4pub struct BoundsCheckerComponent {
5 first_loc: Option<(usize, usize)>,
6 second_loc: Option<(usize, usize)>,
7 sub_first_loc: Option<(usize, usize)>,
8 sub_second_loc: Option<(usize, usize)>,
9}
10
11impl BoundsCheckerComponent {
12 pub fn new() -> Self {
13 Self {
14 first_loc: None,
15 second_loc: None,
16 sub_first_loc: None,
17 sub_second_loc: None,
18 }
19 }
20}
21
22impl Component for BoundsCheckerComponent {
23 fn update(&mut self, _update_info: UpdateInfo, shared_state: &mut SharedState) {
24 let pressed = shared_state.mouse_pressed.left;
26 if pressed {
27 self.first_loc = None;
28 self.second_loc = None;
29
30 self.first_loc = Some(shared_state.mouse_info.last_mouse_pos);
31 }
32
33 if !shared_state.mouse_info.left_mouse_down
35 && self.first_loc.is_some()
36 && self.second_loc.is_none()
37 {
38 self.second_loc = Some(shared_state.mouse_info.last_mouse_pos);
40 }
41
42 let pressed = shared_state.mouse_pressed.right;
44 if pressed {
45 self.sub_first_loc = None;
46 self.sub_second_loc = None;
47
48 self.sub_first_loc = Some(shared_state.mouse_info.last_mouse_pos);
49 }
50
51 if !shared_state.mouse_info.right_mouse_down
53 && self.sub_first_loc.is_some()
54 && self.sub_second_loc.is_none()
55 {
56 self.sub_second_loc = Some(shared_state.mouse_info.last_mouse_pos);
58 }
59 }
60
61 fn render(&self, renderer: &mut dyn Renderer, shared_state: &SharedState, depth_base: i32) {
62 let depth_base = i32::MAX - 1;
63
64 let mut min_x = 0;
65 let mut max_x = 0;
66 let mut min_y = 0;
67 let mut max_y = 0;
68
69 let mut first_bounds = None;
70
71 if let Some((x, y)) = self.first_loc {
72 let first_pos = (x, y);
73
74 let second_pos = if let Some((x, y)) = self.second_loc {
75 (x, y)
76 } else {
77 shared_state.mouse_info.last_mouse_pos
79 };
80
81 min_x = first_pos.0.min(second_pos.0);
82 max_x = first_pos.0.max(second_pos.0);
83 min_y = first_pos.1.min(second_pos.1);
84 max_y = first_pos.1.max(second_pos.1);
85
86 let bounds = Bounds {
87 min_x: min_x as i64,
88 max_x: max_x as i64,
89 min_y: min_y as i64,
90 max_y: max_y as i64,
91 };
92
93 first_bounds = Some(bounds);
94 }
95
96 let mut sub_bounds = None;
97
98 if let Some((x, y)) = self.sub_first_loc {
99 let first_pos = (x, y);
100
101 let second_pos = if let Some((x, y)) = self.sub_second_loc {
102 (x, y)
103 } else {
104 shared_state.mouse_info.last_mouse_pos
106 };
107
108 min_x = first_pos.0.min(second_pos.0);
109 max_x = first_pos.0.max(second_pos.0);
110 min_y = first_pos.1.min(second_pos.1);
111 max_y = first_pos.1.max(second_pos.1);
112
113 let bounds = Bounds {
114 min_x: min_x as i64,
115 max_x: max_x as i64,
116 min_y: min_y as i64,
117 max_y: max_y as i64,
118 };
119
120 sub_bounds = Some(bounds);
121 }
122
123 if let Some(bounds) = first_bounds {
124 let the_bounds = bounds.subtract(sub_bounds.unwrap_or(Bounds::empty()));
125
126 for bound in the_bounds.iter() {
127 for x in bound.min_x..=bound.max_x {
128 for y in bound.min_y..=bound.max_y {
129 renderer.render_pixel(x as usize, y as usize, Pixel::new('█'), depth_base);
130 }
131 }
132 }
133 }
134
135 }
147}