pushrod/geometry.rs
1// Pushrod
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15//! # Geometry
16//!
17//! These are geometric representations of shapes and coordinates in drawing space within
18//! `Pushrod`. All objects positioned within a window are drawn using these `Struct`s and
19//! method calls.
20//!
21//! `X` and `Y` positions are from the upper left-hand corner of the widget.
22
23/// A position in pixels: `x` from left to right, `y` from top to bottom.
24///
25/// `Widget` origins are `Point`s relative to the parent `Widget`'s upper left-hand corner.
26#[derive(Debug, Default, Copy, Clone, PartialEq, Eq)]
27pub struct Point {
28 /// Horizontal point from left to right in pixels.
29 pub x: i32,
30
31 /// Vertical point from top to bottom in pixels.
32 pub y: i32,
33}
34
35impl Point {
36 /// Creates a `Point`.
37 ///
38 /// # Arguments
39 ///
40 /// * `x` - The horizontal position in pixels, from the left.
41 /// * `y` - The vertical position in pixels, from the top.
42 ///
43 /// # Returns
44 ///
45 /// The new `Point`.
46 pub fn new(x: i32, y: i32) -> Self {
47 Self { x, y }
48 }
49}
50
51/// Creates a `Point`; shorthand for [`Point::new`].
52///
53/// # Arguments
54///
55/// * `x` - The horizontal position in pixels, from the left.
56/// * `y` - The vertical position in pixels, from the top.
57///
58/// # Returns
59///
60/// The new `Point`.
61pub fn point(x: i32, y: i32) -> Point {
62 Point::new(x, y)
63}
64
65/// A width and height in pixels.
66#[derive(Debug, Default, Copy, Clone, PartialEq, Eq)]
67pub struct Size {
68 /// Width in pixels.
69 pub w: u32,
70
71 /// Height in pixels.
72 pub h: u32,
73}
74
75impl Size {
76 /// Creates a `Size`.
77 ///
78 /// # Arguments
79 ///
80 /// * `w` - The width in pixels.
81 /// * `h` - The height in pixels.
82 ///
83 /// # Returns
84 ///
85 /// The new `Size`.
86 pub fn new(w: u32, h: u32) -> Self {
87 Self { w, h }
88 }
89
90 /// Returns the GPU memory a texture of this size uses.
91 ///
92 /// # Returns
93 ///
94 /// The size in bytes: width times height times 4 (one byte each for red, green, blue, and alpha).
95 pub fn get_memory_size(&self) -> u32 {
96 self.w * self.h * 4
97 }
98}
99
100/// Creates a `Size`; shorthand for [`Size::new`].
101///
102/// # Arguments
103///
104/// * `w` - The width in pixels.
105/// * `h` - The height in pixels.
106///
107/// # Returns
108///
109/// The new `Size`.
110pub fn size(w: u32, h: u32) -> Size {
111 Size::new(w, h)
112}
113
114/// A rectangle: an upper left-hand corner and a size, in pixels.
115#[derive(Debug, Default, Copy, Clone, PartialEq, Eq)]
116pub struct Rect {
117 /// Left edge in pixels.
118 pub x: i32,
119
120 /// Top edge in pixels.
121 pub y: i32,
122
123 /// Width in pixels.
124 pub w: u32,
125
126 /// Height in pixels.
127 pub h: u32,
128}
129
130impl Rect {
131 /// Creates a `Rect`.
132 ///
133 /// # Arguments
134 ///
135 /// * `x` - The left edge in pixels.
136 /// * `y` - The top edge in pixels.
137 /// * `w` - The width in pixels.
138 /// * `h` - The height in pixels.
139 ///
140 /// # Returns
141 ///
142 /// The new `Rect`.
143 pub fn new(x: i32, y: i32, w: u32, h: u32) -> Self {
144 Self { x, y, w, h }
145 }
146}
147
148/// Creates a rectangle from an origin and a size.
149///
150/// # Arguments
151///
152/// * `origin` - The upper left-hand corner.
153/// * `bounds` - The width and height.
154///
155/// # Returns
156///
157/// The rectangle.
158pub fn make_rect(origin: Point, bounds: Size) -> Rect {
159 Rect::new(origin.x, origin.y, bounds.w, bounds.h)
160}
161
162/// Creates a rectangle from its position and size.
163///
164/// # Arguments
165///
166/// * `x` - The left edge in pixels.
167/// * `y` - The top edge in pixels.
168/// * `w` - The width in pixels.
169/// * `h` - The height in pixels.
170///
171/// # Returns
172///
173/// The rectangle.
174pub fn rect(x: i32, y: i32, w: u32, h: u32) -> Rect {
175 Rect::new(x, y, w, h)
176}
177
178/// Returns the point `(0, 0)`.
179///
180/// # Returns
181///
182/// A `Point` at the origin.
183pub fn origin_point() -> Point {
184 Point { x: 0, y: 0 }
185}