guiTreeViewCtrl.cpp 157 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "platform/platform.h"
  23. #include "gui/controls/guiTreeViewCtrl.h"
  24. #include "core/frameAllocator.h"
  25. #include "core/strings/findMatch.h"
  26. #include "gui/containers/guiScrollCtrl.h"
  27. #include "gui/worldEditor/editorIconRegistry.h"
  28. #include "console/consoleTypes.h"
  29. #include "console/console.h"
  30. #include "gui/core/guiTypes.h"
  31. #include "platform/event.h"
  32. #include "gfx/gfxDrawUtil.h"
  33. #include "gui/controls/guiTextEditCtrl.h"
  34. #include "gui/editor/editorFunctions.h"
  35. #include "console/engineAPI.h"
  36. IMPLEMENT_CONOBJECT(GuiTreeViewCtrl);
  37. ConsoleDocClass( GuiTreeViewCtrl,
  38. "@brief Hierarchical list of text items with optional icons.\n\n"
  39. "Can also be used to inspect SimObject hierarchies, primarily within editors.\n\n"
  40. "GuiTreeViewCtrls can either display arbitrary user-defined trees or can be used to display SimObject hierarchies where "
  41. "each parent node in the tree is a SimSet or SimGroup and each leaf node is a SimObject.\n\n"
  42. "Each item in the tree has a text and a value. For trees that display SimObject hierarchies, the text for each item "
  43. "is automatically derived from objects while the value for each item is the ID of the respective SimObject. For trees "
  44. "that are not tied to SimObjects, both text and value of each item are set by the user.\n\n"
  45. "Additionally, items in the tree can have icons.\n\n"
  46. "Each item in the tree has a distinct numeric ID that is unique within its tree. The ID of the root item, which is always "
  47. "present on a tree, is 0.\n\n"
  48. "@tsexample\n"
  49. "new GuiTreeViewCtrl(DatablockEditorTree)\n"
  50. "{\n"
  51. " tabSize = \"16\";\n"
  52. " textOffset = \"2\";\n"
  53. " fullRowSelect = \"0\";\n"
  54. " itemHeight = \"21\";\n"
  55. " destroyTreeOnSleep = \"0\";\n"
  56. " MouseDragging = \"0\";\n"
  57. " MultipleSelections = \"1\";\n"
  58. " DeleteObjectAllowed = \"1\";\n"
  59. " DragToItemAllowed = \"0\";\n"
  60. " ClearAllOnSingleSelection = \"1\";\n"
  61. " showRoot = \"1\";\n"
  62. " internalNamesOnly = \"0\";\n"
  63. " objectNamesOnly = \"0\";\n"
  64. " compareToObjectID = \"0\";\n"
  65. " Profile = \"GuiTreeViewProfile\";\n"
  66. " tooltipprofile = \"GuiToolTipProfile\";\n"
  67. " hovertime = \"1000\";\n"
  68. "};\n"
  69. "@endtsexample\n\n"
  70. "@ingroup GuiContainers\n");
  71. IMPLEMENT_CALLBACK( GuiTreeViewCtrl, onDeleteObject, bool, ( SimObject* object ), ( object ), "" );
  72. IMPLEMENT_CALLBACK( GuiTreeViewCtrl, isValidDragTarget, bool, ( S32 id, const char* value ), ( id, value ), "" );
  73. IMPLEMENT_CALLBACK( GuiTreeViewCtrl, onDefineIcons, void, (), (), "" );
  74. IMPLEMENT_CALLBACK( GuiTreeViewCtrl, onAddGroupSelected, void, ( SimGroup* group ), ( group ), "" );
  75. IMPLEMENT_CALLBACK( GuiTreeViewCtrl, onAddSelection, void, ( S32 itemOrObjectId, bool isLastSelection ), ( itemOrObjectId, isLastSelection ), "" );
  76. IMPLEMENT_CALLBACK( GuiTreeViewCtrl, onSelect, void, ( S32 itemOrObjectId ), ( itemOrObjectId ), "" );
  77. IMPLEMENT_CALLBACK( GuiTreeViewCtrl, onInspect, void, ( S32 itemOrObjectId ), ( itemOrObjectId ), "" );
  78. IMPLEMENT_CALLBACK( GuiTreeViewCtrl, onRemoveSelection, void, ( S32 itemOrObjectId ), ( itemOrObjectId ), "" );
  79. IMPLEMENT_CALLBACK( GuiTreeViewCtrl, onUnselect, void, ( S32 itemOrObjectId ), ( itemOrObjectId ), "" );
  80. IMPLEMENT_CALLBACK( GuiTreeViewCtrl, onDeleteSelection, void, (), (), "" );
  81. IMPLEMENT_CALLBACK( GuiTreeViewCtrl, onObjectDeleteCompleted, void, (), (), "" );
  82. IMPLEMENT_CALLBACK( GuiTreeViewCtrl, onKeyDown, void, ( S32 modifier, S32 keyCode ), ( modifier, keyCode ), "" );
  83. IMPLEMENT_CALLBACK( GuiTreeViewCtrl, onMouseUp, void, ( S32 hitItemId, S32 mouseClickCount ), ( hitItemId, mouseClickCount ), "" );
  84. IMPLEMENT_CALLBACK( GuiTreeViewCtrl, onMouseDragged, void, (), (), "" );
  85. IMPLEMENT_CALLBACK( GuiTreeViewCtrl, onRightMouseDown, void, ( S32 itemId, const Point2I& mousePos, SimObject* object ), ( itemId, mousePos, object ), "" );
  86. IMPLEMENT_CALLBACK( GuiTreeViewCtrl, onRightMouseUp, void, ( S32 itemId, const Point2I& mousePos, SimObject* object ), ( itemId, mousePos, object ), "" );
  87. IMPLEMENT_CALLBACK( GuiTreeViewCtrl, onBeginReparenting, void, (), (), "" );
  88. IMPLEMENT_CALLBACK( GuiTreeViewCtrl, onEndReparenting, void, (), (), "" );
  89. IMPLEMENT_CALLBACK( GuiTreeViewCtrl, onReparent, void, ( S32 itemOrObjectId, S32 oldParentItemOrObjectId, S32 newParentItemOrObjectId ), ( itemOrObjectId, oldParentItemOrObjectId, newParentItemOrObjectId ), "" );
  90. IMPLEMENT_CALLBACK( GuiTreeViewCtrl, onDragDropped, void, (), (), "" );
  91. IMPLEMENT_CALLBACK( GuiTreeViewCtrl, onAddMultipleSelectionBegin, void, (), (), "" );
  92. IMPLEMENT_CALLBACK( GuiTreeViewCtrl, onAddMultipleSelectionEnd, void, (), (), "" );
  93. IMPLEMENT_CALLBACK( GuiTreeViewCtrl, canRenameObject, bool, ( SimObject* object ), ( object ), "" );
  94. IMPLEMENT_CALLBACK( GuiTreeViewCtrl, handleRenameObject, bool, ( const char* newName, SimObject* object ), ( newName, object ), "" );
  95. IMPLEMENT_CALLBACK( GuiTreeViewCtrl, onClearSelection, void, (), (), "" );
  96. static S32 QSORT_CALLBACK itemCompareCaseSensitive( const void *a, const void *b )
  97. {
  98. GuiTreeViewCtrl::Item* itemA = *( ( GuiTreeViewCtrl::Item** ) a );
  99. GuiTreeViewCtrl::Item* itemB = *( ( GuiTreeViewCtrl::Item** ) b );
  100. char bufferA[ 1024 ];
  101. char bufferB[ 1024 ];
  102. itemA->getDisplayText( sizeof( bufferA ), bufferA );
  103. itemB->getDisplayText( sizeof( bufferB ), bufferB );
  104. return dStrnatcmp( bufferA, bufferB );
  105. }
  106. static S32 QSORT_CALLBACK itemCompareCaseInsensitive( const void *a, const void *b )
  107. {
  108. GuiTreeViewCtrl::Item* itemA = *( ( GuiTreeViewCtrl::Item** ) a );
  109. GuiTreeViewCtrl::Item* itemB = *( ( GuiTreeViewCtrl::Item** ) b );
  110. char bufferA[ 1024 ];
  111. char bufferB[ 1024 ];
  112. itemA->getDisplayText( sizeof( bufferA ), bufferA );
  113. itemB->getDisplayText( sizeof( bufferB ), bufferB );
  114. return dStrnatcasecmp( bufferA, bufferB );
  115. }
  116. static void itemSortList( GuiTreeViewCtrl::Item*& firstChild, bool caseSensitive, bool traverseHierarchy, bool parentsFirst )
  117. {
  118. // Sort the children.
  119. // Do this in a separate scope, so we release the buffers before
  120. // recursing.
  121. {
  122. Vector< GuiTreeViewCtrl::Item* > parents;
  123. Vector< GuiTreeViewCtrl::Item* > items;
  124. // Put all items into the two vectors.
  125. for( GuiTreeViewCtrl::Item* item = firstChild; item != NULL; item = item->mNext )
  126. if( parentsFirst && item->isParent() )
  127. parents.push_back( item );
  128. else
  129. items.push_back( item );
  130. // Sort both vectors.
  131. dQsort( parents.address(), parents.size(), sizeof( GuiTreeViewCtrl::Item* ), caseSensitive ? itemCompareCaseSensitive : itemCompareCaseInsensitive );
  132. dQsort( items.address(), items.size(), sizeof( GuiTreeViewCtrl::Item* ), caseSensitive ? itemCompareCaseSensitive : itemCompareCaseInsensitive );
  133. // Wipe current child chain then reconstruct it in reverse
  134. // as we prepend items.
  135. firstChild = NULL;
  136. // Add child items.
  137. for( U32 i = items.size(); i > 0; -- i )
  138. {
  139. GuiTreeViewCtrl::Item* child = items[ i - 1 ];
  140. child->mNext = firstChild;
  141. if( firstChild )
  142. firstChild->mPrevious = child;
  143. firstChild = child;
  144. }
  145. // Add parent child items, if requested.
  146. for( U32 i = parents.size(); i > 0; -- i )
  147. {
  148. GuiTreeViewCtrl::Item* child = parents[ i - 1 ];
  149. child->mNext = firstChild;
  150. if( firstChild )
  151. firstChild->mPrevious = child;
  152. firstChild = child;
  153. }
  154. firstChild->mPrevious = NULL;
  155. }
  156. // Traverse hierarchy, if requested.
  157. if( traverseHierarchy )
  158. {
  159. GuiTreeViewCtrl::Item* child = firstChild;
  160. while( child )
  161. {
  162. if( child->isParent() )
  163. child->sort( caseSensitive, traverseHierarchy, parentsFirst );
  164. child = child->mNext;
  165. }
  166. }
  167. }
  168. //=============================================================================
  169. // GuiTreeViewCtrl::Item.
  170. //=============================================================================
  171. // MARK: ---- GuiTreeViewCtrl::Item ----
  172. //-----------------------------------------------------------------------------
  173. GuiTreeViewCtrl::Item::Item( GuiTreeViewCtrl* parent, GuiControlProfile *pProfile )
  174. {
  175. AssertFatal( pProfile != NULL , "Cannot create a tree item without a valid tree and control profile!");
  176. mParentControl = parent;
  177. mState = 0;
  178. mId = -1;
  179. mTabLevel = 0;
  180. mIcon = 0;
  181. mDataRenderWidth = 0;
  182. mParent = NULL;
  183. mChild = NULL;
  184. mNext = NULL;
  185. mPrevious = NULL;
  186. mProfile = pProfile;
  187. mScriptInfo.mNormalImage = BmpCon;
  188. mScriptInfo.mExpandedImage = BmpExp;
  189. mScriptInfo.mText = NULL;
  190. mScriptInfo.mValue = NULL;
  191. }
  192. //-----------------------------------------------------------------------------
  193. GuiTreeViewCtrl::Item::~Item()
  194. {
  195. _disconnectMonitors();
  196. }
  197. //-----------------------------------------------------------------------------
  198. void GuiTreeViewCtrl::Item::_connectMonitors()
  199. {
  200. if( mInspectorInfo.mObject != NULL )
  201. {
  202. SimSet* set = dynamic_cast< SimSet* >( mInspectorInfo.mObject.getPointer() );
  203. if( set )
  204. set->getSetModificationSignal().notify( mParentControl, &GuiTreeViewCtrl::_onInspectorSetObjectModified );
  205. }
  206. }
  207. //-----------------------------------------------------------------------------
  208. void GuiTreeViewCtrl::Item::_disconnectMonitors()
  209. {
  210. if( mInspectorInfo.mObject != NULL )
  211. {
  212. SimSet* set = dynamic_cast< SimSet* >( mInspectorInfo.mObject.getPointer() );
  213. if( set )
  214. set->getSetModificationSignal().remove( mParentControl, &GuiTreeViewCtrl::_onInspectorSetObjectModified );
  215. }
  216. }
  217. //-----------------------------------------------------------------------------
  218. void GuiTreeViewCtrl::Item::setNormalImage(S8 id)
  219. {
  220. if(mState.test(InspectorData))
  221. {
  222. Con::errorf("Tried to set normal image %d for item %d, which is InspectorData!", id, mId);
  223. return;
  224. }
  225. mScriptInfo.mNormalImage = id;
  226. }
  227. //-----------------------------------------------------------------------------
  228. void GuiTreeViewCtrl::Item::setExpandedImage(S8 id)
  229. {
  230. if(mState.test(InspectorData))
  231. {
  232. Con::errorf("Tried to set expanded image %d for item %d, which is InspectorData!", id, mId);
  233. return;
  234. }
  235. mScriptInfo.mExpandedImage = id;
  236. }
  237. //-----------------------------------------------------------------------------
  238. void GuiTreeViewCtrl::Item::setText(StringTableEntry txt)
  239. {
  240. if(mState.test(InspectorData))
  241. {
  242. Con::errorf("Tried to set text for item %d, which is InspectorData!", mId);
  243. return;
  244. }
  245. mScriptInfo.mText = txt;
  246. // Update Render Data
  247. if( !mProfile.isNull() )
  248. mDataRenderWidth = getDisplayTextWidth( mProfile->mFont );
  249. }
  250. //-----------------------------------------------------------------------------
  251. void GuiTreeViewCtrl::Item::setValue(StringTableEntry val)
  252. {
  253. if(mState.test(InspectorData))
  254. {
  255. Con::errorf("Tried to set value for item %d, which is InspectorData!", mId);
  256. return;
  257. }
  258. mScriptInfo.mValue = const_cast<char*>(val); // mValue really ought to be a StringTableEntry
  259. // Update Render Data
  260. if( !mProfile.isNull() )
  261. mDataRenderWidth = getDisplayTextWidth( mProfile->mFont );
  262. }
  263. //-----------------------------------------------------------------------------
  264. S8 GuiTreeViewCtrl::Item::getNormalImage() const
  265. {
  266. if(mState.test(InspectorData))
  267. {
  268. Con::errorf("Tried to get the normal image for item %d, which is InspectorData!", mId);
  269. return 0; // fail safe for width determinations
  270. }
  271. return mScriptInfo.mNormalImage;
  272. }
  273. //-----------------------------------------------------------------------------
  274. S8 GuiTreeViewCtrl::Item::getExpandedImage() const
  275. {
  276. if(mState.test(InspectorData))
  277. {
  278. Con::errorf("Tried to get the expanded image for item %d, which is InspectorData!", mId);
  279. return 0; // fail safe for width determinations
  280. }
  281. return mScriptInfo.mExpandedImage;
  282. }
  283. //-----------------------------------------------------------------------------
  284. StringTableEntry GuiTreeViewCtrl::Item::getText()
  285. {
  286. if(mState.test(InspectorData))
  287. {
  288. Con::errorf("Tried to get the text for item %d, which is InspectorData!", mId);
  289. return NULL;
  290. }
  291. return ( mScriptInfo.mText ) ? mScriptInfo.mText : StringTable->EmptyString();
  292. }
  293. //-----------------------------------------------------------------------------
  294. StringTableEntry GuiTreeViewCtrl::Item::getValue()
  295. {
  296. if(mState.test(InspectorData))
  297. {
  298. Con::errorf("Tried to get the value for item %d, which is InspectorData!", mId);
  299. return NULL;
  300. }
  301. return ( mScriptInfo.mValue ) ? mScriptInfo.mValue : StringTable->EmptyString();
  302. }
  303. //-----------------------------------------------------------------------------
  304. void GuiTreeViewCtrl::Item::setObject(SimObject *obj)
  305. {
  306. if(!mState.test(InspectorData))
  307. {
  308. Con::errorf("Tried to set the object for item %d, which is not InspectorData!", mId);
  309. return;
  310. }
  311. _disconnectMonitors();
  312. mInspectorInfo.mObject = obj;
  313. _connectMonitors();
  314. // Update Render Data
  315. if( !mProfile.isNull() )
  316. mDataRenderWidth = getDisplayTextWidth( mProfile->mFont );
  317. }
  318. //-----------------------------------------------------------------------------
  319. SimObject *GuiTreeViewCtrl::Item::getObject()
  320. {
  321. if(!mState.test(InspectorData))
  322. {
  323. Con::errorf("Tried to get the object for item %d, which is not InspectorData!", mId);
  324. return NULL;
  325. }
  326. return mInspectorInfo.mObject;
  327. }
  328. //-----------------------------------------------------------------------------
  329. U32 GuiTreeViewCtrl::Item::getDisplayTextLength()
  330. {
  331. if( mState.test( InspectorData ) )
  332. {
  333. SimObject *obj = getObject();
  334. if( !obj )
  335. return 0;
  336. StringTableEntry name = obj->getName();
  337. StringTableEntry internalName = obj->getInternalName();
  338. StringTableEntry className = obj->getClassName();
  339. if( showInternalNameOnly() )
  340. {
  341. if( internalName && internalName[ 0 ] )
  342. return dStrlen( internalName );
  343. else
  344. return dStrlen( "(none)" );
  345. }
  346. else if( showObjectNameOnly() )
  347. {
  348. if( name && name[ 0 ] )
  349. return dStrlen( name );
  350. else if( mState.test( ShowClassNameForUnnamed ) )
  351. return dStrlen( className );
  352. else
  353. return dStrlen( "(none)" );
  354. }
  355. dsize_t len = 0;
  356. if( mState.test( ShowObjectId ) )
  357. len += dStrlen( obj->getIdString() ) + 2; // '<id>: '
  358. if( mState.test( ShowClassName ) )
  359. {
  360. if( name && name[ 0 ] )
  361. len += dStrlen( className ) + 3; // '<class> - '
  362. else
  363. len += dStrlen( className );
  364. }
  365. if( mState.test( ShowObjectName ) )
  366. {
  367. if( name && name[ 0 ] )
  368. len += dStrlen( name );
  369. else if( mState.test( ShowClassNameForUnnamed ) )
  370. len += dStrlen( className );
  371. }
  372. if( mState.test( ShowInternalName ) )
  373. {
  374. if( internalName && internalName[ 0 ] )
  375. len += dStrlen( internalName ) + 3; // ' [<internalname>]'
  376. }
  377. return len;
  378. }
  379. StringTableEntry pText = getText();
  380. if( pText == NULL )
  381. return 0;
  382. return dStrlen( pText );
  383. }
  384. //-----------------------------------------------------------------------------
  385. void GuiTreeViewCtrl::Item::getDisplayText(U32 bufLen, char *buf)
  386. {
  387. FrameAllocatorMarker txtAlloc;
  388. if( mState.test( InspectorData ) )
  389. {
  390. SimObject *pObject = getObject();
  391. if( pObject )
  392. {
  393. const char* pObjName = pObject->getName();
  394. const char* pInternalName = pObject->getInternalName();
  395. bool hasInternalName = pInternalName && pInternalName[0];
  396. bool hasObjectName = pObjName && pObjName[0];
  397. const char* pClassName = pObject->getClassName();
  398. if( showInternalNameOnly() )
  399. dSprintf( buf, bufLen, "%s", hasInternalName ? pInternalName : "(none)" );
  400. else if( showObjectNameOnly() )
  401. {
  402. if( !hasObjectName && mState.test( ShowClassNameForUnnamed ) )
  403. dSprintf( buf, bufLen, "%s", pClassName );
  404. else
  405. dSprintf( buf, bufLen, "%s", hasObjectName ? pObjName : "(none)" );
  406. }
  407. else
  408. {
  409. char* ptr = buf;
  410. int len = bufLen;
  411. if( mState.test( ShowObjectId ) )
  412. {
  413. S32 n = dSprintf( ptr, len, "%d: ", pObject->getId() );
  414. ptr += n;
  415. len -= n;
  416. }
  417. if( mState.test( ShowClassName ) )
  418. {
  419. S32 n;
  420. if( hasObjectName && mState.test( ShowObjectName ) )
  421. n = dSprintf( ptr, len, "%s - ", pClassName );
  422. else
  423. n = dSprintf( ptr, len, "%s", pClassName );
  424. ptr += n;
  425. len -= n;
  426. }
  427. if( mState.test( ShowObjectName ) )
  428. {
  429. S32 n = 0;
  430. if( hasObjectName )
  431. n = dSprintf( ptr, len, "%s", pObjName );
  432. else if( mState.test( ShowClassNameForUnnamed ) )
  433. n = dSprintf( ptr, len, "%s", pClassName );
  434. ptr += n;
  435. len -= n;
  436. }
  437. if( hasInternalName && mState.test( ShowInternalName ) )
  438. dSprintf( ptr, len, " [%s]", pInternalName );
  439. }
  440. }
  441. else
  442. buf[ 0 ] = '\0';
  443. }
  444. else
  445. {
  446. // Script data! (copy it in)
  447. dStrncpy(buf, getText(), bufLen);
  448. }
  449. }
  450. //-----------------------------------------------------------------------------
  451. S32 GuiTreeViewCtrl::Item::getDisplayTextWidth(GFont *font)
  452. {
  453. if( !font )
  454. return 0;
  455. FrameAllocatorMarker txtAlloc;
  456. U32 bufLen = getDisplayTextLength();
  457. if( bufLen == 0 )
  458. return 0;
  459. // Add space for the string terminator
  460. bufLen++;
  461. char *buf = (char*)txtAlloc.alloc(bufLen);
  462. getDisplayText(bufLen, buf);
  463. return font->getStrWidth(buf);
  464. }
  465. //-----------------------------------------------------------------------------
  466. bool GuiTreeViewCtrl::Item::hasObjectBasedTooltip()
  467. {
  468. if(mState.test(Item::InspectorData))
  469. {
  470. SimObject *pObject = getObject();
  471. if(pObject)
  472. {
  473. const char* pClassName = pObject->getClassName();
  474. // Retrieve custom tooltip string
  475. String method("GetTooltip");
  476. method += pClassName;
  477. if(mParentControl->isMethod(method.c_str()))
  478. {
  479. return true;
  480. }
  481. }
  482. }
  483. return false;
  484. }
  485. //-----------------------------------------------------------------------------
  486. void GuiTreeViewCtrl::Item::getTooltipText(U32 bufLen, char *buf)
  487. {
  488. getDisplayText(bufLen, buf);
  489. if(mState.test(Item::InspectorData))
  490. {
  491. SimObject *pObject = getObject();
  492. if(pObject)
  493. {
  494. const char* pClassName = pObject->getClassName();
  495. // Retrieve custom tooltip string
  496. String method("GetTooltip");
  497. method += pClassName;
  498. if(mParentControl->isMethod(method.c_str()))
  499. {
  500. const char* tooltip = Con::executef( mParentControl, method.c_str(), pObject->getIdString() );
  501. dsize_t len = dStrlen(buf);
  502. S32 newBufLen = bufLen-len;
  503. if(dStrlen(tooltip) > 0 && newBufLen > 0)
  504. {
  505. dSprintf(buf+len, newBufLen, "\n%s", tooltip);
  506. }
  507. }
  508. }
  509. }
  510. }
  511. //-----------------------------------------------------------------------------
  512. bool GuiTreeViewCtrl::Item::isParent() const
  513. {
  514. if(mState.test(VirtualParent))
  515. {
  516. if( !isInspectorData() )
  517. return true;
  518. // Does our object have any children?
  519. if(mInspectorInfo.mObject)
  520. {
  521. SimSet *pSimSet = dynamic_cast<SimSet*>( (SimObject*)mInspectorInfo.mObject);
  522. if ( pSimSet != NULL && pSimSet->size() > 0)
  523. return pSimSet->size();
  524. }
  525. }
  526. // Otherwise, just return whether the child list is populated.
  527. return mChild;
  528. }
  529. //-----------------------------------------------------------------------------
  530. bool GuiTreeViewCtrl::Item::isExpanded() const
  531. {
  532. if(mState.test(InspectorData))
  533. return mInspectorInfo.mObject ? mInspectorInfo.mObject->isExpanded() : false;
  534. else
  535. return mState.test(Expanded);
  536. }
  537. //-----------------------------------------------------------------------------
  538. void GuiTreeViewCtrl::Item::setExpanded(bool f)
  539. {
  540. if( mState.test(InspectorData) )
  541. {
  542. if( !mInspectorInfo.mObject.isNull() )
  543. mInspectorInfo.mObject->setExpanded(f);
  544. }
  545. else
  546. mState.set(Expanded, f);
  547. }
  548. //-----------------------------------------------------------------------------
  549. void GuiTreeViewCtrl::Item::setVirtualParent( bool value )
  550. {
  551. mState.set(VirtualParent, value);
  552. }
  553. //-----------------------------------------------------------------------------
  554. GuiTreeViewCtrl::Item* GuiTreeViewCtrl::Item::findChildByName( const char* name )
  555. {
  556. Item* child = mChild;
  557. while( child )
  558. {
  559. if( dStricmp( child->mScriptInfo.mText, name ) == 0 )
  560. return child;
  561. child = child->mNext;
  562. }
  563. return NULL;
  564. }
  565. //-----------------------------------------------------------------------------
  566. GuiTreeViewCtrl::Item *GuiTreeViewCtrl::Item::findChildByValue(const SimObject *obj)
  567. {
  568. // Iterate over our children and try to find the given
  569. // SimObject
  570. Item *pResultObj = mChild;
  571. while(pResultObj)
  572. {
  573. // CodeReview this check may need to be removed
  574. // if we want to use the tree for data that
  575. // isn't related to SimObject based objects with
  576. // arbitrary values associated with them [5/5/2007 justind]
  577. // Skip non-inspector data stuff.
  578. if(pResultObj->mState.test(InspectorData))
  579. {
  580. if(pResultObj->getObject() == obj)
  581. break; // Whoa.
  582. }
  583. pResultObj = pResultObj->mNext;
  584. }
  585. // If the loop terminated we are NULL, otherwise we have the result in res.
  586. return pResultObj;
  587. }
  588. //-----------------------------------------------------------------------------
  589. GuiTreeViewCtrl::Item *GuiTreeViewCtrl::Item::findChildByValue( StringTableEntry Value )
  590. {
  591. // Iterate over our children and try to find the given Value
  592. // Note : This is a case-insensitive search
  593. Item *pResultObj = mChild;
  594. while(pResultObj)
  595. {
  596. // check the script value of the item against the specified value
  597. if( pResultObj->mScriptInfo.mValue != NULL && dStricmp( pResultObj->mScriptInfo.mValue, Value ) == 0 )
  598. return pResultObj;
  599. pResultObj = pResultObj->mNext;
  600. }
  601. // If the loop terminated we didn't find an item with the specified script value
  602. return NULL;
  603. }
  604. //-----------------------------------------------------------------------------
  605. void GuiTreeViewCtrl::Item::sort( bool caseSensitive, bool traverseHierarchy, bool parentsFirst )
  606. {
  607. itemSortList( mChild, caseSensitive, traverseHierarchy, parentsFirst );
  608. }
  609. //=============================================================================
  610. // GuiTreeViewCtrl.
  611. //=============================================================================
  612. // MARK: ---- GuiTreeViewCtrl ----
  613. //-----------------------------------------------------------------------------
  614. GuiTreeViewCtrl::GuiTreeViewCtrl()
  615. {
  616. VECTOR_SET_ASSOCIATION(mItems);
  617. VECTOR_SET_ASSOCIATION(mVisibleItems);
  618. VECTOR_SET_ASSOCIATION(mSelectedItems);
  619. VECTOR_SET_ASSOCIATION(mSelected);
  620. mItemFreeList = NULL;
  621. mRoot = NULL;
  622. mItemCount = 0;
  623. mSelectedItem = 0;
  624. mStart = 0;
  625. mPossibleRenameItem = NULL;
  626. mRenamingItem = NULL;
  627. mTempItem = NULL;
  628. mRenameCtrl = NULL;
  629. mDraggedToItem = 0;
  630. mCurrentDragCell = 0;
  631. mPreviousDragCell = 0;
  632. mDragMidPoint = NomDragMidPoint;
  633. mMouseDragged = false;
  634. mDebug = false;
  635. // persist info..
  636. mTabSize = 16;
  637. mTextOffset = 2;
  638. mFullRowSelect = false;
  639. mItemHeight = 20;
  640. //
  641. setSize(Point2I(1, 0));
  642. // Set up default state
  643. mFlags.set(ShowTreeLines);
  644. mFlags.set(IsEditable, false);
  645. mDestroyOnSleep = true;
  646. mSupportMouseDragging = true;
  647. mMultipleSelections = true;
  648. mDeleteObjectAllowed = true;
  649. mDragToItemAllowed = true;
  650. mShowRoot = true;
  651. mUseInspectorTooltips = false;
  652. mTooltipOnWidthOnly = false;
  653. mCompareToObjectID = true;
  654. mShowObjectIds = true;
  655. mShowClassNames = true;
  656. mShowObjectNames = true;
  657. mShowInternalNames = true;
  658. mShowClassNameForUnnamedObjects = false;
  659. mFlags.set(RebuildVisible);
  660. mCanRenameObjects = true;
  661. mRenameInternal = false;
  662. mClearAllOnSingleSelection = true;
  663. mBitmapBase = StringTable->insert("");
  664. mTexRollover = NULL;
  665. mTexSelected = NULL;
  666. mRenderTooltipDelegate.bind( this, &GuiTreeViewCtrl::renderTooltip );
  667. }
  668. //-----------------------------------------------------------------------------
  669. GuiTreeViewCtrl::~GuiTreeViewCtrl()
  670. {
  671. _destroyTree();
  672. }
  673. //------------------------------------------------------------------------------
  674. void GuiTreeViewCtrl::initPersistFields()
  675. {
  676. addGroup( "TreeView" );
  677. addField( "tabSize", TypeS32, Offset(mTabSize, GuiTreeViewCtrl));
  678. addField( "textOffset", TypeS32, Offset(mTextOffset, GuiTreeViewCtrl));
  679. addField( "fullRowSelect", TypeBool, Offset(mFullRowSelect, GuiTreeViewCtrl));
  680. addField( "itemHeight", TypeS32, Offset(mItemHeight, GuiTreeViewCtrl));
  681. addField( "destroyTreeOnSleep", TypeBool, Offset(mDestroyOnSleep, GuiTreeViewCtrl),
  682. "If true, the entire tree item hierarchy is deleted when the control goes to sleep." );
  683. addField( "mouseDragging", TypeBool, Offset(mSupportMouseDragging, GuiTreeViewCtrl));
  684. addField( "multipleSelections", TypeBool, Offset(mMultipleSelections, GuiTreeViewCtrl),
  685. "If true, multiple items can be selected concurrently." );
  686. addField( "deleteObjectAllowed", TypeBool, Offset(mDeleteObjectAllowed, GuiTreeViewCtrl));
  687. addField( "dragToItemAllowed", TypeBool, Offset(mDragToItemAllowed, GuiTreeViewCtrl));
  688. addField( "clearAllOnSingleSelection", TypeBool, Offset(mClearAllOnSingleSelection, GuiTreeViewCtrl));
  689. addField( "showRoot", TypeBool, Offset(mShowRoot, GuiTreeViewCtrl),
  690. "If true, the root item is shown in the tree." );
  691. addField( "useInspectorTooltips", TypeBool, Offset(mUseInspectorTooltips, GuiTreeViewCtrl));
  692. addField( "tooltipOnWidthOnly", TypeBool, Offset(mTooltipOnWidthOnly, GuiTreeViewCtrl));
  693. endGroup( "TreeView" );
  694. addGroup( "Inspector Trees" );
  695. addField( "showObjectIds", TypeBool, Offset( mShowObjectIds, GuiTreeViewCtrl ),
  696. "If true, item text labels for objects will include object IDs." );
  697. addField( "showClassNames", TypeBool, Offset( mShowClassNames, GuiTreeViewCtrl ),
  698. "If true, item text labels for objects will include class names." );
  699. addField( "showObjectNames", TypeBool, Offset( mShowObjectNames, GuiTreeViewCtrl ),
  700. "If true, item text labels for objects will include object names." );
  701. addField( "showInternalNames", TypeBool, Offset( mShowInternalNames, GuiTreeViewCtrl ),
  702. "If true, item text labels for obje ts will include internal names." );
  703. addField( "showClassNameForUnnamedObjects", TypeBool, Offset( mShowClassNameForUnnamedObjects, GuiTreeViewCtrl ),
  704. "If true, class names will be used as object names for unnamed objects." );
  705. addField( "compareToObjectID", TypeBool, Offset(mCompareToObjectID, GuiTreeViewCtrl));
  706. addField( "canRenameObjects", TypeBool, Offset(mCanRenameObjects, GuiTreeViewCtrl),
  707. "If true clicking on a selected item ( that is an object and not the root ) will allow you to rename it." );
  708. addField( "renameInternal", TypeBool, Offset(mRenameInternal, GuiTreeViewCtrl),
  709. "If true then object renaming operates on the internalName rather than the object name." );
  710. endGroup( "Inspector Trees" );
  711. Parent::initPersistFields();
  712. }
  713. //------------------------------------------------------------------------------
  714. GuiTreeViewCtrl::Item * GuiTreeViewCtrl::getItem(S32 itemId) const
  715. {
  716. if ( itemId > 0 && itemId <= mItems.size() )
  717. return mItems[itemId-1];
  718. return NULL;
  719. }
  720. //------------------------------------------------------------------------------
  721. GuiTreeViewCtrl::Item * GuiTreeViewCtrl::createItem(S32 icon)
  722. {
  723. Item * pNewItem = NULL;
  724. // grab from the free list?
  725. if( mItemFreeList )
  726. {
  727. pNewItem = mItemFreeList;
  728. mItemFreeList = pNewItem->mNext;
  729. // re-add to vector
  730. mItems[ pNewItem->mId - 1 ] = pNewItem;
  731. }
  732. else
  733. {
  734. pNewItem = new Item( this, mProfile );
  735. AssertFatal( pNewItem != NULL, "Fatal : unable to allocate tree item!");
  736. mItems.push_back( pNewItem );
  737. // set the id
  738. pNewItem->mId = mItems.size();
  739. }
  740. // reset
  741. if (icon)
  742. pNewItem->mIcon = icon;
  743. else
  744. pNewItem->mIcon = Default; //default icon to stick next to an item
  745. pNewItem->mState = Item::ShowObjectId | Item::ShowClassName | Item::ShowObjectName | Item::ShowInternalName;
  746. pNewItem->mTabLevel = 0;
  747. // Null out item pointers
  748. pNewItem->mNext = 0;
  749. pNewItem->mPrevious = 0;
  750. pNewItem->mChild = 0;
  751. pNewItem->mParent = 0;
  752. mItemCount++;
  753. return pNewItem;
  754. }
  755. //------------------------------------------------------------------------------
  756. void GuiTreeViewCtrl::_destroyChildren( Item* item, Item* parent, bool deleteObjects )
  757. {
  758. if ( !item || item == parent || !mItems[item->mId-1] )
  759. return;
  760. // destroy depth first, then siblings from last to first
  761. if ( item->isParent() && item->mChild )
  762. _destroyChildren(item->mChild, item, deleteObjects);
  763. if( item->mNext )
  764. _destroyChildren(item->mNext, parent, deleteObjects);
  765. // destroy the item
  766. _destroyItem( item, deleteObjects );
  767. }
  768. //-----------------------------------------------------------------------------
  769. void GuiTreeViewCtrl::_destroyItem( Item* item, bool deleteObject )
  770. {
  771. if(!item)
  772. return;
  773. if(item->isInspectorData())
  774. {
  775. // make sure the SimObjectPtr is clean!
  776. SimObject *pObject = item->getObject();
  777. if( pObject && pObject->isProperlyAdded() )
  778. {
  779. bool skipDelete = !deleteObject;
  780. if( !skipDelete && isMethod( "onDeleteObject" ) )
  781. skipDelete = onDeleteObject_callback( pObject );
  782. if ( !skipDelete )
  783. pObject->deleteObject();
  784. }
  785. item->setObject( NULL );
  786. }
  787. // Remove item from the selection
  788. if (mSelectedItem == item->mId)
  789. mSelectedItem = 0;
  790. for ( S32 i = 0; i < mSelectedItems.size(); i++ )
  791. {
  792. if ( mSelectedItems[i] == item )
  793. {
  794. mSelectedItems.erase( i );
  795. break;
  796. }
  797. }
  798. item->mState.clear();
  799. // unlink
  800. if( item->mPrevious )
  801. item->mPrevious->mNext = item->mNext;
  802. if( item->mNext )
  803. item->mNext->mPrevious = item->mPrevious;
  804. if( item->mParent && ( item->mParent->mChild == item ) )
  805. item->mParent->mChild = item->mNext;
  806. // remove from vector
  807. mItems[item->mId-1] = 0;
  808. // set as root free item
  809. item->mNext = mItemFreeList;
  810. mItemFreeList = item;
  811. mItemCount--;
  812. }
  813. //------------------------------------------------------------------------------
  814. void GuiTreeViewCtrl::_deleteItem(Item *item)
  815. {
  816. removeItem(item->mId);
  817. }
  818. //------------------------------------------------------------------------------
  819. void GuiTreeViewCtrl::_destroyTree()
  820. {
  821. // clear the item list
  822. for(U32 i = 0; i < mItems.size(); i++)
  823. {
  824. Item *pFreeItem = mItems[ i ];
  825. if( pFreeItem != NULL )
  826. delete pFreeItem;
  827. }
  828. mItems.clear();
  829. // clear the free list
  830. while(mItemFreeList)
  831. {
  832. Item *next = mItemFreeList->mNext;
  833. delete mItemFreeList;
  834. mItemFreeList = next;
  835. }
  836. mVisibleItems.clear();
  837. mSelectedItems.clear();
  838. //
  839. mRoot = NULL;
  840. mItemFreeList = NULL;
  841. mItemCount = 0;
  842. mSelectedItem = 0;
  843. mDraggedToItem = 0;
  844. mRenamingItem = NULL;
  845. mTempItem = NULL;
  846. mPossibleRenameItem = NULL;
  847. }
  848. //------------------------------------------------------------------------------
  849. void GuiTreeViewCtrl::_onInspectorSetObjectModified( SetModification modification, SimSet* set, SimObject* object )
  850. {
  851. // Don't bother searching for the Item to see if it is actually visible and instead just
  852. // mark our tree state as dirty so we get a rebuild on the next render.
  853. mFlags.set( RebuildVisible );
  854. }
  855. //------------------------------------------------------------------------------
  856. GuiTreeViewCtrl::Item* GuiTreeViewCtrl::_findItemByAmbiguousId( S32 itemOrObjectId, bool buildVirtual )
  857. {
  858. Item* item = getItem( itemOrObjectId );
  859. if( item )
  860. return item;
  861. SimObject* object = Sim::findObject( itemOrObjectId );
  862. if( object )
  863. {
  864. // If we should expand virtual trees in order to find the item,
  865. // do so now.
  866. if( buildVirtual )
  867. {
  868. if( mFlags.test( RebuildVisible ) )
  869. buildVisibleTree();
  870. SimGroup* group = object->getGroup();
  871. if( group )
  872. _expandObjectHierarchy( group );
  873. }
  874. if( objectSearch( object, &item ) )
  875. return item;
  876. }
  877. return NULL;
  878. }
  879. //------------------------------------------------------------------------------
  880. void GuiTreeViewCtrl::_expandObjectHierarchy( SimGroup* group )
  881. {
  882. SimGroup* parent = group->getGroup();
  883. if( parent && !parent->isExpanded() )
  884. _expandObjectHierarchy( parent );
  885. if( !group->isExpanded() )
  886. {
  887. Item* item;
  888. if( objectSearch( group, &item ) )
  889. {
  890. item->setExpanded();
  891. onVirtualParentBuild( item, false );
  892. }
  893. }
  894. }
  895. //------------------------------------------------------------------------------
  896. void GuiTreeViewCtrl::_buildItem( Item* item, U32 tabLevel, bool bForceFullUpdate )
  897. {
  898. if (!item || !mActive || !isVisible() || !mProfile )
  899. return;
  900. // If it's inspector data, make sure we still have it, if not, kill it.
  901. if(item->isInspectorData() && !item->getObject() )
  902. {
  903. removeItem(item->mId);
  904. return;
  905. }
  906. // If it's a virtual parent, give a chance to update itself...
  907. if(item->mState.test( Item::VirtualParent) )
  908. {
  909. // If it returns false the item has been removed.
  910. if( !onVirtualParentBuild( item, bForceFullUpdate ) )
  911. return;
  912. }
  913. // If we have a filter pattern, sync the item's filtering status to it.
  914. if( !getFilterText().isEmpty() )
  915. {
  916. // Determine the filtering status by looking for the filter
  917. // text in the item's display text.
  918. char displayText[ 2048 ];
  919. item->getDisplayText( sizeof( displayText ), displayText );
  920. if( !dStristr( displayText, mFilterText ) )
  921. {
  922. item->mState.set( Item::Filtered );
  923. // If it's not a parent, we're done. Otherwise, there may be children
  924. // that are not filtered so we need to process them first.
  925. if( !item->isParent() )
  926. return;
  927. }
  928. else
  929. item->mState.clear( Item::Filtered );
  930. }
  931. else
  932. item->mState.clear( Item::Filtered );
  933. // Is this the root item?
  934. const bool isRoot = item == mRoot;
  935. // Add non-root items or the root if we're supposed to show it.
  936. if( ( mShowRoot || !isRoot ) &&
  937. !item->isFiltered() )
  938. {
  939. item->mTabLevel = tabLevel;
  940. mVisibleItems.push_back( item );
  941. if( mProfile != NULL )
  942. {
  943. mProfile->incLoadCount();
  944. S32 width = mTextOffset + ( mTabSize * item->mTabLevel ) + getInspectorItemIconsWidth( item ) + item->getDisplayTextWidth( mProfile->mFont );
  945. // check image
  946. S32 image = BmpChild;
  947. if ( item->isInspectorData() )
  948. image = item->isExpanded() ? BmpExp : BmpCon;
  949. else
  950. image = item->isExpanded() ? item->getExpandedImage() : item->getNormalImage();
  951. if ( ( image >= 0 ) && ( image < mProfile->mBitmapArrayRects.size() ) )
  952. width += mProfile->mBitmapArrayRects[image].extent.x;
  953. if ( width > mMaxWidth )
  954. mMaxWidth = width;
  955. mProfile->decLoadCount();
  956. }
  957. }
  958. // If expanded or a hidden root, add all the
  959. // children items as well.
  960. if ( item->isExpanded() ||
  961. bForceFullUpdate ||
  962. ( isRoot && !mShowRoot ) )
  963. {
  964. Item* child = item->mChild;
  965. while ( child )
  966. {
  967. // Bit of a hack so we can safely remove items as we
  968. // traverse.
  969. Item *pChildTemp = child;
  970. child = child->mNext;
  971. _buildItem( pChildTemp, tabLevel + 1, bForceFullUpdate );
  972. }
  973. }
  974. }
  975. //------------------------------------------------------------------------------
  976. void GuiTreeViewCtrl::buildVisibleTree(bool bForceFullUpdate)
  977. {
  978. // Recursion Prevention.
  979. if( mFlags.test( BuildingVisTree ) )
  980. return;
  981. mFlags.set( BuildingVisTree, true );
  982. if( mDebug )
  983. Con::printf( "Rebuilding visible tree" );
  984. mMaxWidth = 0;
  985. mVisibleItems.clear();
  986. // If we're filtering, force a full update.
  987. if( !mFilterText.isEmpty() )
  988. bForceFullUpdate = true;
  989. // Update the flags.
  990. mFlags.clear(RebuildVisible);
  991. // build the root items
  992. Item *traverse = mRoot;
  993. while(traverse)
  994. {
  995. _buildItem(traverse, 0, bForceFullUpdate);
  996. traverse = traverse->mNext;
  997. }
  998. // adjust the GuiArrayCtrl
  999. mCellSize.set( mMaxWidth + mTextOffset, mItemHeight );
  1000. setSize(Point2I(1, mVisibleItems.size()));
  1001. syncSelection();
  1002. // Done Recursing.
  1003. mFlags.clear( BuildingVisTree );
  1004. }
  1005. //------------------------------------------------------------------------------
  1006. bool GuiTreeViewCtrl::scrollVisible( S32 itemId )
  1007. {
  1008. Item* item = getItem(itemId);
  1009. if(item)
  1010. return scrollVisible(item);
  1011. return false;
  1012. }
  1013. //-----------------------------------------------------------------------------
  1014. bool GuiTreeViewCtrl::scrollVisible( Item *item )
  1015. {
  1016. // Now, make sure it's visible (ie, all parents expanded)
  1017. Item *parent = item->mParent;
  1018. if( !item->isInspectorData() && item->mState.test(Item::VirtualParent) )
  1019. onVirtualParentExpand(item);
  1020. while(parent)
  1021. {
  1022. parent->setExpanded(true);
  1023. if( !parent->isInspectorData() && parent->mState.test(Item::VirtualParent) )
  1024. onVirtualParentExpand(parent);
  1025. parent = parent->mParent;
  1026. }
  1027. // Get our scroll-pappy, if any.
  1028. GuiScrollCtrl *pScrollParent = dynamic_cast<GuiScrollCtrl*>( getParent() );
  1029. if ( !pScrollParent )
  1030. {
  1031. Con::warnf("GuiTreeViewCtrl::scrollVisible - parent control is not a GuiScrollCtrl!");
  1032. return false;
  1033. }
  1034. // And now, build the visible tree so we know where we have to scroll.
  1035. if( mFlags.test( RebuildVisible ) )
  1036. buildVisibleTree();
  1037. // All done, let's figure out where we have to scroll...
  1038. for(S32 i=0; i<mVisibleItems.size(); i++)
  1039. {
  1040. if(mVisibleItems[i] == item)
  1041. {
  1042. // Fetch X Details.
  1043. const S32 xPos = pScrollParent->getChildRelPos().x;
  1044. const S32 xWidth = ( mMaxWidth - xPos );
  1045. // Scroll to View the Item.
  1046. // Note: Delta X should be 0 so that we maintain the X axis position.
  1047. pScrollParent->scrollRectVisible( RectI( xPos, i * mItemHeight, xWidth, mItemHeight ) );
  1048. return true;
  1049. }
  1050. }
  1051. // If we got here, it's probably bad...
  1052. Con::errorf("GuiTreeViewCtrl::scrollVisible - was unable to find specified item in visible list!");
  1053. return false;
  1054. }
  1055. //------------------------------------------------------------------------------
  1056. S32 GuiTreeViewCtrl::insertItem(S32 parentId, const char * text, const char * value, const char * iconString, S16 normalImage, S16 expandedImage)
  1057. {
  1058. if( ( parentId < 0 ) || ( parentId > mItems.size() ) )
  1059. {
  1060. Con::errorf(ConsoleLogEntry::General, "GuiTreeViewCtrl::insertItem: invalid parent id!");
  1061. return 0;
  1062. }
  1063. if((parentId != 0) && (mItems[parentId-1] == 0))
  1064. {
  1065. Con::errorf(ConsoleLogEntry::General, "GuiTreeViewCtrl::insertItem: parent item invalid!");
  1066. return 0;
  1067. }
  1068. const char * pItemText = ( text != NULL ) ? text : "";
  1069. const char * pItemValue = ( value != NULL ) ? value : "";
  1070. S32 icon = getIcon(iconString);
  1071. // create an item (assigns id)
  1072. Item * pNewItem = createItem(icon);
  1073. if( pNewItem == NULL )
  1074. return 0;
  1075. pNewItem->setText( StringTable->insert( pItemText, true ) );
  1076. pNewItem->setValue( StringTable->insert( pItemValue, true ) );
  1077. pNewItem->setNormalImage( normalImage );
  1078. pNewItem->setExpandedImage( expandedImage );
  1079. // root level?
  1080. if(parentId == 0)
  1081. {
  1082. // insert back
  1083. if( mRoot != NULL )
  1084. {
  1085. Item * pTreeTraverse = mRoot;
  1086. while( pTreeTraverse != NULL && pTreeTraverse->mNext != NULL )
  1087. pTreeTraverse = pTreeTraverse->mNext;
  1088. pTreeTraverse->mNext = pNewItem;
  1089. pNewItem->mPrevious = pTreeTraverse;
  1090. }
  1091. else
  1092. mRoot = pNewItem;
  1093. mFlags.set(RebuildVisible);
  1094. }
  1095. else if( mItems.size() >= ( parentId - 1 ) )
  1096. {
  1097. Item * pParentItem = mItems[parentId-1];
  1098. // insert back
  1099. if( pParentItem != NULL && pParentItem->mChild)
  1100. {
  1101. Item * pTreeTraverse = pParentItem->mChild;
  1102. while( pTreeTraverse != NULL && pTreeTraverse->mNext != NULL )
  1103. pTreeTraverse = pTreeTraverse->mNext;
  1104. pTreeTraverse->mNext = pNewItem;
  1105. pNewItem->mPrevious = pTreeTraverse;
  1106. }
  1107. else
  1108. pParentItem->mChild = pNewItem;
  1109. pNewItem->mParent = pParentItem;
  1110. if( pParentItem->isExpanded() )
  1111. mFlags.set(RebuildVisible);
  1112. }
  1113. return pNewItem->mId;
  1114. }
  1115. //------------------------------------------------------------------------------
  1116. bool GuiTreeViewCtrl::removeItem( S32 itemId, bool deleteObjects )
  1117. {
  1118. if( isSelected( itemId ) )
  1119. removeSelection( itemId );
  1120. // tree?
  1121. if(itemId == 0)
  1122. {
  1123. //RD: this does not delete objects and thus isn't coherent with the semantics of this method
  1124. _destroyTree();
  1125. return(true);
  1126. }
  1127. Item * item = getItem(itemId);
  1128. if(!item)
  1129. {
  1130. //Con::errorf(ConsoleLogEntry::General, "GuiTreeViewCtrl::removeItem: invalid item id!");
  1131. return false;
  1132. }
  1133. // root?
  1134. if(item == mRoot)
  1135. mRoot = item->mNext;
  1136. // Dispose of any children...
  1137. if (item->mChild)
  1138. _destroyChildren( item->mChild, item, deleteObjects );
  1139. // Kill the item...
  1140. _destroyItem( item, deleteObjects );
  1141. // Update the rendered tree...
  1142. mFlags.set(RebuildVisible);
  1143. return true;
  1144. }
  1145. //-----------------------------------------------------------------------------
  1146. void GuiTreeViewCtrl::removeAllChildren(S32 itemId)
  1147. {
  1148. Item * item = getItem(itemId);
  1149. if(item)
  1150. {
  1151. _destroyChildren(item->mChild, item);
  1152. }
  1153. }
  1154. //------------------------------------------------------------------------------
  1155. const S32 GuiTreeViewCtrl::getFirstRootItem() const
  1156. {
  1157. return (mRoot ? mRoot->mId : 0);
  1158. }
  1159. //------------------------------------------------------------------------------
  1160. S32 GuiTreeViewCtrl::getChildItem(S32 itemId)
  1161. {
  1162. Item * item = getItem(itemId);
  1163. if(!item)
  1164. {
  1165. Con::errorf(ConsoleLogEntry::General, "GuiTreeViewCtrl::getChild: invalid item id!");
  1166. return(0);
  1167. }
  1168. return(item->mChild ? item->mChild->mId : 0);
  1169. }
  1170. //-----------------------------------------------------------------------------
  1171. S32 GuiTreeViewCtrl::getParentItem(S32 itemId)
  1172. {
  1173. Item * item = getItem(itemId);
  1174. if(!item)
  1175. {
  1176. Con::errorf(ConsoleLogEntry::General, "GuiTreeViewCtrl::getParent: invalid item id!");
  1177. return(0);
  1178. }
  1179. return(item->mParent ? item->mParent->mId : 0);
  1180. }
  1181. //-----------------------------------------------------------------------------
  1182. S32 GuiTreeViewCtrl::getNextSiblingItem(S32 itemId)
  1183. {
  1184. Item * item = getItem(itemId);
  1185. if(!item)
  1186. {
  1187. Con::errorf(ConsoleLogEntry::General, "GuiTreeViewCtrl::getNextSibling: invalid item id!");
  1188. return(0);
  1189. }
  1190. return(item->mNext ? item->mNext->mId : 0);
  1191. }
  1192. //-----------------------------------------------------------------------------
  1193. S32 GuiTreeViewCtrl::getPrevSiblingItem(S32 itemId)
  1194. {
  1195. Item * item = getItem(itemId);
  1196. if(!item)
  1197. {
  1198. Con::errorf(ConsoleLogEntry::General, "GuiTreeViewCtrl::getPrevSibling: invalid item id!");
  1199. return(0);
  1200. }
  1201. return(item->mPrevious ? item->mPrevious->mId : 0);
  1202. }
  1203. //------------------------------------------------------------------------------
  1204. bool GuiTreeViewCtrl::isValidDragTarget( Item* item )
  1205. {
  1206. bool isValid = true;
  1207. // If this is inspector data, first make sure the item accepts all
  1208. // selected objects as children. This prevents bad surprises when
  1209. // certain SimSet subclasses reject children and start shoving them
  1210. // off to places of their own choosing.
  1211. if( item->isInspectorData() )
  1212. {
  1213. if( mDebug )
  1214. Con::printf( "Checking %i:%s as drag-parent",
  1215. item->getObject()->getId(), item->getObject()->getClassName() );
  1216. SimSet* set = dynamic_cast< SimSet*>( item->getObject() );
  1217. if( set )
  1218. {
  1219. for( U32 i = 0; i < mSelectedItems.size(); ++ i )
  1220. {
  1221. Item* selectedItem = mSelectedItems[ i ];
  1222. if( mDebug )
  1223. Con::printf( "Checking %i:%s as drag-object",
  1224. selectedItem->getObject()->getId(),
  1225. selectedItem->getObject()->getClassName() );
  1226. if( selectedItem->isInspectorData()
  1227. && !set->acceptsAsChild( selectedItem->getObject() ) )
  1228. return false;
  1229. }
  1230. }
  1231. }
  1232. if( isMethod( "isValidDragTarget" ) )
  1233. {
  1234. // We have a callback. Exclusively leave the decision whether
  1235. // the item is a valid drag target to it.
  1236. isValid = isValidDragTarget_callback( item->mId, getItemValue( item->mId ) );
  1237. }
  1238. else
  1239. {
  1240. // Make the item a valid drag target if it either already is
  1241. // a parent (including VirtualParents) or if dragging to non-parent
  1242. // items is explicitly allowed.
  1243. isValid = item->isParent() || mDragToItemAllowed;
  1244. }
  1245. return isValid;
  1246. }
  1247. //------------------------------------------------------------------------------
  1248. S32 GuiTreeViewCtrl::getItemCount()
  1249. {
  1250. return(mItemCount);
  1251. }
  1252. //-----------------------------------------------------------------------------
  1253. S32 GuiTreeViewCtrl::getSelectedItem()
  1254. {
  1255. return mSelectedItem;
  1256. }
  1257. //------------------------------------------------------------------------------
  1258. void GuiTreeViewCtrl::moveItemUp( S32 itemId )
  1259. {
  1260. GuiTreeViewCtrl::Item* pItem = getItem( itemId );
  1261. if ( !pItem )
  1262. {
  1263. Con::errorf( ConsoleLogEntry::General, "GuiTreeViewCtrl::moveItemUp: invalid item id!");
  1264. return;
  1265. }
  1266. Item * pParent = pItem->mParent;
  1267. Item * pPrevItem = pItem->mPrevious;
  1268. if ( pPrevItem == NULL || pParent == NULL )
  1269. {
  1270. Con::errorf( ConsoleLogEntry::General, "GuiTreeViewCtrl::moveItemUp: Unable to move item up, bad data!");
  1271. return;
  1272. }
  1273. // Diddle the linked list!
  1274. if ( pPrevItem->mPrevious )
  1275. pPrevItem->mPrevious->mNext = pItem;
  1276. else if ( pItem->mParent )
  1277. pItem->mParent->mChild = pItem;
  1278. if ( pItem->mNext )
  1279. pItem->mNext->mPrevious = pPrevItem;
  1280. pItem->mPrevious = pPrevItem->mPrevious;
  1281. pPrevItem->mNext = pItem->mNext;
  1282. pItem->mNext = pPrevItem;
  1283. pPrevItem->mPrevious = pItem;
  1284. // Update SimObjects if Appropriate.
  1285. SimObject * pSimObject = NULL;
  1286. SimSet * pParentSet = NULL;
  1287. // Fetch Current Add Set
  1288. if( pParent->isInspectorData() )
  1289. pParentSet = dynamic_cast<SimSet*>( pParent->getObject() );
  1290. else
  1291. {
  1292. // parent is probably script data so we search up the tree for a
  1293. // set to put our object in
  1294. Item * pTraverse = pItem->mParent;
  1295. while ( pTraverse != NULL && !pTraverse->isInspectorData() )
  1296. pTraverse = pTraverse->mParent;
  1297. // found an ancestor who is an inspectorData?
  1298. if (pTraverse != NULL)
  1299. pParentSet = pTraverse->isInspectorData() ? dynamic_cast<SimSet*>( pTraverse->getObject() ) : NULL;
  1300. }
  1301. // Reorder the item and make sure that the children of the item get updated
  1302. // correctly prev item may be script... so find a prevItem if there is.
  1303. // We only need to reorder if there you move it above an inspector item.
  1304. if ( pSimObject != NULL && pParentSet != NULL )
  1305. {
  1306. Item * pTraverse = pItem->mNext;
  1307. while(pTraverse)
  1308. {
  1309. if (pTraverse->isInspectorData())
  1310. break;
  1311. pTraverse = pTraverse->mNext;
  1312. }
  1313. if (pTraverse && pItem->getObject() && pTraverse->getObject())
  1314. pParentSet->reOrder(pItem->getObject(), pTraverse->getObject());
  1315. }
  1316. mFlags.set(RebuildVisible);
  1317. }
  1318. //-----------------------------------------------------------------------------
  1319. void GuiTreeViewCtrl::moveItemDown( S32 itemId )
  1320. {
  1321. GuiTreeViewCtrl::Item* item = getItem( itemId );
  1322. if ( !item )
  1323. {
  1324. Con::errorf( ConsoleLogEntry::General, "GuiTreeViewCtrl::moveItemDown: invalid item id!");
  1325. return;
  1326. }
  1327. Item* nextItem = item->mNext;
  1328. if ( !nextItem )
  1329. {
  1330. Con::errorf( ConsoleLogEntry::General, "GuiTreeViewCtrl::moveItemDown: no next sibling?");
  1331. return;
  1332. }
  1333. // Diddle the linked list!
  1334. if ( nextItem->mNext )
  1335. nextItem->mNext->mPrevious = item;
  1336. if ( item->mPrevious )
  1337. item->mPrevious->mNext = nextItem;
  1338. else if ( item->mParent )
  1339. item->mParent->mChild = nextItem;
  1340. item->mNext = nextItem->mNext;
  1341. nextItem->mPrevious = item->mPrevious;
  1342. item->mPrevious = nextItem;
  1343. nextItem->mNext = item;
  1344. // And update the simobjects if apppropriate...
  1345. SimObject * simobj = NULL;
  1346. if (item->isInspectorData())
  1347. simobj = item->getObject();
  1348. SimSet *parentSet = NULL;
  1349. // grab the current parentSet if there is any...
  1350. if(item->mParent->isInspectorData())
  1351. parentSet = dynamic_cast<SimSet*>(item->mParent->getObject());
  1352. else
  1353. {
  1354. // parent is probably script data so we search up the tree for a
  1355. // set to put our object in
  1356. Item * temp = item->mParent;
  1357. while (temp && !temp->isInspectorData())
  1358. temp = temp->mParent;
  1359. // found an ancestor who is an inspectorData?
  1360. parentSet = (temp && temp->isInspectorData()) ? dynamic_cast<SimSet*>(temp->getObject()) : NULL;
  1361. }
  1362. // Reorder the item and make sure that the children of the item get updated
  1363. // correctly prev item may be script... so find a prevItem if there is.
  1364. // We only need to reorder if there you move it above an inspector item.
  1365. if (simobj && parentSet)
  1366. {
  1367. Item * temp = item->mPrevious;
  1368. while(temp)
  1369. {
  1370. if (temp->isInspectorData())
  1371. break;
  1372. temp = temp->mPrevious;
  1373. }
  1374. if (temp && item->getObject() && temp->getObject())
  1375. parentSet->reOrder(temp->getObject(), item->getObject());
  1376. }
  1377. mFlags.set(RebuildVisible);
  1378. }
  1379. //------------------------------------------------------------------------------
  1380. bool GuiTreeViewCtrl::onAdd()
  1381. {
  1382. if( !Parent::onAdd() )
  1383. return false;
  1384. // If we have dynamic fields, convert the "internalNamesOnly" and "objectNamesOnly"
  1385. // legacy fields.
  1386. if( getFieldDictionary() )
  1387. {
  1388. static StringTableEntry sInternalNamesOnly = StringTable->insert( "internalNamesOnly" );
  1389. static StringTableEntry sObjectNamesOnly = StringTable->insert( "objectNamesOnly" );
  1390. const char* internalNamesOnly = getDataField( sInternalNamesOnly, NULL );
  1391. if( internalNamesOnly && internalNamesOnly[ 0 ] && dAtob( internalNamesOnly ) )
  1392. {
  1393. mShowObjectIds = false;
  1394. mShowClassNames = false;
  1395. mShowObjectNames = false;
  1396. mShowInternalNames = true;
  1397. }
  1398. const char* objectNamesOnly = getDataField( sObjectNamesOnly, NULL );
  1399. if( objectNamesOnly && objectNamesOnly[ 0 ] && dAtob( objectNamesOnly ) )
  1400. {
  1401. mShowObjectIds = false;
  1402. mShowClassNames = false;
  1403. mShowObjectNames = true;
  1404. mShowInternalNames = false;
  1405. }
  1406. }
  1407. return true;
  1408. }
  1409. //------------------------------------------------------------------------------
  1410. bool GuiTreeViewCtrl::onWake()
  1411. {
  1412. if(!Parent::onWake() || !mProfile->constructBitmapArray())
  1413. return false;
  1414. // If destroy on sleep, then we have to give things a chance to rebuild.
  1415. if(mDestroyOnSleep)
  1416. {
  1417. onDefineIcons_callback();
  1418. }
  1419. // Update the row height, if appropriate.
  1420. if(mProfile->mAutoSizeHeight)
  1421. {
  1422. // make sure it's big enough for both bitmap AND font...
  1423. mItemHeight = getMax((S32)mFont->getHeight(), (S32)mProfile->mBitmapArrayRects[0].extent.y);
  1424. }
  1425. return true;
  1426. }
  1427. //-----------------------------------------------------------------------------
  1428. void GuiTreeViewCtrl::onSleep()
  1429. {
  1430. Parent::onSleep();
  1431. // If appropriate, blast the tree. (We probably rebuild it on wake.)
  1432. if( mDestroyOnSleep )
  1433. _destroyTree();
  1434. if ( mRenameCtrl )
  1435. {
  1436. mRenameCtrl->deleteObject();
  1437. mRenameCtrl = NULL;
  1438. }
  1439. }
  1440. //-----------------------------------------------------------------------------
  1441. bool GuiTreeViewCtrl::buildIconTable(const char * icons)
  1442. {
  1443. // Icons should be designated by the bitmap/png file names (minus the file extensions)
  1444. // and separated by colons (:). This list should be synchronized with the Icons enum.
  1445. // Figure the size of the buffer we need...
  1446. const char* temp = dStrchr( icons, '\t' );
  1447. U32 textLen = temp ? ( temp - icons ) : dStrlen( icons );
  1448. // Allocate temporary space.
  1449. FrameAllocatorMarker txtBuff;
  1450. char* drawText = (char*)txtBuff.alloc(sizeof(char) * (textLen + 4));
  1451. dStrncpy( drawText, icons, textLen );
  1452. drawText[textLen] = '\0';
  1453. U32 numIcons = 0;
  1454. char buf[ 1024 ];
  1455. char* pos = drawText;
  1456. // Count the number of icons and store them.
  1457. while( *pos && numIcons < MaxIcons )
  1458. {
  1459. char* start = pos;
  1460. while( *pos && *pos != ':' )
  1461. pos ++;
  1462. const U32 len = pos - start;
  1463. if( len )
  1464. {
  1465. dStrncpy( buf, start, getMin( sizeof( buf ) / sizeof( buf[ 0 ] ) - 1, len ) );
  1466. buf[ len ] = '\0';
  1467. mIconTable[ numIcons ] = GFXTexHandle( buf, &GFXDefaultPersistentProfile, avar( "%s() - mIconTable[%d] (line %d)", __FUNCTION__, numIcons, __LINE__ ) );
  1468. }
  1469. else
  1470. mIconTable[ numIcons ] = GFXTexHandle();
  1471. numIcons ++;
  1472. if( *pos )
  1473. pos ++;
  1474. }
  1475. return true;
  1476. }
  1477. //------------------------------------------------------------------------------
  1478. void GuiTreeViewCtrl::onPreRender()
  1479. {
  1480. Parent::onPreRender();
  1481. S32 nRootItemId = getFirstRootItem();
  1482. if( nRootItemId == 0 )
  1483. return;
  1484. Item *pRootItem = getItem( nRootItemId );
  1485. if( pRootItem == NULL )
  1486. return;
  1487. // Update every render in case new objects are added
  1488. if(mFlags.test(RebuildVisible))
  1489. {
  1490. buildVisibleTree();
  1491. mFlags.clear(RebuildVisible);
  1492. }
  1493. }
  1494. //------------------------------------------------------------------------------
  1495. bool GuiTreeViewCtrl::_hitTest(const Point2I & pnt, Item* & item, BitSet32 & flags)
  1496. {
  1497. // Initialize some things.
  1498. const Point2I pos = globalToLocalCoord(pnt);
  1499. flags.clear();
  1500. item = 0;
  1501. // get the hit cell
  1502. Point2I cell((pos.x < 0 ? -1 : pos.x / mCellSize.x),
  1503. (pos.y < 0 ? -1 : pos.y / mCellSize.y));
  1504. // valid?
  1505. if((cell.x < 0 || cell.x >= mSize.x) ||
  1506. (cell.y < 0 || cell.y >= mSize.y))
  1507. return false;
  1508. flags.set(OnRow);
  1509. // Grab the cell.
  1510. if (cell.y >= mVisibleItems.size())
  1511. return false; //Invalid cell, so don't do anything
  1512. item = mVisibleItems[cell.y];
  1513. S32 min = mTabSize * item->mTabLevel;
  1514. // left of icon/text?
  1515. if(pos.x < min)
  1516. {
  1517. flags.set(OnIndent);
  1518. return true;
  1519. }
  1520. // check image
  1521. S32 image = BmpChild;
  1522. if(item->isInspectorData())
  1523. image = item->isExpanded() ? BmpExp : BmpCon;
  1524. else
  1525. image = item->isExpanded() ? item->getExpandedImage() : item->getNormalImage();
  1526. if((image >= 0) && (image < mProfile->mBitmapArrayRects.size()))
  1527. min += mProfile->mBitmapArrayRects[image].extent.x;
  1528. // Is it on the image?
  1529. if(pos.x < min)
  1530. {
  1531. flags.set(OnImage);
  1532. return(true);
  1533. }
  1534. // Check the icon.
  1535. min += getInspectorItemIconsWidth( item );
  1536. if ( pos.x < min )
  1537. {
  1538. flags.set(OnIcon);
  1539. return true;
  1540. }
  1541. // Check the text.
  1542. min += mProfile->mTextOffset.x;
  1543. FrameAllocatorMarker txtAlloc;
  1544. U32 bufLen = item->getDisplayTextLength() + 1;
  1545. char *buf = (char*)txtAlloc.alloc(bufLen);
  1546. item->getDisplayText(bufLen, buf);
  1547. min += mProfile->mFont->getStrWidth(buf);
  1548. if(pos.x < min)
  1549. flags.set(OnText);
  1550. return true;
  1551. }
  1552. //-----------------------------------------------------------------------------
  1553. S32 GuiTreeViewCtrl::getInspectorItemIconsWidth(Item* & item)
  1554. {
  1555. S32 width = 0;
  1556. if( item->isInspectorData() )
  1557. {
  1558. // Based on code in onRenderCell()
  1559. S32 icon = Lock1;
  1560. S32 icon2 = Hidden;
  1561. if (item->getObject() && item->getObject()->isLocked())
  1562. {
  1563. if (mIconTable[icon])
  1564. {
  1565. width += mIconTable[icon].getWidth();
  1566. }
  1567. }
  1568. if (item->getObject() && item->getObject()->isHidden())
  1569. {
  1570. if (mIconTable[icon2])
  1571. {
  1572. width += mIconTable[icon2].getWidth();
  1573. }
  1574. }
  1575. GFXTexHandle iconHandle;
  1576. if ( ( item->mIcon != -1 ) && mIconTable[item->mIcon] )
  1577. iconHandle = mIconTable[item->mIcon];
  1578. #ifdef TORQUE_TOOLS
  1579. else
  1580. iconHandle = gEditorIcons.findIcon( item->getObject() );
  1581. #endif
  1582. if ( iconHandle.isValid() )
  1583. {
  1584. width += iconHandle.getWidth();
  1585. }
  1586. }
  1587. else
  1588. {
  1589. S32 icon = item->isExpanded() ? item->mScriptInfo.mExpandedImage : item->mScriptInfo.mNormalImage;
  1590. if ( ( icon != -1 ) && mIconTable[icon] )
  1591. {
  1592. width += mIconTable[icon].getWidth();
  1593. }
  1594. }
  1595. return width;
  1596. }
  1597. //-----------------------------------------------------------------------------
  1598. bool GuiTreeViewCtrl::setAddGroup(SimObject * obj)
  1599. {
  1600. // make sure we're talking about a group.
  1601. SimGroup * grp = dynamic_cast<SimGroup*>(obj);
  1602. if(grp)
  1603. {
  1604. onAddGroupSelected_callback( grp );
  1605. return true;
  1606. }
  1607. return false;
  1608. }
  1609. //-----------------------------------------------------------------------------
  1610. void GuiTreeViewCtrl::syncSelection()
  1611. {
  1612. // for each visible item check to see if it is on the mSelected list.
  1613. // if it is then make sure that it is on the mSelectedItems list as well.
  1614. for (S32 i = 0; i < mVisibleItems.size(); i++)
  1615. {
  1616. for (S32 j = 0; j < mSelected.size(); j++)
  1617. {
  1618. if (mVisibleItems[i]->mId == mSelected[j])
  1619. {
  1620. // check to see if it is on the visible items list.
  1621. bool addToSelectedItems = true;
  1622. for (S32 k = 0; k < mSelectedItems.size(); k++)
  1623. {
  1624. if (mSelected[j] == mSelectedItems[k]->mId)
  1625. {
  1626. // don't add it
  1627. addToSelectedItems = false;
  1628. }
  1629. }
  1630. if (addToSelectedItems)
  1631. {
  1632. mVisibleItems[i]->mState.set(Item::Selected, true);
  1633. mSelectedItems.push_front(mVisibleItems[i]);
  1634. break;
  1635. }
  1636. }
  1637. else if (mVisibleItems[i]->isInspectorData())
  1638. {
  1639. if(mCompareToObjectID)
  1640. {
  1641. if (mVisibleItems[i]->getObject() && mVisibleItems[i]->getObject()->getId() == mSelected[j])
  1642. {
  1643. // check to see if it is on the visible items list.
  1644. bool addToSelectedItems = true;
  1645. for (S32 k = 0; k < mSelectedItems.size(); k++)
  1646. {
  1647. if (mSelectedItems[k]->isInspectorData() && mSelectedItems[k]->getObject() )
  1648. {
  1649. if (mSelected[j] == mSelectedItems[k]->getObject()->getId())
  1650. {
  1651. // don't add it
  1652. addToSelectedItems = false;
  1653. }
  1654. }
  1655. else
  1656. {
  1657. if (mSelected[j] == mSelectedItems[k]->mId)
  1658. {
  1659. // don't add it
  1660. addToSelectedItems = false;
  1661. }
  1662. }
  1663. }
  1664. if (addToSelectedItems)
  1665. {
  1666. mVisibleItems[i]->mState.set(Item::Selected, true);
  1667. mSelectedItems.push_front(mVisibleItems[i]);
  1668. break;
  1669. }
  1670. }
  1671. }
  1672. }
  1673. }
  1674. }
  1675. }
  1676. //-----------------------------------------------------------------------------
  1677. void GuiTreeViewCtrl::removeSelection( S32 itemOrObjectId )
  1678. {
  1679. if (mDebug)
  1680. Con::printf( "removeSelection %i", itemOrObjectId );
  1681. Item* item = _findItemByAmbiguousId( itemOrObjectId, false );
  1682. if (!item)
  1683. return;
  1684. // Make sure we have a true item ID even if we started with
  1685. // an object ID.
  1686. S32 itemId = item->getID();
  1687. S32 objectId = -1;
  1688. if ( item->isInspectorData() && item->getObject() )
  1689. objectId = item->getObject()->getId();
  1690. // Remove from vector of selected object ids if it exists there
  1691. if ( objectId != -1 )
  1692. {
  1693. for ( S32 i = 0; i < mSelected.size(); i++ )
  1694. {
  1695. if ( objectId == mSelected[i] || itemId == mSelected[i] )
  1696. {
  1697. mSelected.erase( i );
  1698. break;
  1699. }
  1700. }
  1701. }
  1702. else
  1703. {
  1704. for ( S32 i = 0; i < mSelected.size(); i++ )
  1705. {
  1706. if ( itemId == mSelected[i] )
  1707. {
  1708. mSelected.erase( i );
  1709. break;
  1710. }
  1711. }
  1712. }
  1713. item->mState.set(Item::Selected, false);
  1714. // Remove from vector of selected items if it exists there.
  1715. for ( S32 i = 0; i < mSelectedItems.size(); i++ )
  1716. {
  1717. if ( mSelectedItems[i] == item )
  1718. {
  1719. mSelectedItems.erase( i );
  1720. break;
  1721. }
  1722. }
  1723. // Callback.
  1724. onRemoveSelection( item );
  1725. }
  1726. //-----------------------------------------------------------------------------
  1727. void GuiTreeViewCtrl::addSelection( S32 itemOrObjectId, bool update, bool isLastSelection )
  1728. {
  1729. if (mDebug)
  1730. Con::printf( "addSelection %i", itemOrObjectId );
  1731. Item* item = _findItemByAmbiguousId( itemOrObjectId );
  1732. // Add Item?
  1733. if ( !item || isSelected( item ) || !canAddSelection( item ) )
  1734. {
  1735. // Nope.
  1736. return;
  1737. }
  1738. const S32 itemId = item->getID();
  1739. // Ok, we have an item to select which isn't already selected....
  1740. // Do we want to allow more than one selected item?
  1741. if( !mMultipleSelections )
  1742. clearSelection();
  1743. // Add this object id to the vector of selected objectIds
  1744. // if it is not already.
  1745. bool foundMatch = false;
  1746. for ( S32 i = 0; i < mSelected.size(); i++)
  1747. {
  1748. if ( mSelected[i] == itemId )
  1749. foundMatch = true;
  1750. }
  1751. if ( !foundMatch )
  1752. mSelected.push_front(itemId);
  1753. item->mState.set(Item::Selected, true);
  1754. if( mSelected.size() == 1 )
  1755. {
  1756. onItemSelected( item );
  1757. }
  1758. // Callback Start
  1759. // Set and add the selection to the selected items group
  1760. item->mState.set(Item::Selected, true);
  1761. mSelectedItems.push_front(item);
  1762. if ( item->isInspectorData() &&
  1763. item->getObject() )
  1764. {
  1765. SimObject *obj = item->getObject();
  1766. onAddSelection_callback( obj->getId(), isLastSelection );
  1767. }
  1768. else
  1769. {
  1770. onAddSelection_callback( item->mId, isLastSelection );
  1771. }
  1772. // Callback end
  1773. mFlags.set( RebuildVisible );
  1774. if( update )
  1775. {
  1776. // Also make it so we can see it if we didn't already.
  1777. scrollVisible( item );
  1778. }
  1779. }
  1780. //-----------------------------------------------------------------------------
  1781. void GuiTreeViewCtrl::onItemSelected( Item *item )
  1782. {
  1783. mSelectedItem = item->getID();
  1784. if (item->isInspectorData())
  1785. {
  1786. SimObject* object = item->getObject();
  1787. if( object )
  1788. onSelect_callback( object->getId() );
  1789. if( !item->isParent() && object )
  1790. onInspect_callback( object->getId() );
  1791. }
  1792. else
  1793. {
  1794. onSelect_callback( item->mId );
  1795. if( !item->isParent() )
  1796. onInspect_callback( item->mId );
  1797. }
  1798. }
  1799. //-----------------------------------------------------------------------------
  1800. void GuiTreeViewCtrl::onRemoveSelection( Item *item )
  1801. {
  1802. S32 id = item->mId;
  1803. if( item->isInspectorData() &&
  1804. item->getObject() )
  1805. {
  1806. SimObject* obj = item->getObject();
  1807. id = obj->getId();
  1808. //obj->setSelected( false );
  1809. }
  1810. if( isMethod( "onRemoveSelection" ) )
  1811. onRemoveSelection_callback( id );
  1812. else
  1813. onUnselect_callback( id );
  1814. }
  1815. //-----------------------------------------------------------------------------
  1816. bool GuiTreeViewCtrl::setItemSelected(S32 itemId, bool select)
  1817. {
  1818. Item * item = getItem(itemId);
  1819. if( isSelected( item ) == select )
  1820. return true;
  1821. if (select)
  1822. {
  1823. if (mDebug) Con::printf("setItemSelected called true");
  1824. mSelected.push_front(itemId);
  1825. }
  1826. else
  1827. {
  1828. if (mDebug) Con::printf("setItemSelected called false");
  1829. // remove it from the mSelected list
  1830. for (S32 j = 0; j <mSelected.size(); j++)
  1831. {
  1832. if (item)
  1833. {
  1834. if (item->isInspectorData())
  1835. {
  1836. if (item->getObject())
  1837. {
  1838. if(item->getObject()->getId() == mSelected[j])
  1839. {
  1840. mSelected.erase(j);
  1841. break;
  1842. }
  1843. }
  1844. else
  1845. {
  1846. // Zombie, kill it!
  1847. mSelected.erase(j);
  1848. j--;
  1849. break;
  1850. }
  1851. }
  1852. }
  1853. if (mSelected[j] == itemId)
  1854. {
  1855. mSelected.erase(j);
  1856. break;
  1857. }
  1858. }
  1859. }
  1860. if(!item)
  1861. {
  1862. // maybe what we were passed wasn't an item id but an object id.
  1863. for (S32 i = 0; i <mItems.size(); i++)
  1864. {
  1865. if (mItems[i] != 0)
  1866. {
  1867. if (mItems[i]->isInspectorData())
  1868. {
  1869. if (mItems[i]->getObject())
  1870. {
  1871. if(mItems[i]->getObject()->getId() == itemId)
  1872. {
  1873. item = mItems[i];
  1874. break;
  1875. }
  1876. }
  1877. else
  1878. {
  1879. // It's a zombie, blast it.
  1880. mItems.erase(i);
  1881. i--;
  1882. }
  1883. }
  1884. }
  1885. }
  1886. if (!item)
  1887. {
  1888. //Con::errorf(ConsoleLogEntry::General, "GuiTreeViewCtrl::setItemSelected: invalid item id! Perhaps it isn't visible yet.");
  1889. return(false);
  1890. }
  1891. }
  1892. mFlags.set( RebuildVisible );
  1893. if(select)
  1894. {
  1895. addSelection( item->mId );
  1896. onItemSelected( item );
  1897. }
  1898. else
  1899. {
  1900. // deselect the item, if it's present.
  1901. item->mState.set(Item::Selected, false);
  1902. if (item->isInspectorData() && item->getObject())
  1903. onUnselect_callback( item->getObject()->getId() );
  1904. else
  1905. onUnselect_callback( item->mId );
  1906. // remove it from the selected items list
  1907. for (S32 i = 0; i < mSelectedItems.size(); i++)
  1908. {
  1909. if (mSelectedItems[i] == item)
  1910. {
  1911. mSelectedItems.erase(i);
  1912. break;
  1913. }
  1914. }
  1915. }
  1916. setUpdate();
  1917. return(true);
  1918. }
  1919. //-----------------------------------------------------------------------------
  1920. // Given an item's index in the selection list, return its itemId
  1921. S32 GuiTreeViewCtrl::getSelectedItem(S32 index)
  1922. {
  1923. if(index >= 0 && index < getSelectedItemsCount())
  1924. {
  1925. return mSelectedItems[index]->mId;
  1926. }
  1927. return -1;
  1928. }
  1929. //-----------------------------------------------------------------------------
  1930. bool GuiTreeViewCtrl::setItemExpanded(S32 itemId, bool expand)
  1931. {
  1932. Item * item = getItem(itemId);
  1933. if(!item)
  1934. {
  1935. Con::errorf(ConsoleLogEntry::General, "GuiTreeViewCtrl::setItemExpanded: invalid item id!");
  1936. return(false);
  1937. }
  1938. if(item->isExpanded() == expand)
  1939. return(true);
  1940. // expand parents
  1941. if(expand)
  1942. {
  1943. while(item)
  1944. {
  1945. if(item->mState.test(Item::VirtualParent))
  1946. onVirtualParentExpand(item);
  1947. item->setExpanded(true);
  1948. item = item->mParent;
  1949. }
  1950. }
  1951. else
  1952. {
  1953. if(item->mState.test(Item::VirtualParent))
  1954. onVirtualParentCollapse(item);
  1955. item->setExpanded(false);
  1956. }
  1957. return(true);
  1958. }
  1959. //-----------------------------------------------------------------------------
  1960. bool GuiTreeViewCtrl::setItemValue(S32 itemId, StringTableEntry Value)
  1961. {
  1962. Item * item = getItem(itemId);
  1963. if(!item)
  1964. {
  1965. Con::errorf(ConsoleLogEntry::General, "GuiTreeViewCtrl::setItemValue: invalid item id!");
  1966. return(false);
  1967. }
  1968. item->setValue( ( Value ) ? Value : "" );
  1969. return(true);
  1970. }
  1971. //-----------------------------------------------------------------------------
  1972. const char * GuiTreeViewCtrl::getItemText(S32 itemId)
  1973. {
  1974. Item * item = getItem(itemId);
  1975. if(!item)
  1976. {
  1977. Con::errorf(ConsoleLogEntry::General, "GuiTreeViewCtrl::getItemText: invalid item id!");
  1978. return("");
  1979. }
  1980. return(item->getText() ? item->getText() : "");
  1981. }
  1982. //-----------------------------------------------------------------------------
  1983. const char * GuiTreeViewCtrl::getItemValue(S32 itemId)
  1984. {
  1985. Item * item = getItem(itemId);
  1986. if(!item)
  1987. {
  1988. Con::errorf(ConsoleLogEntry::General, "GuiTreeViewCtrl::getItemValue: invalid item id!");
  1989. return("");
  1990. }
  1991. if(item->mState.test(Item::InspectorData))
  1992. {
  1993. // If it's InspectorData, we let people use this call to get an object reference.
  1994. return item->mInspectorInfo.mObject->getIdString();
  1995. }
  1996. else
  1997. {
  1998. // Just return the script value...
  1999. return item->getValue();
  2000. }
  2001. }
  2002. //-----------------------------------------------------------------------------
  2003. bool GuiTreeViewCtrl::editItem( S32 itemId, const char* newText, const char* newValue )
  2004. {
  2005. Item* item = getItem( itemId );
  2006. if ( !item )
  2007. {
  2008. Con::errorf(ConsoleLogEntry::General, "GuiTreeViewCtrl::editItem: invalid item id: %d!", itemId);
  2009. return false;
  2010. }
  2011. if ( item->mState.test(Item::InspectorData) )
  2012. {
  2013. Con::errorf(ConsoleLogEntry::General, "GuiTreeViewCtrl::editItem: item %d is inspector data and may not be modified!", itemId);
  2014. return false;
  2015. }
  2016. item->setText( StringTable->insert( newText, true ) );
  2017. item->setValue( StringTable->insert( newValue, true ) );
  2018. // Update the widths and such:
  2019. mFlags.set(RebuildVisible);
  2020. return true;
  2021. }
  2022. //-----------------------------------------------------------------------------
  2023. bool GuiTreeViewCtrl::markItem( S32 itemId, bool mark )
  2024. {
  2025. Item *item = getItem( itemId );
  2026. if ( !item )
  2027. {
  2028. Con::errorf(ConsoleLogEntry::General, "GuiTreeViewCtrl::markItem: invalid item id: %d!", itemId);
  2029. return false;
  2030. }
  2031. item->mState.set(Item::Marked, mark);
  2032. return true;
  2033. }
  2034. //-----------------------------------------------------------------------------
  2035. bool GuiTreeViewCtrl::isItemSelected( S32 itemId )
  2036. {
  2037. for( U32 i = 0, num = mSelectedItems.size(); i < num; ++ i )
  2038. if( mSelectedItems[ i ]->mId == itemId )
  2039. return true;
  2040. return false;
  2041. }
  2042. //-----------------------------------------------------------------------------
  2043. void GuiTreeViewCtrl::deleteSelection()
  2044. {
  2045. onDeleteSelection_callback();
  2046. if (mSelectedItems.empty())
  2047. {
  2048. for (S32 i = 0; i < mSelected.size(); i++)
  2049. {
  2050. S32 objectId = mSelected[i];
  2051. // find the object
  2052. SimObject* obj = Sim::findObject(objectId);
  2053. if ( !obj )
  2054. continue;
  2055. bool skipDelete = onDeleteObject_callback( obj );
  2056. if ( !skipDelete )
  2057. obj->deleteObject();
  2058. }
  2059. }
  2060. else
  2061. {
  2062. Vector<Item*> delSelection;
  2063. delSelection = mSelectedItems;
  2064. mSelectedItems.clear();
  2065. while (!delSelection.empty())
  2066. {
  2067. Item * item = delSelection.front();
  2068. setItemSelected(item->mId,false);
  2069. if ( item->mParent )
  2070. _deleteItem( item );
  2071. delSelection.pop_front();
  2072. }
  2073. }
  2074. mSelected.clear();
  2075. mSelectedItems.clear();
  2076. mSelectedItem = 0;
  2077. onObjectDeleteCompleted_callback();
  2078. }
  2079. //------------------------------------------------------------------------------
  2080. // keyboard movement of items is restricted to just one item at a time
  2081. // if more than one item is selected then movement operations are not performed
  2082. bool GuiTreeViewCtrl::onKeyDown( const GuiEvent& event )
  2083. {
  2084. if ( !mVisible || !mActive || !mAwake )
  2085. return false;
  2086. // All the keyboard functionality requires a selected item, so if none exists...
  2087. // Deal with enter and delete
  2088. if ( event.modifier == 0 )
  2089. {
  2090. if ( event.keyCode == KEY_RETURN )
  2091. {
  2092. execAltConsoleCallback();
  2093. return true;
  2094. }
  2095. if ( event.keyCode == KEY_DELETE && mDeleteObjectAllowed )
  2096. {
  2097. // Don't delete the root!
  2098. if (mSelectedItems.empty())
  2099. return true;
  2100. //this may be fighting with the world editor delete
  2101. deleteSelection();
  2102. return true;
  2103. }
  2104. //call a generic bit of script that will let the subclass know that a key was pressed
  2105. onKeyDown_callback( event.modifier, event.keyCode );
  2106. }
  2107. // only do operations if only one item is selected
  2108. if ( mSelectedItems.empty() || (mSelectedItems.size() > 1))
  2109. return false;
  2110. Item* item = mSelectedItems.first();
  2111. if ( !item )
  2112. return false;
  2113. // The Alt key lets you move items around!
  2114. if ( mFlags.test(IsEditable) && event.modifier & SI_ALT )
  2115. {
  2116. switch ( event.keyCode )
  2117. {
  2118. case KEY_UP:
  2119. // Move us up.
  2120. if ( item->mPrevious )
  2121. {
  2122. moveItemUp( item->mId );
  2123. scrollVisible(item);
  2124. }
  2125. return true;
  2126. case KEY_DOWN:
  2127. // Move the item under us up.
  2128. if ( item->mNext )
  2129. {
  2130. moveItemUp( item->mNext->mId );
  2131. scrollVisible(item);
  2132. }
  2133. return true;
  2134. case KEY_LEFT:
  2135. if ( item->mParent )
  2136. {
  2137. if ( item->mParent->mParent )
  2138. {
  2139. // Ok, we have both an immediate parent, and a grandparent.
  2140. // The goal of left-arrow alt is to become the child of our
  2141. // grandparent, ie, to become a sibling of our parent.
  2142. // First, unlink item from its siblings.
  2143. if ( item->mPrevious )
  2144. item->mPrevious->mNext = item->mNext;
  2145. else
  2146. item->mParent->mChild = item->mNext;
  2147. if ( item->mNext )
  2148. item->mNext->mPrevious = item->mPrevious;
  2149. // Now, relink as the next sibling of our parent.
  2150. item->mPrevious = item->mParent;
  2151. item->mNext = item->mParent->mNext;
  2152. // If there was already a next sibling, deal with that case.
  2153. if ( item->mNext )
  2154. item->mNext->mPrevious = item;
  2155. item->mParent->mNext = item;
  2156. // Snag the current parent set if any...
  2157. SimSet *parentSet = NULL;
  2158. if(item->mParent->isInspectorData())
  2159. parentSet = dynamic_cast<SimSet*>(item->mParent->getObject());
  2160. else
  2161. {
  2162. // parent is probably script data so we search up the tree for a
  2163. // set to put our object in
  2164. Item * temp = item->mParent;
  2165. while (!temp->isInspectorData())
  2166. temp = temp->mParent;
  2167. // found a ancestor who is an inspectorData
  2168. if (temp->isInspectorData())
  2169. parentSet = dynamic_cast<SimSet*>(temp->getObject());
  2170. else parentSet = NULL;
  2171. }
  2172. // Get our active SimObject if any
  2173. SimObject *simObj = NULL;
  2174. if(item->isInspectorData())
  2175. simObj = item->getObject();
  2176. // Remove from the old parentset...
  2177. if(simObj && parentSet) {
  2178. if (parentSet->size()>0)
  2179. {
  2180. SimObject *lastObject = parentSet->last();
  2181. parentSet->removeObject(simObj);
  2182. parentSet->reOrder(lastObject);
  2183. } else
  2184. parentSet->removeObject(simObj);
  2185. }
  2186. // And finally, update our item
  2187. item->mParent = item->mParent->mParent;
  2188. // Snag the newparent set if any...
  2189. SimSet *newParentSet = NULL;
  2190. if(item->mParent->isInspectorData())
  2191. newParentSet = dynamic_cast<SimSet*>(item->mParent->getObject());
  2192. else
  2193. {
  2194. // parent is probably script data so we search up the tree for a
  2195. // set to put our object in
  2196. Item * temp = item->mParent;
  2197. while (!temp->isInspectorData())
  2198. temp = temp->mParent;
  2199. // found a ancestor who is an inspectorData
  2200. if (temp->isInspectorData())
  2201. newParentSet = dynamic_cast<SimSet*>(temp->getObject());
  2202. else newParentSet = NULL;
  2203. }
  2204. if(simObj && newParentSet)
  2205. {
  2206. newParentSet->addObject(simObj);
  2207. Item * temp = item->mNext;
  2208. // item->mNext may be script, so find an inspector item to reorder with if any
  2209. if (temp) {
  2210. do {
  2211. if (temp->isInspectorData())
  2212. break;
  2213. temp = temp->mNext;
  2214. } while (temp);
  2215. if (temp && item->getObject() && temp->getObject()) //do we still have a item->mNext? If not then don't bother reordering
  2216. newParentSet->reOrder(item->getObject(), temp->getObject());
  2217. }
  2218. } else if (!simObj&&newParentSet) {
  2219. // our current item is script data. but it may have children who
  2220. // is inspector data who need an updated set
  2221. if (item->mChild)
  2222. inspectorSearch(item->mChild, item, parentSet, newParentSet);
  2223. }
  2224. // And update everything hurrah.
  2225. buildVisibleTree();
  2226. scrollVisible(item);
  2227. }
  2228. }
  2229. return true;
  2230. case KEY_RIGHT:
  2231. if ( item->mPrevious )
  2232. {
  2233. // Make the item the last child of its previous sibling.
  2234. // First, unlink from the current position in the list
  2235. item->mPrevious->mNext = item->mNext;
  2236. if ( item->mNext )
  2237. item->mNext->mPrevious = item->mPrevious;
  2238. // Get the object we're poking with.
  2239. SimObject *simObj = NULL;
  2240. SimSet *parentSet = NULL;
  2241. if(item->isInspectorData())
  2242. simObj = item->getObject();
  2243. if(item->mParent->isInspectorData())
  2244. parentSet = dynamic_cast<SimSet*>(item->mParent->getObject());
  2245. else {
  2246. // parent is probably script data so we search up the tree for a
  2247. // set to put our object in
  2248. Item * temp = item->mParent;
  2249. while (!temp->isInspectorData())
  2250. temp = temp->mParent;
  2251. // found an ancestor who is an inspectorData
  2252. if (temp->isInspectorData())
  2253. parentSet = dynamic_cast<SimSet*>(temp->getObject());
  2254. }
  2255. // If appropriate, remove from the current SimSet.
  2256. if(parentSet && simObj) {
  2257. if (parentSet->size()>0)
  2258. {
  2259. SimObject *lastObject = parentSet->last();
  2260. parentSet->removeObject(simObj);
  2261. parentSet->reOrder(lastObject);
  2262. } else
  2263. parentSet->removeObject(simObj);
  2264. }
  2265. // Now, make our previous sibling our parent...
  2266. item->mParent = item->mPrevious;
  2267. item->mNext = NULL;
  2268. // And sink us down to the end of its siblings, if appropriate.
  2269. if ( item->mParent->mChild )
  2270. {
  2271. Item* temp = item->mParent->mChild;
  2272. while ( temp->mNext )
  2273. temp = temp->mNext;
  2274. temp->mNext = item;
  2275. item->mPrevious = temp;
  2276. }
  2277. else
  2278. {
  2279. // only child...<sniff>
  2280. item->mParent->mChild = item;
  2281. item->mPrevious = NULL;
  2282. }
  2283. // Make sure the new parent is expanded:
  2284. if ( !item->mParent->mState.test( Item::Expanded ) )
  2285. setItemExpanded( item->mParent->mId, true );
  2286. // Snag the new parent simset if any.
  2287. SimSet *newParentSet = NULL;
  2288. // new parent might be script. so figure out what set we need to add it to.
  2289. if(item->mParent->isInspectorData())
  2290. newParentSet = dynamic_cast<SimSet*>(item->mParent->getObject());
  2291. else
  2292. {
  2293. // parent is probably script data so we search up the tree for a
  2294. // set to put our object in
  2295. if (mDebug) Con::printf("oh nos my parent is script!");
  2296. Item * temp = item->mParent;
  2297. while (!temp->isInspectorData())
  2298. temp = temp->mParent;
  2299. // found a ancestor who is an inspectorData
  2300. if (temp->isInspectorData())
  2301. newParentSet = dynamic_cast<SimSet*>(temp->getObject());
  2302. else newParentSet = NULL;
  2303. }
  2304. // Add the item's SimObject to the new parent simset, at the end.
  2305. if(newParentSet && simObj)
  2306. newParentSet->addObject(simObj);
  2307. else if (!simObj&&newParentSet&&parentSet) {
  2308. // our current item is script data. but it may have children who
  2309. // is inspector data who need an updated set
  2310. if (item->mChild) {
  2311. inspectorSearch(item->mChild, item, parentSet, newParentSet);
  2312. }
  2313. }
  2314. scrollVisible(item);
  2315. }
  2316. return true;
  2317. default:
  2318. break;
  2319. }
  2320. }
  2321. // Explorer-esque navigation...
  2322. switch( event.keyCode )
  2323. {
  2324. case KEY_UP:
  2325. // Select previous visible item:
  2326. if ( item->mPrevious )
  2327. {
  2328. item = item->mPrevious;
  2329. while ( item->isParent() && item->isExpanded() )
  2330. {
  2331. item = item->mChild;
  2332. while ( item->mNext )
  2333. item = item->mNext;
  2334. }
  2335. clearSelection();
  2336. addSelection( item->mId );
  2337. return true;
  2338. }
  2339. // or select parent:
  2340. if ( item->mParent )
  2341. {
  2342. clearSelection();
  2343. addSelection( item->mParent->mId );
  2344. return true;
  2345. }
  2346. return false;
  2347. break;
  2348. case KEY_DOWN:
  2349. // Selected child if it is visible:
  2350. if ( item->isParent() && item->isExpanded() )
  2351. {
  2352. clearSelection();
  2353. addSelection( item->mChild->mId );
  2354. return true;
  2355. }
  2356. // or select next sibling (recursively):
  2357. do
  2358. {
  2359. if ( item->mNext )
  2360. {
  2361. clearSelection();
  2362. addSelection( item->mNext->mId );
  2363. return true;
  2364. }
  2365. item = item->mParent;
  2366. } while ( item );
  2367. return false;
  2368. break;
  2369. case KEY_LEFT:
  2370. // Contract current menu:
  2371. if ( item->isExpanded() )
  2372. {
  2373. setItemExpanded( item->mId, false );
  2374. scrollVisible(item);
  2375. return true;
  2376. }
  2377. // or select parent:
  2378. if ( item->mParent )
  2379. {
  2380. clearSelection();
  2381. addSelection( item->mParent->mId );
  2382. return true;
  2383. }
  2384. return false;
  2385. break;
  2386. case KEY_RIGHT:
  2387. // Expand selected item:
  2388. if ( item->isParent() )
  2389. {
  2390. if ( !item->isExpanded() )
  2391. {
  2392. setItemExpanded( item->mId, true );
  2393. scrollVisible(item);
  2394. return true;
  2395. }
  2396. // or select child:
  2397. clearSelection();
  2398. addSelection( item->mChild->mId );
  2399. return true;
  2400. }
  2401. return false;
  2402. break;
  2403. default:
  2404. break;
  2405. }
  2406. // Not processed, so pass the event on:
  2407. return Parent::onKeyDown( event );
  2408. }
  2409. //------------------------------------------------------------------------------
  2410. // on mouse up look at the current item and check to see if it is valid
  2411. // to move the selected item(s) to it.
  2412. void GuiTreeViewCtrl::onMouseUp(const GuiEvent &event)
  2413. {
  2414. if( !mActive || !mAwake || !mVisible )
  2415. return;
  2416. if( isMethod("onMouseUp") )
  2417. {
  2418. BitSet32 hitFlags = 0;
  2419. Item* item;
  2420. S32 hitItemId = -1;
  2421. if( _hitTest( event.mousePoint, item, hitFlags ) )
  2422. hitItemId = item->mId;
  2423. onMouseUp_callback( hitItemId, event.mouseClickCount );
  2424. }
  2425. mouseUnlock();
  2426. if ( mSelectedItems.empty())
  2427. {
  2428. mDragMidPoint = NomDragMidPoint;
  2429. return;
  2430. }
  2431. BitSet32 hitFlags = 0;
  2432. Item *item;
  2433. bool hitCheck = _hitTest( event.mousePoint, item, hitFlags );
  2434. mRenamingItem = NULL;
  2435. if( hitCheck )
  2436. {
  2437. if ( event.mouseClickCount == 1 && !mMouseDragged && mPossibleRenameItem != NULL )
  2438. {
  2439. if ( item == mPossibleRenameItem )
  2440. showItemRenameCtrl( item );
  2441. }
  2442. else // If mouseUp occurs on the same item as mouse down
  2443. {
  2444. bool wasSelected = isSelected( item );
  2445. bool multiSelect = getSelectedItemsCount() > 1;
  2446. if( wasSelected && multiSelect && item == mTempItem )
  2447. {
  2448. clearSelection();
  2449. addSelection( item->mId );
  2450. }
  2451. }
  2452. }
  2453. mPossibleRenameItem = NULL;
  2454. if (!mMouseDragged)
  2455. return;
  2456. Item* newItem = NULL;
  2457. Item* newItem2 = NULL;
  2458. if (mFlags.test(IsEditable))
  2459. {
  2460. Parent::onMouseMove( event );
  2461. BitSet32 hitFlags = 0;
  2462. if( !_hitTest( event.mousePoint, newItem2, hitFlags ) )
  2463. {
  2464. if( !mShowRoot )
  2465. newItem2 = mRoot;
  2466. else
  2467. {
  2468. if( mDebug )
  2469. Con::printf( "Nothing hit" );
  2470. mDragMidPoint = NomDragMidPoint;
  2471. return;
  2472. }
  2473. }
  2474. newItem2->mState.clear(Item::MouseOverBmp | Item::MouseOverText );
  2475. // If the hit item is the visible root, make sure
  2476. // we don't allow dragging above.
  2477. if( newItem2 == mRoot && mDragMidPoint == AbovemDragMidPoint )
  2478. {
  2479. if( mDebug )
  2480. Con::printf( "Rejecting to make child sibling of root" );
  2481. mDragMidPoint = NomDragMidPoint;
  2482. return;
  2483. }
  2484. // if the newItem isn't in the mSelectedItemList then continue.
  2485. Vector<Item *>::iterator k;
  2486. for(k = mSelectedItems.begin(); k != mSelectedItems.end(); k++)
  2487. {
  2488. newItem = newItem2;
  2489. if (*(k) == newItem)
  2490. {
  2491. mDragMidPoint = NomDragMidPoint;
  2492. return;
  2493. }
  2494. Item * temp = *(k);
  2495. Item * grandpaTemp = newItem->mParent;
  2496. // grandpa check, kick out if an item would be its own ancestor
  2497. while (grandpaTemp)
  2498. {
  2499. if (temp == grandpaTemp)
  2500. {
  2501. if (mDebug)
  2502. {
  2503. Con::printf("grandpa check");
  2504. if (temp->isInspectorData())
  2505. Con::printf("temp's name: %s",temp->getObject()->getName());
  2506. if (grandpaTemp->isInspectorData())
  2507. Con::printf("grandpa's name: %s",grandpaTemp->getObject()->getName());
  2508. }
  2509. mDragMidPoint = NomDragMidPoint;
  2510. return;
  2511. }
  2512. grandpaTemp = grandpaTemp->mParent;
  2513. }
  2514. }
  2515. // Notify script for undo.
  2516. onBeginReparenting_callback();
  2517. // Reparent the items.
  2518. for (S32 i = 0; i <mSelectedItems.size();i++)
  2519. {
  2520. newItem = newItem2;
  2521. Item * item = mSelectedItems[i];
  2522. if (mDebug) Con::printf("----------------------------");
  2523. // clear old highlighting of the item
  2524. item->mState.clear(Item::MouseOverBmp | Item::MouseOverText );
  2525. // move the selected item to the newItem
  2526. Item* oldParent = item->mParent;
  2527. // Snag the current parent set if any for future reference
  2528. SimSet *parentSet = NULL;
  2529. if(oldParent->isInspectorData())
  2530. parentSet = dynamic_cast<SimSet*>(oldParent->getObject());
  2531. else
  2532. {
  2533. // parent is probably script data so we search up the tree for a
  2534. // set to put our object in
  2535. Item * temp = oldParent;
  2536. while (temp)
  2537. {
  2538. if (temp->isInspectorData())
  2539. break;
  2540. temp = temp->mParent;
  2541. }
  2542. // found an ancestor who is an inspectorData
  2543. if (temp)
  2544. {
  2545. if (temp->isInspectorData())
  2546. parentSet = dynamic_cast<SimSet*>(temp->getObject());
  2547. }
  2548. }
  2549. // unlink from the current position in the list
  2550. unlinkItem(item);
  2551. // update the parent's children
  2552. // check if we an only child
  2553. if (item->mParent->mChild == item)
  2554. {
  2555. if (item->mNext)
  2556. item->mParent->mChild = item->mNext;
  2557. else
  2558. item->mParent->mChild = NULL;
  2559. }
  2560. if (mDragMidPoint != NomDragMidPoint)
  2561. {
  2562. //if it is below an expanded tree, place as last item in the tree
  2563. //if it is below a parent who isn't expanded put below it
  2564. // position the item above or below another item
  2565. if (mDragMidPoint == AbovemDragMidPoint)
  2566. {
  2567. // easier to treat everything as "Below the mDragMidPoint" so make some adjustments
  2568. if (mDebug) Con::printf("adding item above mDragMidPoint");
  2569. // above the mid point of an item, so grab either the parent
  2570. // or the previous sibling
  2571. // does the item have a previous sibling?
  2572. if (newItem->mPrevious)
  2573. {
  2574. newItem = newItem->mPrevious;
  2575. if (mDebug) Con::printf("treating as if below an item that isn't expanded");
  2576. // otherwise add below that item as a sibling
  2577. item->mParent = newItem->mParent;
  2578. item->mPrevious = newItem;
  2579. item->mNext = newItem->mNext;
  2580. if (newItem->mNext)
  2581. newItem->mNext->mPrevious = item;
  2582. newItem->mNext = item;
  2583. }
  2584. else
  2585. {
  2586. if (mDebug) Con::printf("treating as if adding below the parent of the item");
  2587. // instead we add as the first item below the newItem's parent
  2588. item->mParent = newItem->mParent;
  2589. item->mNext = newItem;
  2590. item->mPrevious = NULL;
  2591. newItem->mPrevious = item;
  2592. item->mParent->mChild = item;
  2593. }
  2594. }
  2595. else if (mDragMidPoint == BelowmDragMidPoint)
  2596. {
  2597. if ((newItem->isParent())&&(newItem->isExpanded()))
  2598. {
  2599. if (mDebug) Con::printf("adding item to an expanded parent below the mDragMidPoint");
  2600. item->mParent = newItem;
  2601. // then add the new item as a child
  2602. item->mNext = newItem->mChild;
  2603. if (newItem->mChild)
  2604. newItem->mChild->mPrevious = item;
  2605. item->mParent->mChild = item;
  2606. item->mPrevious = NULL;
  2607. }
  2608. else if ((!newItem->mNext)&&(newItem->mParent)&&(newItem->mParent->mParent))
  2609. {
  2610. // add below it's parent.
  2611. if (mDebug) Con::printf("adding below a tree");
  2612. item->mParent = newItem->mParent->mParent;
  2613. item->mNext = newItem->mParent->mNext;
  2614. item->mPrevious = newItem->mParent;
  2615. if (newItem->mParent->mNext)
  2616. newItem->mParent->mNext->mPrevious = item;
  2617. newItem->mParent->mNext = item;
  2618. }
  2619. else
  2620. {
  2621. // adding below item not as a child
  2622. if (mDebug) Con::printf("adding item below the mDragMidPoint of an item");
  2623. item->mParent = newItem->mParent;
  2624. // otherwise the item is a sibling
  2625. if (newItem->mNext)
  2626. newItem->mNext->mPrevious = item;
  2627. item->mNext = newItem->mNext;
  2628. item->mPrevious = newItem;
  2629. newItem->mNext = item;
  2630. }
  2631. }
  2632. }
  2633. // if we're not allowed to add to items, then try to add to the parent of the hit item.
  2634. // if we are, just add to the item we hit.
  2635. else
  2636. {
  2637. if (mDebug)
  2638. {
  2639. if (item->isInspectorData() && item->getObject())
  2640. Con::printf("Item: %i",item->getObject()->getId());
  2641. if (newItem->isInspectorData() && newItem->getObject())
  2642. Con::printf("Parent: %i",newItem->getObject()->getId());
  2643. Con::printf("dragged onto an item");
  2644. }
  2645. // If the hit item is not a valid drag target,
  2646. // then try to add to the parent.
  2647. if( !isValidDragTarget( newItem ) )
  2648. {
  2649. // add to the item's parent.
  2650. if(!newItem->mParent || !newItem->mParent->isParent())
  2651. {
  2652. if(mDebug)
  2653. Con::printf("could not find the parent of that item. dragging to an item is not allowed, kicking out.");
  2654. mDragMidPoint = NomDragMidPoint;
  2655. continue;
  2656. }
  2657. newItem = newItem->mParent;
  2658. }
  2659. // new parent is the item in the current cell
  2660. item->mParent = newItem;
  2661. // adjust children if any
  2662. if (newItem->mChild)
  2663. {
  2664. if (mDebug) Con::printf("not the first child");
  2665. // put it at the top of the list (easier to find if there are many children)
  2666. if (newItem->mChild)
  2667. newItem->mChild->mPrevious = item;
  2668. item->mNext = newItem->mChild;
  2669. newItem->mChild = item;
  2670. item->mPrevious = NULL;
  2671. }
  2672. else
  2673. {
  2674. if (mDebug) Con::printf("first child");
  2675. // only child
  2676. newItem->mChild = item;
  2677. item->mNext = NULL;
  2678. item->mPrevious = NULL;
  2679. }
  2680. }
  2681. // expand the item we added to, if it isn't expanded already
  2682. if( !item->mParent->mState.test( Item::Expanded ) )
  2683. setItemExpanded( item->mParent->mId, true );
  2684. //----------------------------------------------------------------
  2685. // handle objects
  2686. // Get our active SimObject if any
  2687. SimObject *simObj = NULL;
  2688. if(item->isInspectorData())
  2689. {
  2690. simObj = item->getObject();
  2691. }
  2692. // Remove from the old parentset
  2693. if((simObj && parentSet)&&(oldParent != item->mParent))
  2694. {
  2695. if (mDebug) Con::printf("removing item from old parentset");
  2696. // hack to get around the way removeObject takes the last item of the set
  2697. // and moves it into the place of the object we removed
  2698. if (parentSet->size()>0)
  2699. {
  2700. SimObject *lastObject = parentSet->last();
  2701. parentSet->removeObject(simObj);
  2702. parentSet->reOrder(lastObject);
  2703. }
  2704. else
  2705. {
  2706. parentSet->removeObject(simObj);
  2707. }
  2708. }
  2709. // Snag the newparent set if any...
  2710. SimSet *newParentSet = NULL;
  2711. if(item->mParent->isInspectorData())
  2712. {
  2713. if (mDebug) Con::printf("getting a new parent set");
  2714. SimObject * tmpObj = item->mParent->getObject();
  2715. newParentSet = dynamic_cast<SimSet*>(tmpObj);
  2716. }
  2717. else
  2718. {
  2719. // parent is probably script data so we search up the tree for a
  2720. // set to put our object in
  2721. if (mDebug) Con::printf("oh nos my parent is script!");
  2722. Item * temp = item->mParent;
  2723. while (temp)
  2724. {
  2725. if (temp->isInspectorData())
  2726. break;
  2727. temp = temp->mParent;
  2728. }
  2729. // found a ancestor who is an inspectorData
  2730. if (temp)
  2731. {
  2732. if (temp->isInspectorData())
  2733. newParentSet = dynamic_cast<SimSet*>(temp->getObject());
  2734. }
  2735. else
  2736. {
  2737. newParentSet = NULL;
  2738. }
  2739. }
  2740. if(simObj && newParentSet)
  2741. {
  2742. if (mDebug) Con::printf("simobj and new ParentSet");
  2743. if (oldParent != item->mParent)
  2744. newParentSet->addObject(simObj);
  2745. //order the objects in the simset according to their
  2746. //order in the tree view control
  2747. if(!item->mNext)
  2748. {
  2749. if( item->mPrevious )
  2750. {
  2751. //bring to the end of the set
  2752. SimObject *prevObject = item->mPrevious->getObject();
  2753. if (prevObject && item->getObject())
  2754. {
  2755. newParentSet->reOrder(item->getObject(), prevObject);
  2756. }
  2757. }
  2758. }
  2759. else
  2760. {
  2761. //reorder within the set
  2762. SimObject *nextObject = item->mNext->getObject();
  2763. if(nextObject && item->getObject())
  2764. {
  2765. newParentSet->reOrder(item->getObject(), nextObject);
  2766. }
  2767. }
  2768. }
  2769. else if (!simObj&&newParentSet)
  2770. {
  2771. // our current item is script data. but it may have children who
  2772. // is inspector data who need an updated set
  2773. if (mDebug) Con::printf("no simobj but new parentSet");
  2774. if (item->mChild)
  2775. inspectorSearch(item->mChild, item, parentSet, newParentSet);
  2776. }
  2777. else if (simObj&&!newParentSet)
  2778. {
  2779. if (mDebug) Con::printf("simobject and no new parent set");
  2780. }
  2781. else
  2782. if (mDebug) Con::printf("no simobject and no new parent set");
  2783. // Notify script.
  2784. if( item->isInspectorData() )
  2785. onReparent_callback(
  2786. item->getObject()->getId(),
  2787. oldParent->getObject()->getId(),
  2788. item->mParent->getObject()->getId()
  2789. );
  2790. else
  2791. onReparent_callback(
  2792. item->mId,
  2793. oldParent->mId,
  2794. item->mParent->mId
  2795. );
  2796. }
  2797. onEndReparenting_callback();
  2798. // And update everything.
  2799. scrollVisible(newItem);
  2800. onDragDropped_callback();
  2801. }
  2802. mDragMidPoint = NomDragMidPoint;
  2803. }
  2804. //------------------------------------------------------------------------------
  2805. void GuiTreeViewCtrl::onMouseDragged(const GuiEvent &event)
  2806. {
  2807. if( mDragStartInSelection )
  2808. onMouseDragged_callback();
  2809. if(!mSupportMouseDragging)
  2810. return;
  2811. if( !mActive || !mAwake || !mVisible )
  2812. return;
  2813. if (mSelectedItems.size() == 0)
  2814. return;
  2815. // Give us a little delta before we actually start a mouse drag so that
  2816. // if the user moves the mouse a little while clicking, he/she does not
  2817. // accidentally trigger a drag.
  2818. if( mFabs( ( mMouseDownPoint - event.mousePoint ).len() ) <= 4.f )
  2819. return;
  2820. Point2I pt = globalToLocalCoord(event.mousePoint);
  2821. Parent::onMouseMove(event);
  2822. mouseLock();
  2823. mMouseDragged = true;
  2824. // If the drag is outside of our visible area,
  2825. // start scrolling.
  2826. GuiScrollCtrl* scrollCtrl = dynamic_cast< GuiScrollCtrl* >( getParent() );
  2827. if( scrollCtrl && !scrollCtrl->isPointVisible( pt ) )
  2828. {
  2829. S32 widthDelta = 0;
  2830. S32 heightDelta = 0;
  2831. if( pt.x < scrollCtrl->getChildRelPos().x )
  2832. widthDelta = pt.x - scrollCtrl->getChildRelPos().x;
  2833. else if( pt.x > scrollCtrl->getChildRelPos().x + scrollCtrl->getContentExtent().x )
  2834. widthDelta = pt.x - scrollCtrl->getChildRelPos().x - scrollCtrl->getContentExtent().x;
  2835. if( pt.y < scrollCtrl->getChildRelPos().y )
  2836. heightDelta = pt.y - scrollCtrl->getChildRelPos().y;
  2837. else if( pt.y > scrollCtrl->getChildRelPos().y + scrollCtrl->getContentExtent().y )
  2838. heightDelta = pt.y - scrollCtrl->getChildRelPos().y - scrollCtrl->getContentExtent().y;
  2839. const F32 SCROLL_RATIO = 0.5f;
  2840. scrollCtrl->scrollDelta( S32( F32( widthDelta ) * SCROLL_RATIO ), S32( F32( heightDelta ) * SCROLL_RATIO ) );
  2841. }
  2842. // whats our mDragMidPoint?
  2843. mCurrentDragCell = mMouseOverCell.y;
  2844. S32 midpCell = mCurrentDragCell * mItemHeight + (mItemHeight/2);
  2845. S32 currentY = pt.y;
  2846. S32 yDiff = currentY-midpCell;
  2847. S32 variance = (mItemHeight/5);
  2848. if( mPreviousDragCell >= 0 && mPreviousDragCell < mVisibleItems.size() )
  2849. mVisibleItems[mPreviousDragCell]->mState.clear( Item::MouseOverBmp | Item::MouseOverText | Item::MouseOverIcon );
  2850. bool hoverItem = false;
  2851. if (mAbs(yDiff) <= variance)
  2852. {
  2853. mDragMidPoint = NomDragMidPoint;
  2854. // highlight the current item
  2855. // hittest to detect whether we are on an item
  2856. // ganked from onMouseMouse
  2857. // used for tracking what our last cell was so we can clear it.
  2858. mPreviousDragCell = mCurrentDragCell;
  2859. if (mCurrentDragCell >= 0)
  2860. {
  2861. Item* item = NULL;
  2862. BitSet32 hitFlags = 0;
  2863. if ( !_hitTest( event.mousePoint, item, hitFlags ) )
  2864. return;
  2865. // If the item is a valid drag target, activate the item
  2866. // highlighting.
  2867. if( isValidDragTarget( item ) )
  2868. {
  2869. hoverItem = true;
  2870. if ( hitFlags.test( OnImage ) )
  2871. item->mState.set( Item::MouseOverBmp );
  2872. if ( hitFlags.test( OnText ) )
  2873. item->mState.set( Item::MouseOverText );
  2874. if ( hitFlags.test( OnIcon ) )
  2875. item->mState.set( Item::MouseOverIcon );
  2876. // Always redraw the entire mouse over item, since we are distinguishing
  2877. // between the bitmap and the text:
  2878. setUpdateRegion( Point2I( mMouseOverCell.x * mCellSize.x, mMouseOverCell.y * mCellSize.y ), mCellSize );
  2879. }
  2880. }
  2881. }
  2882. if ( !hoverItem )
  2883. {
  2884. //above or below an item?
  2885. if (yDiff < 0)
  2886. mDragMidPoint = AbovemDragMidPoint;
  2887. else
  2888. mDragMidPoint = BelowmDragMidPoint;
  2889. }
  2890. }
  2891. //-----------------------------------------------------------------------------
  2892. void GuiTreeViewCtrl::onMiddleMouseDown(const GuiEvent & event)
  2893. {
  2894. //for debugging items
  2895. if (mDebug) {
  2896. Item* item;
  2897. BitSet32 hitFlags = 0;
  2898. _hitTest( event.mousePoint, item, hitFlags );
  2899. Con::printf("debugging %d", item->mId);
  2900. Point2I pt = globalToLocalCoord(event.mousePoint);
  2901. if (item->isInspectorData() && item->getObject()) {
  2902. Con::printf("object data:");
  2903. Con::printf("name:%s",item->getObject()->getName());
  2904. Con::printf("className:%s",item->getObject()->getClassName());
  2905. }
  2906. Con::printf("contents of mSelectedItems:");
  2907. for(S32 i = 0; i < mSelectedItems.size(); i++) {
  2908. if (mSelectedItems[i]->isInspectorData()) {
  2909. Con::printf("%d",mSelectedItems[i]->getObject()->getId());
  2910. } else
  2911. Con::printf("wtf %d", mSelectedItems[i]);
  2912. }
  2913. Con::printf("contents of mSelected");
  2914. for (S32 j = 0; j < mSelected.size(); j++) {
  2915. Con::printf("%d", mSelected[j]);
  2916. }
  2917. S32 mCurrentDragCell = mMouseOverCell.y;
  2918. S32 midpCell = (mCurrentDragCell) * mItemHeight + (mItemHeight/2);
  2919. S32 currentY = pt.y;
  2920. S32 yDiff = currentY-midpCell;
  2921. Con::printf("cell info: (%d,%d) mCurrentDragCell=%d est=(%d,%d,%d) ydiff=%d",pt.x,pt.y,mCurrentDragCell,mCurrentDragCell*mItemHeight, midpCell, (mCurrentDragCell+1)*mItemHeight,yDiff);
  2922. }
  2923. }
  2924. //-----------------------------------------------------------------------------
  2925. void GuiTreeViewCtrl::onMouseDown(const GuiEvent & event)
  2926. {
  2927. if( !mActive || !mAwake || !mVisible )
  2928. {
  2929. Parent::onMouseDown(event);
  2930. return;
  2931. }
  2932. if ( mProfile->mCanKeyFocus )
  2933. setFirstResponder();
  2934. Item * item = 0;
  2935. BitSet32 hitFlags;
  2936. mDragMidPoint = NomDragMidPoint;
  2937. mMouseDragged = false;
  2938. mMouseDownPoint = event.mousePoint;
  2939. //
  2940. if(!_hitTest(event.mousePoint, item, hitFlags))
  2941. return;
  2942. mPossibleRenameItem = NULL;
  2943. mRenamingItem = NULL;
  2944. mTempItem = NULL;
  2945. //
  2946. if( event.modifier & SI_MULTISELECT )
  2947. {
  2948. bool selectFlag = item->mState.test(Item::Selected);
  2949. if (selectFlag == true)
  2950. {
  2951. // already selected, so unselect it and remove it
  2952. removeSelection(item->mId);
  2953. }
  2954. else
  2955. {
  2956. addSelection(item->mId);
  2957. }
  2958. }
  2959. else if( event.modifier & SI_RANGESELECT && mMultipleSelections )
  2960. {
  2961. // is something already selected?
  2962. S32 firstSelectedIndex = 0;
  2963. Item * firstItem = NULL;
  2964. if (!mSelectedItems.empty())
  2965. {
  2966. firstItem = mSelectedItems.front();
  2967. for (S32 i = 0; i < mVisibleItems.size();i++)
  2968. {
  2969. if (mVisibleItems[i] == mSelectedItems.front())
  2970. {
  2971. firstSelectedIndex = i;
  2972. break;
  2973. }
  2974. }
  2975. S32 mCurrentDragCell = mMouseOverCell.y;
  2976. if (mVisibleItems[firstSelectedIndex] != firstItem )
  2977. {
  2978. /*
  2979. Con::printf("something isn't right...");
  2980. if (mVisibleItems[firstSelectedIndex]->isInspectorData())
  2981. Con::printf("visibleItem %s",mVisibleItems[firstSelectedIndex]->getObject()->getName());
  2982. if (firstItem->isInspectorData())
  2983. Con::printf("firstItem %s",firstItem->getObject()->getName());
  2984. */
  2985. }
  2986. else
  2987. {
  2988. // select the cells
  2989. onAddMultipleSelectionBegin_callback();
  2990. if ((mCurrentDragCell) < firstSelectedIndex)
  2991. {
  2992. //select up
  2993. for (S32 j = (mCurrentDragCell); j < firstSelectedIndex; j++)
  2994. {
  2995. if( j != (firstSelectedIndex - 1) )
  2996. addSelection(mVisibleItems[j]->mId, false, false);
  2997. else
  2998. addSelection(mVisibleItems[j]->mId, false);
  2999. }
  3000. }
  3001. else
  3002. {
  3003. // select down
  3004. for (S32 j = firstSelectedIndex+1; j < (mCurrentDragCell+1); j++)
  3005. {
  3006. if( j != mCurrentDragCell )
  3007. addSelection(mVisibleItems[j]->mId, false, false);
  3008. else
  3009. addSelection(mVisibleItems[j]->mId, false);
  3010. }
  3011. }
  3012. // Scroll to view the last selected cell.
  3013. scrollVisible( mVisibleItems[mCurrentDragCell] );
  3014. onAddMultipleSelectionEnd_callback();
  3015. }
  3016. }
  3017. }
  3018. else if ( event.modifier & SI_PRIMARY_ALT )
  3019. {
  3020. if ( item->isInspectorData() && item->getObject() )
  3021. setAddGroup(item->getObject());
  3022. }
  3023. else if ( !hitFlags.test(OnImage) )
  3024. {
  3025. mTempItem = item;
  3026. bool wasSelected = isSelected( item );
  3027. bool multiSelect = getSelectedItemsCount() > 1;
  3028. if( !wasSelected || !multiSelect )
  3029. {
  3030. if ( mClearAllOnSingleSelection )
  3031. clearSelection();
  3032. if ( !wasSelected || mClearAllOnSingleSelection )
  3033. addSelection( item->mId );
  3034. if ( wasSelected &&
  3035. !multiSelect &&
  3036. mCanRenameObjects &&
  3037. hitFlags.test(OnText) &&
  3038. mFlags.test(IsEditable) &&
  3039. item->isInspectorData() &&
  3040. item->getObject() &&
  3041. item->getObject()->isNameChangeAllowed() &&
  3042. item != mRoot &&
  3043. event.mouseClickCount == 1 )
  3044. {
  3045. mPossibleRenameItem = item;
  3046. if ( isMethod( "canRenameObject" ) )
  3047. {
  3048. if( canRenameObject_callback( item->getObject() ) )
  3049. mPossibleRenameItem = NULL;
  3050. }
  3051. }
  3052. }
  3053. }
  3054. if ( ( hitFlags.test( OnText ) || hitFlags.test( OnIcon ) ) &&
  3055. event.mouseClickCount > 1 )
  3056. execAltConsoleCallback();
  3057. // For dragging, note if hit is in selection.
  3058. mDragStartInSelection = isItemSelected( item->mId );
  3059. if(!item->isParent())
  3060. return;
  3061. //
  3062. if ( mFullRowSelect || hitFlags.test( OnImage ) )
  3063. {
  3064. item->setExpanded(!item->isExpanded());
  3065. if( !item->isInspectorData() && item->mState.test(Item::VirtualParent) )
  3066. onVirtualParentExpand(item);
  3067. mFlags.set( RebuildVisible );
  3068. scrollVisible(item);
  3069. }
  3070. }
  3071. //------------------------------------------------------------------------------
  3072. void GuiTreeViewCtrl::onMouseMove( const GuiEvent &event )
  3073. {
  3074. if ( mMouseOverCell.y >= 0 && mVisibleItems.size() > mMouseOverCell.y)
  3075. mVisibleItems[mMouseOverCell.y]->mState.clear( Item::MouseOverBmp | Item::MouseOverText | Item::MouseOverIcon);
  3076. Parent::onMouseMove( event );
  3077. if ( mMouseOverCell.y >= 0 )
  3078. {
  3079. Item* item = NULL;
  3080. BitSet32 hitFlags = 0;
  3081. if ( !_hitTest( event.mousePoint, item, hitFlags ) )
  3082. return;
  3083. if ( hitFlags.test( OnImage ) )
  3084. item->mState.set( Item::MouseOverBmp );
  3085. if ( hitFlags.test( OnText ) )
  3086. item->mState.set( Item::MouseOverText );
  3087. if ( hitFlags.test( OnIcon ) )
  3088. item->mState.set( Item::MouseOverIcon );
  3089. // Always redraw the entire mouse over item, since we are distinguishing
  3090. // between the bitmap and the text:
  3091. setUpdateRegion( Point2I( mMouseOverCell.x * mCellSize.x, mMouseOverCell.y * mCellSize.y ), mCellSize );
  3092. }
  3093. }
  3094. //------------------------------------------------------------------------------
  3095. void GuiTreeViewCtrl::onMouseEnter( const GuiEvent &event )
  3096. {
  3097. Parent::onMouseEnter( event );
  3098. onMouseMove( event );
  3099. }
  3100. //------------------------------------------------------------------------------
  3101. void GuiTreeViewCtrl::onMouseLeave( const GuiEvent &event )
  3102. {
  3103. if ( mMouseOverCell.y >= 0 && mVisibleItems.size() > mMouseOverCell.y)
  3104. mVisibleItems[mMouseOverCell.y]->mState.clear( Item::MouseOverBmp | Item::MouseOverText | Item::MouseOverIcon );
  3105. Parent::onMouseLeave( event );
  3106. }
  3107. //------------------------------------------------------------------------------
  3108. void GuiTreeViewCtrl::onRightMouseDown(const GuiEvent & event)
  3109. {
  3110. if(!mActive)
  3111. {
  3112. Parent::onRightMouseDown(event);
  3113. return;
  3114. }
  3115. Item * item = NULL;
  3116. BitSet32 hitFlags;
  3117. //
  3118. if(!_hitTest(event.mousePoint, item, hitFlags))
  3119. return;
  3120. //
  3121. if (item->isInspectorData() && item->getObject())
  3122. onRightMouseDown_callback( item->mId, event.mousePoint, item->getObject() );
  3123. else
  3124. onRightMouseDown_callback( item->mId, event.mousePoint );
  3125. }
  3126. //-----------------------------------------------------------------------------
  3127. void GuiTreeViewCtrl::onRightMouseUp(const GuiEvent & event)
  3128. {
  3129. Item *item = NULL;
  3130. BitSet32 hitFlags;
  3131. if ( !_hitTest( event.mousePoint, item, hitFlags ) )
  3132. return;
  3133. if ( hitFlags.test( OnText ) || hitFlags.test( OnIcon ) )
  3134. {
  3135. if ( !isItemSelected( item->getID() ) )
  3136. {
  3137. clearSelection();
  3138. addSelection( item->getID() );
  3139. }
  3140. if (item->isInspectorData() && item->getObject())
  3141. onRightMouseUp_callback( item->mId, event.mousePoint, item->getObject() );
  3142. else
  3143. onRightMouseUp_callback( item->mId, event.mousePoint );
  3144. }
  3145. else
  3146. {
  3147. clearSelection();
  3148. }
  3149. Parent::onRightMouseUp(event);
  3150. }
  3151. //------------------------------------------------------------------------------
  3152. void GuiTreeViewCtrl::onRender(Point2I offset, const RectI &updateRect)
  3153. {
  3154. if ( !mRenamingItem && mRenameCtrl )
  3155. {
  3156. mRenameCtrl->deleteObject();
  3157. mRenameCtrl = NULL;
  3158. }
  3159. // Get all our contents drawn!
  3160. Parent::onRender(offset,updateRect);
  3161. // Deal with drawing the drag & drop line, if any...
  3162. GFX->setClipRect(updateRect);
  3163. // only do it if we have a mDragMidPoint
  3164. if (mDragMidPoint == NomDragMidPoint || !mSupportMouseDragging )
  3165. return;
  3166. ColorF greyLine(0.5,0.5,0.5,1);
  3167. Point2F squarePt;
  3168. // CodeReview: LineWidth is not supported in Direct3D. This is lame. [5/10/2007 Pat]
  3169. // draw mDragMidPoint lines with a diamond
  3170. if (mDragMidPoint == AbovemDragMidPoint)
  3171. {
  3172. S32 tempY = mItemHeight*mCurrentDragCell+offset.y ;
  3173. squarePt.y = (F32)tempY;
  3174. squarePt.x = 125.f+offset.x;
  3175. GFX->getDrawUtil()->drawLine(0+offset.x, tempY, 250+offset.x, tempY,greyLine);
  3176. GFX->getDrawUtil()->draw2DSquare(squarePt, 6, 90 );
  3177. }
  3178. if (mDragMidPoint == BelowmDragMidPoint)
  3179. {
  3180. S32 tempY2 = mItemHeight*(mCurrentDragCell+1) +offset.y;
  3181. squarePt.y = (F32)tempY2;
  3182. squarePt.x = 125.f+offset.x;
  3183. GFX->getDrawUtil()->drawLine(0+offset.x, tempY2, 250+offset.x, tempY2,greyLine);
  3184. GFX->getDrawUtil()->draw2DSquare(squarePt,6, 90 );
  3185. }
  3186. }
  3187. //-----------------------------------------------------------------------------
  3188. void GuiTreeViewCtrl::onRenderCell(Point2I offset, Point2I cell, bool, bool )
  3189. {
  3190. if( !mVisibleItems.size() )
  3191. return;
  3192. // Do some sanity checking and data retrieval.
  3193. AssertFatal(cell.y < mVisibleItems.size(), "GuiTreeViewCtrl::onRenderCell: invalid cell");
  3194. Item * item = mVisibleItems[cell.y];
  3195. // If there's no object, deal with it.
  3196. if(item->isInspectorData())
  3197. if(!item->getObject())
  3198. return;
  3199. RectI drawRect( offset, mCellSize );
  3200. GFXDrawUtil *drawer = GFX->getDrawUtil();
  3201. drawer->clearBitmapModulation();
  3202. FrameAllocatorMarker txtBuff;
  3203. // Ok, we have the item. There are a few possibilities at this point:
  3204. // - We need to draw inheritance lines and a treeview-chosen icon
  3205. // OR
  3206. // - We have to draw an item-dependent icon
  3207. // - If we're mouseover, we have to highlight it.
  3208. //
  3209. // - We have to draw the text for the item
  3210. // - Taking into account various mouseover states
  3211. // - Taking into account the value (set or not)
  3212. // - If it's an inspector data, we have to do some custom rendering
  3213. // - ADDED: If it is being renamed, we also have custom rendering.
  3214. // Ok, first draw the tab and icon.
  3215. // Do we draw the tree lines?
  3216. if( mFlags.test(ShowTreeLines) )
  3217. {
  3218. drawRect.point.x += ( mTabSize * item->mTabLevel );
  3219. Item* parent = item->mParent;
  3220. for ( S32 i = item->mTabLevel; ( parent && i > 0 ); i-- )
  3221. {
  3222. drawRect.point.x -= mTabSize;
  3223. if ( parent->mNext )
  3224. drawer->drawBitmapSR( mProfile->mTextureObject, drawRect.point, mProfile->mBitmapArrayRects[BmpLine] );
  3225. parent = parent->mParent;
  3226. }
  3227. }
  3228. // Now, the icon...
  3229. drawRect.point.x = offset.x + mTabSize * item->mTabLevel;
  3230. // First, draw the rollover glow, if it's an inner node.
  3231. if ( item->isParent() && item->mState.test( Item::MouseOverBmp ) )
  3232. drawer->drawBitmapSR( mProfile->mTextureObject, drawRect.point, mProfile->mBitmapArrayRects[BmpGlow] );
  3233. // Now, do we draw a treeview-selected item or an item dependent one?
  3234. S32 newOffset = 0; // This is stored so we can render glow, then update render pos.
  3235. S32 bitmap = 0;
  3236. // Ok, draw the treeview lines as appropriate.
  3237. bool drawBitmap = true;
  3238. if ( !item->isParent() )
  3239. {
  3240. if( mFlags.test( ShowTreeLines ) )
  3241. {
  3242. if( ( item->mNext && item->mPrevious )
  3243. || ( item->mNext && item->mParent && ( !_isRootLevelItem( item ) || mShowRoot ) ) )
  3244. bitmap = BmpChild;
  3245. else if( item->mNext && ( !item->mParent || !mShowRoot ) )
  3246. bitmap = BmpFirstChild;
  3247. else if( item->mPrevious || ( item->mParent && !_isRootLevelItem( item ) ) )
  3248. bitmap = BmpLastChild;
  3249. else
  3250. drawBitmap = false;
  3251. }
  3252. else
  3253. drawBitmap = false;
  3254. }
  3255. else
  3256. {
  3257. bitmap = item->isExpanded() ? BmpExp : BmpCon;
  3258. if( mFlags.test( ShowTreeLines ) )
  3259. {
  3260. // Shift indices to show versions with tree lines.
  3261. if ( item->mParent || item->mPrevious )
  3262. bitmap += ( item->mNext ? 3 : 2 );
  3263. else
  3264. bitmap += ( item->mNext ? 1 : 0 );
  3265. }
  3266. }
  3267. if( ( bitmap >= 0 ) && ( bitmap < mProfile->mBitmapArrayRects.size() ) )
  3268. {
  3269. if( drawBitmap )
  3270. drawer->drawBitmapSR( mProfile->mTextureObject, drawRect.point, mProfile->mBitmapArrayRects[bitmap] );
  3271. newOffset = mProfile->mBitmapArrayRects[bitmap].extent.x;
  3272. }
  3273. if(item->isInspectorData())
  3274. {
  3275. // draw lock icon if need be
  3276. S32 icon = Lock1;
  3277. S32 icon2 = Hidden;
  3278. if (item->getObject() && item->getObject()->isLocked())
  3279. {
  3280. if (mIconTable[icon])
  3281. {
  3282. //drawRect.point.x = offset.x + mTabSize * item->mTabLevel + mIconTable[icon].getWidth();
  3283. drawRect.point.x += mIconTable[icon].getWidth();
  3284. drawer->drawBitmap( mIconTable[icon], drawRect.point );
  3285. }
  3286. }
  3287. if (item->getObject() && item->getObject()->isHidden())
  3288. {
  3289. if (mIconTable[icon2])
  3290. {
  3291. //drawRect.point.x = offset.x + mTabSize * item->mTabLevel + mIconTable[icon].getWidth();
  3292. drawRect.point.x += mIconTable[icon2].getWidth();
  3293. drawer->drawBitmap( mIconTable[icon2], drawRect.point );
  3294. }
  3295. }
  3296. SimObject * pObject = item->getObject();
  3297. SimGroup * pGroup = ( pObject == NULL ) ? NULL : dynamic_cast<SimGroup*>( pObject );
  3298. // If this item is a VirtualParent we can use the generic SimGroup123 icons.
  3299. // However if there is already an icon in the EditorIconRegistry for this
  3300. // exact class (not counting parent class icons) we want to use that instead.
  3301. bool hasClassIcon = gEditorIcons.hasIconNoRecurse( pObject );
  3302. // draw the icon associated with the item
  3303. if ( !hasClassIcon && item->mState.test(Item::VirtualParent))
  3304. {
  3305. if ( pGroup != NULL)
  3306. {
  3307. if (item->isExpanded())
  3308. item->mIcon = SimGroup1;
  3309. else
  3310. item->mIcon = SimGroup2;
  3311. }
  3312. else
  3313. item->mIcon = SimGroup2;
  3314. }
  3315. if ( !hasClassIcon && item->mState.test(Item::Marked))
  3316. {
  3317. if (item->isInspectorData())
  3318. {
  3319. if ( pGroup != NULL )
  3320. {
  3321. if (item->isExpanded())
  3322. item->mIcon = SimGroup3;
  3323. else
  3324. item->mIcon = SimGroup4;
  3325. }
  3326. }
  3327. }
  3328. GFXTexHandle iconHandle;
  3329. if ( ( item->mIcon != -1 ) && mIconTable[item->mIcon] )
  3330. iconHandle = mIconTable[item->mIcon];
  3331. #ifdef TORQUE_TOOLS
  3332. else
  3333. iconHandle = gEditorIcons.findIcon( item->getObject() );
  3334. #endif
  3335. if ( iconHandle.isValid() )
  3336. {
  3337. S32 iconHeight = (mItemHeight - iconHandle.getHeight()) / 2;
  3338. S32 oldHeight = drawRect.point.y;
  3339. if(iconHeight > 0)
  3340. drawRect.point.y += iconHeight;
  3341. drawRect.point.x += iconHandle.getWidth();
  3342. drawer->drawBitmap( iconHandle, drawRect.point );
  3343. drawRect.point.y = oldHeight;
  3344. }
  3345. }
  3346. else
  3347. {
  3348. S32 icon = item->isExpanded() ? item->mScriptInfo.mExpandedImage : item->mScriptInfo.mNormalImage;
  3349. if ( icon )
  3350. {
  3351. if (mIconTable[icon])
  3352. {
  3353. S32 iconHeight = (mItemHeight - mIconTable[icon].getHeight()) / 2;
  3354. S32 oldHeight = drawRect.point.y;
  3355. if(iconHeight > 0)
  3356. drawRect.point.y += iconHeight;
  3357. drawRect.point.x += mIconTable[icon].getWidth();
  3358. drawer->drawBitmap( mIconTable[icon], drawRect.point );
  3359. drawRect.point.y = oldHeight;
  3360. }
  3361. }
  3362. }
  3363. // Ok, update offset so we can render some text!
  3364. drawRect.point.x += newOffset;
  3365. // Ok, now we're off to rendering the actual data for the treeview item.
  3366. U32 bufLen = 1024; //item->mDataRenderWidth + 1;
  3367. char *displayText = (char *)txtBuff.alloc(bufLen);
  3368. displayText[bufLen-1] = 0;
  3369. item->getDisplayText(bufLen, displayText);
  3370. // Draw the rollover/selected bitmap, if one was specified.
  3371. drawRect.extent.x = mProfile->mFont->getStrWidth( displayText ) + ( 2 * mTextOffset );
  3372. if ( item->mState.test( Item::Selected ) && mTexSelected )
  3373. drawer->drawBitmapStretch( mTexSelected, drawRect );
  3374. else if ( item->mState.test( Item::MouseOverText ) && mTexRollover )
  3375. drawer->drawBitmapStretch( mTexRollover, drawRect );
  3376. // Offset a bit so as to space text properly.
  3377. drawRect.point.x += mTextOffset;
  3378. // Determine what color the font should be.
  3379. ColorI fontColor;
  3380. fontColor = item->mState.test( Item::Selected ) ? mProfile->mFontColorSEL :
  3381. ( item->mState.test( Item::MouseOverText ) ? mProfile->mFontColorHL : mProfile->mFontColor );
  3382. if (item->mState.test(Item::Selected))
  3383. {
  3384. drawer->drawRectFill(drawRect, mProfile->mFillColorSEL);
  3385. }
  3386. else if (item->mState.test(Item::MouseOverText))
  3387. {
  3388. drawer->drawRectFill(drawRect, mProfile->mFillColorHL);
  3389. }
  3390. if( item->mState.test(Item::MouseOverText) )
  3391. {
  3392. fontColor = mProfile->mFontColorHL;
  3393. }
  3394. drawer->setBitmapModulation( fontColor );
  3395. // Center the text horizontally.
  3396. S32 height = (mItemHeight - mProfile->mFont->getHeight()) / 2;
  3397. if(height > 0)
  3398. drawRect.point.y += height;
  3399. // JDD - offset by two pixels or so to keep the text from rendering RIGHT ONTOP of the outline
  3400. drawRect.point.x += 2;
  3401. drawer->drawText( mProfile->mFont, drawRect.point, displayText, mProfile->mFontColors );
  3402. if ( mRenamingItem == item && mRenameCtrl )
  3403. {
  3404. Point2I ctrPos = globalToLocalCoord( drawRect.point );
  3405. ctrPos.y -= height;
  3406. ctrPos.x -= 2;
  3407. Point2I ctrExtent( getWidth() - ctrPos.x, drawRect.extent.y );
  3408. mRenameCtrl->setPosition( ctrPos );
  3409. mRenameCtrl->setExtent( ctrExtent );
  3410. mRenameCtrl->setVisible( true );
  3411. }
  3412. }
  3413. //------------------------------------------------------------------------------
  3414. bool GuiTreeViewCtrl::renderTooltip( const Point2I &hoverPos, const Point2I& cursorPos, const char* tipText )
  3415. {
  3416. Item* item;
  3417. BitSet32 flags = 0;
  3418. char buf[ 2048 ];
  3419. if( _hitTest( cursorPos, item, flags ) && (!item->mTooltip.isEmpty() || mUseInspectorTooltips) )
  3420. {
  3421. bool render = true;
  3422. if( mTooltipOnWidthOnly && !item->hasObjectBasedTooltip() )
  3423. {
  3424. // Only render tooltip if the item's text is cut off with its
  3425. // parent scroll control, unless there is custom object-based
  3426. // tooltip information.
  3427. GuiScrollCtrl *pScrollParent = dynamic_cast<GuiScrollCtrl*>( getParent() );
  3428. if ( pScrollParent )
  3429. {
  3430. Point2I textStart;
  3431. Point2I textExt;
  3432. const Point2I pos = globalToLocalCoord(cursorPos);
  3433. textStart.y = pos.y / mCellSize.y;
  3434. textStart.y *= mCellSize.y;
  3435. // The following is taken from _hitTest()
  3436. textStart.x = mTabSize * item->mTabLevel;
  3437. S32 image = BmpChild;
  3438. if((image >= 0) && (image < mProfile->mBitmapArrayRects.size()))
  3439. textStart.x += mProfile->mBitmapArrayRects[image].extent.x;
  3440. textStart.x += mTextOffset;
  3441. textStart.x += getInspectorItemIconsWidth( item );
  3442. FrameAllocatorMarker txtAlloc;
  3443. U32 bufLen = item->getDisplayTextLength() + 1;
  3444. char *buf = (char*)txtAlloc.alloc(bufLen);
  3445. item->getDisplayText(bufLen, buf);
  3446. textExt.x = mProfile->mFont->getStrWidth(buf);
  3447. textExt.y = mProfile->mFont->getHeight();
  3448. if( pScrollParent->isRectCompletelyVisible(RectI(textStart, textExt)) )
  3449. render = false;
  3450. }
  3451. }
  3452. if( render )
  3453. {
  3454. if( mUseInspectorTooltips )
  3455. {
  3456. item->getTooltipText( sizeof( buf ), buf );
  3457. tipText = buf;
  3458. }
  3459. else
  3460. {
  3461. tipText = item->mTooltip.c_str();
  3462. }
  3463. }
  3464. }
  3465. return defaultTooltipRender( cursorPos, cursorPos, tipText );
  3466. }
  3467. //------------------------------------------------------------------------------
  3468. void GuiTreeViewCtrl::clearSelection()
  3469. {
  3470. if( mDebug ) Con::printf( "clearSelection called" );
  3471. while ( !mSelectedItems.empty() )
  3472. {
  3473. removeSelection( mSelectedItems.last()->mId );
  3474. }
  3475. mSelectedItems.clear();
  3476. mSelected.clear();
  3477. onClearSelection();
  3478. onClearSelection_callback();
  3479. }
  3480. //-----------------------------------------------------------------------------
  3481. void GuiTreeViewCtrl::lockSelection(bool lock)
  3482. {
  3483. for(U32 i = 0; i < mSelectedItems.size(); i++)
  3484. {
  3485. if(mSelectedItems[i]->isInspectorData())
  3486. mSelectedItems[i]->getObject()->setLocked(lock);
  3487. }
  3488. }
  3489. //-----------------------------------------------------------------------------
  3490. void GuiTreeViewCtrl::hideSelection(bool hide)
  3491. {
  3492. for(U32 i = 0; i < mSelectedItems.size(); i++)
  3493. {
  3494. if(mSelectedItems[i]->isInspectorData())
  3495. mSelectedItems[i]->getObject()->setHidden(hide);
  3496. }
  3497. }
  3498. //-----------------------------------------------------------------------------
  3499. void GuiTreeViewCtrl::toggleLockSelection()
  3500. {
  3501. for(U32 i = 0; i < mSelectedItems.size(); i++)
  3502. {
  3503. if( mSelectedItems[i]->isInspectorData() )
  3504. {
  3505. SimObject* object = mSelectedItems[ i ]->getObject();
  3506. object->setLocked( !object->isLocked() );
  3507. }
  3508. }
  3509. }
  3510. //-----------------------------------------------------------------------------
  3511. void GuiTreeViewCtrl::toggleHideSelection()
  3512. {
  3513. for(U32 i = 0; i < mSelectedItems.size(); i++)
  3514. {
  3515. if( mSelectedItems[i]->isInspectorData() )
  3516. {
  3517. SimObject* object = mSelectedItems[ i ]->getObject();
  3518. object->setHidden( !object->isHidden() );
  3519. }
  3520. }
  3521. }
  3522. //------------------------------------------------------------------------------
  3523. // handles icon assignments
  3524. S32 GuiTreeViewCtrl::getIcon(const char * iconString)
  3525. {
  3526. return -1;
  3527. }
  3528. //-----------------------------------------------------------------------------
  3529. GuiTreeViewCtrl::Item* GuiTreeViewCtrl::addInspectorDataItem(Item *parent, SimObject *obj)
  3530. {
  3531. S32 icon = getIcon(obj->getClassName());
  3532. Item *item = createItem(icon);
  3533. item->mState.set(Item::InspectorData);
  3534. // Set the item text label flags.
  3535. if( !mShowObjectIds )
  3536. item->mState.clear( Item::ShowObjectId );
  3537. else
  3538. item->mState.set( Item::ShowObjectId );
  3539. if( !mShowClassNames )
  3540. item->mState.clear( Item::ShowClassName );
  3541. else
  3542. item->mState.set( Item::ShowClassName );
  3543. if( !mShowObjectNames )
  3544. item->mState.clear( Item::ShowObjectName );
  3545. else
  3546. item->mState.set( Item::ShowObjectName );
  3547. if( !mShowInternalNames )
  3548. item->mState.clear( Item::ShowInternalName );
  3549. else
  3550. item->mState.set( Item::ShowInternalName );
  3551. if( mShowClassNameForUnnamedObjects )
  3552. item->mState.set( Item::ShowClassNameForUnnamed );
  3553. // Deal with child objects...
  3554. if(dynamic_cast<SimSet*>(obj))
  3555. item->mState.set(Item::VirtualParent);
  3556. // Actually store the data!
  3557. item->setObject(obj);
  3558. // Now add us to the data structure...
  3559. if(parent)
  3560. {
  3561. // Add as child of parent.
  3562. if(parent->mChild)
  3563. {
  3564. Item * traverse = parent->mChild;
  3565. while(traverse->mNext)
  3566. traverse = traverse->mNext;
  3567. traverse->mNext = item;
  3568. item->mPrevious = traverse;
  3569. }
  3570. else
  3571. parent->mChild = item;
  3572. item->mParent = parent;
  3573. }
  3574. else
  3575. {
  3576. // If no parent, add to root.
  3577. item->mNext = mRoot;
  3578. mRoot = item;
  3579. item->mParent = NULL;
  3580. }
  3581. mFlags.set(RebuildVisible);
  3582. return item;
  3583. }
  3584. //-----------------------------------------------------------------------------
  3585. void GuiTreeViewCtrl::unlinkItem(Item * item)
  3586. {
  3587. if (item->mPrevious)
  3588. item->mPrevious->mNext = item->mNext;
  3589. if (item->mNext)
  3590. item->mNext->mPrevious = item->mPrevious;
  3591. }
  3592. //-----------------------------------------------------------------------------
  3593. bool GuiTreeViewCtrl::childSearch(Item * item, SimObject *obj, bool yourBaby)
  3594. {
  3595. Item * temp = item->mChild;
  3596. while (temp)
  3597. {
  3598. //do you have my baby?
  3599. if (temp->isInspectorData())
  3600. {
  3601. if (temp->getObject() == obj)
  3602. yourBaby = false; //probably a child of an inner script
  3603. }
  3604. yourBaby = childSearch(temp,obj, yourBaby);
  3605. temp = temp->mNext;
  3606. }
  3607. return yourBaby;
  3608. }
  3609. //-----------------------------------------------------------------------------
  3610. void GuiTreeViewCtrl::inspectorSearch(Item * item, Item * parent, SimSet * parentSet, SimSet * newParentSet)
  3611. {
  3612. if (!parentSet||!newParentSet)
  3613. return;
  3614. if (item == parent->mNext)
  3615. return;
  3616. if (item)
  3617. {
  3618. if (item->isInspectorData())
  3619. {
  3620. // remove the object from the parentSet and add it to the newParentSet
  3621. SimObject* simObj = item->getObject();
  3622. if (parentSet->size())
  3623. {
  3624. SimObject *lastObject = parentSet->last();
  3625. parentSet->removeObject(simObj);
  3626. parentSet->reOrder(lastObject);
  3627. }
  3628. else
  3629. parentSet->removeObject(simObj);
  3630. newParentSet->addObject(simObj);
  3631. if (item->mNext)
  3632. {
  3633. inspectorSearch(item->mNext, parent, parentSet, newParentSet);
  3634. return;
  3635. }
  3636. else
  3637. {
  3638. // end of children so backing up
  3639. if (item->mParent == parent)
  3640. return;
  3641. else
  3642. {
  3643. inspectorSearch(item->mParent->mNext, parent, parentSet, newParentSet);
  3644. return;
  3645. }
  3646. }
  3647. }
  3648. if (item->mChild)
  3649. {
  3650. inspectorSearch(item->mChild, parent, parentSet, newParentSet);
  3651. return;
  3652. }
  3653. if (item->mNext)
  3654. {
  3655. inspectorSearch(item->mNext, parent, parentSet, newParentSet);
  3656. return;
  3657. }
  3658. }
  3659. }
  3660. //-----------------------------------------------------------------------------
  3661. bool GuiTreeViewCtrl::objectSearch( const SimObject *object, Item **item )
  3662. {
  3663. for ( U32 i = 0; i < mItems.size(); i++ )
  3664. {
  3665. Item *pItem = mItems[i];
  3666. if ( !pItem )
  3667. continue;
  3668. SimObject *pObj = pItem->getObject();
  3669. if ( pObj && pObj == object )
  3670. {
  3671. *item = pItem;
  3672. return true;
  3673. }
  3674. }
  3675. return false;
  3676. }
  3677. //-----------------------------------------------------------------------------
  3678. bool GuiTreeViewCtrl::onVirtualParentBuild(Item *item, bool bForceFullUpdate)
  3679. {
  3680. if(!item->mState.test(Item::InspectorData))
  3681. return true;
  3682. // Blast an item if it doesn't have a corresponding SimObject...
  3683. if(item->mInspectorInfo.mObject == NULL)
  3684. {
  3685. removeItem(item->mId);
  3686. return false;
  3687. }
  3688. // Skip the next stuff unless we're expanded...
  3689. if(!item->isExpanded() && !bForceFullUpdate && !( item == mRoot && !mShowRoot ) )
  3690. return true;
  3691. // Verify that we have all the kids we should in here...
  3692. SimSet *srcObj = dynamic_cast<SimSet*>(&(*item->mInspectorInfo.mObject));
  3693. // If it's not a SimSet... WTF are we doing here?
  3694. if(!srcObj)
  3695. return true;
  3696. // This is slow but probably ok.
  3697. for( SimSet::iterator i = srcObj->begin(); i != srcObj->end(); ++ i )
  3698. {
  3699. SimObject *obj = *i;
  3700. // If we can't find it, add it.
  3701. // unless it has a parent that is a child that is a script
  3702. Item *res = item->findChildByValue(obj);
  3703. bool foundChild = true;
  3704. // search the children. if any of them are the parent of the object then don't add it.
  3705. foundChild = childSearch(item,obj,foundChild);
  3706. if(!res && foundChild)
  3707. {
  3708. if (mDebug) Con::printf( "adding object %i to item %i", obj->getId(), item->mId );
  3709. res = addInspectorDataItem(item, obj);
  3710. }
  3711. if( res )
  3712. res->mState.set( Item::RebuildVisited );
  3713. }
  3714. // Go through our items and purge those that have disappeared from
  3715. // the set.
  3716. for( Item* ptr = item->mChild; ptr != NULL; )
  3717. {
  3718. Item* next = ptr->mNext;
  3719. if( !ptr->mState.test( Item::RebuildVisited ) )
  3720. {
  3721. if( mDebug ) Con::printf( "removing item %i for object %i that is no longer in the set",
  3722. ptr->mId, ptr->getObject()->getId() );
  3723. removeItem( ptr->mId, false );
  3724. }
  3725. else
  3726. ptr->mState.clear( Item::RebuildVisited );
  3727. ptr = next;
  3728. }
  3729. return true;
  3730. }
  3731. //-----------------------------------------------------------------------------
  3732. bool GuiTreeViewCtrl::onVirtualParentExpand(Item *item)
  3733. {
  3734. // Do nothing...
  3735. return true;
  3736. }
  3737. //-----------------------------------------------------------------------------
  3738. bool GuiTreeViewCtrl::onVirtualParentCollapse(Item *item)
  3739. {
  3740. // Do nothing...
  3741. return true;
  3742. }
  3743. //-----------------------------------------------------------------------------
  3744. void GuiTreeViewCtrl::inspectObject( SimObject* obj, bool okToEdit )
  3745. {
  3746. _destroyTree();
  3747. mFlags.set( IsEditable, okToEdit );
  3748. mFlags.set( IsInspector );
  3749. onDefineIcons_callback();
  3750. addInspectorDataItem( NULL, obj );
  3751. }
  3752. //-----------------------------------------------------------------------------
  3753. S32 GuiTreeViewCtrl::findItemByName(const char *name)
  3754. {
  3755. for (S32 i = 0; i < mItems.size(); i++)
  3756. if (mItems[i] && dStrcmp(mItems[i]->getText(),name) == 0)
  3757. return mItems[i]->mId;
  3758. return 0;
  3759. }
  3760. //-----------------------------------------------------------------------------
  3761. S32 GuiTreeViewCtrl::findItemByValue(const char *name)
  3762. {
  3763. for (S32 i = 0; i < mItems.size(); i++)
  3764. if (mItems[i] && dStrcmp(mItems[i]->getValue(),name) == 0)
  3765. return mItems[i]->mId;
  3766. return 0;
  3767. }
  3768. //-----------------------------------------------------------------------------
  3769. void GuiTreeViewCtrl::sortTree( bool caseSensitive, bool traverseHierarchy, bool parentsFirst )
  3770. {
  3771. itemSortList( mRoot, caseSensitive, traverseHierarchy, parentsFirst );
  3772. }
  3773. //-----------------------------------------------------------------------------
  3774. StringTableEntry GuiTreeViewCtrl::getTextToRoot( S32 itemId, const char * delimiter )
  3775. {
  3776. Item * item = getItem(itemId);
  3777. if(!item)
  3778. {
  3779. Con::errorf(ConsoleLogEntry::General, "GuiTreeViewCtrl::getTextToRoot: invalid start item id!");
  3780. return StringTable->insert("");
  3781. }
  3782. if(item->isInspectorData())
  3783. {
  3784. Con::errorf(ConsoleLogEntry::General, "GuiTreeViewCtrl::getTextToRoot: cannot get text to root of inspector data items");
  3785. return StringTable->insert("");
  3786. }
  3787. char bufferOne[1024];
  3788. char bufferTwo[1024];
  3789. char bufferNodeText[128];
  3790. dMemset( bufferOne, 0, sizeof(bufferOne) );
  3791. dMemset( bufferTwo, 0, sizeof(bufferTwo) );
  3792. dStrcpy( bufferOne, item->getText() );
  3793. Item *prevNode = item->mParent;
  3794. while ( prevNode )
  3795. {
  3796. dMemset( bufferNodeText, 0, sizeof(bufferNodeText) );
  3797. dStrcpy( bufferNodeText, prevNode->getText() );
  3798. dSprintf( bufferTwo, 1024, "%s%s%s",bufferNodeText, delimiter, bufferOne );
  3799. dStrcpy( bufferOne, bufferTwo );
  3800. dMemset( bufferTwo, 0, sizeof(bufferTwo) );
  3801. prevNode = prevNode->mParent;
  3802. }
  3803. // Return the result, StringTable-ized.
  3804. return StringTable->insert( bufferOne, true );
  3805. }
  3806. //-----------------------------------------------------------------------------
  3807. void GuiTreeViewCtrl::setFilterText( const String& text )
  3808. {
  3809. mFilterText = text;
  3810. // Trigger rebuild.
  3811. mFlags.set( RebuildVisible );
  3812. }
  3813. //=============================================================================
  3814. // Console Methods.
  3815. //=============================================================================
  3816. // MARK: ---- Console Methods ----
  3817. //-----------------------------------------------------------------------------
  3818. DefineEngineMethod( GuiTreeViewCtrl, findItemByName, S32, ( const char* text ),,
  3819. "Get the ID of the item whose text matches the given @a text.\n\n"
  3820. "@param text Item text to match.\n"
  3821. "@return ID of the item or -1 if no item matches the given text." )
  3822. {
  3823. return object->findItemByName( text );
  3824. }
  3825. //-----------------------------------------------------------------------------
  3826. DefineEngineMethod( GuiTreeViewCtrl, findItemByValue, S32, ( const char* value ),,
  3827. "Get the ID of the item whose value matches @a value.\n\n"
  3828. "@param value Value text to match.\n"
  3829. "@return ID of the item or -1 if no item has the given value." )
  3830. {
  3831. return object->findItemByValue( value );
  3832. }
  3833. //-----------------------------------------------------------------------------
  3834. DefineEngineMethod( GuiTreeViewCtrl, findChildItemByName, S32, ( S32 parentId, const char* childName ),,
  3835. "Get the child item of the given parent item whose text matches @a childName.\n\n"
  3836. "@param parentId Item ID of the parent in which to look for the child.\n"
  3837. "@param childName Text of the child item to find.\n"
  3838. "@return ID of the child item or -1 if no child in @a parentId has the given text @a childName.\n\n"
  3839. "@note This method does not recurse, i.e. it only looks for direct children." )
  3840. {
  3841. if( parentId == 0 )
  3842. {
  3843. if( !object->getRootItem() )
  3844. return 0;
  3845. GuiTreeViewCtrl::Item* root = object->getRootItem();
  3846. while( root )
  3847. {
  3848. if( dStricmp( root->getText(), childName ) == 0 )
  3849. return root->getID();
  3850. root = root->mNext;
  3851. }
  3852. return 0;
  3853. }
  3854. else
  3855. {
  3856. GuiTreeViewCtrl::Item* item = object->getItem( parentId );
  3857. if( !item )
  3858. {
  3859. Con::errorf( "GuiTreeViewCtrl.findChildItemByName - invalid parent ID '%i'", parentId );
  3860. return 0;
  3861. }
  3862. GuiTreeViewCtrl::Item* child = item->findChildByName( childName );
  3863. if( !child )
  3864. return 0;
  3865. return child->mId;
  3866. }
  3867. }
  3868. //-----------------------------------------------------------------------------
  3869. DefineEngineMethod( GuiTreeViewCtrl, insertItem, S32, ( S32 parentId, const char* text, const char* value, const char* icon, S32 normalImage, S32 expandedImage ), ( "", "", 0, 0 ),
  3870. "Add a new item to the tree.\n\n"
  3871. "@param parentId Item ID of parent to which to add the item as a child. 0 is root item.\n"
  3872. "@param text Text to display on the item in the tree.\n"
  3873. "@param value Behind-the-scenes value of the item.\n"
  3874. "@param icon\n"
  3875. "@param normalImage\n"
  3876. "@param expandedImage\n"
  3877. "@return The ID of the newly added item." )
  3878. {
  3879. return object->insertItem( parentId, text, value, icon, normalImage, expandedImage );
  3880. }
  3881. //-----------------------------------------------------------------------------
  3882. DefineEngineMethod( GuiTreeViewCtrl, lockSelection, void, ( bool lock ), ( true ),
  3883. "Set whether the current selection can be changed by the user or not.\n\n"
  3884. "@param lock If true, the current selection is frozen and cannot be changed. If false, "
  3885. "the selection may be modified." )
  3886. {
  3887. object->lockSelection( lock );
  3888. }
  3889. //-----------------------------------------------------------------------------
  3890. DefineEngineMethod( GuiTreeViewCtrl, hideSelection, void, ( bool state ), ( true ),
  3891. "Call SimObject::setHidden( @a state ) on all objects in the current selection.\n\n"
  3892. "@param state Visibility state to set objects in selection to." )
  3893. {
  3894. object->hideSelection( state );
  3895. }
  3896. //-----------------------------------------------------------------------------
  3897. DefineEngineMethod( GuiTreeViewCtrl, toggleLockSelection, void, (),,
  3898. "Toggle the locked state of all objects in the current selection." )
  3899. {
  3900. object->toggleLockSelection();
  3901. }
  3902. //-----------------------------------------------------------------------------
  3903. DefineEngineMethod( GuiTreeViewCtrl, toggleHideSelection, void, (),,
  3904. "Toggle the hidden state of all objects in the current selection." )
  3905. {
  3906. object->toggleHideSelection();
  3907. }
  3908. //-----------------------------------------------------------------------------
  3909. DefineEngineMethod( GuiTreeViewCtrl, clearSelection, void, (),,
  3910. "Unselect all currently selected items." )
  3911. {
  3912. object->clearSelection();
  3913. }
  3914. //-----------------------------------------------------------------------------
  3915. DefineEngineMethod( GuiTreeViewCtrl, deleteSelection, void, (),,
  3916. "Delete all items/objects in the current selection." )
  3917. {
  3918. object->deleteSelection();
  3919. }
  3920. //-----------------------------------------------------------------------------
  3921. DefineEngineMethod( GuiTreeViewCtrl, addSelection, void, ( S32 id, bool isLastSelection ), ( true ),
  3922. "Add an item/object to the current selection.\n\n"
  3923. "@param id ID of item/object to add to the selection.\n"
  3924. "@param isLastSelection Whether there are more pending items/objects to be added to the selection. If false, "
  3925. "the control will defer refreshing the tree and wait until addSelection() is called with this parameter set "
  3926. "to true." )
  3927. {
  3928. object->addSelection( id, isLastSelection, isLastSelection );
  3929. }
  3930. ConsoleMethod(GuiTreeViewCtrl, addChildSelectionByValue, void, 4, 4, "addChildSelectionByValue(TreeItemId parent, value)")
  3931. {
  3932. S32 id = dAtoi(argv[2]);
  3933. GuiTreeViewCtrl::Item* parentItem = object->getItem(id);
  3934. GuiTreeViewCtrl::Item* child = parentItem->findChildByValue(argv[3]);
  3935. object->addSelection(child->getID());
  3936. }
  3937. ConsoleMethod(GuiTreeViewCtrl, removeSelection, void, 3, 3, "(deselects an item)")
  3938. {
  3939. S32 id = dAtoi(argv[2]);
  3940. object->removeSelection(id);
  3941. }
  3942. ConsoleMethod(GuiTreeViewCtrl, removeChildSelectionByValue, void, 4, 4, "removeChildSelectionByValue(TreeItemId parent, value)")
  3943. {
  3944. S32 id = dAtoi(argv[2]);
  3945. GuiTreeViewCtrl::Item* parentItem = object->getItem(id);
  3946. if(parentItem)
  3947. {
  3948. GuiTreeViewCtrl::Item* child = parentItem->findChildByValue(argv[3]);
  3949. if(child)
  3950. {
  3951. object->removeSelection(child->getID());
  3952. }
  3953. }
  3954. }
  3955. ConsoleMethod(GuiTreeViewCtrl, selectItem, bool, 3, 4, "(TreeItemId item, bool select=true)")
  3956. {
  3957. S32 id = dAtoi(argv[2]);
  3958. bool select = true;
  3959. if(argc == 4)
  3960. select = dAtob(argv[3]);
  3961. return(object->setItemSelected(id, select));
  3962. }
  3963. ConsoleMethod(GuiTreeViewCtrl, expandItem, bool, 3, 4, "(TreeItemId item, bool expand=true)")
  3964. {
  3965. S32 id = dAtoi(argv[2]);
  3966. bool expand = true;
  3967. if(argc == 4)
  3968. expand = dAtob(argv[3]);
  3969. return(object->setItemExpanded(id, expand));
  3970. }
  3971. ConsoleMethod(GuiTreeViewCtrl, markItem, bool, 3, 4, "(TreeItemId item, bool mark=true)")
  3972. {
  3973. S32 id = dAtoi(argv[2]);
  3974. bool mark = true;
  3975. if(argc == 4)
  3976. mark = dAtob(argv[3]);
  3977. return object->markItem(id, mark);
  3978. }
  3979. // Make the given item visible.
  3980. ConsoleMethod(GuiTreeViewCtrl, scrollVisible, void, 3, 3, "(TreeItemId item)")
  3981. {
  3982. object->scrollVisible(dAtoi(argv[2]));
  3983. }
  3984. ConsoleMethod(GuiTreeViewCtrl, buildIconTable, bool, 3,3, "(builds an icon table)")
  3985. {
  3986. const char * icons = argv[2];
  3987. return object->buildIconTable(icons);
  3988. }
  3989. ConsoleMethod( GuiTreeViewCtrl, open, void, 3, 4, "(SimSet obj, bool okToEdit=true) Set the root of the tree view to the specified object, or to the root set.")
  3990. {
  3991. SimSet *treeRoot = NULL;
  3992. SimObject* target = Sim::findObject(argv[2]);
  3993. bool okToEdit = true;
  3994. if (argc == 4)
  3995. okToEdit = dAtob(argv[3]);
  3996. if (target)
  3997. treeRoot = dynamic_cast<SimSet*>(target);
  3998. if (! treeRoot)
  3999. Sim::findObject(RootGroupId, treeRoot);
  4000. object->inspectObject(treeRoot,okToEdit);
  4001. }
  4002. ConsoleMethod( GuiTreeViewCtrl, setItemTooltip, void, 4, 4, "( int id, string text ) - Set the tooltip to show for the given item." )
  4003. {
  4004. int id = dAtoi( argv[ 2 ] );
  4005. GuiTreeViewCtrl::Item* item = object->getItem( id );
  4006. if( !item )
  4007. {
  4008. Con::errorf( "GuiTreeViewCtrl::setTooltip() - invalid item id '%i'", id );
  4009. return;
  4010. }
  4011. item->mTooltip = argv[ 3 ];
  4012. }
  4013. ConsoleMethod( GuiTreeViewCtrl, setItemImages, void, 5, 5, "( int id, int normalImage, int expandedImage ) - Sets the normal and expanded images to show for the given item." )
  4014. {
  4015. int id = dAtoi( argv[ 2 ] );
  4016. GuiTreeViewCtrl::Item* item = object->getItem( id );
  4017. if( !item )
  4018. {
  4019. Con::errorf( "GuiTreeViewCtrl::setItemImages() - invalid item id '%i'", id );
  4020. return;
  4021. }
  4022. item->setNormalImage((S8)dAtoi(argv[3]));
  4023. item->setExpandedImage((S8)dAtoi(argv[4]));
  4024. }
  4025. ConsoleMethod( GuiTreeViewCtrl, isParentItem, bool, 3, 3, "( int id ) - Returns true if the given item contains child items." )
  4026. {
  4027. int id = dAtoi( argv[ 2 ] );
  4028. if( !id && object->getItemCount() )
  4029. return true;
  4030. GuiTreeViewCtrl::Item* item = object->getItem( id );
  4031. if( !item )
  4032. {
  4033. Con::errorf( "GuiTreeViewCtrl::isParentItem - invalid item id '%i'", id );
  4034. return false;
  4035. }
  4036. return item->isParent();
  4037. }
  4038. ConsoleMethod(GuiTreeViewCtrl, getItemText, const char *, 3, 3, "(TreeItemId item)")
  4039. {
  4040. return(object->getItemText(dAtoi(argv[2])));
  4041. }
  4042. ConsoleMethod(GuiTreeViewCtrl, getItemValue, const char *, 3, 3, "(TreeItemId item)")
  4043. {
  4044. return(object->getItemValue(dAtoi(argv[2])));
  4045. }
  4046. ConsoleMethod(GuiTreeViewCtrl, editItem, bool, 5, 5, "(TreeItemId item, string newText, string newValue)")
  4047. {
  4048. return(object->editItem(dAtoi(argv[2]), argv[3], argv[4]));
  4049. }
  4050. ConsoleMethod(GuiTreeViewCtrl, removeItem, bool, 3, 3, "(TreeItemId item)")
  4051. {
  4052. return(object->removeItem(dAtoi(argv[2])));
  4053. }
  4054. ConsoleMethod(GuiTreeViewCtrl, removeAllChildren, void, 3, 3, "removeAllChildren(TreeItemId parent)")
  4055. {
  4056. object->removeAllChildren(dAtoi(argv[2]));
  4057. }
  4058. ConsoleMethod(GuiTreeViewCtrl, clear, void, 2, 2, "() - empty tree")
  4059. {
  4060. object->removeItem(0);
  4061. }
  4062. ConsoleMethod(GuiTreeViewCtrl, getFirstRootItem, S32, 2, 2, "Get id for root item.")
  4063. {
  4064. return(object->getFirstRootItem());
  4065. }
  4066. ConsoleMethod(GuiTreeViewCtrl, getChild, S32, 3, 3, "(TreeItemId item)")
  4067. {
  4068. return(object->getChildItem(dAtoi(argv[2])));
  4069. }
  4070. ConsoleMethod(GuiTreeViewCtrl, buildVisibleTree, void, 2, 3, "Build the visible tree")
  4071. {
  4072. bool forceFullUpdate = false;
  4073. if( argc > 2 )
  4074. forceFullUpdate = dAtob( argv[ 2 ] );
  4075. object->buildVisibleTree( forceFullUpdate );
  4076. }
  4077. //FIXME: [rene 11/09/09 - This clashes with GuiControl.getParent(); bad thing; should be getParentItem]
  4078. ConsoleMethod(GuiTreeViewCtrl, getParent, S32, 3, 3, "(TreeItemId item)")
  4079. {
  4080. return(object->getParentItem(dAtoi(argv[2])));
  4081. }
  4082. ConsoleMethod(GuiTreeViewCtrl, getNextSibling, S32, 3, 3, "(TreeItemId item)")
  4083. {
  4084. return(object->getNextSiblingItem(dAtoi(argv[2])));
  4085. }
  4086. ConsoleMethod(GuiTreeViewCtrl, getPrevSibling, S32, 3, 3, "(TreeItemId item)")
  4087. {
  4088. return(object->getPrevSiblingItem(dAtoi(argv[2])));
  4089. }
  4090. ConsoleMethod(GuiTreeViewCtrl, getItemCount, S32, 2, 2, "")
  4091. {
  4092. return(object->getItemCount());
  4093. }
  4094. ConsoleMethod(GuiTreeViewCtrl, getSelectedItem, S32, 2, 3, "( int index=0 ) - Return the selected item at the given index.")
  4095. {
  4096. S32 index = 0;
  4097. if( argc > 2 )
  4098. index = dAtoi( argv[ 2 ] );
  4099. return ( object->getSelectedItem( index ) );
  4100. }
  4101. ConsoleMethod(GuiTreeViewCtrl, getSelectedObject, S32, 2, 3, "( int index=0 ) - Return the currently selected SimObject at the given index in inspector mode or -1")
  4102. {
  4103. S32 index = 0;
  4104. if( argc > 2 )
  4105. index = dAtoi( argv[ 2 ] );
  4106. GuiTreeViewCtrl::Item *item = object->getItem( object->getSelectedItem( index ) );
  4107. if( item != NULL && item->isInspectorData() )
  4108. {
  4109. SimObject *obj = item->getObject();
  4110. if( obj != NULL )
  4111. return obj->getId();
  4112. }
  4113. return -1;
  4114. }
  4115. ConsoleMethod(GuiTreeViewCtrl, getSelectedObjectList, const char*, 2, 2,
  4116. "Returns a space sperated list of all selected object ids.")
  4117. {
  4118. char* buff = Con::getReturnBuffer(1024);
  4119. dSprintf(buff,1024,"");
  4120. const Vector< GuiTreeViewCtrl::Item* > selectedItems = object->getSelectedItems();
  4121. for(int i = 0; i < selectedItems.size(); i++)
  4122. {
  4123. GuiTreeViewCtrl::Item *item = selectedItems[i];
  4124. if ( item->isInspectorData() && item->getObject() )
  4125. {
  4126. S32 id = item->getObject()->getId();
  4127. //get the current length of the buffer
  4128. U32 len = dStrlen(buff);
  4129. //the start of the buffer where we want to write
  4130. char* buffPart = buff+len;
  4131. //the size of the remaining buffer (-1 cause dStrlen doesn't count the \0)
  4132. S32 size = 1024-len-1;
  4133. //write it:
  4134. if(size < 1)
  4135. {
  4136. Con::errorf("GuiTreeViewCtrl::getSelectedItemList - Not enough room to return our object list");
  4137. return buff;
  4138. }
  4139. dSprintf(buffPart,size,"%d ", id);
  4140. }
  4141. }
  4142. return buff;
  4143. }
  4144. ConsoleMethod(GuiTreeViewCtrl, moveItemUp, void, 3, 3, "(TreeItemId item)")
  4145. {
  4146. object->moveItemUp( dAtoi( argv[2] ) );
  4147. }
  4148. ConsoleMethod(GuiTreeViewCtrl, getSelectedItemsCount, S32, 2, 2, "")
  4149. {
  4150. return ( object->getSelectedItemsCount() );
  4151. }
  4152. ConsoleMethod(GuiTreeViewCtrl, moveItemDown, void, 3, 3, "(TreeItemId item)")
  4153. {
  4154. object->moveItemDown( dAtoi( argv[2] ) );
  4155. }
  4156. //-----------------------------------------------------------------------------
  4157. ConsoleMethod(GuiTreeViewCtrl, getTextToRoot, const char*,4,4,"(TreeItemId item,Delimiter=none) gets the text from the current node to the root, concatenating at each branch upward, with a specified delimiter optionally")
  4158. {
  4159. if ( argc < 4 )
  4160. {
  4161. Con::warnf("GuiTreeViewCtrl::getTextToRoot - Invalid number of arguments!");
  4162. return ("");
  4163. }
  4164. S32 itemId = dAtoi( argv[2] );
  4165. StringTableEntry delimiter = argv[3];
  4166. return object->getTextToRoot( itemId, delimiter );
  4167. }
  4168. ConsoleMethod(GuiTreeViewCtrl, getSelectedItemList,const char*, 2,2,"returns a space seperated list of mulitple item ids")
  4169. {
  4170. char* buff = Con::getReturnBuffer(1024);
  4171. dSprintf(buff,1024,"");
  4172. const Vector< S32 >& selected = object->getSelected();
  4173. for(int i = 0; i < selected.size(); i++)
  4174. {
  4175. S32 id = selected[i];
  4176. //get the current length of the buffer
  4177. U32 len = dStrlen(buff);
  4178. //the start of the buffer where we want to write
  4179. char* buffPart = buff+len;
  4180. //the size of the remaining buffer (-1 cause dStrlen doesn't count the \0)
  4181. S32 size = 1024-len-1;
  4182. //write it:
  4183. if(size < 1)
  4184. {
  4185. Con::errorf("GuiTreeViewCtrl::getSelectedItemList - Not enough room to return our object list");
  4186. return buff;
  4187. }
  4188. dSprintf(buffPart,size,"%d ", id);
  4189. }
  4190. //mSelected
  4191. return buff;
  4192. }
  4193. S32 GuiTreeViewCtrl::findItemByObjectId(S32 iObjId)
  4194. {
  4195. for (S32 i = 0; i < mItems.size(); i++)
  4196. {
  4197. if ( !mItems[i] )
  4198. continue;
  4199. SimObject* pObj = mItems[i]->getObject();
  4200. if( pObj && pObj->getId() == iObjId )
  4201. return mItems[i]->mId;
  4202. }
  4203. return -1;
  4204. }
  4205. //------------------------------------------------------------------------------
  4206. ConsoleMethod(GuiTreeViewCtrl, findItemByObjectId, S32, 3, 3, "(find item by object id and returns the mId)")
  4207. {
  4208. return(object->findItemByObjectId(dAtoi(argv[2])));
  4209. }
  4210. //------------------------------------------------------------------------------
  4211. bool GuiTreeViewCtrl::scrollVisibleByObjectId(S32 objID)
  4212. {
  4213. S32 itemID = findItemByObjectId(objID);
  4214. if(itemID == -1)
  4215. {
  4216. // we did not find the item in our current items
  4217. // we should try to find and show the parent of the item.
  4218. SimObject *obj = Sim::findObject(objID);
  4219. if(!obj || !obj->getGroup())
  4220. return false;
  4221. // if we can't show the parent, we fail.
  4222. if(! scrollVisibleByObjectId(obj->getGroup()->getId()) )
  4223. return false;
  4224. // get the parent. expand the parent. rebuild the tree. this ensures that
  4225. // we'll be able to find the child item we're targeting.
  4226. S32 parentID = findItemByObjectId(obj->getGroup()->getId());
  4227. AssertFatal(parentID != -1, "We were able to show the parent, but could not then find the parent. This should not happen.");
  4228. Item *parentItem = getItem(parentID);
  4229. parentItem->setExpanded(true);
  4230. buildVisibleTree();
  4231. // NOW we should be able to find the object. if not... something's wrong.
  4232. itemID = findItemByObjectId(objID);
  4233. AssertWarn(itemID != -1,"GuiTreeViewCtrl::scrollVisibleByObjectId() found the parent, but can't find it's immediate child. This should not happen.");
  4234. if(itemID == -1)
  4235. return false;
  4236. }
  4237. // ok, item found. scroll to it.
  4238. mFlags.set( RebuildVisible );
  4239. scrollVisible(itemID);
  4240. return true;
  4241. }
  4242. //------------------------------------------------------------------------------
  4243. ConsoleMethod(GuiTreeViewCtrl, scrollVisibleByObjectId, S32, 3, 3, "(show item by object id. returns true if sucessful.)")
  4244. {
  4245. return(object->scrollVisibleByObjectId(dAtoi(argv[2])));
  4246. }
  4247. //------------------------------------------------------------------------------
  4248. //FIXME: this clashes with SimSet.sort()
  4249. ConsoleMethod( GuiTreeViewCtrl, sort, void, 2, 6, "( int parent, bool traverseHierarchy=false, bool parentsFirst=false, bool caseSensitive=true ) - Sorts all items of the given parent (or root). With 'hierarchy', traverses hierarchy." )
  4250. {
  4251. S32 parent = 0;
  4252. if( argc >= 3 )
  4253. parent = dAtoi( argv[ 2 ] );
  4254. bool traverseHierarchy = false;
  4255. bool parentsFirst = false;
  4256. bool caseSensitive = true;
  4257. if( argc >= 4 )
  4258. traverseHierarchy = dAtob( argv[ 3 ] );
  4259. if( argc >= 5 )
  4260. parentsFirst = dAtob( argv[ 4 ] );
  4261. if( argc >= 6 )
  4262. caseSensitive = dAtob( argv[ 5 ] );
  4263. if( !parent )
  4264. object->sortTree( caseSensitive, traverseHierarchy, parentsFirst );
  4265. else
  4266. {
  4267. GuiTreeViewCtrl::Item* item = object->getItem( parent );
  4268. if( !item )
  4269. {
  4270. Con::errorf( "GuiTreeViewCtrl::sort - no item '%i' in tree", parent );
  4271. return;
  4272. }
  4273. item->sort( caseSensitive, traverseHierarchy, parentsFirst );
  4274. }
  4275. }
  4276. void GuiTreeViewCtrl::cancelRename()
  4277. {
  4278. if ( !mRenamingItem || !mRenameCtrl )
  4279. return;
  4280. mRenamingItem = NULL;
  4281. if ( mRenameCtrl )
  4282. mRenameCtrl->clearFirstResponder();
  4283. }
  4284. void GuiTreeViewCtrl::onRenameValidate()
  4285. {
  4286. if ( !mRenamingItem || !mRenameCtrl )
  4287. return;
  4288. char data[ GuiTextCtrl::MAX_STRING_LENGTH+1 ];
  4289. mRenameCtrl->getText( data );
  4290. SimObject *obj = mRenamingItem->getObject();
  4291. mRenamingItem = NULL;
  4292. // Object could have been deleted in the interum.
  4293. if ( !obj )
  4294. return;
  4295. if( isMethod( "handleRenameObject" ) && handleRenameObject_callback( data, obj ) )
  4296. return;
  4297. if ( mRenameInternal )
  4298. obj->setInternalName( data );
  4299. else if ( validateObjectName( data, obj ) )
  4300. obj->assignName( data );
  4301. }
  4302. void GuiTreeViewCtrl::showItemRenameCtrl( Item* item )
  4303. {
  4304. SimObject *renameObj = item->getObject();
  4305. mRenamingItem = item;
  4306. if ( !mRenameCtrl )
  4307. {
  4308. mRenameCtrl = new GuiTextEditCtrl;
  4309. mRenameCtrl->registerObject();
  4310. addObject( mRenameCtrl );
  4311. if ( mRenameInternal )
  4312. mRenameCtrl->setText( renameObj->getInternalName() );
  4313. else
  4314. mRenameCtrl->setText( renameObj->getName() );
  4315. mRenameCtrl->setFirstResponder();
  4316. mRenameCtrl->setSinkAllKeys(true);
  4317. mRenameCtrl->selectAllText();
  4318. mRenameCtrl->setCursorPos(0);
  4319. char cmd[256];
  4320. dSprintf( cmd, 256, "%i.onRenameValidate();", getId() );
  4321. mRenameCtrl->setField( "validate", cmd );
  4322. dSprintf( cmd, 256, "%i.cancelRename();", getId() );
  4323. mRenameCtrl->setField( "escapeCommand", cmd );
  4324. }
  4325. }
  4326. ConsoleMethod( GuiTreeViewCtrl, cancelRename, void, 2, 2, "For internal use." )
  4327. {
  4328. object->cancelRename();
  4329. }
  4330. ConsoleMethod( GuiTreeViewCtrl, onRenameValidate, void, 2, 2, "For internal use." )
  4331. {
  4332. object->onRenameValidate();
  4333. }
  4334. ConsoleMethod( GuiTreeViewCtrl, showItemRenameCtrl, void, 3, 3, "( TreeItemId id ) - Show the rename text field for the given item (only one at a time)." )
  4335. {
  4336. S32 id = dAtoi( argv[ 2 ] );
  4337. GuiTreeViewCtrl::Item* item = object->getItem( id );
  4338. if( !item )
  4339. {
  4340. Con::errorf( "GuiTreeViewCtrl::showItemRenameCtrl - invalid item id '%i'", id );
  4341. return;
  4342. }
  4343. object->showItemRenameCtrl( item );
  4344. }
  4345. ConsoleMethod( GuiTreeViewCtrl, setDebug, void, 2, 3, "( bool value=true ) - Enable/disable debug output." )
  4346. {
  4347. bool value = true;
  4348. if( argc > 2 )
  4349. value = dAtob( argv[ 2 ] );
  4350. object->setDebug( value );
  4351. }
  4352. //-----------------------------------------------------------------------------
  4353. DefineEngineMethod( GuiTreeViewCtrl, isItemSelected, bool, ( S32 id ),,
  4354. "Check whether the given item is currently selected in the tree.\n\n"
  4355. "@param id Item/object ID.\n"
  4356. "@return True if the given item/object is currently selected in the tree." )
  4357. {
  4358. const Vector< GuiTreeViewCtrl::Item* >& selectedItems = object->getSelectedItems();
  4359. for( S32 i = 0; i < selectedItems.size(); ++ i )
  4360. if( selectedItems[ i ]->mId == id )
  4361. return true;
  4362. return false;
  4363. }
  4364. //-----------------------------------------------------------------------------
  4365. DefineEngineMethod( GuiTreeViewCtrl, getFilterText, const char*, (),,
  4366. "Get the current filter expression. Only tree items whose text matches this expression "
  4367. "are displayed. By default, the expression is empty and all items are shown.\n\n"
  4368. "@return The current filter pattern or an empty string if no filter pattern is currently active.\n\n"
  4369. "@see setFilterText\n"
  4370. "@see clearFilterText" )
  4371. {
  4372. return object->getFilterText();
  4373. }
  4374. //-----------------------------------------------------------------------------
  4375. DefineEngineMethod( GuiTreeViewCtrl, setFilterText, void, ( const char* pattern ),,
  4376. "Set the pattern by which to filter items in the tree. Only items in the tree whose text "
  4377. "matches this pattern are displayed.\n\n"
  4378. "@param pattern New pattern based on which visible items in the tree should be filtered. If empty, all items become visible.\n\n"
  4379. "@see getFilterText\n"
  4380. "@see clearFilterText" )
  4381. {
  4382. object->setFilterText( pattern );
  4383. }
  4384. //-----------------------------------------------------------------------------
  4385. DefineEngineMethod( GuiTreeViewCtrl, clearFilterText, void, (),,
  4386. "Clear the current item filtering pattern.\n\n"
  4387. "@see setFilterText\n"
  4388. "@see getFilterText" )
  4389. {
  4390. object->clearFilterText();
  4391. }