Elin Decompiled Documentation EA 23.102 Nightly
Loading...
Searching...
No Matches
PrimitiveCanvas.cs
Go to the documentation of this file.
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using UnityEngine;
5using UnityEngine.UI;
6
7namespace PrimitiveUI;
8
9[AddComponentMenu("UI/Primitive Canvas")]
10public class PrimitiveCanvas : MaskableGraphic
11{
12 private abstract class PUIElement
13 {
14 protected Color32 color;
15
16 protected Vector2[] points;
17
18 protected UIVertex[] uiVerts;
19
20 protected List<UIVertex> uiVertexTriangleStream;
21
22 public abstract List<UIVertex> GetUIVertexTriangleStream(Vector2 offset, Vector2 scale, Color32 color);
23 }
24
25 private class PUIFillElement : PUIElement
26 {
27 private int[] triangles;
28
29 public PUIFillElement(Vector2[] points, int[] triangles, Color32 color)
30 {
31 uiVerts = new UIVertex[points.Length];
32 uiVertexTriangleStream = new List<UIVertex>(triangles.Length);
33 base.points = points;
34 this.triangles = triangles;
35 base.color = color;
36 for (int i = 0; i < uiVerts.Length; i++)
37 {
38 UIVertex simpleVert = UIVertex.simpleVert;
39 simpleVert.color = color;
40 uiVerts[i] = simpleVert;
41 }
42 }
43
44 public override List<UIVertex> GetUIVertexTriangleStream(Vector2 offset, Vector2 scale, Color32 color)
45 {
47 color = (Color)color * (Color)base.color;
48 if (color.Equals(uiVerts[0].color))
49 {
50 for (int i = 0; i < uiVerts.Length; i++)
51 {
52 uiVerts[i].position = new Vector3((points[i].x + offset.x) * scale.x, (points[i].y + offset.y) * scale.y, 0f);
53 }
54 }
55 else
56 {
57 for (int j = 0; j < uiVerts.Length; j++)
58 {
59 uiVerts[j].color = color;
60 uiVerts[j].position = new Vector3((points[j].x + offset.x) * scale.x, (points[j].y + offset.y) * scale.y, 0f);
61 }
62 }
63 for (int k = 0; k < triangles.Length; k++)
64 {
66 }
68 }
69 }
70
72 {
73 public Vector2[] rawPoints { get; private set; }
74
75 public StrokeStyle strokeStyle { get; private set; }
76
77 public bool isClosedPath { get; private set; }
78
80 {
81 uiVerts = new UIVertex[points.Length];
82 uiVertexTriangleStream = new List<UIVertex>((points.Length - 1) * 6);
83 this.rawPoints = rawPoints;
84 this.strokeStyle = strokeStyle;
85 this.isClosedPath = isClosedPath;
87 for (int i = 0; i < uiVerts.Length; i++)
88 {
89 UIVertex simpleVert = UIVertex.simpleVert;
90 simpleVert.color = color;
91 uiVerts[i] = simpleVert;
92 }
94 }
95
96 public void UpdatePoints(Vector2[] newPoints)
97 {
98 points = newPoints;
99 }
100
101 public override List<UIVertex> GetUIVertexTriangleStream(Vector2 offset, Vector2 scale, Color32 color)
102 {
104 color = (Color)color * (Color)base.color;
105 if (color.Equals(uiVerts[0].color))
106 {
107 for (int i = 0; i < uiVerts.Length; i++)
108 {
109 uiVerts[i].position = new Vector3((points[i].x + offset.x) * scale.x, (points[i].y + offset.y) * scale.y, 0f);
110 }
111 }
112 else
113 {
114 for (int j = 0; j < uiVerts.Length; j++)
115 {
116 uiVerts[j].color = color;
117 uiVerts[j].position = new Vector3((points[j].x + offset.x) * scale.x, (points[j].y + offset.y) * scale.y, 0f);
118 }
119 }
120 for (int k = 0; k < uiVerts.Length; k += 4)
121 {
128 }
130 }
131 }
132
133 private class PUIUtils
134 {
135 public static float Cross2D(Vector2 lhs, Vector2 rhs)
136 {
137 return lhs.x * rhs.y - lhs.y * rhs.x;
138 }
139
140 public static float GetTriangleArea(Vector2 tri0, Vector2 tri1, Vector2 tri2)
141 {
142 return Mathf.Abs((0f - tri1.y) * tri2.x + tri0.y * (0f - tri1.x + tri2.x) + tri0.x * (tri1.y - tri2.y) + tri1.x * tri2.y);
143 }
144
145 public static bool PointInTriangle(Vector2 point, Vector2 tri0, Vector2 tri1, Vector2 tri2, float triAarea)
146 {
147 float num = tri0.y * tri2.x - tri0.x * tri2.y + (tri2.y - tri0.y) * point.x + (tri0.x - tri2.x) * point.y;
148 float num2 = tri0.x * tri1.y - tri0.y * tri1.x + (tri0.y - tri1.y) * point.x + (tri1.x - tri0.x) * point.y;
149 if (num <= 0f || num2 <= 0f)
150 {
151 return false;
152 }
153 return num + num2 < triAarea;
154 }
155
156 public static Vector2? GetLineIntersection(Vector2 line1P1, Vector2 line1P2, Vector2 line2P1, Vector2 line2P2)
157 {
158 float num = (line2P2.y - line2P1.y) * (line1P2.x - line1P1.x) - (line2P2.x - line2P1.x) * (line1P2.y - line1P1.y);
159 float num2 = (line2P2.x - line2P1.x) * (line1P1.y - line2P1.y) - (line2P2.y - line2P1.y) * (line1P1.x - line2P1.x);
160 float f = (line1P2.x - line1P1.x) * (line1P1.y - line2P1.y) - (line1P2.y - line1P1.y) * (line1P1.x - line2P1.x);
161 if (Mathf.Abs(num2) < Mathf.Epsilon && Mathf.Abs(f) < Mathf.Epsilon && Mathf.Abs(num) < Mathf.Epsilon)
162 {
163 return new Vector2((line1P1.x + line1P2.x) * 0.5f, (line1P1.y + line1P2.y) * 0.5f);
164 }
165 if (Mathf.Abs(num) < Mathf.Epsilon)
166 {
167 return null;
168 }
169 float num3 = num2 / num;
170 return new Vector2(line1P1.x + num3 * (line1P2.x - line1P1.x), line1P1.y + num3 * (line1P2.y - line1P1.y));
171 }
172
173 public static Vector2[] GetLinePoints(Vector2 point1, Vector2 point2, float strokeThickness, float aspectRatio)
174 {
175 Vector2 vector = new Vector2(strokeThickness, strokeThickness * aspectRatio);
176 Vector2 normalized = (point2 - point1).normalized;
177 Vector2 vector2 = new Vector2((0f - normalized.y) * vector.x, normalized.x * vector.y);
178 return new Vector2[4]
179 {
180 point1 - vector2,
181 point1 + vector2,
182 point2 + vector2,
183 point2 - vector2
184 };
185 }
186
187 public static Vector2[] GetPathPoints(Vector2[] points, bool closePath, float strokeThickness, float aspectRatio)
188 {
189 Vector2 vector = new Vector2(strokeThickness, strokeThickness * aspectRatio);
190 Vector2 normalized = (points[1] - points[0]).normalized;
191 Vector2 vector2 = new Vector2((0f - normalized.y) * vector.x, normalized.x * vector.y);
192 List<Vector2> list = new List<Vector2>();
193 list.Add(points[0] - vector2);
194 list.Add(points[0] + vector2);
195 list.Add(points[1] + vector2);
196 list.Add(points[1] - vector2);
197 for (int i = 1; i < points.Length - 1; i++)
198 {
199 normalized = (points[i + 1] - points[i]).normalized;
200 vector2 = new Vector2((0f - normalized.y) * vector.x, normalized.x * vector.y);
201 list.Add(points[i] - vector2);
202 list.Add(points[i] + vector2);
203 list.Add(points[i + 1] + vector2);
204 list.Add(points[i + 1] - vector2);
205 Vector2? lineIntersection = GetLineIntersection(list[list.Count - 8], list[list.Count - 5], list[list.Count - 4], list[list.Count - 1]);
206 Vector2? lineIntersection2 = GetLineIntersection(list[list.Count - 7], list[list.Count - 6], list[list.Count - 3], list[list.Count - 2]);
207 if (lineIntersection.HasValue)
208 {
209 Vector2 value = lineIntersection.Value;
210 int index = list.Count - 5;
211 Vector2 value2 = (list[list.Count - 4] = value);
212 list[index] = value2;
213 }
214 if (lineIntersection2.HasValue)
215 {
216 Vector2 value = lineIntersection2.Value;
217 int index2 = list.Count - 6;
218 Vector2 value2 = (list[list.Count - 3] = value);
219 list[index2] = value2;
220 }
221 }
222 if (closePath)
223 {
224 normalized = (points[^1] - points[0]).normalized;
225 vector2 = new Vector2((0f - normalized.y) * vector.x, normalized.x * vector.y);
226 list.Add(points[^1] - vector2);
227 list.Add(points[^1] + vector2);
228 list.Add(points[0] + vector2);
229 list.Add(points[0] - vector2);
230 Vector2? lineIntersection = GetLineIntersection(list[list.Count - 8], list[list.Count - 5], list[list.Count - 3], list[list.Count - 2]);
231 Vector2? lineIntersection2 = GetLineIntersection(list[list.Count - 7], list[list.Count - 6], list[list.Count - 4], list[list.Count - 1]);
232 if (lineIntersection.HasValue)
233 {
234 Vector2 value = lineIntersection.Value;
235 int index3 = list.Count - 5;
236 Vector2 value2 = (list[list.Count - 3] = value);
237 list[index3] = value2;
238 }
239 if (lineIntersection2.HasValue)
240 {
241 Vector2 value = lineIntersection2.Value;
242 int index4 = list.Count - 6;
243 Vector2 value2 = (list[list.Count - 4] = value);
244 list[index4] = value2;
245 }
246 lineIntersection = GetLineIntersection(list[3], list[0], list[list.Count - 3], list[list.Count - 2]);
247 lineIntersection2 = GetLineIntersection(list[2], list[1], list[list.Count - 4], list[list.Count - 1]);
248 if (lineIntersection.HasValue)
249 {
250 Vector2 value = lineIntersection.Value;
251 Vector2 value2 = (list[list.Count - 2] = value);
252 list[0] = value2;
253 }
254 if (lineIntersection2.HasValue)
255 {
256 Vector2 value = lineIntersection2.Value;
257 Vector2 value2 = (list[list.Count - 1] = value);
258 list[1] = value2;
259 }
260 }
261 return list.ToArray();
262 }
263 }
264
265 public bool setDirtyOnDraw = true;
266
267 private List<PUIElement> elements = new List<PUIElement>();
268
269 public float aspectRatio => base.rectTransform.rect.width / base.rectTransform.rect.height;
270
271 protected override void OnPopulateMesh(VertexHelper vh)
272 {
273 vh.Clear();
274 Vector2 size = base.rectTransform.rect.size;
275 Vector3 vector = new Vector3(0f - base.rectTransform.pivot.x, 0f - base.rectTransform.pivot.y, 0f);
276 List<List<UIVertex>> list = new List<List<UIVertex>>(elements.Count);
277 for (int i = 0; i < elements.Count; i++)
278 {
279 list.Add(elements[i].GetUIVertexTriangleStream(vector, size, color));
280 }
281 vh.AddUIVertexTriangleStream(list.SelectMany((List<UIVertex> l) => l).ToList());
282 }
283
284 public void Clear()
285 {
286 elements.Clear();
287 SetAllDirty();
288 }
289
290 protected override void OnRectTransformDimensionsChange()
291 {
292 base.OnRectTransformDimensionsChange();
293 foreach (PUIElement element in elements)
294 {
295 if (!(element.GetType() == typeof(PUIStrokeElement)))
296 {
297 continue;
298 }
299 PUIStrokeElement pUIStrokeElement = (PUIStrokeElement)element;
300 if (pUIStrokeElement.strokeStyle.scaleMode == StrokeScaleMode.Absolute)
301 {
302 float strokeThickness = pUIStrokeElement.strokeStyle.thickness / base.rectTransform.rect.width;
303 if (pUIStrokeElement.rawPoints.Length == 2)
304 {
305 pUIStrokeElement.UpdatePoints(PUIUtils.GetLinePoints(pUIStrokeElement.rawPoints[0], pUIStrokeElement.rawPoints[1], strokeThickness, aspectRatio));
306 }
307 else
308 {
309 pUIStrokeElement.UpdatePoints(PUIUtils.GetPathPoints(pUIStrokeElement.rawPoints, pUIStrokeElement.isClosedPath, strokeThickness, aspectRatio));
310 }
311 }
312 }
313 }
314
315 public void DrawSquare(Vector2 center, float size, Color fillColor)
316 {
317 DrawSquare(center, size, 0f, fillColor);
318 }
319
320 public void DrawSquare(Vector2 center, float size, StrokeStyle strokeStyle)
321 {
322 DrawSquare(center, size, 0f, null, strokeStyle);
323 }
324
325 public void DrawSquare(Vector2 center, float size, Color fillColor, StrokeStyle strokeStyle)
326 {
327 DrawSquare(center, size, 0f, fillColor, strokeStyle);
328 }
329
330 public void DrawSquare(Vector2 center, float size, float rotation = 0f, Color? fillColor = null, StrokeStyle strokeStyle = null)
331 {
332 DrawRectangle(new Rect(center.x - size * 0.5f, center.y - size * 0.5f * aspectRatio, size, size * aspectRatio), rotation, fillColor, strokeStyle);
333 }
334
335 public void DrawRectangle(float x, float y, float width, float height, Color fillColor)
336 {
337 DrawRectangle(new Rect(x, y, width, height), 0f, fillColor);
338 }
339
340 public void DrawRectangle(float x, float y, float width, float height, StrokeStyle strokeStyle)
341 {
342 DrawRectangle(new Rect(x, y, width, height), strokeStyle);
343 }
344
345 public void DrawRectangle(float x, float y, float width, float height, Color fillColor, StrokeStyle strokeStyle)
346 {
347 DrawRectangle(new Rect(x, y, width, height), fillColor, strokeStyle);
348 }
349
350 public void DrawRectangle(float x, float y, float width, float height, float rotation = 0f, Color? fillColor = null, StrokeStyle strokeStyle = null)
351 {
352 DrawRectangle(new Rect(x, y, width, height), rotation, fillColor, strokeStyle);
353 }
354
355 public void DrawRectangle(Rect rect, Color fillColor)
356 {
357 DrawRectangle(rect, 0f, fillColor);
358 }
359
360 public void DrawRectangle(Rect rect, StrokeStyle strokeStyle)
361 {
362 DrawRectangle(rect, 0f, null, strokeStyle);
363 }
364
365 public void DrawRectangle(Rect rect, Color fillColor, StrokeStyle strokeStyle)
366 {
367 DrawRectangle(rect, 0f, fillColor, strokeStyle);
368 }
369
370 public void DrawRectangle(Rect rect, float rotation = 0f, Color? fillColor = null, StrokeStyle strokeStyle = null)
371 {
372 Vector2 center = rect.center;
373 Vector2[] array = new Vector2[4];
374 if (rotation != 0f)
375 {
376 rect.width *= aspectRatio;
377 }
378 array[0] = new Vector2(rect.min.x, rect.min.y);
379 array[1] = new Vector2(rect.min.x, rect.max.y);
380 array[2] = new Vector2(rect.max.x, rect.max.y);
381 array[3] = new Vector2(rect.max.x, rect.min.y);
382 if (rotation != 0f)
383 {
384 rotation *= MathF.PI / 180f;
385 float num = Mathf.Cos(rotation);
386 float num2 = Mathf.Sin(rotation);
387 for (int i = 0; i < array.Length; i++)
388 {
389 float num3 = array[i].x - rect.center.x;
390 float num4 = array[i].y - rect.center.y;
391 array[i] = new Vector2((num3 * num + num4 * num2) / aspectRatio + center.x, num4 * num - num3 * num2 + center.y);
392 }
393 }
394 if (fillColor.HasValue)
395 {
396 elements.Add(new PUIFillElement(array, new int[6] { 0, 1, 2, 2, 3, 0 }, fillColor.Value));
397 }
398 if (strokeStyle != null)
399 {
400 DrawPath(array, strokeStyle, closePath: true);
401 }
402 if (setDirtyOnDraw)
403 {
404 SetVerticesDirty();
405 }
406 }
407
408 public void DrawCircle(Vector2 center, float radius, Color fillColor)
409 {
410 DrawCircle(center, radius, 1f, 0f, 360f, fillColor);
411 }
412
413 public void DrawCircle(Vector2 center, float radius, StrokeStyle strokeStyle)
414 {
415 DrawCircle(center, radius, 1f, 0f, 360f, Color.white, strokeStyle);
416 }
417
418 public void DrawCircle(Vector2 center, float radius, Color fillColor, StrokeStyle strokeStyle)
419 {
420 DrawCircle(center, radius, 1f, 0f, 360f, fillColor, strokeStyle);
421 }
422
423 public void DrawCircle(Vector2 center, float radius, float stepSize = 1f, float startAngle = 0f, float endAngle = 360f, Color? fillColor = null, StrokeStyle strokeStyle = null)
424 {
425 if (endAngle - startAngle < 0f)
426 {
427 Debug.LogWarning("DrawCircle() only works in the clockwise-direction; please ensure endAngle > startAngle.");
428 }
429 else
430 {
431 DrawEllipse(center, new Vector2(radius, radius * aspectRatio), stepSize, 0f, startAngle, endAngle, fillColor, strokeStyle);
432 }
433 }
434
435 public void DrawEllipse(Vector2 center, Vector2 radii, Color fillColor)
436 {
437 DrawEllipse(center, radii, 1f, 0f, 0f, 360f, fillColor);
438 }
439
440 public void DrawEllipse(Vector2 center, Vector2 radii, StrokeStyle strokeStyle)
441 {
442 DrawEllipse(center, radii, 1f, 0f, 0f, 360f, null, strokeStyle);
443 }
444
445 public void DrawEllipse(Vector2 center, Vector2 radii, Color fillColor, StrokeStyle strokeStyle)
446 {
447 DrawEllipse(center, radii, 1f, 0f, 0f, 360f, fillColor, strokeStyle);
448 }
449
450 public void DrawEllipse(Vector2 center, Vector2 radii, float stepSize = 1f, float rotation = 0f, float startAngle = 0f, float endAngle = 360f, Color? fillColor = null, StrokeStyle strokeStyle = null)
451 {
452 if (endAngle - startAngle == 0f)
453 {
454 return;
455 }
456 if (endAngle - startAngle < 0f)
457 {
458 Debug.LogWarning("DrawEllipse() only works in the clockwise-direction; please ensure endAngle > startAngle.");
459 return;
460 }
461 Vector2 vector = center;
462 if (rotation != 0f)
463 {
464 radii.x *= aspectRatio;
465 }
466 float num = MathF.PI / 2f - startAngle * (MathF.PI / 180f);
467 float num2 = MathF.PI * 2f / (360f / stepSize);
468 int num3 = Mathf.CeilToInt((endAngle - startAngle) / stepSize) + 1;
469 List<Vector2> list = new List<Vector2>();
470 list.Add(center);
471 for (int i = 0; i < num3; i++)
472 {
473 list.Add(new Vector2(Mathf.Cos(num - num2 * (float)i) * radii.x + center.x, Mathf.Sin(num - num2 * (float)i) * radii.y + center.y));
474 }
475 if (rotation != 0f)
476 {
477 rotation *= MathF.PI / 180f;
478 float num4 = aspectRatio;
479 float num5 = Mathf.Cos(rotation);
480 float num6 = Mathf.Sin(rotation);
481 for (int j = 0; j < list.Count; j++)
482 {
483 float num7 = list[j].x - center.x;
484 float num8 = list[j].y - center.y;
485 list[j] = new Vector2((num7 * num5 + num8 * num6) / num4 + vector.x, num8 * num5 - num7 * num6 + vector.y);
486 }
487 }
488 if (fillColor.HasValue)
489 {
490 int[] array;
491 if (endAngle - startAngle == 0f)
492 {
493 array = new int[(list.Count - 1) * 3];
494 int num9 = 0;
495 int num10 = 1;
496 while (num9 < array.Length)
497 {
498 array[num9] = 0;
499 array[num9 + 1] = num10;
500 array[num9 + 2] = num10 + 1;
501 num9 += 3;
502 num10++;
503 }
504 array[^1] = 1;
505 }
506 else
507 {
508 array = new int[(list.Count - 2) * 3];
509 int num11 = 0;
510 int num12 = 1;
511 while (num11 < array.Length)
512 {
513 array[num11] = 0;
514 array[num11 + 1] = num12;
515 array[num11 + 2] = num12 + 1;
516 num11 += 3;
517 num12++;
518 }
519 }
520 elements.Add(new PUIFillElement(list.ToArray(), array, fillColor.Value));
521 }
522 if (strokeStyle != null)
523 {
524 DrawPath(list.GetRange(1, list.Count - 2).ToArray(), strokeStyle, closePath: true);
525 }
526 if (setDirtyOnDraw)
527 {
528 SetVerticesDirty();
529 }
530 }
531
532 public void DrawRegularSolid(Vector2 center, float radius, int sides, Color fillColor)
533 {
534 DrawRegularSolid(center, radius, sides, 0f, fillColor);
535 }
536
537 public void DrawRegularSolid(Vector2 center, float radius, int sides, StrokeStyle strokeStyle)
538 {
539 DrawRegularSolid(center, radius, sides, 0f, null, strokeStyle);
540 }
541
542 public void DrawRegularSolid(Vector2 center, float radius, int sides, Color fillColor, StrokeStyle strokeStyle)
543 {
544 DrawRegularSolid(center, radius, sides, 0f, fillColor, strokeStyle);
545 }
546
547 public void DrawRegularSolid(Vector2 center, float radius, int sides, float rotation = 0f, Color? fillColor = null, StrokeStyle strokeStyle = null)
548 {
549 if (sides < 3)
550 {
551 Debug.LogError("DrawRegularSolid() requires at least 3 sides.");
552 }
553 else
554 {
555 DrawCircle(center, radius, 360f / (float)sides, rotation, 360f + rotation, fillColor, strokeStyle);
556 }
557 }
558
559 public void DrawIrregularSolid(Vector2 center, float[] radii, Color fillColor)
560 {
561 DrawIrregularSolid(center, radii, 0f, fillColor, null);
562 }
563
564 public void DrawIrregularSolid(Vector2 center, float[] radii, StrokeStyle strokeStyle)
565 {
566 DrawIrregularSolid(center, radii, 0f, null, strokeStyle);
567 }
568
569 public void DrawIrregularSolid(Vector2 center, float[] radii, Color fillColor, StrokeStyle strokeStyle)
570 {
571 DrawIrregularSolid(center, radii, 0f, fillColor, strokeStyle);
572 }
573
574 public void DrawIrregularSolid(Vector2 center, float[] radii, float rotation, Color? fillColor, StrokeStyle strokeStyle)
575 {
576 int num = radii.Length;
577 if (num < 3)
578 {
579 Debug.LogError("DrawIrregularSolid() requires at least 3 radii.");
580 return;
581 }
582 float num2 = MathF.PI / 2f - rotation * (MathF.PI / 180f);
583 float num3 = MathF.PI * 2f / (float)num;
584 float num4 = aspectRatio;
585 List<Vector2> list = new List<Vector2>();
586 list.Add(center);
587 for (int i = 0; i < num; i++)
588 {
589 Vector2 vector = new Vector2(radii[i], radii[i] * num4);
590 list.Add(new Vector2(Mathf.Cos(num2 - num3 * (float)i) * vector.x + center.x, Mathf.Sin(num2 - num3 * (float)i) * vector.y + center.y));
591 }
592 if (fillColor.HasValue)
593 {
594 int[] array = new int[(list.Count - 1) * 3];
595 int num5 = 0;
596 int num6 = 1;
597 while (num5 < array.Length)
598 {
599 array[num5] = 0;
600 array[num5 + 1] = num6;
601 array[num5 + 2] = num6 + 1;
602 num5 += 3;
603 num6++;
604 }
605 array[^1] = 1;
606 elements.Add(new PUIFillElement(list.ToArray(), array, fillColor.Value));
607 }
608 if (strokeStyle != null)
609 {
610 DrawPath(list.GetRange(1, list.Count - 2).ToArray(), strokeStyle, closePath: true);
611 }
612 if (setDirtyOnDraw)
613 {
614 SetVerticesDirty();
615 }
616 }
617
618 public void DrawPolygon(Vector2[] points, Color fillColor)
619 {
620 DrawPolygon(points, fillColor, null);
621 }
622
623 public void DrawPolygon(Vector2[] points, StrokeStyle strokeStyle)
624 {
625 DrawPath(points, strokeStyle, closePath: true);
626 }
627
628 public void DrawPolygon(Vector2[] points, Color fillColor, StrokeStyle strokeStyle)
629 {
630 if (points.Length < 3)
631 {
632 Debug.LogError("DrawPolygon() requires at least 3 vertices");
633 return;
634 }
635 if (points.Length == 3)
636 {
637 points = new Vector2[3]
638 {
639 points[0],
640 points[1],
641 points[2]
642 };
643 elements.Add(new PUIFillElement(points, new int[3] { 0, 1, 2 }, fillColor));
644 }
645 else if (points.Length == 4)
646 {
647 elements.Add(new PUIFillElement(points, new int[6] { 0, 1, 2, 2, 3, 0 }, fillColor));
648 }
649 else
650 {
651 int[] array = new int[(points.Length - 2) * 3];
652 int num = 0;
653 LinkedList<Vector2> linkedList = new LinkedList<Vector2>(points);
654 List<LinkedListNode<Vector2>> list = new List<LinkedListNode<Vector2>>();
655 List<LinkedListNode<Vector2>> list2 = new List<LinkedListNode<Vector2>>();
656 List<LinkedListNode<Vector2>> list3 = new List<LinkedListNode<Vector2>>();
657 LinkedListNode<Vector2> linkedListNode = linkedList.First;
658 LinkedListNode<Vector2> linkedListNode2 = linkedList.First;
659 while (linkedListNode.Next != null)
660 {
661 linkedListNode = linkedListNode.Next;
662 if (linkedListNode.Value.x < linkedListNode2.Value.x)
663 {
664 linkedListNode2 = linkedListNode;
665 }
666 }
667 LinkedListNode<Vector2> linkedListNode3 = linkedListNode2.Previous ?? linkedListNode2.List.Last;
668 LinkedListNode<Vector2> linkedListNode4 = linkedListNode2.Next ?? linkedListNode2.List.First;
669 Func<Vector2, Vector2, Vector2, bool> func = ((!(linkedListNode4.Value.y > linkedListNode3.Value.y)) ? ((Func<Vector2, Vector2, Vector2, bool>)delegate(Vector2 prev, Vector2 cur, Vector2 next)
670 {
671 Vector2 vector2 = cur - prev;
672 Vector2 rhs2 = next - cur;
673 return Vector2.Dot(new Vector2(0f - vector2.y, vector2.x), rhs2) > 0f;
674 }) : ((Func<Vector2, Vector2, Vector2, bool>)delegate(Vector2 prev, Vector2 cur, Vector2 next)
675 {
676 Vector2 vector = cur - prev;
677 Vector2 rhs = next - cur;
678 return Vector2.Dot(new Vector2(0f - vector.y, vector.x), rhs) < 0f;
679 }));
680 linkedListNode = linkedList.First;
681 for (int i = 0; i < linkedList.Count; i++)
682 {
683 linkedListNode3 = linkedListNode.Previous ?? linkedListNode.List.Last;
684 linkedListNode4 = linkedListNode.Next ?? linkedListNode.List.First;
685 if (func(linkedListNode3.Value, linkedListNode.Value, linkedListNode4.Value))
686 {
687 list2.Add(linkedListNode);
688 float triangleArea = PUIUtils.GetTriangleArea(linkedListNode3.Value, linkedListNode.Value, linkedListNode4.Value);
689 bool flag = true;
690 foreach (LinkedListNode<Vector2> item in list3)
691 {
692 if (PUIUtils.PointInTriangle(item.Value, linkedListNode3.Value, linkedListNode.Value, linkedListNode4.Value, triangleArea))
693 {
694 flag = false;
695 break;
696 }
697 }
698 if (flag)
699 {
700 list.Add(linkedListNode);
701 }
702 }
703 else
704 {
705 list3.Add(linkedListNode);
706 }
707 linkedListNode = linkedListNode.Next;
708 }
709 while (linkedList.Count > 3)
710 {
711 linkedListNode = list[0];
712 linkedListNode3 = linkedListNode.Previous ?? linkedListNode.List.Last;
713 linkedListNode4 = linkedListNode.Next ?? linkedListNode.List.First;
714 array[num] = Array.IndexOf(points, linkedListNode3.Value);
715 array[num + 1] = Array.IndexOf(points, linkedListNode.Value);
716 array[num + 2] = Array.IndexOf(points, linkedListNode4.Value);
717 num += 3;
718 list.Remove(linkedListNode);
719 linkedList.Remove(linkedListNode);
720 LinkedListNode<Vector2>[] array2 = new LinkedListNode<Vector2>[2] { linkedListNode3, linkedListNode4 };
721 foreach (LinkedListNode<Vector2> linkedListNode5 in array2)
722 {
723 LinkedListNode<Vector2> linkedListNode6 = linkedListNode5.Previous ?? linkedListNode5.List.Last;
724 LinkedListNode<Vector2> linkedListNode7 = linkedListNode5.Next ?? linkedListNode5.List.First;
725 if (func(linkedListNode6.Value, linkedListNode5.Value, linkedListNode7.Value))
726 {
727 if (list3.Contains(linkedListNode5))
728 {
729 list3.Remove(linkedListNode5);
730 list2.Add(linkedListNode5);
731 }
732 float triangleArea2 = PUIUtils.GetTriangleArea(linkedListNode6.Value, linkedListNode5.Value, linkedListNode7.Value);
733 bool flag2 = true;
734 foreach (LinkedListNode<Vector2> item2 in list3)
735 {
736 if (PUIUtils.PointInTriangle(item2.Value, linkedListNode6.Value, linkedListNode5.Value, linkedListNode7.Value, triangleArea2))
737 {
738 flag2 = false;
739 break;
740 }
741 }
742 if (flag2 && !list.Contains(linkedListNode5))
743 {
744 list.Add(linkedListNode5);
745 }
746 else if (!flag2 && list.Contains(linkedListNode5))
747 {
748 list.Remove(linkedListNode5);
749 }
750 }
751 else
752 {
753 list2.Remove(linkedListNode5);
754 list3.Add(linkedListNode5);
755 }
756 }
757 }
758 array[num] = Array.IndexOf(points, linkedList.First.Value);
759 array[num + 1] = Array.IndexOf(points, linkedList.First.Next.Value);
760 array[num + 2] = Array.IndexOf(points, linkedList.First.Next.Next.Value);
761 elements.Add(new PUIFillElement(points, array, fillColor));
762 }
763 if (strokeStyle != null)
764 {
765 DrawPath(points, strokeStyle, closePath: true);
766 }
767 if (setDirtyOnDraw)
768 {
769 SetVerticesDirty();
770 }
771 }
772
773 public void DrawRawMesh(Vector2[] points, int[] triangles, Color fillColor)
774 {
775 elements.Add(new PUIFillElement(points, triangles, fillColor));
776 if (setDirtyOnDraw)
777 {
778 SetVerticesDirty();
779 }
780 }
781
782 public void DrawLine(Vector2 point1, Vector2 point2)
783 {
784 DrawLine(point1, point2, StrokeStyle.defaultStrokeStyle);
785 }
786
787 public void DrawLine(Vector2 point1, Vector2 point2, StrokeStyle strokeStyle)
788 {
789 float strokeThickness = ((strokeStyle.scaleMode == StrokeScaleMode.Absolute) ? (strokeStyle.thickness / base.rectTransform.rect.width) : strokeStyle.thickness);
790 elements.Add(new PUIStrokeElement(new Vector2[2] { point1, point2 }, PUIUtils.GetLinePoints(point1, point2, strokeThickness, aspectRatio), strokeStyle, isClosedPath: false));
791 if (setDirtyOnDraw)
792 {
793 SetVerticesDirty();
794 }
795 }
796
797 public void DrawPath(Vector2[] points)
798 {
799 DrawPath(points, StrokeStyle.defaultStrokeStyle, closePath: false);
800 }
801
802 public void DrawPath(Vector2[] points, StrokeStyle strokeStyle)
803 {
804 DrawPath(points, strokeStyle, closePath: false);
805 }
806
807 public void DrawPath(Vector2[] points, StrokeStyle strokeStyle, bool closePath)
808 {
809 if (points.Length < 2)
810 {
811 Debug.LogError("DrawPath() needs at least two points to draw");
812 return;
813 }
814 if (points.Length == 2)
815 {
816 DrawLine(points[0], points[1], strokeStyle);
817 if (closePath)
818 {
819 Debug.LogWarning("DrawPath() can't close a path with only two points. 'closePath' parameter ignored.");
820 }
821 return;
822 }
823 float strokeThickness = ((strokeStyle.scaleMode == StrokeScaleMode.Absolute) ? (strokeStyle.thickness / base.rectTransform.rect.width) : strokeStyle.thickness);
824 elements.Add(new PUIStrokeElement(points, PUIUtils.GetPathPoints(points, closePath, strokeThickness, aspectRatio), strokeStyle, closePath));
825 if (setDirtyOnDraw)
826 {
827 SetVerticesDirty();
828 }
829 }
830}
abstract List< UIVertex > GetUIVertexTriangleStream(Vector2 offset, Vector2 scale, Color32 color)
PUIFillElement(Vector2[] points, int[] triangles, Color32 color)
override List< UIVertex > GetUIVertexTriangleStream(Vector2 offset, Vector2 scale, Color32 color)
PUIStrokeElement(Vector2[] rawPoints, Vector2[] points, StrokeStyle strokeStyle, bool isClosedPath)
override List< UIVertex > GetUIVertexTriangleStream(Vector2 offset, Vector2 scale, Color32 color)
static bool PointInTriangle(Vector2 point, Vector2 tri0, Vector2 tri1, Vector2 tri2, float triAarea)
static float GetTriangleArea(Vector2 tri0, Vector2 tri1, Vector2 tri2)
static Vector2[] GetLinePoints(Vector2 point1, Vector2 point2, float strokeThickness, float aspectRatio)
static Vector2[] GetPathPoints(Vector2[] points, bool closePath, float strokeThickness, float aspectRatio)
static float Cross2D(Vector2 lhs, Vector2 rhs)
static ? Vector2 GetLineIntersection(Vector2 line1P1, Vector2 line1P2, Vector2 line2P1, Vector2 line2P2)
void DrawRegularSolid(Vector2 center, float radius, int sides, Color fillColor)
void DrawSquare(Vector2 center, float size, Color fillColor, StrokeStyle strokeStyle)
override void OnPopulateMesh(VertexHelper vh)
void DrawCircle(Vector2 center, float radius, float stepSize=1f, float startAngle=0f, float endAngle=360f, Color? fillColor=null, StrokeStyle strokeStyle=null)
void DrawIrregularSolid(Vector2 center, float[] radii, Color fillColor, StrokeStyle strokeStyle)
void DrawRectangle(Rect rect, Color fillColor)
void DrawRectangle(float x, float y, float width, float height, Color fillColor, StrokeStyle strokeStyle)
void DrawRawMesh(Vector2[] points, int[] triangles, Color fillColor)
void DrawPath(Vector2[] points)
void DrawEllipse(Vector2 center, Vector2 radii, Color fillColor, StrokeStyle strokeStyle)
void DrawEllipse(Vector2 center, Vector2 radii, float stepSize=1f, float rotation=0f, float startAngle=0f, float endAngle=360f, Color? fillColor=null, StrokeStyle strokeStyle=null)
void DrawRectangle(Rect rect, float rotation=0f, Color? fillColor=null, StrokeStyle strokeStyle=null)
void DrawRectangle(float x, float y, float width, float height, float rotation=0f, Color? fillColor=null, StrokeStyle strokeStyle=null)
override void OnRectTransformDimensionsChange()
void DrawRectangle(float x, float y, float width, float height, StrokeStyle strokeStyle)
void DrawRegularSolid(Vector2 center, float radius, int sides, float rotation=0f, Color? fillColor=null, StrokeStyle strokeStyle=null)
void DrawPolygon(Vector2[] points, Color fillColor, StrokeStyle strokeStyle)
void DrawRegularSolid(Vector2 center, float radius, int sides, Color fillColor, StrokeStyle strokeStyle)
void DrawSquare(Vector2 center, float size, Color fillColor)
void DrawIrregularSolid(Vector2 center, float[] radii, Color fillColor)
void DrawCircle(Vector2 center, float radius, Color fillColor)
void DrawLine(Vector2 point1, Vector2 point2)
void DrawIrregularSolid(Vector2 center, float[] radii, StrokeStyle strokeStyle)
void DrawEllipse(Vector2 center, Vector2 radii, Color fillColor)
void DrawCircle(Vector2 center, float radius, Color fillColor, StrokeStyle strokeStyle)
void DrawPolygon(Vector2[] points, StrokeStyle strokeStyle)
void DrawPath(Vector2[] points, StrokeStyle strokeStyle, bool closePath)
void DrawIrregularSolid(Vector2 center, float[] radii, float rotation, Color? fillColor, StrokeStyle strokeStyle)
void DrawLine(Vector2 point1, Vector2 point2, StrokeStyle strokeStyle)
void DrawRectangle(float x, float y, float width, float height, Color fillColor)
void DrawRectangle(Rect rect, StrokeStyle strokeStyle)
List< PUIElement > elements
void DrawSquare(Vector2 center, float size, StrokeStyle strokeStyle)
void DrawCircle(Vector2 center, float radius, StrokeStyle strokeStyle)
void DrawSquare(Vector2 center, float size, float rotation=0f, Color? fillColor=null, StrokeStyle strokeStyle=null)
void DrawPath(Vector2[] points, StrokeStyle strokeStyle)
void DrawEllipse(Vector2 center, Vector2 radii, StrokeStyle strokeStyle)
void DrawPolygon(Vector2[] points, Color fillColor)
void DrawRegularSolid(Vector2 center, float radius, int sides, StrokeStyle strokeStyle)
void DrawRectangle(Rect rect, Color fillColor, StrokeStyle strokeStyle)
static StrokeStyle defaultStrokeStyle
Definition: StrokeStyle.cs:18
StrokeScaleMode scaleMode
Definition: StrokeStyle.cs:13