Shape.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. using System.Collections.Generic;
  2. using UnityEngine;
  3. namespace FairyGUI
  4. {
  5. /// <summary>
  6. ///
  7. /// </summary>
  8. public class Shape : DisplayObject
  9. {
  10. /// <summary>
  11. ///
  12. /// </summary>
  13. public Shape()
  14. {
  15. CreateGameObject("Shape");
  16. graphics = new NGraphics(gameObject);
  17. graphics.texture = NTexture.Empty;
  18. graphics.meshFactory = null;
  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.SetMeshDirty();
  33. }
  34. }
  35. /// <summary>
  36. ///
  37. /// </summary>
  38. /// <param name="lineSize"></param>
  39. /// <param name="lineColor"></param>
  40. /// <param name="fillColor"></param>
  41. public void DrawRect(float lineSize, Color lineColor, Color fillColor)
  42. {
  43. RectMesh mesh = graphics.GetMeshFactory<RectMesh>();
  44. mesh.lineWidth = lineSize;
  45. mesh.lineColor = lineColor;
  46. mesh.fillColor = null;
  47. mesh.colors = null;
  48. graphics.color = fillColor;
  49. graphics.SetMeshDirty();
  50. }
  51. /// <summary>
  52. ///
  53. /// </summary>
  54. /// <param name="lineSize"></param>
  55. /// <param name="colors"></param>
  56. public void DrawRect(float lineSize, Color32[] colors)
  57. {
  58. RectMesh mesh = graphics.GetMeshFactory<RectMesh>();
  59. mesh.lineWidth = lineSize;
  60. mesh.colors = colors;
  61. graphics.SetMeshDirty();
  62. }
  63. /// <summary>
  64. ///
  65. /// </summary>
  66. /// <param name="lineSize"></param>
  67. /// <param name="lineColor"></param>
  68. /// <param name="fillColor"></param>
  69. /// <param name="topLeftRadius"></param>
  70. /// <param name="topRightRadius"></param>
  71. /// <param name="bottomLeftRadius"></param>
  72. /// <param name="bottomRightRadius"></param>
  73. public void DrawRoundRect(float lineSize, Color lineColor, Color fillColor,
  74. float topLeftRadius, float topRightRadius, float bottomLeftRadius, float bottomRightRadius)
  75. {
  76. RoundedRectMesh mesh = graphics.GetMeshFactory<RoundedRectMesh>();
  77. mesh.lineWidth = lineSize;
  78. mesh.lineColor = lineColor;
  79. mesh.fillColor = null;
  80. mesh.topLeftRadius = topLeftRadius;
  81. mesh.topRightRadius = topRightRadius;
  82. mesh.bottomLeftRadius = bottomLeftRadius;
  83. mesh.bottomRightRadius = bottomRightRadius;
  84. graphics.color = fillColor;
  85. graphics.SetMeshDirty();
  86. }
  87. /// <summary>
  88. ///
  89. /// </summary>
  90. /// <param name="fillColor"></param>
  91. public void DrawEllipse(Color fillColor)
  92. {
  93. EllipseMesh mesh = graphics.GetMeshFactory<EllipseMesh>();
  94. mesh.lineWidth = 0;
  95. mesh.startDegree = 0;
  96. mesh.endDegreee = 360;
  97. mesh.fillColor = null;
  98. mesh.centerColor = null;
  99. graphics.color = fillColor;
  100. graphics.SetMeshDirty();
  101. }
  102. /// <summary>
  103. ///
  104. /// </summary>
  105. /// <param name="lineSize"></param>
  106. /// <param name="centerColor"></param>
  107. /// <param name="lineColor"></param>
  108. /// <param name="fillColor"></param>
  109. /// <param name="startDegree"></param>
  110. /// <param name="endDegree"></param>
  111. public void DrawEllipse(float lineSize, Color centerColor, Color lineColor, Color fillColor, float startDegree, float endDegree)
  112. {
  113. EllipseMesh mesh = graphics.GetMeshFactory<EllipseMesh>();
  114. mesh.lineWidth = lineSize;
  115. if (centerColor.Equals(fillColor))
  116. mesh.centerColor = null;
  117. else
  118. mesh.centerColor = centerColor;
  119. mesh.lineColor = lineColor;
  120. mesh.fillColor = null;
  121. mesh.startDegree = startDegree;
  122. mesh.endDegreee = endDegree;
  123. graphics.color = fillColor;
  124. graphics.SetMeshDirty();
  125. }
  126. /// <summary>
  127. ///
  128. /// </summary>
  129. /// <param name="points"></param>
  130. /// <param name="fillColor"></param>
  131. public void DrawPolygon(IList<Vector2> points, Color fillColor)
  132. {
  133. PolygonMesh mesh = graphics.GetMeshFactory<PolygonMesh>();
  134. mesh.points.Clear();
  135. mesh.points.AddRange(points);
  136. mesh.fillColor = null;
  137. mesh.colors = null;
  138. graphics.color = fillColor;
  139. graphics.SetMeshDirty();
  140. }
  141. /// <summary>
  142. ///
  143. /// </summary>
  144. /// <param name="points"></param>
  145. /// <param name="colors"></param>
  146. public void DrawPolygon(IList<Vector2> points, Color32[] colors)
  147. {
  148. PolygonMesh mesh = graphics.GetMeshFactory<PolygonMesh>();
  149. mesh.points.Clear();
  150. mesh.points.AddRange(points);
  151. mesh.fillColor = null;
  152. mesh.colors = colors;
  153. graphics.SetMeshDirty();
  154. }
  155. /// <summary>
  156. ///
  157. /// </summary>
  158. /// <param name="points"></param>
  159. /// <param name="fillColor"></param>
  160. /// <param name="lineSize"></param>
  161. /// <param name="lineColor"></param>
  162. public void DrawPolygon(IList<Vector2> points, Color fillColor, float lineSize, Color lineColor)
  163. {
  164. PolygonMesh mesh = graphics.GetMeshFactory<PolygonMesh>();
  165. mesh.points.Clear();
  166. mesh.points.AddRange(points);
  167. mesh.fillColor = null;
  168. mesh.lineWidth = lineSize;
  169. mesh.lineColor = lineColor;
  170. mesh.colors = null;
  171. graphics.color = fillColor;
  172. graphics.SetMeshDirty();
  173. }
  174. /// <summary>
  175. ///
  176. /// </summary>
  177. /// <param name="sides"></param>
  178. /// <param name="lineSize"></param>
  179. /// <param name="centerColor"></param>
  180. /// <param name="lineColor"></param>
  181. /// <param name="fillColor"></param>
  182. /// <param name="rotation"></param>
  183. /// <param name="distances"></param>
  184. public void DrawRegularPolygon(int sides, float lineSize, Color centerColor, Color lineColor, Color fillColor, float rotation, float[] distances)
  185. {
  186. RegularPolygonMesh mesh = graphics.GetMeshFactory<RegularPolygonMesh>();
  187. mesh.sides = sides;
  188. mesh.lineWidth = lineSize;
  189. mesh.centerColor = centerColor;
  190. mesh.lineColor = lineColor;
  191. mesh.fillColor = null;
  192. mesh.rotation = rotation;
  193. mesh.distances = distances;
  194. graphics.color = fillColor;
  195. graphics.SetMeshDirty();
  196. }
  197. /// <summary>
  198. ///
  199. /// </summary>
  200. public void Clear()
  201. {
  202. graphics.meshFactory = null;
  203. }
  204. /// <summary>
  205. ///
  206. /// </summary>
  207. public bool isEmpty
  208. {
  209. get { return graphics.meshFactory == null; }
  210. }
  211. protected override DisplayObject HitTest()
  212. {
  213. if (graphics.meshFactory == null)
  214. return null;
  215. Vector2 localPoint = WorldToLocal(HitTestContext.worldPoint, HitTestContext.direction);
  216. IHitTest ht = graphics.meshFactory as IHitTest;
  217. if (ht != null)
  218. return ht.HitTest(_contentRect, localPoint) ? this : null;
  219. else if (_contentRect.Contains(localPoint))
  220. return this;
  221. else
  222. return null;
  223. }
  224. }
  225. }