GComponent.cs 52 KB

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