GComponent.cs 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using FairyGUI.Utils;
  5. #if FAIRYGUI_TOLUA
  6. using LuaInterface;
  7. #endif
  8. namespace FairyGUI
  9. {
  10. /// <summary>
  11. /// Component
  12. /// </summary>
  13. public class GComponent : GObject
  14. {
  15. /// <summary>
  16. /// Root container.
  17. /// </summary>
  18. public Container rootContainer { get; private set; }
  19. /// <summary>
  20. /// Content container. If the component is not clipped, then container==rootContainer.
  21. /// </summary>
  22. public Container container { get; protected set; }
  23. /// <summary>
  24. /// ScrollPane of the component. If the component is not scrollable, the value is null.
  25. /// </summary>
  26. public ScrollPane scrollPane { get; private set; }
  27. internal List<GObject> _children;
  28. internal List<Controller> _controllers;
  29. internal List<Transition> _transitions;
  30. internal bool _buildingDisplayList;
  31. protected Margin _margin;
  32. protected bool _trackBounds;
  33. protected bool _boundsChanged;
  34. protected ChildrenRenderOrder _childrenRenderOrder;
  35. protected int _apexIndex;
  36. internal Vector2 _alignOffset;
  37. Vector2 _clipSoftness;
  38. int _sortingChildCount;
  39. Action _buildDelegate;
  40. Controller _applyingController;
  41. EventListener _onDrop;
  42. public GComponent()
  43. {
  44. _children = new List<GObject>();
  45. _controllers = new List<Controller>();
  46. _transitions = new List<Transition>();
  47. _margin = new Margin();
  48. _buildDelegate = BuildNativeDisplayList;
  49. }
  50. override protected void CreateDisplayObject()
  51. {
  52. rootContainer = new Container("GComponent");
  53. rootContainer.gOwner = this;
  54. rootContainer.onUpdate += OnUpdate;
  55. container = rootContainer;
  56. displayObject = rootContainer;
  57. }
  58. override public void Dispose()
  59. {
  60. int cnt = _transitions.Count;
  61. for (int i = 0; i < cnt; ++i)
  62. {
  63. Transition trans = _transitions[i];
  64. trans.Dispose();
  65. }
  66. cnt = _controllers.Count;
  67. for (int i = 0; i < cnt; ++i)
  68. {
  69. Controller c = _controllers[i];
  70. c.Dispose();
  71. }
  72. if (scrollPane != null)
  73. scrollPane.Dispose();
  74. cnt = _children.Count;
  75. for (int i = cnt - 1; i >= 0; --i)
  76. {
  77. GObject obj = _children[i];
  78. obj.InternalSetParent(null); //Avoid GObject.RemoveParent call
  79. obj.Dispose();
  80. }
  81. //因list的dispost无法正常释放loader加载的资源,修改第三方代码
  82. base.Dispose(); //Dispose native tree first, avoid DisplayObject.RemoveFromParent call
  83. #if FAIRYGUI_TOLUA
  84. if (_peerTable != null)
  85. {
  86. _peerTable.Dispose();
  87. _peerTable = null;
  88. }
  89. #endif
  90. #if FAIRYGUI_PUERTS
  91. if (__onDispose != null)
  92. __onDispose();
  93. __onConstruct = null;
  94. __onDispose = null;
  95. #endif
  96. }
  97. /// <summary>
  98. /// Dispatched when an object was dragged and dropped to this component.
  99. /// </summary>
  100. public EventListener onDrop
  101. {
  102. get { return _onDrop ?? (_onDrop = new EventListener(this, "onDrop")); }
  103. }
  104. /// <summary>
  105. /// Draw call optimization switch.
  106. /// </summary>
  107. public bool fairyBatching
  108. {
  109. get { return rootContainer.fairyBatching; }
  110. set { rootContainer.fairyBatching = value; }
  111. }
  112. /// <summary>
  113. ///
  114. /// </summary>
  115. /// <param name="childChanged"></param>
  116. public void InvalidateBatchingState(bool childChanged)
  117. {
  118. if (childChanged)
  119. container.InvalidateBatchingState(childChanged);
  120. else
  121. rootContainer.InvalidateBatchingState();
  122. }
  123. /// <summary>
  124. /// If true, mouse/touch events cannot pass through the empty area of the component. Default is true.
  125. /// </summary>
  126. public bool opaque
  127. {
  128. get { return rootContainer.opaque; }
  129. set { rootContainer.opaque = value; }
  130. }
  131. /// <summary>
  132. ///
  133. /// </summary>
  134. /// <value></value>
  135. public Margin margin
  136. {
  137. get { return _margin; }
  138. set
  139. {
  140. _margin = value;
  141. if (rootContainer.clipRect != null && scrollPane == null) //如果scrollPane不为空,则HandleSizeChanged里面的处理会促使ScrollPane处理
  142. container.SetXY(_margin.left + _alignOffset.x, _margin.top + _alignOffset.y);
  143. HandleSizeChanged();
  144. }
  145. }
  146. /// <summary>
  147. ///
  148. /// </summary>
  149. public ChildrenRenderOrder childrenRenderOrder
  150. {
  151. get { return _childrenRenderOrder; }
  152. set
  153. {
  154. if (_childrenRenderOrder != value)
  155. {
  156. _childrenRenderOrder = value;
  157. BuildNativeDisplayList();
  158. }
  159. }
  160. }
  161. /// <summary>
  162. ///
  163. /// </summary>
  164. public int apexIndex
  165. {
  166. get { return _apexIndex; }
  167. set
  168. {
  169. if (_apexIndex != value)
  170. {
  171. _apexIndex = value;
  172. if (_childrenRenderOrder == ChildrenRenderOrder.Arch)
  173. BuildNativeDisplayList();
  174. }
  175. }
  176. }
  177. /// <summary>
  178. /// If true, children can be navigated by TAB from first to last, and repeat
  179. /// </summary>
  180. public bool tabStopChildren
  181. {
  182. get { return rootContainer.tabStopChildren; }
  183. set { rootContainer.tabStopChildren = value; }
  184. }
  185. /// <summary>
  186. /// Add a child to the component. It will be at the frontmost position.
  187. /// </summary>
  188. /// <param name="child">A child object</param>
  189. /// <returns>GObject</returns>
  190. public GObject AddChild(GObject child)
  191. {
  192. AddChildAt(child, _children.Count);
  193. return child;
  194. }
  195. /// <summary>
  196. /// Adds a child to the component at a certain index.
  197. /// </summary>
  198. /// <param name="child">A child object</param>
  199. /// <param name="index">Index</param>
  200. /// <returns>GObject</returns>
  201. virtual public GObject AddChildAt(GObject child, int index)
  202. {
  203. if (index >= 0 && index <= _children.Count)
  204. {
  205. if (child.parent == this)
  206. {
  207. SetChildIndex(child, index);
  208. }
  209. else
  210. {
  211. child.RemoveFromParent();
  212. child.InternalSetParent(this);
  213. int cnt = _children.Count;
  214. if (child.sortingOrder != 0)
  215. {
  216. _sortingChildCount++;
  217. index = GetInsertPosForSortingChild(child);
  218. }
  219. else if (_sortingChildCount > 0)
  220. {
  221. if (index > (cnt - _sortingChildCount))
  222. index = cnt - _sortingChildCount;
  223. }
  224. if (index == cnt)
  225. _children.Add(child);
  226. else
  227. _children.Insert(index, child);
  228. ChildStateChanged(child);
  229. SetBoundsChangedFlag();
  230. }
  231. return child;
  232. }
  233. else
  234. {
  235. throw new Exception("Invalid child index: " + index + ">" + _children.Count);
  236. }
  237. }
  238. int GetInsertPosForSortingChild(GObject target)
  239. {
  240. int cnt = _children.Count;
  241. int i;
  242. for (i = 0; i < cnt; i++)
  243. {
  244. GObject child = _children[i];
  245. if (child == target)
  246. continue;
  247. if (target.sortingOrder < child.sortingOrder)
  248. break;
  249. }
  250. return i;
  251. }
  252. /// <summary>
  253. /// Removes a child from the component. If the object is not a child, nothing happens.
  254. /// </summary>
  255. /// <param name="child">A child object</param>
  256. /// <returns>GObject</returns>
  257. public GObject RemoveChild(GObject child)
  258. {
  259. return RemoveChild(child, false);
  260. }
  261. /// <summary>
  262. /// Removes a child from the component. If the object is not a child, nothing happens.
  263. /// </summary>
  264. /// <param name="child">A child object</param>
  265. /// <param name="dispose">If true, the child will be disposed right away.</param>
  266. /// <returns>GObject</returns>
  267. public GObject RemoveChild(GObject child, bool dispose)
  268. {
  269. int childIndex = _children.IndexOf(child);
  270. if (childIndex != -1)
  271. {
  272. RemoveChildAt(childIndex, dispose);
  273. }
  274. return child;
  275. }
  276. /// <summary>
  277. /// Removes a child at a certain index. Children above the child will move down.
  278. /// </summary>
  279. /// <param name="index">Index</param>
  280. /// <returns>GObject</returns>
  281. public GObject RemoveChildAt(int index)
  282. {
  283. return RemoveChildAt(index, false);
  284. }
  285. /// <summary>
  286. /// Removes a child at a certain index. Children above the child will move down.
  287. /// </summary>
  288. /// <param name="index">Index</param>
  289. /// <param name="dispose">If true, the child will be disposed right away.</param>
  290. /// <returns>GObject</returns>
  291. virtual public GObject RemoveChildAt(int index, bool dispose)
  292. {
  293. if (index >= 0 && index < numChildren)
  294. {
  295. GObject child = _children[index];
  296. child.InternalSetParent(null);
  297. if (child.sortingOrder != 0)
  298. _sortingChildCount--;
  299. _children.RemoveAt(index);
  300. child.group = null;
  301. if (child.inContainer)
  302. {
  303. container.RemoveChild(child.displayObject);
  304. if (_childrenRenderOrder == ChildrenRenderOrder.Arch)
  305. {
  306. UpdateContext.OnBegin -= _buildDelegate;
  307. UpdateContext.OnBegin += _buildDelegate;
  308. }
  309. }
  310. if (dispose)
  311. child.Dispose();
  312. SetBoundsChangedFlag();
  313. return child;
  314. }
  315. else
  316. throw new Exception("Invalid child index: " + index + ">" + numChildren);
  317. }
  318. /// <summary>
  319. /// Remove all children.
  320. /// </summary>
  321. public void RemoveChildren()
  322. {
  323. RemoveChildren(0, -1, false);
  324. }
  325. /// <summary>
  326. /// Removes a range of children from the container (endIndex included).
  327. /// </summary>
  328. /// <param name="beginIndex">Begin index.</param>
  329. /// <param name="endIndex">End index.(Included).</param>
  330. /// <param name="dispose">If true, the child will be disposed right away.</param>
  331. public void RemoveChildren(int beginIndex, int endIndex, bool dispose)
  332. {
  333. if (endIndex < 0 || endIndex >= numChildren)
  334. endIndex = numChildren - 1;
  335. for (int i = beginIndex; i <= endIndex; ++i)
  336. RemoveChildAt(beginIndex, dispose);
  337. }
  338. /// <summary>
  339. /// Returns a child object at a certain index. If index out of bounds, exception raised.
  340. /// </summary>
  341. /// <param name="index">Index</param>
  342. /// <returns>A child object.</returns>
  343. public GObject GetChildAt(int index)
  344. {
  345. if (index >= 0 && index < numChildren)
  346. return _children[index];
  347. else
  348. throw new Exception("Invalid child index: " + index + ">" + numChildren);
  349. }
  350. /// <summary>
  351. /// Returns a child object with a certain name.
  352. /// </summary>
  353. /// <param name="name">Name</param>
  354. /// <returns>A child object. Null if not found.</returns>
  355. public GObject GetChild(string name)
  356. {
  357. int cnt = _children.Count;
  358. for (int i = 0; i < cnt; ++i)
  359. {
  360. if (_children[i].name == name)
  361. return _children[i];
  362. }
  363. return null;
  364. }
  365. public GObject GetChildByPath(string path)
  366. {
  367. string[] arr = path.Split('.');
  368. int cnt = arr.Length;
  369. GComponent gcom = this;
  370. GObject obj = null;
  371. for (int i = 0; i < cnt; ++i)
  372. {
  373. obj = gcom.GetChild(arr[i]);
  374. if (obj == null)
  375. break;
  376. if (i != cnt - 1)
  377. {
  378. if (!(obj is GComponent))
  379. {
  380. obj = null;
  381. break;
  382. }
  383. else
  384. gcom = (GComponent)obj;
  385. }
  386. }
  387. return obj;
  388. }
  389. /// <summary>
  390. /// Returns a visible child object with a certain name.
  391. /// </summary>
  392. /// <param name="name">Name</param>
  393. /// <returns>A child object. Null if not found.</returns>
  394. public GObject GetVisibleChild(string name)
  395. {
  396. int cnt = _children.Count;
  397. for (int i = 0; i < cnt; ++i)
  398. {
  399. GObject child = _children[i];
  400. if (child.internalVisible && child.internalVisible2 && child.name == name)
  401. return child;
  402. }
  403. return null;
  404. }
  405. /// <summary>
  406. /// Returns a child object belong to a group with a certain name.
  407. /// </summary>
  408. /// <param name="group">A group object</param>
  409. /// <param name="name">Name</param>
  410. /// <returns>A child object. Null if not found.</returns>
  411. public GObject GetChildInGroup(GGroup group, string name)
  412. {
  413. int cnt = _children.Count;
  414. for (int i = 0; i < cnt; ++i)
  415. {
  416. GObject child = _children[i];
  417. if (child.group == group && child.name == name)
  418. return child;
  419. }
  420. return null;
  421. }
  422. internal GObject GetChildById(string id)
  423. {
  424. int cnt = _children.Count;
  425. for (int i = 0; i < cnt; ++i)
  426. {
  427. if (_children[i].id == id)
  428. return _children[i];
  429. }
  430. return null;
  431. }
  432. /// <summary>
  433. /// Returns a copy of all children with an array.
  434. /// </summary>
  435. /// <returns>An array contains all children</returns>
  436. public GObject[] GetChildren()
  437. {
  438. return _children.ToArray();
  439. }
  440. /// <summary>
  441. /// Returns the index of a child within the container, or "-1" if it is not found.
  442. /// </summary>
  443. /// <param name="child">A child object</param>
  444. /// <returns>Index of the child. -1 If not found.</returns>
  445. public int GetChildIndex(GObject child)
  446. {
  447. return _children.IndexOf(child);
  448. }
  449. /// <summary>
  450. /// Moves a child to a certain index. Children at and after the replaced position move up.
  451. /// </summary>
  452. /// <param name="child">A Child</param>
  453. /// <param name="index">Index</param>
  454. public void SetChildIndex(GObject child, int index)
  455. {
  456. int oldIndex = _children.IndexOf(child);
  457. if (oldIndex == -1)
  458. throw new ArgumentException("Not a child of this container");
  459. if (child.sortingOrder != 0) //no effect
  460. return;
  461. if (_sortingChildCount > 0)
  462. {
  463. int cnt = _children.Count;
  464. if (index > (cnt - _sortingChildCount - 1))
  465. index = cnt - _sortingChildCount - 1;
  466. }
  467. _SetChildIndex(child, oldIndex, index);
  468. }
  469. /// <summary>
  470. /// Moves a child to a certain position which is in front of the child previously at given index.
  471. /// 与SetChildIndex不同的是,如果child原来在index的前面,那么child插入的位置是index-1,即保证排在原来占据index的对象的前面。
  472. /// </summary>
  473. /// <param name="child"></param>
  474. /// <param name="index"></param>
  475. public int SetChildIndexBefore(GObject child, int index)
  476. {
  477. int oldIndex = _children.IndexOf(child);
  478. if (oldIndex == -1)
  479. throw new ArgumentException("Not a child of this container");
  480. if (child.sortingOrder != 0) //no effect
  481. return oldIndex;
  482. int cnt = _children.Count;
  483. if (_sortingChildCount > 0)
  484. {
  485. if (index > (cnt - _sortingChildCount - 1))
  486. index = cnt - _sortingChildCount - 1;
  487. }
  488. if (oldIndex < index)
  489. return _SetChildIndex(child, oldIndex, index - 1);
  490. else
  491. return _SetChildIndex(child, oldIndex, index);
  492. }
  493. int _SetChildIndex(GObject child, int oldIndex, int index)
  494. {
  495. int cnt = _children.Count;
  496. if (index > cnt)
  497. index = cnt;
  498. if (oldIndex == index)
  499. return oldIndex;
  500. _children.RemoveAt(oldIndex);
  501. if (index >= cnt)
  502. _children.Add(child);
  503. else
  504. _children.Insert(index, child);
  505. if (child.inContainer)
  506. {
  507. int displayIndex = 0;
  508. if (_childrenRenderOrder == ChildrenRenderOrder.Ascent)
  509. {
  510. for (int i = 0; i < index; i++)
  511. {
  512. GObject g = _children[i];
  513. if (g.inContainer)
  514. displayIndex++;
  515. }
  516. container.SetChildIndex(child.displayObject, displayIndex);
  517. }
  518. else if (_childrenRenderOrder == ChildrenRenderOrder.Descent)
  519. {
  520. for (int i = cnt - 1; i > index; i--)
  521. {
  522. GObject g = _children[i];
  523. if (g.inContainer)
  524. displayIndex++;
  525. }
  526. container.SetChildIndex(child.displayObject, displayIndex);
  527. }
  528. else
  529. {
  530. UpdateContext.OnBegin -= _buildDelegate;
  531. UpdateContext.OnBegin += _buildDelegate;
  532. }
  533. SetBoundsChangedFlag();
  534. }
  535. return index;
  536. }
  537. /// <summary>
  538. /// Swaps the indexes of two children.
  539. /// </summary>
  540. /// <param name="child1">A child object</param>
  541. /// <param name="child2">A child object</param>
  542. public void SwapChildren(GObject child1, GObject child2)
  543. {
  544. int index1 = _children.IndexOf(child1);
  545. int index2 = _children.IndexOf(child2);
  546. if (index1 == -1 || index2 == -1)
  547. throw new Exception("Not a child of this container");
  548. SwapChildrenAt(index1, index2);
  549. }
  550. /// <summary>
  551. /// Swaps the indexes of two children.
  552. /// </summary>
  553. /// <param name="index1">index of first child</param>
  554. /// <param name="index2">index of second child</param>
  555. public void SwapChildrenAt(int index1, int index2)
  556. {
  557. GObject child1 = _children[index1];
  558. GObject child2 = _children[index2];
  559. SetChildIndex(child1, index2);
  560. SetChildIndex(child2, index1);
  561. }
  562. /// <summary>
  563. /// The number of children of this component.
  564. /// </summary>
  565. public int numChildren
  566. {
  567. get { return _children.Count; }
  568. }
  569. /// <summary>
  570. ///
  571. /// </summary>
  572. /// <param name="obj"></param>
  573. /// <returns></returns>
  574. public bool IsAncestorOf(GObject obj)
  575. {
  576. if (obj == null)
  577. return false;
  578. GComponent p = obj.parent;
  579. while (p != null)
  580. {
  581. if (p == this)
  582. return true;
  583. p = p.parent;
  584. }
  585. return false;
  586. }
  587. /// <summary>
  588. ///
  589. /// </summary>
  590. /// <param name="objs"></param>
  591. public void ChangeChildrenOrder(IList<GObject> objs)
  592. {
  593. int cnt = objs.Count;
  594. for (int i = 0; i < cnt; i++)
  595. {
  596. GObject obj = objs[i];
  597. if (obj.parent != this)
  598. throw new Exception("Not a child of this container");
  599. _children[i] = obj;
  600. }
  601. BuildNativeDisplayList();
  602. SetBoundsChangedFlag();
  603. }
  604. /// <summary>
  605. /// Adds a controller to the container.
  606. /// </summary>
  607. /// <param name="controller">Controller object</param>
  608. public void AddController(Controller controller)
  609. {
  610. _controllers.Add(controller);
  611. controller.parent = this;
  612. ApplyController(controller);
  613. }
  614. /// <summary>
  615. /// Returns a controller object at a certain index.
  616. /// </summary>
  617. /// <param name="index">Index</param>
  618. /// <returns>Controller object.</returns>
  619. public Controller GetControllerAt(int index)
  620. {
  621. return _controllers[index];
  622. }
  623. /// <summary>
  624. /// Returns a controller object with a certain name.
  625. /// </summary>
  626. /// <param name="name">Name</param>
  627. /// <returns>Controller object. Null if not found.</returns>
  628. public Controller GetController(string name)
  629. {
  630. int cnt = _controllers.Count;
  631. for (int i = 0; i < cnt; ++i)
  632. {
  633. Controller c = _controllers[i];
  634. if (c.name == name)
  635. return c;
  636. }
  637. return null;
  638. }
  639. /// <summary>
  640. /// Removes a controller from the container.
  641. /// </summary>
  642. /// <param name="c">Controller object.</param>
  643. public void RemoveController(Controller c)
  644. {
  645. int index = _controllers.IndexOf(c);
  646. if (index == -1)
  647. throw new Exception("controller not exists: " + c.name);
  648. c.parent = null;
  649. _controllers.RemoveAt(index);
  650. int cnt = _children.Count;
  651. for (int i = 0; i < cnt; ++i)
  652. {
  653. GObject child = _children[i];
  654. child.HandleControllerChanged(c);
  655. }
  656. }
  657. /// <summary>
  658. /// Returns controller list.
  659. /// </summary>
  660. /// <returns>Controller list</returns>
  661. public List<Controller> Controllers
  662. {
  663. get { return _controllers; }
  664. }
  665. /// <summary>
  666. /// Returns a transition object at a certain index.
  667. /// </summary>
  668. /// <param name="index">Index</param>
  669. /// <returns>transition object.</returns>
  670. public Transition GetTransitionAt(int index)
  671. {
  672. return _transitions[index];
  673. }
  674. /// <summary>
  675. /// Returns a transition object at a certain name.
  676. /// </summary>
  677. /// <param name="name">Name</param>
  678. /// <returns>Transition Object</returns>
  679. public Transition GetTransition(string name)
  680. {
  681. int cnt = _transitions.Count;
  682. for (int i = 0; i < cnt; ++i)
  683. {
  684. Transition trans = _transitions[i];
  685. if (trans.name == name)
  686. return trans;
  687. }
  688. return null;
  689. }
  690. internal void ChildStateChanged(GObject child)
  691. {
  692. if (_buildingDisplayList)
  693. return;
  694. int cnt = _children.Count;
  695. if (child is GGroup)
  696. {
  697. for (int i = 0; i < cnt; ++i)
  698. {
  699. GObject g = _children[i];
  700. if (g.group == child)
  701. ChildStateChanged(g);
  702. }
  703. return;
  704. }
  705. if (child.displayObject == null)
  706. return;
  707. if (child.internalVisible)
  708. {
  709. if (child.displayObject.parent == null)
  710. {
  711. if (_childrenRenderOrder == ChildrenRenderOrder.Ascent)
  712. {
  713. int index = 0;
  714. for (int i = 0; i < cnt; i++)
  715. {
  716. GObject g = _children[i];
  717. if (g == child)
  718. break;
  719. if (g.displayObject != null && g.displayObject.parent != null)
  720. index++;
  721. }
  722. container.AddChildAt(child.displayObject, index);
  723. }
  724. else if (_childrenRenderOrder == ChildrenRenderOrder.Descent)
  725. {
  726. int index = 0;
  727. for (int i = cnt - 1; i >= 0; i--)
  728. {
  729. GObject g = _children[i];
  730. if (g == child)
  731. break;
  732. if (g.displayObject != null && g.displayObject.parent != null)
  733. index++;
  734. }
  735. container.AddChildAt(child.displayObject, index);
  736. }
  737. else
  738. {
  739. container.AddChild(child.displayObject);
  740. UpdateContext.OnBegin -= _buildDelegate;
  741. UpdateContext.OnBegin += _buildDelegate;
  742. }
  743. }
  744. }
  745. else
  746. {
  747. if (child.displayObject.parent != null)
  748. {
  749. container.RemoveChild(child.displayObject);
  750. if (_childrenRenderOrder == ChildrenRenderOrder.Arch)
  751. {
  752. UpdateContext.OnBegin -= _buildDelegate;
  753. UpdateContext.OnBegin += _buildDelegate;
  754. }
  755. }
  756. }
  757. }
  758. void BuildNativeDisplayList()
  759. {
  760. if (displayObject == null || displayObject.isDisposed)
  761. return;
  762. int cnt = _children.Count;
  763. if (cnt == 0)
  764. return;
  765. switch (_childrenRenderOrder)
  766. {
  767. case ChildrenRenderOrder.Ascent:
  768. {
  769. for (int i = 0; i < cnt; i++)
  770. {
  771. GObject child = _children[i];
  772. if (child.displayObject != null && child.internalVisible)
  773. container.AddChild(child.displayObject);
  774. }
  775. }
  776. break;
  777. case ChildrenRenderOrder.Descent:
  778. {
  779. for (int i = cnt - 1; i >= 0; i--)
  780. {
  781. GObject child = _children[i];
  782. if (child.displayObject != null && child.internalVisible)
  783. container.AddChild(child.displayObject);
  784. }
  785. }
  786. break;
  787. case ChildrenRenderOrder.Arch:
  788. {
  789. int apex = Mathf.Clamp(_apexIndex, 0, cnt);
  790. for (int i = 0; i < apex; i++)
  791. {
  792. GObject child = _children[i];
  793. if (child.displayObject != null && child.internalVisible)
  794. container.AddChild(child.displayObject);
  795. }
  796. for (int i = cnt - 1; i >= apex; i--)
  797. {
  798. GObject child = _children[i];
  799. if (child.displayObject != null && child.internalVisible)
  800. container.AddChild(child.displayObject);
  801. }
  802. }
  803. break;
  804. }
  805. }
  806. internal void ApplyController(Controller c)
  807. {
  808. _applyingController = c;
  809. int cnt = _children.Count;
  810. for (int i = 0; i < cnt; ++i)
  811. {
  812. GObject child = _children[i];
  813. child.HandleControllerChanged(c);
  814. }
  815. _applyingController = null;
  816. c.RunActions();
  817. }
  818. void ApplyAllControllers()
  819. {
  820. int cnt = _controllers.Count;
  821. for (int i = 0; i < cnt; ++i)
  822. {
  823. Controller controller = _controllers[i];
  824. ApplyController(controller);
  825. }
  826. }
  827. internal void AdjustRadioGroupDepth(GObject obj, Controller c)
  828. {
  829. int cnt = _children.Count;
  830. int i;
  831. GObject child;
  832. int myIndex = -1, maxIndex = -1;
  833. for (i = 0; i < cnt; i++)
  834. {
  835. child = _children[i];
  836. if (child == obj)
  837. {
  838. myIndex = i;
  839. }
  840. else if ((child is GButton)
  841. && ((GButton)child).relatedController == c)
  842. {
  843. if (i > maxIndex)
  844. maxIndex = i;
  845. }
  846. }
  847. if (myIndex < maxIndex)
  848. {
  849. if (_applyingController != null)
  850. _children[maxIndex].HandleControllerChanged(_applyingController);
  851. this.SwapChildrenAt(myIndex, maxIndex);
  852. }
  853. }
  854. /// <summary>
  855. /// If clipping softness is set, clipped containers will have soft border effect.
  856. /// </summary>
  857. public Vector2 clipSoftness
  858. {
  859. get { return _clipSoftness; }
  860. set
  861. {
  862. _clipSoftness = value;
  863. if (scrollPane != null)
  864. scrollPane.UpdateClipSoft();
  865. else if (_clipSoftness.x > 0 || _clipSoftness.y > 0)
  866. rootContainer.clipSoftness = new Vector4(value.x, value.y, value.x, value.y);
  867. else
  868. rootContainer.clipSoftness = null;
  869. }
  870. }
  871. /// <summary>
  872. /// The mask of the component.
  873. /// </summary>
  874. public DisplayObject mask
  875. {
  876. get { return container.mask; }
  877. set
  878. {
  879. container.mask = value;
  880. if (value != null && value.parent != container)
  881. container.AddChild(value);
  882. }
  883. }
  884. /// <summary>
  885. ///
  886. /// </summary>
  887. public bool reversedMask
  888. {
  889. get { return container.reversedMask; }
  890. set { container.reversedMask = value; }
  891. }
  892. /// <summary>
  893. ///
  894. /// </summary>
  895. public string baseUserData
  896. {
  897. get
  898. {
  899. ByteBuffer buffer = packageItem.rawData;
  900. buffer.Seek(0, 4);
  901. return buffer.ReadS();
  902. }
  903. }
  904. /// <summary>
  905. /// Test if a child is in view.
  906. /// </summary>
  907. /// <param name="child">A child object</param>
  908. /// <returns>True if in view</returns>
  909. public bool IsChildInView(GObject child)
  910. {
  911. if (scrollPane != null)
  912. {
  913. return scrollPane.IsChildInView(child);
  914. }
  915. else if (rootContainer.clipRect != null)
  916. {
  917. return child.x + child.width >= 0 && child.x <= this.width
  918. && child.y + child.height >= 0 && child.y <= this.height;
  919. }
  920. else
  921. return true;
  922. }
  923. virtual public int GetFirstChildInView()
  924. {
  925. int cnt = _children.Count;
  926. for (int i = 0; i < cnt; ++i)
  927. {
  928. GObject child = _children[i];
  929. if (IsChildInView(child))
  930. return i;
  931. }
  932. return -1;
  933. }
  934. protected void SetupScroll(ByteBuffer buffer)
  935. {
  936. if (rootContainer == container)
  937. {
  938. container = new Container();
  939. rootContainer.AddChild(container);
  940. }
  941. scrollPane = new ScrollPane(this);
  942. scrollPane.Setup(buffer);
  943. }
  944. protected void SetupOverflow(OverflowType overflow)
  945. {
  946. if (overflow == OverflowType.Hidden)
  947. {
  948. if (rootContainer == container)
  949. {
  950. container = new Container();
  951. rootContainer.AddChild(container);
  952. }
  953. UpdateClipRect();
  954. container.SetXY(_margin.left, _margin.top);
  955. }
  956. else if (_margin.left != 0 || _margin.top != 0)
  957. {
  958. if (rootContainer == container)
  959. {
  960. container = new Container();
  961. rootContainer.AddChild(container);
  962. }
  963. container.SetXY(_margin.left, _margin.top);
  964. }
  965. }
  966. void UpdateClipRect()
  967. {
  968. if (scrollPane == null)
  969. {
  970. float w = this.width - (_margin.left + _margin.right);
  971. float h = this.height - (_margin.top + _margin.bottom);
  972. rootContainer.clipRect = new Rect(_margin.left, _margin.top, w, h);
  973. }
  974. else
  975. rootContainer.clipRect = new Rect(0, 0, this.width, this.height);
  976. }
  977. override protected void HandleSizeChanged()
  978. {
  979. base.HandleSizeChanged();
  980. if (scrollPane != null)
  981. scrollPane.OnOwnerSizeChanged();
  982. if (rootContainer.clipRect != null)
  983. UpdateClipRect();
  984. }
  985. override protected void HandleGrayedChanged()
  986. {
  987. Controller cc = GetController("grayed");
  988. if (cc != null)
  989. cc.selectedIndex = this.grayed ? 1 : 0;
  990. else
  991. base.HandleGrayedChanged();
  992. }
  993. override public void HandleControllerChanged(Controller c)
  994. {
  995. base.HandleControllerChanged(c);
  996. if (scrollPane != null)
  997. scrollPane.HandleControllerChanged(c);
  998. }
  999. /// <summary>
  1000. /// Notify the component the bounds should recaculate.
  1001. /// </summary>
  1002. public void SetBoundsChangedFlag()
  1003. {
  1004. if (scrollPane == null && !_trackBounds)
  1005. return;
  1006. _boundsChanged = true;
  1007. }
  1008. /// <summary>
  1009. /// Make sure the bounds of the component is correct.
  1010. /// Bounds of the component is not updated on every changed. For example, you add a new child to the list, children in the list will be rearranged in next frame.
  1011. /// If you want to access the correct child position immediatelly, call this function first.
  1012. /// </summary>
  1013. public void EnsureBoundsCorrect()
  1014. {
  1015. if (_boundsChanged)
  1016. UpdateBounds();
  1017. }
  1018. virtual protected void UpdateBounds()
  1019. {
  1020. float ax, ay, aw, ah;
  1021. if (_children.Count > 0)
  1022. {
  1023. ax = int.MaxValue;
  1024. ay = int.MaxValue;
  1025. float ar = int.MinValue, ab = int.MinValue;
  1026. float tmp;
  1027. int cnt = _children.Count;
  1028. for (int i = 0; i < cnt; ++i)
  1029. {
  1030. GObject child = _children[i];
  1031. tmp = child.x;
  1032. if (tmp < ax)
  1033. ax = tmp;
  1034. tmp = child.y;
  1035. if (tmp < ay)
  1036. ay = tmp;
  1037. tmp = child.x + child.actualWidth;
  1038. if (tmp > ar)
  1039. ar = tmp;
  1040. tmp = child.y + child.actualHeight;
  1041. if (tmp > ab)
  1042. ab = tmp;
  1043. }
  1044. aw = ar - ax;
  1045. ah = ab - ay;
  1046. }
  1047. else
  1048. {
  1049. ax = 0;
  1050. ay = 0;
  1051. aw = 0;
  1052. ah = 0;
  1053. }
  1054. SetBounds(ax, ay, aw, ah);
  1055. }
  1056. protected void SetBounds(float ax, float ay, float aw, float ah)
  1057. {
  1058. _boundsChanged = false;
  1059. if (scrollPane != null)
  1060. scrollPane.SetContentSize(Mathf.RoundToInt(ax + aw), Mathf.RoundToInt(ay + ah));
  1061. }
  1062. /// <summary>
  1063. /// Viwe port width of the container.
  1064. /// </summary>
  1065. public float viewWidth
  1066. {
  1067. get
  1068. {
  1069. if (scrollPane != null)
  1070. return scrollPane.viewWidth;
  1071. else
  1072. return this.width - _margin.left - _margin.right;
  1073. }
  1074. set
  1075. {
  1076. if (scrollPane != null)
  1077. scrollPane.viewWidth = value;
  1078. else
  1079. this.width = value + _margin.left + _margin.right;
  1080. }
  1081. }
  1082. /// <summary>
  1083. /// View port height of the container.
  1084. /// </summary>
  1085. public float viewHeight
  1086. {
  1087. get
  1088. {
  1089. if (scrollPane != null)
  1090. return scrollPane.viewHeight;
  1091. else
  1092. return this.height - _margin.top - _margin.bottom;
  1093. }
  1094. set
  1095. {
  1096. if (scrollPane != null)
  1097. scrollPane.viewHeight = value;
  1098. else
  1099. this.height = value + _margin.top + _margin.bottom;
  1100. }
  1101. }
  1102. virtual protected internal void GetSnappingPosition(ref float xValue, ref float yValue)
  1103. {
  1104. int cnt = _children.Count;
  1105. if (cnt == 0)
  1106. return;
  1107. EnsureBoundsCorrect();
  1108. GObject obj = null;
  1109. int i = 0;
  1110. if (yValue != 0)
  1111. {
  1112. for (; i < cnt; i++)
  1113. {
  1114. obj = _children[i];
  1115. if (yValue < obj.y)
  1116. {
  1117. if (i == 0)
  1118. {
  1119. yValue = 0;
  1120. break;
  1121. }
  1122. else
  1123. {
  1124. GObject prev = _children[i - 1];
  1125. if (yValue < prev.y + prev.height / 2) //top half part
  1126. yValue = prev.y;
  1127. else //bottom half part
  1128. yValue = obj.y;
  1129. break;
  1130. }
  1131. }
  1132. }
  1133. if (i == cnt)
  1134. yValue = obj.y;
  1135. }
  1136. if (xValue != 0)
  1137. {
  1138. if (i > 0)
  1139. i--;
  1140. for (; i < cnt; i++)
  1141. {
  1142. obj = _children[i];
  1143. if (xValue < obj.x)
  1144. {
  1145. if (i == 0)
  1146. {
  1147. xValue = 0;
  1148. break;
  1149. }
  1150. else
  1151. {
  1152. GObject prev = _children[i - 1];
  1153. if (xValue < prev.x + prev.width / 2) // top half part
  1154. xValue = prev.x;
  1155. else//bottom half part
  1156. xValue = obj.x;
  1157. break;
  1158. }
  1159. }
  1160. }
  1161. if (i == cnt)
  1162. xValue = obj.x;
  1163. }
  1164. }
  1165. internal void ChildSortingOrderChanged(GObject child, int oldValue, int newValue)
  1166. {
  1167. if (newValue == 0)
  1168. {
  1169. _sortingChildCount--;
  1170. SetChildIndex(child, _children.Count);
  1171. }
  1172. else
  1173. {
  1174. if (oldValue == 0)
  1175. _sortingChildCount++;
  1176. int oldIndex = _children.IndexOf(child);
  1177. int index = GetInsertPosForSortingChild(child);
  1178. if (oldIndex < index)
  1179. _SetChildIndex(child, oldIndex, index - 1);
  1180. else
  1181. _SetChildIndex(child, oldIndex, index);
  1182. }
  1183. }
  1184. /// <summary>
  1185. /// 每帧调用的一个回调。如果你要override,请记住以下两点:
  1186. /// 1、记得调用base.onUpdate;
  1187. /// 2、不要在方法里进行任何会更改显示列表的操作,例如AddChild、RemoveChild、visible等。
  1188. /// </summary>
  1189. virtual protected void OnUpdate()
  1190. {
  1191. if (_boundsChanged)
  1192. UpdateBounds();
  1193. }
  1194. override public void ConstructFromResource()
  1195. {
  1196. ConstructFromResource(null, 0);
  1197. }
  1198. internal void ConstructFromResource(List<GObject> objectPool, int poolIndex)
  1199. {
  1200. this.gameObjectName = packageItem.name;
  1201. PackageItem contentItem = packageItem.getBranch();
  1202. if (!contentItem.translated)
  1203. {
  1204. contentItem.translated = true;
  1205. TranslationHelper.TranslateComponent(contentItem);
  1206. }
  1207. ByteBuffer buffer = contentItem.rawData;
  1208. buffer.Seek(0, 0);
  1209. underConstruct = true;
  1210. sourceWidth = buffer.ReadInt();
  1211. sourceHeight = buffer.ReadInt();
  1212. initWidth = sourceWidth;
  1213. initHeight = sourceHeight;
  1214. SetSize(sourceWidth, sourceHeight);
  1215. if (buffer.ReadBool())
  1216. {
  1217. minWidth = buffer.ReadInt();
  1218. maxWidth = buffer.ReadInt();
  1219. minHeight = buffer.ReadInt();
  1220. maxHeight = buffer.ReadInt();
  1221. }
  1222. if (buffer.ReadBool())
  1223. {
  1224. float f1 = buffer.ReadFloat();
  1225. float f2 = buffer.ReadFloat();
  1226. SetPivot(f1, f2, buffer.ReadBool());
  1227. }
  1228. if (buffer.ReadBool())
  1229. {
  1230. _margin.top = buffer.ReadInt();
  1231. _margin.bottom = buffer.ReadInt();
  1232. _margin.left = buffer.ReadInt();
  1233. _margin.right = buffer.ReadInt();
  1234. }
  1235. OverflowType overflow = (OverflowType)buffer.ReadByte();
  1236. if (overflow == OverflowType.Scroll)
  1237. {
  1238. int savedPos = buffer.position;
  1239. buffer.Seek(0, 7);
  1240. SetupScroll(buffer);
  1241. buffer.position = savedPos;
  1242. }
  1243. else
  1244. SetupOverflow(overflow);
  1245. if (buffer.ReadBool())
  1246. {
  1247. int i1 = buffer.ReadInt();
  1248. int i2 = buffer.ReadInt();
  1249. this.clipSoftness = new Vector2(i1, i2);
  1250. }
  1251. _buildingDisplayList = true;
  1252. buffer.Seek(0, 1);
  1253. int controllerCount = buffer.ReadShort();
  1254. for (int i = 0; i < controllerCount; i++)
  1255. {
  1256. int nextPos = buffer.ReadShort();
  1257. nextPos += buffer.position;
  1258. Controller controller = new Controller();
  1259. _controllers.Add(controller);
  1260. controller.parent = this;
  1261. controller.Setup(buffer);
  1262. buffer.position = nextPos;
  1263. }
  1264. buffer.Seek(0, 2);
  1265. GObject child;
  1266. int childCount = buffer.ReadShort();
  1267. for (int i = 0; i < childCount; i++)
  1268. {
  1269. int dataLen = buffer.ReadShort();
  1270. int curPos = buffer.position;
  1271. if (objectPool != null)
  1272. child = objectPool[poolIndex + i];
  1273. else
  1274. {
  1275. buffer.Seek(curPos, 0);
  1276. ObjectType type = (ObjectType)buffer.ReadByte();
  1277. string src = buffer.ReadS();
  1278. string pkgId = buffer.ReadS();
  1279. PackageItem pi = null;
  1280. if (src != null)
  1281. {
  1282. UIPackage pkg;
  1283. if (pkgId != null)
  1284. pkg = UIPackage.GetById(pkgId);
  1285. else
  1286. pkg = contentItem.owner;
  1287. pi = pkg != null ? pkg.GetItem(src) : null;
  1288. }
  1289. if (pi != null)
  1290. {
  1291. child = UIObjectFactory.NewObject(pi);
  1292. child.ConstructFromResource();
  1293. }
  1294. else
  1295. child = UIObjectFactory.NewObject(type);
  1296. }
  1297. child.underConstruct = true;
  1298. child.Setup_BeforeAdd(buffer, curPos);
  1299. child.InternalSetParent(this);
  1300. _children.Add(child);
  1301. buffer.position = curPos + dataLen;
  1302. }
  1303. buffer.Seek(0, 3);
  1304. this.relations.Setup(buffer, true);
  1305. buffer.Seek(0, 2);
  1306. buffer.Skip(2);
  1307. for (int i = 0; i < childCount; i++)
  1308. {
  1309. int nextPos = buffer.ReadShort();
  1310. nextPos += buffer.position;
  1311. buffer.Seek(buffer.position, 3);
  1312. _children[i].relations.Setup(buffer, false);
  1313. buffer.position = nextPos;
  1314. }
  1315. buffer.Seek(0, 2);
  1316. buffer.Skip(2);
  1317. for (int i = 0; i < childCount; i++)
  1318. {
  1319. int nextPos = buffer.ReadShort();
  1320. nextPos += buffer.position;
  1321. child = _children[i];
  1322. child.Setup_AfterAdd(buffer, buffer.position);
  1323. child.underConstruct = false;
  1324. if (child.displayObject != null)
  1325. child.displayObject.cachedTransform.SetParent(this.displayObject.cachedTransform, false);
  1326. buffer.position = nextPos;
  1327. }
  1328. buffer.Seek(0, 4);
  1329. buffer.Skip(2); //customData
  1330. this.opaque = buffer.ReadBool();
  1331. int maskId = buffer.ReadShort();
  1332. if (maskId != -1)
  1333. {
  1334. container.mask = GetChildAt(maskId).displayObject;
  1335. if (buffer.ReadBool())
  1336. container.reversedMask = true;
  1337. }
  1338. {
  1339. string hitTestId = buffer.ReadS();
  1340. int i1 = buffer.ReadInt();
  1341. int i2 = buffer.ReadInt();
  1342. if (hitTestId != null)
  1343. {
  1344. PackageItem pi = contentItem.owner.GetItem(hitTestId);
  1345. if (pi != null && pi.pixelHitTestData != null)
  1346. rootContainer.hitArea = new PixelHitTest(pi.pixelHitTestData, i1, i2, sourceWidth, sourceHeight);
  1347. }
  1348. else if (i1 != 0 && i2 != -1)
  1349. {
  1350. rootContainer.hitArea = new ShapeHitTest(this.GetChildAt(i2).displayObject);
  1351. }
  1352. }
  1353. if (buffer.version >= 5)
  1354. {
  1355. string str = buffer.ReadS();
  1356. if (!string.IsNullOrEmpty(str))
  1357. this.onAddedToStage.Add(() => __playSound(str, 1));
  1358. string str2 = buffer.ReadS();
  1359. if (!string.IsNullOrEmpty(str2))
  1360. this.onRemovedFromStage.Add(() => __playSound(str2, 1));
  1361. }
  1362. buffer.Seek(0, 5);
  1363. int transitionCount = buffer.ReadShort();
  1364. for (int i = 0; i < transitionCount; i++)
  1365. {
  1366. int nextPos = buffer.ReadShort();
  1367. nextPos += buffer.position;
  1368. Transition trans = new Transition(this);
  1369. trans.Setup(buffer);
  1370. _transitions.Add(trans);
  1371. buffer.position = nextPos;
  1372. }
  1373. if (_transitions.Count > 0)
  1374. {
  1375. this.onAddedToStage.Add(__addedToStage);
  1376. this.onRemovedFromStage.Add(__removedFromStage);
  1377. }
  1378. ApplyAllControllers();
  1379. _buildingDisplayList = false;
  1380. underConstruct = false;
  1381. BuildNativeDisplayList();
  1382. SetBoundsChangedFlag();
  1383. if (contentItem.objectType != ObjectType.Component)
  1384. ConstructExtension(buffer);
  1385. ConstructFromXML(null);
  1386. #if FAIRYGUI_TOLUA
  1387. CallLua("ctor");
  1388. #endif
  1389. #if FAIRYGUI_PUERTS
  1390. if (__onConstruct != null)
  1391. __onConstruct();
  1392. #endif
  1393. }
  1394. virtual protected void ConstructExtension(ByteBuffer buffer)
  1395. {
  1396. }
  1397. /// <summary>
  1398. /// Method for extensions to override
  1399. /// </summary>
  1400. /// <param name="xml"></param>
  1401. virtual public void ConstructFromXML(XML xml)
  1402. {
  1403. }
  1404. public override void Setup_AfterAdd(ByteBuffer buffer, int beginPos)
  1405. {
  1406. base.Setup_AfterAdd(buffer, beginPos);
  1407. buffer.Seek(beginPos, 4);
  1408. int pageController = buffer.ReadShort();
  1409. if (pageController != -1 && scrollPane != null && scrollPane.pageMode)
  1410. scrollPane.pageController = parent.GetControllerAt(pageController);
  1411. int cnt = buffer.ReadShort();
  1412. for (int i = 0; i < cnt; i++)
  1413. {
  1414. Controller cc = GetController(buffer.ReadS());
  1415. string pageId = buffer.ReadS();
  1416. if (cc != null)
  1417. cc.selectedPageId = pageId;
  1418. }
  1419. if (buffer.version >= 2)
  1420. {
  1421. cnt = buffer.ReadShort();
  1422. for (int i = 0; i < cnt; i++)
  1423. {
  1424. string target = buffer.ReadS();
  1425. int propertyId = buffer.ReadShort();
  1426. string value = buffer.ReadS();
  1427. GObject obj = this.GetChildByPath(target);
  1428. if (obj != null)
  1429. {
  1430. if (propertyId == 0)
  1431. obj.text = value;
  1432. else if (propertyId == 1)
  1433. obj.icon = value;
  1434. }
  1435. }
  1436. }
  1437. }
  1438. void __playSound(string soundRes, float volumeScale)
  1439. {
  1440. NAudioClip sound = UIPackage.GetItemAssetByURL(soundRes) as NAudioClip;
  1441. if (sound != null && sound.nativeClip != null)
  1442. Stage.inst.PlayOneShotSound(sound.nativeClip, volumeScale);
  1443. }
  1444. void __addedToStage()
  1445. {
  1446. int cnt = _transitions.Count;
  1447. for (int i = 0; i < cnt; ++i)
  1448. _transitions[i].OnOwnerAddedToStage();
  1449. }
  1450. void __removedFromStage()
  1451. {
  1452. int cnt = _transitions.Count;
  1453. for (int i = 0; i < cnt; ++i)
  1454. _transitions[i].OnOwnerRemovedFromStage();
  1455. }
  1456. #if FAIRYGUI_TOLUA
  1457. internal LuaTable _peerTable;
  1458. public void SetLuaPeer(LuaTable peerTable)
  1459. {
  1460. _peerTable = peerTable;
  1461. }
  1462. [NoToLua]
  1463. public bool CallLua(string funcName)
  1464. {
  1465. if (_peerTable != null)
  1466. {
  1467. LuaFunction ctor = _peerTable.GetLuaFunction(funcName);
  1468. if (ctor != null)
  1469. {
  1470. try
  1471. {
  1472. ctor.Call(this);
  1473. }
  1474. catch (Exception err)
  1475. {
  1476. Debug.LogError(err);
  1477. }
  1478. ctor.Dispose();
  1479. return true;
  1480. }
  1481. }
  1482. return false;
  1483. }
  1484. #endif
  1485. #if FAIRYGUI_PUERTS
  1486. public Action __onConstruct;
  1487. public Action __onDispose;
  1488. #endif
  1489. }
  1490. }