UILayoutGraphics.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. using System.Collections.Generic;
  2. using UnityEngine;
  3. using CommonUI.Data;
  4. using CommonUnity3D.UGUI;
  5. using UnityImage = CommonUI_Unity3D.Impl.UnityImage;
  6. using CommonUI.Cell.Game;
  7. using CommonUI_Unity3D.Impl;
  8. using System;
  9. namespace CommonUnity3D.UGUIEditor
  10. {
  11. public class UILayoutGraphics : ImageGraphics
  12. {
  13. private UILayout mLayout;
  14. private float mAlpha = 1f;
  15. [SerializeField]
  16. private bool m_IsShowUILayout = true;
  17. public bool IsShowUILayout
  18. {
  19. get { return m_IsShowUILayout; }
  20. set
  21. {
  22. if (m_IsShowUILayout != value)
  23. {
  24. m_IsShowUILayout = value;
  25. base.SetVerticesDirty();
  26. }
  27. }
  28. }
  29. private Texture2D mLastLayoutTexture;
  30. public override Texture mainTexture
  31. {
  32. get { return (IsShowUILayout) ? mLayout.MainTexture : base.mainTexture; }
  33. }
  34. public float Alpha
  35. {
  36. get { return mAlpha; }
  37. set
  38. {
  39. if (value != mAlpha)
  40. {
  41. mAlpha = value;
  42. Color c = base.color;
  43. c.a = value;
  44. base.color = c;
  45. this.SetAllDirty();
  46. }
  47. }
  48. }
  49. public UILayoutGraphics()
  50. {
  51. }
  52. public void UpdateSprite()
  53. {
  54. if (mLayout != null && mLayout.mSpriteController != null)
  55. {
  56. if (mLayout.mSpriteController.Update())
  57. {
  58. this.SetVerticesDirty();
  59. }
  60. }
  61. }
  62. public UILayoutGraphics SetCurrentLayout(UILayout layout)
  63. {
  64. if (mLayout != layout ||
  65. (mLayout != null && mLayout.MainTexture != mLastLayoutTexture))
  66. {
  67. this.mLayout = layout;
  68. if (layout != null)
  69. {
  70. this.enabled = true;
  71. mLastLayoutTexture = mLayout.MainTexture;
  72. if (mLayout.mImageSrc != null && mLayout.mImageRegion != null)
  73. {
  74. SetImage(mLayout.mImageSrc, mLayout.mImageRegion, Vector2.zero);
  75. }
  76. }
  77. else
  78. {
  79. this.enabled = false;
  80. }
  81. this.SetAllDirty();
  82. }
  83. return this;
  84. }
  85. public UILayoutGraphics SetFillMode(FillMethod fill, int fillOrigin, bool fillClockwise = false, bool fillCenter = true)
  86. {
  87. base.type = Type.Filled;
  88. base.fillMethod = fill;
  89. base.fillOrigin = fillOrigin;
  90. base.fillClockwise = fillClockwise;
  91. base.fillCenter = fillCenter;
  92. this.m_IsShowUILayout = false;
  93. this.SetAllDirty();
  94. return this;
  95. }
  96. public UILayoutGraphics SetFillPercent(float percent)
  97. {
  98. base.fillAmount = CommonLang.CMath.getInRange(percent, 0, 100) / 100f;
  99. return this;
  100. }
  101. public override void CalculateLayoutInputHorizontal() { }
  102. public override void CalculateLayoutInputVertical() { }
  103. public override void OnAfterDeserialize() { }
  104. public override void OnBeforeSerialize() { }
  105. public override void SetNativeSize()
  106. {
  107. if (mLayout != null)
  108. {
  109. this.SetAllDirty();
  110. }
  111. else
  112. {
  113. base.SetNativeSize();
  114. }
  115. }
  116. protected override void OnDestroy()
  117. {
  118. if (base.sprite != null)
  119. {
  120. Sprite.Destroy(sprite);
  121. }
  122. base.OnDestroy();
  123. }
  124. protected override void OnPopulateMesh(UnityEngine.UI.VertexHelper vh)
  125. {
  126. if (IsShowUILayout)
  127. {
  128. vh.Clear();
  129. using (var o = new HelperVBO(vh, this.color))
  130. {
  131. o.OnFillVBO(rectTransform.sizeDelta, mLayout);
  132. }
  133. }
  134. else
  135. {
  136. // if (base.sprite == null && mLayout != null)
  137. // {
  138. // if (mLayout.mImageSrc != null && mLayout.mImageRegion != null)
  139. // {
  140. // if (base.sprite != null)
  141. // {
  142. // Sprite.Destroy(sprite);
  143. // }
  144. // base.sprite = UIUtils.CreateSprite(mLayout.mImageSrc, mLayout.mImageRegion, Vector2.zero);
  145. // }
  146. // }
  147. base.OnPopulateMesh(vh);
  148. }
  149. }
  150. private static UIVertex[] s_UIVertexQuard = new UIVertex[4];
  151. private static UIVertex[,] s_UIVertex4x4 = new UIVertex[4, 4];
  152. private static float[] ax_4 = new float[4];
  153. private static float[] ax_2 = new float[2];
  154. private static float[] ay_4 = new float[4];
  155. private static float[] ay_2 = new float[2];
  156. private static float[] au_4 = new float[4];
  157. private static float[] au_2 = new float[2];
  158. private static float[] av_4 = new float[4];
  159. private static float[] av_2 = new float[2];
  160. private static void ArraySet4(float[] array, float a, float b, float c, float d)
  161. {
  162. array[0] = a;
  163. array[1] = b;
  164. array[2] = c;
  165. array[3] = d;
  166. }
  167. private static void ArraySet2(float[] array, float a, float b)
  168. {
  169. array[0] = a;
  170. array[1] = b;
  171. }
  172. private struct HelperVBO : VBO
  173. {
  174. private UnityEngine.UI.VertexHelper toFill;
  175. private VertexHelperBuffer vbo;
  176. public HelperVBO(UnityEngine.UI.VertexHelper mesh, Color color)
  177. {
  178. this.toFill = mesh;
  179. this.vbo = VertexHelperBuffer.AllocAutoRelease(mesh);
  180. this.vbo.BlendColor = color;
  181. }
  182. public void Dispose()
  183. {
  184. vbo.Dispose();
  185. vbo = null;
  186. toFill = null;
  187. }
  188. public void OnFillVBO(Vector2 size, UILayout layout)
  189. {
  190. switch (layout.Style)
  191. {
  192. case UILayoutStyle.NULL:
  193. break;
  194. case UILayoutStyle.COLOR:
  195. VertexFillColor(layout, size.x, size.y, layout.mFillColor);
  196. break;
  197. case UILayoutStyle.SPRITE:
  198. if (layout.mSpriteController != null) VertexSprite(layout, size.x, size.y);
  199. break;
  200. case UILayoutStyle.IMAGE_STYLE_ALL_8:
  201. if (layout.mImageSrc != null) VertexAll9(layout, size.x, size.y);
  202. break;
  203. case UILayoutStyle.IMAGE_STYLE_ALL_9:
  204. if (layout.mImageSrc != null) VertexAll9(layout, size.x, size.y);
  205. break;
  206. case UILayoutStyle.IMAGE_STYLE_H_012:
  207. if (layout.mImageSrc != null) VertexH012(layout, size.x, size.y);
  208. break;
  209. case UILayoutStyle.IMAGE_STYLE_V_036:
  210. if (layout.mImageSrc != null) VertexV036(layout, size.x, size.y);
  211. break;
  212. case UILayoutStyle.IMAGE_STYLE_HLM:
  213. break;
  214. case UILayoutStyle.IMAGE_STYLE_VTM:
  215. break;
  216. case UILayoutStyle.IMAGE_STYLE_BACK_4:
  217. if (layout.mImageSrc != null) VertexBack4(layout, size.x, size.y);
  218. break;
  219. case UILayoutStyle.IMAGE_STYLE_BACK_4_CENTER:
  220. if (layout.mImageSrc != null) VertexBack4Center(layout, size.x, size.y);
  221. break;
  222. default:
  223. break;
  224. }
  225. }
  226. private void VertexBuffer(UnityImage src, float[] ax, float[] ay, float[] au, float[] av)
  227. {
  228. for (int iy = 0; iy < ay.Length; ++iy)
  229. {
  230. for (int ix = 0; ix < ax.Length; ++ix)
  231. {
  232. s_UIVertex4x4[ix, iy] = UIUtils.CreateVertex(src, vbo.BlendColor, au[ix], av[iy], ax[ix], ay[iy]);
  233. }
  234. }
  235. for (int iy = 0; iy < ay.Length - 1; ++iy)
  236. {
  237. for (int ix = 0; ix < ax.Length - 1; ++ix)
  238. {
  239. s_UIVertexQuard[0] = s_UIVertex4x4[ix + 0, iy + 0];
  240. s_UIVertexQuard[1] = s_UIVertex4x4[ix + 1, iy + 0];
  241. s_UIVertexQuard[2] = s_UIVertex4x4[ix + 1, iy + 1];
  242. s_UIVertexQuard[3] = s_UIVertex4x4[ix + 0, iy + 1];
  243. toFill.AddUIVertexQuad(s_UIVertexQuard);
  244. }
  245. }
  246. }
  247. private void VertexAll9(UILayout mLayout, float w, float h)
  248. {
  249. float cw = mLayout.mClipSize;
  250. float ch = mLayout.mClipSize;
  251. if (cw * 2 > w) { cw = w / 2f; }
  252. if (ch * 2 > h) { ch = h / 2f; }
  253. ArraySet4(ax_4, 0, cw, w - cw, w);
  254. ArraySet4(ay_4, 0, ch, h - ch, h);
  255. ArraySet4(au_4,
  256. mLayout.mImageRegion.x,
  257. mLayout.mImageRegion.x + mLayout.mClipSize,
  258. mLayout.mImageRegion.x + mLayout.mImageRegion.width - mLayout.mClipSize,
  259. mLayout.mImageRegion.x + mLayout.mImageRegion.width
  260. );
  261. ArraySet4(av_4,
  262. mLayout.mImageRegion.y,
  263. mLayout.mImageRegion.y + mLayout.mClipSize,
  264. mLayout.mImageRegion.y + mLayout.mImageRegion.height - mLayout.mClipSize,
  265. mLayout.mImageRegion.y + mLayout.mImageRegion.height
  266. );
  267. VertexBuffer(mLayout.mImageSrc, ax_4, ay_4, au_4, av_4);
  268. }
  269. private void VertexH012(UILayout mLayout, float w, float h)
  270. {
  271. float cw = mLayout.mClipSize;
  272. if (cw * 2 > w) { cw = w / 2; }
  273. ArraySet4(ax_4, 0, cw, w - cw, w);
  274. ArraySet2(ay_2, 0, h);
  275. ArraySet4(au_4,
  276. mLayout.mImageRegion.x,
  277. mLayout.mImageRegion.x + mLayout.mClipSize,
  278. mLayout.mImageRegion.x + mLayout.mImageRegion.width - mLayout.mClipSize,
  279. mLayout.mImageRegion.x + mLayout.mImageRegion.width
  280. );
  281. ArraySet2(av_2,
  282. mLayout.mImageRegion.y,
  283. mLayout.mImageRegion.y + mLayout.mImageRegion.height
  284. );
  285. VertexBuffer(mLayout.mImageSrc, ax_4, ay_2, au_4, av_2);
  286. }
  287. private void VertexV036(UILayout mLayout, float w, float h)
  288. {
  289. float ch = mLayout.mClipSize;
  290. if (ch * 2 > h) { ch = h / 2; }
  291. ArraySet2(ax_2, 0, w);
  292. ArraySet4(ay_4, 0, ch, h - ch, h);
  293. ArraySet2(au_2,
  294. mLayout.mImageRegion.x,
  295. mLayout.mImageRegion.x + mLayout.mImageRegion.width
  296. );
  297. ArraySet4(av_4,
  298. mLayout.mImageRegion.y,
  299. mLayout.mImageRegion.y + mLayout.mClipSize,
  300. mLayout.mImageRegion.y + mLayout.mImageRegion.height - mLayout.mClipSize,
  301. mLayout.mImageRegion.y + mLayout.mImageRegion.height
  302. );
  303. VertexBuffer(mLayout.mImageSrc, ax_2, ay_4, au_2, av_4);
  304. }
  305. private void VertexBack4(UILayout mLayout, float w, float h)
  306. {
  307. ArraySet2(ax_2, 0, w);
  308. ArraySet2(ay_2, 0, h);
  309. ArraySet2(au_2,
  310. mLayout.mImageRegion.x,
  311. mLayout.mImageRegion.x + mLayout.mImageRegion.width
  312. );
  313. ArraySet2(av_2,
  314. mLayout.mImageRegion.y,
  315. mLayout.mImageRegion.y + mLayout.mImageRegion.height
  316. );
  317. VertexBuffer(mLayout.mImageSrc, ax_2, ay_2, au_2, av_2);
  318. }
  319. private void VertexBack4Center(UILayout mLayout, float w, float h)
  320. {
  321. float tx = (w - mLayout.mImageRegion.width) * 0.5f;
  322. float ty = (h - mLayout.mImageRegion.height) * 0.5f;
  323. ArraySet2(ax_2, tx, tx + mLayout.mImageRegion.width);
  324. ArraySet2(ay_2, ty, ty + mLayout.mImageRegion.height);
  325. ArraySet2(au_2,
  326. mLayout.mImageRegion.x,
  327. mLayout.mImageRegion.x + mLayout.mImageRegion.width
  328. );
  329. ArraySet2(av_2,
  330. mLayout.mImageRegion.y,
  331. mLayout.mImageRegion.y + mLayout.mImageRegion.height
  332. );
  333. VertexBuffer(mLayout.mImageSrc, ax_2, ay_2, au_2, av_2);
  334. }
  335. private void VertexFillColor(UILayout mLayout, float w, float h, Color c)
  336. {
  337. UIUtils.CreateVertexQuardColor(c * vbo.BlendColor, 0, 0, w, h, toFill);
  338. }
  339. private void VertexSprite(UILayout mLayout, float w, float h)
  340. {
  341. mLayout.mSpriteController.Meta.addVertex(vbo,
  342. mLayout.mSpriteController.CurrentAnimate,
  343. mLayout.mSpriteController.CurrentFrame,
  344. w / 2, h / 2);
  345. }
  346. }
  347. //---------------------------------------------------------------------------------------------------------------
  348. interface VBO : IDisposable
  349. {
  350. void OnFillVBO(Vector2 size, UILayout layout);
  351. }
  352. //---------------------------------------------------------------------------------------------------------------
  353. //---------------------------------------------------------------------------------------------------------------
  354. //---------------------------------------------------------------------------------------------------------------
  355. }
  356. }