Tanoda
LocalRect.cs
Go to the documentation of this file.
1/******************************************************************************
2 * Copyright (C) Ultraleap, Inc. 2011-2020. *
3 * *
4 * Use subject to the terms of the Apache License 2.0 available at *
5 * http://www.apache.org/licenses/LICENSE-2.0, or another agreement *
6 * between Ultraleap and you, your company or other organization. *
7 ******************************************************************************/
8
9using System.Collections;
10using System.Collections.Generic;
11using Leap.Unity.Encoding;
12using Leap.Unity.Infix;
13using UnityEngine;
14
15namespace Leap.Unity.Geometry {
16
17 using UnityRect = UnityEngine.Rect;
18
19 [System.Serializable]
20 public struct LocalRect : IInterpolable<LocalRect> {
21
22 public Vector3 center;
23 public Vector2 radii;
24
25 public static readonly LocalRect unit =
26 new LocalRect(Vector3.zero, new Vector2(0.5f, 0.5f));
27
28 public LocalRect(Vector3 center, Vector2 radii) {
29 this.center = center;
30 this.radii = radii;
31 }
32
33 public LocalRect(Vector2 radii) : this(default(Vector3), radii) { }
34
35 public LocalRect(float top, float bottom, float left, float right) {
36 this.center = new Vector3((left + right) / 2f, (top + bottom) / 2f, 0f);
37 var width = Mathf.Abs(right - left);
38 var height = Mathf.Abs(top - bottom);
39 this.radii = new Vector2(width / 2f, height / 2f);
40 }
41
42 public LocalRect(UnityRect uRect) {
43 this.center = uRect.center;
44 this.radii = new Vector2(uRect.width / 2f, uRect.height / 2f);
45 }
46
47 public static LocalRect FromUnityRect(UnityRect uRect) {
48 return new LocalRect(uRect.center, new Vector2(uRect.width / 2f,
49 uRect.height / 2f));
50 }
51
52 public static LocalRect FromRadius(float boxRadius) {
53 return new LocalRect(Vector3.zero, Vector2.one * boxRadius);
54 }
55
56 public Vector3 corner00 { get { return center - radii.WithZ(0f); } }
57 public Vector3 corner10 { get {
58 return center + radii.CompMul(new Vector2(-1f, 1f)).WithZ(0f); }
59 }
60
61 public float top { get { return center.y + radii.y; } }
62 public float bottom { get { return center.y - radii.y; } }
63 public float left { get { return center.x - radii.x; } }
64 public float right { get { return center.x + radii.x; } }
65 public float width { get { return radii.x * 2f; } }
66 public float height { get { return radii.y * 2f; } }
67
68 public Rect With(Transform transform) {
69 return new Rect(this, transform);
70 }
71
73 return new UnityRect(center.ToVector2() - radii, radii * 2f);
74 }
75
78 public LocalRect Scale(float radiusMultipler) {
79 return new LocalRect(this.center, this.radii * radiusMultipler);
80 }
81
84 public LocalRect Scale(Vector2 radiusMultiplers) {
85 return new LocalRect(this.center, this.radii.CompMul(radiusMultiplers));
86 }
87
91 return new LocalRect(ofRect.center, this.radii);
92 }
93
94 public LocalRect CopyFrom(LocalRect toCopy) {
95 center = toCopy.center;
96 radii = toCopy.radii;
97 return this;
98 }
99
100 public bool FillLerped(LocalRect from, LocalRect to, float t) {
101 center = Vector3.Lerp(from.center, to.center, t);
102 radii = Vector2.Max(Vector2.Lerp(from.radii, to.radii, t), Vector2.zero);
103 return true;
104 }
105
107 float t)
108 {
109 center = Splines.CatmullRom.ToCHS(a.center, b.center, c.center, d.center,
110 centripetal: false).PositionAt(t);
111 radii = Vector2.Max(Splines.CatmullRom.ToCHS(a.radii, b.radii, c.radii,
112 d.radii, centripetal: false).PositionAt(t).ToVector2(), Vector2.zero);
113 return true;
114 }
115
117 Margins? cellMargins = null, VerticalOrigin? verticalOrigin = null)
118 {
119 var useMargins = cellMargins.UnwrapOr(Margins.zero);
120 var useVerticalOrigin = verticalOrigin.UnwrapOr(VerticalOrigin.Top);
121 return new LineEnumerator(this, height, useMargins, useVerticalOrigin);
122 }
123
124 public struct LineEnumerator {
125 private LocalRect _rect;
126 private float _height;
127 private VerticalOrigin _verticalOrigin;
128 private Margins _margins;
129 private int _index;
130
131 public LineEnumerator(LocalRect rect, float height, Margins margins,
132 VerticalOrigin verticalOrigin) {
133 _rect = rect;
134 _height = height;
135 _margins = margins;
136 _verticalOrigin = verticalOrigin;
137 _index = -1;
138 }
139
141 get { return new LeapGrid.Cell() {
142 row = _index, col = 0, index = _index, margins = _margins,
143 outerRect =
144 (_verticalOrigin == VerticalOrigin.Bottom ?
145 new LocalRect(
146 top: _index * _height,
147 bottom: _index * _height + _height,
148 left: _rect.left,
149 right: _rect.right
150 )
151 : new LocalRect(
152 top: _rect.top - (_index * _height),
153 bottom: _rect.top - (_index * _height + _height),
154 left: _rect.left,
155 right: _rect.right
156 )
157 )
158 }; }
159 }
160 public bool MoveNext() {
161 _index += 1;
162 return _index * _height + _height <= _rect.height;
163 }
164 public LineEnumerator GetEnumerator() { return this; }
165 }
166
167 }
168
169}
An interface that signifies this class can interpolate via the standard techniques
Definition: VectorHand.cs:19
UnityEngine.Rect UnityRect
Definition: LeapGrid.cs:14
LineEnumerator(LocalRect rect, float height, Margins margins, VerticalOrigin verticalOrigin)
Definition: LocalRect.cs:131
Rect With(Transform transform)
Definition: LocalRect.cs:68
static LocalRect FromRadius(float boxRadius)
Definition: LocalRect.cs:52
LineEnumerator TakeLines(float height, Margins? cellMargins=null, VerticalOrigin? verticalOrigin=null)
Definition: LocalRect.cs:116
LocalRect CopyFrom(LocalRect toCopy)
Definition: LocalRect.cs:94
bool FillLerped(LocalRect from, LocalRect to, float t)
Definition: LocalRect.cs:100
LocalRect Scale(float radiusMultipler)
Returns a new LocalRect with scaled radii. The center is unchanged.
Definition: LocalRect.cs:78
LocalRect(UnityRect uRect)
Definition: LocalRect.cs:42
LocalRect(float top, float bottom, float left, float right)
Definition: LocalRect.cs:35
static LocalRect FromUnityRect(UnityRect uRect)
Definition: LocalRect.cs:47
LocalRect(Vector2 radii)
Definition: LocalRect.cs:33
bool FillSplined(LocalRect a, LocalRect b, LocalRect c, LocalRect d, float t)
Definition: LocalRect.cs:106
LocalRect(Vector3 center, Vector2 radii)
Definition: LocalRect.cs:28
static readonly LocalRect unit
Definition: LocalRect.cs:25
LocalRect Scale(Vector2 radiusMultiplers)
Returns a new LocalRect with component-wise scaled radii. The center is unchanged.
Definition: LocalRect.cs:84
LocalRect AlignToCenter(LocalRect ofRect)
Returns a new LocalRect whose center matches the center of the argument rect.
Definition: LocalRect.cs:90
static readonly Margins zero
Definition: Margins.cs:20