DisplayNode.cs 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320
  1. using CommonLang;
  2. using CommonLang.Concurrent;
  3. using CommonLang.Property;
  4. using CommonUnity3D.UGUIAction;
  5. using System;
  6. using System.Collections;
  7. using System.Collections.Generic;
  8. using System.Linq;
  9. using System.Text;
  10. using UnityEngine;
  11. using UnityEngine.EventSystems;
  12. using UnityEngine.UI;
  13. namespace CommonUnity3D.UGUI
  14. {
  15. public class DisplayNode : IDisposable, IActionCompment
  16. {
  17. private static List<GameObject> s_fuck_list = new List<GameObject>();
  18. private static AtomicLong s_RefCount = new AtomicLong(0);
  19. private static AtomicLong s_AliveCount = new AtomicLong(0);
  20. public static long RefCount { get { return s_RefCount.Value; } }
  21. public static long AliveCount { get { return s_AliveCount.Value; } }
  22. protected internal static void FuckFuckList()
  23. {
  24. lock (s_fuck_list)
  25. {
  26. if (s_fuck_list.Count > 0)
  27. {
  28. foreach (var o in s_fuck_list)
  29. {
  30. GameObject.Destroy(o, 0.3f);
  31. }
  32. s_fuck_list.Clear();
  33. }
  34. }
  35. }
  36. private readonly DisplayNodeBehaviour mBinding;
  37. internal protected readonly GameObject mGameObject;
  38. internal protected readonly RectTransform mTransform;
  39. internal protected readonly CanvasRenderer mCanvasRender;
  40. private DisplayNode mParent;
  41. private DisplayRoot mRoot;
  42. // 一般用于上层临时隐藏这个单位 //
  43. private bool mVisibleInParent = true;
  44. private float mAlpha = 1f;
  45. private bool mIsGray = false;
  46. private bool mVisible = true;
  47. private bool mIsDispose = false;
  48. private bool mIsEnable = false;
  49. private bool mIsEnableChildren = false;
  50. private bool mIsPointerDown = false;
  51. private GrayMaterialModifier mGrayModifier;
  52. private IInteractiveComponent mSelectable;
  53. private bool mNeedRefreshInteractive = true;
  54. private static RectTransform mDisabledNode = null;
  55. //-------------------------------------------------------------------------------------------------------
  56. public DisplayNode(string name = "")
  57. {
  58. s_RefCount++;
  59. s_AliveCount++;
  60. mGameObject = new GameObject(name);
  61. mGameObject.SetActive(false);
  62. mTransform = mGameObject.AddComponent<RectTransform>();
  63. mTransform.anchorMin = new Vector2(0, 1);
  64. mTransform.anchorMax = new Vector2(0, 1);
  65. mTransform.pivot = new Vector2(0, 1);
  66. mTransform.position = Vector3.zero;
  67. mTransform.anchoredPosition = Vector2.zero;
  68. mTransform.localScale = Vector3.one;
  69. mTransform.sizeDelta = new Vector2(100, 100);
  70. mCanvasRender = GenCanvasRenderer();
  71. mBinding = GenNodeBehavior();
  72. mBinding.mBinding = this;
  73. if (mDisabledNode == null)
  74. {
  75. GameObject o = new GameObject("DisabledNode");
  76. mDisabledNode = o.AddComponent<RectTransform>();
  77. UnityEngine.Object.DontDestroyOnLoad(o);
  78. //o.AddComponent<Canvas>();
  79. //mDisabledNode.localScale = this.Transform.lossyScale;
  80. //mDisabledNode.pivot = this.Transform.pivot;
  81. //mDisabledNode.anchoredPosition = this.Transform.anchoredPosition;
  82. //mDisabledNode.sizeDelta = this.Transform.sizeDelta;
  83. o.SetActive(false);
  84. }
  85. }
  86. ~DisplayNode()
  87. {
  88. if (!mIsDispose)
  89. {
  90. lock (s_fuck_list)
  91. {
  92. s_fuck_list.Add(mGameObject);
  93. }
  94. }
  95. s_RefCount--;
  96. }
  97. public void Dispose()
  98. {
  99. //避免2次dispose,造成崩溃.
  100. if (mIsDispose)
  101. {
  102. // throw new Exception("DisplayNode:\"{0}\" already disposed !");
  103. return;
  104. }
  105. this.mUpdateNodes.Clear();
  106. if (this.event_disposed != null)
  107. {
  108. this.event_disposed.Invoke(this);
  109. }
  110. this.RemoveAllAction();
  111. this.OnDisposeEvents();
  112. this.OnDispose();
  113. this.mAttributes.Clear();
  114. foreach (var c in AllChildren)
  115. {
  116. c.Dispose();
  117. }
  118. this.mParent = null;
  119. this.mTransform.SetParent(mDisabledNode, false);
  120. GameObject.Destroy(mGameObject, 0.3f);
  121. this.mIsDispose = true;
  122. s_AliveCount--;
  123. }
  124. public virtual DisplayNode Clone()
  125. {
  126. return null;
  127. }
  128. //-------------------------------------------------------------------------------------------------------
  129. public GameObject UnityObject { get { return mGameObject; } }
  130. public DisplayNode Parent { get { return mParent; } }
  131. public RectTransform Transform { get { return mTransform; } }
  132. public DisplayRoot Root
  133. {
  134. get
  135. {
  136. if (this is DisplayRoot)
  137. {
  138. mRoot = this as DisplayRoot;
  139. return mRoot;
  140. }
  141. else if (mRoot != null)
  142. {
  143. return mRoot;
  144. }
  145. else
  146. {
  147. DisplayNode parent = mParent;
  148. while (parent != null)
  149. {
  150. if (parent is DisplayRoot)
  151. {
  152. mRoot = parent as DisplayRoot;
  153. return mRoot;
  154. }
  155. parent = parent.mParent;
  156. }
  157. }
  158. return mRoot;
  159. }
  160. }
  161. public bool IsDispose { get { return mIsDispose; } }
  162. public int NumChildren { get { return mTransform.childCount; } }
  163. public string UserData { get; set; }
  164. public int UserTag { get; set; }
  165. public object Tag { get; set; }
  166. public string Name
  167. {
  168. get { return mGameObject.name; }
  169. set { mGameObject.name = value; }
  170. }
  171. public float X
  172. {
  173. get { return Position2D.x; }
  174. set { Position2D = new Vector2(value, Position2D.y); }
  175. }
  176. public float Y
  177. {
  178. get { return Position2D.y; }
  179. set { Position2D = new Vector2(Position2D.x, value); }
  180. }
  181. public float Width
  182. {
  183. get { return Size2D.x; }
  184. set { Size2D = new Vector2(value, Size2D.y); }
  185. }
  186. public float Height
  187. {
  188. get { return Size2D.y; }
  189. set { Size2D = new Vector2(Size2D.x, value); }
  190. }
  191. /// <summary>
  192. /// 可显示
  193. /// </summary>
  194. public bool Visible
  195. {
  196. get { return mVisible; }
  197. set
  198. {
  199. if (mVisible != value)
  200. {
  201. mVisible = value;
  202. if (mParent != null)
  203. {
  204. mGameObject.SetActive(mVisibleInParent && mVisible);
  205. }
  206. else
  207. {
  208. mGameObject.SetActive(false);
  209. }
  210. OnVisibleChanged(value);
  211. }
  212. }
  213. }
  214. /// <summary>
  215. /// 是否在父节点显示范围内,用于滚动控件交互以及临时需要隐藏节点操作
  216. /// </summary>
  217. public bool VisibleInParent
  218. {
  219. get { return mVisibleInParent; }
  220. set
  221. {
  222. if (mVisibleInParent != value)
  223. {
  224. mVisibleInParent = value;
  225. if (mParent != null)
  226. {
  227. mGameObject.SetActive(mVisibleInParent && mVisible);
  228. }
  229. else
  230. {
  231. mGameObject.SetActive(false);
  232. }
  233. OnVisibleChanged(value);
  234. }
  235. }
  236. }
  237. /// <summary>
  238. /// Alpha度
  239. /// </summary>
  240. public float Alpha
  241. {
  242. get { return mAlpha; }
  243. set
  244. {
  245. if (mAlpha != value)
  246. {
  247. SetAlphaInternal(value);
  248. this.mAlpha = value;
  249. this.mBinding.Alpha = value;
  250. }
  251. }
  252. }
  253. public bool IsGray
  254. {
  255. get { return mIsGray; }
  256. set
  257. {
  258. if (mIsGray != value)
  259. {
  260. this.SetGrayInternal(value);
  261. this.mIsGray = value;
  262. this.mBinding.IsGray = value;
  263. }
  264. }
  265. }
  266. /// <summary>
  267. /// 缩放系数.
  268. /// </summary>
  269. public Vector2 Scale
  270. {
  271. get
  272. {
  273. Vector3 temp = mTransform.localScale;
  274. return new Vector2(temp.x, temp.y);
  275. }
  276. set
  277. {
  278. mTransform.localScale = new Vector3(value.x, value.y);
  279. }
  280. }
  281. /// <summary>
  282. /// 目标外也能触发move事件
  283. /// </summary>
  284. public bool EnableOutMove { get; set; }
  285. /// <summary>
  286. /// 可点击自身
  287. /// </summary>
  288. public bool Enable
  289. {
  290. get { return mIsEnable; }
  291. set
  292. {
  293. mBinding.IsEnable = value;
  294. if (mIsEnable != value)
  295. {
  296. mIsEnable = value;
  297. mNeedRefreshInteractive = true;
  298. }
  299. }
  300. }
  301. /// <summary>
  302. /// 可点击子节点
  303. /// </summary>
  304. public bool EnableChildren
  305. {
  306. get { return mIsEnableChildren; }
  307. set
  308. {
  309. mBinding.IsEnableChildren = value;
  310. mIsEnableChildren = value;
  311. }
  312. }
  313. /// <summary>
  314. /// 是否允许点击事件
  315. /// </summary>
  316. public bool IsInteractive
  317. {
  318. get { return mSelectable != null && mSelectable.AsSelectable.enabled; }
  319. set
  320. {
  321. mBinding.IsInteractive = value;
  322. if (IsInteractive != value)
  323. {
  324. if (value)
  325. {
  326. if (mSelectable == null)
  327. {
  328. mSelectable = GenInteractive();
  329. mSelectable.event_PointerDown = this.DoPointerDown;
  330. mSelectable.event_PointerUp = this.DoPointerUp;
  331. mSelectable.event_PointerClick = this.DoPointerClick;
  332. mSelectable.AsSelectable.enabled = true;
  333. }
  334. else
  335. {
  336. mSelectable.AsSelectable.enabled = true;
  337. }
  338. }
  339. else
  340. {
  341. if (mSelectable != null)
  342. {
  343. mSelectable.AsSelectable.enabled = false;
  344. }
  345. }
  346. mNeedRefreshInteractive = true;
  347. }
  348. }
  349. }
  350. /// <summary>
  351. /// 父节点允许子节点点击
  352. /// </summary>
  353. public bool EnableTouchInParents
  354. {
  355. get
  356. {
  357. DisplayNode parent = mParent;
  358. while (parent != null)
  359. {
  360. if (!parent.EnableChildren)
  361. {
  362. return false;
  363. }
  364. parent = parent.mParent;
  365. }
  366. return true;
  367. }
  368. }
  369. public void SetAnchor(Vector2 v)
  370. {
  371. if (this.Parent == null || (mTransform.anchorMin == v && mTransform.anchorMax == v))
  372. {
  373. return;
  374. }
  375. Vector2 last = mTransform.anchoredPosition;
  376. Vector2 s = mTransform.sizeDelta;
  377. Vector2 p = this.Position2D;
  378. mTransform.anchorMin = v;
  379. mTransform.anchorMax = v;
  380. mTransform.offsetMin = Vector2.zero;
  381. mTransform.offsetMax = Vector2.zero;
  382. Vector2 half = new Vector2(this.Parent.Size2D.x * v.x, this.Parent.Size2D.y * v.y);
  383. Vector2 shalf = new Vector2(s.x * v.x, s.y * v.y);
  384. Vector2 offet = new Vector2(half.x - last.x - shalf.x, half.y + last.y - shalf.y);
  385. mTransform.anchoredPosition = offet;
  386. }
  387. //-------------------------------------------------------------------------------------------------------
  388. public T AddComponent<T>() where T : Component
  389. {
  390. return mGameObject.AddComponent<T>();
  391. }
  392. public Component AddComponent(Type componentType)
  393. {
  394. return mGameObject.AddComponent(componentType);
  395. }
  396. //---------------------------------------------------------------------------------------------------------------------------------------------
  397. #region _InternalCall_
  398. private void SetAlphaInternal(float _alpha)
  399. {
  400. Graphic grap = mTransform.GetComponent<Graphic>();
  401. if (grap != null)
  402. {
  403. var c = grap.color;
  404. c.a = _alpha * mAlpha;
  405. grap.color = c;
  406. }
  407. int count = mTransform.childCount;
  408. for (int i = 0; i < count; i++)
  409. {
  410. var child = mTransform.GetChild(i);
  411. DisplayNode node = AsDisplayNode(child.gameObject);
  412. if (node != null)
  413. {
  414. node.SetAlphaInternal(_alpha);
  415. }
  416. }
  417. }
  418. private void SetGrayInternal(bool isGray)
  419. {
  420. Graphic grap = mTransform.GetComponent<Graphic>();
  421. if (grap != null)
  422. {
  423. if (isGray)
  424. {
  425. if (this.mGrayModifier == null)
  426. {
  427. this.mGrayModifier = GenGrayMaterialModifier(grap);
  428. }
  429. }
  430. else
  431. {
  432. if (this.mGrayModifier != null)
  433. {
  434. MonoBehaviour.DestroyImmediate(mGrayModifier);
  435. mGrayModifier = null;
  436. }
  437. }
  438. grap.SetMaterialDirty();
  439. }
  440. int count = mTransform.childCount;
  441. for (int i = 0; i < count; i++)
  442. {
  443. var child = mTransform.GetChild(i);
  444. DisplayNode node = AsDisplayNode(child.gameObject);
  445. if (node != null)
  446. {
  447. node.SetGrayInternal(isGray);
  448. }
  449. }
  450. }
  451. #endregion
  452. //---------------------------------------------------------------------------------------------------------------------------------------------
  453. #region __Events__
  454. internal void DoStart()
  455. {
  456. this.OnStart();
  457. }
  458. internal void DoUpdate()
  459. {
  460. if (mNeedRefreshInteractive)
  461. {
  462. mNeedRefreshInteractive = false;
  463. OnInteractiveChanged();
  464. }
  465. if (mSelectable != null && (EnableOutMove ? mIsPointerDown : mSelectable.IsPressDown))
  466. {
  467. DoPointerMove(mSelectable.LastPointerDown);
  468. }
  469. this.UpdateAction(Time.deltaTime);
  470. this.OnUpdate();
  471. }
  472. internal void DoEndUpdate()
  473. {
  474. this.OnEndUpdate();
  475. }
  476. private void DoPointerDown(PointerEventData e)
  477. {
  478. this.OnPointerDown(e);
  479. mIsPointerDown = true;
  480. if (this.event_PointerDown != null)
  481. {
  482. this.event_PointerDown.Invoke(this, e);
  483. }
  484. }
  485. private void DoPointerUp(PointerEventData e)
  486. {
  487. this.OnPointerUp(e);
  488. mIsPointerDown = false;
  489. if (this.event_PointerUp != null)
  490. {
  491. this.event_PointerUp.Invoke(this, e);
  492. }
  493. }
  494. private void DoPointerClick(PointerEventData e)
  495. {
  496. this.OnPointerClick(e);
  497. if (this.event_PointerClick != null)
  498. {
  499. this.event_PointerClick.Invoke(this, e);
  500. }
  501. }
  502. private void DoPointerMove(PointerEventData e)
  503. {
  504. this.OnPointerMove(e);
  505. if (this.event_PointerMove != null)
  506. {
  507. this.event_PointerMove.Invoke(this, e);
  508. }
  509. }
  510. protected virtual void OnDispose() { }
  511. protected virtual void OnStart() { }
  512. protected virtual void OnUpdate() { }
  513. protected virtual void OnEndUpdate() { }
  514. protected virtual void OnChildAdded(DisplayNode child) { }
  515. protected virtual void OnChildRemoved(DisplayNode child) { }
  516. protected virtual void OnSizeChanged(Vector2 size) { }
  517. protected virtual void OnVisibleChanged(bool visible) { }
  518. protected virtual void OnPointerDown(PointerEventData e) { }
  519. protected virtual void OnPointerUp(PointerEventData e) { }
  520. protected virtual void OnPointerClick(PointerEventData e) { }
  521. protected virtual void OnPointerMove(PointerEventData e) { }
  522. public delegate void PointerEventHandler(DisplayNode sender, PointerEventData e);
  523. public delegate void ChildEventHandler(DisplayNode sender, DisplayNode e);
  524. public delegate void DiposeEventHandle(DisplayNode sender);
  525. [Desc("子节点已添加")]
  526. public ChildEventHandler event_ChildAdded;
  527. [Desc("子节点已移除")]
  528. public ChildEventHandler event_ChildRemoved;
  529. [Desc("鼠标(触摸)已按下")]
  530. public PointerEventHandler event_PointerDown;
  531. [Desc("鼠标(触摸)已松开")]
  532. public PointerEventHandler event_PointerUp;
  533. [Desc("鼠标(触摸)已移动")]
  534. public PointerEventHandler event_PointerMove;
  535. [Desc("Click")]
  536. public PointerEventHandler event_PointerClick;
  537. [Desc("节点已销毁")]
  538. public DiposeEventHandle event_disposed;
  539. [Desc("节点已销毁")]
  540. public event DiposeEventHandle Disposed { add { event_disposed += value; } remove { event_disposed -= value; } }
  541. [Desc("子节点已添加")]
  542. public event ChildEventHandler ChildAdded { add { event_ChildAdded += value; } remove { event_ChildAdded -= value; } }
  543. [Desc("子节点已移除")]
  544. public event ChildEventHandler ChildRemoved { add { event_ChildRemoved += value; } remove { event_ChildRemoved -= value; } }
  545. [Desc("鼠标(触摸)已按下")]
  546. public event PointerEventHandler PointerDown { add { event_PointerDown += value; } remove { event_PointerDown -= value; } }
  547. [Desc("鼠标(触摸)已松开")]
  548. public event PointerEventHandler PointerUp { add { event_PointerUp += value; } remove { event_PointerUp -= value; } }
  549. [Desc("鼠标(触摸)已移动")]
  550. public event PointerEventHandler PointerMove { add { event_PointerMove += value; } remove { event_PointerMove -= value; } }
  551. [Desc("Click")]
  552. public event PointerEventHandler PointerClick { add { event_PointerClick += value; } remove { event_PointerClick -= value; } }
  553. protected virtual void OnDisposeEvents()
  554. {
  555. this.event_ChildAdded = null;
  556. this.event_ChildRemoved = null;
  557. this.event_PointerDown = null;
  558. this.event_PointerUp = null;
  559. this.event_PointerMove = null;
  560. this.event_PointerClick = null;
  561. this.event_disposed = null;
  562. }
  563. #endregion
  564. //-----------------------------------------------------------------------------------
  565. #region __Transform2D__
  566. public Rect Bounds2D
  567. {
  568. get
  569. {
  570. Rect rect = new Rect();
  571. rect.position = mTransform.localPosition;
  572. rect.size = mTransform.sizeDelta;
  573. rect.y = -rect.y;
  574. return rect;
  575. }
  576. set
  577. {
  578. this.Position2D = value.position;
  579. this.Size2D = value.size;
  580. }
  581. }
  582. public Vector2 Position2D
  583. {
  584. get
  585. {
  586. Vector2 pos = mTransform.localPosition;
  587. pos.y = -pos.y;
  588. return pos;
  589. }
  590. set
  591. {
  592. value.y = -value.y;
  593. this.mTransform.localPosition = value;
  594. }
  595. }
  596. public Vector2 Size2D
  597. {
  598. get { return mTransform.sizeDelta; }
  599. set
  600. {
  601. if (this.mTransform.sizeDelta != value)
  602. {
  603. this.mTransform.sizeDelta = value;
  604. OnSizeChanged(value);
  605. }
  606. }
  607. }
  608. public Vector2 ScreenToLocalPoint2D(PointerEventData e)
  609. {
  610. Vector2 local;
  611. RectTransformUtility.ScreenPointToLocalPointInRectangle(mTransform, e.position, e.pressEventCamera, out local);
  612. local.y = -local.y;
  613. return local;
  614. }
  615. /// <summary>
  616. /// 本地坐标转世界坐标系.
  617. /// </summary>
  618. /// <returns></returns>
  619. public Vector2 LocalToGlobal()
  620. {
  621. return this.UnityObject.transform.parent.TransformPoint(mTransform.localPosition);
  622. }
  623. /// <summary>
  624. /// 世界坐标转本地坐标系.是否为XMUI坐标系,若为false则会自动转换.
  625. /// </summary>
  626. /// <param name="wordpos"></param>
  627. /// <param name="isXMUGUIPos"></param>
  628. /// <returns></returns>
  629. public Vector2 GlobalToLocal(Vector2 wordpos, bool isXMUGUIPos = true)
  630. {
  631. Vector2 ret = this.UnityObject.transform.InverseTransformPoint(wordpos);
  632. if (isXMUGUIPos)
  633. {
  634. ret.y = -ret.y;
  635. }
  636. return ret;
  637. }
  638. #endregion
  639. //-----------------------------------------------------------------------------------
  640. #region __Attribute__
  641. private HashMap<string, object> mAttributes = new HashMap<string, object>();
  642. public bool IsAttribute(string key)
  643. {
  644. return mAttributes.ContainsKey(key);
  645. }
  646. public void SetAttribute(string key, object value)
  647. {
  648. mAttributes.Put(key, value);
  649. }
  650. public object GetAttribute(string key)
  651. {
  652. return mAttributes.Get(key);
  653. }
  654. public T GetAttributeAs<T>(string key)
  655. {
  656. object obj = mAttributes.Get(key);
  657. if (obj != null)
  658. {
  659. return (T)obj;
  660. }
  661. return default(T);
  662. }
  663. #endregion
  664. //-----------------------------------------------------------------------------------
  665. #region __BindingMonoBehaviour__
  666. public static DisplayNode AsDisplayNode(GameObject obj)
  667. {
  668. DisplayNodeBehaviour binding = obj.GetComponent<DisplayNodeBehaviour>();
  669. if (binding != null)
  670. {
  671. return binding.Binding;
  672. }
  673. return null;
  674. }
  675. public static DisplayNode AsDisplayNode(Component obj)
  676. {
  677. DisplayNodeBehaviour binding = obj.GetComponent<DisplayNodeBehaviour>();
  678. if (binding != null)
  679. {
  680. return binding.Binding;
  681. }
  682. return null;
  683. }
  684. private bool mIsInit = false;
  685. private List<DisplayNode> mUpdateNodes = new List<DisplayNode>();
  686. private List<DisplayNode> RefreshUpdateNodes()
  687. {
  688. int num_children = NumChildren;
  689. if (mUpdateNodes.Count != num_children)
  690. {
  691. CUtils.SetListSize(mUpdateNodes, num_children);
  692. }
  693. for (int i = num_children - 1; i >= 0; --i)
  694. {
  695. Transform child = mTransform.GetChild(i);
  696. if (mUpdateNodes[i] != null && mUpdateNodes[i].mTransform == child)
  697. {
  698. continue;
  699. }
  700. else
  701. {
  702. mUpdateNodes[i] = AsDisplayNode(child);
  703. }
  704. }
  705. return mUpdateNodes;
  706. }
  707. internal virtual void InternalUpdate()
  708. {
  709. if (this.mIsInit == false)
  710. {
  711. this.mIsInit = true;
  712. this.DoStart();
  713. mBinding.IsEnable = this.Enable;
  714. mBinding.IsEnableChildren = this.EnableChildren;
  715. mBinding.IsInteractive = this.IsInteractive;
  716. mBinding.Alpha = this.Alpha;
  717. mBinding.IsGray = this.IsGray;
  718. }
  719. else
  720. {
  721. if (this.Enable != mBinding.IsEnable)
  722. {
  723. this.Enable = mBinding.IsEnable;
  724. }
  725. if (this.EnableChildren != mBinding.IsEnableChildren)
  726. {
  727. this.EnableChildren = mBinding.IsEnableChildren;
  728. }
  729. if (this.IsInteractive != mBinding.IsInteractive)
  730. {
  731. this.IsInteractive = mBinding.IsInteractive;
  732. }
  733. if (this.Alpha != mBinding.Alpha)
  734. {
  735. this.Alpha = mBinding.Alpha;
  736. }
  737. if (this.IsGray != mBinding.IsGray)
  738. {
  739. this.IsGray = mBinding.IsGray;
  740. }
  741. }
  742. if (mIsDispose) return;
  743. if (mGameObject.activeSelf)
  744. {
  745. this.DoUpdate();
  746. List<DisplayNode> childs = this.RefreshUpdateNodes();
  747. DisplayNode child;
  748. for (int i = childs.Count - 1; i >= 0 && i < childs.Count; --i)
  749. {
  750. if ((child = childs[i]) != null)
  751. {
  752. child.InternalUpdate();
  753. }
  754. }
  755. this.DoEndUpdate();
  756. }
  757. }
  758. protected virtual IInteractiveComponent GenInteractive()
  759. {
  760. return mGameObject.AddComponent<DisplayNodeInteractive>();
  761. }
  762. protected virtual DisplayNodeBehaviour GenNodeBehavior()
  763. {
  764. return mGameObject.AddComponent<DisplayNodeBehaviour>();
  765. }
  766. protected virtual CanvasRenderer GenCanvasRenderer()
  767. {
  768. return mGameObject.AddComponent<CanvasRenderer>();
  769. }
  770. protected virtual GrayMaterialModifier GenGrayMaterialModifier(UnityEngine.UI.Graphic g)
  771. {
  772. if (g is Text)
  773. {
  774. return mGameObject.AddComponent<TextGrayMaterialModifier>();
  775. }
  776. else
  777. {
  778. return mGameObject.AddComponent<GrayMaterialModifier>();
  779. }
  780. }
  781. protected virtual void OnInteractiveChanged()
  782. {
  783. var g = mGameObject.GetComponent<UnityEngine.UI.Graphic>();
  784. if (g != null)
  785. {
  786. g.raycastTarget = IsInteractive && Enable;
  787. }
  788. //ForEachChilds<DisplayNode>((c) => { c.OnInteractiveChanged(); }, true);
  789. }
  790. /// <summary>
  791. /// 交互能力
  792. /// </summary>
  793. public IInteractiveComponent Selectable
  794. {
  795. get { return mSelectable; }
  796. }
  797. /// <summary>
  798. /// 是否被按下
  799. /// </summary>
  800. public bool IsPressed
  801. {
  802. get { return (mSelectable != null) ? mSelectable.IsPressDown : false; }
  803. }
  804. #endregion
  805. //-----------------------------------------------------------------------------------
  806. #region __Container__
  807. public bool ContainsChild(DisplayNode child)
  808. {
  809. while (child != null)
  810. {
  811. if (child == this)
  812. return true;
  813. else
  814. child = child.Parent;
  815. }
  816. return false;
  817. }
  818. public void AddChild(DisplayNode child)
  819. {
  820. this.AddChildAt(child, NumChildren);
  821. }
  822. public bool AddChildAt(DisplayNode child, int index)
  823. {
  824. if (child == null || index < 0)
  825. {
  826. return false;
  827. }
  828. if (child.Parent == this)
  829. {
  830. SetChildIndex(child, index);
  831. }
  832. else
  833. {
  834. child.RemoveFromParent();
  835. child.mParent = this;
  836. child.mTransform.SetParent(this.mTransform, false);
  837. child.mGameObject.SetActive(child.mVisible);
  838. this.SetChildIndex(child, index);
  839. this.OnChildAdded(child);
  840. if (event_ChildAdded != null) event_ChildAdded.Invoke(this, child);
  841. return true;
  842. }
  843. return false;
  844. }
  845. public bool RemoveChild(DisplayNode child, bool dispose = false)
  846. {
  847. if (child.mParent == this)
  848. {
  849. child.mParent = null;
  850. child.mTransform.SetParent(mDisabledNode, false);
  851. //child.mTransform.SetParent(this.mTransform.root.transform, false);
  852. //child.mGameObject.SetActive(false);
  853. this.OnChildRemoved(child);
  854. if (event_ChildRemoved != null) event_ChildRemoved.Invoke(this, child);
  855. if (dispose)
  856. {
  857. child.Dispose();
  858. }
  859. return true;
  860. }
  861. return false;
  862. }
  863. public DisplayNode RemoveChildByName(string name, bool dispose = false)
  864. {
  865. Transform child = mTransform.FindChild(name);
  866. if (child == null)
  867. {
  868. return null;
  869. }
  870. DisplayNode ret = AsDisplayNode(child.gameObject);
  871. if (ret != null)
  872. {
  873. ret.RemoveFromParent(dispose);
  874. }
  875. return ret;
  876. }
  877. public DisplayNode RemoveChildAt(int index, bool dispose = false)
  878. {
  879. DisplayNode ret = GetChildAt(index);
  880. if (ret != null)
  881. {
  882. ret.RemoveFromParent(dispose);
  883. }
  884. return ret;
  885. }
  886. public void RemoveChildren(int beginIndex = 0, int endIndex = -1, bool dispose = false)
  887. {
  888. if (endIndex < 0 || endIndex >= NumChildren)
  889. endIndex = NumChildren - 1;
  890. for (int i = beginIndex; i <= endIndex; ++i)
  891. RemoveChildAt(beginIndex, dispose);
  892. }
  893. public void RemoveAllChildren(bool dispose = true)
  894. {
  895. RemoveChildren(0, -1, dispose);
  896. }
  897. public void RemoveFromParent(bool dispose = false)
  898. {
  899. if (mParent != null)
  900. {
  901. mParent.RemoveChild(this, dispose);
  902. mParent = null;
  903. }
  904. }
  905. /** Returns a child object at a certain index. */
  906. public DisplayNode GetChildAt(int index)
  907. {
  908. Transform child = mTransform.GetChild(index);
  909. DisplayNode ret = AsDisplayNode(child.gameObject);
  910. return ret;
  911. }
  912. public IEnumerable<DisplayNode> AllChildren
  913. {
  914. get { return GetAllChild(); }
  915. }
  916. public IEnumerable<DisplayNode> GetAllChild()
  917. {
  918. List<DisplayNode> ret = new List<DisplayNode>(mTransform.childCount);
  919. GetAllChild(ret);
  920. return ret;
  921. }
  922. public void GetAllChild(List<DisplayNode> ret)
  923. {
  924. int count = mTransform.childCount;
  925. for (int i = 0; i < count; i++)
  926. {
  927. var child = mTransform.GetChild(i);
  928. DisplayNode node = AsDisplayNode(child.gameObject);
  929. if (node != null)
  930. {
  931. ret.Add(node);
  932. }
  933. }
  934. }
  935. public void SetParentIndex(int index)
  936. {
  937. mTransform.SetSiblingIndex(index);
  938. }
  939. public void SetChildIndex(DisplayNode child, int index)
  940. {
  941. child.mTransform.SetSiblingIndex(index);
  942. }
  943. public int GetChildIndex(DisplayNode child)
  944. {
  945. return child.mTransform.GetSiblingIndex();
  946. }
  947. #endregion
  948. //-----------------------------------------------------------------------------------
  949. #region __ChildsUtils__
  950. /// <summary>
  951. /// 根据名字搜寻子节点
  952. /// </summary>
  953. /// <typeparam name="T"></typeparam>
  954. /// <param name="name"></param>
  955. /// <param name="recursive"></param>
  956. /// <returns></returns>
  957. public T FindChildByName<T>(string name, bool recursive = true) where T : DisplayNode
  958. {
  959. return FindChildAs<T>((child) =>
  960. {
  961. return (name == child.Name);
  962. },
  963. recursive);
  964. }
  965. /// <summary>
  966. /// 搜寻子节点
  967. /// </summary>
  968. /// <typeparam name="T"></typeparam>
  969. /// <param name="select">选择器,返回True表示搜索到</param>
  970. /// <param name="recursive">是否递归</param>
  971. /// <returns></returns>
  972. public T FindChildAs<T>(Predicate<T> select, bool recursive = true) where T : DisplayNode
  973. {
  974. T child = null;
  975. T uicc = null;
  976. int i;
  977. int length = NumChildren;
  978. for (i = length - 1; i >= 0; --i)
  979. {
  980. child = this.GetChildAt(i) as T;
  981. if (child != null && select(child))
  982. {
  983. return child;
  984. }
  985. }
  986. if (recursive)
  987. {
  988. length = NumChildren;
  989. for (i = length - 1; i >= 0; --i)
  990. {
  991. DisplayNode sc = this.GetChildAt(i);
  992. if (sc != null)
  993. {
  994. uicc = sc.FindChildAs<T>(select, recursive);
  995. if (uicc != null)
  996. {
  997. return uicc;
  998. }
  999. }
  1000. }
  1001. }
  1002. return null;
  1003. }
  1004. public void ForEachChilds<T>(Action<T> action, bool recursive = true) where T : DisplayNode
  1005. {
  1006. T child = null;
  1007. int i;
  1008. int length = NumChildren;
  1009. for (i = length - 1; i >= 0; --i)
  1010. {
  1011. child = this.GetChildAt(i) as T;
  1012. if (child != null)
  1013. {
  1014. action(child);
  1015. }
  1016. }
  1017. if (recursive)
  1018. {
  1019. length = NumChildren;
  1020. for (i = length - 1; i >= 0; --i)
  1021. {
  1022. DisplayNode sc = this.GetChildAt(i);
  1023. if (sc != null)
  1024. {
  1025. sc.ForEachChilds<T>(action, recursive);
  1026. }
  1027. }
  1028. }
  1029. }
  1030. /// <summary>
  1031. /// 获得所有子节点总共占用尺寸
  1032. /// </summary>
  1033. /// <returns></returns>
  1034. public Vector2 GetChildsContentSize()
  1035. {
  1036. Vector2 ret = new Vector2(0, 0);
  1037. int count = mTransform.childCount;
  1038. for (int i = 0; i < mTransform.childCount; i++)
  1039. {
  1040. var child = mTransform.GetChild(i);
  1041. DisplayNode node = AsDisplayNode(child.gameObject);
  1042. if (node != null)
  1043. {
  1044. Vector2 size = node.Size2D;
  1045. ret.x += Math.Max(ret.x, size.x);
  1046. ret.y += Math.Max(ret.y, size.y);
  1047. }
  1048. }
  1049. return ret;
  1050. }
  1051. #endregion
  1052. //-----------------------------------------------------------------------------------
  1053. #region XMAction.
  1054. private readonly List<CellAction> mActionList = new List<CellAction>();
  1055. private List<IAction> mWaitList = new List<IAction>();
  1056. protected class CellAction
  1057. {
  1058. public string mActionType = null;
  1059. public IAction mAction = null;
  1060. public CellAction(IAction action)
  1061. {
  1062. mAction = action;
  1063. mActionType = action.GetActionType();
  1064. }
  1065. public void Dispose()
  1066. {
  1067. mActionType = null;
  1068. mAction = null;
  1069. }
  1070. }
  1071. protected virtual string ParseActionType(IAction action)
  1072. {
  1073. return action.GetActionType();
  1074. }
  1075. public virtual void AddAction(IAction action)
  1076. {
  1077. if (action == null)
  1078. {
  1079. throw new Exception("action can not be null");
  1080. }
  1081. PushWaitList(action);
  1082. }
  1083. private void PushWaitList(IAction action)
  1084. {
  1085. mWaitList.Add(action);
  1086. }
  1087. private void CheckAction(IAction action)
  1088. {
  1089. string actionType = ParseActionType(action);
  1090. IAction oldAction = null;
  1091. if (HasAction(actionType))
  1092. {
  1093. //mMapAction.TryGetValue(actionType, out oldAction);
  1094. oldAction = GetAction(actionType);
  1095. if (oldAction != null)
  1096. {
  1097. RemoveAction(oldAction, false);
  1098. }
  1099. }
  1100. StartAction(actionType, action);
  1101. }
  1102. private void UpdateWaitList()
  1103. {
  1104. for (int i = 0; i < mWaitList.Count; i++)
  1105. {
  1106. CheckAction(mWaitList[i]);
  1107. }
  1108. mWaitList.Clear();
  1109. }
  1110. protected virtual void StartAction(string actionType, IAction action)
  1111. {
  1112. CellAction ca = new CellAction(action);
  1113. //先放到队列中,下一帧执行.
  1114. mActionList.Add(ca);
  1115. action.onStart(this);
  1116. }
  1117. public virtual void RemoveAction(IAction action, bool sendCallBack)
  1118. {
  1119. RemoveAction(ParseActionType(action), sendCallBack);
  1120. }
  1121. public virtual void RemoveAction(string actionType, bool sendCallBack)
  1122. {
  1123. for (int i = 0; i < mActionList.Count; i++)
  1124. {
  1125. if (mActionList[i].mActionType == actionType)
  1126. {
  1127. mActionList[i].mAction.onStop(this, sendCallBack);
  1128. mActionList[i].Dispose();
  1129. mActionList.RemoveAt(i);
  1130. i--;
  1131. return;
  1132. }
  1133. }
  1134. }
  1135. public virtual bool HasAction(IAction action)
  1136. {
  1137. string name = ParseActionType(action);
  1138. return HasAction(name);
  1139. }
  1140. public virtual bool HasAction(string ActionType)
  1141. {
  1142. for (int i = 0; i < mActionList.Count; i++)
  1143. {
  1144. if (mActionList[i].mActionType == ActionType)
  1145. {
  1146. return true;
  1147. }
  1148. }
  1149. return false;
  1150. }
  1151. public virtual IAction GetAction(string actionType)
  1152. {
  1153. for (int i = 0; i < mActionList.Count; i++)
  1154. {
  1155. if (mActionList[i].mActionType == actionType)
  1156. {
  1157. return mActionList[i].mAction;
  1158. }
  1159. }
  1160. return null;
  1161. }
  1162. public virtual void RemoveAllAction(bool sendCallBack = false)
  1163. {
  1164. for (int i = mActionList.Count - 1; i >= 0; i--)
  1165. {
  1166. mActionList[i].mAction.onStop(this, sendCallBack);
  1167. mActionList[i].Dispose();
  1168. }
  1169. mActionList.Clear();
  1170. mWaitList.Clear();
  1171. }
  1172. public virtual void UpdateAction(float deltaTime)
  1173. {
  1174. UpdateWaitList();
  1175. if (mActionList != null && mActionList.Count > 0)
  1176. {
  1177. IAction act = null;
  1178. for (int i = 0; i < mActionList.Count; i++)
  1179. {
  1180. CellAction ca = mActionList[i];
  1181. act = ca.mAction;
  1182. act.onUpdate(this, deltaTime);
  1183. if (act.IsEnd())
  1184. {
  1185. act.onStop(this, true);
  1186. ca.Dispose();
  1187. mActionList.Remove(ca);
  1188. i--;
  1189. }
  1190. }
  1191. }
  1192. }
  1193. #endregion
  1194. //-----------------------------------------------------------------------------------
  1195. }
  1196. }