fyrox_ui/thickness.rs
1// Copyright (c) 2019-present Dmitry Stepanov and Fyrox Engine contributors.
2//
3// Permission is hereby granted, free of charge, to any person obtaining a copy
4// of this software and associated documentation files (the "Software"), to deal
5// in the Software without restriction, including without limitation the rights
6// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7// copies of the Software, and to permit persons to whom the Software is
8// furnished to do so, subject to the following conditions:
9//
10// The above copyright notice and this permission notice shall be included in all
11// copies or substantial portions of the Software.
12//
13// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19// SOFTWARE.
20
21//! Describes the thickness of a frame around a rectangle (for all four sides).
22
23use crate::core::{algebra::Vector2, reflect::prelude::*, visitor::prelude::*};
24
25/// Describes the thickness of a frame around a rectangle (for all four sides). It is primarily used to
26/// define margins and to define stroke thickness for various widgets.
27#[derive(Copy, Clone, PartialEq, Debug, Reflect, Visit)]
28pub struct Thickness {
29 /// Thickness of the left side of a rectangle.
30 pub left: f32,
31 /// Thickness of the top side of a rectangle.
32 pub top: f32,
33 /// Thickness of the right side of a rectangle.
34 pub right: f32,
35 /// Thickness of the bottom side of a rectangle.
36 pub bottom: f32,
37}
38
39impl Default for Thickness {
40 fn default() -> Self {
41 Self::uniform(0.0)
42 }
43}
44
45impl Thickness {
46 /// Degenerate thickness that has no effect.
47 pub fn zero() -> Self {
48 Self {
49 left: 0.0,
50 top: 0.0,
51 right: 0.0,
52 bottom: 0.0,
53 }
54 }
55
56 /// Uniform thickness for all four sides of a rectangle.
57 pub fn uniform(v: f32) -> Self {
58 Self {
59 left: v,
60 top: v,
61 right: v,
62 bottom: v,
63 }
64 }
65
66 /// Thickness for the bottom side of a rectangle.
67 pub fn bottom(v: f32) -> Self {
68 Self {
69 left: 0.0,
70 top: 0.0,
71 right: 0.0,
72 bottom: v,
73 }
74 }
75
76 /// Thickness for the top side of a rectangle.
77 pub fn top(v: f32) -> Self {
78 Self {
79 left: 0.0,
80 top: v,
81 right: 0.0,
82 bottom: 0.0,
83 }
84 }
85
86 /// Thickness for the left side of a rectangle.
87 pub fn left(v: f32) -> Self {
88 Self {
89 left: v,
90 top: 0.0,
91 right: 0.0,
92 bottom: 0.0,
93 }
94 }
95
96 /// Thickness for the rigth side of a rectangle.
97 pub fn right(v: f32) -> Self {
98 Self {
99 left: 0.0,
100 top: 0.0,
101 right: v,
102 bottom: 0.0,
103 }
104 }
105
106 /// Thickness for the top and right sides of a rectangle.
107 pub fn top_right(v: f32) -> Self {
108 Self {
109 left: 0.0,
110 top: v,
111 right: v,
112 bottom: 0.0,
113 }
114 }
115
116 /// Thickness for the top and left sides of a rectangle.
117 pub fn top_left(v: f32) -> Self {
118 Self {
119 left: v,
120 top: v,
121 right: 0.0,
122 bottom: 0.0,
123 }
124 }
125
126 /// Thickness for the bottom and right sides of a rectangle.
127 pub fn bottom_right(v: f32) -> Self {
128 Self {
129 left: 0.0,
130 top: 0.0,
131 right: v,
132 bottom: v,
133 }
134 }
135
136 /// Thickness for the bottom and left sides of a rectangle.
137 pub fn bottom_left(v: f32) -> Self {
138 Self {
139 left: v,
140 top: 0.0,
141 right: 0.0,
142 bottom: v,
143 }
144 }
145
146 /// Thickness for the top and bottom sides of a rectangle.
147 pub fn top_bottom(v: f32) -> Self {
148 Self {
149 left: 0.0,
150 top: v,
151 right: 0.0,
152 bottom: v,
153 }
154 }
155
156 /// Thickness for the left and right sides of a rectangle.
157 pub fn left_right(v: f32) -> Self {
158 Self {
159 left: v,
160 top: 0.0,
161 right: v,
162 bottom: 0.0,
163 }
164 }
165
166 /// Returns an offset defined by this thickness. It is just a vector `(left, top)`.
167 pub fn offset(&self) -> Vector2<f32> {
168 Vector2::new(self.left, self.top)
169 }
170
171 /// Returns a margin for each axis (horizontal and vertical).
172 pub fn axes_margin(&self) -> Vector2<f32> {
173 Vector2::new(self.left + self.right, self.top + self.bottom)
174 }
175}