2021-03-21 16:47:03 +00:00
|
|
|
use std::ops::{Add, AddAssign, Sub, SubAssign};
|
2020-08-27 16:07:33 +00:00
|
|
|
|
2021-01-10 10:37:47 +00:00
|
|
|
use crate::*;
|
2020-08-27 16:07:33 +00:00
|
|
|
|
|
|
|
/// A position on screen.
|
|
|
|
///
|
2020-12-27 11:57:15 +00:00
|
|
|
/// Normally given in points (logical pixels).
|
|
|
|
///
|
|
|
|
/// Mathematically this is known as a "point", but the term position was chosen so not to
|
|
|
|
/// conflict with the unit (one point = X physical pixels).
|
2021-02-14 20:39:04 +00:00
|
|
|
#[derive(Clone, Copy, Default, PartialEq)]
|
2020-08-27 16:07:33 +00:00
|
|
|
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
|
|
|
|
pub struct Pos2 {
|
|
|
|
pub x: f32,
|
|
|
|
pub y: f32,
|
|
|
|
// implicit w = 1
|
|
|
|
}
|
|
|
|
|
2020-12-27 11:57:15 +00:00
|
|
|
/// `pos2(x,y) == Pos2::new(x, y)`
|
|
|
|
#[inline(always)]
|
2020-09-09 15:14:42 +00:00
|
|
|
pub const fn pos2(x: f32, y: f32) -> Pos2 {
|
2020-08-27 16:07:33 +00:00
|
|
|
Pos2 { x, y }
|
|
|
|
}
|
|
|
|
|
2021-01-25 20:11:19 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// Compatibility and convenience conversions to and from [f32; 2]:
|
|
|
|
|
2020-08-27 16:07:33 +00:00
|
|
|
impl From<[f32; 2]> for Pos2 {
|
2021-03-28 21:16:19 +00:00
|
|
|
#[inline(always)]
|
2020-08-27 16:07:33 +00:00
|
|
|
fn from(v: [f32; 2]) -> Self {
|
|
|
|
Self { x: v[0], y: v[1] }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<&[f32; 2]> for Pos2 {
|
2021-03-28 21:16:19 +00:00
|
|
|
#[inline(always)]
|
2020-08-27 16:07:33 +00:00
|
|
|
fn from(v: &[f32; 2]) -> Self {
|
|
|
|
Self { x: v[0], y: v[1] }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-25 20:11:19 +00:00
|
|
|
impl From<Pos2> for [f32; 2] {
|
2021-03-28 21:16:19 +00:00
|
|
|
#[inline(always)]
|
2021-01-25 20:11:19 +00:00
|
|
|
fn from(v: Pos2) -> Self {
|
|
|
|
[v.x, v.y]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<&Pos2> for [f32; 2] {
|
2021-03-28 21:16:19 +00:00
|
|
|
#[inline(always)]
|
2021-01-25 20:11:19 +00:00
|
|
|
fn from(v: &Pos2) -> Self {
|
|
|
|
[v.x, v.y]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// Compatibility and convenience conversions to and from (f32, f32):
|
|
|
|
|
|
|
|
impl From<(f32, f32)> for Pos2 {
|
2021-03-28 21:16:19 +00:00
|
|
|
#[inline(always)]
|
2021-01-25 20:11:19 +00:00
|
|
|
fn from(v: (f32, f32)) -> Self {
|
|
|
|
Self { x: v.0, y: v.1 }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<&(f32, f32)> for Pos2 {
|
2021-03-28 21:16:19 +00:00
|
|
|
#[inline(always)]
|
2021-01-25 20:11:19 +00:00
|
|
|
fn from(v: &(f32, f32)) -> Self {
|
|
|
|
Self { x: v.0, y: v.1 }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<Pos2> for (f32, f32) {
|
2021-03-28 21:16:19 +00:00
|
|
|
#[inline(always)]
|
2021-01-25 20:11:19 +00:00
|
|
|
fn from(v: Pos2) -> Self {
|
|
|
|
(v.x, v.y)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<&Pos2> for (f32, f32) {
|
2021-03-28 21:16:19 +00:00
|
|
|
#[inline(always)]
|
2021-01-25 20:11:19 +00:00
|
|
|
fn from(v: &Pos2) -> Self {
|
|
|
|
(v.x, v.y)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-08 06:17:01 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// Mint compatibility and convenience conversions
|
|
|
|
|
|
|
|
#[cfg(feature = "mint")]
|
|
|
|
impl From<mint::Point2<f32>> for Pos2 {
|
|
|
|
fn from(v: mint::Point2<f32>) -> Self {
|
|
|
|
Self::new(v.x, v.y)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(feature = "mint")]
|
|
|
|
impl From<Pos2> for mint::Point2<f32> {
|
|
|
|
fn from(v: Pos2) -> Self {
|
|
|
|
Self { x: v.x, y: v.y }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-25 20:11:19 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
2020-08-27 16:07:33 +00:00
|
|
|
impl Pos2 {
|
2021-01-16 19:57:31 +00:00
|
|
|
/// The zero position, the origin.
|
|
|
|
/// The top left corner in a GUI.
|
|
|
|
/// Same as `Pos2::default()`.
|
2021-02-05 08:49:21 +00:00
|
|
|
pub const ZERO: Self = Self { x: 0.0, y: 0.0 };
|
|
|
|
|
New text layout (#682)
This PR introduces a completely rewritten text layout engine which is simpler and more powerful. It allows mixing different text styles (heading, body, etc) and formats (color, underlining, strikethrough, …) in the same layout pass, and baked into the same `Galley`.
This opens up the door to having a syntax-highlighed code editor, or a WYSIWYG markdown editor.
One major change is the color is now baked in at layout time. However, many widgets changes text color on hovered. But we need to do the text layout before we know if it is hovered. Therefor the painter has an option to override the text color of a galley.
## Performance
Text layout alone is about 20% slower, but a lot of that is because more tessellation is done upfront. Text tessellation is now a lot faster, but text layout + tessellation still lands at a net loss of 5-10% in performance. There are however a few tricks to speed it up (like using `smallvec`) which I am saving for later. Text layout is also cached, meaning that in most cases (when all text isn't changing each frame) text tessellation is actually more important (and that's more than 2x faster!).
Sadly, the actual text cache lookup is significantly slower (300ns -> 600ns). That's because the `TextLayoutJob` is a lot bigger (it has more options, like underlining, fonts etc), so it is slower to hash and compare. I have an idea how to speed this up, but I need to do some other work before I can implement that.
All in all, the performance impact on `demo_with_tesselate__realistic` is about 5-6% in the red. Not great; not terrible. The benefits are worth it, but I also think with some work I can get that down significantly, hopefully down to the old levels.
2021-09-03 16:18:00 +00:00
|
|
|
#[inline(always)]
|
2020-09-09 15:14:42 +00:00
|
|
|
pub const fn new(x: f32, y: f32) -> Self {
|
2020-08-27 16:07:33 +00:00
|
|
|
Self { x, y }
|
|
|
|
}
|
|
|
|
|
2020-12-27 11:57:15 +00:00
|
|
|
/// The vector from origin to this position.
|
|
|
|
/// `p.to_vec2()` is equivalent to `p - Pos2::default()`.
|
2021-03-28 21:16:19 +00:00
|
|
|
#[inline(always)]
|
2020-08-27 16:07:33 +00:00
|
|
|
pub fn to_vec2(self) -> Vec2 {
|
|
|
|
Vec2 {
|
|
|
|
x: self.x,
|
|
|
|
y: self.y,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-01 20:10:34 +00:00
|
|
|
#[inline]
|
2020-10-08 20:24:55 +00:00
|
|
|
pub fn distance(self, other: Self) -> f32 {
|
2020-08-27 16:07:33 +00:00
|
|
|
(self - other).length()
|
|
|
|
}
|
|
|
|
|
2021-04-01 20:10:34 +00:00
|
|
|
#[inline]
|
2020-10-08 20:24:55 +00:00
|
|
|
pub fn distance_sq(self, other: Self) -> f32 {
|
2020-08-27 16:07:33 +00:00
|
|
|
(self - other).length_sq()
|
|
|
|
}
|
|
|
|
|
2021-04-01 20:10:34 +00:00
|
|
|
#[inline(always)]
|
2020-08-27 16:07:33 +00:00
|
|
|
pub fn floor(self) -> Self {
|
|
|
|
pos2(self.x.floor(), self.y.floor())
|
|
|
|
}
|
|
|
|
|
2021-04-01 20:10:34 +00:00
|
|
|
#[inline(always)]
|
2020-08-27 16:07:33 +00:00
|
|
|
pub fn round(self) -> Self {
|
|
|
|
pos2(self.x.round(), self.y.round())
|
|
|
|
}
|
|
|
|
|
2021-04-01 20:10:34 +00:00
|
|
|
#[inline(always)]
|
2020-08-27 16:07:33 +00:00
|
|
|
pub fn ceil(self) -> Self {
|
|
|
|
pos2(self.x.ceil(), self.y.ceil())
|
|
|
|
}
|
|
|
|
|
2021-03-15 20:26:58 +00:00
|
|
|
/// True if all members are also finite.
|
2021-04-01 20:10:34 +00:00
|
|
|
#[inline(always)]
|
2020-08-27 16:07:33 +00:00
|
|
|
pub fn is_finite(self) -> bool {
|
|
|
|
self.x.is_finite() && self.y.is_finite()
|
|
|
|
}
|
|
|
|
|
2021-03-15 20:26:58 +00:00
|
|
|
/// True if any member is NaN.
|
2021-04-01 20:10:34 +00:00
|
|
|
#[inline(always)]
|
2021-03-15 20:26:58 +00:00
|
|
|
pub fn any_nan(self) -> bool {
|
|
|
|
self.x.is_nan() || self.y.is_nan()
|
|
|
|
}
|
|
|
|
|
2020-08-27 16:07:33 +00:00
|
|
|
#[must_use]
|
2021-04-01 20:10:34 +00:00
|
|
|
#[inline]
|
2020-08-27 16:07:33 +00:00
|
|
|
pub fn min(self, other: Self) -> Self {
|
|
|
|
pos2(self.x.min(other.x), self.y.min(other.y))
|
|
|
|
}
|
|
|
|
|
|
|
|
#[must_use]
|
2021-04-01 20:10:34 +00:00
|
|
|
#[inline]
|
2020-08-27 16:07:33 +00:00
|
|
|
pub fn max(self, other: Self) -> Self {
|
|
|
|
pos2(self.x.max(other.x), self.y.max(other.y))
|
|
|
|
}
|
|
|
|
|
|
|
|
#[must_use]
|
2021-04-01 20:10:34 +00:00
|
|
|
#[inline]
|
2021-03-21 16:47:03 +00:00
|
|
|
pub fn clamp(self, min: Self, max: Self) -> Self {
|
2020-08-27 16:07:33 +00:00
|
|
|
Self {
|
2021-03-21 16:47:03 +00:00
|
|
|
x: self.x.clamp(min.x, max.x),
|
|
|
|
y: self.y.clamp(min.y, max.y),
|
2020-08-27 16:07:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-14 20:39:04 +00:00
|
|
|
impl std::ops::Index<usize> for Pos2 {
|
|
|
|
type Output = f32;
|
2021-03-28 21:16:19 +00:00
|
|
|
|
2021-04-01 20:10:34 +00:00
|
|
|
#[inline(always)]
|
2021-02-14 20:39:04 +00:00
|
|
|
fn index(&self, index: usize) -> &f32 {
|
|
|
|
match index {
|
|
|
|
0 => &self.x,
|
|
|
|
1 => &self.y,
|
|
|
|
_ => panic!("Pos2 index out of bounds: {}", index),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl std::ops::IndexMut<usize> for Pos2 {
|
2021-04-01 20:10:34 +00:00
|
|
|
#[inline(always)]
|
2021-02-14 20:39:04 +00:00
|
|
|
fn index_mut(&mut self, index: usize) -> &mut f32 {
|
|
|
|
match index {
|
|
|
|
0 => &mut self.x,
|
|
|
|
1 => &mut self.y,
|
|
|
|
_ => panic!("Pos2 index out of bounds: {}", index),
|
|
|
|
}
|
2020-08-27 16:07:33 +00:00
|
|
|
}
|
|
|
|
}
|
2021-02-14 20:39:04 +00:00
|
|
|
|
2020-08-27 16:07:33 +00:00
|
|
|
impl Eq for Pos2 {}
|
|
|
|
|
|
|
|
impl AddAssign<Vec2> for Pos2 {
|
2021-03-28 21:16:19 +00:00
|
|
|
#[inline(always)]
|
2020-08-27 16:07:33 +00:00
|
|
|
fn add_assign(&mut self, rhs: Vec2) {
|
|
|
|
*self = Pos2 {
|
|
|
|
x: self.x + rhs.x,
|
|
|
|
y: self.y + rhs.y,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl SubAssign<Vec2> for Pos2 {
|
2021-03-28 21:16:19 +00:00
|
|
|
#[inline(always)]
|
2020-08-27 16:07:33 +00:00
|
|
|
fn sub_assign(&mut self, rhs: Vec2) {
|
|
|
|
*self = Pos2 {
|
|
|
|
x: self.x - rhs.x,
|
|
|
|
y: self.y - rhs.y,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Add<Vec2> for Pos2 {
|
|
|
|
type Output = Pos2;
|
2021-03-28 21:16:19 +00:00
|
|
|
|
|
|
|
#[inline(always)]
|
2020-08-27 16:07:33 +00:00
|
|
|
fn add(self, rhs: Vec2) -> Pos2 {
|
|
|
|
Pos2 {
|
|
|
|
x: self.x + rhs.x,
|
|
|
|
y: self.y + rhs.y,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Sub for Pos2 {
|
|
|
|
type Output = Vec2;
|
2021-03-28 21:16:19 +00:00
|
|
|
|
|
|
|
#[inline(always)]
|
2020-08-27 16:07:33 +00:00
|
|
|
fn sub(self, rhs: Pos2) -> Vec2 {
|
|
|
|
Vec2 {
|
|
|
|
x: self.x - rhs.x,
|
|
|
|
y: self.y - rhs.y,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Sub<Vec2> for Pos2 {
|
|
|
|
type Output = Pos2;
|
2021-03-28 21:16:19 +00:00
|
|
|
|
|
|
|
#[inline(always)]
|
2020-08-27 16:07:33 +00:00
|
|
|
fn sub(self, rhs: Vec2) -> Pos2 {
|
|
|
|
Pos2 {
|
|
|
|
x: self.x - rhs.x,
|
|
|
|
y: self.y - rhs.y,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl std::fmt::Debug for Pos2 {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
write!(f, "[{:.1} {:.1}]", self.x, self.y)
|
|
|
|
}
|
|
|
|
}
|