aboutsummaryrefslogtreecommitdiff
path: root/src/tool
diff options
context:
space:
mode:
Diffstat (limited to 'src/tool')
-rw-r--r--src/tool/mod.rs16
-rw-r--r--src/tool/room_tool.rs81
2 files changed, 97 insertions, 0 deletions
diff --git a/src/tool/mod.rs b/src/tool/mod.rs
new file mode 100644
index 0000000..e0d4f1e
--- /dev/null
+++ b/src/tool/mod.rs
@@ -0,0 +1,16 @@
+pub mod room_tool;
+pub use room_tool::RoomTool;
+
+use crate::transform::Transform;
+use raylib::core::drawing::RaylibDraw;
+use raylib::RaylibHandle;
+
+pub trait Tool {
+ fn update(&mut self, rl: &RaylibHandle, transform: &Transform);
+
+ fn draw<D>(&self, _rld: &mut D, _transform: &Transform)
+ where
+ D: RaylibDraw,
+ {
+ }
+}
diff --git a/src/tool/room_tool.rs b/src/tool/room_tool.rs
new file mode 100644
index 0000000..16ff5ce
--- /dev/null
+++ b/src/tool/room_tool.rs
@@ -0,0 +1,81 @@
+use super::Tool;
+use crate::math;
+use crate::transform::Transform;
+use raylib::core::drawing::RaylibDraw;
+use raylib::ffi::{Color, MouseButton};
+use raylib::math::{Rectangle, Vector2};
+use raylib::RaylibHandle;
+
+pub struct RoomTool {
+ /// Vector of all Rectangles representing rooms that have already been drawn.
+ room_rects: Vec<Rectangle>,
+ /// 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<(Vector2, Vector2)>,
+}
+
+impl RoomTool {
+ /// Create a new room tool where no rooms have been drawn yet.
+ pub fn new() -> Self {
+ Self {
+ room_rects: Vec::new(),
+ unfinished_rect: None,
+ }
+ }
+}
+
+impl Tool for RoomTool {
+ fn update(&mut self, rl: &RaylibHandle, transform: &Transform) {
+ let mouse_pos_m = transform.point_px_to_m(rl.get_mouse_position());
+ // Update the currently drawn rectangle, if it exists
+ if let Some((_, ref mut pos2)) = &mut self.unfinished_rect {
+ *pos2 = mouse_pos_m;
+ }
+
+ // Start or finish drawing the currently unfinished rectangle
+ if rl.is_mouse_button_pressed(MouseButton::MOUSE_LEFT_BUTTON) {
+ if let Some((pos1, pos2)) = self.unfinished_rect {
+ self.room_rects.push(math::bounding_rect(pos1, pos2));
+ self.unfinished_rect = None;
+ } else {
+ self.unfinished_rect = Some((mouse_pos_m, mouse_pos_m))
+ }
+ }
+
+ // Abort drawing the room (if any) in case the right mouse button was pressed.
+ if rl.is_mouse_button_pressed(MouseButton::MOUSE_RIGHT_BUTTON) {
+ self.unfinished_rect = None;
+ }
+ }
+
+ fn draw<D>(&self, rld: &mut D, transform: &Transform)
+ where
+ D: RaylibDraw,
+ {
+ // Draw all finished rectangles.
+ for &rect in &self.room_rects {
+ rld.draw_rectangle_rec(
+ transform.rect_m_to_px(rect),
+ Color {
+ r: 50,
+ g: 50,
+ b: 50,
+ a: 255,
+ },
+ );
+ }
+
+ // Do the same for the unfinished rectangle
+ if let Some((pos1, pos2)) = self.unfinished_rect {
+ rld.draw_rectangle_rec(
+ transform.rect_m_to_px(math::bounding_rect(pos1, pos2)),
+ Color {
+ r: 70,
+ g: 100,
+ b: 70,
+ a: 255,
+ },
+ );
+ }
+ }
+}