From 2db23f3213ff1bb2fe0cf005e922536da7ff5cd7 Mon Sep 17 00:00:00 2001 From: Arne Dußin Date: Sun, 1 Nov 2020 17:44:51 +0100 Subject: 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. --- src/math/mod.rs | 23 ++++++ src/math/rect.rs | 178 ++++++++++++++++++++++++++++++++++++++++++ src/math/vec2.rs | 233 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 434 insertions(+) create mode 100644 src/math/mod.rs create mode 100644 src/math/rect.rs create mode 100644 src/math/vec2.rs (limited to 'src/math') diff --git a/src/math/mod.rs b/src/math/mod.rs new file mode 100644 index 0000000..07d518e --- /dev/null +++ b/src/math/mod.rs @@ -0,0 +1,23 @@ +pub mod rect; +pub use self::rect::*; + +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 +/// 0.25 to 0.74999... will be 0.5 and so on. +pub fn round(num: f32, step: f32) -> f32 { + // Only positive steps will be accepted. + assert!(step > 0.); + + let lower_bound = ((num / step) as i32) as f32 * step; + let upper_bound = lower_bound + step; + + // Compare the distances and prefer the smaller. If they are the same, prefer the upper bound. + if (num - lower_bound) < (upper_bound - num) { + lower_bound + } else { + upper_bound + } +} 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 { + /// 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 + Scalar + Copy> Into for Rect { + 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 + Scalar + Copy> From for Rect { + 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 + Scalar + Copy> Into for Rect { + 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 + Scalar + Copy> From for Rect { + 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 Rect { + 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 + 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) + 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) { + self.x = pos.x; + self.y = pos.y; + } + + /// Test if two rectangles intersect. + pub fn intersect<'a>(this: &'a Rect, other: &'a Rect) -> bool + where + T: Add + 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) -> bool + where + T: PartialOrd + Add, + { + 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, pos2: Vec2) -> 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) -> Vec2 + 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 { + pub x: T, + pub y: T, +} + +impl Vec2 { + 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 Into for Vec2 +where + T: Into + Scalar + Copy, +{ + fn into(self) -> raylib::ffi::Vector2 { + raylib::ffi::Vector2 { + x: self.x.into(), + y: self.y.into(), + } + } +} + +impl From for Vec2 +where + T: From + Scalar + Copy, +{ + fn from(v: raylib::ffi::Vector2) -> Self { + Self { + x: T::from(v.x), + y: T::from(v.y), + } + } +} +impl Into for Vec2 +where + T: Into, +{ + fn into(self) -> raylib::math::Vector2 { + raylib::math::Vector2 { + x: self.x.into(), + y: self.y.into(), + } + } +} + +impl From for Vec2 +where + T: From, +{ + fn from(v: raylib::math::Vector2) -> Self { + Self { + x: T::from(v.x), + y: T::from(v.y), + } + } +} + +// Begin mathematical operators ----------------------------------------------- + +// Addition +impl Add for Vec2 { + type Output = Self; + + fn add(self, rhs: Self) -> Self { + Vec2::new(self.x + rhs.x, self.y + rhs.y) + } +} + +impl Add<(T, T)> for Vec2 { + type Output = Self; + + fn add(self, (x, y): (T, T)) -> Self { + Vec2::new(self.x + x, self.y + y) + } +} + +impl Add for Vec2 { + type Output = Self; + + fn add(self, rhs: T) -> Self { + Vec2::new(self.x + rhs, self.y + rhs) + } +} + +impl AddAssign for Vec2 { + fn add_assign(&mut self, rhs: Self) { + self.x += rhs.x; + self.y += rhs.y; + } +} + +impl AddAssign<(T, T)> for Vec2 { + fn add_assign(&mut self, (x, y): (T, T)) { + self.x += x; + self.y += y; + } +} + +// Subtraction +impl Sub for Vec2 { + type Output = Self; + + fn sub(self, rhs: Self) -> Self { + Vec2::new(self.x - rhs.x, self.y - rhs.y) + } +} + +impl Sub<(T, T)> for Vec2 { + type Output = Self; + + fn sub(self, (x, y): (T, T)) -> Self { + Vec2::new(self.x - x, self.y - y) + } +} + +impl Sub for Vec2 { + type Output = Self; + + fn sub(self, rhs: T) -> Self { + Vec2::new(self.x - rhs, self.y - rhs) + } +} + +impl SubAssign for Vec2 { + fn sub_assign(&mut self, rhs: Self) { + self.x -= rhs.x; + self.y -= rhs.y; + } +} + +impl SubAssign<(T, T)> for Vec2 { + fn sub_assign(&mut self, (x, y): (T, T)) { + self.x -= x; + self.y -= y; + } +} + +// Scalar multiplication +impl + Mul + Copy> Mul for Vec2 { + type Output = T; + + fn mul(self, rhs: Self) -> T { + self.x * rhs.x + self.y * rhs.y + } +} + +impl + Copy> Mul for Vec2 { + type Output = Self; + + fn mul(self, rhs: T) -> Self { + Vec2::new(self.x * rhs, self.y * rhs) + } +} + +impl + Copy> Div for Vec2 { + 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 Ord for Vec2 +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(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(items: Vec>, limits: Option>) -> Vec> +where + T: PartialOrd + std::fmt::Debug + Copy + Add, +{ + // 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() +} -- cgit v1.2.3-70-g09d2