diff options
| author | Arne Dußin | 2021-01-27 14:01:50 +0100 |
|---|---|---|
| committer | Arne Dußin | 2021-02-02 22:16:15 +0100 |
| commit | f92e9f6f07b1e3834c2ca58ce3510734819d08e4 (patch) | |
| tree | 20e3d3afce342a56ae98f6c20491482ccd2b5c6b /src/client/tool/rect_room_tool.rs | |
| parent | c60a6d07efb120724b308e29e8e70f27c87c952d (diff) | |
| download | graf_karto-f92e9f6f07b1e3834c2ca58ce3510734819d08e4.tar.gz graf_karto-f92e9f6f07b1e3834c2ca58ce3510734819d08e4.zip | |
Rework graf karto to fit the client/server structure
Diffstat (limited to 'src/client/tool/rect_room_tool.rs')
| -rw-r--r-- | src/client/tool/rect_room_tool.rs | 91 |
1 files changed, 91 insertions, 0 deletions
diff --git a/src/client/tool/rect_room_tool.rs b/src/client/tool/rect_room_tool.rs new file mode 100644 index 0000000..41f2a91 --- /dev/null +++ b/src/client/tool/rect_room_tool.rs @@ -0,0 +1,91 @@ +//! The rectangle room tool is a specialised tool to create rooms of rectangular shape and with the +//! sides of the room parallel to the x and y-axes. This is often useful, when a quick room creation +//! is necessary and the shape of the room does not have to be very special. + +use super::Tool; +use crate::client::colours::DEFAULT_COLOURS; +use crate::client::map::Map; +use crate::client::transform::Transform; +use crate::math::{Rect, Vec2}; +use crate::net::{Cargo, Connection}; +use crate::world::Room; +use raylib::core::drawing::{RaylibDraw, RaylibDrawHandle}; + +/// The tool to create simple, rectangular rooms. +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. Should be created only once per + /// program instance and by the editor. + #[allow(clippy::new_without_default)] + 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, + server: &Connection<Cargo>, + 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; + } + + server.send(Cargo::AddRoom(Room::new( + Rect::bounding_rect(pos1, pos2).into(), + ))); + self.unfinished_rect = None; + } else { + self.unfinished_rect = Some((*mouse_pos_m, *mouse_pos_m)); + } + } + + fn finish(&mut self, _map: &mut Map, server: &Connection<Cargo>) { + if let Some((pos1, pos2)) = self.unfinished_rect { + if pos1 == pos2 { + warn!("Cannot place rectangle with start and endpoint being the same"); + return; + } + + server.send(Cargo::AddRoom(Room::new( + Rect::bounding_rect(pos1, pos2).into(), + ))); + self.unfinished_rect = None; + } + } + + fn abort(&mut self) { + self.unfinished_rect = None; + } +} |
