Skip to content

Commit 25d6515

Browse files
author
Christiaan Bloemendaal
committed
ADD CenterHorizontally and CenterVertically
1 parent 6f38df7 commit 25d6515

File tree

5 files changed

+71
-6
lines changed

5 files changed

+71
-6
lines changed

CHANGELOG.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
## [2.1.0] - 2020-06-07
4+
## Added
5+
- Options to center horizontally and vertically
6+
37
## [2.0.2] - 2020-02-19
48
## Fixed
59
- Incorrect naming of assembly definitions
@@ -18,4 +22,4 @@
1822

1923
## [1.0.0] - 2020-02-16
2024
### Added
21-
- Initial version
25+
- Initial version

Editor/ConstrainedRect.cs

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ public class ConstrainedRect
1313
public Constraint Width { get; }
1414
public Constraint Height { get; }
1515

16+
private bool centerHorizontally;
17+
private bool centerVertically;
18+
1619
public ConstrainedRect(Rect parent)
1720
{
1821
this.parent = parent;
@@ -51,6 +54,15 @@ public Rect ToRect()
5154

5255
private void CalculateHorizontal(out float left, out float right)
5356
{
57+
if (centerHorizontally && Width.IsSet)
58+
{
59+
float width = Mathf.Abs(Width.RawValue);
60+
left = parent.center.x - width * 0.5f;
61+
right = width;
62+
63+
return;
64+
}
65+
5466
if (!Width.IsSet || (Left.IsSet && Right.IsSet))
5567
{
5668
left = Left.Apply(parent.xMin);
@@ -77,6 +89,15 @@ private void CalculateHorizontal(out float left, out float right)
7789

7890
private void CalculateVertical(out float top, out float bottom)
7991
{
92+
if (centerVertically && Height.IsSet)
93+
{
94+
float height = Mathf.Abs(Height.RawValue);
95+
top = parent.center.y - height * 0.5f;
96+
bottom = height;
97+
98+
return;
99+
}
100+
80101
if (!Height.IsSet || (Top.IsSet && Bottom.IsSet))
81102
{
82103
top = Top.Apply(parent.yMin);
@@ -100,5 +121,17 @@ private void CalculateVertical(out float top, out float bottom)
100121
bottom = Height.Apply(parent.height);
101122
}
102123
}
124+
125+
public ConstrainedRect CenterHorizontally()
126+
{
127+
centerHorizontally = true;
128+
return this;
129+
}
130+
131+
public ConstrainedRect CenterVertically()
132+
{
133+
centerVertically = true;
134+
return this;
135+
}
103136
}
104-
}
137+
}

Editor/Constraint.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ private enum ConstrainMode
1717

1818
private float Value => negateValue ? -value : value;
1919

20+
internal float RawValue => value;
21+
2022
public bool IsSet => mode != ConstrainMode.NotSet;
2123

2224
public Constraint(ConstrainedRect parent, bool negateValue)
@@ -61,4 +63,4 @@ internal float Apply(float value)
6163
return value;
6264
}
6365
}
64-
}
66+
}

Tests/Editor/ConstrainedRectTests.cs

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,5 +161,31 @@ public void BottomHeightPercentage()
161161
Assert.AreEqual(120, constrained.yMax);
162162
Assert.AreEqual(64, constrained.height);
163163
}
164+
165+
[Test]
166+
public void CenterHorizontally()
167+
{
168+
var constrained = Constrain.To(rect)
169+
.Width.Absolute(20)
170+
.CenterHorizontally()
171+
.ToRect();
172+
173+
Assert.AreEqual(20, constrained.width);
174+
Assert.AreEqual(128 / 2 - 10, constrained.xMin);
175+
Assert.AreEqual(128 / 2 + 10, constrained.xMax);
176+
}
177+
178+
[Test]
179+
public void CenterVertically()
180+
{
181+
var constrained = Constrain.To(rect)
182+
.Height.Absolute(20)
183+
.CenterVertically()
184+
.ToRect();
185+
186+
Assert.AreEqual(20, constrained.height);
187+
Assert.AreEqual(128 / 2 - 10, constrained.yMin);
188+
Assert.AreEqual(128 / 2 + 10, constrained.yMax);
189+
}
164190
}
165-
}
191+
}

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "net.tnrd.constrainedrect",
3-
"version": "2.0.2",
3+
"version": "2.1.0",
44
"displayName": "Constrained Rect",
55
"description": "A simple helper to constrain a Rect to an EditorWindow or another Rect",
66
"unity": "2019.1",
@@ -16,4 +16,4 @@
1616
"url": "https://www.tnrd.net"
1717
},
1818
"dependencies": {}
19-
}
19+
}

0 commit comments

Comments
 (0)