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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
use crate::Vec2;
/// View Bounds
/// ::::Used For::::
/// Clipping Text, Within Text internally.
/// Clipping objects, Using Rendering Scissor.
/// Checking Coords.
#[derive(Copy, Clone, Debug, PartialEq)]
pub struct Bounds {
pub left: f32,
pub bottom: f32,
pub right: f32,
pub top: f32,
}
impl Bounds {
/// Used to create [`Bounds`].
///
/// # Arguments
/// - left: Position from the left side of the screen.
/// - bottom: Position from the bottom of the screen.
/// - right: Position from the left side + Right offset.
/// - top: Position from the bottom of the screen + top offset.
///
pub fn new(left: f32, bottom: f32, right: f32, top: f32) -> Self {
Self {
left,
bottom,
right,
top,
}
}
/// Used to update offset x and y within a limited range.
///
/// # Arguments
/// - offset: Variable to Set and check against Self [`Bounds`] and limit [`Bounds`].
/// - limits: Limit of what we will allow for lower or higher offset changes.
///
pub fn set_offset_within_limits(&self, offset: &mut Vec2, limits: &Bounds) {
if self.left + offset.x < limits.left {
offset.x = limits.left - self.left;
} else if self.right + offset.x > limits.right {
offset.x = limits.right - self.right;
}
if self.bottom + offset.y < limits.bottom {
offset.y = limits.bottom - self.bottom;
} else if self.top + offset.y > limits.top {
offset.y = limits.top - self.top;
}
}
/// Used to add offset to [`Bounds`].
///
/// # Arguments
/// - offset: Amount to move the [`Bounds`].
///
pub fn add_offset(&mut self, offset: Vec2) {
self.left += offset.x;
self.right += offset.x;
self.top += offset.y;
self.bottom += offset.y;
}
/// Used to adjust [`Bounds`] to a limited range.
///
/// # Arguments
/// - limits: limits to limit the [`Bounds`] too.
///
pub fn set_within_limits(&mut self, limits: &Bounds) {
if self.left < limits.left {
self.left = limits.left;
}
if self.bottom < limits.bottom {
self.bottom = limits.bottom;
}
if self.top > limits.top {
self.top = limits.top;
}
if self.right > limits.right {
self.right = limits.right;
}
}
}
impl Default for Bounds {
fn default() -> Self {
Self {
left: 0.0,
bottom: 0.0,
right: 2_147_483_600.0,
top: 2_147_483_600.0,
}
}
}