Skip to content

Commit 28af90b

Browse files
committed
Formatted code
1 parent fcddc11 commit 28af90b

4 files changed

Lines changed: 32 additions & 37 deletions

File tree

Assets/Hypertext/Examples/RegexExample.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,14 @@ namespace Hypertext
44
{
55
public class RegexExample : MonoBehaviour
66
{
7-
[SerializeField]
8-
RegexHypertext text;
7+
[SerializeField] RegexHypertext text;
98

10-
const string RegexURL = "http(s)?://([\\w-]+\\.)+[\\w-]+(/[\\w- ./?%&=]*)?";
9+
const string RegexUrl = "http(s)?://([\\w-]+\\.)+[\\w-]+(/[\\w- ./?%&=]*)?";
1110
const string RegexHashtag = "[##][A-Za-zA-Za-z一-鿆0-90-9ぁ-ヶヲ-゚ー]+";
1211

1312
void Start()
1413
{
15-
text.OnClick(RegexURL, Color.cyan, url => Debug.Log(url));
14+
text.OnClick(RegexUrl, Color.cyan, url => Debug.Log(url));
1615
text.OnClick(RegexHashtag, Color.green, hashtag => Debug.Log(hashtag));
1716
}
1817
}

Assets/Hypertext/Examples/RegexHypertext.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ public class RegexHypertext : HypertextBase
1111

1212
struct Entry
1313
{
14-
public string RegexPattern;
15-
public Color Color;
16-
public Action<string> Callback;
14+
public readonly string RegexPattern;
15+
public readonly Color Color;
16+
public readonly Action<string> Callback;
1717

1818
public Entry(string regexPattern, Color color, Action<string> callback)
1919
{

Assets/Hypertext/Scripts/HypertextBase.cs

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,18 @@ namespace Hypertext
99
public abstract class HypertextBase : Text, IPointerClickHandler
1010
{
1111
Canvas rootCanvas;
12-
Canvas RootCanvas { get { return rootCanvas ?? (rootCanvas = GetComponentInParent<Canvas>()); } }
12+
Canvas RootCanvas => rootCanvas ?? (rootCanvas = GetComponentInParent<Canvas>());
1313

1414
const int CharVerts = 6;
1515
readonly List<Span> spans = new List<Span>();
1616
static readonly ObjectPool<List<UIVertex>> verticesPool = new ObjectPool<List<UIVertex>>(null, l => l.Clear());
1717

1818
struct Span
1919
{
20-
public int StartIndex;
21-
public int Length;
22-
public Color Color;
23-
public Action<string> Callback;
20+
public readonly int StartIndex;
21+
public readonly int Length;
22+
public readonly Color Color;
23+
public readonly Action<string> Callback;
2424
public List<Rect> BoundingBoxes;
2525

2626
public Span(int startIndex, int endIndex, Color color, Action<string> callback)
@@ -96,12 +96,12 @@ void Modify(ref List<UIVertex> vertices)
9696
{
9797
var verticesCount = vertices.Count;
9898

99-
for (int i = 0; i < spans.Count; i++)
99+
for (var i = 0; i < spans.Count; i++)
100100
{
101101
var span = spans[i];
102102
var endIndex = span.StartIndex + span.Length;
103103

104-
for (int textIndex = span.StartIndex; textIndex < endIndex; textIndex++)
104+
for (var textIndex = span.StartIndex; textIndex < endIndex; textIndex++)
105105
{
106106
var vertexStartIndex = textIndex * CharVerts;
107107
if (vertexStartIndex + CharVerts > verticesCount)
@@ -112,7 +112,7 @@ void Modify(ref List<UIVertex> vertices)
112112
var min = Vector2.one * float.MaxValue;
113113
var max = Vector2.one * float.MinValue;
114114

115-
for (int vertexIndex = 0; vertexIndex < CharVerts; vertexIndex++)
115+
for (var vertexIndex = 0; vertexIndex < CharVerts; vertexIndex++)
116116
{
117117
var vertex = vertices[vertexStartIndex + vertexIndex];
118118
vertex.color = span.Color;
@@ -141,7 +141,7 @@ void Modify(ref List<UIVertex> vertices)
141141
}
142142
}
143143

144-
span.BoundingBoxes.Add(new Rect { min = min, max = max });
144+
span.BoundingBoxes.Add(new Rect {min = min, max = max});
145145
}
146146

147147
// 文字ごとのバウンディングボックスを行ごとのバウンディングボックスにまとめる
@@ -150,18 +150,20 @@ void Modify(ref List<UIVertex> vertices)
150150
}
151151
}
152152

153-
List<Rect> CalculateLineBoundingBoxes(List<Rect> charBoundingBoxes)
153+
static List<Rect> CalculateLineBoundingBoxes(List<Rect> charBoundingBoxes)
154154
{
155155
var lineBoundingBoxes = new List<Rect>();
156156
var lineStartIndex = 0;
157157

158-
for (int i = 1; i < charBoundingBoxes.Count; i++)
158+
for (var i = 1; i < charBoundingBoxes.Count; i++)
159159
{
160-
if (charBoundingBoxes[i].xMin < charBoundingBoxes[i - 1].xMin)
160+
if (charBoundingBoxes[i].xMin >= charBoundingBoxes[i - 1].xMin)
161161
{
162-
lineBoundingBoxes.Add(CalculateAABB(charBoundingBoxes.GetRange(lineStartIndex, i - lineStartIndex)));
163-
lineStartIndex = i;
162+
continue;
164163
}
164+
165+
lineBoundingBoxes.Add(CalculateAABB(charBoundingBoxes.GetRange(lineStartIndex, i - lineStartIndex)));
166+
lineStartIndex = i;
165167
}
166168

167169
if (lineStartIndex < charBoundingBoxes.Count)
@@ -172,12 +174,12 @@ List<Rect> CalculateLineBoundingBoxes(List<Rect> charBoundingBoxes)
172174
return lineBoundingBoxes;
173175
}
174176

175-
Rect CalculateAABB(List<Rect> rects)
177+
static Rect CalculateAABB(IReadOnlyList<Rect> rects)
176178
{
177179
var min = Vector2.one * float.MaxValue;
178180
var max = Vector2.one * float.MinValue;
179181

180-
for (int i = 0; i < rects.Count; i++)
182+
for (var i = 0; i < rects.Count; i++)
181183
{
182184
if (rects[i].xMin < min.x)
183185
{
@@ -200,7 +202,7 @@ Rect CalculateAABB(List<Rect> rects)
200202
}
201203
}
202204

203-
return new Rect { min = min, max = max };
205+
return new Rect {min = min, max = max};
204206
}
205207

206208
Vector3 CalculateLocalPosition(Vector3 position, Camera pressEventCamera)
@@ -215,7 +217,7 @@ Vector3 CalculateLocalPosition(Vector3 position, Camera pressEventCamera)
215217
return transform.InverseTransformPoint(position);
216218
}
217219

218-
var localPosition = Vector2.zero;
220+
Vector2 localPosition;
219221
RectTransformUtility.ScreenPointToLocalPointInRectangle(rectTransform, position, pressEventCamera, out localPosition);
220222
return localPosition;
221223
}
@@ -224,9 +226,9 @@ void IPointerClickHandler.OnPointerClick(PointerEventData eventData)
224226
{
225227
var localPosition = CalculateLocalPosition(eventData.position, eventData.pressEventCamera);
226228

227-
for (int s = 0; s < spans.Count; s++)
229+
for (var s = 0; s < spans.Count; s++)
228230
{
229-
for (int b = 0; b < spans[s].BoundingBoxes.Count; b++)
231+
for (var b = 0; b < spans[s].BoundingBoxes.Count; b++)
230232
{
231233
if (spans[s].BoundingBoxes[b].Contains(localPosition))
232234
{

Assets/Hypertext/Scripts/ObjectPool.cs

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ namespace Hypertext
1010
readonly Action<T> onRelease;
1111

1212
public int CountAll { get; set; }
13-
public int CountActive { get { return CountAll - CountInactive; } }
14-
public int CountInactive { get { return stack.Count; } }
13+
public int CountActive => CountAll - CountInactive;
14+
public int CountInactive => stack.Count;
1515

1616
public ObjectPool(Action<T> onGet, Action<T> onRelease)
1717
{
@@ -32,10 +32,7 @@ public T Get()
3232
element = stack.Pop();
3333
}
3434

35-
if (onGet != null)
36-
{
37-
onGet(element);
38-
}
35+
onGet?.Invoke(element);
3936

4037
return element;
4138
}
@@ -47,10 +44,7 @@ public void Release(T element)
4744
UnityEngine.Debug.LogError("Internal error. Trying to destroy object that is already released to pool.");
4845
}
4946

50-
if (onRelease != null)
51-
{
52-
onRelease(element);
53-
}
47+
onRelease?.Invoke(element);
5448

5549
stack.Push(element);
5650
}

0 commit comments

Comments
 (0)