Skip to content

Commit

Permalink
ADD CenterHorizontally and CenterVertically
Browse files Browse the repository at this point in the history
  • Loading branch information
Christiaan Bloemendaal committed Jun 7, 2020
1 parent 6f38df7 commit 25d6515
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 6 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## [2.1.0] - 2020-06-07
## Added
- Options to center horizontally and vertically

## [2.0.2] - 2020-02-19
## Fixed
- Incorrect naming of assembly definitions
Expand All @@ -18,4 +22,4 @@

## [1.0.0] - 2020-02-16
### Added
- Initial version
- Initial version
35 changes: 34 additions & 1 deletion Editor/ConstrainedRect.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ public class ConstrainedRect
public Constraint Width { get; }
public Constraint Height { get; }

private bool centerHorizontally;
private bool centerVertically;

public ConstrainedRect(Rect parent)
{
this.parent = parent;
Expand Down Expand Up @@ -51,6 +54,15 @@ public Rect ToRect()

private void CalculateHorizontal(out float left, out float right)
{
if (centerHorizontally && Width.IsSet)
{
float width = Mathf.Abs(Width.RawValue);
left = parent.center.x - width * 0.5f;
right = width;

return;
}

if (!Width.IsSet || (Left.IsSet && Right.IsSet))
{
left = Left.Apply(parent.xMin);
Expand All @@ -77,6 +89,15 @@ private void CalculateHorizontal(out float left, out float right)

private void CalculateVertical(out float top, out float bottom)
{
if (centerVertically && Height.IsSet)
{
float height = Mathf.Abs(Height.RawValue);
top = parent.center.y - height * 0.5f;
bottom = height;

return;
}

if (!Height.IsSet || (Top.IsSet && Bottom.IsSet))
{
top = Top.Apply(parent.yMin);
Expand All @@ -100,5 +121,17 @@ private void CalculateVertical(out float top, out float bottom)
bottom = Height.Apply(parent.height);
}
}

public ConstrainedRect CenterHorizontally()
{
centerHorizontally = true;
return this;
}

public ConstrainedRect CenterVertically()
{
centerVertically = true;
return this;
}
}
}
}
4 changes: 3 additions & 1 deletion Editor/Constraint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ private enum ConstrainMode

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

internal float RawValue => value;

public bool IsSet => mode != ConstrainMode.NotSet;

public Constraint(ConstrainedRect parent, bool negateValue)
Expand Down Expand Up @@ -61,4 +63,4 @@ internal float Apply(float value)
return value;
}
}
}
}
28 changes: 27 additions & 1 deletion Tests/Editor/ConstrainedRectTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -161,5 +161,31 @@ public void BottomHeightPercentage()
Assert.AreEqual(120, constrained.yMax);
Assert.AreEqual(64, constrained.height);
}

[Test]
public void CenterHorizontally()
{
var constrained = Constrain.To(rect)
.Width.Absolute(20)
.CenterHorizontally()
.ToRect();

Assert.AreEqual(20, constrained.width);
Assert.AreEqual(128 / 2 - 10, constrained.xMin);
Assert.AreEqual(128 / 2 + 10, constrained.xMax);
}

[Test]
public void CenterVertically()
{
var constrained = Constrain.To(rect)
.Height.Absolute(20)
.CenterVertically()
.ToRect();

Assert.AreEqual(20, constrained.height);
Assert.AreEqual(128 / 2 - 10, constrained.yMin);
Assert.AreEqual(128 / 2 + 10, constrained.yMax);
}
}
}
}
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "net.tnrd.constrainedrect",
"version": "2.0.2",
"version": "2.1.0",
"displayName": "Constrained Rect",
"description": "A simple helper to constrain a Rect to an EditorWindow or another Rect",
"unity": "2019.1",
Expand All @@ -16,4 +16,4 @@
"url": "https://www.tnrd.net"
},
"dependencies": {}
}
}

0 comments on commit 25d6515

Please sign in to comment.