CPJSpriteGraphics.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. using CommonLang;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using UnityEngine;
  7. using CSpriteMeta = CommonUI.Cell.Game.CSpriteMeta;
  8. using CSpriteController = CommonUI.Cell.Game.CSpriteController;
  9. using CSpriteEventHandler = CommonUI.Cell.Game.CSpriteEventHandler;
  10. using CommonUI_Unity3D.Impl;
  11. using UnityEngine.UI;
  12. namespace CommonUnity3D.UGUI
  13. { //---------------------------------------------------------------------------------------------------
  14. public class CPJSpriteGraphics : UnityEngine.UI.MaskableGraphic
  15. {
  16. private DisplayNode mBinding;
  17. private CPJSprite mSpr;
  18. private Texture2D mMainTexture;
  19. private float mAlpha;
  20. private int mCurrentActionIndex;
  21. private int mCurrentFrameIndex = 0;
  22. private DrawType mDrawType = DrawType.Simple;
  23. private Vector2 mDrawGridSize = Vector2.zero;
  24. public DrawType drawType { get { return mDrawType; } set { mDrawType = value; } }
  25. public Vector2 drawGridSize { get { return mDrawGridSize; } set { mDrawGridSize = value; } }
  26. private CommonUI_Unity3D.Impl.UnityImage mSrc;
  27. public override Texture mainTexture { get { return mMainTexture; } }
  28. public float Alpha
  29. {
  30. get { return mAlpha; }
  31. set
  32. {
  33. if (mAlpha != value)
  34. {
  35. mAlpha = value;
  36. Color c = base.color;
  37. c.a = value;
  38. base.color = c;
  39. this.SetAllDirty();
  40. }
  41. }
  42. }
  43. protected override void Start()
  44. {
  45. this.mBinding = DisplayNode.AsDisplayNode(gameObject);
  46. base.Start();
  47. }
  48. public void SetSpriteMeta(CPJSprite spr)
  49. {
  50. this.mSpr = spr;
  51. mSrc = spr.Controller.Meta.Atlas.GetTile(0) as CommonUI_Unity3D.Impl.UnityImage;
  52. this.mMainTexture = mSrc.Texture2D;
  53. this.material = mSrc.TextureMaterial;
  54. this.SetAllDirty();
  55. }
  56. public void SetFrame(int anim, int frame)
  57. {
  58. if (mCurrentActionIndex != anim || mCurrentFrameIndex != frame)
  59. {
  60. mCurrentActionIndex = anim;
  61. mCurrentFrameIndex = frame;
  62. this.SetVerticesDirty();
  63. }
  64. }
  65. public override void SetNativeSize()
  66. {
  67. if (mSpr != null)
  68. {
  69. this.SetAllDirty();
  70. }
  71. }
  72. //-------------------------------------------------------------------------------------------------
  73. protected override void OnPopulateMesh(VertexHelper vh)
  74. {
  75. vh.Clear();
  76. if (mSpr != null)
  77. {
  78. using (VertexHelperBuffer mesh = VertexHelperBuffer.AllocAutoRelease(vh))
  79. {
  80. mesh.BlendColor = this.color;
  81. Vector2 size = rectTransform.sizeDelta;
  82. Vector2 center = rectTransform.rect.center;
  83. if (this.mDrawType == DrawType.Simple)
  84. {
  85. mSpr.Controller.Meta.addVertex(mesh,
  86. mCurrentActionIndex,
  87. mCurrentFrameIndex,
  88. 0, 0);
  89. }
  90. else if (this.mDrawType == DrawType.Center)
  91. {
  92. mSpr.Controller.Meta.addVertex(mesh,
  93. mCurrentActionIndex,
  94. mCurrentFrameIndex,
  95. center.x, center.y);
  96. }
  97. else if (this.mDrawType == DrawType.FillGrid)
  98. {
  99. if (this.mDrawGridSize.x > 0 || this.mDrawGridSize.y > 0)
  100. {
  101. for (float dx = 0; dx < size.x; dx += mDrawGridSize.x)
  102. {
  103. for (float dy = 0; dy < size.y; dy += mDrawGridSize.y)
  104. {
  105. mSpr.Controller.Meta.addVertex(mesh,
  106. mCurrentActionIndex,
  107. mCurrentFrameIndex,
  108. dx, dy);
  109. }
  110. }
  111. }
  112. else
  113. {
  114. mSpr.Controller.Meta.addVertex(mesh,
  115. mCurrentActionIndex,
  116. mCurrentFrameIndex,
  117. 0, 0);
  118. }
  119. }
  120. }
  121. }
  122. }
  123. //-------------------------------------------------------------------------------------------------
  124. public virtual void CalculateLayoutInputHorizontal()
  125. {
  126. }
  127. public virtual void CalculateLayoutInputVertical()
  128. {
  129. }
  130. public override bool Raycast(Vector2 sp, Camera eventCamera)
  131. {
  132. if (mBinding.Enable && mBinding.EnableTouchInParents)
  133. {
  134. return base.Raycast(sp, eventCamera);
  135. }
  136. return false;
  137. }
  138. public enum DrawType
  139. {
  140. Simple,
  141. Center,
  142. FillGrid,
  143. }
  144. private void ResetTexture()
  145. {
  146. if (mSrc != null && mSrc.Texture2D != mMainTexture)
  147. {
  148. this.mMainTexture = mSrc.Texture2D;
  149. this.material = mSrc.TextureMaterial;
  150. base.SetAllDirty();
  151. }
  152. }
  153. void Update()
  154. {
  155. ResetTexture();
  156. }
  157. protected override void OnEnable()
  158. {
  159. base.OnEnable();
  160. ResetTexture();
  161. }
  162. }
  163. }