Skip to content

Commit 854fe7f

Browse files
committed
ADD Package
0 parents  commit 854fe7f

18 files changed

+278
-0
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Changelog
2+
3+
## [1.0.0] - 2020-02-16
4+
### Added
5+
- Initial version

CHANGELOG.md.meta

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

Documentation~/ConstrainedRect.md

Whitespace-only changes.

Editor.meta

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

Editor/Constrain.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using UnityEditor;
2+
using UnityEngine;
3+
4+
namespace TNRD.Constraints
5+
{
6+
public class Constrain
7+
{
8+
public static ConstrainedRect To(EditorWindow editorWindow)
9+
{
10+
return To(new Rect(Vector2.zero, editorWindow.position.size));
11+
}
12+
13+
public static ConstrainedRect To(Rect rect)
14+
{
15+
return new ConstrainedRect(rect);
16+
}
17+
}
18+
}

Editor/Constrain.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/ConstrainedRect.cs

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
using UnityEngine;
2+
3+
namespace TNRD.Constraints
4+
{
5+
public class ConstrainedRect
6+
{
7+
private readonly Rect parent;
8+
9+
public Constraint Top { get; }
10+
public Constraint Bottom { get; }
11+
public Constraint Left { get; }
12+
public Constraint Right { get; }
13+
public Constraint Width { get; }
14+
public Constraint Height { get; }
15+
16+
public ConstrainedRect(Rect parent)
17+
{
18+
this.parent = parent;
19+
20+
Top = new Constraint(this, false);
21+
Bottom = new Constraint(this, true);
22+
Left = new Constraint(this, false);
23+
Right = new Constraint(this, true);
24+
Width = new Constraint(this, false);
25+
Height = new Constraint(this, false);
26+
}
27+
28+
public Rect Relative(float value)
29+
{
30+
return Relative(value, value, value, value);
31+
}
32+
33+
public Rect Relative(float left, float top, float right, float bottom)
34+
{
35+
return Left.Relative(left)
36+
.Top.Relative(top)
37+
.Right.Relative(right)
38+
.Bottom.Relative(bottom)
39+
.ToRect();
40+
}
41+
42+
public Rect ToRect()
43+
{
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();
48+
49+
return new Rect(left, top, width, height);
50+
}
51+
52+
private float CalculateWidth()
53+
{
54+
var right = Right.Apply(parent.xMax);
55+
var left = Left.Apply(parent.xMin);
56+
return right - left;
57+
}
58+
59+
private float CalculateHeight()
60+
{
61+
var bottom = Bottom.Apply(parent.yMax);
62+
var top = Top.Apply(parent.yMin);
63+
return bottom - top;
64+
}
65+
}
66+
}

Editor/ConstrainedRect.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.

Editor/Constraint.cs

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
using System;
2+
3+
namespace TNRD.Constraints
4+
{
5+
public class Constraint
6+
{
7+
private enum ConstrainMode
8+
{
9+
NotSet,
10+
Relative,
11+
Absolute,
12+
Percentage
13+
}
14+
15+
private readonly ConstrainedRect parent;
16+
private ConstrainMode mode;
17+
private bool negateValue;
18+
private float value;
19+
20+
private float Value => negateValue ? -value : value;
21+
22+
public bool IsSet => mode != ConstrainMode.NotSet;
23+
24+
public Constraint(ConstrainedRect parent, bool negateValue)
25+
{
26+
this.parent = parent;
27+
this.negateValue = negateValue;
28+
}
29+
30+
public ConstrainedRect Relative(float value)
31+
{
32+
mode = ConstrainMode.Relative;
33+
this.value = value;
34+
return parent;
35+
}
36+
37+
public ConstrainedRect Absolute(float value)
38+
{
39+
mode = ConstrainMode.Absolute;
40+
this.value = value;
41+
return parent;
42+
}
43+
44+
public ConstrainedRect Percentage(float value)
45+
{
46+
mode = ConstrainMode.Percentage;
47+
this.value = value;
48+
return parent;
49+
}
50+
51+
internal float Apply(float value)
52+
{
53+
switch (mode)
54+
{
55+
case ConstrainMode.Relative:
56+
return value + Value;
57+
case ConstrainMode.Absolute:
58+
return this.value; // We don't want to negate the value here
59+
case ConstrainMode.Percentage:
60+
return value * Value;
61+
}
62+
63+
return value;
64+
}
65+
}
66+
}

Editor/Constraint.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.

0 commit comments

Comments
 (0)