Skip to content

Commit

Permalink
Merge pull request #1 from Thundernerd/develop
Browse files Browse the repository at this point in the history
Added pooling for performance gains
  • Loading branch information
Thundernerd committed Aug 2, 2020
2 parents 25d6515 + b7e1691 commit f17ffd6
Show file tree
Hide file tree
Showing 11 changed files with 153 additions and 35 deletions.
3 changes: 3 additions & 0 deletions .codacy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
exclude_paths:
- '*.md'
- '*.json'
14 changes: 9 additions & 5 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,22 +1,26 @@
# Changelog

## [3.0.0] - 2020-08-02
### Added
- Pooling of Constrained Rect

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

## [2.0.2] - 2020-02-19
## Fixed
### Fixed
- Incorrect naming of assembly definitions

## [2.0.1] - 2020-02-19
## Fixed
### Fixed
- Tests assembly references

## [2.0.0] - 2020-02-17
## Added
### Added
- Tests

## Changed
### Changed
- Calculation method for left, top, right, bottom
- Width and height no longer override right and bottom by default

Expand Down
4 changes: 2 additions & 2 deletions Editor/Constrain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public static ConstrainedRect To(EditorWindow editorWindow)

public static ConstrainedRect To(Rect rect)
{
return new ConstrainedRect(rect);
return ConstrainedRectPool.Create(rect);
}
}
}
}
58 changes: 45 additions & 13 deletions Editor/ConstrainedRect.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace TNRD.Constraints
{
public class ConstrainedRect
{
private readonly Rect parent;
private Rect parent;

public Constraint Top { get; }
public Constraint Bottom { get; }
Expand All @@ -16,39 +16,75 @@ public class ConstrainedRect
private bool centerHorizontally;
private bool centerVertically;

public ConstrainedRect(Rect parent)
{
this.parent = parent;
private bool isValid;

internal ConstrainedRect(Rect parent)
{
Top = new Constraint(this, false);
Bottom = new Constraint(this, true);
Left = new Constraint(this, false);
Right = new Constraint(this, true);
Width = new Constraint(this, false);
Height = new Constraint(this, false);

Reset(parent);
}

internal void Reset(Rect parent)
{
this.parent = parent;

Top.Reset();
Bottom.Reset();
Left.Reset();
Right.Reset();
Width.Reset();
Height.Reset();

isValid = true;
}

public Rect Relative(float value)
{
ThrowIfInvalid();
return Relative(value, value, value, value);
}

public Rect Relative(float left, float top, float right, float bottom)
{
ThrowIfInvalid();
return Left.Relative(left)
.Top.Relative(top)
.Right.Relative(right)
.Bottom.Relative(bottom)
.ToRect();
}

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

public ConstrainedRect CenterVertically()
{
ThrowIfInvalid();
centerVertically = true;
return this;
}

public Rect ToRect()
{
float left = 0, top = 0, right = 0, bottom = 0;
ThrowIfInvalid();
float left, top, right, bottom;

CalculateHorizontal(out left, out right);
CalculateVertical(out top, out bottom);

ConstrainedRectPool.Return(this);
isValid = false;

return new Rect(left, top, right, bottom);
}

Expand Down Expand Up @@ -122,16 +158,12 @@ private void CalculateVertical(out float top, out float bottom)
}
}

public ConstrainedRect CenterHorizontally()
internal void ThrowIfInvalid()
{
centerHorizontally = true;
return this;
}
if (isValid)
return;

public ConstrainedRect CenterVertically()
{
centerVertically = true;
return this;
throw new InvalidStateException();
}
}
}
34 changes: 34 additions & 0 deletions Editor/ConstrainedRectPool.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using System.Collections.Generic;
using UnityEngine;

namespace TNRD.Constraints
{
internal static class ConstrainedRectPool
{
private static List<ConstrainedRect> available = new List<ConstrainedRect>();

public static ConstrainedRect Create(Rect to)
{
if (available.Count == 0)
{
return CreateNew(to);
}

var constrainedRect = available[0];
available.Remove(constrainedRect);
constrainedRect.Reset(to);
return constrainedRect;
}

private static ConstrainedRect CreateNew(Rect to)
{
var constrainedRect = new ConstrainedRect(to);
return constrainedRect;
}

public static void Return(ConstrainedRect constrainedRect)
{
available.Add(constrainedRect);
}
}
}
11 changes: 11 additions & 0 deletions Editor/ConstrainedRectPool.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 12 additions & 1 deletion Editor/Constraint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,35 +21,46 @@ private enum ConstrainMode

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

public Constraint(ConstrainedRect parent, bool negateValue)
internal Constraint(ConstrainedRect parent, bool negateValue)
{
this.parent = parent;
this.negateValue = negateValue;
Reset();
}

internal void Reset()
{
mode = ConstrainMode.NotSet;
value = 0;
}

public ConstrainedRect Relative(float value)
{
parent.ThrowIfInvalid();
mode = ConstrainMode.Relative;
this.value = value;
return parent;
}

public ConstrainedRect Absolute(float value)
{
parent.ThrowIfInvalid();
mode = ConstrainMode.Absolute;
this.value = value;
return parent;
}

public ConstrainedRect Percentage(float value)
{
parent.ThrowIfInvalid();
mode = ConstrainMode.Percentage;
this.value = value;
return parent;
}

internal float Apply(float value)
{
parent.ThrowIfInvalid();
switch (mode)
{
case ConstrainMode.Relative:
Expand Down
12 changes: 12 additions & 0 deletions Editor/InvalidStateException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System;

namespace TNRD.Constraints
{
public class InvalidStateException : Exception
{
public InvalidStateException()
: base("The Constrained Rect that you're trying to modify has already been pooled and is no longer available for usage")
{
}
}
}
3 changes: 3 additions & 0 deletions Editor/InvalidStateException.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 21 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,17 @@

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

## Installation
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).
```
openupm add net.tnrd.constrainedrect
```
2. You can also install via git url by adding these entries in your **manifest.json**
```json
"net.tnrd.constrainedrect": "https://github.com/Thundernerd/Unity3D-ConstrainedRect.git"
```


## Usage
Using constrained rects is easy. You simply call `Constrain.To(...)` and pass it either a `Rect` or an `EditorWindow`.

Expand Down Expand Up @@ -59,20 +70,17 @@ private void Foo()
}
```

## Contributing
Pull requests are welcomed. Please feel free to fix any issues you find, or add new features.
### Important
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.

## Installing
Installing Constrained Rect into your Unity3D project is done through the [Package Manager](https://docs.unity3d.com/Manual/Packages.html).
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.

You can either add the package manually to the [manifest.json](https://docs.unity3d.com/Manual/upm-dependencies.html) file:
```json
{
"dependencies": {
"net.tnrd.constrainedrect": "https://github.com/Thundernerd/Unity3D-ConstrainedRect.git"
}
}
```
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.

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.
## Support
**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.

[![ko-fi](https://www.ko-fi.com/img/githubbutton_sm.svg)](https://ko-fi.com/J3J11GEYY)

## Contributing
Pull requests are welcomed. Please feel free to fix any issues you find, or add new features.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "net.tnrd.constrainedrect",
"version": "2.1.0",
"version": "3.0.0",
"displayName": "Constrained Rect",
"description": "A simple helper to constrain a Rect to an EditorWindow or another Rect",
"unity": "2019.1",
Expand Down

0 comments on commit f17ffd6

Please sign in to comment.