GComponent.cs 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705
  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. //因list的dispost无法正常释放loader加载的资源,修改第三方代码
  75. base.Dispose(); //Dispose native tree first, avoid DisplayObject.RemoveFromParent call
  76. cnt = _children.Count;
  77. for (int i = cnt - 1; i >= 0; --i)
  78. {
  79. GObject obj = _children[i];
  80. obj.InternalSetParent(null); //Avoid GObject.RemoveParent call
  81. obj.Dispose();
  82. }
  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. }