summaryrefslogtreecommitdiffstats
path: root/core
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--core/src/keyboard.rs6
-rw-r--r--core/src/keyboard/key_code.rs (renamed from native/src/input/keyboard/key_code.rs)0
-rw-r--r--core/src/keyboard/modifiers_state.rs30
-rw-r--r--core/src/lib.rs1
-rw-r--r--core/src/point.rs21
-rw-r--r--core/src/rectangle.rs50
-rw-r--r--core/src/vector.rs13
7 files changed, 120 insertions, 1 deletions
diff --git a/core/src/keyboard.rs b/core/src/keyboard.rs
new file mode 100644
index 00000000..d98b2989
--- /dev/null
+++ b/core/src/keyboard.rs
@@ -0,0 +1,6 @@
+//! Reuse basic keyboard types.
+mod key_code;
+mod modifiers_state;
+
+pub use key_code::KeyCode;
+pub use modifiers_state::ModifiersState;
diff --git a/native/src/input/keyboard/key_code.rs b/core/src/keyboard/key_code.rs
index 26020a57..26020a57 100644
--- a/native/src/input/keyboard/key_code.rs
+++ b/core/src/keyboard/key_code.rs
diff --git a/core/src/keyboard/modifiers_state.rs b/core/src/keyboard/modifiers_state.rs
new file mode 100644
index 00000000..4d24266f
--- /dev/null
+++ b/core/src/keyboard/modifiers_state.rs
@@ -0,0 +1,30 @@
+/// The current state of the keyboard modifiers.
+#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
+pub struct ModifiersState {
+ /// Whether a shift key is pressed
+ pub shift: bool,
+
+ /// Whether a control key is pressed
+ pub control: bool,
+
+ /// Whether an alt key is pressed
+ pub alt: bool,
+
+ /// Whether a logo key is pressed (e.g. windows key, command key...)
+ pub logo: bool,
+}
+
+impl ModifiersState {
+ /// Returns true if the current [`ModifiersState`] has at least the same
+ /// modifiers enabled as the given value, and false otherwise.
+ ///
+ /// [`ModifiersState`]: struct.ModifiersState.html
+ pub fn matches(&self, modifiers: ModifiersState) -> bool {
+ let shift = !modifiers.shift || self.shift;
+ let control = !modifiers.control || self.control;
+ let alt = !modifiers.alt || self.alt;
+ let logo = !modifiers.logo || self.logo;
+
+ shift && control && alt && logo
+ }
+}
diff --git a/core/src/lib.rs b/core/src/lib.rs
index ea5e8b43..c2887a0b 100644
--- a/core/src/lib.rs
+++ b/core/src/lib.rs
@@ -14,6 +14,7 @@
#![deny(unused_results)]
#![forbid(unsafe_code)]
#![forbid(rust_2018_idioms)]
+pub mod keyboard;
mod align;
mod background;
diff --git a/core/src/point.rs b/core/src/point.rs
index b9a8149c..43ee2143 100644
--- a/core/src/point.rs
+++ b/core/src/point.rs
@@ -22,6 +22,16 @@ impl Point {
pub const fn new(x: f32, y: f32) -> Self {
Self { x, y }
}
+
+ /// Computes the distance to another [`Point`].
+ ///
+ /// [`Point`]: struct.Point.html
+ pub fn distance(&self, to: Point) -> f32 {
+ let a = self.x - to.x;
+ let b = self.y - to.y;
+
+ a.hypot(b)
+ }
}
impl From<[f32; 2]> for Point {
@@ -46,3 +56,14 @@ impl std::ops::Add<Vector> for Point {
}
}
}
+
+impl std::ops::Sub<Vector> for Point {
+ type Output = Self;
+
+ fn sub(self, vector: Vector) -> Self {
+ Self {
+ x: self.x - vector.x,
+ y: self.y - vector.y,
+ }
+ }
+}
diff --git a/core/src/rectangle.rs b/core/src/rectangle.rs
index ee1e3807..7ed3d2df 100644
--- a/core/src/rectangle.rs
+++ b/core/src/rectangle.rs
@@ -27,6 +27,34 @@ impl Rectangle<f32> {
&& self.y <= point.y
&& point.y <= self.y + self.height
}
+
+ /// Computes the intersection with the given [`Rectangle`].
+ ///
+ /// [`Rectangle`]: struct.Rectangle.html
+ pub fn intersection(
+ &self,
+ other: &Rectangle<f32>,
+ ) -> Option<Rectangle<f32>> {
+ let x = self.x.max(other.x);
+ let y = self.y.max(other.y);
+
+ let lower_right_x = (self.x + self.width).min(other.x + other.width);
+ let lower_right_y = (self.y + self.height).min(other.y + other.height);
+
+ let width = lower_right_x - x;
+ let height = lower_right_y - y;
+
+ if width > 0.0 && height > 0.0 {
+ Some(Rectangle {
+ x,
+ y,
+ width,
+ height,
+ })
+ } else {
+ None
+ }
+ }
}
impl std::ops::Mul<f32> for Rectangle<u32> {
@@ -41,3 +69,25 @@ impl std::ops::Mul<f32> for Rectangle<u32> {
}
}
}
+
+impl From<Rectangle<u32>> for Rectangle<f32> {
+ fn from(rectangle: Rectangle<u32>) -> Rectangle<f32> {
+ Rectangle {
+ x: rectangle.x as f32,
+ y: rectangle.y as f32,
+ width: rectangle.width as f32,
+ height: rectangle.height as f32,
+ }
+ }
+}
+
+impl From<Rectangle<f32>> for Rectangle<u32> {
+ fn from(rectangle: Rectangle<f32>) -> Rectangle<u32> {
+ Rectangle {
+ x: rectangle.x as u32,
+ y: rectangle.y as u32,
+ width: rectangle.width.ceil() as u32,
+ height: rectangle.height.ceil() as u32,
+ }
+ }
+}
diff --git a/core/src/vector.rs b/core/src/vector.rs
index 1c09ee3e..a75053a0 100644
--- a/core/src/vector.rs
+++ b/core/src/vector.rs
@@ -16,7 +16,7 @@ impl<T> Vector<T> {
/// Creates a new [`Vector`] with the given components.
///
/// [`Vector`]: struct.Vector.html
- pub fn new(x: T, y: T) -> Self {
+ pub const fn new(x: T, y: T) -> Self {
Self { x, y }
}
}
@@ -32,6 +32,17 @@ where
}
}
+impl<T> std::ops::Sub for Vector<T>
+where
+ T: std::ops::Sub<Output = T>,
+{
+ type Output = Self;
+
+ fn sub(self, b: Self) -> Self {
+ Self::new(self.x - b.x, self.y - b.y)
+ }
+}
+
impl<T> Default for Vector<T>
where
T: Default,