GList.cs 106 KB

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