Skip to content

Commit 5e2a007

Browse files
committed
Added valid state to ConstrainedRect
Added Reset to ConstrainedRect Added Reset to Constraint
1 parent 891ac72 commit 5e2a007

File tree

4 files changed

+72
-14
lines changed

4 files changed

+72
-14
lines changed

Editor/ConstrainedRect.cs

Lines changed: 45 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ namespace TNRD.Constraints
44
{
55
public class ConstrainedRect
66
{
7-
private readonly Rect parent;
7+
private Rect parent;
88

99
public Constraint Top { get; }
1010
public Constraint Bottom { get; }
@@ -16,39 +16,75 @@ public class ConstrainedRect
1616
private bool centerHorizontally;
1717
private bool centerVertically;
1818

19-
public ConstrainedRect(Rect parent)
20-
{
21-
this.parent = parent;
19+
private bool isValid;
2220

21+
internal ConstrainedRect(Rect parent)
22+
{
2323
Top = new Constraint(this, false);
2424
Bottom = new Constraint(this, true);
2525
Left = new Constraint(this, false);
2626
Right = new Constraint(this, true);
2727
Width = new Constraint(this, false);
2828
Height = new Constraint(this, false);
29+
30+
Reset(parent);
31+
}
32+
33+
internal void Reset(Rect parent)
34+
{
35+
this.parent = parent;
36+
37+
Top.Reset();
38+
Bottom.Reset();
39+
Left.Reset();
40+
Right.Reset();
41+
Width.Reset();
42+
Height.Reset();
43+
44+
isValid = true;
2945
}
3046

3147
public Rect Relative(float value)
3248
{
49+
ThrowIfInvalid();
3350
return Relative(value, value, value, value);
3451
}
3552

3653
public Rect Relative(float left, float top, float right, float bottom)
3754
{
55+
ThrowIfInvalid();
3856
return Left.Relative(left)
3957
.Top.Relative(top)
4058
.Right.Relative(right)
4159
.Bottom.Relative(bottom)
4260
.ToRect();
4361
}
4462

63+
public ConstrainedRect CenterHorizontally()
64+
{
65+
ThrowIfInvalid();
66+
centerHorizontally = true;
67+
return this;
68+
}
69+
70+
public ConstrainedRect CenterVertically()
71+
{
72+
ThrowIfInvalid();
73+
centerVertically = true;
74+
return this;
75+
}
76+
4577
public Rect ToRect()
4678
{
47-
float left = 0, top = 0, right = 0, bottom = 0;
79+
ThrowIfInvalid();
80+
float left, top, right, bottom;
4881

4982
CalculateHorizontal(out left, out right);
5083
CalculateVertical(out top, out bottom);
5184

85+
ConstrainedRectPool.Return(this);
86+
isValid = false;
87+
5288
return new Rect(left, top, right, bottom);
5389
}
5490

@@ -122,16 +158,12 @@ private void CalculateVertical(out float top, out float bottom)
122158
}
123159
}
124160

125-
public ConstrainedRect CenterHorizontally()
161+
internal void ThrowIfInvalid()
126162
{
127-
centerHorizontally = true;
128-
return this;
129-
}
163+
if (isValid)
164+
return;
130165

131-
public ConstrainedRect CenterVertically()
132-
{
133-
centerVertically = true;
134-
return this;
166+
throw new InvalidStateException();
135167
}
136168
}
137169
}

Editor/Constraint.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,35 +21,46 @@ private enum ConstrainMode
2121

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

24-
public Constraint(ConstrainedRect parent, bool negateValue)
24+
internal Constraint(ConstrainedRect parent, bool negateValue)
2525
{
2626
this.parent = parent;
2727
this.negateValue = negateValue;
28+
Reset();
29+
}
30+
31+
internal void Reset()
32+
{
33+
mode = ConstrainMode.NotSet;
34+
value = 0;
2835
}
2936

3037
public ConstrainedRect Relative(float value)
3138
{
39+
parent.ThrowIfInvalid();
3240
mode = ConstrainMode.Relative;
3341
this.value = value;
3442
return parent;
3543
}
3644

3745
public ConstrainedRect Absolute(float value)
3846
{
47+
parent.ThrowIfInvalid();
3948
mode = ConstrainMode.Absolute;
4049
this.value = value;
4150
return parent;
4251
}
4352

4453
public ConstrainedRect Percentage(float value)
4554
{
55+
parent.ThrowIfInvalid();
4656
mode = ConstrainMode.Percentage;
4757
this.value = value;
4858
return parent;
4959
}
5060

5161
internal float Apply(float value)
5262
{
63+
parent.ThrowIfInvalid();
5364
switch (mode)
5465
{
5566
case ConstrainMode.Relative:

Editor/InvalidStateException.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using System;
2+
3+
namespace TNRD.Constraints
4+
{
5+
public class InvalidStateException : Exception
6+
{
7+
public InvalidStateException()
8+
: base("The Constrained Rect that you're trying to modify has already been pooled and is no longer available for usage")
9+
{
10+
}
11+
}
12+
}

Editor/InvalidStateException.cs.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)