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
|
use super::Mappable;
use crate::colours::DEFAULT_COLOURS;
use crate::math::{self, Polygon, Rect, Triangle, Vec2};
use crate::scaleable::Scaleable;
use crate::transform::Transform;
use raylib::drawing::{RaylibDraw, RaylibDrawHandle};
pub type PolygonRoomData = Polygon<f64>;
pub struct PolygonRoom {
data: PolygonRoomData,
// The polygon shape, but in triangles, so the polygon does not have to be triangulated every frame.
triangulated: Vec<Triangle<f64>>,
selected: bool,
}
impl PolygonRoom {
pub fn from_data(data: PolygonRoomData) -> Self {
Self {
data: data.clone(),
triangulated: math::triangulate(data),
selected: false,
}
}
fn retriangulate(&mut self) {
self.triangulated = math::triangulate(self.data.clone());
}
}
impl Mappable for PolygonRoom {
fn draw(&self, rld: &mut RaylibDrawHandle, transform: &Transform) {
for triangle in &self.triangulated {
rld.draw_triangle(
transform.point_m_to_px(&triangle.corners()[0]),
transform.point_m_to_px(&triangle.corners()[1]),
transform.point_m_to_px(&triangle.corners()[2]),
if self.selected() {
DEFAULT_COLOURS.room_selected
} else {
DEFAULT_COLOURS.room_normal
},
)
}
}
fn set_selected(&mut self, selected: bool) {
self.selected = selected;
}
fn selected(&self) -> bool {
self.selected
}
fn bounding_rect(&self) -> Rect<f64> {
Rect::bounding_rect_n(&self.data.corners())
}
fn as_scaleable(&self) -> Option<&dyn Scaleable> {
Some(self as &dyn Scaleable)
}
}
impl Scaleable for PolygonRoom {
fn scale(&mut self, by: &Vec2<f64>) {
if by.x < 0. || by.y < 0. {
panic!("Cannot set dimensions with negative size");
}
for corner in self.data.corners_mut() {
corner.x *= by.x;
corner.y *= by.y;
}
self.retriangulate();
}
}
|