UIComponent.cs 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. using CommonUI.Data;
  2. using CommonUnity3D.UGUI;
  3. using UnityEngine;
  4. using UnityEngine.EventSystems;
  5. namespace CommonUnity3D.UGUIEditor
  6. {
  7. public class UIComponent : DisplayNode
  8. {
  9. private UILayout mLayout = null;
  10. private bool mDisable = false;
  11. protected UILayoutGraphics mCurrentLayout;
  12. protected UIComponentMeta mOwnerXML;
  13. public UIComponent(string name = null)
  14. : base(name)
  15. {
  16. }
  17. //----------------------------------------------------------------------------------------------------------------------
  18. public UIEditor Editor { get; private set; }
  19. public string EditName { get; private set; }
  20. public string EditType { get; private set; }
  21. public UILayoutGraphics Graphics { get { return mCurrentLayout; } }
  22. public UIComponentMeta MetaData { get { return mOwnerXML; } }
  23. public UILayout Layout
  24. {
  25. get { return mLayout; }
  26. set
  27. {
  28. mLayout = value;
  29. if (value != null)
  30. {
  31. if (this.mCurrentLayout == null)
  32. {
  33. this.mCurrentLayout = GenLayoutGraphics();
  34. }
  35. }
  36. }
  37. }
  38. public bool Disable
  39. {
  40. set { mDisable = value; }
  41. get { return mDisable; }
  42. }
  43. public UILayout LayoutDisable { get; set; }
  44. //----------------------------------------------------------------------------------------------------------------------
  45. public override DisplayNode Clone()
  46. {
  47. if (mOwnerXML != null)
  48. {
  49. return Editor.CreateFromMeta(mOwnerXML);
  50. }
  51. return null;
  52. }
  53. /// <summary>
  54. /// 触发了OnPointerDown事件
  55. /// </summary>
  56. private bool mIsPointerDown = false;
  57. private float mLongPressSec = 1.0f;
  58. public bool IsLongClick
  59. {
  60. get; private set;
  61. }
  62. public float mPressDownSec;
  63. public delegate void LongPressHandle(DisplayNode sender,float pressSec);
  64. public LongPressHandle event_LongPoniterDown;
  65. public LongPressHandle event_LongPoniterDownStep;
  66. public LongPressHandle event_LongPoniterClick;
  67. public event LongPressHandle LongPoniterDown { add { event_LongPoniterDown += value; } remove { event_LongPoniterDown -= value; } }
  68. public event LongPressHandle LongPoniterDownStep { add { event_LongPoniterDownStep += value; } remove { event_LongPoniterDownStep -= value; } }
  69. public event LongPressHandle LongPoniterClick { add { event_LongPoniterClick += value; } remove { event_LongPoniterClick -= value; } }
  70. protected virtual void OnLongPointerDown(float pressSec) { }
  71. protected virtual void OnLongPointerClick(float pressSec) { }
  72. protected virtual void OnLongPointerDownStep(float pressSec) { }
  73. public float LongPressSecond
  74. {
  75. get
  76. {
  77. return mLongPressSec;
  78. }
  79. set
  80. {
  81. mLongPressSec = value;
  82. }
  83. }
  84. private void CheckLongPressDown()
  85. {
  86. if (mIsPointerDown)
  87. {
  88. mPressDownSec = mPressDownSec + Time.deltaTime;
  89. if (mPressDownSec >= mLongPressSec)
  90. {
  91. if (!IsLongClick)
  92. {
  93. IsLongClick = true;
  94. OnLongPointerDown(mPressDownSec);
  95. if (event_LongPoniterDown != null)
  96. {
  97. event_LongPoniterDown.Invoke(this,mPressDownSec);
  98. }
  99. }
  100. OnLongPointerDownStep(mPressDownSec);
  101. if (event_LongPoniterDownStep != null)
  102. {
  103. event_LongPoniterDownStep.Invoke(this,mPressDownSec);
  104. }
  105. }
  106. }
  107. }
  108. protected override void OnPointerClick(PointerEventData e)
  109. {
  110. base.OnPointerClick(e);
  111. if (IsLongClick)
  112. {
  113. OnLongPointerClick(mPressDownSec);
  114. if(event_LongPoniterClick != null)
  115. {
  116. event_LongPoniterClick.Invoke(this, mPressDownSec);
  117. }
  118. }
  119. }
  120. protected override void OnPointerDown(PointerEventData e)
  121. {
  122. base.OnPointerDown(e);
  123. mIsPointerDown = true;
  124. mPressDownSec = 0;
  125. IsLongClick = false;
  126. }
  127. protected override void OnPointerUp(PointerEventData e)
  128. {
  129. mIsPointerDown = false;
  130. base.OnPointerUp(e);
  131. }
  132. protected override void OnDispose()
  133. {
  134. base.OnDispose();
  135. this.event_LongPoniterDown = null;
  136. this.event_LongPoniterDownStep = null;
  137. this.event_LongPoniterClick = null;
  138. }
  139. /// <summary>
  140. /// 根据编辑器名字搜寻子节点
  141. /// </summary>
  142. /// <typeparam name="T"></typeparam>
  143. /// <param name="edit_name"></param>
  144. /// <param name="recursive"></param>
  145. /// <returns></returns>
  146. public T FindChildByEditName<T>(string edit_name, bool recursive = true) where T : UIComponent
  147. {
  148. return FindChildAs<T>((child) =>
  149. {
  150. return (edit_name == child.EditName);
  151. },
  152. recursive);
  153. }
  154. public UIComponent FindChildByEditName(string edit_name, bool recursive = true)
  155. {
  156. return FindChildByEditName<UIComponent>(edit_name, recursive);
  157. }
  158. //----------------------------------------------------------------------------------------------------------------------
  159. protected override void OnUpdate()
  160. {
  161. base.OnUpdate();
  162. if (mCurrentLayout != null)
  163. {
  164. this.OnUpdateLayout();
  165. }
  166. CheckLongPressDown();
  167. }
  168. protected virtual UILayoutGraphics GenLayoutGraphics()
  169. {
  170. var comp = mGameObject.AddComponent<UILayoutGraphics>();
  171. comp.enabled = false;
  172. return comp;
  173. }
  174. protected virtual void OnUpdateLayout()
  175. {
  176. mCurrentLayout.UpdateSprite();
  177. mCurrentLayout.Alpha = this.Alpha;
  178. if (Enable)
  179. {
  180. mCurrentLayout.SetCurrentLayout(Layout);
  181. }
  182. else
  183. {
  184. if (LayoutDisable != null)
  185. {
  186. mCurrentLayout.SetCurrentLayout(LayoutDisable);
  187. }
  188. else
  189. {
  190. mCurrentLayout.SetCurrentLayout(Layout);
  191. }
  192. }
  193. }
  194. //----------------------------------------------------------------------------------------------------------------------
  195. #region _解析XML_
  196. protected virtual void AddEditorComopnent(UIComponent c)
  197. {
  198. this.AddChild(c);
  199. }
  200. internal void DecodeFromXML(UIEditor.Decoder editor, UIComponentMeta e)
  201. {
  202. this.Editor = editor.editor;
  203. this.EditType = e.ClassName;
  204. this.mOwnerXML = e;
  205. this.DecodeBegin(editor, e);
  206. this.DecodeFields(editor, e);
  207. this.DecodeAttributes(editor, e);
  208. this.DecodeChilds(editor, e);
  209. this.DecodeEnd(editor, e);
  210. if (mCurrentLayout != null)
  211. {
  212. this.OnUpdateLayout();
  213. }
  214. }
  215. protected virtual void DecodeAttributes(UIEditor.Decoder editor, UIComponentMeta e)
  216. {
  217. if (!string.IsNullOrEmpty(e.Attributes))
  218. {
  219. foreach (var entry in e.GetAttributsMap())
  220. {
  221. this.SetAttribute(entry.Key, entry.Value);
  222. }
  223. }
  224. }
  225. protected virtual void DecodeChilds(UIEditor.Decoder editor, UIComponentMeta e)
  226. {
  227. if (e.Childs != null)
  228. {
  229. int len = e.Childs.Count;
  230. for (int i = 0; i < len; ++i)
  231. {
  232. UIComponentMeta child = e.Childs[i];
  233. UIComponent cui = editor.CreateFromMeta(child);
  234. if (cui != null)
  235. {
  236. AddEditorComopnent(cui);
  237. }
  238. }
  239. }
  240. }
  241. protected virtual void DecodeBegin(UIEditor.Decoder editor, UIComponentMeta e)
  242. {
  243. }
  244. protected virtual void DecodeEnd(UIEditor.Decoder editor, UIComponentMeta e)
  245. {
  246. }
  247. protected virtual void DecodeFields(UIEditor.Decoder editor, UIComponentMeta e)
  248. {
  249. this.Bounds2D = new Rect(e.X, e.Y, e.Width, e.Height);
  250. this.Visible = e.Visible;
  251. this.EditName = e.EditorName;
  252. this.Name = string.Format("{0} - {1}", this.EditName, this.EditType);
  253. this.UserData = e.UserData;
  254. this.UserTag = e.UserTag;
  255. this.Layout = editor.CreateLayout(e.Layout);
  256. this.LayoutDisable = editor.CreateLayout(e.DisableLayout);
  257. this.Enable = e.Enable;
  258. this.EnableChildren = e.EnableChilds;
  259. }
  260. #endregion
  261. //----------------------------------------------------------------------------------------------------------------------
  262. }
  263. }