Use actual floats for logical coordinates
This commit is contained in:
parent
9f29aec128
commit
67408311f4
12 changed files with 165 additions and 136 deletions
|
|
@ -1,26 +1,34 @@
|
|||
use crate::Vector;
|
||||
|
||||
use num_traits::{Float, Num};
|
||||
use std::fmt;
|
||||
|
||||
/// A 2D point.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Default)]
|
||||
pub struct Point {
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
|
||||
pub struct Point<T = f32> {
|
||||
/// The X coordinate.
|
||||
pub x: f32,
|
||||
pub x: T,
|
||||
|
||||
/// The Y coordinate.
|
||||
pub y: f32,
|
||||
pub y: T,
|
||||
}
|
||||
|
||||
impl Point {
|
||||
/// The origin (i.e. a [`Point`] at (0, 0)).
|
||||
pub const ORIGIN: Point = Point::new(0.0, 0.0);
|
||||
pub const ORIGIN: Self = Self::new(0.0, 0.0);
|
||||
}
|
||||
|
||||
impl<T: Num> Point<T> {
|
||||
/// Creates a new [`Point`] with the given coordinates.
|
||||
pub const fn new(x: f32, y: f32) -> Self {
|
||||
pub const fn new(x: T, y: T) -> Self {
|
||||
Self { x, y }
|
||||
}
|
||||
|
||||
/// Computes the distance to another [`Point`].
|
||||
pub fn distance(&self, to: Point) -> f32 {
|
||||
pub fn distance(&self, to: Self) -> T
|
||||
where
|
||||
T: Float,
|
||||
{
|
||||
let a = self.x - to.x;
|
||||
let b = self.y - to.y;
|
||||
|
||||
|
|
@ -34,9 +42,9 @@ impl From<[f32; 2]> for Point {
|
|||
}
|
||||
}
|
||||
|
||||
impl From<[u16; 2]> for Point {
|
||||
impl From<[u16; 2]> for Point<u16> {
|
||||
fn from([x, y]: [u16; 2]) -> Self {
|
||||
Point::new(x.into(), y.into())
|
||||
Point::new(x, y)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -46,10 +54,13 @@ impl From<Point> for [f32; 2] {
|
|||
}
|
||||
}
|
||||
|
||||
impl std::ops::Add<Vector> for Point {
|
||||
impl<T> std::ops::Add<Vector<T>> for Point<T>
|
||||
where
|
||||
T: std::ops::Add<Output = T>,
|
||||
{
|
||||
type Output = Self;
|
||||
|
||||
fn add(self, vector: Vector) -> Self {
|
||||
fn add(self, vector: Vector<T>) -> Self {
|
||||
Self {
|
||||
x: self.x + vector.x,
|
||||
y: self.y + vector.y,
|
||||
|
|
@ -57,10 +68,13 @@ impl std::ops::Add<Vector> for Point {
|
|||
}
|
||||
}
|
||||
|
||||
impl std::ops::Sub<Vector> for Point {
|
||||
impl<T> std::ops::Sub<Vector<T>> for Point<T>
|
||||
where
|
||||
T: std::ops::Sub<Output = T>,
|
||||
{
|
||||
type Output = Self;
|
||||
|
||||
fn sub(self, vector: Vector) -> Self {
|
||||
fn sub(self, vector: Vector<T>) -> Self {
|
||||
Self {
|
||||
x: self.x - vector.x,
|
||||
y: self.y - vector.y,
|
||||
|
|
@ -68,10 +82,22 @@ impl std::ops::Sub<Vector> for Point {
|
|||
}
|
||||
}
|
||||
|
||||
impl std::ops::Sub<Point> for Point {
|
||||
type Output = Vector;
|
||||
impl<T> std::ops::Sub<Point<T>> for Point<T>
|
||||
where
|
||||
T: std::ops::Sub<Output = T>,
|
||||
{
|
||||
type Output = Vector<T>;
|
||||
|
||||
fn sub(self, point: Point) -> Vector {
|
||||
fn sub(self, point: Self) -> Vector<T> {
|
||||
Vector::new(self.x - point.x, self.y - point.y)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> fmt::Display for Point<T>
|
||||
where
|
||||
T: fmt::Display,
|
||||
{
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
write!(f, "Point {{ x: {}, y: {} }}", self.x, self.y)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,10 +1,10 @@
|
|||
use crate::time::Instant;
|
||||
use crate::Size;
|
||||
use crate::{Point, Size};
|
||||
|
||||
use std::path::PathBuf;
|
||||
|
||||
/// A window-related event.
|
||||
#[derive(PartialEq, Eq, Clone, Debug)]
|
||||
#[derive(PartialEq, Clone, Debug)]
|
||||
pub enum Event {
|
||||
/// A window was moved.
|
||||
Moved {
|
||||
|
|
@ -30,22 +30,22 @@ pub enum Event {
|
|||
/// The user has requested for the window to close.
|
||||
CloseRequested,
|
||||
|
||||
/// A window was destroyed by the runtime.
|
||||
Destroyed,
|
||||
|
||||
/// A window was created.
|
||||
///
|
||||
/// **Note:** this event is not supported on Wayland.
|
||||
Created {
|
||||
/// The position of the created window. This is relative to the top-left corner of the desktop
|
||||
/// the window is on, including virtual desktops. Refers to window's "inner" position,
|
||||
/// or the client area, in logical pixels.
|
||||
position: (i32, i32),
|
||||
///
|
||||
/// **Note**: Not available in Wayland.
|
||||
position: Option<Point>,
|
||||
/// The size of the created window. This is its "inner" size, or the size of the
|
||||
/// client area, in logical pixels.
|
||||
size: Size<u32>,
|
||||
size: Size,
|
||||
},
|
||||
|
||||
/// A window was destroyed by the runtime.
|
||||
Destroyed,
|
||||
|
||||
/// A window was focused.
|
||||
Focused,
|
||||
|
||||
|
|
|
|||
|
|
@ -1,5 +1,7 @@
|
|||
use crate::Point;
|
||||
|
||||
/// The position of a window in a given screen.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
#[derive(Debug, Clone, Copy, PartialEq)]
|
||||
pub enum Position {
|
||||
/// The platform-specific default position for a new window.
|
||||
Default,
|
||||
|
|
@ -12,7 +14,7 @@ pub enum Position {
|
|||
/// position. So if you have decorations enabled and want the window to be
|
||||
/// at (0, 0) you would have to set the position to
|
||||
/// `(PADDING_X, PADDING_Y)`.
|
||||
Specific(i32, i32),
|
||||
Specific(Point),
|
||||
}
|
||||
|
||||
impl Default for Position {
|
||||
|
|
|
|||
|
|
@ -25,22 +25,23 @@ mod platform;
|
|||
mod platform;
|
||||
|
||||
use crate::window::{Icon, Level, Position};
|
||||
use crate::Size;
|
||||
|
||||
pub use platform::PlatformSpecific;
|
||||
/// The window settings of an application.
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Settings {
|
||||
/// The initial size of the window.
|
||||
pub size: (u32, u32),
|
||||
/// The initial logical dimensions of the window.
|
||||
pub size: Size,
|
||||
|
||||
/// The initial position of the window.
|
||||
pub position: Position,
|
||||
|
||||
/// The minimum size of the window.
|
||||
pub min_size: Option<(u32, u32)>,
|
||||
pub min_size: Option<Size>,
|
||||
|
||||
/// The maximum size of the window.
|
||||
pub max_size: Option<(u32, u32)>,
|
||||
pub max_size: Option<Size>,
|
||||
|
||||
/// Whether the window should be visible or not.
|
||||
pub visible: bool,
|
||||
|
|
@ -77,7 +78,7 @@ pub struct Settings {
|
|||
impl Default for Settings {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
size: (1024, 768),
|
||||
size: Size::new(1024.0, 768.0),
|
||||
position: Position::default(),
|
||||
min_size: None,
|
||||
max_size: None,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue