Skip to content

Commit

Permalink
Implement Scalable for Vec2/Line/Insets.
Browse files Browse the repository at this point in the history
  • Loading branch information
xStrom committed May 9, 2020
1 parent a12357a commit 9c494bc
Showing 1 changed file with 61 additions and 1 deletion.
62 changes: 61 additions & 1 deletion druid-shell/src/scale.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
use float_cmp::ApproxEq;

use crate::kurbo::{Point, Rect, Size};
use crate::kurbo::{Insets, Line, Point, Rect, Size, Vec2};

const SCALE_TARGET_DPI: f64 = 96.0;

Expand Down Expand Up @@ -242,6 +242,22 @@ impl std::fmt::Debug for Scale {
}
}

impl Scalable for Vec2 {
/// Converts a `Vec2` from display points into pixels,
/// using the x axis scale factor for `x` and the y axis scale factor for `y`.
#[inline]
fn to_px(&self, scale: &Scale) -> Vec2 {
Vec2::new(self.x * scale.scale_x, self.y * scale.scale_y)
}

/// Converts a `Vec2` from pixels into display points,
/// using the x axis scale factor for `x` and the y axis scale factor for `y`.
#[inline]
fn to_dp(&self, scale: &Scale) -> Vec2 {
Vec2::new(self.x / scale.scale_x, self.y / scale.scale_y)
}
}

impl Scalable for Point {
/// Converts a `Point` from display points into pixels,
/// using the x axis scale factor for `x` and the y axis scale factor for `y`.
Expand All @@ -258,6 +274,22 @@ impl Scalable for Point {
}
}

impl Scalable for Line {
/// Converts a `Line` from display points into pixels,
/// using the x axis scale factor for `x` and the y axis scale factor for `y`.
#[inline]
fn to_px(&self, scale: &Scale) -> Line {
Line::new(self.p0.to_px(scale), self.p1.to_px(scale))
}

/// Converts a `Line` from pixels into display points,
/// using the x axis scale factor for `x` and the y axis scale factor for `y`.
#[inline]
fn to_dp(&self, scale: &Scale) -> Line {
Line::new(self.p0.to_dp(scale), self.p1.to_dp(scale))
}
}

impl Scalable for Size {
/// Converts a `Size` from display points into pixels,
/// using the x axis scale factor for `width`
Expand Down Expand Up @@ -304,6 +336,34 @@ impl Scalable for Rect {
}
}

impl Scalable for Insets {
/// Converts `Insets` from display points into pixels,
/// using the x axis scale factor for `x0` and `x1`
/// and the y axis scale factor for `y0` and `y1`.
#[inline]
fn to_px(&self, scale: &Scale) -> Insets {
Insets::new(
self.x0 * scale.scale_x,
self.y0 * scale.scale_y,
self.x1 * scale.scale_x,
self.y1 * scale.scale_y,
)
}

/// Converts `Insets` from pixels into display points,
/// using the x axis scale factor for `x0` and `x1`
/// and the y axis scale factor for `y0` and `y1`.
#[inline]
fn to_dp(&self, scale: &Scale) -> Insets {
Insets::new(
self.x0 / scale.scale_x,
self.y0 / scale.scale_y,
self.x1 / scale.scale_x,
self.y1 / scale.scale_y,
)
}
}

// TODO: Replace usages of this with rect.expand() after kurbo#107 has landed.
#[allow(dead_code)]
#[doc(hidden)]
Expand Down

0 comments on commit 9c494bc

Please sign in to comment.