1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067 |
- using System;
- using System.Collections.Generic;
- using FairyGUI.Utils;
- using UnityEngine;
- namespace FairyGUI
- {
- /// <summary>
- /// Callback function when an item is needed to update its look.
- /// </summary>
- /// <param name="index">Item index.</param>
- /// <param name="item">Item object.</param>
- public delegate void ListItemRenderer(int index, GObject item);
- /// <summary>
- ///
- /// </summary>
- /// <param name="index"></param>
- /// <returns></returns>
- public delegate string ListItemProvider(int index);
- /// <summary>
- /// GList class.
- /// </summary>
- public class GList : GComponent
- {
- /// <summary>
- /// 如果true,当item不可见时自动折叠,否则依然占位
- /// </summary>
- public bool foldInvisibleItems = false;
- /// <summary>
- /// List selection mode
- /// </summary>
- /// <seealso cref="ListSelectionMode"/>
- public ListSelectionMode selectionMode;
- /// <summary>
- /// Callback function when an item is needed to update its look.
- /// </summary>
- public ListItemRenderer itemRenderer;
- /// <summary>
- /// Callback funtion to return item resource url.
- /// </summary>
- public ListItemProvider itemProvider;
- /// <summary>
- ///
- /// </summary>
- public bool scrollItemToViewOnClick;
- string _defaultItem;
- ListLayoutType _layout;
- int _lineCount;
- int _columnCount;
- int _lineGap;
- int _columnGap;
- AlignType _align;
- VertAlignType _verticalAlign;
- bool _autoResizeItem;
- Controller _selectionController;
- GObjectPool _pool;
- int _lastSelectedIndex;
- EventListener _onClickItem;
- EventListener _onRightClickItem;
- //Virtual List support
- bool _virtual;
- bool _loop;
- int _numItems;
- int _realNumItems;
- int _firstIndex; //the top left index
- int _curLineItemCount; //item count in one line
- int _curLineItemCount2; //只用在页面模式,表示垂直方向的项目数
- Vector2 _itemSize;
- int _virtualListChanged; //1-content changed, 2-size changed
- uint itemInfoVer; //用来标志item是否在本次处理中已经被重用了
- int _miscFlags; //1-event locked, 2-focus events registered
- class ItemInfo
- {
- public Vector2 size;
- public GObject obj;
- public uint updateFlag;
- public bool selected;
- }
- List<ItemInfo> _virtualItems;
- EventCallback1 _itemClickDelegate;
- public GList()
- : base()
- {
- _trackBounds = true;
- this.opaque = true;
- scrollItemToViewOnClick = true;
- container = new Container();
- rootContainer.AddChild(container);
- rootContainer.gameObject.name = "GList";
- _pool = new GObjectPool(container.cachedTransform);
- _itemClickDelegate = __clickItem;
- }
- public override void Dispose()
- {
- _pool.Clear();
- if (_virtualListChanged != 0)
- Timers.inst.Remove(this.RefreshVirtualList);
- _selectionController = null;
- scrollItemToViewOnClick = false;
- itemRenderer = null;
- itemProvider = null;
- base.Dispose();
- }
- /// <summary>
- /// Dispatched when a list item being clicked.
- /// </summary>
- public EventListener onClickItem
- {
- get { return _onClickItem ?? (_onClickItem = new EventListener(this, "onClickItem")); }
- }
- /// <summary>
- /// Dispatched when a list item being clicked with right button.
- /// </summary>
- public EventListener onRightClickItem
- {
- get { return _onRightClickItem ?? (_onRightClickItem = new EventListener(this, "onRightClickItem")); }
- }
- /// <summary>
- /// Resource url of the default item.
- /// </summary>
- public string defaultItem
- {
- get { return _defaultItem; }
- set
- {
- _defaultItem = UIPackage.NormalizeURL(value);
- }
- }
- /// <summary>
- /// List layout type.
- /// </summary>
- public ListLayoutType layout
- {
- get { return _layout; }
- set
- {
- if (_layout != value)
- {
- _layout = value;
- SetBoundsChangedFlag();
- if (_virtual)
- SetVirtualListChangedFlag(true);
- }
- }
- }
- /// <summary>
- ///
- /// </summary>
- public int lineCount
- {
- get { return _lineCount; }
- set
- {
- if (_lineCount != value)
- {
- _lineCount = value;
- if (_layout == ListLayoutType.FlowVertical || _layout == ListLayoutType.Pagination)
- {
- SetBoundsChangedFlag();
- if (_virtual)
- SetVirtualListChangedFlag(true);
- }
- }
- }
- }
- /// <summary>
- ///
- /// </summary>
- public int columnCount
- {
- get { return _columnCount; }
- set
- {
- if (_columnCount != value)
- {
- _columnCount = value;
- if (_layout == ListLayoutType.FlowHorizontal || _layout == ListLayoutType.Pagination)
- {
- SetBoundsChangedFlag();
- if (_virtual)
- SetVirtualListChangedFlag(true);
- }
- }
- }
- }
- /// <summary>
- ///
- /// </summary>
- public int lineGap
- {
- get { return _lineGap; }
- set
- {
- if (_lineGap != value)
- {
- _lineGap = value;
- SetBoundsChangedFlag();
- if (_virtual)
- SetVirtualListChangedFlag(true);
- }
- }
- }
- /// <summary>
- ///
- /// </summary>
- public int columnGap
- {
- get { return _columnGap; }
- set
- {
- if (_columnGap != value)
- {
- _columnGap = value;
- SetBoundsChangedFlag();
- if (_virtual)
- SetVirtualListChangedFlag(true);
- }
- }
- }
- /// <summary>
- ///
- /// </summary>
- public AlignType align
- {
- get { return _align; }
- set
- {
- if (_align != value)
- {
- _align = value;
- SetBoundsChangedFlag();
- if (_virtual)
- SetVirtualListChangedFlag(true);
- }
- }
- }
- /// <summary>
- ///
- /// </summary>
- public VertAlignType verticalAlign
- {
- get { return _verticalAlign; }
- set
- {
- if (_verticalAlign != value)
- {
- _verticalAlign = value;
- SetBoundsChangedFlag();
- if (_virtual)
- SetVirtualListChangedFlag(true);
- }
- }
- }
- /// <summary>
- /// If the item will resize itself to fit the list width/height.
- /// </summary>
- public bool autoResizeItem
- {
- get { return _autoResizeItem; }
- set
- {
- if (_autoResizeItem != value)
- {
- _autoResizeItem = value;
- SetBoundsChangedFlag();
- if (_virtual)
- SetVirtualListChangedFlag(true);
- }
- }
- }
- /// <summary>
- ///
- /// </summary>
- /// <value></value>
- public Vector2 defaultItemSize
- {
- get { return _itemSize; }
- set
- {
- _itemSize = value;
- if (_virtual)
- {
- if (_layout == ListLayoutType.SingleColumn || _layout == ListLayoutType.FlowHorizontal)
- this.scrollPane.scrollStep = _itemSize.y;
- else
- this.scrollPane.scrollStep = _itemSize.x;
- SetVirtualListChangedFlag(true);
- }
- }
- }
- /// <summary>
- ///
- /// </summary>
- public GObjectPool itemPool
- {
- get { return _pool; }
- }
- /// <summary>
- ///
- /// </summary>
- /// <param name="url"></param>
- /// <returns></returns>
- public GObject GetFromPool(string url)
- {
- if (string.IsNullOrEmpty(url))
- url = _defaultItem;
- GObject ret = _pool.GetObject(url);
- if (ret != null)
- ret.visible = true;
- return ret;
- }
- void ReturnToPool(GObject obj)
- {
- _pool.ReturnObject(obj);
- }
- /// <summary>
- /// Add a item to list, same as GetFromPool+AddChild
- /// </summary>
- /// <returns>Item object</returns>
- public GObject AddItemFromPool()
- {
- GObject obj = GetFromPool(null);
- return AddChild(obj);
- }
- /// <summary>
- /// Add a item to list, same as GetFromPool+AddChild
- /// </summary>
- /// <param name="url">Item resource url</param>
- /// <returns>Item object</returns>
- public GObject AddItemFromPool(string url)
- {
- GObject obj = GetFromPool(url);
- return AddChild(obj);
- }
- /// <summary>
- ///
- /// </summary>
- /// <param name="child"></param>
- /// <param name="index"></param>
- /// <returns></returns>
- override public GObject AddChildAt(GObject child, int index)
- {
- base.AddChildAt(child, index);
- if (child is GButton)
- {
- GButton button = (GButton)child;
- button.selected = false;
- button.changeStateOnClick = false;
- }
- child.onClick.Add(_itemClickDelegate);
- child.onRightClick.Add(_itemClickDelegate);
- return child;
- }
- /// <summary>
- ///
- /// </summary>
- /// <param name="index"></param>
- /// <param name="dispose"></param>
- /// <returns></returns>
- override public GObject RemoveChildAt(int index, bool dispose)
- {
- GObject child = base.RemoveChildAt(index, dispose);
- child.onClick.Remove(_itemClickDelegate);
- child.onRightClick.Remove(_itemClickDelegate);
- return child;
- }
- /// <summary>
- ///
- /// </summary>
- /// <param name="index"></param>
- public void RemoveChildToPoolAt(int index)
- {
- GObject child = base.RemoveChildAt(index);
- ReturnToPool(child);
- }
- /// <summary>
- ///
- /// </summary>
- /// <param name="child"></param>
- public void RemoveChildToPool(GObject child)
- {
- base.RemoveChild(child);
- ReturnToPool(child);
- }
- /// <summary>
- ///
- /// </summary>
- public void RemoveChildrenToPool()
- {
- RemoveChildrenToPool(0, -1);
- }
- /// <summary>
- ///
- /// </summary>
- /// <param name="beginIndex"></param>
- /// <param name="endIndex"></param>
- public void RemoveChildrenToPool(int beginIndex, int endIndex)
- {
- if (endIndex < 0 || endIndex >= _children.Count)
- endIndex = _children.Count - 1;
- for (int i = beginIndex; i <= endIndex; ++i)
- RemoveChildToPoolAt(beginIndex);
- }
- /// <summary>
- ///
- /// </summary>
- public int selectedIndex
- {
- get
- {
- if (_virtual)
- {
- int cnt = _realNumItems;
- for (int i = 0; i < cnt; i++)
- {
- ItemInfo ii = _virtualItems[i];
- if ((ii.obj is GButton) && ((GButton)ii.obj).selected
- || ii.obj == null && ii.selected)
- {
- if (_loop)
- return i % _numItems;
- else
- return i;
- }
- }
- }
- else
- {
- int cnt = _children.Count;
- for (int i = 0; i < cnt; i++)
- {
- GButton obj = _children[i].asButton;
- if (obj != null && obj.selected)
- return i;
- }
- }
- return -1;
- }
- set
- {
- if (value >= 0 && value < this.numItems)
- {
- if (selectionMode != ListSelectionMode.Single)
- ClearSelection();
- AddSelection(value, false);
- }
- else
- ClearSelection();
- }
- }
- /// <summary>
- ///
- /// </summary>
- public Controller selectionController
- {
- get { return _selectionController; }
- set { _selectionController = value; }
- }
- /// <summary>
- ///
- /// </summary>
- /// <returns></returns>
- public List<int> GetSelection()
- {
- return GetSelection(null);
- }
- /// <summary>
- ///
- /// </summary>
- /// <returns></returns>
- public List<int> GetSelection(List<int> result)
- {
- if (result == null)
- result = new List<int>();
- if (_virtual)
- {
- int cnt = _realNumItems;
- for (int i = 0; i < cnt; i++)
- {
- ItemInfo ii = _virtualItems[i];
- if ((ii.obj is GButton) && ((GButton)ii.obj).selected
- || ii.obj == null && ii.selected)
- {
- int j = i;
- if (_loop)
- {
- j = i % _numItems;
- if (result.Contains(j))
- continue;
- }
- result.Add(j);
- }
- }
- }
- else
- {
- int cnt = _children.Count;
- for (int i = 0; i < cnt; i++)
- {
- GButton obj = _children[i].asButton;
- if (obj != null && obj.selected)
- result.Add(i);
- }
- }
- return result;
- }
- /// <summary>
- ///
- /// </summary>
- /// <param name="index"></param>
- /// <param name="scrollItToView"></param>
- public void AddSelection(int index, bool scrollItToView)
- {
- if (selectionMode == ListSelectionMode.None)
- return;
- CheckVirtualList();
- if (selectionMode == ListSelectionMode.Single)
- ClearSelection();
- if (scrollItToView)
- ScrollToView(index);
- _lastSelectedIndex = index;
- GButton obj = null;
- if (_virtual)
- {
- ItemInfo ii = _virtualItems[index];
- if (ii.obj != null)
- obj = ii.obj.asButton;
- ii.selected = true;
- }
- else
- obj = GetChildAt(index).asButton;
- if (obj != null && !obj.selected)
- {
- obj.selected = true;
- UpdateSelectionController(index);
- }
- }
- /// <summary>
- ///
- /// </summary>
- /// <param name="index"></param>
- public void RemoveSelection(int index)
- {
- if (selectionMode == ListSelectionMode.None)
- return;
- GButton obj = null;
- if (_virtual)
- {
- ItemInfo ii = _virtualItems[index];
- if (ii.obj != null)
- obj = ii.obj.asButton;
- ii.selected = false;
- }
- else
- obj = GetChildAt(index).asButton;
- if (obj != null)
- obj.selected = false;
- }
- /// <summary>
- ///
- /// </summary>
- public void ClearSelection()
- {
- if (_virtual)
- {
- int cnt = _realNumItems;
- for (int i = 0; i < cnt; i++)
- {
- ItemInfo ii = _virtualItems[i];
- if ((ii.obj is GButton))
- ((GButton)ii.obj).selected = false;
- ii.selected = false;
- }
- }
- else
- {
- int cnt = _children.Count;
- for (int i = 0; i < cnt; i++)
- {
- GButton obj = _children[i].asButton;
- if (obj != null)
- obj.selected = false;
- }
- }
- }
- void ClearSelectionExcept(GObject g)
- {
- if (_virtual)
- {
- int cnt = _realNumItems;
- for (int i = 0; i < cnt; i++)
- {
- ItemInfo ii = _virtualItems[i];
- if (ii.obj != g)
- {
- if ((ii.obj is GButton))
- ((GButton)ii.obj).selected = false;
- ii.selected = false;
- }
- }
- }
- else
- {
- int cnt = _children.Count;
- for (int i = 0; i < cnt; i++)
- {
- GButton obj = _children[i].asButton;
- if (obj != null && obj != g)
- obj.selected = false;
- }
- }
- }
- /// <summary>
- ///
- /// </summary>
- public void SelectAll()
- {
- CheckVirtualList();
- int last = -1;
- if (_virtual)
- {
- int cnt = _realNumItems;
- for (int i = 0; i < cnt; i++)
- {
- ItemInfo ii = _virtualItems[i];
- if ((ii.obj is GButton) && !((GButton)ii.obj).selected)
- {
- ((GButton)ii.obj).selected = true;
- last = i;
- }
- ii.selected = true;
- }
- }
- else
- {
- int cnt = _children.Count;
- for (int i = 0; i < cnt; i++)
- {
- GButton obj = _children[i].asButton;
- if (obj != null && !obj.selected)
- {
- obj.selected = true;
- last = i;
- }
- }
- }
- if (last != -1)
- UpdateSelectionController(last);
- }
- /// <summary>
- ///
- /// </summary>
- public void SelectNone()
- {
- ClearSelection();
- }
- /// <summary>
- ///
- /// </summary>
- public void SelectReverse()
- {
- CheckVirtualList();
- int last = -1;
- if (_virtual)
- {
- int cnt = _realNumItems;
- for (int i = 0; i < cnt; i++)
- {
- ItemInfo ii = _virtualItems[i];
- if ((ii.obj is GButton))
- {
- ((GButton)ii.obj).selected = !((GButton)ii.obj).selected;
- if (((GButton)ii.obj).selected)
- last = i;
- }
- ii.selected = !ii.selected;
- }
- }
- else
- {
- int cnt = _children.Count;
- for (int i = 0; i < cnt; i++)
- {
- GButton obj = _children[i].asButton;
- if (obj != null)
- {
- obj.selected = !obj.selected;
- if (obj.selected)
- last = i;
- }
- }
- }
- if (last != -1)
- UpdateSelectionController(last);
- }
- /// <summary>
- ///
- /// </summary>
- /// <param name="enabled"></param>
- public void EnableSelectionFocusEvents(bool enabled)
- {
- if (((_miscFlags & 2) != 0) == enabled)
- return;
- if (enabled)
- {
- _miscFlags |= 2;
- this.tabStopChildren = true;
- onFocusIn.Add(NotifySelection);
- onFocusOut.Add(NotifySelection);
- }
- else
- {
- _miscFlags &= 0xFD;
- onFocusIn.Remove(NotifySelection);
- onFocusOut.Remove(NotifySelection);
- }
- }
- void NotifySelection(EventContext context)
- {
- string eventType = context.type == "onFocusIn" ? "onListFocusIn" : "onListFocusOut";
- int cnt = _children.Count;
- for (int i = 0; i < cnt; i++)
- {
- GButton obj = _children[i].asButton;
- if (obj != null && obj.selected)
- obj.DispatchEvent(eventType);
- }
- }
- /// <summary>
- ///
- /// </summary>
- public void EnableArrowKeyNavigation(bool enabled)
- {
- if (enabled)
- {
- this.tabStopChildren = true;
- onKeyDown.Add(__keydown);
- }
- else
- {
- this.tabStopChildren = false;
- onKeyDown.Remove(__keydown);
- }
- }
- void __keydown(EventContext context)
- {
- int index = -1;
- switch (context.inputEvent.keyCode)
- {
- case KeyCode.LeftArrow:
- index = HandleArrowKey(7);
- break;
- case KeyCode.RightArrow:
- index = HandleArrowKey(3);
- break;
- case KeyCode.UpArrow:
- index = HandleArrowKey(1);
- break;
- case KeyCode.DownArrow:
- index = HandleArrowKey(5);
- break;
- }
- if (index != -1)
- {
- index = ItemIndexToChildIndex(index);
- if (index != -1)
- DispatchItemEvent(GetChildAt(index), context);
- context.StopPropagation();
- }
- }
- /// <summary>
- ///
- /// </summary>
- /// <param name="dir"></param>
- public int HandleArrowKey(int dir)
- {
- int curIndex = this.selectedIndex;
- if (curIndex == -1)
- return -1;
- int index = curIndex;
- switch (dir)
- {
- case 1://up
- if (_layout == ListLayoutType.SingleColumn || _layout == ListLayoutType.FlowVertical)
- {
- index--;
- }
- else if (_layout == ListLayoutType.FlowHorizontal || _layout == ListLayoutType.Pagination)
- {
- if (_virtual)
- {
- index -= _curLineItemCount;
- }
- else
- {
- GObject current = _children[index];
- int k = 0;
- int i;
- for (i = index - 1; i >= 0; i--)
- {
- GObject obj = _children[i];
- if (obj.y != current.y)
- {
- current = obj;
- break;
- }
- k++;
- }
- for (; i >= 0; i--)
- {
- GObject obj = _children[i];
- if (obj.y != current.y)
- {
- index = i + k + 1;
- break;
- }
- }
- }
- }
- break;
- case 3://right
- if (_layout == ListLayoutType.SingleRow || _layout == ListLayoutType.FlowHorizontal || _layout == ListLayoutType.Pagination)
- {
- index++;
- }
- else if (_layout == ListLayoutType.FlowVertical)
- {
- if (_virtual)
- {
- index += _curLineItemCount;
- }
- else
- {
- GObject current = _children[index];
- int k = 0;
- int cnt = _children.Count;
- int i;
- for (i = index + 1; i < cnt; i++)
- {
- GObject obj = _children[i];
- if (obj.x != current.x)
- {
- current = obj;
- break;
- }
- k++;
- }
- for (; i < cnt; i++)
- {
- GObject obj = _children[i];
- if (obj.x != current.x)
- {
- index = i - k - 1;
- break;
- }
- }
- }
- }
- break;
- case 5://down
- if (_layout == ListLayoutType.SingleColumn || _layout == ListLayoutType.FlowVertical)
- {
- index++;
- }
- else if (_layout == ListLayoutType.FlowHorizontal || _layout == ListLayoutType.Pagination)
- {
- if (_virtual)
- {
- index += _curLineItemCount;
- }
- else
- {
- GObject current = _children[index];
- int k = 0;
- int cnt = _children.Count;
- int i;
- for (i = index + 1; i < cnt; i++)
- {
- GObject obj = _children[i];
- if (obj.y != current.y)
- {
- current = obj;
- break;
- }
- k++;
- }
- for (; i < cnt; i++)
- {
- GObject obj = _children[i];
- if (obj.y != current.y)
- {
- index = i - k - 1;
- break;
- }
- }
- }
- }
- break;
- case 7://left
- if (_layout == ListLayoutType.SingleRow || _layout == ListLayoutType.FlowHorizontal || _layout == ListLayoutType.Pagination)
- {
- index--;
- }
- else if (_layout == ListLayoutType.FlowVertical)
- {
- if (_virtual)
- {
- index -= _curLineItemCount;
- }
- else
- {
- GObject current = _children[index];
- int k = 0;
- int i;
- for (i = index - 1; i >= 0; i--)
- {
- GObject obj = _children[i];
- if (obj.x != current.x)
- {
- current = obj;
- break;
- }
- k++;
- }
- for (; i >= 0; i--)
- {
- GObject obj = _children[i];
- if (obj.x != current.x)
- {
- index = i + k + 1;
- break;
- }
- }
- }
- }
- break;
- }
- if (index != curIndex && index >= 0 && index < this.numItems)
- {
- ClearSelection();
- AddSelection(index, true);
- return index;
- }
- else
- return -1;
- }
- void __clickItem(EventContext context)
- {
- GObject item = context.sender as GObject;
- if ((item is GButton) && selectionMode != ListSelectionMode.None)
- SetSelectionOnEvent(item, context.inputEvent);
- if (scrollPane != null && scrollItemToViewOnClick)
- scrollPane.ScrollToView(item, true);
- DispatchItemEvent(item, context);
- }
- virtual protected void DispatchItemEvent(GObject item, EventContext context)
- {
- if (context.type == item.onRightClick.type)
- DispatchEvent("onRightClickItem", item);
- else
- DispatchEvent("onClickItem", item);
- }
- void SetSelectionOnEvent(GObject item, InputEvent evt)
- {
- bool dontChangeLastIndex = false;
- GButton button = (GButton)item;
- int index = ChildIndexToItemIndex(GetChildIndex(item));
- if (selectionMode == ListSelectionMode.Single)
- {
- if (!button.selected)
- {
- ClearSelectionExcept(button);
- button.selected = true;
- }
- }
- else
- {
- if (evt.shift)
- {
- if (!button.selected)
- {
- if (_lastSelectedIndex != -1)
- {
- int min = Math.Min(_lastSelectedIndex, index);
- int max = Math.Max(_lastSelectedIndex, index);
- max = Math.Min(max, this.numItems - 1);
- if (_virtual)
- {
- for (int i = min; i <= max; i++)
- {
- ItemInfo ii = _virtualItems[i];
- if (ii.obj is GButton)
- ((GButton)ii.obj).selected = true;
- ii.selected = true;
- }
- }
- else
- {
- for (int i = min; i <= max; i++)
- {
- GButton obj = GetChildAt(i).asButton;
- if (obj != null && !obj.selected)
- obj.selected = true;
- }
- }
- dontChangeLastIndex = true;
- }
- else
- {
- button.selected = true;
- }
- }
- }
- else if (evt.ctrlOrCmd || selectionMode == ListSelectionMode.Multiple_SingleClick)
- {
- button.selected = !button.selected;
- }
- else
- {
- if (!button.selected)
- {
- ClearSelectionExcept(button);
- button.selected = true;
- }
- else if (evt.button == 0)
- ClearSelectionExcept(button);
- }
- }
- if (!dontChangeLastIndex)
- _lastSelectedIndex = index;
- if (button.selected)
- UpdateSelectionController(index);
- }
- /// <summary>
- /// Resize to list size to fit specified item count.
- /// If list layout is single column or flow horizontally, the height will change to fit.
- /// If list layout is single row or flow vertically, the width will change to fit.
- /// </summary>
- public void ResizeToFit()
- {
- ResizeToFit(int.MaxValue, 0);
- }
- /// <summary>
- /// Resize to list size to fit specified item count.
- /// If list layout is single column or flow horizontally, the height will change to fit.
- /// If list layout is single row or flow vertically, the width will change to fit.
- /// </summary>
- /// <param name="itemCount">Item count</param>
- public void ResizeToFit(int itemCount)
- {
- ResizeToFit(itemCount, 0);
- }
- /// <summary>
- /// Resize to list size to fit specified item count.
- /// If list layout is single column or flow horizontally, the height will change to fit.
- /// If list layout is single row or flow vertically, the width will change to fit.
- /// </summary>
- /// <param name="itemCount">>Item count</param>
- /// <param name="minSize">If the result size if smaller than minSize, then use minSize.</param>
- public void ResizeToFit(int itemCount, int minSize)
- {
- EnsureBoundsCorrect();
- int curCount = this.numItems;
- if (itemCount > curCount)
- itemCount = curCount;
- if (_virtual)
- {
- int lineCount = Mathf.CeilToInt((float)itemCount / _curLineItemCount);
- if (_layout == ListLayoutType.SingleColumn || _layout == ListLayoutType.FlowHorizontal)
- this.viewHeight = lineCount * _itemSize.y + Math.Max(0, lineCount - 1) * _lineGap;
- else
- this.viewWidth = lineCount * _itemSize.x + Math.Max(0, lineCount - 1) * _columnGap;
- }
- else if (itemCount == 0)
- {
- if (_layout == ListLayoutType.SingleColumn || _layout == ListLayoutType.FlowHorizontal)
- this.viewHeight = minSize;
- else
- this.viewWidth = minSize;
- }
- else
- {
- int i = itemCount - 1;
- GObject obj = null;
- while (i >= 0)
- {
- obj = this.GetChildAt(i);
- if (!foldInvisibleItems || obj.visible)
- break;
- i--;
- }
- if (i < 0)
- {
- if (_layout == ListLayoutType.SingleColumn || _layout == ListLayoutType.FlowHorizontal)
- this.viewHeight = minSize;
- else
- this.viewWidth = minSize;
- }
- else
- {
- float size;
- if (_layout == ListLayoutType.SingleColumn || _layout == ListLayoutType.FlowHorizontal)
- {
- size = obj.y + obj.height;
- if (size < minSize)
- size = minSize;
- this.viewHeight = size;
- }
- else
- {
- size = obj.x + obj.width;
- if (size < minSize)
- size = minSize;
- this.viewWidth = size;
- }
- }
- }
- }
- /// <summary>
- ///
- /// </summary>
- override protected void HandleSizeChanged()
- {
- base.HandleSizeChanged();
- SetBoundsChangedFlag();
- if (_virtual)
- SetVirtualListChangedFlag(true);
- }
- override public void HandleControllerChanged(Controller c)
- {
- base.HandleControllerChanged(c);
- if (_selectionController == c)
- this.selectedIndex = c.selectedIndex;
- }
- void UpdateSelectionController(int index)
- {
- if (_selectionController != null && !_selectionController.changing
- && index < _selectionController.pageCount)
- {
- Controller c = _selectionController;
- _selectionController = null;
- c.selectedIndex = index;
- _selectionController = c;
- }
- }
- /// <summary>
- /// Scroll the list to make an item with certain index visible.
- /// </summary>
- /// <param name="index">Item index</param>
- public void ScrollToView(int index)
- {
- ScrollToView(index, false);
- }
- /// <summary>
- /// Scroll the list to make an item with certain index visible.
- /// </summary>
- /// <param name="index">Item index</param>
- /// <param name="ani">True to scroll smoothly, othewise immdediately.</param>
- public void ScrollToView(int index, bool ani)
- {
- ScrollToView(index, ani, false);
- }
- /// <summary>
- /// Scroll the list to make an item with certain index visible.
- /// </summary>
- /// <param name="index">Item index</param>
- /// <param name="ani">True to scroll smoothly, othewise immdediately.</param>
- /// <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>
- public void ScrollToView(int index, bool ani, bool setFirst)
- {
- if (_virtual)
- {
- if (_numItems == 0)
- return;
- CheckVirtualList();
- if (index >= _virtualItems.Count)
- throw new Exception("Invalid child index: " + index + ">" + _virtualItems.Count);
- if (_loop)
- index = Mathf.FloorToInt((float)_firstIndex / _numItems) * _numItems + index;
- Rect rect;
- ItemInfo ii = _virtualItems[index];
- if (_layout == ListLayoutType.SingleColumn || _layout == ListLayoutType.FlowHorizontal)
- {
- float pos = 0;
- for (int i = _curLineItemCount - 1; i < index; i += _curLineItemCount)
- pos += _virtualItems[i].size.y + _lineGap;
- rect = new Rect(0, pos, _itemSize.x, ii.size.y);
- }
- else if (_layout == ListLayoutType.SingleRow || _layout == ListLayoutType.FlowVertical)
- {
- float pos = 0;
- for (int i = _curLineItemCount - 1; i < index; i += _curLineItemCount)
- pos += _virtualItems[i].size.x + _columnGap;
- rect = new Rect(pos, 0, ii.size.x, _itemSize.y);
- }
- else
- {
- int page = index / (_curLineItemCount * _curLineItemCount2);
- rect = new Rect(page * viewWidth + (index % _curLineItemCount) * (ii.size.x + _columnGap),
- (index / _curLineItemCount) % _curLineItemCount2 * (ii.size.y + _lineGap),
- ii.size.x, ii.size.y);
- }
- if (this.scrollPane != null)
- scrollPane.ScrollToView(rect, ani, setFirst);
- else if (parent != null && parent.scrollPane != null)
- parent.scrollPane.ScrollToView(this.TransformRect(rect, parent), ani, setFirst);
- }
- else
- {
- GObject obj = GetChildAt(index);
- if (this.scrollPane != null)
- scrollPane.ScrollToView(obj, ani, setFirst);
- else if (parent != null && parent.scrollPane != null)
- parent.scrollPane.ScrollToView(obj, ani, setFirst);
- }
- }
- /// <summary>
- /// 获取当前点击哪个item
- /// </summary>
- public GObject touchItem
- {
- get
- {
- //find out which item is under finger
- //逐层往上知道查到点击了那个item
- GObject obj = GRoot.inst.touchTarget;
- GObject p = obj.parent;
- while (p != null)
- {
- if (p == this)
- return obj;
- obj = p;
- p = p.parent;
- }
- return null;
- }
- }
- /// <summary>
- /// Get first child in view.
- /// </summary>
- /// <returns></returns>
- public override int GetFirstChildInView()
- {
- return ChildIndexToItemIndex(base.GetFirstChildInView());
- }
- public int ChildIndexToItemIndex(int index)
- {
- if (!_virtual)
- return index;
- if (_layout == ListLayoutType.Pagination)
- {
- for (int i = _firstIndex; i < _realNumItems; i++)
- {
- if (_virtualItems[i].obj != null)
- {
- index--;
- if (index < 0)
- return i;
- }
- }
- return index;
- }
- else
- {
- index += _firstIndex;
- if (_loop && _numItems > 0)
- index = index % _numItems;
- return index;
- }
- }
- public int ItemIndexToChildIndex(int index)
- {
- if (!_virtual)
- return index;
- if (_layout == ListLayoutType.Pagination)
- {
- return GetChildIndex(_virtualItems[index].obj);
- }
- else
- {
- if (_loop && _numItems > 0)
- {
- int j = _firstIndex % _numItems;
- if (index >= j)
- index = index - j;
- else
- index = _numItems - j + index;
- }
- else
- index -= _firstIndex;
- return index;
- }
- }
- /// <summary>
- /// Set the list to be virtual list.
- /// 设置列表为虚拟列表模式。在虚拟列表模式下,列表不会为每一条列表数据创建一个实体对象,而是根据视口大小创建最小量的显示对象,然后通过itemRenderer指定的回调函数设置列表数据。
- /// 在虚拟模式下,你不能通过AddChild、RemoveChild等方式管理列表,只能通过设置numItems设置列表数据的长度。
- /// 如果要刷新列表,可以通过重新设置numItems,或者调用RefreshVirtualList完成。
- /// ‘单行’或者‘单列’的列表布局可支持不等高的列表项目。
- /// 除了‘页面’的列表布局,其他布局均支持使用不同资源构建列表项目,你可以在itemProvider里返回。如果不提供,默认使用defaultItem。
- /// </summary>
- public void SetVirtual()
- {
- SetVirtual(false);
- }
- public bool isVirtual
- {
- get { return _virtual; }
- }
- /// <summary>
- /// Set the list to be virtual list, and has loop behavior.
- /// </summary>
- public void SetVirtualAndLoop()
- {
- SetVirtual(true);
- }
- void SetVirtual(bool loop)
- {
- if (!_virtual)
- {
- if (this.scrollPane == null)
- Debug.LogError("FairyGUI: Virtual list must be scrollable!");
- if (loop)
- {
- if (_layout == ListLayoutType.FlowHorizontal || _layout == ListLayoutType.FlowVertical)
- Debug.LogError("FairyGUI: Loop list is not supported for FlowHorizontal or FlowVertical layout!");
- this.scrollPane.bouncebackEffect = false;
- }
- _virtual = true;
- _loop = loop;
- _virtualItems = new List<ItemInfo>();
- RemoveChildrenToPool();
- if (_itemSize.x == 0 || _itemSize.y == 0)
- {
- GObject obj = GetFromPool(null);
- if (obj == null)
- {
- Debug.LogError("FairyGUI: Virtual List must have a default list item resource.");
- _itemSize = new Vector2(100, 100);
- }
- else
- {
- _itemSize = obj.size;
- _itemSize.x = Mathf.CeilToInt(_itemSize.x);
- _itemSize.y = Mathf.CeilToInt(_itemSize.y);
- ReturnToPool(obj);
- }
- }
- if (_layout == ListLayoutType.SingleColumn || _layout == ListLayoutType.FlowHorizontal)
- {
- this.scrollPane.scrollStep = _itemSize.y;
- if (_loop)
- this.scrollPane._loop = 2;
- }
- else
- {
- this.scrollPane.scrollStep = _itemSize.x;
- if (_loop)
- this.scrollPane._loop = 1;
- }
- this.scrollPane.onScroll.AddCapture(__scrolled);
- SetVirtualListChangedFlag(true);
- }
- }
- /// <summary>
- /// Set the list item count.
- /// If the list is not virtual, specified number of items will be created.
- /// If the list is virtual, only items in view will be created.
- /// </summary>
- public int numItems
- {
- get
- {
- if (_virtual)
- return _numItems;
- else
- return _children.Count;
- }
- set
- {
- if (_virtual)
- {
- if (itemRenderer == null)
- throw new Exception("FairyGUI: Set itemRenderer first!");
- _numItems = value;
- if (_loop)
- _realNumItems = _numItems * 6;//设置6倍数量,用于循环滚动
- else
- _realNumItems = _numItems;
- //_virtualItems的设计是只增不减的
- int oldCount = _virtualItems.Count;
- if (_realNumItems > oldCount)
- {
- for (int i = oldCount; i < _realNumItems; i++)
- {
- ItemInfo ii = new ItemInfo();
- ii.size = _itemSize;
- _virtualItems.Add(ii);
- }
- }
- else
- {
- for (int i = _realNumItems; i < oldCount; i++)
- _virtualItems[i].selected = false;
- }
- if (_virtualListChanged != 0)
- Timers.inst.Remove(this.RefreshVirtualList);
- //立即刷新
- this.RefreshVirtualList(null);
- }
- else
- {
- int cnt = _children.Count;
- if (value > cnt)
- {
- for (int i = cnt; i < value; i++)
- {
- if (itemProvider == null)
- AddItemFromPool();
- else
- AddItemFromPool(itemProvider(i));
- }
- }
- else
- {
- RemoveChildrenToPool(value, cnt);
- }
- if (itemRenderer != null)
- {
- for (int i = 0; i < value; i++)
- itemRenderer(i, GetChildAt(i));
- }
- }
- }
- }
- public void RefreshVirtualList()
- {
- if (!_virtual)
- throw new Exception("FairyGUI: not virtual list");
- SetVirtualListChangedFlag(false);
- }
- void CheckVirtualList()
- {
- if (_virtualListChanged != 0)
- {
- this.RefreshVirtualList(null);
- Timers.inst.Remove(this.RefreshVirtualList);
- }
- }
- void SetVirtualListChangedFlag(bool layoutChanged)
- {
- if (layoutChanged)
- _virtualListChanged = 2;
- else if (_virtualListChanged == 0)
- _virtualListChanged = 1;
- Timers.inst.CallLater(RefreshVirtualList);
- }
- void RefreshVirtualList(object param)
- {
- bool layoutChanged = _virtualListChanged == 2;
- _virtualListChanged = 0;
- _miscFlags |= 1;
- if (layoutChanged)
- {
- if (_layout == ListLayoutType.SingleColumn || _layout == ListLayoutType.SingleRow)
- _curLineItemCount = 1;
- else if (_layout == ListLayoutType.FlowHorizontal)
- {
- if (_columnCount > 0)
- _curLineItemCount = _columnCount;
- else
- {
- _curLineItemCount = Mathf.FloorToInt((this.scrollPane.viewWidth + _columnGap) / (_itemSize.x + _columnGap));
- if (_curLineItemCount <= 0)
- _curLineItemCount = 1;
- }
- }
- else if (_layout == ListLayoutType.FlowVertical)
- {
- if (_lineCount > 0)
- _curLineItemCount = _lineCount;
- else
- {
- _curLineItemCount = Mathf.FloorToInt((this.scrollPane.viewHeight + _lineGap) / (_itemSize.y + _lineGap));
- if (_curLineItemCount <= 0)
- _curLineItemCount = 1;
- }
- }
- else //pagination
- {
- if (_columnCount > 0)
- _curLineItemCount = _columnCount;
- else
- {
- _curLineItemCount = Mathf.FloorToInt((this.scrollPane.viewWidth + _columnGap) / (_itemSize.x + _columnGap));
- if (_curLineItemCount <= 0)
- _curLineItemCount = 1;
- }
- if (_lineCount > 0)
- _curLineItemCount2 = _lineCount;
- else
- {
- _curLineItemCount2 = Mathf.FloorToInt((this.scrollPane.viewHeight + _lineGap) / (_itemSize.y + _lineGap));
- if (_curLineItemCount2 <= 0)
- _curLineItemCount2 = 1;
- }
- }
- }
- float ch = 0, cw = 0;
- if (_realNumItems > 0)
- {
- int len = Mathf.CeilToInt((float)_realNumItems / _curLineItemCount) * _curLineItemCount;
- int len2 = Math.Min(_curLineItemCount, _realNumItems);
- if (_layout == ListLayoutType.SingleColumn || _layout == ListLayoutType.FlowHorizontal)
- {
- for (int i = 0; i < len; i += _curLineItemCount)
- ch += _virtualItems[i].size.y + _lineGap;
- if (ch > 0)
- ch -= _lineGap;
- if (_autoResizeItem)
- cw = scrollPane.viewWidth;
- else
- {
- for (int i = 0; i < len2; i++)
- cw += _virtualItems[i].size.x + _columnGap;
- if (cw > 0)
- cw -= _columnGap;
- }
- }
- else if (_layout == ListLayoutType.SingleRow || _layout == ListLayoutType.FlowVertical)
- {
- for (int i = 0; i < len; i += _curLineItemCount)
- cw += _virtualItems[i].size.x + _columnGap;
- if (cw > 0)
- cw -= _columnGap;
- if (_autoResizeItem)
- ch = this.scrollPane.viewHeight;
- else
- {
- for (int i = 0; i < len2; i++)
- ch += _virtualItems[i].size.y + _lineGap;
- if (ch > 0)
- ch -= _lineGap;
- }
- }
- else
- {
- int pageCount = Mathf.CeilToInt((float)len / (_curLineItemCount * _curLineItemCount2));
- cw = pageCount * viewWidth;
- ch = viewHeight;
- }
- }
- HandleAlign(cw, ch);
- this.scrollPane.SetContentSize(cw, ch);
- _miscFlags &= 0xFE;
- HandleScroll(true);
- }
- void __scrolled(EventContext context)
- {
- HandleScroll(false);
- }
- int GetIndexOnPos1(ref float pos, bool forceUpdate)
- {
- if (_realNumItems < _curLineItemCount)
- {
- pos = 0;
- return 0;
- }
- if (numChildren > 0 && !forceUpdate)
- {
- float pos2 = this.GetChildAt(0).y;
- if (pos2 + (_lineGap > 0 ? 0 : -_lineGap) > pos)
- {
- for (int i = _firstIndex - _curLineItemCount; i >= 0; i -= _curLineItemCount)
- {
- pos2 -= (_virtualItems[i].size.y + _lineGap);
- if (pos2 <= pos)
- {
- pos = pos2;
- return i;
- }
- }
- pos = 0;
- return 0;
- }
- else
- {
- float testGap = _lineGap > 0 ? _lineGap : 0;
- for (int i = _firstIndex; i < _realNumItems; i += _curLineItemCount)
- {
- float pos3 = pos2 + _virtualItems[i].size.y;
- if (pos3 + testGap > pos)
- {
- pos = pos2;
- return i;
- }
- pos2 = pos3 + _lineGap;
- }
- pos = pos2;
- return _realNumItems - _curLineItemCount;
- }
- }
- else
- {
- float pos2 = 0;
- float testGap = _lineGap > 0 ? _lineGap : 0;
- for (int i = 0; i < _realNumItems; i += _curLineItemCount)
- {
- float pos3 = pos2 + _virtualItems[i].size.y;
- if (pos3 + testGap > pos)
- {
- pos = pos2;
- return i;
- }
- pos2 = pos3 + _lineGap;
- }
- pos = pos2;
- return _realNumItems - _curLineItemCount;
- }
- }
- int GetIndexOnPos2(ref float pos, bool forceUpdate)
- {
- if (_realNumItems < _curLineItemCount)
- {
- pos = 0;
- return 0;
- }
- if (numChildren > 0 && !forceUpdate)
- {
- float pos2 = this.GetChildAt(0).x;
- if (pos2 + (_columnGap > 0 ? 0 : -_columnGap) > pos)
- {
- for (int i = _firstIndex - _curLineItemCount; i >= 0; i -= _curLineItemCount)
- {
- pos2 -= (_virtualItems[i].size.x + _columnGap);
- if (pos2 <= pos)
- {
- pos = pos2;
- return i;
- }
- }
- pos = 0;
- return 0;
- }
- else
- {
- float testGap = _columnGap > 0 ? _columnGap : 0;
- for (int i = _firstIndex; i < _realNumItems; i += _curLineItemCount)
- {
- float pos3 = pos2 + _virtualItems[i].size.x;
- if (pos3 + testGap > pos)
- {
- pos = pos2;
- return i;
- }
- pos2 = pos3 + _columnGap;
- }
- pos = pos2;
- return _realNumItems - _curLineItemCount;
- }
- }
- else
- {
- float pos2 = 0;
- float testGap = _columnGap > 0 ? _columnGap : 0;
- for (int i = 0; i < _realNumItems; i += _curLineItemCount)
- {
- float pos3 = pos2 + _virtualItems[i].size.x;
- if (pos3 + testGap > pos)
- {
- pos = pos2;
- return i;
- }
- pos2 = pos3 + _columnGap;
- }
- pos = pos2;
- return _realNumItems - _curLineItemCount;
- }
- }
- int GetIndexOnPos3(ref float pos, bool forceUpdate)
- {
- if (_realNumItems < _curLineItemCount)
- {
- pos = 0;
- return 0;
- }
- float viewWidth = this.viewWidth;
- int page = Mathf.FloorToInt(pos / viewWidth);
- int startIndex = page * (_curLineItemCount * _curLineItemCount2);
- float pos2 = page * viewWidth;
- float testGap = _columnGap > 0 ? _columnGap : 0;
- for (int i = 0; i < _curLineItemCount; i++)
- {
- float pos3 = pos2 + _virtualItems[startIndex + i].size.x;
- if (pos3 + testGap > pos)
- {
- pos = pos2;
- return startIndex + i;
- }
- pos2 = pos3 + _columnGap;
- }
- pos = pos2;
- return startIndex + _curLineItemCount - 1;
- }
- void HandleScroll(bool forceUpdate)
- {
- if ((_miscFlags & 1) != 0)
- return;
- if (_layout == ListLayoutType.SingleColumn || _layout == ListLayoutType.FlowHorizontal)
- {
- int enterCounter = 0;
- while (HandleScroll1(forceUpdate))
- {
- //可能会因为ITEM资源改变导致ITEM大小发生改变,所有出现最后一页填不满的情况,这时要反复尝试填满。
- enterCounter++;
- forceUpdate = false;
- if (enterCounter > 20)
- {
- Debug.Log("FairyGUI: list will never be filled as the item renderer function always returns a different size.");
- break;
- }
- }
- HandleArchOrder1();
- }
- else if (_layout == ListLayoutType.SingleRow || _layout == ListLayoutType.FlowVertical)
- {
- int enterCounter = 0;
- while (HandleScroll2(forceUpdate))
- {
- enterCounter++;
- forceUpdate = false;
- if (enterCounter > 20)
- {
- Debug.Log("FairyGUI: list will never be filled as the item renderer function always returns a different size.");
- break;
- }
- }
- HandleArchOrder2();
- }
- else
- {
- HandleScroll3(forceUpdate);
- }
- _boundsChanged = false;
- }
- bool HandleScroll1(bool forceUpdate)
- {
- float pos = scrollPane.scrollingPosY;
- float max = pos + scrollPane.viewHeight;
- bool end = max == scrollPane.contentHeight;//这个标志表示当前需要滚动到最末,无论内容变化大小
- //寻找当前位置的第一条项目
- int newFirstIndex = GetIndexOnPos1(ref pos, forceUpdate);
- if (newFirstIndex == _firstIndex && !forceUpdate)
- return false;
- int oldFirstIndex = _firstIndex;
- _firstIndex = newFirstIndex;
- int curIndex = newFirstIndex;
- bool forward = oldFirstIndex > newFirstIndex;
- int childCount = this.numChildren;
- int lastIndex = oldFirstIndex + childCount - 1;
- int reuseIndex = forward ? lastIndex : oldFirstIndex;
- float curX = 0, curY = pos;
- bool needRender;
- float deltaSize = 0;
- float firstItemDeltaSize = 0;
- string url = _defaultItem;
- int partSize = (int)((scrollPane.viewWidth - _columnGap * (_curLineItemCount - 1)) / _curLineItemCount);
- itemInfoVer++;
- while (curIndex < _realNumItems && (end || curY < max))
- {
- ItemInfo ii = _virtualItems[curIndex];
- if (ii.obj == null || forceUpdate)
- {
- if (itemProvider != null)
- {
- url = itemProvider(curIndex % _numItems);
- if (url == null)
- url = _defaultItem;
- url = UIPackage.NormalizeURL(url);
- }
- if (ii.obj != null && ii.obj.resourceURL != url)
- {
- if (ii.obj is GButton)
- ii.selected = ((GButton)ii.obj).selected;
- RemoveChildToPool(ii.obj);
- ii.obj = null;
- }
- }
- if (ii.obj == null)
- {
- //搜索最适合的重用item,保证每次刷新需要新建或者重新render的item最少
- if (forward)
- {
- for (int j = reuseIndex; j >= oldFirstIndex; j--)
- {
- ItemInfo ii2 = _virtualItems[j];
- if (ii2.obj != null && ii2.updateFlag != itemInfoVer && ii2.obj.resourceURL == url)
- {
- if (ii2.obj is GButton)
- ii2.selected = ((GButton)ii2.obj).selected;
- ii.obj = ii2.obj;
- ii2.obj = null;
- if (j == reuseIndex)
- reuseIndex--;
- break;
- }
- }
- }
- else
- {
- for (int j = reuseIndex; j <= lastIndex; j++)
- {
- ItemInfo ii2 = _virtualItems[j];
- if (ii2.obj != null && ii2.updateFlag != itemInfoVer && ii2.obj.resourceURL == url)
- {
- if (ii2.obj is GButton)
- ii2.selected = ((GButton)ii2.obj).selected;
- ii.obj = ii2.obj;
- ii2.obj = null;
- if (j == reuseIndex)
- reuseIndex++;
- break;
- }
- }
- }
- if (ii.obj != null)
- {
- SetChildIndex(ii.obj, forward ? curIndex - newFirstIndex : numChildren);
- }
- else
- {
- ii.obj = _pool.GetObject(url);
- if (forward)
- this.AddChildAt(ii.obj, curIndex - newFirstIndex);
- else
- this.AddChild(ii.obj);
- }
- if (ii.obj is GButton)
- ((GButton)ii.obj).selected = ii.selected;
- needRender = true;
- }
- else
- needRender = forceUpdate;
- if (needRender)
- {
- if (_autoResizeItem && (_layout == ListLayoutType.SingleColumn || _columnCount > 0))
- ii.obj.SetSize(partSize, ii.obj.height, true);
- itemRenderer(curIndex % _numItems, ii.obj);
- if (curIndex % _curLineItemCount == 0)
- {
- deltaSize += Mathf.CeilToInt(ii.obj.size.y) - ii.size.y;
- if (curIndex == newFirstIndex && oldFirstIndex > newFirstIndex)
- {
- //当内容向下滚动时,如果新出现的项目大小发生变化,需要做一个位置补偿,才不会导致滚动跳动
- firstItemDeltaSize = Mathf.CeilToInt(ii.obj.size.y) - ii.size.y;
- }
- }
- ii.size.x = Mathf.CeilToInt(ii.obj.size.x);
- ii.size.y = Mathf.CeilToInt(ii.obj.size.y);
- }
- ii.updateFlag = itemInfoVer;
- ii.obj.SetXY(curX, curY);
- if (curIndex == newFirstIndex) //要显示多一条才不会穿帮
- max += ii.size.y;
- curX += ii.size.x + _columnGap;
- if (curIndex % _curLineItemCount == _curLineItemCount - 1)
- {
- curX = 0;
- curY += ii.size.y + _lineGap;
- }
- curIndex++;
- }
- for (int i = 0; i < childCount; i++)
- {
- ItemInfo ii = _virtualItems[oldFirstIndex + i];
- if (ii.updateFlag != itemInfoVer && ii.obj != null)
- {
- if (ii.obj is GButton)
- ii.selected = ((GButton)ii.obj).selected;
- RemoveChildToPool(ii.obj);
- ii.obj = null;
- }
- }
- childCount = _children.Count;
- for (int i = 0; i < childCount; i++)
- {
- GObject obj = _virtualItems[newFirstIndex + i].obj;
- if (_children[i] != obj)
- SetChildIndex(obj, i);
- }
- if (deltaSize != 0 || firstItemDeltaSize != 0)
- this.scrollPane.ChangeContentSizeOnScrolling(0, deltaSize, 0, firstItemDeltaSize);
- if (curIndex > 0 && this.numChildren > 0 && this.container.y <= 0 && GetChildAt(0).y > -this.container.y)//最后一页没填满!
- return true;
- else
- return false;
- }
- bool HandleScroll2(bool forceUpdate)
- {
- float pos = scrollPane.scrollingPosX;
- float max = pos + scrollPane.viewWidth;
- bool end = pos == scrollPane.contentWidth;//这个标志表示当前需要滚动到最末,无论内容变化大小
- //寻找当前位置的第一条项目
- int newFirstIndex = GetIndexOnPos2(ref pos, forceUpdate);
- if (newFirstIndex == _firstIndex && !forceUpdate)
- return false;
- int oldFirstIndex = _firstIndex;
- _firstIndex = newFirstIndex;
- int curIndex = newFirstIndex;
- bool forward = oldFirstIndex > newFirstIndex;
- int childCount = this.numChildren;
- int lastIndex = oldFirstIndex + childCount - 1;
- int reuseIndex = forward ? lastIndex : oldFirstIndex;
- float curX = pos, curY = 0;
- bool needRender;
- float deltaSize = 0;
- float firstItemDeltaSize = 0;
- string url = _defaultItem;
- int partSize = (int)((scrollPane.viewHeight - _lineGap * (_curLineItemCount - 1)) / _curLineItemCount);
- itemInfoVer++;
- while (curIndex < _realNumItems && (end || curX < max))
- {
- ItemInfo ii = _virtualItems[curIndex];
- if (ii.obj == null || forceUpdate)
- {
- if (itemProvider != null)
- {
- url = itemProvider(curIndex % _numItems);
- if (url == null)
- url = _defaultItem;
- url = UIPackage.NormalizeURL(url);
- }
- if (ii.obj != null && ii.obj.resourceURL != url)
- {
- if (ii.obj is GButton)
- ii.selected = ((GButton)ii.obj).selected;
- RemoveChildToPool(ii.obj);
- ii.obj = null;
- }
- }
- if (ii.obj == null)
- {
- if (forward)
- {
- for (int j = reuseIndex; j >= oldFirstIndex; j--)
- {
- ItemInfo ii2 = _virtualItems[j];
- if (ii2.obj != null && ii2.updateFlag != itemInfoVer && ii2.obj.resourceURL == url)
- {
- if (ii2.obj is GButton)
- ii2.selected = ((GButton)ii2.obj).selected;
- ii.obj = ii2.obj;
- ii2.obj = null;
- if (j == reuseIndex)
- reuseIndex--;
- break;
- }
- }
- }
- else
- {
- for (int j = reuseIndex; j <= lastIndex; j++)
- {
- ItemInfo ii2 = _virtualItems[j];
- if (ii2.obj != null && ii2.updateFlag != itemInfoVer && ii2.obj.resourceURL == url)
- {
- if (ii2.obj is GButton)
- ii2.selected = ((GButton)ii2.obj).selected;
- ii.obj = ii2.obj;
- ii2.obj = null;
- if (j == reuseIndex)
- reuseIndex++;
- break;
- }
- }
- }
- if (ii.obj != null)
- {
- SetChildIndex(ii.obj, forward ? curIndex - newFirstIndex : numChildren);
- }
- else
- {
- ii.obj = _pool.GetObject(url);
- if (forward)
- this.AddChildAt(ii.obj, curIndex - newFirstIndex);
- else
- this.AddChild(ii.obj);
- }
- if (ii.obj is GButton)
- ((GButton)ii.obj).selected = ii.selected;
- needRender = true;
- }
- else
- needRender = forceUpdate;
- if (needRender)
- {
- if (_autoResizeItem && (_layout == ListLayoutType.SingleRow || _lineCount > 0))
- ii.obj.SetSize(ii.obj.width, partSize, true);
- itemRenderer(curIndex % _numItems, ii.obj);
- if (curIndex % _curLineItemCount == 0)
- {
- deltaSize += Mathf.CeilToInt(ii.obj.size.x) - ii.size.x;
- if (curIndex == newFirstIndex && oldFirstIndex > newFirstIndex)
- {
- //当内容向下滚动时,如果新出现的一个项目大小发生变化,需要做一个位置补偿,才不会导致滚动跳动
- firstItemDeltaSize = Mathf.CeilToInt(ii.obj.size.x) - ii.size.x;
- }
- }
- ii.size.x = Mathf.CeilToInt(ii.obj.size.x);
- ii.size.y = Mathf.CeilToInt(ii.obj.size.y);
- }
- ii.updateFlag = itemInfoVer;
- ii.obj.SetXY(curX, curY);
- if (curIndex == newFirstIndex) //要显示多一条才不会穿帮
- max += ii.size.x;
- curY += ii.size.y + _lineGap;
- if (curIndex % _curLineItemCount == _curLineItemCount - 1)
- {
- curY = 0;
- curX += ii.size.x + _columnGap;
- }
- curIndex++;
- }
- for (int i = 0; i < childCount; i++)
- {
- ItemInfo ii = _virtualItems[oldFirstIndex + i];
- if (ii.updateFlag != itemInfoVer && ii.obj != null)
- {
- if (ii.obj is GButton)
- ii.selected = ((GButton)ii.obj).selected;
- RemoveChildToPool(ii.obj);
- ii.obj = null;
- }
- }
- childCount = _children.Count;
- for (int i = 0; i < childCount; i++)
- {
- GObject obj = _virtualItems[newFirstIndex + i].obj;
- if (_children[i] != obj)
- SetChildIndex(obj, i);
- }
- if (deltaSize != 0 || firstItemDeltaSize != 0)
- this.scrollPane.ChangeContentSizeOnScrolling(deltaSize, 0, firstItemDeltaSize, 0);
- if (curIndex > 0 && this.numChildren > 0 && this.container.x <= 0 && GetChildAt(0).x > -this.container.x)//最后一页没填满!
- return true;
- else
- return false;
- }
- void HandleScroll3(bool forceUpdate)
- {
- float pos = scrollPane.scrollingPosX;
- //寻找当前位置的第一条项目
- int newFirstIndex = GetIndexOnPos3(ref pos, forceUpdate);
- if (newFirstIndex == _firstIndex && !forceUpdate)
- return;
- int oldFirstIndex = _firstIndex;
- _firstIndex = newFirstIndex;
- //分页模式不支持不等高,所以渲染满一页就好了
- int reuseIndex = oldFirstIndex;
- int virtualItemCount = _virtualItems.Count;
- int pageSize = _curLineItemCount * _curLineItemCount2;
- int startCol = newFirstIndex % _curLineItemCount;
- float viewWidth = this.viewWidth;
- int page = (int)(newFirstIndex / pageSize);
- int startIndex = page * pageSize;
- int lastIndex = startIndex + pageSize * 2; //测试两页
- bool needRender;
- string url = _defaultItem;
- int partWidth = (int)((scrollPane.viewWidth - _columnGap * (_curLineItemCount - 1)) / _curLineItemCount);
- int partHeight = (int)((scrollPane.viewHeight - _lineGap * (_curLineItemCount2 - 1)) / _curLineItemCount2);
- itemInfoVer++;
- //先标记这次要用到的项目
- for (int i = startIndex; i < lastIndex; i++)
- {
- if (i >= _realNumItems)
- continue;
- int col = i % _curLineItemCount;
- if (i - startIndex < pageSize)
- {
- if (col < startCol)
- continue;
- }
- else
- {
- if (col > startCol)
- continue;
- }
- ItemInfo ii = _virtualItems[i];
- ii.updateFlag = itemInfoVer;
- }
- GObject lastObj = null;
- int insertIndex = 0;
- for (int i = startIndex; i < lastIndex; i++)
- {
- if (i >= _realNumItems)
- continue;
- ItemInfo ii = _virtualItems[i];
- if (ii.updateFlag != itemInfoVer)
- continue;
- if (ii.obj == null)
- {
- //寻找看有没有可重用的
- while (reuseIndex < virtualItemCount)
- {
- ItemInfo ii2 = _virtualItems[reuseIndex];
- if (ii2.obj != null && ii2.updateFlag != itemInfoVer)
- {
- if (ii2.obj is GButton)
- ii2.selected = ((GButton)ii2.obj).selected;
- ii.obj = ii2.obj;
- ii2.obj = null;
- break;
- }
- reuseIndex++;
- }
- if (insertIndex == -1)
- insertIndex = GetChildIndex(lastObj) + 1;
- if (ii.obj == null)
- {
- if (itemProvider != null)
- {
- url = itemProvider(i % _numItems);
- if (url == null)
- url = _defaultItem;
- url = UIPackage.NormalizeURL(url);
- }
- ii.obj = _pool.GetObject(url);
- this.AddChildAt(ii.obj, insertIndex);
- }
- else
- {
- insertIndex = SetChildIndexBefore(ii.obj, insertIndex);
- }
- insertIndex++;
- if (ii.obj is GButton)
- ((GButton)ii.obj).selected = ii.selected;
- needRender = true;
- }
- else
- {
- needRender = forceUpdate;
- insertIndex = -1;
- lastObj = ii.obj;
- }
- if (needRender)
- {
- if (_autoResizeItem)
- {
- if (_curLineItemCount == _columnCount && _curLineItemCount2 == _lineCount)
- ii.obj.SetSize(partWidth, partHeight, true);
- else if (_curLineItemCount == _columnCount)
- ii.obj.SetSize(partWidth, ii.obj.height, true);
- else if (_curLineItemCount2 == _lineCount)
- ii.obj.SetSize(ii.obj.width, partHeight, true);
- }
- itemRenderer(i % _numItems, ii.obj);
- ii.size.x = Mathf.CeilToInt(ii.obj.size.x);
- ii.size.y = Mathf.CeilToInt(ii.obj.size.y);
- }
- }
- //排列item
- float borderX = (startIndex / pageSize) * viewWidth;
- float xx = borderX;
- float yy = 0;
- float lineHeight = 0;
- for (int i = startIndex; i < lastIndex; i++)
- {
- if (i >= _realNumItems)
- continue;
- ItemInfo ii = _virtualItems[i];
- if (ii.updateFlag == itemInfoVer)
- ii.obj.SetXY(xx, yy);
- if (ii.size.y > lineHeight)
- lineHeight = ii.size.y;
- if (i % _curLineItemCount == _curLineItemCount - 1)
- {
- xx = borderX;
- yy += lineHeight + _lineGap;
- lineHeight = 0;
- if (i == startIndex + pageSize - 1)
- {
- borderX += viewWidth;
- xx = borderX;
- yy = 0;
- }
- }
- else
- xx += ii.size.x + _columnGap;
- }
- //释放未使用的
- for (int i = reuseIndex; i < virtualItemCount; i++)
- {
- ItemInfo ii = _virtualItems[i];
- if (ii.updateFlag != itemInfoVer && ii.obj != null)
- {
- if (ii.obj is GButton)
- ii.selected = ((GButton)ii.obj).selected;
- RemoveChildToPool(ii.obj);
- ii.obj = null;
- }
- }
- }
- void HandleArchOrder1()
- {
- if (this.childrenRenderOrder == ChildrenRenderOrder.Arch)
- {
- float mid = this.scrollPane.posY + this.viewHeight / 2;
- float minDist = int.MaxValue, dist;
- int apexIndex = 0;
- int cnt = this.numChildren;
- for (int i = 0; i < cnt; i++)
- {
- GObject obj = GetChildAt(i);
- if (!foldInvisibleItems || obj.visible)
- {
- dist = Mathf.Abs(mid - obj.y - obj.height / 2);
- if (dist < minDist)
- {
- minDist = dist;
- apexIndex = i;
- }
- }
- }
- this.apexIndex = apexIndex;
- }
- }
- void HandleArchOrder2()
- {
- if (this.childrenRenderOrder == ChildrenRenderOrder.Arch)
- {
- float mid = this.scrollPane.posX + this.viewWidth / 2;
- float minDist = int.MaxValue, dist;
- int apexIndex = 0;
- int cnt = this.numChildren;
- for (int i = 0; i < cnt; i++)
- {
- GObject obj = GetChildAt(i);
- if (!foldInvisibleItems || obj.visible)
- {
- dist = Mathf.Abs(mid - obj.x - obj.width / 2);
- if (dist < minDist)
- {
- minDist = dist;
- apexIndex = i;
- }
- }
- }
- this.apexIndex = apexIndex;
- }
- }
- override public void GetSnappingPositionWithDir(ref float xValue, ref float yValue, float xDir, float yDir)
- {
- if (_virtual)
- {
- if (_layout == ListLayoutType.SingleColumn || _layout == ListLayoutType.FlowHorizontal)
- {
- float saved = yValue;
- int index = GetIndexOnPos1(ref yValue, false);
- if (index < _virtualItems.Count && index < _realNumItems)
- {
- float size = _virtualItems[index].size.y;
- if (ShouldSnapToNext(yDir, saved - yValue, size))
- yValue += size + _lineGap;
- }
- }
- else if (_layout == ListLayoutType.SingleRow || _layout == ListLayoutType.FlowVertical)
- {
- float saved = xValue;
- int index = GetIndexOnPos2(ref xValue, false);
- if (index < _virtualItems.Count && index < _realNumItems)
- {
- float size = _virtualItems[index].size.x;
- if (ShouldSnapToNext(xDir, saved - xValue, size))
- xValue += size + _columnGap;
- }
- }
- else
- {
- float saved = xValue;
- int index = GetIndexOnPos3(ref xValue, false);
- if (index < _virtualItems.Count && index < _realNumItems)
- {
- float size = _virtualItems[index].size.x;
- if (ShouldSnapToNext(xDir, saved - xValue, size))
- xValue += size + _columnGap;
- }
- }
- }
- else
- base.GetSnappingPositionWithDir(ref xValue, ref yValue, xDir, yDir);
- }
- private void HandleAlign(float contentWidth, float contentHeight)
- {
- Vector2 newOffset = Vector2.zero;
- if (contentHeight < viewHeight)
- {
- if (_verticalAlign == VertAlignType.Middle)
- newOffset.y = (int)((viewHeight - contentHeight) / 2);
- else if (_verticalAlign == VertAlignType.Bottom)
- newOffset.y = viewHeight - contentHeight;
- }
- if (contentWidth < this.viewWidth)
- {
- if (_align == AlignType.Center)
- newOffset.x = (int)((viewWidth - contentWidth) / 2);
- else if (_align == AlignType.Right)
- newOffset.x = viewWidth - contentWidth;
- }
- if (newOffset != _alignOffset)
- {
- _alignOffset = newOffset;
- if (scrollPane != null)
- scrollPane.AdjustMaskContainer();
- else
- container.SetXY(_margin.left + _alignOffset.x, _margin.top + _alignOffset.y);
- }
- }
- override protected void UpdateBounds()
- {
- if (_virtual)
- return;
- int cnt = _children.Count;
- int i;
- int j = 0;
- GObject child;
- float curX = 0;
- float curY = 0;
- float cw, ch;
- float maxWidth = 0;
- float maxHeight = 0;
- float viewWidth = this.viewWidth;
- float viewHeight = this.viewHeight;
- if (_layout == ListLayoutType.SingleColumn)
- {
- for (i = 0; i < cnt; i++)
- {
- child = GetChildAt(i);
- if (foldInvisibleItems && !child.visible)
- continue;
- if (curY != 0)
- curY += _lineGap;
- child.y = curY;
- if (_autoResizeItem)
- child.SetSize(viewWidth, child.height, true);
- curY += Mathf.CeilToInt(child.height);
- if (child.width > maxWidth)
- maxWidth = child.width;
- }
- ch = curY;
- if (ch <= viewHeight && _autoResizeItem && scrollPane != null && scrollPane._displayInDemand && scrollPane.vtScrollBar != null)
- {
- viewWidth += scrollPane.vtScrollBar.width;
- for (i = 0; i < cnt; i++)
- {
- child = GetChildAt(i);
- if (foldInvisibleItems && !child.visible)
- continue;
- child.SetSize(viewWidth, child.height, true);
- }
- }
- cw = Mathf.CeilToInt(maxWidth);
- }
- else if (_layout == ListLayoutType.SingleRow)
- {
- for (i = 0; i < cnt; i++)
- {
- child = GetChildAt(i);
- if (foldInvisibleItems && !child.visible)
- continue;
- if (curX != 0)
- curX += _columnGap;
- child.x = curX;
- if (_autoResizeItem)
- child.SetSize(child.width, viewHeight, true);
- curX += Mathf.CeilToInt(child.width);
- if (child.height > maxHeight)
- maxHeight = child.height;
- }
- cw = curX;
- if (cw <= viewWidth && _autoResizeItem && scrollPane != null && scrollPane._displayInDemand && scrollPane.hzScrollBar != null)
- {
- viewHeight += scrollPane.hzScrollBar.height;
- for (i = 0; i < cnt; i++)
- {
- child = GetChildAt(i);
- if (foldInvisibleItems && !child.visible)
- continue;
- child.SetSize(child.width, viewHeight, true);
- }
- }
- ch = Mathf.CeilToInt(maxHeight);
- }
- else if (_layout == ListLayoutType.FlowHorizontal)
- {
- if (_autoResizeItem && _columnCount > 0)
- {
- float lineSize = 0;
- int lineStart = 0;
- float remainSize;
- float remainPercent;
- for (i = 0; i < cnt; i++)
- {
- child = GetChildAt(i);
- if (foldInvisibleItems && !child.visible)
- continue;
- lineSize += child.sourceWidth;
- j++;
- if (j == _columnCount || i == cnt - 1)
- {
- remainSize = viewWidth - (j - 1) * _columnGap;
- remainPercent = 1;
- curX = 0;
- for (j = lineStart; j <= i; j++)
- {
- child = GetChildAt(j);
- if (foldInvisibleItems && !child.visible)
- continue;
- child.SetXY(curX, curY);
- float perc = child.sourceWidth / lineSize;
- child.SetSize(Mathf.Round(perc / remainPercent * remainSize), child.height, true);
- remainSize -= child.width;
- remainPercent -= perc;
- curX += child.width + _columnGap;
- if (child.height > maxHeight)
- maxHeight = child.height;
- }
- //new line
- curY += Mathf.CeilToInt(maxHeight) + _lineGap;
- maxHeight = 0;
- j = 0;
- lineStart = i + 1;
- lineSize = 0;
- }
- }
- ch = curY + Mathf.CeilToInt(maxHeight);
- cw = viewWidth;
- }
- else
- {
- for (i = 0; i < cnt; i++)
- {
- child = GetChildAt(i);
- if (foldInvisibleItems && !child.visible)
- continue;
- if (curX != 0)
- curX += _columnGap;
- if (_columnCount != 0 && j >= _columnCount
- || _columnCount == 0 && curX + child.width > viewWidth && maxHeight != 0)
- {
- //new line
- curX = 0;
- curY += Mathf.CeilToInt(maxHeight) + _lineGap;
- maxHeight = 0;
- j = 0;
- }
- child.SetXY(curX, curY);
- curX += Mathf.CeilToInt(child.width);
- if (curX > maxWidth)
- maxWidth = curX;
- if (child.height > maxHeight)
- maxHeight = child.height;
- j++;
- }
- ch = curY + Mathf.CeilToInt(maxHeight);
- cw = Mathf.CeilToInt(maxWidth);
- }
- }
- else if (_layout == ListLayoutType.FlowVertical)
- {
- if (_autoResizeItem && _lineCount > 0)
- {
- float lineSize = 0;
- int lineStart = 0;
- float remainSize;
- float remainPercent;
- for (i = 0; i < cnt; i++)
- {
- child = GetChildAt(i);
- if (foldInvisibleItems && !child.visible)
- continue;
- lineSize += child.sourceHeight;
- j++;
- if (j == _lineCount || i == cnt - 1)
- {
- remainSize = viewHeight - (j - 1) * _lineGap;
- remainPercent = 1;
- curY = 0;
- for (j = lineStart; j <= i; j++)
- {
- child = GetChildAt(j);
- if (foldInvisibleItems && !child.visible)
- continue;
- child.SetXY(curX, curY);
- float perc = child.sourceHeight / lineSize;
- child.SetSize(child.width, Mathf.Round(perc / remainPercent * remainSize), true);
- remainSize -= child.height;
- remainPercent -= perc;
- curY += child.height + _lineGap;
- if (child.width > maxWidth)
- maxWidth = child.width;
- }
- //new line
- curX += Mathf.CeilToInt(maxWidth) + _columnGap;
- maxWidth = 0;
- j = 0;
- lineStart = i + 1;
- lineSize = 0;
- }
- }
- cw = curX + Mathf.CeilToInt(maxWidth);
- ch = viewHeight;
- }
- else
- {
- for (i = 0; i < cnt; i++)
- {
- child = GetChildAt(i);
- if (foldInvisibleItems && !child.visible)
- continue;
- if (curY != 0)
- curY += _lineGap;
- if (_lineCount != 0 && j >= _lineCount
- || _lineCount == 0 && curY + child.height > viewHeight && maxWidth != 0)
- {
- curY = 0;
- curX += Mathf.CeilToInt(maxWidth) + _columnGap;
- maxWidth = 0;
- j = 0;
- }
- child.SetXY(curX, curY);
- curY += child.height;
- if (curY > maxHeight)
- maxHeight = curY;
- if (child.width > maxWidth)
- maxWidth = child.width;
- j++;
- }
- cw = curX + Mathf.CeilToInt(maxWidth);
- ch = Mathf.CeilToInt(maxHeight);
- }
- }
- else //pagination
- {
- int page = 0;
- int k = 0;
- float eachHeight = 0;
- if (_autoResizeItem && _lineCount > 0)
- eachHeight = Mathf.Floor((viewHeight - (_lineCount - 1) * _lineGap) / _lineCount);
- if (_autoResizeItem && _columnCount > 0)
- {
- float lineSize = 0;
- int lineStart = 0;
- float remainSize;
- float remainPercent;
- for (i = 0; i < cnt; i++)
- {
- child = GetChildAt(i);
- if (foldInvisibleItems && !child.visible)
- continue;
- if (j == 0 && (_lineCount != 0 && k >= _lineCount
- || _lineCount == 0 && curY + (_lineCount > 0 ? eachHeight : child.height) > viewHeight))
- {
- //new page
- page++;
- curY = 0;
- k = 0;
- }
- lineSize += child.sourceWidth;
- j++;
- if (j == _columnCount || i == cnt - 1)
- {
- remainSize = viewWidth - (j - 1) * _columnGap;
- remainPercent = 1;
- curX = 0;
- for (j = lineStart; j <= i; j++)
- {
- child = GetChildAt(j);
- if (foldInvisibleItems && !child.visible)
- continue;
- child.SetXY(page * viewWidth + curX, curY);
- float perc = child.sourceWidth / lineSize;
- child.SetSize(Mathf.Round(perc / remainPercent * remainSize), _lineCount > 0 ? eachHeight : child.height, true);
- remainSize -= child.width;
- remainPercent -= perc;
- curX += child.width + _columnGap;
- if (child.height > maxHeight)
- maxHeight = child.height;
- }
- //new line
- curY += Mathf.CeilToInt(maxHeight) + _lineGap;
- maxHeight = 0;
- j = 0;
- lineStart = i + 1;
- lineSize = 0;
- k++;
- }
- }
- }
- else
- {
- for (i = 0; i < cnt; i++)
- {
- child = GetChildAt(i);
- if (foldInvisibleItems && !child.visible)
- continue;
- if (curX != 0)
- curX += _columnGap;
- if (_autoResizeItem && _lineCount > 0)
- child.SetSize(child.width, eachHeight, true);
- if (_columnCount != 0 && j >= _columnCount
- || _columnCount == 0 && curX + child.width > viewWidth && maxHeight != 0)
- {
- curX = 0;
- curY += maxHeight + _lineGap;
- maxHeight = 0;
- j = 0;
- k++;
- if (_lineCount != 0 && k >= _lineCount
- || _lineCount == 0 && curY + child.height > viewHeight && maxWidth != 0)//new page
- {
- page++;
- curY = 0;
- k = 0;
- }
- }
- child.SetXY(page * viewWidth + curX, curY);
- curX += Mathf.CeilToInt(child.width);
- if (curX > maxWidth)
- maxWidth = curX;
- if (child.height > maxHeight)
- maxHeight = child.height;
- j++;
- }
- }
- ch = page > 0 ? viewHeight : (curY + Mathf.CeilToInt(maxHeight));
- cw = (page + 1) * viewWidth;
- }
- HandleAlign(cw, ch);
- SetBounds(0, 0, cw, ch);
- InvalidateBatchingState(true);
- }
- override public void Setup_BeforeAdd(ByteBuffer buffer, int beginPos)
- {
- base.Setup_BeforeAdd(buffer, beginPos);
- buffer.Seek(beginPos, 5);
- _layout = (ListLayoutType)buffer.ReadByte();
- selectionMode = (ListSelectionMode)buffer.ReadByte();
- _align = (AlignType)buffer.ReadByte();
- _verticalAlign = (VertAlignType)buffer.ReadByte();
- _lineGap = buffer.ReadShort();
- _columnGap = buffer.ReadShort();
- _lineCount = buffer.ReadShort();
- _columnCount = buffer.ReadShort();
- _autoResizeItem = buffer.ReadBool();
- _childrenRenderOrder = (ChildrenRenderOrder)buffer.ReadByte();
- _apexIndex = buffer.ReadShort();
- if (buffer.ReadBool())
- {
- _margin.top = buffer.ReadInt();
- _margin.bottom = buffer.ReadInt();
- _margin.left = buffer.ReadInt();
- _margin.right = buffer.ReadInt();
- }
- OverflowType overflow = (OverflowType)buffer.ReadByte();
- if (overflow == OverflowType.Scroll)
- {
- int savedPos = buffer.position;
- buffer.Seek(beginPos, 7);
- SetupScroll(buffer);
- buffer.position = savedPos;
- }
- else
- SetupOverflow(overflow);
- if (buffer.ReadBool())
- {
- int i1 = buffer.ReadInt();
- int i2 = buffer.ReadInt();
- this.clipSoftness = new Vector2(i1, i2);
- }
- if (buffer.version >= 2)
- {
- scrollItemToViewOnClick = buffer.ReadBool();
- foldInvisibleItems = buffer.ReadBool();
- }
- buffer.Seek(beginPos, 8);
- _defaultItem = buffer.ReadS();
- ReadItems(buffer);
- }
- virtual protected void ReadItems(ByteBuffer buffer)
- {
- int itemCount = buffer.ReadShort();
- for (int i = 0; i < itemCount; i++)
- {
- int nextPos = buffer.ReadUshort();
- nextPos += buffer.position;
- string str = buffer.ReadS();
- if (str == null)
- {
- str = _defaultItem;
- if (string.IsNullOrEmpty(str))
- {
- buffer.position = nextPos;
- continue;
- }
- }
- GObject obj = GetFromPool(str);
- if (obj != null)
- {
- AddChild(obj);
- SetupItem(buffer, obj);
- }
- buffer.position = nextPos;
- }
- }
- protected void SetupItem(ByteBuffer buffer, GObject obj)
- {
- string str;
- str = buffer.ReadS();
- if (str != null)
- obj.text = str;
- str = buffer.ReadS();
- if (str != null && (obj is GButton))
- (obj as GButton).selectedTitle = str;
- str = buffer.ReadS();
- if (str != null)
- obj.icon = str;
- str = buffer.ReadS();
- if (str != null && (obj is GButton))
- (obj as GButton).selectedIcon = str;
- str = buffer.ReadS();
- if (str != null)
- obj.name = str;
- if (obj is GComponent)
- {
- int cnt = buffer.ReadShort();
- for (int i = 0; i < cnt; i++)
- {
- Controller cc = ((GComponent)obj).GetController(buffer.ReadS());
- str = buffer.ReadS();
- if (cc != null)
- cc.selectedPageId = str;
- }
- if (buffer.version >= 2)
- {
- cnt = buffer.ReadShort();
- for (int i = 0; i < cnt; i++)
- {
- string target = buffer.ReadS();
- int propertyId = buffer.ReadShort();
- string value = buffer.ReadS();
- GObject obj2 = ((GComponent)obj).GetChildByPath(target);
- if (obj2 != null)
- {
- if (propertyId == 0)
- obj2.text = value;
- else if (propertyId == 1)
- obj2.icon = value;
- }
- }
- }
- }
- }
- override public void Setup_AfterAdd(ByteBuffer buffer, int beginPos)
- {
- base.Setup_AfterAdd(buffer, beginPos);
- buffer.Seek(beginPos, 6);
- int i = buffer.ReadShort();
- if (i != -1)
- _selectionController = parent.GetControllerAt(i);
- }
- }
- }
|