SelectionShape.cs 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. using System.Collections.Generic;
  2. using UnityEngine;
  3. using FairyGUI.Utils;
  4. namespace FairyGUI
  5. {
  6. /// <summary>
  7. ///
  8. /// </summary>
  9. public class SelectionShape : DisplayObject, IMeshFactory
  10. {
  11. public readonly List<Rect> rects;
  12. public SelectionShape()
  13. {
  14. CreateGameObject("SelectionShape");
  15. graphics = new NGraphics(gameObject);
  16. graphics.texture = NTexture.Empty;
  17. graphics.meshFactory = this;
  18. rects = new List<Rect>();
  19. }
  20. /// <summary>
  21. ///
  22. /// </summary>
  23. public Color color
  24. {
  25. get
  26. {
  27. return graphics.color;
  28. }
  29. set
  30. {
  31. graphics.color = value;
  32. graphics.Tint();
  33. }
  34. }
  35. public void Refresh()
  36. {
  37. int count = rects.Count;
  38. if (count > 0)
  39. {
  40. Rect rect = new Rect();
  41. rect = rects[0];
  42. Rect tmp;
  43. for (int i = 1; i < count; i++)
  44. {
  45. tmp = rects[i];
  46. rect = ToolSet.Union(ref rect, ref tmp);
  47. }
  48. SetSize(rect.xMax, rect.yMax);
  49. }
  50. else
  51. SetSize(0, 0);
  52. graphics.SetMeshDirty();
  53. }
  54. public void Clear()
  55. {
  56. rects.Clear();
  57. graphics.SetMeshDirty();
  58. }
  59. public void OnPopulateMesh(VertexBuffer vb)
  60. {
  61. int count = rects.Count;
  62. if (count == 0 || this.color == Color.clear)
  63. return;
  64. for (int i = 0; i < count; i++)
  65. vb.AddQuad(rects[i]);
  66. vb.AddTriangles();
  67. }
  68. protected override DisplayObject HitTest()
  69. {
  70. Vector2 localPoint = WorldToLocal(HitTestContext.worldPoint, HitTestContext.direction);
  71. if (_contentRect.Contains(localPoint))
  72. {
  73. int count = rects.Count;
  74. for (int i = 0; i < count; i++)
  75. {
  76. if (rects[i].Contains(localPoint))
  77. return this;
  78. }
  79. }
  80. return null;
  81. }
  82. }
  83. }