GGraph.cs 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using FairyGUI.Utils;
  5. namespace FairyGUI
  6. {
  7. /// <summary>
  8. /// GGraph class.
  9. /// 对应编辑器里的图形对象。图形有两个用途,一是用来显示简单的图形,例如矩形等;二是作为一个占位的用途,
  10. /// 可以将本对象替换为其他对象,或者在它的前后添加其他对象,相当于一个位置和深度的占位;还可以直接将内容设置
  11. /// 为原生对象。
  12. /// </summary>
  13. public class GGraph : GObject, IColorGear
  14. {
  15. Shape _shape;
  16. public GGraph()
  17. {
  18. }
  19. override protected void CreateDisplayObject()
  20. {
  21. _shape = new Shape();
  22. _shape.gOwner = this;
  23. displayObject = _shape;
  24. }
  25. /// <summary>
  26. /// Replace this object to another object in the display list.
  27. /// 在显示列表中,将指定对象取代这个图形对象。这个图形对象相当于一个占位的用途。
  28. /// </summary>
  29. /// <param name="target">Target object.</param>
  30. public void ReplaceMe(GObject target)
  31. {
  32. if (parent == null)
  33. throw new Exception("parent not set");
  34. target.name = this.name;
  35. target.alpha = this.alpha;
  36. target.rotation = this.rotation;
  37. target.visible = this.visible;
  38. target.touchable = this.touchable;
  39. target.grayed = this.grayed;
  40. target.SetXY(this.x, this.y);
  41. target.SetSize(this.width, this.height);
  42. int index = parent.GetChildIndex(this);
  43. parent.AddChildAt(target, index);
  44. target.relations.CopyFrom(this.relations);
  45. parent.RemoveChild(this, true);
  46. }
  47. /// <summary>
  48. /// Add another object before this object.
  49. /// 在显示列表中,将另一个对象插入到这个对象的前面。
  50. /// </summary>
  51. /// <param name="target">Target object.</param>
  52. public void AddBeforeMe(GObject target)
  53. {
  54. if (parent == null)
  55. throw new Exception("parent not set");
  56. int index = parent.GetChildIndex(this);
  57. parent.AddChildAt(target, index);
  58. }
  59. /// <summary>
  60. /// Add another object after this object.
  61. /// 在显示列表中,将另一个对象插入到这个对象的后面。
  62. /// </summary>
  63. /// <param name="target">Target object.</param>
  64. public void AddAfterMe(GObject target)
  65. {
  66. if (parent == null)
  67. throw new Exception("parent not set");
  68. int index = parent.GetChildIndex(this);
  69. index++;
  70. parent.AddChildAt(target, index);
  71. }
  72. /// <summary>
  73. /// 设置内容为一个原生对象。这个图形对象相当于一个占位的用途。
  74. /// </summary>
  75. /// <param name="obj">原生对象</param>
  76. public void SetNativeObject(DisplayObject obj)
  77. {
  78. if (displayObject == obj)
  79. return;
  80. if (_shape != null)
  81. {
  82. if (_shape.parent != null)
  83. _shape.parent.RemoveChild(displayObject, true);
  84. else
  85. _shape.Dispose();
  86. _shape.gOwner = null;
  87. _shape = null;
  88. }
  89. displayObject = obj;
  90. if (displayObject != null)
  91. {
  92. displayObject.alpha = this.alpha;
  93. displayObject.rotation = this.rotation;
  94. displayObject.visible = this.visible;
  95. displayObject.touchable = this.touchable;
  96. displayObject.gOwner = this;
  97. }
  98. if (parent != null)
  99. parent.ChildStateChanged(this);
  100. HandlePositionChanged();
  101. }
  102. /// <summary>
  103. ///
  104. /// </summary>
  105. public Color color
  106. {
  107. get
  108. {
  109. if (_shape != null)
  110. return _shape.color;
  111. else
  112. return Color.clear;
  113. }
  114. set
  115. {
  116. if (_shape != null && _shape.color != value)
  117. {
  118. _shape.color = value;
  119. UpdateGear(4);
  120. }
  121. }
  122. }
  123. /// <summary>
  124. /// Get the shape object. It can be used for drawing.
  125. /// 获取图形的原生对象,可用于绘制图形。
  126. /// </summary>
  127. public Shape shape
  128. {
  129. get { return _shape; }
  130. }
  131. /// <summary>
  132. /// Draw a rectangle.
  133. /// 画矩形。
  134. /// </summary>
  135. /// <param name="aWidth">Width</param>
  136. /// <param name="aHeight">Height</param>
  137. /// <param name="lineSize">Line size</param>
  138. /// <param name="lineColor">Line color</param>
  139. /// <param name="fillColor">Fill color</param>
  140. public void DrawRect(float aWidth, float aHeight, int lineSize, Color lineColor, Color fillColor)
  141. {
  142. this.SetSize(aWidth, aHeight);
  143. _shape.DrawRect(lineSize, lineColor, fillColor);
  144. }
  145. /// <summary>
  146. ///
  147. /// </summary>
  148. /// <param name="aWidth"></param>
  149. /// <param name="aHeight"></param>
  150. /// <param name="fillColor"></param>
  151. /// <param name="corner"></param>
  152. public void DrawRoundRect(float aWidth, float aHeight, Color fillColor, float[] corner)
  153. {
  154. this.SetSize(aWidth, aHeight);
  155. this.shape.DrawRoundRect(0, Color.white, fillColor, corner[0], corner[1], corner[2], corner[3]);
  156. }
  157. /// <summary>
  158. ///
  159. /// </summary>
  160. /// <param name="aWidth"></param>
  161. /// <param name="aHeight"></param>
  162. /// <param name="fillColor"></param>
  163. public void DrawEllipse(float aWidth, float aHeight, Color fillColor)
  164. {
  165. this.SetSize(aWidth, aHeight);
  166. _shape.DrawEllipse(fillColor);
  167. }
  168. /// <summary>
  169. ///
  170. /// </summary>
  171. /// <param name="aWidth"></param>
  172. /// <param name="aHeight"></param>
  173. /// <param name="points"></param>
  174. /// <param name="fillColor"></param>
  175. public void DrawPolygon(float aWidth, float aHeight, IList<Vector2> points, Color fillColor)
  176. {
  177. this.SetSize(aWidth, aHeight);
  178. _shape.DrawPolygon(points, fillColor);
  179. }
  180. /// <summary>
  181. ///
  182. /// </summary>
  183. /// <param name="aWidth"></param>
  184. /// <param name="aHeight"></param>
  185. /// <param name="points"></param>
  186. /// <param name="fillColor"></param>
  187. /// <param name="lineSize"></param>
  188. /// <param name="lineColor"></param>
  189. public void DrawPolygon(float aWidth, float aHeight, IList<Vector2> points, Color fillColor, float lineSize, Color lineColor)
  190. {
  191. this.SetSize(aWidth, aHeight);
  192. _shape.DrawPolygon(points, fillColor, lineSize, lineColor);
  193. }
  194. override public void Setup_BeforeAdd(ByteBuffer buffer, int beginPos)
  195. {
  196. base.Setup_BeforeAdd(buffer, beginPos);
  197. buffer.Seek(beginPos, 5);
  198. int type = buffer.ReadByte();
  199. if (type != 0)
  200. {
  201. int lineSize = buffer.ReadInt();
  202. Color lineColor = buffer.ReadColor();
  203. Color fillColor = buffer.ReadColor();
  204. bool roundedRect = buffer.ReadBool();
  205. Vector4 cornerRadius = new Vector4();
  206. if (roundedRect)
  207. {
  208. for (int i = 0; i < 4; i++)
  209. cornerRadius[i] = buffer.ReadFloat();
  210. }
  211. if (type == 1)
  212. {
  213. if (roundedRect)
  214. _shape.DrawRoundRect(lineSize, lineColor, fillColor, cornerRadius.x, cornerRadius.y, cornerRadius.z, cornerRadius.w);
  215. else
  216. _shape.DrawRect(lineSize, lineColor, fillColor);
  217. }
  218. else if (type == 2)
  219. _shape.DrawEllipse(lineSize, fillColor, lineColor, fillColor, 0, 360);
  220. else if (type == 3)
  221. {
  222. int cnt = buffer.ReadShort() / 2;
  223. Vector2[] points = new Vector2[cnt];
  224. for (int i = 0; i < cnt; i++)
  225. points[i].Set(buffer.ReadFloat(), buffer.ReadFloat());
  226. _shape.DrawPolygon(points, fillColor, lineSize, lineColor);
  227. }
  228. else if (type == 4)
  229. {
  230. int sides = buffer.ReadShort();
  231. float startAngle = buffer.ReadFloat();
  232. int cnt = buffer.ReadShort();
  233. float[] distances = null;
  234. if (cnt > 0)
  235. {
  236. distances = new float[cnt];
  237. for (int i = 0; i < cnt; i++)
  238. distances[i] = buffer.ReadFloat();
  239. }
  240. _shape.DrawRegularPolygon(sides, lineSize, fillColor, lineColor, fillColor, startAngle, distances);
  241. }
  242. }
  243. }
  244. }
  245. }