1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
//! Offset type
use iced::{Point, Rectangle};
/// A 2D offset vector with a horizontal and vertical offset in pixels.
#[derive(Debug, Copy, Clone, PartialEq)]
pub struct Offset {
/// The horizontal offset in pixels.
pub x: f32,
/// the vertical offset in pixels.
pub y: f32,
}
impl Offset {
/// An [`Offset`] with zero x and zero y offset.
///
/// [`Offset`]: struct.Offset.html
pub const ZERO: Offset = Offset { x: 0.0, y: 0.0 };
/// Creates a new [`Offset`].
///
/// `x` - The horizontal offset in pixels.
/// `y` - The vertical offset in pixels.
///
/// [`Offset`]: struct.Offset.html
pub const fn new(x: f32, y: f32) -> Self {
Self { x, y }
}
/// Return an offsetted rectangle.
#[inline]
pub fn offset_rect(&self, rect: &Rectangle) -> Rectangle {
Rectangle {
x: rect.x + self.x,
y: rect.y + self.y,
width: rect.width,
height: rect.height,
}
}
/// Offset the given rectangle.
#[inline]
pub fn offset_rect_mut(&self, rect: &mut Rectangle) {
rect.x += self.x;
rect.y += self.y;
}
}
impl Default for Offset {
fn default() -> Self {
Offset::ZERO
}
}
impl From<Offset> for Point {
fn from(offset: Offset) -> Self {
Point {
x: offset.x,
y: offset.y,
}
}
}