aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorArne Dußin2020-11-01 17:44:51 +0100
committerArne Dußin2020-11-01 17:44:51 +0100
commit2db23f3213ff1bb2fe0cf005e922536da7ff5cd7 (patch)
tree912b6d829ea8c8c28a2ed257e70e0fd6814bef50
parenta6be14ada463cb5a3fc0a810b8b341093abbb68d (diff)
downloadgraf_karto-2db23f3213ff1bb2fe0cf005e922536da7ff5cd7.tar.gz
graf_karto-2db23f3213ff1bb2fe0cf005e922536da7ff5cd7.zip
Refactor a major part of the project
In order to be able to save and load the map, a major rework of the code seemed necessary, since Vector2 and Rectangle of raylib do not implement serialize, and it seems cleanest to use the serialize/deserialize traits of serde, to save for instance to RON. ToolShed was renamed to Editor, since it should better show, that it does quite a bit more than harbour tools. The map data is now centrally saved in the editor, instead of decentralised in the tool structs.
-rw-r--r--Cargo.lock326
-rw-r--r--Cargo.toml4
-rw-r--r--src/editor.rs59
-rw-r--r--src/main.rs15
-rw-r--r--src/map_data.rs44
-rw-r--r--src/math/mod.rs (renamed from src/math.rs)21
-rw-r--r--src/math/rect.rs178
-rw-r--r--src/math/vec2.rs233
-rw-r--r--src/tool/deletion_tool.rs7
-rw-r--r--src/tool/mod.rs52
-rw-r--r--src/tool/room_tool.rs25
-rw-r--r--src/tool/wall_tool.rs36
-rw-r--r--src/transform.rs34
13 files changed, 911 insertions, 123 deletions
diff --git a/Cargo.lock b/Cargo.lock
index 2cfae0e..1d4f2fa 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -1,12 +1,65 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
[[package]]
+name = "alga"
+version = "0.9.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "4f823d037a7ec6ea2197046bafd4ae150e6bc36f9ca347404f46a46823fa84f2"
+dependencies = [
+ "approx 0.3.2",
+ "num-complex 0.2.4",
+ "num-traits",
+]
+
+[[package]]
+name = "approx"
+version = "0.3.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "f0e60b75072ecd4168020818c0107f2857bb6c4e64252d8d3983f6263b40a5c3"
+dependencies = [
+ "num-traits",
+]
+
+[[package]]
+name = "approx"
+version = "0.4.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "3f2a05fd1bd10b2527e20a2cd32d8873d115b8b39fe219ee25f42a8aca6ba278"
+dependencies = [
+ "num-traits",
+]
+
+[[package]]
+name = "autocfg"
+version = "1.0.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "cdb031dd78e28731d87d56cc8ffef4a8f36ca26c38fe2de700543e627f8a464a"
+
+[[package]]
+name = "base64"
+version = "0.12.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "3441f0f7b02788e948e47f457ca01f1d7e6d92c693bc132c22b087d3141c03ff"
+
+[[package]]
+name = "bitflags"
+version = "1.2.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693"
+
+[[package]]
name = "cc"
version = "1.0.61"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ed67cbde08356238e75fc4656be4749481eeffb09e19f320a25237d5221c985d"
[[package]]
+name = "cfg-if"
+version = "0.1.10"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822"
+
+[[package]]
name = "cmake"
version = "0.1.44"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -22,10 +75,35 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2022715d62ab30faffd124d40b76f4134a550a87792276512b18d63272333394"
[[package]]
+name = "generic-array"
+version = "0.14.4"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "501466ecc8a30d1d3b7fc9229b122b2ce8ed6e9d9223f1138d4babb253e51817"
+dependencies = [
+ "typenum",
+ "version_check",
+]
+
+[[package]]
+name = "getrandom"
+version = "0.1.15"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "fc587bc0ec293155d5bfa6b9891ec18a1e330c234f896ea47fbada4cadbe47e6"
+dependencies = [
+ "cfg-if",
+ "libc",
+ "wasi",
+]
+
+[[package]]
name = "graf_karto"
version = "0.1.0"
dependencies = [
+ "alga",
+ "nalgebra",
"raylib",
+ "ron",
+ "serde",
]
[[package]]
@@ -41,6 +119,176 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2448f6066e80e3bfc792e9c98bf705b4b0fc6e8ef5b43e5889aff0eaa9c58743"
[[package]]
+name = "libm"
+version = "0.2.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "c7d73b3f436185384286bd8098d17ec07c9a7d2388a6599f824d8502b529702a"
+
+[[package]]
+name = "matrixmultiply"
+version = "0.2.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "d4f7ec66360130972f34830bfad9ef05c6610a43938a467bcc9ab9369ab3478f"
+dependencies = [
+ "rawpointer",
+]
+
+[[package]]
+name = "nalgebra"
+version = "0.23.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "02f2c3d9ba22f24e44de8652a9745d5985ce158a5081b04e7e75a5091ba7ac43"
+dependencies = [
+ "approx 0.4.0",
+ "generic-array",
+ "matrixmultiply",
+ "num-complex 0.3.1",
+ "num-rational",
+ "num-traits",
+ "rand",
+ "rand_distr",
+ "simba",
+ "typenum",
+]
+
+[[package]]
+name = "num-complex"
+version = "0.2.4"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "b6b19411a9719e753aff12e5187b74d60d3dc449ec3f4dc21e3989c3f554bc95"
+dependencies = [
+ "autocfg",
+ "num-traits",
+]
+
+[[package]]
+name = "num-complex"
+version = "0.3.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "747d632c0c558b87dbabbe6a82f3b4ae03720d0646ac5b7b4dae89394be5f2c5"
+dependencies = [
+ "num-traits",
+]
+
+[[package]]
+name = "num-integer"
+version = "0.1.44"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "d2cc698a63b549a70bc047073d2949cce27cd1c7b0a4a862d08a8031bc2801db"
+dependencies = [
+ "autocfg",
+ "num-traits",
+]
+
+[[package]]
+name = "num-rational"
+version = "0.3.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "e5fa6d5f418879385b213d905f7cf5bf4aa553d4c380f0152d1d4f2749186fa9"
+dependencies = [
+ "autocfg",
+ "num-integer",
+ "num-traits",
+]
+
+[[package]]
+name = "num-traits"
+version = "0.2.14"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "9a64b1ec5cda2586e284722486d802acf1f7dbdc623e2bfc57e65ca1cd099290"
+dependencies = [
+ "autocfg",
+ "libm",
+]
+
+[[package]]
+name = "paste"
+version = "1.0.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "ba7ae1a2180ed02ddfdb5ab70c70d596a26dd642e097bb6fe78b1bde8588ed97"
+
+[[package]]
+name = "ppv-lite86"
+version = "0.2.9"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "c36fa947111f5c62a733b652544dd0016a43ce89619538a8ef92724a6f501a20"
+
+[[package]]
+name = "proc-macro2"
+version = "1.0.24"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "1e0704ee1a7e00d7bb417d0770ea303c1bccbabf0ef1667dae92b5967f5f8a71"
+dependencies = [
+ "unicode-xid",
+]
+
+[[package]]
+name = "quote"
+version = "1.0.7"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "aa563d17ecb180e500da1cfd2b028310ac758de548efdd203e18f283af693f37"
+dependencies = [
+ "proc-macro2",
+]
+
+[[package]]
+name = "rand"
+version = "0.7.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "6a6b1679d49b24bbfe0c803429aa1874472f50d9b363131f0e89fc356b544d03"
+dependencies = [
+ "getrandom",
+ "libc",
+ "rand_chacha",
+ "rand_core",
+ "rand_hc",
+]
+
+[[package]]
+name = "rand_chacha"
+version = "0.2.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "f4c8ed856279c9737206bf725bf36935d8666ead7aa69b52be55af369d193402"
+dependencies = [
+ "ppv-lite86",
+ "rand_core",
+]
+
+[[package]]
+name = "rand_core"
+version = "0.5.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "90bde5296fc891b0cef12a6d03ddccc162ce7b2aff54160af9338f8d40df6d19"
+dependencies = [
+ "getrandom",
+]
+
+[[package]]
+name = "rand_distr"
+version = "0.3.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "c9e9532ada3929fb8b2e9dbe28d1e06c9b2cc65813f074fcb6bd5fbefeff9d56"
+dependencies = [
+ "num-traits",
+ "rand",
+]
+
+[[package]]
+name = "rand_hc"
+version = "0.2.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "ca3129af7b92a17112d59ad498c6f81eaf463253766b90396d39ea7a39d6613c"
+dependencies = [
+ "rand_core",
+]
+
+[[package]]
+name = "rawpointer"
+version = "0.2.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "60a357793950651c4ed0f3f52338f53b2f809f32d83a07f72909fa13e4c6c1e3"
+
+[[package]]
name = "raylib"
version = "3.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -61,3 +309,81 @@ dependencies = [
"cmake",
"fs_extra",
]
+
+[[package]]
+name = "ron"
+version = "0.6.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "f8a58080b7bb83b2ea28c3b7a9a994fd5e310330b7c8ca5258d99b98128ecfe4"
+dependencies = [
+ "base64",
+ "bitflags",
+ "serde",
+]
+
+[[package]]
+name = "serde"
+version = "1.0.117"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "b88fa983de7720629c9387e9f517353ed404164b1e482c970a90c1a4aaf7dc1a"
+dependencies = [
+ "serde_derive",
+]
+
+[[package]]
+name = "serde_derive"
+version = "1.0.117"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "cbd1ae72adb44aab48f325a02444a5fc079349a8d804c1fc922aed3f7454c74e"
+dependencies = [
+ "proc-macro2",
+ "quote",
+ "syn",
+]
+
+[[package]]
+name = "simba"
+version = "0.3.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "17bfe642b1728a6e89137ad428ef5d4738eca4efaba9590f9e110b8944028621"
+dependencies = [
+ "approx 0.4.0",
+ "num-complex 0.3.1",
+ "num-traits",
+ "paste",
+]
+
+[[package]]
+name = "syn"
+version = "1.0.48"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "cc371affeffc477f42a221a1e4297aedcea33d47d19b61455588bd9d8f6b19ac"
+dependencies = [
+ "proc-macro2",
+ "quote",
+ "unicode-xid",
+]
+
+[[package]]
+name = "typenum"
+version = "1.12.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "373c8a200f9e67a0c95e62a4f52fbf80c23b4381c05a17845531982fa99e6b33"
+
+[[package]]
+name = "unicode-xid"
+version = "0.2.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "f7fe0bb3479651439c9112f72b6c505038574c9fbb575ed1bf3b797fa39dd564"
+
+[[package]]
+name = "version_check"
+version = "0.9.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "b5a972e5669d67ba988ce3dc826706fb0a8b01471c088cb0b6110b805cc36aed"
+
+[[package]]
+name = "wasi"
+version = "0.9.0+wasi-snapshot-preview1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "cccddf32554fecc6acb585f82a32a72e28b48f8c4c1883ddfeeeaa96f7d8e519"
diff --git a/Cargo.toml b/Cargo.toml
index 5d470c8..9632f86 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -6,3 +6,7 @@ edition = "2018"
[dependencies]
raylib = "3.0.0"
+ron = "*"
+serde = "*"
+nalgebra = "*"
+alga = "*"
diff --git a/src/editor.rs b/src/editor.rs
new file mode 100644
index 0000000..9dcbefc
--- /dev/null
+++ b/src/editor.rs
@@ -0,0 +1,59 @@
+use crate::map_data::MapData;
+use crate::tool::*;
+use crate::transform::Transform;
+use raylib::core::drawing::RaylibDrawHandle;
+use raylib::ffi::KeyboardKey;
+use raylib::RaylibHandle;
+
+pub struct Editor {
+ map_data: MapData,
+ tools: Vec<Box<dyn Tool>>,
+ active: usize,
+}
+
+impl Editor {
+ pub fn new() -> Self {
+ let mut tools: Vec<Box<dyn Tool>> = Vec::with_capacity(ToolType::NumTools as usize);
+ assert_eq!(ToolType::RoomTool as u8, 0);
+ tools.push(Box::new(RoomTool::new()));
+ assert_eq!(ToolType::WallTool as u8, 1);
+ tools.push(Box::new(WallTool::new()));
+ assert_eq!(ToolType::DeletionTool as u8, 2);
+ tools.push(Box::new(DeletionTool::new()));
+
+ assert_eq!(ToolType::NumTools as usize, tools.len());
+
+ Self {
+ map_data: MapData::new(),
+ tools,
+ active: 0,
+ }
+ }
+
+ pub fn update(&mut self, rl: &RaylibHandle, transform: &Transform) {
+ // Handle keybindings for tool change
+ self.active = if rl.is_key_pressed(KeyboardKey::KEY_R) {
+ ToolType::RoomTool as usize
+ } else if rl.is_key_pressed(KeyboardKey::KEY_W) {
+ ToolType::WallTool as usize
+ } else {
+ self.active
+ };
+
+ for tool in &mut self.tools {
+ tool.update(&self.map_data, rl, transform);
+ }
+
+ self.tools[self.active].active_update(&mut self.map_data, rl, transform);
+ }
+
+ pub fn draw_tools(&self, rld: &mut RaylibDrawHandle, transform: &Transform) {
+ for tool in &self.tools {
+ tool.draw(&self.map_data, rld, transform);
+ }
+ }
+
+ pub fn map_data(&self) -> &MapData {
+ &self.map_data
+ }
+}
diff --git a/src/main.rs b/src/main.rs
index 723a1fd..9ba63e0 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1,17 +1,18 @@
+pub mod editor;
pub mod grid;
+pub mod map_data;
pub mod math;
pub mod tool;
pub mod transform;
-pub use transform::Transform;
-
+use editor::Editor;
use raylib::prelude::*;
-use tool::{Tool, ToolShed};
+use transform::Transform;
fn main() {
let (mut rl, thread) = raylib::init().resizable().title("Hello there!").build();
- let mut tool_shed = ToolShed::new();
+ let mut editor = Editor::new();
let mut transform = Transform::new();
let mut last_mouse_pos = rl.get_mouse_position();
@@ -21,7 +22,7 @@ fn main() {
// Move the canvas together with the mouse
if rl.is_mouse_button_down(MouseButton::MOUSE_MIDDLE_BUTTON) {
- transform.move_by_px(rl.get_mouse_position() - last_mouse_pos);
+ transform.move_by_px((rl.get_mouse_position() - last_mouse_pos).into());
}
// Handle scrolling of the canvas
@@ -31,7 +32,7 @@ fn main() {
transform.try_zoom_out();
}
- tool_shed.update(&rl, &transform);
+ editor.update(&rl, &transform);
// Update the last mouse position
last_mouse_pos = rl.get_mouse_position();
@@ -42,7 +43,7 @@ fn main() {
d.clear_background(Color::BLACK);
grid::draw_grid(&mut d, screen_width, screen_height, &transform);
- tool_shed.draw_tools(&mut d, &transform);
+ editor.draw_tools(&mut d, &transform);
}
}
}
diff --git a/src/map_data.rs b/src/map_data.rs
new file mode 100644
index 0000000..d958736
--- /dev/null
+++ b/src/map_data.rs
@@ -0,0 +1,44 @@
+use crate::math::{Rect, Vec2};
+use serde::{Deserialize, Serialize};
+use std::io;
+use std::path::Path;
+
+/// All the data of the currently opened map. This does not include things that are currently being
+/// drawn, but not finished. It also does not contain Metadata, like the current position of the
+/// transform, or the zoom-level
+#[derive(Serialize, Deserialize)]
+pub struct MapData {
+ rooms: Vec<Rect<f32>>,
+ walls: Vec<(Vec2<f32>, Vec2<f32>)>,
+}
+
+impl MapData {
+ pub fn new() -> Self {
+ Self {
+ rooms: Vec::new(),
+ walls: Vec::new(),
+ }
+ }
+
+ pub fn load_file<P: AsRef<Path>>(&mut self, path: P) -> io::Result<MapData> {
+ todo!()
+ }
+
+ pub fn write_file<P: AsRef<Path>>(&self, path: P) -> io::Result<()> {
+ todo!()
+ }
+
+ pub fn rooms(&self) -> &Vec<Rect<f32>> {
+ &self.rooms
+ }
+ pub fn rooms_mut(&mut self) -> &mut Vec<Rect<f32>> {
+ &mut self.rooms
+ }
+
+ pub fn walls(&self) -> &Vec<(Vec2<f32>, Vec2<f32>)> {
+ &self.walls
+ }
+ pub fn walls_mut(&mut self) -> &mut Vec<(Vec2<f32>, Vec2<f32>)> {
+ &mut self.walls
+ }
+}
diff --git a/src/math.rs b/src/math/mod.rs
index 60a53e8..07d518e 100644
--- a/src/math.rs
+++ b/src/math/mod.rs
@@ -1,21 +1,8 @@
-use raylib::math::{Rectangle, Vector2};
+pub mod rect;
+pub use self::rect::*;
-/// Function to calculate the bounding rectangle that is between two vectors. The order of the
-/// vectors is irrelevent for this. As long as they are diagonally opposite of each other, this
-/// function will work.
-pub fn bounding_rect(pos1: Vector2, pos2: Vector2) -> Rectangle {
- let min_x = pos1.x.min(pos2.x);
- let min_y = pos1.y.min(pos2.y);
- let max_x = pos1.x.max(pos2.x);
- let max_y = pos1.y.max(pos2.y);
-
- Rectangle {
- x: min_x,
- y: min_y,
- width: max_x - min_x,
- height: max_y - min_y,
- }
-}
+pub mod vec2;
+pub use self::vec2::*;
/// Round a floating point number to the nearest step given by the step argument. For instance, if
/// the step is 0.5, then all numbers from 0.0 to 0.24999... will be 0., while all numbers from
diff --git a/src/math/rect.rs b/src/math/rect.rs
new file mode 100644
index 0000000..c126d56
--- /dev/null
+++ b/src/math/rect.rs
@@ -0,0 +1,178 @@
+use super::Vec2;
+use nalgebra::{RealField, Scalar};
+use serde::{Deserialize, Serialize};
+use std::ops::{Add, AddAssign};
+
+/// Represents a Rectangle with the value type T.
+#[derive(Copy, Clone, Debug, Default, Serialize, Deserialize)]
+pub struct Rect<T: Scalar + Copy> {
+ /// The x coordinate, or leftmost coordinate of the Rect.
+ pub x: T,
+ /// The y coordinate, or rightmost coordinate of the Rect.
+ pub y: T,
+ /// The width of the Rect.
+ pub w: T,
+ /// The height of the Rect.
+ pub h: T,
+}
+
+// This is sad, but also sadly necessary :/
+impl<T: Into<f32> + Scalar + Copy> Into<raylib::ffi::Rectangle> for Rect<T> {
+ fn into(self) -> raylib::ffi::Rectangle {
+ raylib::ffi::Rectangle {
+ x: self.x.into(),
+ y: self.y.into(),
+ width: self.w.into(),
+ height: self.h.into(),
+ }
+ }
+}
+impl<T: From<f32> + Scalar + Copy> From<raylib::ffi::Rectangle> for Rect<T> {
+ fn from(r: raylib::ffi::Rectangle) -> Self {
+ Self {
+ x: T::from(r.x),
+ y: T::from(r.y),
+ w: T::from(r.width),
+ h: T::from(r.height),
+ }
+ }
+}
+impl<T: Into<f32> + Scalar + Copy> Into<raylib::math::Rectangle> for Rect<T> {
+ fn into(self) -> raylib::math::Rectangle {
+ raylib::math::Rectangle {
+ x: self.x.into(),
+ y: self.y.into(),
+ width: self.w.into(),
+ height: self.h.into(),
+ }
+ }
+}
+impl<T: From<f32> + Scalar + Copy> From<raylib::math::Rectangle> for Rect<T> {
+ fn from(r: raylib::math::Rectangle) -> Self {
+ Self {
+ x: T::from(r.x),
+ y: T::from(r.y),
+ w: T::from(r.width),
+ h: T::from(r.height),
+ }
+ }
+}
+
+impl<T: Scalar + Copy> Rect<T> {
+ pub fn new(x: T, y: T, w: T, h: T) -> Self {
+ Self { x, y, w, h }
+ }
+
+ /// Create a Rectangle from a slice. Indices are [x, y, w, h].
+ pub fn from_slice(slice: [T; 4]) -> Rect<T>
+ where
+ T: Copy,
+ {
+ Rect {
+ x: slice[0],
+ y: slice[1],
+ w: slice[2],
+ h: slice[3],
+ }
+ }
+
+ /// Move by the Vec provided.
+ pub fn translate(&mut self, by: Vec2<T>)
+ where
+ T: AddAssign,
+ {
+ self.x += by.x;
+ self.y += by.y;
+ }
+
+ /// Set the posiotien of the rectangle to the given one without changing its
+ /// size
+ pub fn set_pos(&mut self, pos: Vec2<T>) {
+ self.x = pos.x;
+ self.y = pos.y;
+ }
+
+ /// Test if two rectangles intersect.
+ pub fn intersect<'a>(this: &'a Rect<T>, other: &'a Rect<T>) -> bool
+ where
+ T: Add<Output = T> + PartialOrd + Copy,
+ {
+ !(this.x > other.x + other.w
+ || this.x + this.w < other.x
+ || this.y > other.y + other.h
+ || this.y + this.h < other.y)
+ }
+
+ /// Check if the point is inside this Rect and return true if so.
+ pub fn contains(&self, point: Vec2<T>) -> bool
+ where
+ T: PartialOrd + Add<Output = T>,
+ {
+ point.x >= self.x
+ && point.x <= self.x + self.w
+ && point.y >= self.y
+ && point.y <= self.y + self.h
+ }
+
+ /// Function to calculate the bounding rectangle that is between two vectors. The order of the
+ /// vectors is irrelevent for this. As long as they are diagonally opposite of each other, this
+ /// function will work.
+ pub fn bounding_rect(pos1: Vec2<T>, pos2: Vec2<T>) -> Self
+ where
+ T: RealField,
+ {
+ let min_x = pos1.x.min(pos2.x);
+ let min_y = pos1.y.min(pos2.y);
+ let max_x = pos1.x.max(pos2.x);
+ let max_y = pos1.y.max(pos2.y);
+
+ Self {
+ x: min_x,
+ y: min_y,
+ w: max_x - min_x,
+ h: max_y - min_y,
+ }
+ }
+
+ /// Get the shortest way that must be applied to this Rect to clear out of
+ /// another Rect of the same type so that they would not intersect any more.
+ pub fn shortest_way_out(&self, of: &Rect<T>) -> Vec2<T>
+ where
+ T: RealField,
+ {
+ // Check upwards
+ let mut move_y = of.y - self.y - self.h;
+ // Check downwards
+ let move_down = of.y + of.h - self.y;
+ if move_down < -move_y {
+ move_y = move_down;
+ }
+
+ // Check left
+ let mut move_x = of.x - self.x - self.w;
+ // Check right
+ let move_right = of.x + of.w - self.x;
+ if move_right < -move_x {
+ move_x = move_right;
+ }
+
+ if move_x.abs() < move_y.abs() {
+ Vec2::new(move_x, T::zero())
+ } else {
+ Vec2::new(T::zero(), move_y)
+ }
+ }
+}
+
+#[cfg(test)]
+mod test {
+ use super::*;
+
+ #[test]
+ fn test_intersect() {
+ let a = Rect::from_slice([0, 0, 4, 4]);
+ let b = Rect::from_slice([-1, -1, 1, 1]);
+
+ assert!(Rect::intersect(&a, &b));
+ }
+}
diff --git a/src/math/vec2.rs b/src/math/vec2.rs
new file mode 100644
index 0000000..ea549d7
--- /dev/null
+++ b/src/math/vec2.rs
@@ -0,0 +1,233 @@
+use crate::math::Rect;
+use alga::general::{ClosedAdd, ClosedSub};
+use nalgebra::{RealField, Scalar};
+use serde::{Deserialize, Serialize};
+use std::cmp::Ordering;
+use std::convert::{From, Into};
+use std::fmt;
+use std::ops::{Add, AddAssign, Div, Mul, Sub, SubAssign};
+
+#[derive(Clone, Copy, Debug, Default, PartialEq, PartialOrd, Serialize, Deserialize, Eq)]
+pub struct Vec2<T: Scalar + Copy> {
+ pub x: T,
+ pub y: T,
+}
+
+impl<T: Scalar + Copy> Vec2<T> {
+ pub fn new(x: T, y: T) -> Self {
+ Self { x, y }
+ }
+
+ pub fn len(&self) -> T
+ where
+ T: RealField,
+ {
+ (self.x * self.x + self.y * self.y).sqrt()
+ }
+}
+
+// This is sad, but also sadly necessary :/
+impl<T> Into<raylib::ffi::Vector2> for Vec2<T>
+where
+ T: Into<f32> + Scalar + Copy,
+{
+ fn into(self) -> raylib::ffi::Vector2 {
+ raylib::ffi::Vector2 {
+ x: self.x.into(),
+ y: self.y.into(),
+ }
+ }
+}
+
+impl<T> From<raylib::ffi::Vector2> for Vec2<T>
+where
+ T: From<f32> + Scalar + Copy,
+{
+ fn from(v: raylib::ffi::Vector2) -> Self {
+ Self {
+ x: T::from(v.x),
+ y: T::from(v.y),
+ }
+ }
+}
+impl<T: Scalar + Copy> Into<raylib::math::Vector2> for Vec2<T>
+where
+ T: Into<f32>,
+{
+ fn into(self) -> raylib::math::Vector2 {
+ raylib::math::Vector2 {
+ x: self.x.into(),
+ y: self.y.into(),
+ }
+ }
+}
+
+impl<T: Scalar + Copy> From<raylib::math::Vector2> for Vec2<T>
+where
+ T: From<f32>,
+{
+ fn from(v: raylib::math::Vector2) -> Self {
+ Self {
+ x: T::from(v.x),
+ y: T::from(v.y),
+ }
+ }
+}
+
+// Begin mathematical operators -----------------------------------------------
+
+// Addition
+impl<T: Scalar + ClosedAdd + Copy> Add for Vec2<T> {
+ type Output = Self;
+
+ fn add(self, rhs: Self) -> Self {
+ Vec2::new(self.x + rhs.x, self.y + rhs.y)
+ }
+}
+
+impl<T: Scalar + ClosedAdd + Copy> Add<(T, T)> for Vec2<T> {
+ type Output = Self;
+
+ fn add(self, (x, y): (T, T)) -> Self {
+ Vec2::new(self.x + x, self.y + y)
+ }
+}
+
+impl<T: Scalar + ClosedAdd + Copy> Add<T> for Vec2<T> {
+ type Output = Self;
+
+ fn add(self, rhs: T) -> Self {
+ Vec2::new(self.x + rhs, self.y + rhs)
+ }
+}
+
+impl<T: Scalar + AddAssign + Copy> AddAssign for Vec2<T> {
+ fn add_assign(&mut self, rhs: Self) {
+ self.x += rhs.x;
+ self.y += rhs.y;
+ }
+}
+
+impl<T: Scalar + AddAssign + Copy> AddAssign<(T, T)> for Vec2<T> {
+ fn add_assign(&mut self, (x, y): (T, T)) {
+ self.x += x;
+ self.y += y;
+ }
+}
+
+// Subtraction
+impl<T: Scalar + ClosedSub + Copy> Sub for Vec2<T> {
+ type Output = Self;
+
+ fn sub(self, rhs: Self) -> Self {
+ Vec2::new(self.x - rhs.x, self.y - rhs.y)
+ }
+}
+
+impl<T: Scalar + ClosedSub + Copy> Sub<(T, T)> for Vec2<T> {
+ type Output = Self;
+
+ fn sub(self, (x, y): (T, T)) -> Self {
+ Vec2::new(self.x - x, self.y - y)
+ }
+}
+
+impl<T: Scalar + ClosedSub + Copy> Sub<T> for Vec2<T> {
+ type Output = Self;
+
+ fn sub(self, rhs: T) -> Self {
+ Vec2::new(self.x - rhs, self.y - rhs)
+ }
+}
+
+impl<T: Scalar + SubAssign + Copy> SubAssign for Vec2<T> {
+ fn sub_assign(&mut self, rhs: Self) {
+ self.x -= rhs.x;
+ self.y -= rhs.y;
+ }
+}
+
+impl<T: Scalar + SubAssign + Copy> SubAssign<(T, T)> for Vec2<T> {
+ fn sub_assign(&mut self, (x, y): (T, T)) {
+ self.x -= x;
+ self.y -= y;
+ }
+}
+
+// Scalar multiplication
+impl<T: Scalar + Add<Output = T> + Mul<Output = T> + Copy> Mul for Vec2<T> {
+ type Output = T;
+
+ fn mul(self, rhs: Self) -> T {
+ self.x * rhs.x + self.y * rhs.y
+ }
+}
+
+impl<T: Scalar + Mul<Output = T> + Copy> Mul<T> for Vec2<T> {
+ type Output = Self;
+
+ fn mul(self, rhs: T) -> Self {
+ Vec2::new(self.x * rhs, self.y * rhs)
+ }
+}
+
+impl<T: Scalar + Div<Output = T> + Copy> Div<T> for Vec2<T> {
+ type Output = Self;
+
+ fn div(self, rhs: T) -> Self {
+ Vec2::new(self.x / rhs, self.y / rhs)
+ }
+}
+
+// End of mathematical operators ----------------------------------------------
+
+// By default, the coordinates are first compared by their y-coordinates, then
+// their x-coordinates
+impl<T: fmt::Debug> Ord for Vec2<T>
+where
+ T: Ord + Copy + 'static,
+{
+ fn cmp(&self, other: &Self) -> Ordering {
+ match self.y.cmp(&other.y) {
+ Ordering::Equal => self.x.cmp(&other.x),
+ y_order => y_order,
+ }
+ }
+}
+
+// Helper function to determine the absolute positive difference between two
+// Values, which don't have to be signed.
+fn difference_abs<T>(a: T, b: T) -> T
+where
+ T: ClosedSub + PartialOrd,
+{
+ if a > b {
+ a - b
+ } else {
+ b - a
+ }
+}
+
+// Helper function that removes all points inside the vector that are not
+// contained inside the optional limit Rect
+fn retain_inside_limits<T: 'static>(items: Vec<Vec2<T>>, limits: Option<Rect<T>>) -> Vec<Vec2<T>>
+where
+ T: PartialOrd + std::fmt::Debug + Copy + Add<Output = T>,
+{
+ // Fast return in case there are no limits
+ if limits.is_none() {
+ return items;
+ }
+ let limits = limits.unwrap();
+
+ // Retain only items that are within the bounds of the limits rect
+ items
+ .into_iter()
+ .filter(|v| {
+ v.x >= limits.x
+ && v.x <= limits.x + limits.w
+ && v.y >= limits.y
+ && v.y <= limits.y + limits.h
+ })
+ .collect()
+}
diff --git a/src/tool/deletion_tool.rs b/src/tool/deletion_tool.rs
index 4d255d9..3eca92a 100644
--- a/src/tool/deletion_tool.rs
+++ b/src/tool/deletion_tool.rs
@@ -1,4 +1,5 @@
use super::Tool;
+use crate::map_data::MapData;
use crate::transform::Transform;
use raylib::core::drawing::RaylibDrawHandle;
use raylib::RaylibHandle;
@@ -12,8 +13,8 @@ impl DeletionTool {
}
impl Tool for DeletionTool {
- fn update(&mut self, rl: &RaylibHandle, transform: &Transform) {}
- fn active_update(&mut self, rl: &RaylibHandle, transform: &Transform) {}
+ fn update(&mut self, map_data: &MapData, rl: &RaylibHandle, transform: &Transform) {}
+ fn active_update(&mut self, map_data: &mut MapData, rl: &RaylibHandle, transform: &Transform) {}
- fn draw(&self, _rld: &mut RaylibDrawHandle, _transform: &Transform) {}
+ fn draw(&self, map_data: &MapData, _rld: &mut RaylibDrawHandle, _transform: &Transform) {}
}
diff --git a/src/tool/mod.rs b/src/tool/mod.rs
index da95c61..73654d9 100644
--- a/src/tool/mod.rs
+++ b/src/tool/mod.rs
@@ -6,9 +6,9 @@ pub use deletion_tool::DeletionTool;
pub use room_tool::RoomTool;
pub use wall_tool::WallTool;
+use crate::map_data::MapData;
use crate::transform::Transform;
use raylib::core::drawing::RaylibDrawHandle;
-use raylib::ffi::KeyboardKey;
use raylib::RaylibHandle;
#[derive(Debug)]
@@ -21,52 +21,8 @@ pub enum ToolType {
}
pub trait Tool {
- fn update(&mut self, rl: &RaylibHandle, transform: &Transform) {}
- fn active_update(&mut self, rl: &RaylibHandle, transform: &Transform);
+ fn update(&mut self, _map: &MapData, _rl: &RaylibHandle, _transform: &Transform) {}
+ fn active_update(&mut self, map: &mut MapData, rl: &RaylibHandle, transform: &Transform);
- fn draw(&self, _rld: &mut RaylibDrawHandle, _transform: &Transform) {}
-}
-
-pub struct ToolShed {
- tools: Vec<Box<dyn Tool>>,
- active: usize,
-}
-
-impl ToolShed {
- pub fn new() -> Self {
- let mut tools: Vec<Box<dyn Tool>> = Vec::with_capacity(ToolType::NumTools as usize);
- assert_eq!(ToolType::RoomTool as u8, 0);
- tools.push(Box::new(RoomTool::new()));
- assert_eq!(ToolType::WallTool as u8, 1);
- tools.push(Box::new(WallTool::new()));
- assert_eq!(ToolType::DeletionTool as u8, 2);
- tools.push(Box::new(DeletionTool::new()));
-
- assert_eq!(ToolType::NumTools as usize, tools.len());
-
- Self { tools, active: 0 }
- }
-
- pub fn update(&mut self, rl: &RaylibHandle, transform: &Transform) {
- // Handle keybindings for tool change
- self.active = if rl.is_key_pressed(KeyboardKey::KEY_R) {
- ToolType::RoomTool as usize
- } else if rl.is_key_pressed(KeyboardKey::KEY_W) {
- ToolType::WallTool as usize
- } else {
- self.active
- };
-
- for tool in &mut self.tools {
- tool.update(rl, transform);
- }
-
- self.tools[self.active].active_update(rl, transform);
- }
-
- pub fn draw_tools(&self, rld: &mut RaylibDrawHandle, transform: &Transform) {
- for tool in &self.tools {
- tool.draw(rld, transform);
- }
- }
+ fn draw(&self, _map: &MapData, _rld: &mut RaylibDrawHandle, _transform: &Transform) {}
}
diff --git a/src/tool/room_tool.rs b/src/tool/room_tool.rs
index 10b9edf..0321ff3 100644
--- a/src/tool/room_tool.rs
+++ b/src/tool/room_tool.rs
@@ -1,35 +1,32 @@
use super::Tool;
-use crate::math;
+use crate::map_data::MapData;
+use crate::math::{self, Rect, Vec2};
use crate::transform::Transform;
use raylib::core::drawing::{RaylibDraw, RaylibDrawHandle};
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)>,
+ unfinished_rect: Option<(Vec2<f32>, Vec2<f32>)>,
}
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 active_update(&mut self, rl: &RaylibHandle, transform: &Transform) {
- let mouse_pos_m = transform.point_px_to_m(rl.get_mouse_position());
+ fn active_update(&mut self, map_data: &mut MapData, rl: &RaylibHandle, transform: &Transform) {
+ let mouse_pos_m = transform.point_px_to_m(rl.get_mouse_position().into());
// Update the currently drawn rectangle, if it exists
if let Some((_, ref mut pos2)) = &mut self.unfinished_rect {
- let snapped_mouse_pos = Vector2::new(
+ let snapped_mouse_pos = Vec2::new(
math::round(mouse_pos_m.x, 0.5),
math::round(mouse_pos_m.y, 0.5),
);
@@ -39,10 +36,10 @@ impl Tool for RoomTool {
// 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));
+ map_data.rooms_mut().push(Rect::bounding_rect(pos1, pos2));
self.unfinished_rect = None;
} else {
- let snapped_mouse_pos = Vector2::new(
+ let snapped_mouse_pos = Vec2::new(
math::round(mouse_pos_m.x, 0.5),
math::round(mouse_pos_m.y, 0.5),
);
@@ -56,9 +53,9 @@ impl Tool for RoomTool {
}
}
- fn draw(&self, rld: &mut RaylibDrawHandle, transform: &Transform) {
+ fn draw(&self, map_data: &MapData, rld: &mut RaylibDrawHandle, transform: &Transform) {
// Draw all finished rectangles.
- for &rect in &self.room_rects {
+ for &rect in map_data.rooms() {
rld.draw_rectangle_rec(
transform.rect_m_to_px(rect),
Color {
@@ -73,7 +70,7 @@ impl Tool for RoomTool {
// 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)),
+ transform.rect_m_to_px(Rect::bounding_rect(pos1, pos2)),
Color {
r: 150,
g: 200,
diff --git a/src/tool/wall_tool.rs b/src/tool/wall_tool.rs
index 21eb895..a1d3c15 100644
--- a/src/tool/wall_tool.rs
+++ b/src/tool/wall_tool.rs
@@ -1,30 +1,28 @@
use super::Tool;
-use crate::math;
+use crate::map_data::MapData;
+use crate::math::{self, Vec2};
use crate::transform::Transform;
use raylib::core::drawing::{RaylibDraw, RaylibDrawHandle};
-use raylib::ffi::{Color, MouseButton};
-use raylib::math::Vector2;
+use raylib::ffi::{Color, MouseButton, Vector2};
use raylib::RaylibHandle;
pub struct WallTool {
- walls: Vec<(Vector2, Vector2)>,
- unfinished_wall: Option<(Vector2, Vector2)>,
+ unfinished_wall: Option<(Vec2<f32>, Vec2<f32>)>,
}
impl WallTool {
pub fn new() -> Self {
Self {
- walls: Vec::new(),
unfinished_wall: None,
}
}
}
impl Tool for WallTool {
- fn active_update(&mut self, rl: &RaylibHandle, transform: &Transform) {
- let mouse_pos_m = transform.point_px_to_m(rl.get_mouse_position());
+ fn active_update(&mut self, map_data: &mut MapData, rl: &RaylibHandle, transform: &Transform) {
+ let mouse_pos_m = transform.point_px_to_m(rl.get_mouse_position().into());
if let Some((_, ref mut pos2)) = &mut self.unfinished_wall {
- let snapped_mouse_pos = Vector2::new(
+ let snapped_mouse_pos = Vec2::new(
math::round(mouse_pos_m.x, 0.5),
math::round(mouse_pos_m.y, 0.5),
);
@@ -33,10 +31,10 @@ impl Tool for WallTool {
if rl.is_mouse_button_pressed(MouseButton::MOUSE_LEFT_BUTTON) {
if let Some((pos1, pos2)) = self.unfinished_wall {
- self.walls.push((pos1, pos2));
+ map_data.walls_mut().push((pos1, pos2));
self.unfinished_wall = None;
} else {
- let snapped_mouse_pos = Vector2::new(
+ let snapped_mouse_pos = Vec2::new(
math::round(mouse_pos_m.x, 0.5),
math::round(mouse_pos_m.y, 0.5),
);
@@ -49,11 +47,13 @@ impl Tool for WallTool {
}
}
- fn draw(&self, rld: &mut RaylibDrawHandle, transform: &Transform) {
- for &(pos1, pos2) in &self.walls {
+ fn draw(&self, map_data: &MapData, rld: &mut RaylibDrawHandle, transform: &Transform) {
+ for &(pos1, pos2) in map_data.walls() {
+ let pos1: Vector2 = transform.point_m_to_px(pos1).into();
+ let pos2: Vector2 = transform.point_m_to_px(pos2).into();
rld.draw_line_ex(
- transform.point_m_to_px(pos1),
- transform.point_m_to_px(pos2),
+ pos1,
+ pos2,
5.,
Color {
r: 200,
@@ -65,9 +65,11 @@ impl Tool for WallTool {
}
if let Some((pos1, pos2)) = self.unfinished_wall {
+ let pos1: Vector2 = transform.point_m_to_px(pos1).into();
+ let pos2: Vector2 = transform.point_m_to_px(pos2).into();
rld.draw_line_ex(
- transform.point_m_to_px(pos1),
- transform.point_m_to_px(pos2),
+ pos1,
+ pos2,
5.,
Color {
r: 150,
diff --git a/src/transform.rs b/src/transform.rs
index fa24636..f44888b 100644
--- a/src/transform.rs
+++ b/src/transform.rs
@@ -3,7 +3,7 @@
//! Useful to turn on-screen coordinates into measurements of the "real" world the map describes
//! and the other way around.
-use raylib::prelude::*;
+use crate::math::{Rect, Vec2};
const STANDARD_PIXELS_PER_M: f32 = 64.;
const MIN_PIXELS_PER_M: f32 = 0.5;
@@ -13,7 +13,7 @@ pub struct Transform {
/// The (not necessarily natural) number of pixels per m, i.e. the current scale of the map
pixels_per_m: f32,
/// The vector the entire on-screen map is moved by in pixels
- translation_px: Vector2,
+ translation_px: Vec2<f32>,
}
impl Transform {
@@ -21,13 +21,13 @@ impl Transform {
pub fn new() -> Self {
Self {
pixels_per_m: STANDARD_PIXELS_PER_M,
- translation_px: Vector2::new(0., 0.),
+ translation_px: Vec2::new(0., 0.),
}
}
/// Convert a point that is given in meters into the corresponding point in pixels.
#[inline]
- pub fn point_m_to_px(&self, point: Vector2) -> Vector2 {
+ pub fn point_m_to_px(&self, point: Vec2<f32>) -> Vec2<f32> {
// Start by converting the absolute position in meters into the absolute position in
// pixels, then add the translation of the screen.
(point * self.pixels_per_m) + self.translation_px
@@ -35,7 +35,7 @@ impl Transform {
/// Convert an on-screen point into an absolute point with values in meters.
#[inline]
- pub fn point_px_to_m(&self, point: Vector2) -> Vector2 {
+ pub fn point_px_to_m(&self, point: Vec2<f32>) -> Vec2<f32> {
// Start by subtracting the pixel translation and afterwards convert these absolute pixel
// measurements into meters.
(point - self.translation_px) / self.pixels_per_m
@@ -55,25 +55,25 @@ impl Transform {
/// Convert a rectangle which has measurements in meters into one of pixels
#[inline]
- pub fn rect_m_to_px(&self, rect: Rectangle) -> Rectangle {
- let left_upper = self.point_m_to_px(Vector2::new(rect.x, rect.y));
- Rectangle::new(
+ pub fn rect_m_to_px(&self, rect: Rect<f32>) -> Rect<f32> {
+ let left_upper = self.point_m_to_px(Vec2::new(rect.x, rect.y));
+ Rect::new(
left_upper.x,
left_upper.y,
- self.length_m_to_px(rect.width),
- self.length_m_to_px(rect.height),
+ self.length_m_to_px(rect.w),
+ self.length_m_to_px(rect.h),
)
}
/// Convert a rectangle which has measurements in pixels into one of meters
#[inline]
- pub fn rect_px_to_m(&self, rect: Rectangle) -> Rectangle {
- let left_upper = self.point_px_to_m(Vector2::new(rect.x, rect.y));
- Rectangle::new(
+ pub fn rect_px_to_m(&self, rect: Rect<f32>) -> Rect<f32> {
+ let left_upper = self.point_px_to_m(Vec2::new(rect.x, rect.y));
+ Rect::new(
left_upper.x,
left_upper.y,
- self.length_px_to_m(rect.width),
- self.length_px_to_m(rect.height),
+ self.length_px_to_m(rect.w),
+ self.length_px_to_m(rect.h),
)
}
@@ -116,14 +116,14 @@ impl Transform {
}
/// Move the canvas by the vector in pixels.
- pub fn move_by_px(&mut self, by: Vector2) {
+ pub fn move_by_px(&mut self, by: Vec2<f32>) {
self.translation_px += by;
}
pub fn pixels_per_m(&self) -> f32 {
self.pixels_per_m
}
- pub fn translation_px(&self) -> Vector2 {
+ pub fn translation_px(&self) -> Vec2<f32> {
self.translation_px
}
}