Skip to content

Commit f17ffd6

Browse files
authored
Merge pull request #1 from Thundernerd/develop
Added pooling for performance gains
2 parents 25d6515 + b7e1691 commit f17ffd6

11 files changed

+153
-35
lines changed

.codacy.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
exclude_paths:
2+
- '*.md'
3+
- '*.json'

CHANGELOG.md

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

3+
## [3.0.0] - 2020-08-02
4+
### Added
5+
- Pooling of Constrained Rect
6+
37
## [2.1.0] - 2020-06-07
4-
## Added
8+
### Added
59
- Options to center horizontally and vertically
610

711
## [2.0.2] - 2020-02-19
8-
## Fixed
12+
### Fixed
913
- Incorrect naming of assembly definitions
1014

1115
## [2.0.1] - 2020-02-19
12-
## Fixed
16+
### Fixed
1317
- Tests assembly references
1418

1519
## [2.0.0] - 2020-02-17
16-
## Added
20+
### Added
1721
- Tests
1822

19-
## Changed
23+
### Changed
2024
- Calculation method for left, top, right, bottom
2125
- Width and height no longer override right and bottom by default
2226

Editor/Constrain.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public static ConstrainedRect To(EditorWindow editorWindow)
1212

1313
public static ConstrainedRect To(Rect rect)
1414
{
15-
return new ConstrainedRect(rect);
15+
return ConstrainedRectPool.Create(rect);
1616
}
1717
}
18-
}
18+
}

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/ConstrainedRectPool.cs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
using System.Collections.Generic;
2+
using UnityEngine;
3+
4+
namespace TNRD.Constraints
5+
{
6+
internal static class ConstrainedRectPool
7+
{
8+
private static List<ConstrainedRect> available = new List<ConstrainedRect>();
9+
10+
public static ConstrainedRect Create(Rect to)
11+
{
12+
if (available.Count == 0)
13+
{
14+
return CreateNew(to);
15+
}
16+
17+
var constrainedRect = available[0];
18+
available.Remove(constrainedRect);
19+
constrainedRect.Reset(to);
20+
return constrainedRect;
21+
}
22+
23+
private static ConstrainedRect CreateNew(Rect to)
24+
{
25+
var constrainedRect = new ConstrainedRect(to);
26+
return constrainedRect;
27+
}
28+
29+
public static void Return(ConstrainedRect constrainedRect)
30+
{
31+
available.Add(constrainedRect);
32+
}
33+
}
34+
}

Editor/ConstrainedRectPool.cs.meta

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

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.

README.md

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,17 @@
1616

1717
Constrained Rect is a small helper that aims to make it easier to create Rect's based on existing ones.
1818

19+
## Installation
20+
1. The package is available on the [openupm registry](https://openupm.com). You can install it via [openupm-cli](https://github.com/openupm/openupm-cli).
21+
```
22+
openupm add net.tnrd.constrainedrect
23+
```
24+
2. You can also install via git url by adding these entries in your **manifest.json**
25+
```json
26+
"net.tnrd.constrainedrect": "https://github.com/Thundernerd/Unity3D-ConstrainedRect.git"
27+
```
28+
29+
1930
## Usage
2031
Using constrained rects is easy. You simply call `Constrain.To(...)` and pass it either a `Rect` or an `EditorWindow`.
2132

@@ -59,20 +70,17 @@ private void Foo()
5970
}
6071
```
6172

62-
## Contributing
63-
Pull requests are welcomed. Please feel free to fix any issues you find, or add new features.
73+
### Important
74+
Due to the nature of Unity's editor architecture it is common to use Constrained Rects in high volume. In an attempt to prevent creating and collecting too much garbage as a result of using Constrained Rects they are now being pooled.
6475

65-
## Installing
66-
Installing Constrained Rect into your Unity3D project is done through the [Package Manager](https://docs.unity3d.com/Manual/Packages.html).
76+
After you've finalized your Constrained Rect by calling `.ToRect()` the Constrained Rect will be returned to the pool and free to use for other instances.
6777

68-
You can either add the package manually to the [manifest.json](https://docs.unity3d.com/Manual/upm-dependencies.html) file:
69-
```json
70-
{
71-
"dependencies": {
72-
"net.tnrd.constrainedrect": "https://github.com/Thundernerd/Unity3D-ConstrainedRect.git"
73-
}
74-
}
75-
```
78+
While all of this happens under the hood it is important to understand that from the moment that you call `.ToRect()` the Constrained Rect will throw an exception if it is not being used. This also means that if it is being used then the properties and variables might be different from what you might expect.
7679

77-
Or add it through the UI by selecting the **+ button** in the top left of the Package Manager, selecting _Add package from git URL..._, and pasting https://github.com/Thundernerd/Unity3D-ConstrainedRect.git in the input field.
80+
## Support
81+
**Constrained Rect** is a small and open-source utility that I hope helps other people. It is by no means necessary but if you feel generous you can support me by donating.
7882

83+
[![ko-fi](https://www.ko-fi.com/img/githubbutton_sm.svg)](https://ko-fi.com/J3J11GEYY)
84+
85+
## Contributing
86+
Pull requests are welcomed. Please feel free to fix any issues you find, or add new features.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "net.tnrd.constrainedrect",
3-
"version": "2.1.0",
3+
"version": "3.0.0",
44
"displayName": "Constrained Rect",
55
"description": "A simple helper to constrain a Rect to an EditorWindow or another Rect",
66
"unity": "2019.1",

0 commit comments

Comments
 (0)