aboutsummaryrefslogtreecommitdiff
path: root/src/map/wall.rs
diff options
context:
space:
mode:
authorArne Dußin2020-12-15 00:46:54 +0100
committerArne Dußin2020-12-15 22:51:46 +0100
commit9799d3c6a8f0c242668203a1c70d7b6cfed3e855 (patch)
tree9116acbc886f680f82309a42b4e6147e65c1433b /src/map/wall.rs
parent3bc690803fb59493ea8180fd630d65b3e26642d0 (diff)
downloadgraf_karto-9799d3c6a8f0c242668203a1c70d7b6cfed3e855.tar.gz
graf_karto-9799d3c6a8f0c242668203a1c70d7b6cfed3e855.zip
Refactor to make interaction between tools easier
Diffstat (limited to 'src/map/wall.rs')
-rw-r--r--src/map/wall.rs98
1 files changed, 98 insertions, 0 deletions
diff --git a/src/map/wall.rs b/src/map/wall.rs
new file mode 100644
index 0000000..6c90fda
--- /dev/null
+++ b/src/map/wall.rs
@@ -0,0 +1,98 @@
+use super::Mappable;
+use crate::math::{LineSegment, Vec2, Rect};
+use crate::scaleable::Scaleable;
+use crate::transform::Transform;
+use raylib::drawing::{RaylibDraw, RaylibDrawHandle};
+use raylib::ffi::Color;
+use std::ops::{Deref, DerefMut};
+
+pub type WallData = LineSegment<f64>;
+
+pub struct Wall {
+ data: WallData,
+ round_start: bool,
+ round_end: bool,
+}
+
+impl Wall {
+ pub fn from_data(data: WallData, round_start: bool, round_end: bool) -> Self {
+ Self {
+ data,
+ round_start,
+ round_end,
+ }
+ }
+
+ pub fn data(&self) -> &WallData {
+ &self.data
+ }
+}
+
+fn draw_round_corner(rld: &mut RaylibDrawHandle, pos_px: Vec2<f64>, transform: &Transform) {
+ rld.draw_circle_v(
+ pos_px,
+ transform.length_m_to_px(0.05) as f32,
+ Color {
+ r: 200,
+ g: 120,
+ b: 120,
+ a: 255,
+ },
+ );
+}
+
+impl Mappable for Wall {
+ fn draw(&self, rld: &mut RaylibDrawHandle, transform: &Transform) {
+ let start_px = transform.point_m_to_px(&self.data.start);
+ let end_px = transform.point_m_to_px(&self.data.end);
+ rld.draw_line_ex(
+ start_px,
+ end_px,
+ transform.length_m_to_px(0.1) as f32,
+ Color {
+ r: 200,
+ g: 120,
+ b: 120,
+ a: 255,
+ },
+ );
+
+ if self.round_start {
+ draw_round_corner(rld, start_px, transform);
+ }
+ if self.round_end {
+ draw_round_corner(rld, end_px, transform);
+ }
+ }
+
+ fn bounding_rect(&self) -> Rect<f64> {
+ Rect::bounding_rect(self.data.start, self.data.end)
+ }
+}
+
+impl Scaleable for Wall {
+ fn scale(&mut self, by: &Vec2<f64>) {
+ if by.x <= 0. || by.y <= 0. {
+ panic!("Cannot set dimensions with negative size");
+ }
+
+ self.data.start.x *= by.x;
+ self.data.start.y *= by.y;
+ self.data.end.x *= by.x;
+ self.data.end.y *= by.y;
+ }
+}
+
+impl Deref for Wall {
+ type Target = WallData;
+
+ fn deref(&self) -> &Self::Target {
+ &self.data
+ }
+}
+
+impl DerefMut for Wall {
+ fn deref_mut(&mut self) -> &mut Self::Target {
+ &mut self.data
+ }
+}