GList.cs 105 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051
  1. using System;
  2. using System.Collections.Generic;
  3. using FairyGUI.Utils;
  4. using UnityEngine;
  5. namespace FairyGUI
  6. {
  7. /// <summary>
  8. /// Callback function when an item is needed to update its look.
  9. /// </summary>
  10. /// <param name="index">Item index.</param>
  11. /// <param name="item">Item object.</param>
  12. public delegate void ListItemRenderer(int index, GObject item);
  13. /// <summary>
  14. ///
  15. /// </summary>
  16. /// <param name="index"></param>
  17. /// <returns></returns>
  18. public delegate string ListItemProvider(int index);
  19. /// <summary>
  20. /// GList class.
  21. /// </summary>
  22. public class GList : GComponent
  23. {
  24. /// <summary>
  25. /// Resource url of the default item.
  26. /// </summary>
  27. public string defaultItem;
  28. /// <summary>
  29. /// 如果true,当item不可见时自动折叠,否则依然占位
  30. /// </summary>
  31. public bool foldInvisibleItems = false;
  32. /// <summary>
  33. /// List selection mode
  34. /// </summary>
  35. /// <seealso cref="ListSelectionMode"/>
  36. public ListSelectionMode selectionMode;
  37. /// <summary>
  38. /// Callback function when an item is needed to update its look.
  39. /// </summary>
  40. public ListItemRenderer itemRenderer;
  41. /// <summary>
  42. /// Callback funtion to return item resource url.
  43. /// </summary>
  44. public ListItemProvider itemProvider;
  45. /// <summary>
  46. ///
  47. /// </summary>
  48. public bool scrollItemToViewOnClick;
  49. ListLayoutType _layout;
  50. int _lineCount;
  51. int _columnCount;
  52. int _lineGap;
  53. int _columnGap;
  54. AlignType _align;
  55. VertAlignType _verticalAlign;
  56. bool _autoResizeItem;
  57. Controller _selectionController;
  58. GObjectPool _pool;
  59. int _lastSelectedIndex;
  60. EventListener _onClickItem;
  61. EventListener _onRightClickItem;
  62. //Virtual List support
  63. bool _virtual;
  64. bool _loop;
  65. int _numItems;
  66. int _realNumItems;
  67. int _firstIndex; //the top left index
  68. int _curLineItemCount; //item count in one line
  69. int _curLineItemCount2; //只用在页面模式,表示垂直方向的项目数
  70. Vector2 _itemSize;
  71. int _virtualListChanged; //1-content changed, 2-size changed
  72. uint itemInfoVer; //用来标志item是否在本次处理中已经被重用了
  73. int _miscFlags; //1-event locked, 2-focus events registered
  74. class ItemInfo
  75. {
  76. public Vector2 size;
  77. public GObject obj;
  78. public uint updateFlag;
  79. public bool selected;
  80. }
  81. List<ItemInfo> _virtualItems;
  82. EventCallback1 _itemClickDelegate;
  83. public GList()
  84. : base()
  85. {
  86. _trackBounds = true;
  87. this.opaque = true;
  88. scrollItemToViewOnClick = true;
  89. container = new Container();
  90. rootContainer.AddChild(container);
  91. rootContainer.gameObject.name = "GList";
  92. _pool = new GObjectPool(container.cachedTransform);
  93. _itemClickDelegate = __clickItem;
  94. }
  95. public override void Dispose()
  96. {
  97. _pool.Clear();
  98. if (_virtualListChanged != 0)
  99. Timers.inst.Remove(this.RefreshVirtualList);
  100. _selectionController = null;
  101. scrollItemToViewOnClick = false;
  102. itemRenderer = null;
  103. itemProvider = null;
  104. base.Dispose();
  105. }
  106. /// <summary>
  107. /// Dispatched when a list item being clicked.
  108. /// </summary>
  109. public EventListener onClickItem
  110. {
  111. get { return _onClickItem ?? (_onClickItem = new EventListener(this, "onClickItem")); }
  112. }
  113. /// <summary>
  114. /// Dispatched when a list item being clicked with right button.
  115. /// </summary>
  116. public EventListener onRightClickItem
  117. {
  118. get { return _onRightClickItem ?? (_onRightClickItem = new EventListener(this, "onRightClickItem")); }
  119. }
  120. /// <summary>
  121. /// List layout type.
  122. /// </summary>
  123. public ListLayoutType layout
  124. {
  125. get { return _layout; }
  126. set
  127. {
  128. if (_layout != value)
  129. {
  130. _layout = value;
  131. SetBoundsChangedFlag();
  132. if (_virtual)
  133. SetVirtualListChangedFlag(true);
  134. }
  135. }
  136. }
  137. /// <summary>
  138. ///
  139. /// </summary>
  140. public int lineCount
  141. {
  142. get { return _lineCount; }
  143. set
  144. {
  145. if (_lineCount != value)
  146. {
  147. _lineCount = value;
  148. if (_layout == ListLayoutType.FlowVertical || _layout == ListLayoutType.Pagination)
  149. {
  150. SetBoundsChangedFlag();
  151. if (_virtual)
  152. SetVirtualListChangedFlag(true);
  153. }
  154. }
  155. }
  156. }
  157. /// <summary>
  158. ///
  159. /// </summary>
  160. public int columnCount
  161. {
  162. get { return _columnCount; }
  163. set
  164. {
  165. if (_columnCount != value)
  166. {
  167. _columnCount = value;
  168. if (_layout == ListLayoutType.FlowHorizontal || _layout == ListLayoutType.Pagination)
  169. {
  170. SetBoundsChangedFlag();
  171. if (_virtual)
  172. SetVirtualListChangedFlag(true);
  173. }
  174. }
  175. }
  176. }
  177. /// <summary>
  178. ///
  179. /// </summary>
  180. public int lineGap
  181. {
  182. get { return _lineGap; }
  183. set
  184. {
  185. if (_lineGap != value)
  186. {
  187. _lineGap = value;
  188. SetBoundsChangedFlag();
  189. if (_virtual)
  190. SetVirtualListChangedFlag(true);
  191. }
  192. }
  193. }
  194. /// <summary>
  195. ///
  196. /// </summary>
  197. public int columnGap
  198. {
  199. get { return _columnGap; }
  200. set
  201. {
  202. if (_columnGap != value)
  203. {
  204. _columnGap = value;
  205. SetBoundsChangedFlag();
  206. if (_virtual)
  207. SetVirtualListChangedFlag(true);
  208. }
  209. }
  210. }
  211. /// <summary>
  212. ///
  213. /// </summary>
  214. public AlignType align
  215. {
  216. get { return _align; }
  217. set
  218. {
  219. if (_align != value)
  220. {
  221. _align = value;
  222. SetBoundsChangedFlag();
  223. if (_virtual)
  224. SetVirtualListChangedFlag(true);
  225. }
  226. }
  227. }
  228. /// <summary>
  229. ///
  230. /// </summary>
  231. public VertAlignType verticalAlign
  232. {
  233. get { return _verticalAlign; }
  234. set
  235. {
  236. if (_verticalAlign != value)
  237. {
  238. _verticalAlign = value;
  239. SetBoundsChangedFlag();
  240. if (_virtual)
  241. SetVirtualListChangedFlag(true);
  242. }
  243. }
  244. }
  245. /// <summary>
  246. /// If the item will resize itself to fit the list width/height.
  247. /// </summary>
  248. public bool autoResizeItem
  249. {
  250. get { return _autoResizeItem; }
  251. set
  252. {
  253. if (_autoResizeItem != value)
  254. {
  255. _autoResizeItem = value;
  256. SetBoundsChangedFlag();
  257. if (_virtual)
  258. SetVirtualListChangedFlag(true);
  259. }
  260. }
  261. }
  262. /// <summary>
  263. ///
  264. /// </summary>
  265. /// <value></value>
  266. public Vector2 defaultItemSize
  267. {
  268. get { return _itemSize; }
  269. set
  270. {
  271. _itemSize = value;
  272. if (_virtual)
  273. {
  274. if (_layout == ListLayoutType.SingleColumn || _layout == ListLayoutType.FlowHorizontal)
  275. this.scrollPane.scrollStep = _itemSize.y;
  276. else
  277. this.scrollPane.scrollStep = _itemSize.x;
  278. SetVirtualListChangedFlag(true);
  279. }
  280. }
  281. }
  282. /// <summary>
  283. ///
  284. /// </summary>
  285. public GObjectPool itemPool
  286. {
  287. get { return _pool; }
  288. }
  289. /// <summary>
  290. ///
  291. /// </summary>
  292. /// <param name="url"></param>
  293. /// <returns></returns>
  294. public GObject GetFromPool(string url)
  295. {
  296. if (string.IsNullOrEmpty(url))
  297. url = defaultItem;
  298. GObject ret = _pool.GetObject(url);
  299. if (ret != null)
  300. ret.visible = true;
  301. return ret;
  302. }
  303. void ReturnToPool(GObject obj)
  304. {
  305. _pool.ReturnObject(obj);
  306. }
  307. /// <summary>
  308. /// Add a item to list, same as GetFromPool+AddChild
  309. /// </summary>
  310. /// <returns>Item object</returns>
  311. public GObject AddItemFromPool()
  312. {
  313. GObject obj = GetFromPool(null);
  314. return AddChild(obj);
  315. }
  316. /// <summary>
  317. /// Add a item to list, same as GetFromPool+AddChild
  318. /// </summary>
  319. /// <param name="url">Item resource url</param>
  320. /// <returns>Item object</returns>
  321. public GObject AddItemFromPool(string url)
  322. {
  323. GObject obj = GetFromPool(url);
  324. return AddChild(obj);
  325. }
  326. /// <summary>
  327. ///
  328. /// </summary>
  329. /// <param name="child"></param>
  330. /// <param name="index"></param>
  331. /// <returns></returns>
  332. override public GObject AddChildAt(GObject child, int index)
  333. {
  334. base.AddChildAt(child, index);
  335. if (child is GButton)
  336. {
  337. GButton button = (GButton)child;
  338. button.selected = false;
  339. button.changeStateOnClick = false;
  340. }
  341. child.onClick.Add(_itemClickDelegate);
  342. child.onRightClick.Add(_itemClickDelegate);
  343. return child;
  344. }
  345. /// <summary>
  346. ///
  347. /// </summary>
  348. /// <param name="index"></param>
  349. /// <param name="dispose"></param>
  350. /// <returns></returns>
  351. override public GObject RemoveChildAt(int index, bool dispose)
  352. {
  353. GObject child = base.RemoveChildAt(index, dispose);
  354. child.onClick.Remove(_itemClickDelegate);
  355. child.onRightClick.Remove(_itemClickDelegate);
  356. return child;
  357. }
  358. /// <summary>
  359. ///
  360. /// </summary>
  361. /// <param name="index"></param>
  362. public void RemoveChildToPoolAt(int index)
  363. {
  364. GObject child = base.RemoveChildAt(index);
  365. ReturnToPool(child);
  366. }
  367. /// <summary>
  368. ///
  369. /// </summary>
  370. /// <param name="child"></param>
  371. public void RemoveChildToPool(GObject child)
  372. {
  373. base.RemoveChild(child);
  374. ReturnToPool(child);
  375. }
  376. /// <summary>
  377. ///
  378. /// </summary>
  379. public void RemoveChildrenToPool()
  380. {
  381. RemoveChildrenToPool(0, -1);
  382. }
  383. /// <summary>
  384. ///
  385. /// </summary>
  386. /// <param name="beginIndex"></param>
  387. /// <param name="endIndex"></param>
  388. public void RemoveChildrenToPool(int beginIndex, int endIndex)
  389. {
  390. if (endIndex < 0 || endIndex >= _children.Count)
  391. endIndex = _children.Count - 1;
  392. for (int i = beginIndex; i <= endIndex; ++i)
  393. RemoveChildToPoolAt(beginIndex);
  394. }
  395. /// <summary>
  396. ///
  397. /// </summary>
  398. public int selectedIndex
  399. {
  400. get
  401. {
  402. if (_virtual)
  403. {
  404. int cnt = _realNumItems;
  405. for (int i = 0; i < cnt; i++)
  406. {
  407. ItemInfo ii = _virtualItems[i];
  408. if ((ii.obj is GButton) && ((GButton)ii.obj).selected
  409. || ii.obj == null && ii.selected)
  410. {
  411. if (_loop)
  412. return i % _numItems;
  413. else
  414. return i;
  415. }
  416. }
  417. }
  418. else
  419. {
  420. int cnt = _children.Count;
  421. for (int i = 0; i < cnt; i++)
  422. {
  423. GButton obj = _children[i].asButton;
  424. if (obj != null && obj.selected)
  425. return i;
  426. }
  427. }
  428. return -1;
  429. }
  430. set
  431. {
  432. if (value >= 0 && value < this.numItems)
  433. {
  434. if (selectionMode != ListSelectionMode.Single)
  435. ClearSelection();
  436. AddSelection(value, false);
  437. }
  438. else
  439. ClearSelection();
  440. }
  441. }
  442. /// <summary>
  443. ///
  444. /// </summary>
  445. public Controller selectionController
  446. {
  447. get { return _selectionController; }
  448. set { _selectionController = value; }
  449. }
  450. /// <summary>
  451. ///
  452. /// </summary>
  453. /// <returns></returns>
  454. public List<int> GetSelection()
  455. {
  456. return GetSelection(null);
  457. }
  458. /// <summary>
  459. ///
  460. /// </summary>
  461. /// <returns></returns>
  462. public List<int> GetSelection(List<int> result)
  463. {
  464. if (result == null)
  465. result = new List<int>();
  466. if (_virtual)
  467. {
  468. int cnt = _realNumItems;
  469. for (int i = 0; i < cnt; i++)
  470. {
  471. ItemInfo ii = _virtualItems[i];
  472. if ((ii.obj is GButton) && ((GButton)ii.obj).selected
  473. || ii.obj == null && ii.selected)
  474. {
  475. int j = i;
  476. if (_loop)
  477. {
  478. j = i % _numItems;
  479. if (result.Contains(j))
  480. continue;
  481. }
  482. result.Add(j);
  483. }
  484. }
  485. }
  486. else
  487. {
  488. int cnt = _children.Count;
  489. for (int i = 0; i < cnt; i++)
  490. {
  491. GButton obj = _children[i].asButton;
  492. if (obj != null && obj.selected)
  493. result.Add(i);
  494. }
  495. }
  496. return result;
  497. }
  498. /// <summary>
  499. ///
  500. /// </summary>
  501. /// <param name="index"></param>
  502. /// <param name="scrollItToView"></param>
  503. public void AddSelection(int index, bool scrollItToView)
  504. {
  505. if (selectionMode == ListSelectionMode.None)
  506. return;
  507. CheckVirtualList();
  508. if (selectionMode == ListSelectionMode.Single)
  509. ClearSelection();
  510. if (scrollItToView)
  511. ScrollToView(index);
  512. _lastSelectedIndex = index;
  513. GButton obj = null;
  514. if (_virtual)
  515. {
  516. ItemInfo ii = _virtualItems[index];
  517. if (ii.obj != null)
  518. obj = ii.obj.asButton;
  519. ii.selected = true;
  520. }
  521. else
  522. obj = GetChildAt(index).asButton;
  523. if (obj != null && !obj.selected)
  524. {
  525. obj.selected = true;
  526. UpdateSelectionController(index);
  527. }
  528. }
  529. /// <summary>
  530. ///
  531. /// </summary>
  532. /// <param name="index"></param>
  533. public void RemoveSelection(int index)
  534. {
  535. if (selectionMode == ListSelectionMode.None)
  536. return;
  537. GButton obj = null;
  538. if (_virtual)
  539. {
  540. ItemInfo ii = _virtualItems[index];
  541. if (ii.obj != null)
  542. obj = ii.obj.asButton;
  543. ii.selected = false;
  544. }
  545. else
  546. obj = GetChildAt(index).asButton;
  547. if (obj != null)
  548. obj.selected = false;
  549. }
  550. /// <summary>
  551. ///
  552. /// </summary>
  553. public void ClearSelection()
  554. {
  555. if (_virtual)
  556. {
  557. int cnt = _realNumItems;
  558. for (int i = 0; i < cnt; i++)
  559. {
  560. ItemInfo ii = _virtualItems[i];
  561. if ((ii.obj is GButton))
  562. ((GButton)ii.obj).selected = false;
  563. ii.selected = false;
  564. }
  565. }
  566. else
  567. {
  568. int cnt = _children.Count;
  569. for (int i = 0; i < cnt; i++)
  570. {
  571. GButton obj = _children[i].asButton;
  572. if (obj != null)
  573. obj.selected = false;
  574. }
  575. }
  576. }
  577. void ClearSelectionExcept(GObject g)
  578. {
  579. if (_virtual)
  580. {
  581. int cnt = _realNumItems;
  582. for (int i = 0; i < cnt; i++)
  583. {
  584. ItemInfo ii = _virtualItems[i];
  585. if (ii.obj != g)
  586. {
  587. if ((ii.obj is GButton))
  588. ((GButton)ii.obj).selected = false;
  589. ii.selected = false;
  590. }
  591. }
  592. }
  593. else
  594. {
  595. int cnt = _children.Count;
  596. for (int i = 0; i < cnt; i++)
  597. {
  598. GButton obj = _children[i].asButton;
  599. if (obj != null && obj != g)
  600. obj.selected = false;
  601. }
  602. }
  603. }
  604. /// <summary>
  605. ///
  606. /// </summary>
  607. public void SelectAll()
  608. {
  609. CheckVirtualList();
  610. int last = -1;
  611. if (_virtual)
  612. {
  613. int cnt = _realNumItems;
  614. for (int i = 0; i < cnt; i++)
  615. {
  616. ItemInfo ii = _virtualItems[i];
  617. if ((ii.obj is GButton) && !((GButton)ii.obj).selected)
  618. {
  619. ((GButton)ii.obj).selected = true;
  620. last = i;
  621. }
  622. ii.selected = true;
  623. }
  624. }
  625. else
  626. {
  627. int cnt = _children.Count;
  628. for (int i = 0; i < cnt; i++)
  629. {
  630. GButton obj = _children[i].asButton;
  631. if (obj != null && !obj.selected)
  632. {
  633. obj.selected = true;
  634. last = i;
  635. }
  636. }
  637. }
  638. if (last != -1)
  639. UpdateSelectionController(last);
  640. }
  641. /// <summary>
  642. ///
  643. /// </summary>
  644. public void SelectNone()
  645. {
  646. ClearSelection();
  647. }
  648. /// <summary>
  649. ///
  650. /// </summary>
  651. public void SelectReverse()
  652. {
  653. CheckVirtualList();
  654. int last = -1;
  655. if (_virtual)
  656. {
  657. int cnt = _realNumItems;
  658. for (int i = 0; i < cnt; i++)
  659. {
  660. ItemInfo ii = _virtualItems[i];
  661. if ((ii.obj is GButton))
  662. {
  663. ((GButton)ii.obj).selected = !((GButton)ii.obj).selected;
  664. if (((GButton)ii.obj).selected)
  665. last = i;
  666. }
  667. ii.selected = !ii.selected;
  668. }
  669. }
  670. else
  671. {
  672. int cnt = _children.Count;
  673. for (int i = 0; i < cnt; i++)
  674. {
  675. GButton obj = _children[i].asButton;
  676. if (obj != null)
  677. {
  678. obj.selected = !obj.selected;
  679. if (obj.selected)
  680. last = i;
  681. }
  682. }
  683. }
  684. if (last != -1)
  685. UpdateSelectionController(last);
  686. }
  687. /// <summary>
  688. ///
  689. /// </summary>
  690. /// <param name="enabled"></param>
  691. public void EnableSelectionFocusEvents(bool enabled)
  692. {
  693. if (((_miscFlags & 2) != 0) == enabled)
  694. return;
  695. if (enabled)
  696. {
  697. _miscFlags |= 2;
  698. this.tabStopChildren = true;
  699. onFocusIn.Add(NotifySelection);
  700. onFocusOut.Add(NotifySelection);
  701. }
  702. else
  703. {
  704. _miscFlags &= 0xFD;
  705. onFocusIn.Remove(NotifySelection);
  706. onFocusOut.Remove(NotifySelection);
  707. }
  708. }
  709. void NotifySelection(EventContext context)
  710. {
  711. string eventType = context.type == "onFocusIn" ? "onListFocusIn" : "onListFocusOut";
  712. int cnt = _children.Count;
  713. for (int i = 0; i < cnt; i++)
  714. {
  715. GButton obj = _children[i].asButton;
  716. if (obj != null && obj.selected)
  717. obj.DispatchEvent(eventType);
  718. }
  719. }
  720. /// <summary>
  721. ///
  722. /// </summary>
  723. public void EnableArrowKeyNavigation(bool enabled)
  724. {
  725. if (enabled)
  726. {
  727. this.tabStopChildren = true;
  728. onKeyDown.Add(__keydown);
  729. }
  730. else
  731. {
  732. this.tabStopChildren = false;
  733. onKeyDown.Remove(__keydown);
  734. }
  735. }
  736. void __keydown(EventContext context)
  737. {
  738. int index = -1;
  739. switch (context.inputEvent.keyCode)
  740. {
  741. case KeyCode.LeftArrow:
  742. index = HandleArrowKey(7);
  743. break;
  744. case KeyCode.RightArrow:
  745. index = HandleArrowKey(3);
  746. break;
  747. case KeyCode.UpArrow:
  748. index = HandleArrowKey(1);
  749. break;
  750. case KeyCode.DownArrow:
  751. index = HandleArrowKey(5);
  752. break;
  753. }
  754. if (index != -1)
  755. {
  756. index = ItemIndexToChildIndex(index);
  757. if (index != -1)
  758. DispatchItemEvent(GetChildAt(index), context);
  759. context.StopPropagation();
  760. }
  761. }
  762. /// <summary>
  763. ///
  764. /// </summary>
  765. /// <param name="dir"></param>
  766. public int HandleArrowKey(int dir)
  767. {
  768. int curIndex = this.selectedIndex;
  769. if (curIndex == -1)
  770. return -1;
  771. int index = curIndex;
  772. switch (dir)
  773. {
  774. case 1://up
  775. if (_layout == ListLayoutType.SingleColumn || _layout == ListLayoutType.FlowVertical)
  776. {
  777. index--;
  778. }
  779. else if (_layout == ListLayoutType.FlowHorizontal || _layout == ListLayoutType.Pagination)
  780. {
  781. if (_virtual)
  782. {
  783. index -= _curLineItemCount;
  784. }
  785. else
  786. {
  787. GObject current = _children[index];
  788. int k = 0;
  789. int i;
  790. for (i = index - 1; i >= 0; i--)
  791. {
  792. GObject obj = _children[i];
  793. if (obj.y != current.y)
  794. {
  795. current = obj;
  796. break;
  797. }
  798. k++;
  799. }
  800. for (; i >= 0; i--)
  801. {
  802. GObject obj = _children[i];
  803. if (obj.y != current.y)
  804. {
  805. index = i + k + 1;
  806. break;
  807. }
  808. }
  809. }
  810. }
  811. break;
  812. case 3://right
  813. if (_layout == ListLayoutType.SingleRow || _layout == ListLayoutType.FlowHorizontal || _layout == ListLayoutType.Pagination)
  814. {
  815. index++;
  816. }
  817. else if (_layout == ListLayoutType.FlowVertical)
  818. {
  819. if (_virtual)
  820. {
  821. index += _curLineItemCount;
  822. }
  823. else
  824. {
  825. GObject current = _children[index];
  826. int k = 0;
  827. int cnt = _children.Count;
  828. int i;
  829. for (i = index + 1; i < cnt; i++)
  830. {
  831. GObject obj = _children[i];
  832. if (obj.x != current.x)
  833. {
  834. current = obj;
  835. break;
  836. }
  837. k++;
  838. }
  839. for (; i < cnt; i++)
  840. {
  841. GObject obj = _children[i];
  842. if (obj.x != current.x)
  843. {
  844. index = i - k - 1;
  845. break;
  846. }
  847. }
  848. }
  849. }
  850. break;
  851. case 5://down
  852. if (_layout == ListLayoutType.SingleColumn || _layout == ListLayoutType.FlowVertical)
  853. {
  854. index++;
  855. }
  856. else if (_layout == ListLayoutType.FlowHorizontal || _layout == ListLayoutType.Pagination)
  857. {
  858. if (_virtual)
  859. {
  860. index += _curLineItemCount;
  861. }
  862. else
  863. {
  864. GObject current = _children[index];
  865. int k = 0;
  866. int cnt = _children.Count;
  867. int i;
  868. for (i = index + 1; i < cnt; i++)
  869. {
  870. GObject obj = _children[i];
  871. if (obj.y != current.y)
  872. {
  873. current = obj;
  874. break;
  875. }
  876. k++;
  877. }
  878. for (; i < cnt; i++)
  879. {
  880. GObject obj = _children[i];
  881. if (obj.y != current.y)
  882. {
  883. index = i - k - 1;
  884. break;
  885. }
  886. }
  887. }
  888. }
  889. break;
  890. case 7://left
  891. if (_layout == ListLayoutType.SingleRow || _layout == ListLayoutType.FlowHorizontal || _layout == ListLayoutType.Pagination)
  892. {
  893. index--;
  894. }
  895. else if (_layout == ListLayoutType.FlowVertical)
  896. {
  897. if (_virtual)
  898. {
  899. index -= _curLineItemCount;
  900. }
  901. else
  902. {
  903. GObject current = _children[index];
  904. int k = 0;
  905. int i;
  906. for (i = index - 1; i >= 0; i--)
  907. {
  908. GObject obj = _children[i];
  909. if (obj.x != current.x)
  910. {
  911. current = obj;
  912. break;
  913. }
  914. k++;
  915. }
  916. for (; i >= 0; i--)
  917. {
  918. GObject obj = _children[i];
  919. if (obj.x != current.x)
  920. {
  921. index = i + k + 1;
  922. break;
  923. }
  924. }
  925. }
  926. }
  927. break;
  928. }
  929. if (index != curIndex && index >= 0 && index < this.numItems)
  930. {
  931. ClearSelection();
  932. AddSelection(index, true);
  933. return index;
  934. }
  935. else
  936. return -1;
  937. }
  938. void __clickItem(EventContext context)
  939. {
  940. GObject item = context.sender as GObject;
  941. if ((item is GButton) && selectionMode != ListSelectionMode.None)
  942. SetSelectionOnEvent(item, context.inputEvent);
  943. if (scrollPane != null && scrollItemToViewOnClick)
  944. scrollPane.ScrollToView(item, true);
  945. DispatchItemEvent(item, context);
  946. }
  947. virtual protected void DispatchItemEvent(GObject item, EventContext context)
  948. {
  949. if (context.type == item.onRightClick.type)
  950. DispatchEvent("onRightClickItem", item);
  951. else
  952. DispatchEvent("onClickItem", item);
  953. }
  954. void SetSelectionOnEvent(GObject item, InputEvent evt)
  955. {
  956. bool dontChangeLastIndex = false;
  957. GButton button = (GButton)item;
  958. int index = ChildIndexToItemIndex(GetChildIndex(item));
  959. if (selectionMode == ListSelectionMode.Single)
  960. {
  961. if (!button.selected)
  962. {
  963. ClearSelectionExcept(button);
  964. button.selected = true;
  965. }
  966. }
  967. else
  968. {
  969. if (evt.shift)
  970. {
  971. if (!button.selected)
  972. {
  973. if (_lastSelectedIndex != -1)
  974. {
  975. int min = Math.Min(_lastSelectedIndex, index);
  976. int max = Math.Max(_lastSelectedIndex, index);
  977. max = Math.Min(max, this.numItems - 1);
  978. if (_virtual)
  979. {
  980. for (int i = min; i <= max; i++)
  981. {
  982. ItemInfo ii = _virtualItems[i];
  983. if (ii.obj is GButton)
  984. ((GButton)ii.obj).selected = true;
  985. ii.selected = true;
  986. }
  987. }
  988. else
  989. {
  990. for (int i = min; i <= max; i++)
  991. {
  992. GButton obj = GetChildAt(i).asButton;
  993. if (obj != null && !obj.selected)
  994. obj.selected = true;
  995. }
  996. }
  997. dontChangeLastIndex = true;
  998. }
  999. else
  1000. {
  1001. button.selected = true;
  1002. }
  1003. }
  1004. }
  1005. else if (evt.ctrlOrCmd || selectionMode == ListSelectionMode.Multiple_SingleClick)
  1006. {
  1007. button.selected = !button.selected;
  1008. }
  1009. else
  1010. {
  1011. if (!button.selected)
  1012. {
  1013. ClearSelectionExcept(button);
  1014. button.selected = true;
  1015. }
  1016. else if (evt.button == 0)
  1017. ClearSelectionExcept(button);
  1018. }
  1019. }
  1020. if (!dontChangeLastIndex)
  1021. _lastSelectedIndex = index;
  1022. if (button.selected)
  1023. UpdateSelectionController(index);
  1024. }
  1025. /// <summary>
  1026. /// Resize to list size to fit specified item count.
  1027. /// If list layout is single column or flow horizontally, the height will change to fit.
  1028. /// If list layout is single row or flow vertically, the width will change to fit.
  1029. /// </summary>
  1030. public void ResizeToFit()
  1031. {
  1032. ResizeToFit(int.MaxValue, 0);
  1033. }
  1034. /// <summary>
  1035. /// Resize to list size to fit specified item count.
  1036. /// If list layout is single column or flow horizontally, the height will change to fit.
  1037. /// If list layout is single row or flow vertically, the width will change to fit.
  1038. /// </summary>
  1039. /// <param name="itemCount">Item count</param>
  1040. public void ResizeToFit(int itemCount)
  1041. {
  1042. ResizeToFit(itemCount, 0);
  1043. }
  1044. /// <summary>
  1045. /// Resize to list size to fit specified item count.
  1046. /// If list layout is single column or flow horizontally, the height will change to fit.
  1047. /// If list layout is single row or flow vertically, the width will change to fit.
  1048. /// </summary>
  1049. /// <param name="itemCount">>Item count</param>
  1050. /// <param name="minSize">If the result size if smaller than minSize, then use minSize.</param>
  1051. public void ResizeToFit(int itemCount, int minSize)
  1052. {
  1053. EnsureBoundsCorrect();
  1054. int curCount = this.numItems;
  1055. if (itemCount > curCount)
  1056. itemCount = curCount;
  1057. if (_virtual)
  1058. {
  1059. int lineCount = Mathf.CeilToInt((float)itemCount / _curLineItemCount);
  1060. if (_layout == ListLayoutType.SingleColumn || _layout == ListLayoutType.FlowHorizontal)
  1061. this.viewHeight = lineCount * _itemSize.y + Math.Max(0, lineCount - 1) * _lineGap;
  1062. else
  1063. this.viewWidth = lineCount * _itemSize.x + Math.Max(0, lineCount - 1) * _columnGap;
  1064. }
  1065. else if (itemCount == 0)
  1066. {
  1067. if (_layout == ListLayoutType.SingleColumn || _layout == ListLayoutType.FlowHorizontal)
  1068. this.viewHeight = minSize;
  1069. else
  1070. this.viewWidth = minSize;
  1071. }
  1072. else
  1073. {
  1074. int i = itemCount - 1;
  1075. GObject obj = null;
  1076. while (i >= 0)
  1077. {
  1078. obj = this.GetChildAt(i);
  1079. if (!foldInvisibleItems || obj.visible)
  1080. break;
  1081. i--;
  1082. }
  1083. if (i < 0)
  1084. {
  1085. if (_layout == ListLayoutType.SingleColumn || _layout == ListLayoutType.FlowHorizontal)
  1086. this.viewHeight = minSize;
  1087. else
  1088. this.viewWidth = minSize;
  1089. }
  1090. else
  1091. {
  1092. float size;
  1093. if (_layout == ListLayoutType.SingleColumn || _layout == ListLayoutType.FlowHorizontal)
  1094. {
  1095. size = obj.y + obj.height;
  1096. if (size < minSize)
  1097. size = minSize;
  1098. this.viewHeight = size;
  1099. }
  1100. else
  1101. {
  1102. size = obj.x + obj.width;
  1103. if (size < minSize)
  1104. size = minSize;
  1105. this.viewWidth = size;
  1106. }
  1107. }
  1108. }
  1109. }
  1110. /// <summary>
  1111. ///
  1112. /// </summary>
  1113. override protected void HandleSizeChanged()
  1114. {
  1115. base.HandleSizeChanged();
  1116. SetBoundsChangedFlag();
  1117. if (_virtual)
  1118. SetVirtualListChangedFlag(true);
  1119. }
  1120. override public void HandleControllerChanged(Controller c)
  1121. {
  1122. base.HandleControllerChanged(c);
  1123. if (_selectionController == c)
  1124. this.selectedIndex = c.selectedIndex;
  1125. }
  1126. void UpdateSelectionController(int index)
  1127. {
  1128. if (_selectionController != null && !_selectionController.changing
  1129. && index < _selectionController.pageCount)
  1130. {
  1131. Controller c = _selectionController;
  1132. _selectionController = null;
  1133. c.selectedIndex = index;
  1134. _selectionController = c;
  1135. }
  1136. }
  1137. /// <summary>
  1138. /// Scroll the list to make an item with certain index visible.
  1139. /// </summary>
  1140. /// <param name="index">Item index</param>
  1141. public void ScrollToView(int index)
  1142. {
  1143. ScrollToView(index, false);
  1144. }
  1145. /// <summary>
  1146. /// Scroll the list to make an item with certain index visible.
  1147. /// </summary>
  1148. /// <param name="index">Item index</param>
  1149. /// <param name="ani">True to scroll smoothly, othewise immdediately.</param>
  1150. public void ScrollToView(int index, bool ani)
  1151. {
  1152. ScrollToView(index, ani, false);
  1153. }
  1154. /// <summary>
  1155. /// Scroll the list to make an item with certain index visible.
  1156. /// </summary>
  1157. /// <param name="index">Item index</param>
  1158. /// <param name="ani">True to scroll smoothly, othewise immdediately.</param>
  1159. /// <param name="setFirst">If true, scroll to make the target on the top/left; If false, scroll to make the target any position in view.</param>
  1160. public void ScrollToView(int index, bool ani, bool setFirst)
  1161. {
  1162. if (_virtual)
  1163. {
  1164. if (_numItems == 0)
  1165. return;
  1166. CheckVirtualList();
  1167. if (index >= _virtualItems.Count)
  1168. throw new Exception("Invalid child index: " + index + ">" + _virtualItems.Count);
  1169. if (_loop)
  1170. index = Mathf.FloorToInt((float)_firstIndex / _numItems) * _numItems + index;
  1171. Rect rect;
  1172. ItemInfo ii = _virtualItems[index];
  1173. if (_layout == ListLayoutType.SingleColumn || _layout == ListLayoutType.FlowHorizontal)
  1174. {
  1175. float pos = 0;
  1176. for (int i = _curLineItemCount - 1; i < index; i += _curLineItemCount)
  1177. pos += _virtualItems[i].size.y + _lineGap;
  1178. rect = new Rect(0, pos, _itemSize.x, ii.size.y);
  1179. }
  1180. else if (_layout == ListLayoutType.SingleRow || _layout == ListLayoutType.FlowVertical)
  1181. {
  1182. float pos = 0;
  1183. for (int i = _curLineItemCount - 1; i < index; i += _curLineItemCount)
  1184. pos += _virtualItems[i].size.x + _columnGap;
  1185. rect = new Rect(pos, 0, ii.size.x, _itemSize.y);
  1186. }
  1187. else
  1188. {
  1189. int page = index / (_curLineItemCount * _curLineItemCount2);
  1190. rect = new Rect(page * viewWidth + (index % _curLineItemCount) * (ii.size.x + _columnGap),
  1191. (index / _curLineItemCount) % _curLineItemCount2 * (ii.size.y + _lineGap),
  1192. ii.size.x, ii.size.y);
  1193. }
  1194. if (this.scrollPane != null)
  1195. scrollPane.ScrollToView(rect, ani, setFirst);
  1196. else if (parent != null && parent.scrollPane != null)
  1197. parent.scrollPane.ScrollToView(this.TransformRect(rect, parent), ani, setFirst);
  1198. }
  1199. else
  1200. {
  1201. GObject obj = GetChildAt(index);
  1202. if (this.scrollPane != null)
  1203. scrollPane.ScrollToView(obj, ani, setFirst);
  1204. else if (parent != null && parent.scrollPane != null)
  1205. parent.scrollPane.ScrollToView(obj, ani, setFirst);
  1206. }
  1207. }
  1208. /// <summary>
  1209. /// 获取当前点击哪个item
  1210. /// </summary>
  1211. public GObject touchItem
  1212. {
  1213. get
  1214. {
  1215. //find out which item is under finger
  1216. //逐层往上知道查到点击了那个item
  1217. GObject obj = GRoot.inst.touchTarget;
  1218. GObject p = obj.parent;
  1219. while (p != null)
  1220. {
  1221. if (p == this)
  1222. return obj;
  1223. obj = p;
  1224. p = p.parent;
  1225. }
  1226. return null;
  1227. }
  1228. }
  1229. /// <summary>
  1230. /// Get first child in view.
  1231. /// </summary>
  1232. /// <returns></returns>
  1233. public override int GetFirstChildInView()
  1234. {
  1235. return ChildIndexToItemIndex(base.GetFirstChildInView());
  1236. }
  1237. public int ChildIndexToItemIndex(int index)
  1238. {
  1239. if (!_virtual)
  1240. return index;
  1241. if (_layout == ListLayoutType.Pagination)
  1242. {
  1243. for (int i = _firstIndex; i < _realNumItems; i++)
  1244. {
  1245. if (_virtualItems[i].obj != null)
  1246. {
  1247. index--;
  1248. if (index < 0)
  1249. return i;
  1250. }
  1251. }
  1252. return index;
  1253. }
  1254. else
  1255. {
  1256. index += _firstIndex;
  1257. if (_loop && _numItems > 0)
  1258. index = index % _numItems;
  1259. return index;
  1260. }
  1261. }
  1262. public int ItemIndexToChildIndex(int index)
  1263. {
  1264. if (!_virtual)
  1265. return index;
  1266. if (_layout == ListLayoutType.Pagination)
  1267. {
  1268. return GetChildIndex(_virtualItems[index].obj);
  1269. }
  1270. else
  1271. {
  1272. if (_loop && _numItems > 0)
  1273. {
  1274. int j = _firstIndex % _numItems;
  1275. if (index >= j)
  1276. index = index - j;
  1277. else
  1278. index = _numItems - j + index;
  1279. }
  1280. else
  1281. index -= _firstIndex;
  1282. return index;
  1283. }
  1284. }
  1285. /// <summary>
  1286. /// Set the list to be virtual list.
  1287. /// 设置列表为虚拟列表模式。在虚拟列表模式下,列表不会为每一条列表数据创建一个实体对象,而是根据视口大小创建最小量的显示对象,然后通过itemRenderer指定的回调函数设置列表数据。
  1288. /// 在虚拟模式下,你不能通过AddChild、RemoveChild等方式管理列表,只能通过设置numItems设置列表数据的长度。
  1289. /// 如果要刷新列表,可以通过重新设置numItems,或者调用RefreshVirtualList完成。
  1290. /// ‘单行’或者‘单列’的列表布局可支持不等高的列表项目。
  1291. /// 除了‘页面’的列表布局,其他布局均支持使用不同资源构建列表项目,你可以在itemProvider里返回。如果不提供,默认使用defaultItem。
  1292. /// </summary>
  1293. public void SetVirtual()
  1294. {
  1295. SetVirtual(false);
  1296. }
  1297. public bool isVirtual
  1298. {
  1299. get { return _virtual; }
  1300. }
  1301. /// <summary>
  1302. /// Set the list to be virtual list, and has loop behavior.
  1303. /// </summary>
  1304. public void SetVirtualAndLoop()
  1305. {
  1306. SetVirtual(true);
  1307. }
  1308. void SetVirtual(bool loop)
  1309. {
  1310. if (!_virtual)
  1311. {
  1312. if (this.scrollPane == null)
  1313. Debug.LogError("FairyGUI: Virtual list must be scrollable!");
  1314. if (loop)
  1315. {
  1316. if (_layout == ListLayoutType.FlowHorizontal || _layout == ListLayoutType.FlowVertical)
  1317. Debug.LogError("FairyGUI: Loop list is not supported for FlowHorizontal or FlowVertical layout!");
  1318. this.scrollPane.bouncebackEffect = false;
  1319. }
  1320. _virtual = true;
  1321. _loop = loop;
  1322. _virtualItems = new List<ItemInfo>();
  1323. RemoveChildrenToPool();
  1324. if (_itemSize.x == 0 || _itemSize.y == 0)
  1325. {
  1326. GObject obj = GetFromPool(null);
  1327. if (obj == null)
  1328. {
  1329. Debug.LogError("FairyGUI: Virtual List must have a default list item resource.");
  1330. _itemSize = new Vector2(100, 100);
  1331. }
  1332. else
  1333. {
  1334. _itemSize = obj.size;
  1335. _itemSize.x = Mathf.CeilToInt(_itemSize.x);
  1336. _itemSize.y = Mathf.CeilToInt(_itemSize.y);
  1337. ReturnToPool(obj);
  1338. }
  1339. }
  1340. if (_layout == ListLayoutType.SingleColumn || _layout == ListLayoutType.FlowHorizontal)
  1341. {
  1342. this.scrollPane.scrollStep = _itemSize.y;
  1343. if (_loop)
  1344. this.scrollPane._loop = 2;
  1345. }
  1346. else
  1347. {
  1348. this.scrollPane.scrollStep = _itemSize.x;
  1349. if (_loop)
  1350. this.scrollPane._loop = 1;
  1351. }
  1352. this.scrollPane.onScroll.AddCapture(__scrolled);
  1353. SetVirtualListChangedFlag(true);
  1354. }
  1355. }
  1356. /// <summary>
  1357. /// Set the list item count.
  1358. /// If the list is not virtual, specified number of items will be created.
  1359. /// If the list is virtual, only items in view will be created.
  1360. /// </summary>
  1361. public int numItems
  1362. {
  1363. get
  1364. {
  1365. if (_virtual)
  1366. return _numItems;
  1367. else
  1368. return _children.Count;
  1369. }
  1370. set
  1371. {
  1372. if (_virtual)
  1373. {
  1374. if (itemRenderer == null)
  1375. throw new Exception("FairyGUI: Set itemRenderer first!");
  1376. _numItems = value;
  1377. if (_loop)
  1378. _realNumItems = _numItems * 6;//设置6倍数量,用于循环滚动
  1379. else
  1380. _realNumItems = _numItems;
  1381. //_virtualItems的设计是只增不减的
  1382. int oldCount = _virtualItems.Count;
  1383. if (_realNumItems > oldCount)
  1384. {
  1385. for (int i = oldCount; i < _realNumItems; i++)
  1386. {
  1387. ItemInfo ii = new ItemInfo();
  1388. ii.size = _itemSize;
  1389. _virtualItems.Add(ii);
  1390. }
  1391. }
  1392. else
  1393. {
  1394. for (int i = _realNumItems; i < oldCount; i++)
  1395. _virtualItems[i].selected = false;
  1396. }
  1397. if (_virtualListChanged != 0)
  1398. Timers.inst.Remove(this.RefreshVirtualList);
  1399. //立即刷新
  1400. this.RefreshVirtualList(null);
  1401. }
  1402. else
  1403. {
  1404. int cnt = _children.Count;
  1405. if (value > cnt)
  1406. {
  1407. for (int i = cnt; i < value; i++)
  1408. {
  1409. if (itemProvider == null)
  1410. AddItemFromPool();
  1411. else
  1412. AddItemFromPool(itemProvider(i));
  1413. }
  1414. }
  1415. else
  1416. {
  1417. RemoveChildrenToPool(value, cnt);
  1418. }
  1419. if (itemRenderer != null)
  1420. {
  1421. for (int i = 0; i < value; i++)
  1422. itemRenderer(i, GetChildAt(i));
  1423. }
  1424. }
  1425. }
  1426. }
  1427. public void RefreshVirtualList()
  1428. {
  1429. if (!_virtual)
  1430. throw new Exception("FairyGUI: not virtual list");
  1431. SetVirtualListChangedFlag(false);
  1432. }
  1433. void CheckVirtualList()
  1434. {
  1435. if (_virtualListChanged != 0)
  1436. {
  1437. this.RefreshVirtualList(null);
  1438. Timers.inst.Remove(this.RefreshVirtualList);
  1439. }
  1440. }
  1441. void SetVirtualListChangedFlag(bool layoutChanged)
  1442. {
  1443. if (layoutChanged)
  1444. _virtualListChanged = 2;
  1445. else if (_virtualListChanged == 0)
  1446. _virtualListChanged = 1;
  1447. Timers.inst.CallLater(RefreshVirtualList);
  1448. }
  1449. void RefreshVirtualList(object param)
  1450. {
  1451. bool layoutChanged = _virtualListChanged == 2;
  1452. _virtualListChanged = 0;
  1453. _miscFlags |= 1;
  1454. if (layoutChanged)
  1455. {
  1456. if (_layout == ListLayoutType.SingleColumn || _layout == ListLayoutType.SingleRow)
  1457. _curLineItemCount = 1;
  1458. else if (_layout == ListLayoutType.FlowHorizontal)
  1459. {
  1460. if (_columnCount > 0)
  1461. _curLineItemCount = _columnCount;
  1462. else
  1463. {
  1464. _curLineItemCount = Mathf.FloorToInt((this.scrollPane.viewWidth + _columnGap) / (_itemSize.x + _columnGap));
  1465. if (_curLineItemCount <= 0)
  1466. _curLineItemCount = 1;
  1467. }
  1468. }
  1469. else if (_layout == ListLayoutType.FlowVertical)
  1470. {
  1471. if (_lineCount > 0)
  1472. _curLineItemCount = _lineCount;
  1473. else
  1474. {
  1475. _curLineItemCount = Mathf.FloorToInt((this.scrollPane.viewHeight + _lineGap) / (_itemSize.y + _lineGap));
  1476. if (_curLineItemCount <= 0)
  1477. _curLineItemCount = 1;
  1478. }
  1479. }
  1480. else //pagination
  1481. {
  1482. if (_columnCount > 0)
  1483. _curLineItemCount = _columnCount;
  1484. else
  1485. {
  1486. _curLineItemCount = Mathf.FloorToInt((this.scrollPane.viewWidth + _columnGap) / (_itemSize.x + _columnGap));
  1487. if (_curLineItemCount <= 0)
  1488. _curLineItemCount = 1;
  1489. }
  1490. if (_lineCount > 0)
  1491. _curLineItemCount2 = _lineCount;
  1492. else
  1493. {
  1494. _curLineItemCount2 = Mathf.FloorToInt((this.scrollPane.viewHeight + _lineGap) / (_itemSize.y + _lineGap));
  1495. if (_curLineItemCount2 <= 0)
  1496. _curLineItemCount2 = 1;
  1497. }
  1498. }
  1499. }
  1500. float ch = 0, cw = 0;
  1501. if (_realNumItems > 0)
  1502. {
  1503. int len = Mathf.CeilToInt((float)_realNumItems / _curLineItemCount) * _curLineItemCount;
  1504. int len2 = Math.Min(_curLineItemCount, _realNumItems);
  1505. if (_layout == ListLayoutType.SingleColumn || _layout == ListLayoutType.FlowHorizontal)
  1506. {
  1507. for (int i = 0; i < len; i += _curLineItemCount)
  1508. ch += _virtualItems[i].size.y + _lineGap;
  1509. if (ch > 0)
  1510. ch -= _lineGap;
  1511. if (_autoResizeItem)
  1512. cw = scrollPane.viewWidth;
  1513. else
  1514. {
  1515. for (int i = 0; i < len2; i++)
  1516. cw += _virtualItems[i].size.x + _columnGap;
  1517. if (cw > 0)
  1518. cw -= _columnGap;
  1519. }
  1520. }
  1521. else if (_layout == ListLayoutType.SingleRow || _layout == ListLayoutType.FlowVertical)
  1522. {
  1523. for (int i = 0; i < len; i += _curLineItemCount)
  1524. cw += _virtualItems[i].size.x + _columnGap;
  1525. if (cw > 0)
  1526. cw -= _columnGap;
  1527. if (_autoResizeItem)
  1528. ch = this.scrollPane.viewHeight;
  1529. else
  1530. {
  1531. for (int i = 0; i < len2; i++)
  1532. ch += _virtualItems[i].size.y + _lineGap;
  1533. if (ch > 0)
  1534. ch -= _lineGap;
  1535. }
  1536. }
  1537. else
  1538. {
  1539. int pageCount = Mathf.CeilToInt((float)len / (_curLineItemCount * _curLineItemCount2));
  1540. cw = pageCount * viewWidth;
  1541. ch = viewHeight;
  1542. }
  1543. }
  1544. HandleAlign(cw, ch);
  1545. this.scrollPane.SetContentSize(cw, ch);
  1546. _miscFlags &= 0xFE;
  1547. HandleScroll(true);
  1548. }
  1549. void __scrolled(EventContext context)
  1550. {
  1551. HandleScroll(false);
  1552. }
  1553. int GetIndexOnPos1(ref float pos, bool forceUpdate)
  1554. {
  1555. if (_realNumItems < _curLineItemCount)
  1556. {
  1557. pos = 0;
  1558. return 0;
  1559. }
  1560. if (numChildren > 0 && !forceUpdate)
  1561. {
  1562. float pos2 = this.GetChildAt(0).y;
  1563. if (pos2 + (_lineGap > 0 ? 0 : -_lineGap) > pos)
  1564. {
  1565. for (int i = _firstIndex - _curLineItemCount; i >= 0; i -= _curLineItemCount)
  1566. {
  1567. pos2 -= (_virtualItems[i].size.y + _lineGap);
  1568. if (pos2 <= pos)
  1569. {
  1570. pos = pos2;
  1571. return i;
  1572. }
  1573. }
  1574. pos = 0;
  1575. return 0;
  1576. }
  1577. else
  1578. {
  1579. float testGap = _lineGap > 0 ? _lineGap : 0;
  1580. for (int i = _firstIndex; i < _realNumItems; i += _curLineItemCount)
  1581. {
  1582. float pos3 = pos2 + _virtualItems[i].size.y;
  1583. if (pos3 + testGap > pos)
  1584. {
  1585. pos = pos2;
  1586. return i;
  1587. }
  1588. pos2 = pos3 + _lineGap;
  1589. }
  1590. pos = pos2;
  1591. return _realNumItems - _curLineItemCount;
  1592. }
  1593. }
  1594. else
  1595. {
  1596. float pos2 = 0;
  1597. float testGap = _lineGap > 0 ? _lineGap : 0;
  1598. for (int i = 0; i < _realNumItems; i += _curLineItemCount)
  1599. {
  1600. float pos3 = pos2 + _virtualItems[i].size.y;
  1601. if (pos3 + testGap > pos)
  1602. {
  1603. pos = pos2;
  1604. return i;
  1605. }
  1606. pos2 = pos3 + _lineGap;
  1607. }
  1608. pos = pos2;
  1609. return _realNumItems - _curLineItemCount;
  1610. }
  1611. }
  1612. int GetIndexOnPos2(ref float pos, bool forceUpdate)
  1613. {
  1614. if (_realNumItems < _curLineItemCount)
  1615. {
  1616. pos = 0;
  1617. return 0;
  1618. }
  1619. if (numChildren > 0 && !forceUpdate)
  1620. {
  1621. float pos2 = this.GetChildAt(0).x;
  1622. if (pos2 + (_columnGap > 0 ? 0 : -_columnGap) > pos)
  1623. {
  1624. for (int i = _firstIndex - _curLineItemCount; i >= 0; i -= _curLineItemCount)
  1625. {
  1626. pos2 -= (_virtualItems[i].size.x + _columnGap);
  1627. if (pos2 <= pos)
  1628. {
  1629. pos = pos2;
  1630. return i;
  1631. }
  1632. }
  1633. pos = 0;
  1634. return 0;
  1635. }
  1636. else
  1637. {
  1638. float testGap = _columnGap > 0 ? _columnGap : 0;
  1639. for (int i = _firstIndex; i < _realNumItems; i += _curLineItemCount)
  1640. {
  1641. float pos3 = pos2 + _virtualItems[i].size.x;
  1642. if (pos3 + testGap > pos)
  1643. {
  1644. pos = pos2;
  1645. return i;
  1646. }
  1647. pos2 = pos3 + _columnGap;
  1648. }
  1649. pos = pos2;
  1650. return _realNumItems - _curLineItemCount;
  1651. }
  1652. }
  1653. else
  1654. {
  1655. float pos2 = 0;
  1656. float testGap = _columnGap > 0 ? _columnGap : 0;
  1657. for (int i = 0; i < _realNumItems; i += _curLineItemCount)
  1658. {
  1659. float pos3 = pos2 + _virtualItems[i].size.x;
  1660. if (pos3 + testGap > pos)
  1661. {
  1662. pos = pos2;
  1663. return i;
  1664. }
  1665. pos2 = pos3 + _columnGap;
  1666. }
  1667. pos = pos2;
  1668. return _realNumItems - _curLineItemCount;
  1669. }
  1670. }
  1671. int GetIndexOnPos3(ref float pos, bool forceUpdate)
  1672. {
  1673. if (_realNumItems < _curLineItemCount)
  1674. {
  1675. pos = 0;
  1676. return 0;
  1677. }
  1678. float viewWidth = this.viewWidth;
  1679. int page = Mathf.FloorToInt(pos / viewWidth);
  1680. int startIndex = page * (_curLineItemCount * _curLineItemCount2);
  1681. float pos2 = page * viewWidth;
  1682. float testGap = _columnGap > 0 ? _columnGap : 0;
  1683. for (int i = 0; i < _curLineItemCount; i++)
  1684. {
  1685. float pos3 = pos2 + _virtualItems[startIndex + i].size.x;
  1686. if (pos3 + testGap > pos)
  1687. {
  1688. pos = pos2;
  1689. return startIndex + i;
  1690. }
  1691. pos2 = pos3 + _columnGap;
  1692. }
  1693. pos = pos2;
  1694. return startIndex + _curLineItemCount - 1;
  1695. }
  1696. void HandleScroll(bool forceUpdate)
  1697. {
  1698. if ((_miscFlags & 1) != 0)
  1699. return;
  1700. if (_layout == ListLayoutType.SingleColumn || _layout == ListLayoutType.FlowHorizontal)
  1701. {
  1702. int enterCounter = 0;
  1703. while (HandleScroll1(forceUpdate))
  1704. {
  1705. //可能会因为ITEM资源改变导致ITEM大小发生改变,所有出现最后一页填不满的情况,这时要反复尝试填满。
  1706. enterCounter++;
  1707. forceUpdate = false;
  1708. if (enterCounter > 20)
  1709. {
  1710. Debug.Log("FairyGUI: list will never be filled as the item renderer function always returns a different size.");
  1711. break;
  1712. }
  1713. }
  1714. HandleArchOrder1();
  1715. }
  1716. else if (_layout == ListLayoutType.SingleRow || _layout == ListLayoutType.FlowVertical)
  1717. {
  1718. int enterCounter = 0;
  1719. while (HandleScroll2(forceUpdate))
  1720. {
  1721. enterCounter++;
  1722. forceUpdate = false;
  1723. if (enterCounter > 20)
  1724. {
  1725. Debug.Log("FairyGUI: list will never be filled as the item renderer function always returns a different size.");
  1726. break;
  1727. }
  1728. }
  1729. HandleArchOrder2();
  1730. }
  1731. else
  1732. {
  1733. HandleScroll3(forceUpdate);
  1734. }
  1735. _boundsChanged = false;
  1736. }
  1737. bool HandleScroll1(bool forceUpdate)
  1738. {
  1739. float pos = scrollPane.scrollingPosY;
  1740. float max = pos + scrollPane.viewHeight;
  1741. bool end = max == scrollPane.contentHeight;//这个标志表示当前需要滚动到最末,无论内容变化大小
  1742. //寻找当前位置的第一条项目
  1743. int newFirstIndex = GetIndexOnPos1(ref pos, forceUpdate);
  1744. if (newFirstIndex == _firstIndex && !forceUpdate)
  1745. return false;
  1746. int oldFirstIndex = _firstIndex;
  1747. _firstIndex = newFirstIndex;
  1748. int curIndex = newFirstIndex;
  1749. bool forward = oldFirstIndex > newFirstIndex;
  1750. int childCount = this.numChildren;
  1751. int lastIndex = oldFirstIndex + childCount - 1;
  1752. int reuseIndex = forward ? lastIndex : oldFirstIndex;
  1753. float curX = 0, curY = pos;
  1754. bool needRender;
  1755. float deltaSize = 0;
  1756. float firstItemDeltaSize = 0;
  1757. string url = defaultItem;
  1758. int partSize = (int)((scrollPane.viewWidth - _columnGap * (_curLineItemCount - 1)) / _curLineItemCount);
  1759. itemInfoVer++;
  1760. while (curIndex < _realNumItems && (end || curY < max))
  1761. {
  1762. ItemInfo ii = _virtualItems[curIndex];
  1763. if (ii.obj == null || forceUpdate)
  1764. {
  1765. if (itemProvider != null)
  1766. {
  1767. url = itemProvider(curIndex % _numItems);
  1768. if (url == null)
  1769. url = defaultItem;
  1770. url = UIPackage.NormalizeURL(url);
  1771. }
  1772. if (ii.obj != null && ii.obj.resourceURL != url)
  1773. {
  1774. if (ii.obj is GButton)
  1775. ii.selected = ((GButton)ii.obj).selected;
  1776. RemoveChildToPool(ii.obj);
  1777. ii.obj = null;
  1778. }
  1779. }
  1780. if (ii.obj == null)
  1781. {
  1782. //搜索最适合的重用item,保证每次刷新需要新建或者重新render的item最少
  1783. if (forward)
  1784. {
  1785. for (int j = reuseIndex; j >= oldFirstIndex; j--)
  1786. {
  1787. ItemInfo ii2 = _virtualItems[j];
  1788. if (ii2.obj != null && ii2.updateFlag != itemInfoVer && ii2.obj.resourceURL == url)
  1789. {
  1790. if (ii2.obj is GButton)
  1791. ii2.selected = ((GButton)ii2.obj).selected;
  1792. ii.obj = ii2.obj;
  1793. ii2.obj = null;
  1794. if (j == reuseIndex)
  1795. reuseIndex--;
  1796. break;
  1797. }
  1798. }
  1799. }
  1800. else
  1801. {
  1802. for (int j = reuseIndex; j <= lastIndex; j++)
  1803. {
  1804. ItemInfo ii2 = _virtualItems[j];
  1805. if (ii2.obj != null && ii2.updateFlag != itemInfoVer && ii2.obj.resourceURL == url)
  1806. {
  1807. if (ii2.obj is GButton)
  1808. ii2.selected = ((GButton)ii2.obj).selected;
  1809. ii.obj = ii2.obj;
  1810. ii2.obj = null;
  1811. if (j == reuseIndex)
  1812. reuseIndex++;
  1813. break;
  1814. }
  1815. }
  1816. }
  1817. if (ii.obj != null)
  1818. {
  1819. SetChildIndex(ii.obj, forward ? curIndex - newFirstIndex : numChildren);
  1820. }
  1821. else
  1822. {
  1823. ii.obj = _pool.GetObject(url);
  1824. if (forward)
  1825. this.AddChildAt(ii.obj, curIndex - newFirstIndex);
  1826. else
  1827. this.AddChild(ii.obj);
  1828. }
  1829. if (ii.obj is GButton)
  1830. ((GButton)ii.obj).selected = ii.selected;
  1831. needRender = true;
  1832. }
  1833. else
  1834. needRender = forceUpdate;
  1835. if (needRender)
  1836. {
  1837. if (_autoResizeItem && (_layout == ListLayoutType.SingleColumn || _columnCount > 0))
  1838. ii.obj.SetSize(partSize, ii.obj.height, true);
  1839. itemRenderer(curIndex % _numItems, ii.obj);
  1840. if (curIndex % _curLineItemCount == 0)
  1841. {
  1842. deltaSize += Mathf.CeilToInt(ii.obj.size.y) - ii.size.y;
  1843. if (curIndex == newFirstIndex && oldFirstIndex > newFirstIndex)
  1844. {
  1845. //当内容向下滚动时,如果新出现的项目大小发生变化,需要做一个位置补偿,才不会导致滚动跳动
  1846. firstItemDeltaSize = Mathf.CeilToInt(ii.obj.size.y) - ii.size.y;
  1847. }
  1848. }
  1849. ii.size.x = Mathf.CeilToInt(ii.obj.size.x);
  1850. ii.size.y = Mathf.CeilToInt(ii.obj.size.y);
  1851. }
  1852. ii.updateFlag = itemInfoVer;
  1853. ii.obj.SetXY(curX, curY);
  1854. if (curIndex == newFirstIndex) //要显示多一条才不会穿帮
  1855. max += ii.size.y;
  1856. curX += ii.size.x + _columnGap;
  1857. if (curIndex % _curLineItemCount == _curLineItemCount - 1)
  1858. {
  1859. curX = 0;
  1860. curY += ii.size.y + _lineGap;
  1861. }
  1862. curIndex++;
  1863. }
  1864. for (int i = 0; i < childCount; i++)
  1865. {
  1866. ItemInfo ii = _virtualItems[oldFirstIndex + i];
  1867. if (ii.updateFlag != itemInfoVer && ii.obj != null)
  1868. {
  1869. if (ii.obj is GButton)
  1870. ii.selected = ((GButton)ii.obj).selected;
  1871. RemoveChildToPool(ii.obj);
  1872. ii.obj = null;
  1873. }
  1874. }
  1875. childCount = _children.Count;
  1876. for (int i = 0; i < childCount; i++)
  1877. {
  1878. GObject obj = _virtualItems[newFirstIndex + i].obj;
  1879. if (_children[i] != obj)
  1880. SetChildIndex(obj, i);
  1881. }
  1882. if (deltaSize != 0 || firstItemDeltaSize != 0)
  1883. this.scrollPane.ChangeContentSizeOnScrolling(0, deltaSize, 0, firstItemDeltaSize);
  1884. if (curIndex > 0 && this.numChildren > 0 && this.container.y <= 0 && GetChildAt(0).y > -this.container.y)//最后一页没填满!
  1885. return true;
  1886. else
  1887. return false;
  1888. }
  1889. bool HandleScroll2(bool forceUpdate)
  1890. {
  1891. float pos = scrollPane.scrollingPosX;
  1892. float max = pos + scrollPane.viewWidth;
  1893. bool end = pos == scrollPane.contentWidth;//这个标志表示当前需要滚动到最末,无论内容变化大小
  1894. //寻找当前位置的第一条项目
  1895. int newFirstIndex = GetIndexOnPos2(ref pos, forceUpdate);
  1896. if (newFirstIndex == _firstIndex && !forceUpdate)
  1897. return false;
  1898. int oldFirstIndex = _firstIndex;
  1899. _firstIndex = newFirstIndex;
  1900. int curIndex = newFirstIndex;
  1901. bool forward = oldFirstIndex > newFirstIndex;
  1902. int childCount = this.numChildren;
  1903. int lastIndex = oldFirstIndex + childCount - 1;
  1904. int reuseIndex = forward ? lastIndex : oldFirstIndex;
  1905. float curX = pos, curY = 0;
  1906. bool needRender;
  1907. float deltaSize = 0;
  1908. float firstItemDeltaSize = 0;
  1909. string url = defaultItem;
  1910. int partSize = (int)((scrollPane.viewHeight - _lineGap * (_curLineItemCount - 1)) / _curLineItemCount);
  1911. itemInfoVer++;
  1912. while (curIndex < _realNumItems && (end || curX < max))
  1913. {
  1914. ItemInfo ii = _virtualItems[curIndex];
  1915. if (ii.obj == null || forceUpdate)
  1916. {
  1917. if (itemProvider != null)
  1918. {
  1919. url = itemProvider(curIndex % _numItems);
  1920. if (url == null)
  1921. url = defaultItem;
  1922. url = UIPackage.NormalizeURL(url);
  1923. }
  1924. if (ii.obj != null && ii.obj.resourceURL != url)
  1925. {
  1926. if (ii.obj is GButton)
  1927. ii.selected = ((GButton)ii.obj).selected;
  1928. RemoveChildToPool(ii.obj);
  1929. ii.obj = null;
  1930. }
  1931. }
  1932. if (ii.obj == null)
  1933. {
  1934. if (forward)
  1935. {
  1936. for (int j = reuseIndex; j >= oldFirstIndex; j--)
  1937. {
  1938. ItemInfo ii2 = _virtualItems[j];
  1939. if (ii2.obj != null && ii2.updateFlag != itemInfoVer && ii2.obj.resourceURL == url)
  1940. {
  1941. if (ii2.obj is GButton)
  1942. ii2.selected = ((GButton)ii2.obj).selected;
  1943. ii.obj = ii2.obj;
  1944. ii2.obj = null;
  1945. if (j == reuseIndex)
  1946. reuseIndex--;
  1947. break;
  1948. }
  1949. }
  1950. }
  1951. else
  1952. {
  1953. for (int j = reuseIndex; j <= lastIndex; j++)
  1954. {
  1955. ItemInfo ii2 = _virtualItems[j];
  1956. if (ii2.obj != null && ii2.updateFlag != itemInfoVer && ii2.obj.resourceURL == url)
  1957. {
  1958. if (ii2.obj is GButton)
  1959. ii2.selected = ((GButton)ii2.obj).selected;
  1960. ii.obj = ii2.obj;
  1961. ii2.obj = null;
  1962. if (j == reuseIndex)
  1963. reuseIndex++;
  1964. break;
  1965. }
  1966. }
  1967. }
  1968. if (ii.obj != null)
  1969. {
  1970. SetChildIndex(ii.obj, forward ? curIndex - newFirstIndex : numChildren);
  1971. }
  1972. else
  1973. {
  1974. ii.obj = _pool.GetObject(url);
  1975. if (forward)
  1976. this.AddChildAt(ii.obj, curIndex - newFirstIndex);
  1977. else
  1978. this.AddChild(ii.obj);
  1979. }
  1980. if (ii.obj is GButton)
  1981. ((GButton)ii.obj).selected = ii.selected;
  1982. needRender = true;
  1983. }
  1984. else
  1985. needRender = forceUpdate;
  1986. if (needRender)
  1987. {
  1988. if (_autoResizeItem && (_layout == ListLayoutType.SingleRow || _lineCount > 0))
  1989. ii.obj.SetSize(ii.obj.width, partSize, true);
  1990. itemRenderer(curIndex % _numItems, ii.obj);
  1991. if (curIndex % _curLineItemCount == 0)
  1992. {
  1993. deltaSize += Mathf.CeilToInt(ii.obj.size.x) - ii.size.x;
  1994. if (curIndex == newFirstIndex && oldFirstIndex > newFirstIndex)
  1995. {
  1996. //当内容向下滚动时,如果新出现的一个项目大小发生变化,需要做一个位置补偿,才不会导致滚动跳动
  1997. firstItemDeltaSize = Mathf.CeilToInt(ii.obj.size.x) - ii.size.x;
  1998. }
  1999. }
  2000. ii.size.x = Mathf.CeilToInt(ii.obj.size.x);
  2001. ii.size.y = Mathf.CeilToInt(ii.obj.size.y);
  2002. }
  2003. ii.updateFlag = itemInfoVer;
  2004. ii.obj.SetXY(curX, curY);
  2005. if (curIndex == newFirstIndex) //要显示多一条才不会穿帮
  2006. max += ii.size.x;
  2007. curY += ii.size.y + _lineGap;
  2008. if (curIndex % _curLineItemCount == _curLineItemCount - 1)
  2009. {
  2010. curY = 0;
  2011. curX += ii.size.x + _columnGap;
  2012. }
  2013. curIndex++;
  2014. }
  2015. for (int i = 0; i < childCount; i++)
  2016. {
  2017. ItemInfo ii = _virtualItems[oldFirstIndex + i];
  2018. if (ii.updateFlag != itemInfoVer && ii.obj != null)
  2019. {
  2020. if (ii.obj is GButton)
  2021. ii.selected = ((GButton)ii.obj).selected;
  2022. RemoveChildToPool(ii.obj);
  2023. ii.obj = null;
  2024. }
  2025. }
  2026. childCount = _children.Count;
  2027. for (int i = 0; i < childCount; i++)
  2028. {
  2029. GObject obj = _virtualItems[newFirstIndex + i].obj;
  2030. if (_children[i] != obj)
  2031. SetChildIndex(obj, i);
  2032. }
  2033. if (deltaSize != 0 || firstItemDeltaSize != 0)
  2034. this.scrollPane.ChangeContentSizeOnScrolling(deltaSize, 0, firstItemDeltaSize, 0);
  2035. if (curIndex > 0 && this.numChildren > 0 && this.container.x <= 0 && GetChildAt(0).x > -this.container.x)//最后一页没填满!
  2036. return true;
  2037. else
  2038. return false;
  2039. }
  2040. void HandleScroll3(bool forceUpdate)
  2041. {
  2042. float pos = scrollPane.scrollingPosX;
  2043. //寻找当前位置的第一条项目
  2044. int newFirstIndex = GetIndexOnPos3(ref pos, forceUpdate);
  2045. if (newFirstIndex == _firstIndex && !forceUpdate)
  2046. return;
  2047. int oldFirstIndex = _firstIndex;
  2048. _firstIndex = newFirstIndex;
  2049. //分页模式不支持不等高,所以渲染满一页就好了
  2050. int reuseIndex = oldFirstIndex;
  2051. int virtualItemCount = _virtualItems.Count;
  2052. int pageSize = _curLineItemCount * _curLineItemCount2;
  2053. int startCol = newFirstIndex % _curLineItemCount;
  2054. float viewWidth = this.viewWidth;
  2055. int page = (int)(newFirstIndex / pageSize);
  2056. int startIndex = page * pageSize;
  2057. int lastIndex = startIndex + pageSize * 2; //测试两页
  2058. bool needRender;
  2059. string url = defaultItem;
  2060. int partWidth = (int)((scrollPane.viewWidth - _columnGap * (_curLineItemCount - 1)) / _curLineItemCount);
  2061. int partHeight = (int)((scrollPane.viewHeight - _lineGap * (_curLineItemCount2 - 1)) / _curLineItemCount2);
  2062. itemInfoVer++;
  2063. //先标记这次要用到的项目
  2064. for (int i = startIndex; i < lastIndex; i++)
  2065. {
  2066. if (i >= _realNumItems)
  2067. continue;
  2068. int col = i % _curLineItemCount;
  2069. if (i - startIndex < pageSize)
  2070. {
  2071. if (col < startCol)
  2072. continue;
  2073. }
  2074. else
  2075. {
  2076. if (col > startCol)
  2077. continue;
  2078. }
  2079. ItemInfo ii = _virtualItems[i];
  2080. ii.updateFlag = itemInfoVer;
  2081. }
  2082. GObject lastObj = null;
  2083. int insertIndex = 0;
  2084. for (int i = startIndex; i < lastIndex; i++)
  2085. {
  2086. if (i >= _realNumItems)
  2087. continue;
  2088. ItemInfo ii = _virtualItems[i];
  2089. if (ii.updateFlag != itemInfoVer)
  2090. continue;
  2091. if (ii.obj == null)
  2092. {
  2093. //寻找看有没有可重用的
  2094. while (reuseIndex < virtualItemCount)
  2095. {
  2096. ItemInfo ii2 = _virtualItems[reuseIndex];
  2097. if (ii2.obj != null && ii2.updateFlag != itemInfoVer)
  2098. {
  2099. if (ii2.obj is GButton)
  2100. ii2.selected = ((GButton)ii2.obj).selected;
  2101. ii.obj = ii2.obj;
  2102. ii2.obj = null;
  2103. break;
  2104. }
  2105. reuseIndex++;
  2106. }
  2107. if (insertIndex == -1)
  2108. insertIndex = GetChildIndex(lastObj) + 1;
  2109. if (ii.obj == null)
  2110. {
  2111. if (itemProvider != null)
  2112. {
  2113. url = itemProvider(i % _numItems);
  2114. if (url == null)
  2115. url = defaultItem;
  2116. url = UIPackage.NormalizeURL(url);
  2117. }
  2118. ii.obj = _pool.GetObject(url);
  2119. this.AddChildAt(ii.obj, insertIndex);
  2120. }
  2121. else
  2122. {
  2123. insertIndex = SetChildIndexBefore(ii.obj, insertIndex);
  2124. }
  2125. insertIndex++;
  2126. if (ii.obj is GButton)
  2127. ((GButton)ii.obj).selected = ii.selected;
  2128. needRender = true;
  2129. }
  2130. else
  2131. {
  2132. needRender = forceUpdate;
  2133. insertIndex = -1;
  2134. lastObj = ii.obj;
  2135. }
  2136. if (needRender)
  2137. {
  2138. if (_autoResizeItem)
  2139. {
  2140. if (_curLineItemCount == _columnCount && _curLineItemCount2 == _lineCount)
  2141. ii.obj.SetSize(partWidth, partHeight, true);
  2142. else if (_curLineItemCount == _columnCount)
  2143. ii.obj.SetSize(partWidth, ii.obj.height, true);
  2144. else if (_curLineItemCount2 == _lineCount)
  2145. ii.obj.SetSize(ii.obj.width, partHeight, true);
  2146. }
  2147. itemRenderer(i % _numItems, ii.obj);
  2148. ii.size.x = Mathf.CeilToInt(ii.obj.size.x);
  2149. ii.size.y = Mathf.CeilToInt(ii.obj.size.y);
  2150. }
  2151. }
  2152. //排列item
  2153. float borderX = (startIndex / pageSize) * viewWidth;
  2154. float xx = borderX;
  2155. float yy = 0;
  2156. float lineHeight = 0;
  2157. for (int i = startIndex; i < lastIndex; i++)
  2158. {
  2159. if (i >= _realNumItems)
  2160. continue;
  2161. ItemInfo ii = _virtualItems[i];
  2162. if (ii.updateFlag == itemInfoVer)
  2163. ii.obj.SetXY(xx, yy);
  2164. if (ii.size.y > lineHeight)
  2165. lineHeight = ii.size.y;
  2166. if (i % _curLineItemCount == _curLineItemCount - 1)
  2167. {
  2168. xx = borderX;
  2169. yy += lineHeight + _lineGap;
  2170. lineHeight = 0;
  2171. if (i == startIndex + pageSize - 1)
  2172. {
  2173. borderX += viewWidth;
  2174. xx = borderX;
  2175. yy = 0;
  2176. }
  2177. }
  2178. else
  2179. xx += ii.size.x + _columnGap;
  2180. }
  2181. //释放未使用的
  2182. for (int i = reuseIndex; i < virtualItemCount; i++)
  2183. {
  2184. ItemInfo ii = _virtualItems[i];
  2185. if (ii.updateFlag != itemInfoVer && ii.obj != null)
  2186. {
  2187. if (ii.obj is GButton)
  2188. ii.selected = ((GButton)ii.obj).selected;
  2189. RemoveChildToPool(ii.obj);
  2190. ii.obj = null;
  2191. }
  2192. }
  2193. }
  2194. void HandleArchOrder1()
  2195. {
  2196. if (this.childrenRenderOrder == ChildrenRenderOrder.Arch)
  2197. {
  2198. float mid = this.scrollPane.posY + this.viewHeight / 2;
  2199. float minDist = int.MaxValue, dist;
  2200. int apexIndex = 0;
  2201. int cnt = this.numChildren;
  2202. for (int i = 0; i < cnt; i++)
  2203. {
  2204. GObject obj = GetChildAt(i);
  2205. if (!foldInvisibleItems || obj.visible)
  2206. {
  2207. dist = Mathf.Abs(mid - obj.y - obj.height / 2);
  2208. if (dist < minDist)
  2209. {
  2210. minDist = dist;
  2211. apexIndex = i;
  2212. }
  2213. }
  2214. }
  2215. this.apexIndex = apexIndex;
  2216. }
  2217. }
  2218. void HandleArchOrder2()
  2219. {
  2220. if (this.childrenRenderOrder == ChildrenRenderOrder.Arch)
  2221. {
  2222. float mid = this.scrollPane.posX + this.viewWidth / 2;
  2223. float minDist = int.MaxValue, dist;
  2224. int apexIndex = 0;
  2225. int cnt = this.numChildren;
  2226. for (int i = 0; i < cnt; i++)
  2227. {
  2228. GObject obj = GetChildAt(i);
  2229. if (!foldInvisibleItems || obj.visible)
  2230. {
  2231. dist = Mathf.Abs(mid - obj.x - obj.width / 2);
  2232. if (dist < minDist)
  2233. {
  2234. minDist = dist;
  2235. apexIndex = i;
  2236. }
  2237. }
  2238. }
  2239. this.apexIndex = apexIndex;
  2240. }
  2241. }
  2242. override protected internal void GetSnappingPosition(ref float xValue, ref float yValue)
  2243. {
  2244. if (_virtual)
  2245. {
  2246. if (_layout == ListLayoutType.SingleColumn || _layout == ListLayoutType.FlowHorizontal)
  2247. {
  2248. float saved = yValue;
  2249. int index = GetIndexOnPos1(ref yValue, false);
  2250. if (index < _virtualItems.Count && saved - yValue > _virtualItems[index].size.y / 2 && index < _realNumItems)
  2251. yValue += _virtualItems[index].size.y + _lineGap;
  2252. }
  2253. else if (_layout == ListLayoutType.SingleRow || _layout == ListLayoutType.FlowVertical)
  2254. {
  2255. float saved = xValue;
  2256. int index = GetIndexOnPos2(ref xValue, false);
  2257. if (index < _virtualItems.Count && saved - xValue > _virtualItems[index].size.x / 2 && index < _realNumItems)
  2258. xValue += _virtualItems[index].size.x + _columnGap;
  2259. }
  2260. else
  2261. {
  2262. float saved = xValue;
  2263. int index = GetIndexOnPos3(ref xValue, false);
  2264. if (index < _virtualItems.Count && saved - xValue > _virtualItems[index].size.x / 2 && index < _realNumItems)
  2265. xValue += _virtualItems[index].size.x + _columnGap;
  2266. }
  2267. }
  2268. else
  2269. base.GetSnappingPosition(ref xValue, ref yValue);
  2270. }
  2271. private void HandleAlign(float contentWidth, float contentHeight)
  2272. {
  2273. Vector2 newOffset = Vector2.zero;
  2274. if (contentHeight < viewHeight)
  2275. {
  2276. if (_verticalAlign == VertAlignType.Middle)
  2277. newOffset.y = (int)((viewHeight - contentHeight) / 2);
  2278. else if (_verticalAlign == VertAlignType.Bottom)
  2279. newOffset.y = viewHeight - contentHeight;
  2280. }
  2281. if (contentWidth < this.viewWidth)
  2282. {
  2283. if (_align == AlignType.Center)
  2284. newOffset.x = (int)((viewWidth - contentWidth) / 2);
  2285. else if (_align == AlignType.Right)
  2286. newOffset.x = viewWidth - contentWidth;
  2287. }
  2288. if (newOffset != _alignOffset)
  2289. {
  2290. _alignOffset = newOffset;
  2291. if (scrollPane != null)
  2292. scrollPane.AdjustMaskContainer();
  2293. else
  2294. container.SetXY(_margin.left + _alignOffset.x, _margin.top + _alignOffset.y);
  2295. }
  2296. }
  2297. override protected void UpdateBounds()
  2298. {
  2299. if (_virtual)
  2300. return;
  2301. int cnt = _children.Count;
  2302. int i;
  2303. int j = 0;
  2304. GObject child;
  2305. float curX = 0;
  2306. float curY = 0;
  2307. float cw, ch;
  2308. float maxWidth = 0;
  2309. float maxHeight = 0;
  2310. float viewWidth = this.viewWidth;
  2311. float viewHeight = this.viewHeight;
  2312. if (_layout == ListLayoutType.SingleColumn)
  2313. {
  2314. for (i = 0; i < cnt; i++)
  2315. {
  2316. child = GetChildAt(i);
  2317. if (foldInvisibleItems && !child.visible)
  2318. continue;
  2319. if (curY != 0)
  2320. curY += _lineGap;
  2321. child.y = curY;
  2322. if (_autoResizeItem)
  2323. child.SetSize(viewWidth, child.height, true);
  2324. curY += Mathf.CeilToInt(child.height);
  2325. if (child.width > maxWidth)
  2326. maxWidth = child.width;
  2327. }
  2328. ch = curY;
  2329. if (ch <= viewHeight && _autoResizeItem && scrollPane != null && scrollPane._displayInDemand && scrollPane.vtScrollBar != null)
  2330. {
  2331. viewWidth += scrollPane.vtScrollBar.width;
  2332. for (i = 0; i < cnt; i++)
  2333. {
  2334. child = GetChildAt(i);
  2335. if (foldInvisibleItems && !child.visible)
  2336. continue;
  2337. child.SetSize(viewWidth, child.height, true);
  2338. if (child.width > maxWidth)
  2339. maxWidth = child.width;
  2340. }
  2341. }
  2342. cw = Mathf.CeilToInt(maxWidth);
  2343. }
  2344. else if (_layout == ListLayoutType.SingleRow)
  2345. {
  2346. for (i = 0; i < cnt; i++)
  2347. {
  2348. child = GetChildAt(i);
  2349. if (foldInvisibleItems && !child.visible)
  2350. continue;
  2351. if (curX != 0)
  2352. curX += _columnGap;
  2353. child.x = curX;
  2354. if (_autoResizeItem)
  2355. child.SetSize(child.width, viewHeight, true);
  2356. curX += Mathf.CeilToInt(child.width);
  2357. if (child.height > maxHeight)
  2358. maxHeight = child.height;
  2359. }
  2360. cw = curX;
  2361. if (cw <= viewWidth && _autoResizeItem && scrollPane != null && scrollPane._displayInDemand && scrollPane.hzScrollBar != null)
  2362. {
  2363. viewHeight += scrollPane.hzScrollBar.height;
  2364. for (i = 0; i < cnt; i++)
  2365. {
  2366. child = GetChildAt(i);
  2367. if (foldInvisibleItems && !child.visible)
  2368. continue;
  2369. child.SetSize(child.width, viewHeight, true);
  2370. if (child.height > maxHeight)
  2371. maxHeight = child.height;
  2372. }
  2373. }
  2374. ch = Mathf.CeilToInt(maxHeight);
  2375. }
  2376. else if (_layout == ListLayoutType.FlowHorizontal)
  2377. {
  2378. if (_autoResizeItem && _columnCount > 0)
  2379. {
  2380. float lineSize = 0;
  2381. int lineStart = 0;
  2382. float remainSize;
  2383. float remainPercent;
  2384. for (i = 0; i < cnt; i++)
  2385. {
  2386. child = GetChildAt(i);
  2387. if (foldInvisibleItems && !child.visible)
  2388. continue;
  2389. lineSize += child.sourceWidth;
  2390. j++;
  2391. if (j == _columnCount || i == cnt - 1)
  2392. {
  2393. remainSize = viewWidth - (j - 1) * _columnGap;
  2394. remainPercent = 1;
  2395. curX = 0;
  2396. for (j = lineStart; j <= i; j++)
  2397. {
  2398. child = GetChildAt(j);
  2399. if (foldInvisibleItems && !child.visible)
  2400. continue;
  2401. child.SetXY(curX, curY);
  2402. float perc = child.sourceWidth / lineSize;
  2403. child.SetSize(Mathf.Round(perc / remainPercent * remainSize), child.height, true);
  2404. remainSize -= child.width;
  2405. remainPercent -= perc;
  2406. curX += child.width + _columnGap;
  2407. if (child.height > maxHeight)
  2408. maxHeight = child.height;
  2409. }
  2410. //new line
  2411. curY += Mathf.CeilToInt(maxHeight) + _lineGap;
  2412. maxHeight = 0;
  2413. j = 0;
  2414. lineStart = i + 1;
  2415. lineSize = 0;
  2416. }
  2417. }
  2418. ch = curY + Mathf.CeilToInt(maxHeight);
  2419. cw = viewWidth;
  2420. }
  2421. else
  2422. {
  2423. for (i = 0; i < cnt; i++)
  2424. {
  2425. child = GetChildAt(i);
  2426. if (foldInvisibleItems && !child.visible)
  2427. continue;
  2428. if (curX != 0)
  2429. curX += _columnGap;
  2430. if (_columnCount != 0 && j >= _columnCount
  2431. || _columnCount == 0 && curX + child.width > viewWidth && maxHeight != 0)
  2432. {
  2433. //new line
  2434. curX = 0;
  2435. curY += Mathf.CeilToInt(maxHeight) + _lineGap;
  2436. maxHeight = 0;
  2437. j = 0;
  2438. }
  2439. child.SetXY(curX, curY);
  2440. curX += Mathf.CeilToInt(child.width);
  2441. if (curX > maxWidth)
  2442. maxWidth = curX;
  2443. if (child.height > maxHeight)
  2444. maxHeight = child.height;
  2445. j++;
  2446. }
  2447. ch = curY + Mathf.CeilToInt(maxHeight);
  2448. cw = Mathf.CeilToInt(maxWidth);
  2449. }
  2450. }
  2451. else if (_layout == ListLayoutType.FlowVertical)
  2452. {
  2453. if (_autoResizeItem && _lineCount > 0)
  2454. {
  2455. float lineSize = 0;
  2456. int lineStart = 0;
  2457. float remainSize;
  2458. float remainPercent;
  2459. for (i = 0; i < cnt; i++)
  2460. {
  2461. child = GetChildAt(i);
  2462. if (foldInvisibleItems && !child.visible)
  2463. continue;
  2464. lineSize += child.sourceHeight;
  2465. j++;
  2466. if (j == _lineCount || i == cnt - 1)
  2467. {
  2468. remainSize = viewHeight - (j - 1) * _lineGap;
  2469. remainPercent = 1;
  2470. curY = 0;
  2471. for (j = lineStart; j <= i; j++)
  2472. {
  2473. child = GetChildAt(j);
  2474. if (foldInvisibleItems && !child.visible)
  2475. continue;
  2476. child.SetXY(curX, curY);
  2477. float perc = child.sourceHeight / lineSize;
  2478. child.SetSize(child.width, Mathf.Round(perc / remainPercent * remainSize), true);
  2479. remainSize -= child.height;
  2480. remainPercent -= perc;
  2481. curY += child.height + _lineGap;
  2482. if (child.width > maxWidth)
  2483. maxWidth = child.width;
  2484. }
  2485. //new line
  2486. curX += Mathf.CeilToInt(maxWidth) + _columnGap;
  2487. maxWidth = 0;
  2488. j = 0;
  2489. lineStart = i + 1;
  2490. lineSize = 0;
  2491. }
  2492. }
  2493. cw = curX + Mathf.CeilToInt(maxWidth);
  2494. ch = viewHeight;
  2495. }
  2496. else
  2497. {
  2498. for (i = 0; i < cnt; i++)
  2499. {
  2500. child = GetChildAt(i);
  2501. if (foldInvisibleItems && !child.visible)
  2502. continue;
  2503. if (curY != 0)
  2504. curY += _lineGap;
  2505. if (_lineCount != 0 && j >= _lineCount
  2506. || _lineCount == 0 && curY + child.height > viewHeight && maxWidth != 0)
  2507. {
  2508. curY = 0;
  2509. curX += Mathf.CeilToInt(maxWidth) + _columnGap;
  2510. maxWidth = 0;
  2511. j = 0;
  2512. }
  2513. child.SetXY(curX, curY);
  2514. curY += child.height;
  2515. if (curY > maxHeight)
  2516. maxHeight = curY;
  2517. if (child.width > maxWidth)
  2518. maxWidth = child.width;
  2519. j++;
  2520. }
  2521. cw = curX + Mathf.CeilToInt(maxWidth);
  2522. ch = Mathf.CeilToInt(maxHeight);
  2523. }
  2524. }
  2525. else //pagination
  2526. {
  2527. int page = 0;
  2528. int k = 0;
  2529. float eachHeight = 0;
  2530. if (_autoResizeItem && _lineCount > 0)
  2531. eachHeight = Mathf.Floor((viewHeight - (_lineCount - 1) * _lineGap) / _lineCount);
  2532. if (_autoResizeItem && _columnCount > 0)
  2533. {
  2534. float lineSize = 0;
  2535. int lineStart = 0;
  2536. float remainSize;
  2537. float remainPercent;
  2538. for (i = 0; i < cnt; i++)
  2539. {
  2540. child = GetChildAt(i);
  2541. if (foldInvisibleItems && !child.visible)
  2542. continue;
  2543. if (j == 0 && (_lineCount != 0 && k >= _lineCount
  2544. || _lineCount == 0 && curY + (_lineCount > 0 ? eachHeight : child.height) > viewHeight))
  2545. {
  2546. //new page
  2547. page++;
  2548. curY = 0;
  2549. k = 0;
  2550. }
  2551. lineSize += child.sourceWidth;
  2552. j++;
  2553. if (j == _columnCount || i == cnt - 1)
  2554. {
  2555. remainSize = viewWidth - (j - 1) * _columnGap;
  2556. remainPercent = 1;
  2557. curX = 0;
  2558. for (j = lineStart; j <= i; j++)
  2559. {
  2560. child = GetChildAt(j);
  2561. if (foldInvisibleItems && !child.visible)
  2562. continue;
  2563. child.SetXY(page * viewWidth + curX, curY);
  2564. float perc = child.sourceWidth / lineSize;
  2565. child.SetSize(Mathf.Round(perc / remainPercent * remainSize), _lineCount > 0 ? eachHeight : child.height, true);
  2566. remainSize -= child.width;
  2567. remainPercent -= perc;
  2568. curX += child.width + _columnGap;
  2569. if (child.height > maxHeight)
  2570. maxHeight = child.height;
  2571. }
  2572. //new line
  2573. curY += Mathf.CeilToInt(maxHeight) + _lineGap;
  2574. maxHeight = 0;
  2575. j = 0;
  2576. lineStart = i + 1;
  2577. lineSize = 0;
  2578. k++;
  2579. }
  2580. }
  2581. }
  2582. else
  2583. {
  2584. for (i = 0; i < cnt; i++)
  2585. {
  2586. child = GetChildAt(i);
  2587. if (foldInvisibleItems && !child.visible)
  2588. continue;
  2589. if (curX != 0)
  2590. curX += _columnGap;
  2591. if (_autoResizeItem && _lineCount > 0)
  2592. child.SetSize(child.width, eachHeight, true);
  2593. if (_columnCount != 0 && j >= _columnCount
  2594. || _columnCount == 0 && curX + child.width > viewWidth && maxHeight != 0)
  2595. {
  2596. curX = 0;
  2597. curY += maxHeight + _lineGap;
  2598. maxHeight = 0;
  2599. j = 0;
  2600. k++;
  2601. if (_lineCount != 0 && k >= _lineCount
  2602. || _lineCount == 0 && curY + child.height > viewHeight && maxWidth != 0)//new page
  2603. {
  2604. page++;
  2605. curY = 0;
  2606. k = 0;
  2607. }
  2608. }
  2609. child.SetXY(page * viewWidth + curX, curY);
  2610. curX += Mathf.CeilToInt(child.width);
  2611. if (curX > maxWidth)
  2612. maxWidth = curX;
  2613. if (child.height > maxHeight)
  2614. maxHeight = child.height;
  2615. j++;
  2616. }
  2617. }
  2618. ch = page > 0 ? viewHeight : (curY + Mathf.CeilToInt(maxHeight));
  2619. cw = (page + 1) * viewWidth;
  2620. }
  2621. HandleAlign(cw, ch);
  2622. SetBounds(0, 0, cw, ch);
  2623. InvalidateBatchingState(true);
  2624. }
  2625. override public void Setup_BeforeAdd(ByteBuffer buffer, int beginPos)
  2626. {
  2627. base.Setup_BeforeAdd(buffer, beginPos);
  2628. buffer.Seek(beginPos, 5);
  2629. _layout = (ListLayoutType)buffer.ReadByte();
  2630. selectionMode = (ListSelectionMode)buffer.ReadByte();
  2631. _align = (AlignType)buffer.ReadByte();
  2632. _verticalAlign = (VertAlignType)buffer.ReadByte();
  2633. _lineGap = buffer.ReadShort();
  2634. _columnGap = buffer.ReadShort();
  2635. _lineCount = buffer.ReadShort();
  2636. _columnCount = buffer.ReadShort();
  2637. _autoResizeItem = buffer.ReadBool();
  2638. _childrenRenderOrder = (ChildrenRenderOrder)buffer.ReadByte();
  2639. _apexIndex = buffer.ReadShort();
  2640. if (buffer.ReadBool())
  2641. {
  2642. _margin.top = buffer.ReadInt();
  2643. _margin.bottom = buffer.ReadInt();
  2644. _margin.left = buffer.ReadInt();
  2645. _margin.right = buffer.ReadInt();
  2646. }
  2647. OverflowType overflow = (OverflowType)buffer.ReadByte();
  2648. if (overflow == OverflowType.Scroll)
  2649. {
  2650. int savedPos = buffer.position;
  2651. buffer.Seek(beginPos, 7);
  2652. SetupScroll(buffer);
  2653. buffer.position = savedPos;
  2654. }
  2655. else
  2656. SetupOverflow(overflow);
  2657. if (buffer.ReadBool())
  2658. {
  2659. int i1 = buffer.ReadInt();
  2660. int i2 = buffer.ReadInt();
  2661. this.clipSoftness = new Vector2(i1, i2);
  2662. }
  2663. if (buffer.version >= 2)
  2664. {
  2665. scrollItemToViewOnClick = buffer.ReadBool();
  2666. foldInvisibleItems = buffer.ReadBool();
  2667. }
  2668. buffer.Seek(beginPos, 8);
  2669. defaultItem = buffer.ReadS();
  2670. ReadItems(buffer);
  2671. }
  2672. virtual protected void ReadItems(ByteBuffer buffer)
  2673. {
  2674. int itemCount = buffer.ReadShort();
  2675. for (int i = 0; i < itemCount; i++)
  2676. {
  2677. int nextPos = buffer.ReadShort();
  2678. nextPos += buffer.position;
  2679. string str = buffer.ReadS();
  2680. if (str == null)
  2681. {
  2682. str = defaultItem;
  2683. if (string.IsNullOrEmpty(str))
  2684. {
  2685. buffer.position = nextPos;
  2686. continue;
  2687. }
  2688. }
  2689. GObject obj = GetFromPool(str);
  2690. if (obj != null)
  2691. {
  2692. AddChild(obj);
  2693. SetupItem(buffer, obj);
  2694. }
  2695. buffer.position = nextPos;
  2696. }
  2697. }
  2698. protected void SetupItem(ByteBuffer buffer, GObject obj)
  2699. {
  2700. string str;
  2701. str = buffer.ReadS();
  2702. if (str != null)
  2703. obj.text = str;
  2704. str = buffer.ReadS();
  2705. if (str != null && (obj is GButton))
  2706. (obj as GButton).selectedTitle = str;
  2707. str = buffer.ReadS();
  2708. if (str != null)
  2709. obj.icon = str;
  2710. str = buffer.ReadS();
  2711. if (str != null && (obj is GButton))
  2712. (obj as GButton).selectedIcon = str;
  2713. str = buffer.ReadS();
  2714. if (str != null)
  2715. obj.name = str;
  2716. if (obj is GComponent)
  2717. {
  2718. int cnt = buffer.ReadShort();
  2719. for (int i = 0; i < cnt; i++)
  2720. {
  2721. Controller cc = ((GComponent)obj).GetController(buffer.ReadS());
  2722. str = buffer.ReadS();
  2723. if (cc != null)
  2724. cc.selectedPageId = str;
  2725. }
  2726. if (buffer.version >= 2)
  2727. {
  2728. cnt = buffer.ReadShort();
  2729. for (int i = 0; i < cnt; i++)
  2730. {
  2731. string target = buffer.ReadS();
  2732. int propertyId = buffer.ReadShort();
  2733. string value = buffer.ReadS();
  2734. GObject obj2 = ((GComponent)obj).GetChildByPath(target);
  2735. if (obj2 != null)
  2736. {
  2737. if (propertyId == 0)
  2738. obj2.text = value;
  2739. else if (propertyId == 1)
  2740. obj2.icon = value;
  2741. }
  2742. }
  2743. }
  2744. }
  2745. }
  2746. override public void Setup_AfterAdd(ByteBuffer buffer, int beginPos)
  2747. {
  2748. base.Setup_AfterAdd(buffer, beginPos);
  2749. buffer.Seek(beginPos, 6);
  2750. int i = buffer.ReadShort();
  2751. if (i != -1)
  2752. _selectionController = parent.GetControllerAt(i);
  2753. }
  2754. }
  2755. }