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
|
use super::Tool;
use crate::colours::DEFAULT_COLOURS;
use crate::map::Map;
use crate::math::{Rect, Vec2};
use crate::transform::Transform;
use raylib::core::drawing::{RaylibDraw, RaylibDrawHandle};
pub struct RectRoomTool {
/// The rectangle that is currently being drawn by the user. Once it is finished, it will be
/// pushed into the room_rects.
unfinished_rect: Option<(Vec2<f64>, Vec2<f64>)>,
}
impl RectRoomTool {
/// Create a new room tool where no rooms have been drawn yet.
pub fn new() -> Self {
Self {
unfinished_rect: None,
}
}
}
impl Tool for RectRoomTool {
fn deactivate(&mut self) {
self.unfinished_rect = None;
}
fn update(&mut self, _map: &Map, mouse_pos_m: &Vec2<f64>) {
if let Some((_, ref mut pos2)) = &mut self.unfinished_rect {
*pos2 = *mouse_pos_m;
}
}
fn draw(&self, rld: &mut RaylibDrawHandle, transform: &Transform) {
if let Some((pos1, pos2)) = self.unfinished_rect {
rld.draw_rectangle_rec(
transform.rect_m_to_px(&Rect::bounding_rect(pos1, pos2)),
DEFAULT_COLOURS.room_selected,
);
}
}
fn place_single(&mut self, map: &mut Map, mouse_pos_m: &Vec2<f64>) {
// Try to finish the rectangle if it has been started.
if let Some((pos1, pos2)) = self.unfinished_rect {
if pos1 == pos2 {
warn!("Cannot place rectangle with start and endpoint being the same");
return;
}
map.push_rect_room(Rect::bounding_rect(pos1, pos2));
self.unfinished_rect = None;
} else {
self.unfinished_rect = Some((*mouse_pos_m, *mouse_pos_m));
}
}
fn finish(&mut self, map: &mut Map) {
if let Some((pos1, pos2)) = self.unfinished_rect {
if pos1 == pos2 {
warn!("Cannot place rectangle with start and endpoint being the same");
return;
}
map.push_rect_room(Rect::bounding_rect(pos1, pos2));
self.unfinished_rect = None;
}
}
fn abort(&mut self) {
self.unfinished_rect = None;
}
}
|