Skip to content

Commit 8a3cbca

Browse files
committed
UPDATE Calculation method
Width and height no longer override right and top by default
1 parent 1d4789b commit 8a3cbca

File tree

2 files changed

+53
-17
lines changed

2 files changed

+53
-17
lines changed

Editor/ConstrainedRect.cs

Lines changed: 51 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -41,26 +41,64 @@ public Rect Relative(float left, float top, float right, float bottom)
4141

4242
public Rect ToRect()
4343
{
44-
var left = Left.Apply(parent.xMin);
45-
var top = Top.Apply(parent.yMin);
46-
var width = Width.IsSet ? Width.Apply(parent.width) : CalculateWidth();
47-
var height = Height.IsSet ? Height.Apply(parent.height) : CalculateHeight();
44+
float left = 0, top = 0, right = 0, bottom = 0;
4845

49-
return new Rect(left, top, width, height);
46+
CalculateHorizontal(out left, out right);
47+
CalculateVertical(out top, out bottom);
48+
49+
return new Rect(left, top, right, bottom);
5050
}
5151

52-
private float CalculateWidth()
52+
private void CalculateHorizontal(out float left, out float right)
5353
{
54-
var right = Right.Apply(parent.xMax);
55-
var left = Left.Apply(parent.xMin);
56-
return right - left;
54+
if (!Width.IsSet || (Left.IsSet && Right.IsSet))
55+
{
56+
left = Left.Apply(parent.xMin);
57+
right = Right.Apply(parent.xMax) - left;
58+
return;
59+
}
60+
61+
if (Left.IsSet && !Right.IsSet)
62+
{
63+
left = Left.Apply(parent.xMin);
64+
right = Width.Apply(parent.width);
65+
}
66+
else if (!Left.IsSet && Right.IsSet)
67+
{
68+
right = Width.Apply(parent.width);
69+
left = Right.Apply(parent.xMax) - right;
70+
}
71+
else
72+
{
73+
left = Left.Apply(parent.xMin);
74+
right = Width.Apply(parent.width);
75+
}
5776
}
5877

59-
private float CalculateHeight()
78+
private void CalculateVertical(out float top, out float bottom)
6079
{
61-
var bottom = Bottom.Apply(parent.yMax);
62-
var top = Top.Apply(parent.yMin);
63-
return bottom - top;
80+
if (!Height.IsSet || (Top.IsSet && Bottom.IsSet))
81+
{
82+
top = Top.Apply(parent.yMin);
83+
bottom = Bottom.Apply(parent.yMax) - top;
84+
return;
85+
}
86+
87+
if (Top.IsSet && !Bottom.IsSet)
88+
{
89+
top = Top.Apply(parent.yMin);
90+
bottom = Height.Apply(parent.height);
91+
}
92+
else if (!Top.IsSet && Bottom.IsSet)
93+
{
94+
bottom = Height.Apply(parent.height);
95+
top = Bottom.Apply(parent.yMax) - bottom;
96+
}
97+
else
98+
{
99+
top = Top.Apply(parent.yMin);
100+
bottom = Height.Apply(parent.height);
101+
}
64102
}
65103
}
66104
}

Editor/Constraint.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
using System;
2-
3-
namespace TNRD.Constraints
1+
namespace TNRD.Constraints
42
{
53
public class Constraint
64
{
@@ -13,8 +11,8 @@ private enum ConstrainMode
1311
}
1412

1513
private readonly ConstrainedRect parent;
14+
private readonly bool negateValue;
1615
private ConstrainMode mode;
17-
private bool negateValue;
1816
private float value;
1917

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

0 commit comments

Comments
 (0)