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
|
use super::Tool;
use crate::button::Button;
use crate::config::{DeletionToolKeybindings, ToolKeybindings};
use crate::map_data::MapData;
use crate::math::{Rect, Vec2};
use crate::transform::Transform;
use raylib::core::drawing::{RaylibDraw, RaylibDrawHandle};
use raylib::ffi::Color;
use raylib::RaylibHandle;
pub struct DeletionTool {
keybindings: DeletionToolKeybindings,
deletion_rect: Option<(Vec2<f32>, Vec2<f32>)>,
}
impl DeletionTool {
pub fn new(keybindings: DeletionToolKeybindings) -> Self {
Self {
keybindings,
deletion_rect: None,
}
}
/// Delete all map-data that is contained inside the provided rectangular space.
pub fn delete_rect(map_data: &mut MapData, rect: Rect<f32>) {
map_data
.rooms_mut()
.retain(|&room| !rect.contains_rect(room));
map_data
.walls_mut()
.retain(|&(pos1, pos2)| !rect.contains(pos1) || !rect.contains(pos2));
map_data
.icons_mut()
.retain(|icon| !rect.contains(icon.position));
}
}
impl Tool for DeletionTool {
fn deactivate(&mut self) {
self.deletion_rect = None;
}
fn active_update(
&mut self,
map_data: &mut MapData,
rl: &RaylibHandle,
transform: &Transform,
mouse_blocked: bool,
) {
let mouse_pos_m = transform.point_px_to_m(rl.get_mouse_position().into());
if let Some((_, ref mut pos2)) = &mut self.deletion_rect {
*pos2 = mouse_pos_m;
}
if self.keybindings.do_delete.is_pressed(rl, mouse_blocked) && self.deletion_rect.is_some()
{
let (pos1, pos2) = self.deletion_rect.take().unwrap();
Self::delete_rect(map_data, Rect::bounding_rect(pos1, pos2));
} else if self
.keybindings
.start_selection
.is_pressed(rl, mouse_blocked)
{
self.deletion_rect = Some((mouse_pos_m, mouse_pos_m))
} else if self.keybindings.abort_deletion.is_pressed(rl, false) {
self.deletion_rect = None;
}
}
fn draw(&self, _map_data: &MapData, rld: &mut RaylibDrawHandle, transform: &Transform) {
if let Some((pos1, pos2)) = self.deletion_rect {
let rect_px = transform.rect_m_to_px(Rect::bounding_rect(pos1, pos2));
rld.draw_rectangle_rec(
rect_px,
Color {
r: 200,
g: 150,
b: 150,
a: 50,
},
);
rld.draw_rectangle_lines_ex(
rect_px,
4,
Color {
r: 200,
g: 150,
b: 150,
a: 150,
},
);
}
}
fn activation_key(&self) -> Button {
self.keybindings.activation_key()
}
}
|