aboutsummaryrefslogtreecommitdiff
path: root/src/tool/room_tool.rs
diff options
context:
space:
mode:
authorArne Dußin2020-10-30 22:32:28 +0100
committerArne Dußin2020-10-30 22:32:28 +0100
commit48c425a193cb13012eb9303df56ac04b9d683ed4 (patch)
treedbc7fc9bd4e595bd8438d4233c08fe3775c5c690 /src/tool/room_tool.rs
parent20c73199167ce3ef1b4a256db5a95acab8f467b3 (diff)
downloadgraf_karto-48c425a193cb13012eb9303df56ac04b9d683ed4.tar.gz
graf_karto-48c425a193cb13012eb9303df56ac04b9d683ed4.zip
Rewrite project to use raylib instead of piston
Sorry piston.. I really tried liking you, but I just couldn't :/ It's not you, it's me. What am I saying? It's you, sorry not sorry.
Diffstat (limited to 'src/tool/room_tool.rs')
-rw-r--r--src/tool/room_tool.rs81
1 files changed, 81 insertions, 0 deletions
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,
+ },
+ );
+ }
+ }
+}