guiTreeViewCtrl.cc 117 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 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 "memory/frameAllocator.h"
  23. #include "gui/guiTreeViewCtrl.h"
  24. #include "gui/containers/guiScrollCtrl.h"
  25. #include "console/consoleTypes.h"
  26. #include "console/console.h"
  27. #include "graphics/dgl.h"
  28. #include "gui/guiTypes.h"
  29. #include "platform/event.h"
  30. IMPLEMENT_CONOBJECT(GuiTreeViewCtrl);
  31. //-----------------------------------------------------------------------------
  32. // TreeView Item
  33. //-----------------------------------------------------------------------------
  34. GuiTreeViewCtrl::Item::Item( GuiControlProfile *pProfile )
  35. {
  36. AssertFatal( pProfile != NULL , "Cannot create a tree item without a valid tree and control profile!");
  37. mState = 0;
  38. mId = -1;
  39. mTabLevel = 0;
  40. mIcon = 0;
  41. mDataRenderWidth = 0;
  42. mScriptInfo.mText = NULL;
  43. mScriptInfo.mValue = NULL;
  44. mInspectorInfo.mObject = NULL;
  45. mParent = NULL;
  46. mChild = NULL;
  47. mNext = NULL;
  48. mPrevious = NULL;
  49. mProfile = pProfile;
  50. }
  51. GuiTreeViewCtrl::Item::~Item()
  52. {
  53. if( ! mState.test(InspectorData) )
  54. {
  55. if ( getText() )
  56. {
  57. delete [] getText();
  58. setText(NULL);
  59. }
  60. if ( getValue() )
  61. {
  62. delete [] getValue();
  63. setValue( NULL );
  64. }
  65. }
  66. }
  67. void GuiTreeViewCtrl::Item::setNormalImage(S8 id)
  68. {
  69. if(mState.test(InspectorData))
  70. {
  71. Con::errorf("Tried to set normal image %d for item %d, which is InspectorData!", id, mId);
  72. return;
  73. }
  74. mScriptInfo.mNormalImage = id;
  75. }
  76. void GuiTreeViewCtrl::Item::setExpandedImage(S8 id)
  77. {
  78. if(mState.test(InspectorData))
  79. {
  80. Con::errorf("Tried to set expanded image %d for item %d, which is InspectorData!", id, mId);
  81. return;
  82. }
  83. mScriptInfo.mExpandedImage = id;
  84. }
  85. void GuiTreeViewCtrl::Item::setText(char *txt)
  86. {
  87. if(mState.test(InspectorData))
  88. {
  89. Con::errorf("Tried to set text for item %d, which is InspectorData!", mId);
  90. return;
  91. }
  92. mScriptInfo.mText = txt;
  93. // Update Render Data
  94. if( !mProfile.isNull() )
  95. mDataRenderWidth = getDisplayTextWidth( mProfile->getFont() );
  96. }
  97. void GuiTreeViewCtrl::Item::setValue(const char *val)
  98. {
  99. if(mState.test(InspectorData))
  100. {
  101. Con::errorf("Tried to set value for item %d, which is InspectorData!", mId);
  102. return;
  103. }
  104. mScriptInfo.mValue = const_cast<char*>(val); // mValue really ought to be a StringTableEntry
  105. // Update Render Data
  106. if( !mProfile.isNull() )
  107. mDataRenderWidth = getDisplayTextWidth( mProfile->getFont());
  108. }
  109. const S8 GuiTreeViewCtrl::Item::getNormalImage() const
  110. {
  111. if(mState.test(InspectorData))
  112. {
  113. Con::errorf("Tried to get the normal image for item %d, which is InspectorData!", mId);
  114. return 0; // fail safe for width determinations
  115. }
  116. return mScriptInfo.mNormalImage;
  117. }
  118. const S8 GuiTreeViewCtrl::Item::getExpandedImage() const
  119. {
  120. if(mState.test(InspectorData))
  121. {
  122. Con::errorf("Tried to get the expanded image for item %d, which is InspectorData!", mId);
  123. return 0; // fail safe for width determinations
  124. }
  125. return mScriptInfo.mExpandedImage;
  126. }
  127. char *GuiTreeViewCtrl::Item::getText()
  128. {
  129. if(mState.test(InspectorData))
  130. {
  131. Con::errorf("Tried to get the text for item %d, which is InspectorData!", mId);
  132. return NULL;
  133. }
  134. return mScriptInfo.mText;
  135. }
  136. char *GuiTreeViewCtrl::Item::getValue()
  137. {
  138. if(mState.test(InspectorData))
  139. {
  140. Con::errorf("Tried to get the value for item %d, which is InspectorData!", mId);
  141. return NULL;
  142. }
  143. return mScriptInfo.mValue;
  144. }
  145. void GuiTreeViewCtrl::Item::setObject(SimObject *obj)
  146. {
  147. if(!mState.test(InspectorData))
  148. {
  149. Con::errorf("Tried to set the object for item %d, which is not InspectorData!", mId);
  150. return;
  151. }
  152. mInspectorInfo.mObject = obj;
  153. // Update Render Data
  154. if( !mProfile.isNull() )
  155. mDataRenderWidth = getDisplayTextWidth( mProfile->getFont());
  156. }
  157. SimObject *GuiTreeViewCtrl::Item::getObject()
  158. {
  159. if(!mState.test(InspectorData))
  160. {
  161. Con::errorf("Tried to get the object for item %d, which is not InspectorData!", mId);
  162. return NULL;
  163. }
  164. return mInspectorInfo.mObject;
  165. }
  166. const U32 GuiTreeViewCtrl::Item::getDisplayTextLength()
  167. {
  168. if(mState.test(Item::InspectorData))
  169. {
  170. SimObject *obj = getObject();
  171. if(!obj)
  172. return 0;
  173. // For the results buffer, it's prefix along with a bunch of other stuff.
  174. // So we'll be mostly accurate and add a bit of fudge.
  175. return (16
  176. + (obj->getName() ? dStrlen(obj->getName()) : 0) + dStrlen(obj->getClassName())
  177. + dStrlen(obj->getIdString()) + 20
  178. );
  179. }
  180. char *pText = getText();
  181. if( pText == NULL )
  182. return 0;
  183. return dStrlen( pText );
  184. }
  185. void GuiTreeViewCtrl::Item::getDisplayText(U32 bufLen, char *buf)
  186. {
  187. FrameAllocatorMarker txtAlloc;
  188. if(mState.test(Item::InspectorData))
  189. {
  190. // Inspector data!
  191. SimObject *pObject = getObject();
  192. if(pObject)
  193. {
  194. const char* pObjName = pObject->getName();
  195. const char* pInternalName = pObject->getInternalName();
  196. if( pObjName != NULL )
  197. dSprintf(buf, bufLen, "%d: %s - %s", pObject->getId(), pObject->getClassName(), pObjName );
  198. else if ( pInternalName != NULL )
  199. dSprintf(buf, bufLen, "%d: %s [%s]", pObject->getId(), pObject->getClassName(), pInternalName);
  200. else
  201. dSprintf(buf, bufLen, "%d: %s", pObject->getId(), pObject->getClassName());
  202. }
  203. }
  204. else
  205. {
  206. // Script data! (copy it in)
  207. dStrncpy(buf, getText(), bufLen);
  208. }
  209. }
  210. const S32 GuiTreeViewCtrl::Item::getDisplayTextWidth(GFont *font)
  211. {
  212. if( !font )
  213. return 0;
  214. FrameAllocatorMarker txtAlloc;
  215. U32 bufLen = getDisplayTextLength();
  216. if( bufLen == 0 )
  217. return 0;
  218. char *buf = (char*)txtAlloc.alloc(bufLen);
  219. getDisplayText(bufLen, buf);
  220. return font->getStrWidth(buf);
  221. }
  222. const bool GuiTreeViewCtrl::Item::isParent() const
  223. {
  224. if(mState.test(VirtualParent))
  225. {
  226. if( !isInspectorData() )
  227. return true;
  228. // Does our object have any children?
  229. if(mInspectorInfo.mObject)
  230. {
  231. SimSet *pSimSet = dynamic_cast<SimSet*>( (SimObject*)mInspectorInfo.mObject);
  232. if ( pSimSet != NULL && pSimSet->size() > 0)
  233. return pSimSet->size();
  234. }
  235. }
  236. // Otherwise, just return whether the child list is populated.
  237. return mChild;
  238. }
  239. const bool GuiTreeViewCtrl::Item::isExpanded() const
  240. {
  241. if(mState.test(InspectorData))
  242. return mInspectorInfo.mObject ? mInspectorInfo.mObject->isExpanded() : false;
  243. else
  244. return mState.test(Expanded);
  245. }
  246. void GuiTreeViewCtrl::Item::setExpanded(bool f)
  247. {
  248. if(mState.test(InspectorData) && !mInspectorInfo.mObject.isNull() )
  249. mInspectorInfo.mObject->setExpanded(f);
  250. else
  251. mState.set(Expanded, f);
  252. }
  253. void GuiTreeViewCtrl::Item::setVirtualParent( bool value )
  254. {
  255. mState.set(VirtualParent, value);
  256. }
  257. GuiTreeViewCtrl::Item *GuiTreeViewCtrl::Item::findChildByValue(const SimObject *obj)
  258. {
  259. // Iterate over our children and try to find the given
  260. // SimObject
  261. Item *pResultObj = mChild;
  262. while(pResultObj)
  263. {
  264. // Skip non-inspector data stuff.
  265. if(pResultObj->mState.test(InspectorData))
  266. {
  267. if(pResultObj->getObject() == obj)
  268. return pResultObj;
  269. }
  270. pResultObj = pResultObj->mNext;
  271. }
  272. // If the loop terminated we are NULL, otherwise we have the result in res.
  273. return NULL;
  274. }
  275. GuiTreeViewCtrl::Item *GuiTreeViewCtrl::Item::findChildByValue( StringTableEntry Value )
  276. {
  277. // Iterate over our children and try to find the given Value
  278. // Note : This is a case-insensitive search
  279. Item *pResultObj = mChild;
  280. while(pResultObj)
  281. {
  282. // check the script value of the item against the specified value
  283. if( pResultObj->mScriptInfo.mValue != NULL && dStricmp( pResultObj->mScriptInfo.mValue, Value ) == 0 )
  284. return pResultObj;
  285. pResultObj = pResultObj->mNext;
  286. }
  287. // If the loop terminated we didn't find an item with the specified script value
  288. return NULL;
  289. }
  290. //------------------------------------------------------------------------------
  291. GuiTreeViewCtrl::GuiTreeViewCtrl()
  292. {
  293. VECTOR_SET_ASSOCIATION(mItems);
  294. VECTOR_SET_ASSOCIATION(mVisibleItems);
  295. VECTOR_SET_ASSOCIATION(mSelectedItems);
  296. VECTOR_SET_ASSOCIATION(mSelected);
  297. mItemFreeList = NULL;
  298. mRoot = NULL;
  299. mInstantGroup = 0;
  300. mItemCount = 0;
  301. mSelectedItem = 0;
  302. mStart = 0;
  303. mTicksPassed = 0;
  304. mTreeRefreshInterval = 30;
  305. mDraggedToItem = 0;
  306. mOldDragY = 0;
  307. mCurrentDragCell = 0;
  308. mPreviousDragCell = 0;
  309. mDragMidPoint = NomDragMidPoint;
  310. mMouseDragged = false;
  311. mDebug = false;
  312. // persist info..
  313. mTabSize = 16;
  314. mTextOffset = 2;
  315. mFullRowSelect = false;
  316. mItemHeight = 20;
  317. //
  318. setSize(Point2I(1, 0));
  319. // Set up default state
  320. mFlags.set(ShowTreeLines);
  321. mFlags.set(IsEditable, false);
  322. mDestroyOnSleep = true;
  323. mSupportMouseDragging = true;
  324. mMultipleSelections = true;
  325. mDeleteObjectAllowed = true;
  326. mDragToItemAllowed = true;
  327. mBitmapBase = StringTable->EmptyString;
  328. mTexRollover = NULL;
  329. mTexSelected = NULL;
  330. }
  331. GuiTreeViewCtrl::~GuiTreeViewCtrl()
  332. {
  333. destroyTree();
  334. }
  335. //------------------------------------------------------------------------------
  336. void GuiTreeViewCtrl::initPersistFields()
  337. {
  338. Parent::initPersistFields();
  339. addGroup("TreeView");
  340. addField("tabSize", TypeS32, Offset(mTabSize, GuiTreeViewCtrl));
  341. addField("textOffset", TypeS32, Offset(mTextOffset, GuiTreeViewCtrl));
  342. addField("fullRowSelect", TypeBool, Offset(mFullRowSelect, GuiTreeViewCtrl));
  343. addField("itemHeight", TypeS32, Offset(mItemHeight, GuiTreeViewCtrl));
  344. addField("destroyTreeOnSleep", TypeBool, Offset(mDestroyOnSleep, GuiTreeViewCtrl));
  345. addField("MouseDragging", TypeBool, Offset(mSupportMouseDragging, GuiTreeViewCtrl));
  346. addField("MultipleSelections", TypeBool, Offset(mMultipleSelections, GuiTreeViewCtrl));
  347. addField("DeleteObjectAllowed", TypeBool, Offset(mDeleteObjectAllowed, GuiTreeViewCtrl));
  348. addField("DragToItemAllowed", TypeBool, Offset(mDragToItemAllowed, GuiTreeViewCtrl));
  349. endGroup("TreeView");
  350. }
  351. //------------------------------------------------------------------------------
  352. GuiTreeViewCtrl::Item * GuiTreeViewCtrl::getItem(S32 itemId)
  353. {
  354. if((itemId > 0) && (itemId <= mItems.size()))
  355. {
  356. if (mItems[itemId-1] != NULL)
  357. return(mItems[itemId-1]);
  358. }
  359. return(0);
  360. }
  361. //------------------------------------------------------------------------------
  362. GuiTreeViewCtrl::Item * GuiTreeViewCtrl::createItem(S32 icon)
  363. {
  364. Item * pNewItem = NULL;
  365. // grab from the free list?
  366. if( mItemFreeList )
  367. {
  368. pNewItem = mItemFreeList;
  369. mItemFreeList = pNewItem->mNext;
  370. // re-add to vector
  371. mItems[ pNewItem->mId - 1 ] = pNewItem;
  372. }
  373. else
  374. {
  375. pNewItem = new Item( mProfile );
  376. AssertFatal( pNewItem != NULL, "Fatal : unable to allocate tree item!");
  377. mItems.push_back( pNewItem );
  378. // set the id
  379. pNewItem->mId = mItems.size();
  380. }
  381. // reset
  382. if (icon)
  383. pNewItem->mIcon = icon;
  384. else
  385. pNewItem->mIcon = Default; //default icon to stick next to an item
  386. pNewItem->mState.clear();
  387. pNewItem->mState = 0;
  388. pNewItem->mTabLevel = 0;
  389. // Null out item pointers
  390. pNewItem->mNext = 0;
  391. pNewItem->mPrevious = 0;
  392. pNewItem->mChild = 0;
  393. pNewItem->mParent = 0;
  394. mItemCount++;
  395. return pNewItem;
  396. }
  397. //------------------------------------------------------------------------------
  398. void GuiTreeViewCtrl::destroyChildren(Item * item, Item * parent)
  399. {
  400. if ( !item || item == parent )
  401. return;
  402. if ( item->isParent() && item->mChild )
  403. destroyChildren(item->mChild, parent);
  404. else if( item->mNext )
  405. destroyChildren(item->mNext, parent);
  406. else
  407. {
  408. // destroy the item and back up
  409. Item * pPrevItem = item->mPrevious;
  410. destroyItem( item );
  411. destroyChildren( pPrevItem, parent );
  412. }
  413. }
  414. void GuiTreeViewCtrl::destroyItem(Item * item)
  415. {
  416. if(!item)
  417. return;
  418. if(item->isInspectorData())
  419. {
  420. // make sure the SimObjectPtr is clean!
  421. //Con::executef(this,2, "onDeleteObject",Con::getIntArg(item->mInspectorInfo.mObject->getIdString()));
  422. SimObject *pObject = item->getObject();
  423. if( pObject && pObject->isProperlyAdded() )
  424. pObject->deleteObject();
  425. item->setObject( NULL );
  426. }
  427. else
  428. {
  429. char *pCleanup = item->getText();
  430. // Clean up the memory...
  431. if ( pCleanup != NULL )
  432. {
  433. delete []pCleanup;
  434. item->setText( NULL );
  435. }
  436. pCleanup = item->getValue();
  437. if ( pCleanup != NULL )
  438. {
  439. delete []pCleanup;
  440. item->setValue( NULL );
  441. }
  442. }
  443. // unlink
  444. if( item->mPrevious )
  445. item->mPrevious->mNext = item->mNext;
  446. if( item->mNext )
  447. item->mNext->mPrevious = item->mPrevious;
  448. if( item->mParent && ( item->mParent->mChild == item ) )
  449. item->mParent->mChild = item->mNext;
  450. // remove from vector
  451. mItems[item->mId-1] = 0;
  452. // set as root free item
  453. item->mNext = mItemFreeList;
  454. mItemFreeList = item;
  455. mItemCount--;
  456. }
  457. //------------------------------------------------------------------------------
  458. void GuiTreeViewCtrl::deleteItem(Item *item)
  459. {
  460. removeItem(item->mId);
  461. }
  462. //------------------------------------------------------------------------------
  463. void GuiTreeViewCtrl::destroyTree()
  464. {
  465. // clear the item list
  466. for(U32 i = 0; i < (U32)mItems.size(); i++)
  467. {
  468. Item *pFreeItem = mItems[ i ];
  469. if( pFreeItem != NULL )
  470. delete pFreeItem;
  471. }
  472. mItems.clear();
  473. // clear the free list
  474. while(mItemFreeList)
  475. {
  476. Item *next = mItemFreeList->mNext;
  477. delete mItemFreeList;
  478. mItemFreeList = next;
  479. }
  480. mVisibleItems.clear();
  481. mSelectedItems.clear();
  482. //
  483. mRoot = NULL;
  484. mItemFreeList = NULL;
  485. mItemCount = 0;
  486. mSelectedItem = 0;
  487. mDraggedToItem = 0;
  488. }
  489. //------------------------------------------------------------------------------
  490. void GuiTreeViewCtrl::buildItem( Item* item, U32 tabLevel, bool bForceFullUpdate )
  491. {
  492. if (!item || !mActive || !isVisible() || !mProfile )
  493. return;
  494. // If it's inspector data, make sure we still have it, if not, kill it.
  495. if(item->isInspectorData() && !item->getObject() )
  496. {
  497. removeItem(item->mId);
  498. return;
  499. }
  500. // If it's a virtual parent, give a chance to update itself...
  501. if(item->mState.test( Item::VirtualParent) )
  502. {
  503. // If it returns false the item has been removed.
  504. if(!onVirtualParentBuild(item, bForceFullUpdate))
  505. return;
  506. }
  507. item->mTabLevel = tabLevel;
  508. mVisibleItems.push_back( item );
  509. if ( mProfile != NULL && mProfile->getFont(mFontSizeAdjust) )
  510. {
  511. S32 width = ( tabLevel + 1 ) * mTabSize + item->getDisplayTextWidth(mProfile->getFont(mFontSizeAdjust));
  512. if ( mProfile->mBitmapArrayRects.size() > 0 )
  513. width += mProfile->mBitmapArrayRects[0].extent.x;
  514. width += (item->mTabLevel+1) * mItemHeight; // using mItemHeight for icon width, close enough
  515. // this will only fail if somebody starts using super wide icons.
  516. if ( width > mMaxWidth )
  517. mMaxWidth = width;
  518. }
  519. // if expanded, then add all the children items as well
  520. if ( item->isExpanded() || bForceFullUpdate)
  521. {
  522. Item * child = item->mChild;
  523. while ( child )
  524. {
  525. // Bit of a hack so we can safely remove items as we
  526. // traverse.
  527. Item *pChildTemp = child;
  528. child = child->mNext;
  529. buildItem( pChildTemp, tabLevel + 1, bForceFullUpdate );
  530. }
  531. }
  532. }
  533. //------------------------------------------------------------------------------
  534. void GuiTreeViewCtrl::buildVisibleTree(bool bForceFullUpdate)
  535. {
  536. // Recursion Prevention.
  537. if( mFlags.test( BuildingVisTree ) )
  538. return;
  539. mFlags.set( BuildingVisTree, true );
  540. mMaxWidth = 0;
  541. mVisibleItems.clear();
  542. // Update the flags.
  543. mFlags.clear(RebuildVisible);
  544. // build the root items
  545. Item *traverse = mRoot;
  546. while(traverse)
  547. {
  548. buildItem(traverse, 0, bForceFullUpdate);
  549. traverse = traverse->mNext;
  550. }
  551. // adjust the GuiArrayCtrl
  552. mCellSize.set(mMaxWidth+1, mItemHeight);
  553. setSize(Point2I(1, mVisibleItems.size()));
  554. syncSelection();
  555. // Done Recursing.
  556. mFlags.clear( BuildingVisTree );
  557. }
  558. //------------------------------------------------------------------------------
  559. bool GuiTreeViewCtrl::scrollVisible( S32 itemId )
  560. {
  561. Item* item = getItem(itemId);
  562. if(item)
  563. return scrollVisible(item);
  564. return false;
  565. }
  566. bool GuiTreeViewCtrl::scrollVisible( Item *item )
  567. {
  568. // Now, make sure it's visible (ie, all parents expanded)
  569. Item *parent = item->mParent;
  570. if( !item->isInspectorData() && item->mState.test(Item::VirtualParent) )
  571. onVirtualParentExpand(item);
  572. while(parent)
  573. {
  574. parent->setExpanded(true);
  575. if( !parent->isInspectorData() && parent->mState.test(Item::VirtualParent) )
  576. onVirtualParentExpand(parent);
  577. parent = parent->mParent;
  578. }
  579. // Get our scroll-pappy, if any.
  580. GuiScrollCtrl *pScrollParent = dynamic_cast<GuiScrollCtrl*>( getParent() );
  581. if ( !pScrollParent )
  582. {
  583. Con::warnf("GuiTreeViewCtrl::scrollVisible - parent control is not a GuiScrollCtrl!");
  584. return false;
  585. }
  586. // And now, build the visible tree so we know where we have to scroll.
  587. buildVisibleTree();
  588. // All done, let's figure out where we have to scroll...
  589. for(S32 i=0; i<mVisibleItems.size(); i++)
  590. {
  591. if(mVisibleItems[i] == item)
  592. {
  593. pScrollParent->scrollRectVisible(RectI(0, i * mItemHeight, mMaxWidth, mItemHeight));
  594. return true;
  595. }
  596. }
  597. // If we got here, it's probably bad...
  598. Con::errorf("GuiTreeViewCtrl::scrollVisible - was unable to find specified item in visible list!");
  599. return false;
  600. }
  601. //------------------------------------------------------------------------------
  602. S32 GuiTreeViewCtrl::insertItem(S32 parentId, const char * text, const char * value, const char * iconString, S16 normalImage, S16 expandedImage)
  603. {
  604. if( ( parentId < 0 ) || ( parentId > mItems.size() ) )
  605. {
  606. Con::errorf(ConsoleLogEntry::General, "GuiTreeViewCtrl::insertItem: invalid parent id!");
  607. return 0;
  608. }
  609. if((parentId != 0) && (mItems[parentId-1] == 0))
  610. {
  611. Con::errorf(ConsoleLogEntry::General, "GuiTreeViewCtrl::insertItem: parent item invalid!");
  612. return 0;
  613. }
  614. const char * pItemText = ( text != NULL ) ? dStrdup( text ) : "";
  615. const char * pItemValue = ( value != NULL ) ? dStrdup( value ) : "";
  616. S32 icon = getIcon(iconString);
  617. // create an item (assigns id)
  618. Item * pNewItem = createItem(icon);
  619. if( pNewItem == NULL )
  620. return 0;
  621. // // Ugh. This code bothers me. - JDD
  622. // pNewItem->setText( new char[dStrlen( pItemText ) + 1] );
  623. // dStrcpy( pNewItem->getText(), pItemText );
  624. // pNewItem->setValue( new char[dStrlen( pItemValue ) + 1] );
  625. // dStrcpy( pNewItem->getValue(), pItemValue );
  626. // paxorr fix:
  627. char *tmp = new char[dStrlen( pItemText ) + 1];
  628. dStrcpy( tmp, pItemText );
  629. pNewItem->setText( tmp );
  630. tmp = new char[dStrlen( pItemValue ) + 1];
  631. dStrcpy( tmp, pItemValue );
  632. pNewItem->setValue( tmp );
  633. pNewItem->setNormalImage( (S8)normalImage );
  634. pNewItem->setExpandedImage( (S8)expandedImage );
  635. // root level?
  636. if(parentId == 0)
  637. {
  638. // insert back
  639. if( mRoot != NULL )
  640. {
  641. Item * pTreeTraverse = mRoot;
  642. while( pTreeTraverse != NULL && pTreeTraverse->mNext != NULL )
  643. pTreeTraverse = pTreeTraverse->mNext;
  644. pTreeTraverse->mNext = pNewItem;
  645. pNewItem->mPrevious = pTreeTraverse;
  646. }
  647. else
  648. mRoot = pNewItem;
  649. mFlags.set(RebuildVisible);
  650. }
  651. else if( mItems.size() >= ( parentId - 1 ) )
  652. {
  653. Item * pParentItem = mItems[parentId-1];
  654. // insert back
  655. if( pParentItem != NULL && pParentItem->mChild)
  656. {
  657. Item * pTreeTraverse = pParentItem->mChild;
  658. while( pTreeTraverse != NULL && pTreeTraverse->mNext != NULL )
  659. pTreeTraverse = pTreeTraverse->mNext;
  660. pTreeTraverse->mNext = pNewItem;
  661. pNewItem->mPrevious = pTreeTraverse;
  662. }
  663. else
  664. pParentItem->mChild = pNewItem;
  665. pNewItem->mParent = pParentItem;
  666. if( pParentItem->isExpanded() )
  667. mFlags.set(RebuildVisible);
  668. }
  669. //
  670. if(mFlags.test(RebuildVisible))
  671. buildVisibleTree();
  672. return pNewItem->mId;
  673. }
  674. //------------------------------------------------------------------------------
  675. bool GuiTreeViewCtrl::removeItem(S32 itemId)
  676. {
  677. // tree?
  678. if(itemId == 0)
  679. {
  680. destroyTree();
  681. return(true);
  682. }
  683. Item * item = getItem(itemId);
  684. if(!item)
  685. {
  686. //Con::errorf(ConsoleLogEntry::General, "GuiTreeViewCtrl::removeItem: invalid item id!");
  687. return false;
  688. }
  689. // root?
  690. if(item == mRoot)
  691. mRoot = item->mNext;
  692. // Dispose of any children...
  693. if (item->mChild)
  694. destroyChildren(item->mChild, item);
  695. // Kill the item...
  696. destroyItem(item);
  697. // Update the rendered tree...
  698. buildVisibleTree();
  699. return true;
  700. }
  701. void GuiTreeViewCtrl::removeAllChildren(S32 itemId)
  702. {
  703. Item * item = getItem(itemId);
  704. if(item)
  705. {
  706. destroyChildren(item->mChild, item);
  707. }
  708. }
  709. //------------------------------------------------------------------------------
  710. const S32 GuiTreeViewCtrl::getFirstRootItem() const
  711. {
  712. return (mRoot ? mRoot->mId : 0);
  713. }
  714. //------------------------------------------------------------------------------
  715. S32 GuiTreeViewCtrl::getChildItem(S32 itemId)
  716. {
  717. Item * item = getItem(itemId);
  718. if(!item)
  719. {
  720. Con::errorf(ConsoleLogEntry::General, "GuiTreeViewCtrl::getChild: invalid item id!");
  721. return(0);
  722. }
  723. return(item->mChild ? item->mChild->mId : 0);
  724. }
  725. S32 GuiTreeViewCtrl::getParentItem(S32 itemId)
  726. {
  727. Item * item = getItem(itemId);
  728. if(!item)
  729. {
  730. Con::errorf(ConsoleLogEntry::General, "GuiTreeViewCtrl::getParent: invalid item id!");
  731. return(0);
  732. }
  733. return(item->mParent ? item->mParent->mId : 0);
  734. }
  735. S32 GuiTreeViewCtrl::getNextSiblingItem(S32 itemId)
  736. {
  737. Item * item = getItem(itemId);
  738. if(!item)
  739. {
  740. Con::errorf(ConsoleLogEntry::General, "GuiTreeViewCtrl::getNextSibling: invalid item id!");
  741. return(0);
  742. }
  743. return(item->mNext ? item->mNext->mId : 0);
  744. }
  745. S32 GuiTreeViewCtrl::getPrevSiblingItem(S32 itemId)
  746. {
  747. Item * item = getItem(itemId);
  748. if(!item)
  749. {
  750. Con::errorf(ConsoleLogEntry::General, "GuiTreeViewCtrl::getPrevSibling: invalid item id!");
  751. return(0);
  752. }
  753. return(item->mPrevious ? item->mPrevious->mId : 0);
  754. }
  755. //------------------------------------------------------------------------------
  756. S32 GuiTreeViewCtrl::getItemCount()
  757. {
  758. return(mItemCount);
  759. }
  760. S32 GuiTreeViewCtrl::getSelectedItem()
  761. {
  762. return mSelectedItem;
  763. }
  764. //------------------------------------------------------------------------------
  765. void GuiTreeViewCtrl::moveItemUp( S32 itemId )
  766. {
  767. GuiTreeViewCtrl::Item* pItem = getItem( itemId );
  768. if ( !pItem )
  769. {
  770. Con::errorf( ConsoleLogEntry::General, "GuiTreeViewCtrl::moveItemUp: invalid item id!");
  771. return;
  772. }
  773. Item * pParent = pItem->mParent;
  774. Item * pPrevItem = pItem->mPrevious;
  775. if ( pPrevItem == NULL || pParent == NULL )
  776. {
  777. Con::errorf( ConsoleLogEntry::General, "GuiTreeViewCtrl::moveItemUp: Unable to move item up, bad data!");
  778. return;
  779. }
  780. // Diddle the linked list!
  781. if ( pPrevItem->mPrevious )
  782. pPrevItem->mPrevious->mNext = pItem;
  783. else if ( pItem->mParent )
  784. pItem->mParent->mChild = pItem;
  785. if ( pItem->mNext )
  786. pItem->mNext->mPrevious = pPrevItem;
  787. pItem->mPrevious = pPrevItem->mPrevious;
  788. pPrevItem->mNext = pItem->mNext;
  789. pItem->mNext = pPrevItem;
  790. pPrevItem->mPrevious = pItem;
  791. // Update SimObjects if Appropriate.
  792. SimObject * pSimObject = NULL;
  793. SimSet * pParentSet = NULL;
  794. // Fetch Current Add Set
  795. if( pParent->isInspectorData() )
  796. pParentSet = dynamic_cast<SimSet*>( pParent->getObject() );
  797. else
  798. {
  799. // parent is probably script data so we search up the tree for a
  800. // set to put our object in
  801. Item * pTraverse = pItem->mParent;
  802. while ( pTraverse != NULL && !pTraverse->isInspectorData() )
  803. pTraverse = pTraverse->mParent;
  804. // found an ancestor who is an inspectorData?
  805. pParentSet = pTraverse->isInspectorData() ? dynamic_cast<SimSet*>( pTraverse->getObject() ) : NULL;
  806. }
  807. // Reorder the item and make sure that the children of the item get updated
  808. // correctly prev item may be script... so find a prevItem if there is.
  809. // We only need to reorder if there you move it above an inspector item.
  810. if ( pSimObject != NULL && pParentSet != NULL )
  811. {
  812. Item * pTraverse = pItem->mNext;
  813. while(pTraverse)
  814. {
  815. if (pTraverse->isInspectorData())
  816. break;
  817. pTraverse = pTraverse->mNext;
  818. }
  819. if (pTraverse && pItem->getObject() && pTraverse->getObject())
  820. pParentSet->reOrder(pItem->getObject(), pTraverse->getObject());
  821. }
  822. buildVisibleTree();
  823. }
  824. void GuiTreeViewCtrl::moveItemDown( S32 itemId )
  825. {
  826. GuiTreeViewCtrl::Item* item = getItem( itemId );
  827. if ( !item )
  828. {
  829. Con::errorf( ConsoleLogEntry::General, "GuiTreeViewCtrl::moveItemDown: invalid item id!");
  830. return;
  831. }
  832. Item* nextItem = item->mNext;
  833. if ( !nextItem )
  834. {
  835. Con::errorf( ConsoleLogEntry::General, "GuiTreeViewCtrl::moveItemDown: no next sibling?");
  836. return;
  837. }
  838. // Diddle the linked list!
  839. if ( nextItem->mNext )
  840. nextItem->mNext->mPrevious = item;
  841. if ( item->mPrevious )
  842. item->mPrevious->mNext = nextItem;
  843. else if ( item->mParent )
  844. item->mParent->mChild = nextItem;
  845. item->mNext = nextItem->mNext;
  846. nextItem->mPrevious = item->mPrevious;
  847. item->mPrevious = nextItem;
  848. nextItem->mNext = item;
  849. // And update the simobjects if apppropriate...
  850. SimObject * simobj = NULL;
  851. if (item->isInspectorData())
  852. simobj = item->getObject();
  853. SimSet *parentSet = NULL;
  854. // grab the current parentSet if there is any...
  855. if(item->mParent->isInspectorData())
  856. parentSet = dynamic_cast<SimSet*>(item->mParent->getObject());
  857. else
  858. {
  859. // parent is probably script data so we search up the tree for a
  860. // set to put our object in
  861. Item * temp = item->mParent;
  862. while (!temp->isInspectorData())
  863. temp = temp->mParent;
  864. // found an ancestor who is an inspectorData?
  865. parentSet = temp->isInspectorData() ? dynamic_cast<SimSet*>(temp->getObject()) : NULL;
  866. }
  867. // Reorder the item and make sure that the children of the item get updated
  868. // correctly prev item may be script... so find a prevItem if there is.
  869. // We only need to reorder if there you move it above an inspector item.
  870. if (simobj && parentSet)
  871. {
  872. Item * temp = item->mPrevious;
  873. while(temp)
  874. {
  875. if (temp->isInspectorData())
  876. break;
  877. temp = temp->mPrevious;
  878. }
  879. if (temp && item->getObject() && temp->getObject())
  880. parentSet->reOrder(temp->getObject(), item->getObject());
  881. }
  882. buildVisibleTree();
  883. }
  884. //------------------------------------------------------------------------------
  885. bool GuiTreeViewCtrl::onWake()
  886. {
  887. if(!Parent::onWake() || !mProfile->constructBitmapArray())
  888. return false;
  889. // If destroy on sleep, then we have to give things a chance to rebuild.
  890. if(mDestroyOnSleep)
  891. {
  892. destroyTree();
  893. Con::executef(this, 1, "onWake");
  894. // (Re)build our icon table.
  895. const char * res = Con::executef(this, 1, "onDefineIcons");
  896. // If no icons were defined in script then use defaults.
  897. if(!(dAtob(res)))
  898. {
  899. buildIconTable(NULL);
  900. }
  901. }
  902. // Update the row height, if appropriate.
  903. // DEPRECIATED
  904. /*
  905. if(mProfile->mAutoSizeHeight)
  906. {
  907. // make sure it's big enough for both bitmap AND font...
  908. mItemHeight = getMax((S32)mFont->getHeight(), (S32)mProfile->mBitmapArrayRects[0].extent.y);
  909. }
  910. */
  911. return true;
  912. }
  913. void GuiTreeViewCtrl::onSleep()
  914. {
  915. Parent::onSleep();
  916. // If appropriate, blast the tree. (We probably rebuild it on wake.)
  917. if( mDestroyOnSleep )
  918. destroyTree();
  919. }
  920. bool GuiTreeViewCtrl::buildIconTable(const char * icons)
  921. {
  922. // Icons should be designated by the bitmap/png file names (minus the file extensions)
  923. // and separated by colons (:). This list should be synchronized with the Icons enum.
  924. // This is an abominal piece of code. -- BJG
  925. if (!icons)
  926. {
  927. icons = "^Sandbox/gui/images/default:"
  928. "^Sandbox/gui/images/simgroup:"
  929. "^Sandbox/gui/images/simgroupClosed:"
  930. "^Sandbox/gui/images/simgroupSelected:"
  931. "^Sandbox/gui/images/simgroupSelectedClosed:"
  932. "^Sandbox/gui/images/Camera:"
  933. "^Sandbox/gui/images/iconVisible:"
  934. "^Sandbox/gui/images/iconLocked:";
  935. }
  936. // Figure the size of the buffer we need...
  937. const char* temp = dStrchr( icons, '\t' );
  938. U32 textLen = temp ? (U32)( temp - icons ) : dStrlen( icons );
  939. // Allocate temporary space.
  940. FrameAllocatorMarker txtBuff;
  941. char* drawText = (char*)txtBuff.alloc(sizeof(char) * (textLen + 4));
  942. dStrncpy( drawText, icons, textLen );
  943. drawText[textLen] = '\0';
  944. U32 numIcons = 0;
  945. char *buf = (char*)txtBuff.alloc(sizeof(char) * 256);
  946. char* token = dStrtok( drawText, ":" );
  947. // Count the number of icons and store them.
  948. while (token && numIcons < MaxIcons)
  949. {
  950. dSprintf( buf, sizeof( char ) * 256, "%s", token );
  951. mIconTable[numIcons] = TextureHandle( buf, TextureHandle::BitmapKeepTexture );
  952. token = dStrtok( NULL, ":" );
  953. numIcons++;
  954. }
  955. return true;
  956. }
  957. //------------------------------------------------------------------------------
  958. void GuiTreeViewCtrl::onPreRender()
  959. {
  960. Parent::onPreRender();
  961. S32 nRootItemId = getFirstRootItem();
  962. if( nRootItemId == 0 )
  963. return;
  964. Item *pRootItem = getItem( nRootItemId );
  965. if( pRootItem == NULL )
  966. return;
  967. mTicksPassed++;
  968. if( mTicksPassed > mTreeRefreshInterval )
  969. {
  970. // Update every render in case new objects are added
  971. buildVisibleTree();
  972. mTicksPassed = 0;
  973. }
  974. }
  975. //------------------------------------------------------------------------------
  976. bool GuiTreeViewCtrl::hitTest(const Point2I & pnt, Item* & item, BitSet32 & flags)
  977. {
  978. // Initialize some things.
  979. const Point2I pos = globalToLocalCoord(pnt);
  980. flags.clear();
  981. item = 0;
  982. // get the hit cell
  983. Point2I cell((pos.x < 0 ? -1 : pos.x / mCellSize.x),
  984. (pos.y < 0 ? -1 : pos.y / mCellSize.y));
  985. // valid?
  986. if((cell.x < 0 || cell.x >= mSize.x) ||
  987. (cell.y < 0 || cell.y >= mSize.y))
  988. return false;
  989. flags.set(OnRow);
  990. // Grab the cell.
  991. if (cell.y >= mVisibleItems.size())
  992. return false; //Invalid cell, so don't do anything
  993. item = mVisibleItems[cell.y];
  994. S32 min = mTabSize * item->mTabLevel;
  995. // left of icon/text?
  996. if(pos.x < min)
  997. {
  998. flags.set(OnIndent);
  999. return true;
  1000. }
  1001. // check image
  1002. S32 image = BmpChild;
  1003. if(item->isInspectorData())
  1004. image = item->isExpanded() ? BmpExp : BmpCon;
  1005. else
  1006. image = item->isExpanded() ? item->getExpandedImage() : item->getNormalImage();
  1007. if((image >= 0) && (image < mProfile->mBitmapArrayRects.size()))
  1008. min += mProfile->mBitmapArrayRects[image].extent.x;
  1009. // Is it on the image?
  1010. if(pos.x < min)
  1011. {
  1012. flags.set(OnImage);
  1013. return(true);
  1014. }
  1015. // Bump over to the start of the text.
  1016. min += mTextOffset;
  1017. // Check against the text.
  1018. FrameAllocatorMarker txtAlloc;
  1019. U32 bufLen = item->getDisplayTextLength();
  1020. char *buf = (char*)txtAlloc.alloc(bufLen);
  1021. item->getDisplayText(bufLen, buf);
  1022. min += mProfile->getFont(mFontSizeAdjust)->getStrWidth(buf);
  1023. if(pos.x < min)
  1024. flags.set(OnText);
  1025. return true;
  1026. }
  1027. void GuiTreeViewCtrl::setInstantGroup(SimObject * obj)
  1028. {
  1029. // make sure we're talking about a group.
  1030. SimGroup * grp = dynamic_cast<SimGroup*>(obj);
  1031. // Set the instant group variable.
  1032. if(grp)
  1033. {
  1034. Con::setVariable("instantGroup", grp->getIdString());
  1035. // Notify Script
  1036. Con::executef(this,2,"onInstantGroupSelected",Con::getIntArg(grp->getId()));
  1037. }
  1038. }
  1039. void GuiTreeViewCtrl::syncSelection()
  1040. {
  1041. // for each visible item check to see if it is on the mSelected list.
  1042. // if it is then make sure that it is on the mSelectedItems list as well.
  1043. for (S32 i = 0; i < mVisibleItems.size(); i++)
  1044. {
  1045. for (S32 j = 0; j < mSelected.size(); j++)
  1046. {
  1047. if (mVisibleItems[i]->mId == mSelected[j])
  1048. {
  1049. // check to see if it is on the visible items list.
  1050. bool addToSelectedItems = true;
  1051. for (S32 k = 0; k < mSelectedItems.size(); k++)
  1052. {
  1053. if (mSelected[j] == mSelectedItems[k]->mId)
  1054. {
  1055. // don't add it
  1056. addToSelectedItems = false;
  1057. }
  1058. }
  1059. if (addToSelectedItems)
  1060. {
  1061. mVisibleItems[i]->mState.set(Item::Selected, true);
  1062. mSelectedItems.push_front(mVisibleItems[i]);
  1063. break;
  1064. }
  1065. }
  1066. else if (mVisibleItems[i]->isInspectorData())
  1067. {
  1068. if (mVisibleItems[i]->getObject() && mVisibleItems[i]->getObject()->getId() == mSelected[j])
  1069. {
  1070. // check to see if it is on the visible items list.
  1071. bool addToSelectedItems = true;
  1072. for (S32 k = 0; k < mSelectedItems.size(); k++)
  1073. {
  1074. if (mSelectedItems[k]->isInspectorData())
  1075. {
  1076. if (mSelected[j] == mSelectedItems[k]->getObject()->getId())
  1077. {
  1078. // don't add it
  1079. addToSelectedItems = false;
  1080. }
  1081. }
  1082. else
  1083. {
  1084. if (mSelected[j] == mSelectedItems[k]->mId)
  1085. {
  1086. // don't add it
  1087. addToSelectedItems = false;
  1088. }
  1089. }
  1090. }
  1091. if (addToSelectedItems)
  1092. {
  1093. mVisibleItems[i]->mState.set(Item::Selected, true);
  1094. mSelectedItems.push_front(mVisibleItems[i]);
  1095. break;
  1096. }
  1097. }
  1098. }
  1099. }
  1100. }
  1101. }
  1102. void GuiTreeViewCtrl::removeSelection(S32 itemId)
  1103. {
  1104. if (mDebug)
  1105. Con::printf("removeSelection called");
  1106. Item * item = getItem(itemId);
  1107. // the item may have been selected at one point but was never created/visible in the tree
  1108. // so remove it.
  1109. for (S32 j = 0; j <mSelected.size(); j++)
  1110. {
  1111. if (item)
  1112. {
  1113. if (item->isInspectorData())
  1114. {
  1115. if (item->getObject() && item->getObject()->getId() == mSelected[j])
  1116. {
  1117. mSelected.erase(j);
  1118. break;
  1119. }
  1120. }
  1121. }
  1122. if (mSelected[j] == itemId)
  1123. {
  1124. mSelected.erase(j);
  1125. break;
  1126. }
  1127. }
  1128. if(!item)
  1129. {
  1130. // maybe what we were passed wasn't an item id but an object id.
  1131. for (S32 i = 0; i <mItems.size(); i++)
  1132. {
  1133. if (mItems[i] != NULL)
  1134. {
  1135. if (mItems[i]->isInspectorData())
  1136. {
  1137. if (mItems[i]->getObject()->getId() == itemId)
  1138. {
  1139. item = mItems[i];
  1140. break;
  1141. }
  1142. }
  1143. }
  1144. }
  1145. if (!item)
  1146. {
  1147. //Con::errorf(ConsoleLogEntry::General, "GuiTreeViewCtrl::removeSelection: invalid item id! Perhaps it isn't visible yet");
  1148. return;
  1149. }
  1150. }
  1151. item->mState.set(Item::Selected, false);
  1152. for (S32 i = 0; i < mSelectedItems.size(); i++)
  1153. {
  1154. if (mSelectedItems[i] == item)
  1155. {
  1156. mSelectedItems.erase(i);
  1157. break;
  1158. }
  1159. }
  1160. // Callback - onRemoveSelection( %itemID )
  1161. if (item->getObject())
  1162. Con::executef(this, 2, "onRemoveSelection", Con::getIntArg(item->getObject()->getId()));
  1163. }
  1164. void GuiTreeViewCtrl::addSelection(S32 itemId)
  1165. {
  1166. if (mDebug)
  1167. Con::printf("addSelection called");
  1168. Item * item = getItem(itemId);
  1169. S32 itr; // used to loop thru items
  1170. bool foundMatch = false;
  1171. if(!item)
  1172. {
  1173. // maybe what we were passed wasn't an item id but an object id.
  1174. for (itr = 0; itr <mItems.size(); itr++)
  1175. {
  1176. if (mItems[itr] != NULL)
  1177. {
  1178. if (mItems[itr]->isInspectorData())
  1179. {
  1180. if ( mItems[itr]->getObject() && mItems[itr]->getObject()->getId() == itemId)
  1181. {
  1182. item = mItems[itr];
  1183. //looks like it is. check to see if it is on the list
  1184. bool alreadySelected = false;
  1185. Vector<Item *>::iterator i;
  1186. for(i = mSelectedItems.begin(); i != mSelectedItems.end(); i++)
  1187. {
  1188. if (*(i) == item)
  1189. {
  1190. //already a selected item which means this call should be ignored
  1191. alreadySelected = true;
  1192. return;
  1193. }
  1194. }
  1195. break;
  1196. }
  1197. }
  1198. }
  1199. }
  1200. if (!item)
  1201. {
  1202. // Do we want to allow more than one selected item?
  1203. if( !mMultipleSelections )
  1204. clearSelection();
  1205. // rdbnote: this isn't going to be very fast, but we need to make sure we're not adding things to this list twice!!
  1206. // in a perfect world, code would be cleanly written enough to not have this problem..
  1207. foundMatch = false;
  1208. for (itr = 0; itr < mSelected.size(); itr++)
  1209. {
  1210. if (mSelected[itr] == itemId)
  1211. foundMatch = true;
  1212. }
  1213. //Con::errorf(ConsoleLogEntry::General, "GuiTreeViewCtrl::addSelection: invalid item id! Perhaps it isn't visible yet.");
  1214. if (!foundMatch)
  1215. mSelected.push_front(itemId);
  1216. return;
  1217. }
  1218. }
  1219. else
  1220. {
  1221. // Do we want to allow more than one selected item?
  1222. if( !mMultipleSelections )
  1223. clearSelection();
  1224. // rdbnote: this isn't going to be very fast, but we need to make sure we're not adding things to this list twice!!
  1225. // in a perfect world, code would be cleanly written enough to not have this problem..
  1226. foundMatch = false;
  1227. for (itr = 0; itr < mSelected.size(); itr++)
  1228. {
  1229. if (mSelected[itr] == itemId)
  1230. foundMatch = true;
  1231. }
  1232. // regardless of whether we found an item, we keep track of the Id that was passed
  1233. // as the item may simply not have been created/visible yet.
  1234. if (!foundMatch)
  1235. mSelected.push_front(itemId);
  1236. }
  1237. item->mState.set(Item::Selected, true);
  1238. // Also make it so we can see it if we didn't already.
  1239. scrollVisible(item);
  1240. // Do we want to allow more than one selected item?
  1241. //if( !mMultipleSelections )
  1242. // clearSelection();
  1243. // rdbnote: this isn't going to be very fast, but we need to make sure we're not adding things to this list twice!!
  1244. // in a perfect world, code would be cleanly written enough to not have this problem..
  1245. foundMatch = false;
  1246. for (itr = 0; itr < mSelectedItems.size(); itr++)
  1247. {
  1248. if (mSelectedItems[itr]->mId == item->mId)
  1249. foundMatch = true;
  1250. }
  1251. if (!foundMatch)
  1252. mSelectedItems.push_front(item);
  1253. // Callback - onAddSelection( %itemID )
  1254. if (item->getObject())
  1255. Con::executef(this, 2, "onAddSelection", Con::getIntArg(item->getObject()->getId()));
  1256. }
  1257. void GuiTreeViewCtrl::onItemSelected( Item *item )
  1258. {
  1259. char buf[16];
  1260. dSprintf(buf, 16, "%d", item->mId);
  1261. if (item->isInspectorData())
  1262. {
  1263. if(item->getObject())
  1264. Con::executef(this, 2, "onSelect", Con::getIntArg(item->getObject()->getId()));
  1265. if (!(item->isParent()) && item->getObject())
  1266. Con::executef(this, 2, "onInspect", Con::getIntArg(item->getObject()->getId()));
  1267. }
  1268. else
  1269. {
  1270. Con::executef(this, 2, "onSelect", buf);
  1271. if (!(item->isParent()))
  1272. Con::executef(this, 2, "onInspect", buf);
  1273. }
  1274. mSelectedItem = item->getID();
  1275. }
  1276. bool GuiTreeViewCtrl::setItemSelected(S32 itemId, bool select)
  1277. {
  1278. Item * item = getItem(itemId);
  1279. if (select)
  1280. {
  1281. if (mDebug) Con::printf("setItemSelected called true");
  1282. // rdbnote: this isn't going to be very fast, but we need to make sure we're not adding things to this list twice!!
  1283. // in a perfect world, code would be cleanly written enough to not have this problem..
  1284. bool foundMatch = false;
  1285. for (S32 itr = 0; itr < mSelected.size(); itr++)
  1286. {
  1287. if (mSelected[itr] == itemId)
  1288. foundMatch = true;
  1289. }
  1290. if (!foundMatch)
  1291. mSelected.push_front(itemId);
  1292. }
  1293. else
  1294. {
  1295. if (mDebug) Con::printf("setItemSelected called false");
  1296. // remove it from the mSelected list
  1297. for (S32 j = 0; j <mSelected.size(); j++)
  1298. {
  1299. if (item)
  1300. {
  1301. if (item->isInspectorData())
  1302. {
  1303. if (item->getObject())
  1304. {
  1305. if(item->getObject()->getId() == mSelected[j])
  1306. {
  1307. mSelected.erase(j);
  1308. break;
  1309. }
  1310. }
  1311. else
  1312. {
  1313. // Zombie, kill it!
  1314. mSelected.erase(j);
  1315. j--;
  1316. }
  1317. }
  1318. }
  1319. if (mSelected[j] == itemId)
  1320. {
  1321. mSelected.erase(j);
  1322. break;
  1323. }
  1324. }
  1325. }
  1326. if(!item)
  1327. {
  1328. // maybe what we were passed wasn't an item id but an object id.
  1329. for (S32 i = 0; i <mItems.size(); i++)
  1330. {
  1331. if (mItems[i] != NULL)
  1332. {
  1333. if (mItems[i]->isInspectorData())
  1334. {
  1335. if (mItems[i]->getObject())
  1336. {
  1337. if(mItems[i]->getObject()->getId() == itemId)
  1338. {
  1339. item = mItems[i];
  1340. break;
  1341. }
  1342. }
  1343. else
  1344. {
  1345. // It's a zombie, blast it.
  1346. mItems.erase(i);
  1347. i--;
  1348. }
  1349. }
  1350. }
  1351. }
  1352. if (!item)
  1353. {
  1354. //Con::errorf(ConsoleLogEntry::General, "GuiTreeViewCtrl::setItemSelected: invalid item id! Perhaps it isn't visible yet.");
  1355. return(false);
  1356. }
  1357. }
  1358. if(select)
  1359. {
  1360. addSelection( item->mId );
  1361. onItemSelected( item );
  1362. }
  1363. else
  1364. {
  1365. // unselect the item, if it's present.
  1366. item->mState.set(Item::Selected, false);
  1367. if (item->isInspectorData() && item->getObject())
  1368. Con::executef(this, 2, "onUnSelect", Con::getIntArg(item->getObject()->getId()));
  1369. else
  1370. Con::executef(this, 2, "onUnSelect", Con::getIntArg(item->mId));
  1371. // remove it from the selected items list
  1372. for (S32 i = 0; i < mSelectedItems.size(); i++)
  1373. {
  1374. if (mSelectedItems[i] == item)
  1375. {
  1376. mSelectedItems.erase(i);
  1377. break;
  1378. }
  1379. }
  1380. }
  1381. setUpdate();
  1382. return(true);
  1383. }
  1384. // Given an item's index in the selection list, return its itemId
  1385. S32 GuiTreeViewCtrl::getSelectedItem(S32 index)
  1386. {
  1387. if(index >= 0 && index < getSelectedItemsCount())
  1388. {
  1389. return mSelectedItems[index]->mId;
  1390. }
  1391. return -1;
  1392. }
  1393. bool GuiTreeViewCtrl::setItemExpanded(S32 itemId, bool expand)
  1394. {
  1395. Item * item = getItem(itemId);
  1396. if(!item)
  1397. {
  1398. Con::errorf(ConsoleLogEntry::General, "GuiTreeViewCtrl::setItemExpanded: invalid item id!");
  1399. return(false);
  1400. }
  1401. if(item->isExpanded() == expand)
  1402. return(true);
  1403. // expand parents
  1404. if(expand)
  1405. {
  1406. while(item)
  1407. {
  1408. if(item->mState.test(Item::VirtualParent))
  1409. onVirtualParentExpand(item);
  1410. item->setExpanded(true);
  1411. item = item->mParent;
  1412. }
  1413. }
  1414. else
  1415. {
  1416. if(item->mState.test(Item::VirtualParent))
  1417. onVirtualParentCollapse(item);
  1418. item->setExpanded(false);
  1419. }
  1420. return(true);
  1421. }
  1422. bool GuiTreeViewCtrl::setItemValue(S32 itemId, StringTableEntry Value)
  1423. {
  1424. Item * item = getItem(itemId);
  1425. if(!item)
  1426. {
  1427. Con::errorf(ConsoleLogEntry::General, "GuiTreeViewCtrl::setItemValue: invalid item id!");
  1428. return(false);
  1429. }
  1430. item->setValue( ( Value ) ? Value : "" );
  1431. return(true);
  1432. }
  1433. const char * GuiTreeViewCtrl::getItemText(S32 itemId)
  1434. {
  1435. Item * item = getItem(itemId);
  1436. if(!item)
  1437. {
  1438. Con::errorf(ConsoleLogEntry::General, "GuiTreeViewCtrl::getItemText: invalid item id!");
  1439. return("");
  1440. }
  1441. return(item->getText() ? item->getText() : "");
  1442. }
  1443. const char * GuiTreeViewCtrl::getItemValue(S32 itemId)
  1444. {
  1445. Item * item = getItem(itemId);
  1446. if(!item)
  1447. {
  1448. Con::errorf(ConsoleLogEntry::General, "GuiTreeViewCtrl::getItemValue: invalid item id!");
  1449. return("");
  1450. }
  1451. if(item->mState.test(Item::InspectorData))
  1452. {
  1453. // If it's InspectorData, we let people use this call to get an object reference.
  1454. return item->mInspectorInfo.mObject->getIdString();
  1455. }
  1456. else
  1457. {
  1458. // Just return the script value...
  1459. return(item->getValue() ? item->getValue() : "");
  1460. }
  1461. }
  1462. bool GuiTreeViewCtrl::editItem( S32 itemId, const char* newText, const char* newValue )
  1463. {
  1464. Item* item = getItem( itemId );
  1465. if ( !item )
  1466. {
  1467. Con::errorf(ConsoleLogEntry::General, "GuiTreeViewCtrl::editItem: invalid item id!");
  1468. return false;
  1469. }
  1470. if ( item->mState.test(Item::InspectorData) )
  1471. {
  1472. Con::errorf(ConsoleLogEntry::General, "GuiTreeViewCtrl::editItem: item %d is inspector data and may not be modified!", itemId);
  1473. return false;
  1474. }
  1475. delete [] item->getText();
  1476. item->setText (new char[dStrlen( newText ) + 1]);
  1477. dStrcpy( item->getText(), newText );
  1478. delete [] item->getValue();
  1479. item->setValue( new char[dStrlen( newValue ) + 1] );
  1480. dStrcpy( item->getValue(), newValue );
  1481. // Update the widths and such:
  1482. buildVisibleTree();
  1483. return true;
  1484. }
  1485. void GuiTreeViewCtrl::deleteSelection()
  1486. {
  1487. Con::executef(this, 1, "onDeleteSelection");
  1488. if (mSelectedItems.empty())
  1489. {
  1490. for (S32 i = 0; i < mSelected.size(); i++)
  1491. {
  1492. S32 objectId = mSelected[i];
  1493. // find the object
  1494. SimObject* obj = Sim::findObject(objectId);
  1495. obj->deleteObject();
  1496. }
  1497. }
  1498. else
  1499. {
  1500. Vector<Item*> delSelection;
  1501. delSelection = mSelectedItems;
  1502. mSelectedItems.clear();
  1503. while (!delSelection.empty())
  1504. {
  1505. Item * item = delSelection.front();
  1506. setItemSelected(item->mId,false);
  1507. if ( item->mParent )
  1508. deleteItem( item );
  1509. delSelection.pop_front();
  1510. }
  1511. }
  1512. mSelected.clear();
  1513. mSelectedItems.clear();
  1514. Con::executef( this, 1, "onObjectDeleteCompleted");
  1515. }
  1516. //------------------------------------------------------------------------------
  1517. // keyboard movement of items is restricted to just one item at a time
  1518. // if more than one item is selected then movement operations are not performed
  1519. bool GuiTreeViewCtrl::onKeyDown( const GuiEvent& event )
  1520. {
  1521. if ( !mVisible || !mActive || !mAwake )
  1522. return true;
  1523. // All the keyboard functionality requires a selected item, so if none exists...
  1524. // Deal with enter and delete
  1525. if ( event.modifier == 0 )
  1526. {
  1527. if ( event.keyCode == KEY_RETURN )
  1528. {
  1529. if ( mAltConsoleCommand[0] )
  1530. Con::evaluate( mAltConsoleCommand );
  1531. return true;
  1532. }
  1533. if ( event.keyCode == KEY_DELETE && mDeleteObjectAllowed )
  1534. {
  1535. // Don't delete the root!
  1536. if (mSelectedItems.empty())
  1537. return true;
  1538. //this may be fighting with the world editor delete
  1539. deleteSelection();
  1540. return true;
  1541. }
  1542. //call a generic bit of script that will let the subclass know that a key was pressed
  1543. Con::executef(this, 3, "onKeyDown", Con::getIntArg(event.modifier), Con::getIntArg(event.keyCode));
  1544. }
  1545. // only do operations if only one item is selected
  1546. if ( mSelectedItems.empty() || (mSelectedItems.size() > 1))
  1547. return true;
  1548. Item* item = mSelectedItems.first();
  1549. if ( !item )
  1550. return true;
  1551. // The Alt key lets you move items around!
  1552. if ( mFlags.test(IsEditable) && event.modifier & SI_ALT )
  1553. {
  1554. switch ( event.keyCode )
  1555. {
  1556. case KEY_UP:
  1557. // Move us up.
  1558. if ( item->mPrevious )
  1559. {
  1560. moveItemUp( item->mId );
  1561. scrollVisible(item);
  1562. }
  1563. return true;
  1564. case KEY_DOWN:
  1565. // Move the item under us up.
  1566. if ( item->mNext )
  1567. {
  1568. moveItemUp( item->mNext->mId );
  1569. scrollVisible(item);
  1570. }
  1571. return true;
  1572. case KEY_LEFT:
  1573. if ( item->mParent )
  1574. {
  1575. if ( item->mParent->mParent )
  1576. {
  1577. // Ok, we have both an immediate parent, and a grandparent.
  1578. // The goal of left-arrow alt is to become the child of our
  1579. // grandparent, ie, to become a sibling of our parent.
  1580. // First, unlink item from its siblings.
  1581. if ( item->mPrevious )
  1582. item->mPrevious->mNext = item->mNext;
  1583. else
  1584. item->mParent->mChild = item->mNext;
  1585. if ( item->mNext )
  1586. item->mNext->mPrevious = item->mPrevious;
  1587. // Now, relink as the next sibling of our parent.
  1588. item->mPrevious = item->mParent;
  1589. item->mNext = item->mParent->mNext;
  1590. // If there was already a next sibling, deal with that case.
  1591. if ( item->mNext )
  1592. item->mNext->mPrevious = item;
  1593. item->mParent->mNext = item;
  1594. // Snag the current parent set if any...
  1595. SimSet *parentSet = NULL;
  1596. if(item->mParent->isInspectorData())
  1597. parentSet = dynamic_cast<SimSet*>(item->mParent->getObject());
  1598. else
  1599. {
  1600. // parent is probably script data so we search up the tree for a
  1601. // set to put our object in
  1602. Item * temp = item->mParent;
  1603. while (!temp->isInspectorData())
  1604. temp = temp->mParent;
  1605. // found a ancestor who is an inspectorData
  1606. if (temp->isInspectorData())
  1607. parentSet = dynamic_cast<SimSet*>(temp->getObject());
  1608. else parentSet = NULL;
  1609. }
  1610. // Get our active SimObject if any
  1611. SimObject *simObj = NULL;
  1612. if(item->isInspectorData())
  1613. simObj = item->getObject();
  1614. // Remove from the old parentset...
  1615. if(simObj && parentSet) {
  1616. if (parentSet->size()>0)
  1617. {
  1618. SimObject *lastObject = parentSet->last();
  1619. parentSet->removeObject(simObj);
  1620. parentSet->reOrder(lastObject);
  1621. } else
  1622. parentSet->removeObject(simObj);
  1623. }
  1624. // And finally, update our item
  1625. item->mParent = item->mParent->mParent;
  1626. // Snag the newparent set if any...
  1627. SimSet *newParentSet = NULL;
  1628. if(item->mParent->isInspectorData())
  1629. newParentSet = dynamic_cast<SimSet*>(item->mParent->getObject());
  1630. else
  1631. {
  1632. // parent is probably script data so we search up the tree for a
  1633. // set to put our object in
  1634. Item * temp = item->mParent;
  1635. while (!temp->isInspectorData())
  1636. temp = temp->mParent;
  1637. // found a ancestor who is an inspectorData
  1638. if (temp->isInspectorData())
  1639. newParentSet = dynamic_cast<SimSet*>(temp->getObject());
  1640. else newParentSet = NULL;
  1641. }
  1642. if(simObj && newParentSet)
  1643. {
  1644. newParentSet->addObject(simObj);
  1645. Item * temp = item->mNext;
  1646. // item->mNext may be script, so find an inspector item to reorder with if any
  1647. if (temp) {
  1648. do {
  1649. if (temp->isInspectorData())
  1650. break;
  1651. temp = temp->mNext;
  1652. } while (temp);
  1653. if (temp && item->getObject() && temp->getObject()) //do we still have a item->mNext? If not then don't bother reordering
  1654. newParentSet->reOrder(item->getObject(), temp->getObject());
  1655. }
  1656. } else if (!simObj&&newParentSet) {
  1657. // our current item is script data. but it may have children who
  1658. // is inspector data who need an updated set
  1659. if (item->mChild)
  1660. inspectorSearch(item->mChild, item, parentSet, newParentSet);
  1661. }
  1662. // And update everything hurrah.
  1663. buildVisibleTree();
  1664. scrollVisible(item);
  1665. }
  1666. }
  1667. return true;
  1668. case KEY_RIGHT:
  1669. if ( item->mPrevious )
  1670. {
  1671. // Make the item the last child of its previous sibling.
  1672. // First, unlink from the current position in the list
  1673. item->mPrevious->mNext = item->mNext;
  1674. if ( item->mNext )
  1675. item->mNext->mPrevious = item->mPrevious;
  1676. // Get the object we're poking with.
  1677. SimObject *simObj = NULL;
  1678. SimSet *parentSet = NULL;
  1679. if(item->isInspectorData())
  1680. simObj = item->getObject();
  1681. if(item->mParent->isInspectorData())
  1682. parentSet = dynamic_cast<SimSet*>(item->mParent->getObject());
  1683. else {
  1684. // parent is probably script data so we search up the tree for a
  1685. // set to put our object in
  1686. Item * temp = item->mParent;
  1687. while (!temp->isInspectorData())
  1688. temp = temp->mParent;
  1689. // found an ancestor who is an inspectorData
  1690. if (temp->isInspectorData())
  1691. parentSet = dynamic_cast<SimSet*>(temp->getObject());
  1692. }
  1693. // If appropriate, remove from the current SimSet.
  1694. if(parentSet && simObj) {
  1695. if (parentSet->size()>0)
  1696. {
  1697. SimObject *lastObject = parentSet->last();
  1698. parentSet->removeObject(simObj);
  1699. parentSet->reOrder(lastObject);
  1700. } else
  1701. parentSet->removeObject(simObj);
  1702. }
  1703. // Now, make our previous sibling our parent...
  1704. item->mParent = item->mPrevious;
  1705. item->mNext = NULL;
  1706. // And sink us down to the end of its siblings, if appropriate.
  1707. if ( item->mParent->mChild )
  1708. {
  1709. Item* temp = item->mParent->mChild;
  1710. while ( temp->mNext )
  1711. temp = temp->mNext;
  1712. temp->mNext = item;
  1713. item->mPrevious = temp;
  1714. }
  1715. else
  1716. {
  1717. // only child...<sniff>
  1718. item->mParent->mChild = item;
  1719. item->mPrevious = NULL;
  1720. }
  1721. // Make sure the new parent is expanded:
  1722. if ( !item->mParent->mState.test( Item::Expanded ) )
  1723. setItemExpanded( item->mParent->mId, true );
  1724. // Snag the new parent simset if any.
  1725. SimSet *newParentSet = NULL;
  1726. // new parent might be script. so figure out what set we need to add it to.
  1727. if(item->mParent->isInspectorData())
  1728. newParentSet = dynamic_cast<SimSet*>(item->mParent->getObject());
  1729. else
  1730. {
  1731. // parent is probably script data so we search up the tree for a
  1732. // set to put our object in
  1733. if (mDebug) Con::printf("oh nos my parent is script!");
  1734. Item * temp = item->mParent;
  1735. while (!temp->isInspectorData())
  1736. temp = temp->mParent;
  1737. // found a ancestor who is an inspectorData
  1738. if (temp->isInspectorData())
  1739. newParentSet = dynamic_cast<SimSet*>(temp->getObject());
  1740. else newParentSet = NULL;
  1741. }
  1742. // Add the item's SimObject to the new parent simset, at the end.
  1743. if(newParentSet && simObj)
  1744. newParentSet->addObject(simObj);
  1745. else if (!simObj&&newParentSet&&parentSet) {
  1746. // our current item is script data. but it may have children who
  1747. // is inspector data who need an updated set
  1748. if (item->mChild) {
  1749. inspectorSearch(item->mChild, item, parentSet, newParentSet);
  1750. }
  1751. }
  1752. scrollVisible(item);
  1753. }
  1754. return true;
  1755. }
  1756. }
  1757. // Explorer-esque navigation...
  1758. switch( event.keyCode )
  1759. {
  1760. case KEY_UP:
  1761. // Select previous visible item:
  1762. if ( item->mPrevious )
  1763. {
  1764. item = item->mPrevious;
  1765. while ( item->isParent() && item->isExpanded() )
  1766. {
  1767. item = item->mChild;
  1768. while ( item->mNext )
  1769. item = item->mNext;
  1770. }
  1771. setItemSelected(mSelectedItems.first()->mId,false);
  1772. setItemSelected( item->mId, true );
  1773. scrollVisible(item);
  1774. return true;
  1775. }
  1776. // or select parent:
  1777. if ( item->mParent )
  1778. {
  1779. setItemSelected(mSelectedItems.first()->mId,false);
  1780. setItemSelected( item->mParent->mId, true );
  1781. scrollVisible(item->mParent);
  1782. return true;
  1783. }
  1784. return false;
  1785. break;
  1786. case KEY_DOWN:
  1787. // Selected child if it is visible:
  1788. if ( item->isParent() && item->isExpanded() )
  1789. {
  1790. setItemSelected(mSelectedItems.first()->mId,false);
  1791. setItemSelected( item->mChild->mId, true );
  1792. scrollVisible(item->mChild);
  1793. return true;
  1794. }
  1795. // or select next sibling (recursively):
  1796. do
  1797. {
  1798. if ( item->mNext )
  1799. {
  1800. setItemSelected(mSelectedItems.first()->mId,false);
  1801. setItemSelected(item->mNext->mId, true );
  1802. scrollVisible(item->mNext);
  1803. return true;
  1804. }
  1805. item = item->mParent;
  1806. } while ( item );
  1807. return false;
  1808. break;
  1809. case KEY_LEFT:
  1810. // Contract current menu:
  1811. if ( item->isExpanded() )
  1812. {
  1813. setItemExpanded( item->mId, false );
  1814. scrollVisible(item);
  1815. return true;
  1816. }
  1817. // or select parent:
  1818. if ( item->mParent )
  1819. {
  1820. setItemSelected(mSelectedItems.first()->mId,false);
  1821. setItemSelected( item->mParent->mId, true );
  1822. scrollVisible(item->mParent);
  1823. return true;
  1824. }
  1825. return false;
  1826. break;
  1827. case KEY_RIGHT:
  1828. // Expand selected item:
  1829. if ( item->isParent() )
  1830. {
  1831. if ( !item->isExpanded() )
  1832. {
  1833. setItemExpanded( item->mId, true );
  1834. scrollVisible(item);
  1835. return true;
  1836. }
  1837. // or select child:
  1838. setItemSelected(mSelectedItems.first()->mId,false);
  1839. setItemSelected( item->mChild->mId, true );
  1840. scrollVisible(item->mChild);
  1841. return true;
  1842. }
  1843. return false;
  1844. break;
  1845. }
  1846. // Not processed, so pass the event on:
  1847. return Parent::onKeyDown( event );
  1848. }
  1849. //------------------------------------------------------------------------------
  1850. // on mouse up look at the current item and check to see if it is valid
  1851. // to move the selected item(s) to it.
  1852. void GuiTreeViewCtrl::onTouchUp(const GuiEvent &event)
  1853. {
  1854. if( !mActive || !mAwake || !mVisible )
  1855. return;
  1856. mouseUnlock();
  1857. if ( mSelectedItems.empty())
  1858. {
  1859. mDragMidPoint = NomDragMidPoint;
  1860. return;
  1861. }
  1862. if (!mMouseDragged)
  1863. return;
  1864. else
  1865. mMouseDragged = false;
  1866. Item* newItem = NULL;
  1867. Item* newItem2 = NULL;
  1868. if (mFlags.test(IsEditable))
  1869. {
  1870. Parent::onTouchMove( event );
  1871. if (mOldDragY != mMouseOverCell.y)
  1872. {
  1873. mOldDragY = mMouseOverCell.y;
  1874. BitSet32 hitFlags = 0;
  1875. if ( !hitTest( event.mousePoint, newItem2, hitFlags ) )
  1876. {
  1877. mDragMidPoint = NomDragMidPoint;
  1878. return;
  1879. }
  1880. newItem2->mState.clear(Item::MouseOverBmp | Item::MouseOverText );
  1881. // if the newItem isn't in the mSelectedItemList then continue.
  1882. Vector<Item *>::iterator k;
  1883. for(k = mSelectedItems.begin(); k != mSelectedItems.end(); k++)
  1884. {
  1885. newItem = newItem2;
  1886. if (*(k) == newItem)
  1887. {
  1888. mDragMidPoint = NomDragMidPoint;
  1889. return;
  1890. }
  1891. Item * temp = *(k);
  1892. Item * grandpaTemp = newItem->mParent;
  1893. // grandpa check, kick out if an item would be its own ancestor
  1894. while (grandpaTemp)
  1895. {
  1896. if (temp == grandpaTemp)
  1897. {
  1898. if (mDebug)
  1899. {
  1900. Con::printf("grandpa check");
  1901. if (temp->isInspectorData())
  1902. Con::printf("temp's name: %s",temp->getObject()->getName());
  1903. if (grandpaTemp->isInspectorData())
  1904. Con::printf("grandpa's name: %s",grandpaTemp->getObject()->getName());
  1905. }
  1906. mDragMidPoint = NomDragMidPoint;
  1907. return;
  1908. }
  1909. grandpaTemp = grandpaTemp->mParent;
  1910. }
  1911. }
  1912. for (S32 i = 0; i <mSelectedItems.size();i++)
  1913. {
  1914. newItem = newItem2;
  1915. Item * item = mSelectedItems[i];
  1916. if (mDebug) Con::printf("----------------------------");
  1917. // clear old highlighting of the item
  1918. item->mState.clear(Item::MouseOverBmp | Item::MouseOverText );
  1919. // move the selected item to the newItem
  1920. Item* oldParent = item->mParent;
  1921. // Snag the current parent set if any for future reference
  1922. SimSet *parentSet = NULL;
  1923. if(oldParent->isInspectorData())
  1924. parentSet = dynamic_cast<SimSet*>(oldParent->getObject());
  1925. else
  1926. {
  1927. // parent is probably script data so we search up the tree for a
  1928. // set to put our object in
  1929. Item * temp = oldParent;
  1930. while (temp)
  1931. {
  1932. if (temp->isInspectorData())
  1933. break;
  1934. temp = temp->mParent;
  1935. }
  1936. // found an ancestor who is an inspectorData
  1937. if (temp)
  1938. {
  1939. if (temp->isInspectorData())
  1940. parentSet = dynamic_cast<SimSet*>(temp->getObject());
  1941. }
  1942. }
  1943. // unlink from the current position in the list
  1944. unlinkItem(item);
  1945. // update the parent's children
  1946. // check if we an only child
  1947. if (item->mParent->mChild == item)
  1948. {
  1949. if (item->mNext)
  1950. item->mParent->mChild = item->mNext;
  1951. else
  1952. item->mParent->mChild = NULL;
  1953. }
  1954. if (mDragMidPoint != NomDragMidPoint)
  1955. {
  1956. //if it is below an expanded tree, place as last item in the tree
  1957. //if it is below a parent who isn't expanded put below it
  1958. // position the item above or below another item
  1959. if (mDragMidPoint == AbovemDragMidPoint)
  1960. {
  1961. // easier to treat everything as "Below the mDragMidPoint" so make some adjustments
  1962. if (mDebug) Con::printf("adding item above mDragMidPoint");
  1963. // above the mid point of an item, so grab either the parent
  1964. // or the previous sibling
  1965. // does the item have a previous sibling?
  1966. if (newItem->mPrevious)
  1967. {
  1968. newItem = newItem->mPrevious;
  1969. if (mDebug) Con::printf("treating as if below an item that isn't expanded");
  1970. // otherwise add below that item as a sibling
  1971. item->mParent = newItem->mParent;
  1972. item->mPrevious = newItem;
  1973. item->mNext = newItem->mNext;
  1974. if (newItem->mNext)
  1975. newItem->mNext->mPrevious = item;
  1976. newItem->mNext = item;
  1977. }
  1978. else
  1979. {
  1980. if (mDebug) Con::printf("treating as if adding below the parent of the item");
  1981. // instead we add as the first item below the newItem's parent
  1982. item->mParent = newItem->mParent;
  1983. item->mNext = newItem;
  1984. item->mPrevious = NULL;
  1985. newItem->mPrevious = item;
  1986. item->mParent->mChild = item;
  1987. }
  1988. }
  1989. else if (mDragMidPoint == BelowmDragMidPoint)
  1990. {
  1991. if ((newItem->isParent())&&(newItem->isExpanded()))
  1992. {
  1993. if (mDebug) Con::printf("adding item to an expanded parent below the mDragMidPoint");
  1994. item->mParent = newItem;
  1995. // then add the new item as a child
  1996. item->mNext = newItem->mChild;
  1997. if (newItem->mChild)
  1998. newItem->mChild->mPrevious = item;
  1999. item->mParent->mChild = item;
  2000. item->mPrevious = NULL;
  2001. }
  2002. else if ((!newItem->mNext)&&(newItem->mParent)&&(newItem->mParent->mParent))
  2003. {
  2004. // add below it's parent.
  2005. if (mDebug) Con::printf("adding below a tree");
  2006. item->mParent = newItem->mParent->mParent;
  2007. item->mNext = newItem->mParent->mNext;
  2008. item->mPrevious = newItem->mParent;
  2009. if (newItem->mParent->mNext)
  2010. newItem->mParent->mNext->mPrevious = item;
  2011. newItem->mParent->mNext = item;
  2012. }
  2013. else
  2014. {
  2015. // adding below item not as a child
  2016. if (mDebug) Con::printf("adding item below the mDragMidPoint of an item");
  2017. item->mParent = newItem->mParent;
  2018. // otherwise the item is a sibling
  2019. if (newItem->mNext)
  2020. newItem->mNext->mPrevious = item;
  2021. item->mNext = newItem->mNext;
  2022. item->mPrevious = newItem;
  2023. newItem->mNext = item;
  2024. }
  2025. }
  2026. }
  2027. // if we're not allowed to add to items, then try to add to the parent of the hit item.
  2028. // if we are, just add to the item we hit.
  2029. else
  2030. {
  2031. if (mDebug)
  2032. {
  2033. if (item->isInspectorData() && item->getObject())
  2034. Con::printf("Item: %i",item->getObject()->getId());
  2035. if (newItem->isInspectorData() && newItem->getObject())
  2036. Con::printf("Parent: %i",newItem->getObject()->getId());
  2037. Con::printf("dragged onto an item");
  2038. }
  2039. // if the hit item is not already a group, and we're not allowed to drag to items,
  2040. // then try to add to the parent.
  2041. if(!mDragToItemAllowed && !newItem->isParent())
  2042. {
  2043. // add to the item's parent.
  2044. if(!newItem->mParent || !newItem->mParent->isParent())
  2045. {
  2046. if(mDebug)
  2047. Con::printf("could not find the parent of that item. dragging to an item is not allowed, kicking out.");
  2048. mDragMidPoint = NomDragMidPoint;
  2049. return;
  2050. }
  2051. newItem = newItem->mParent;
  2052. }
  2053. // new parent is the item in the current cell
  2054. item->mParent = newItem;
  2055. // adjust children if any
  2056. if (newItem->mChild)
  2057. {
  2058. if (mDebug) Con::printf("not the first child");
  2059. // put it at the top of the list (easier to find if there are many children)
  2060. if (newItem->mChild)
  2061. newItem->mChild->mPrevious = item;
  2062. item->mNext = newItem->mChild;
  2063. newItem->mChild = item;
  2064. item->mPrevious = NULL;
  2065. }
  2066. else
  2067. {
  2068. if (mDebug) Con::printf("first child");
  2069. // only child
  2070. newItem->mChild = item;
  2071. item->mNext = NULL;
  2072. item->mPrevious = NULL;
  2073. }
  2074. }
  2075. // expand the item we added to, if it isn't expanded already
  2076. if ( !item->mParent->mState.test( Item::Expanded ) )
  2077. setItemExpanded( item->mParent->mId, true );
  2078. //----------------------------------------------------------------
  2079. // handle objects
  2080. // Get our active SimObject if any
  2081. SimObject *simObj = NULL;
  2082. if(item->isInspectorData())
  2083. {
  2084. simObj = item->getObject();
  2085. }
  2086. // Remove from the old parentset
  2087. if((simObj && parentSet)&&(oldParent != item->mParent))
  2088. {
  2089. if (mDebug) Con::printf("removing item from old parentset");
  2090. // hack to get around the way removeObject takes the last item of the set
  2091. // and moves it into the place of the object we removed
  2092. if (parentSet->size()>0)
  2093. {
  2094. SimObject *lastObject = parentSet->last();
  2095. parentSet->removeObject(simObj);
  2096. parentSet->reOrder(lastObject);
  2097. }
  2098. else
  2099. {
  2100. parentSet->removeObject(simObj);
  2101. }
  2102. }
  2103. // Snag the newparent set if any...
  2104. SimSet *newParentSet = NULL;
  2105. if(item->mParent->isInspectorData())
  2106. {
  2107. if (mDebug) Con::printf("getting a new parent set");
  2108. SimObject * tmpObj = item->mParent->getObject();
  2109. newParentSet = dynamic_cast<SimSet*>(tmpObj);
  2110. }
  2111. else
  2112. {
  2113. // parent is probably script data so we search up the tree for a
  2114. // set to put our object in
  2115. if (mDebug) Con::printf("oh nos my parent is script!");
  2116. Item * temp = item->mParent;
  2117. while (temp)
  2118. {
  2119. if (temp->isInspectorData())
  2120. break;
  2121. temp = temp->mParent;
  2122. }
  2123. // found a ancestor who is an inspectorData
  2124. if (temp)
  2125. {
  2126. if (temp->isInspectorData())
  2127. newParentSet = dynamic_cast<SimSet*>(temp->getObject());
  2128. }
  2129. else
  2130. {
  2131. newParentSet = NULL;
  2132. }
  2133. }
  2134. if(simObj && newParentSet)
  2135. {
  2136. if (mDebug) Con::printf("simobj and new ParentSet");
  2137. if (oldParent != item->mParent)
  2138. newParentSet->addObject(simObj);
  2139. //order the objects in the simset according to their
  2140. //order in the tree view control
  2141. if(!item->mNext)
  2142. {
  2143. if(!item->mPrevious) break;
  2144. //bring to the end of the set
  2145. SimObject *prevObject = item->mPrevious->getObject();
  2146. if (prevObject && item->getObject())
  2147. {
  2148. newParentSet->reOrder(item->getObject(), prevObject);
  2149. }
  2150. }
  2151. else
  2152. {
  2153. //reorder within the set
  2154. SimObject *nextObject = item->mNext->getObject();
  2155. if(nextObject && item->getObject())
  2156. {
  2157. newParentSet->reOrder(item->getObject(), nextObject);
  2158. }
  2159. }
  2160. }
  2161. else if (!simObj&&newParentSet)
  2162. {
  2163. // our current item is script data. but it may have children who
  2164. // is inspector data who need an updated set
  2165. if (mDebug) Con::printf("no simobj but new parentSet");
  2166. if (item->mChild)
  2167. inspectorSearch(item->mChild, item, parentSet, newParentSet);
  2168. }
  2169. else if (simObj&&!newParentSet)
  2170. {
  2171. if (mDebug) Con::printf("simobject and no new parent set");
  2172. }
  2173. else
  2174. if (mDebug) Con::printf("no simobject and no new parent set");
  2175. }
  2176. // And update everything.
  2177. scrollVisible(newItem);
  2178. }
  2179. }
  2180. mDragMidPoint = NomDragMidPoint;
  2181. }
  2182. //------------------------------------------------------------------------------
  2183. void GuiTreeViewCtrl::onTouchDragged(const GuiEvent &event)
  2184. {
  2185. if(!mSupportMouseDragging) return;
  2186. if( !mActive || !mAwake || !mVisible )
  2187. return;
  2188. if (mSelectedItems.size() == 0)
  2189. return;
  2190. Point2I pt = globalToLocalCoord(event.mousePoint);
  2191. Parent::onTouchMove(event);
  2192. mouseLock();
  2193. mMouseDragged = true;
  2194. // whats our mDragMidPoint?
  2195. mCurrentDragCell = mMouseOverCell.y;
  2196. S32 midpCell = mCurrentDragCell * mItemHeight + (mItemHeight/2);
  2197. S32 currentY = pt.y;
  2198. S32 yDiff = currentY-midpCell;
  2199. S32 variance = (mItemHeight/5);
  2200. if (mPreviousDragCell >= 0)
  2201. mVisibleItems[mPreviousDragCell]->mState.clear( Item::MouseOverBmp | Item::MouseOverText );
  2202. if (mAbs(yDiff) > variance)
  2203. {
  2204. //above or below an item?
  2205. if (yDiff < 0)
  2206. mDragMidPoint = AbovemDragMidPoint;
  2207. else
  2208. mDragMidPoint = BelowmDragMidPoint;
  2209. } else
  2210. {
  2211. mDragMidPoint = NomDragMidPoint;
  2212. // highlight the current item
  2213. // hittest to detect whether we are on an item
  2214. // ganked from onMouseMouse
  2215. // used for tracking what our last cell was so we can clear it.
  2216. mPreviousDragCell = mCurrentDragCell;
  2217. if (mCurrentDragCell >= 0)
  2218. {
  2219. Item* item = NULL;
  2220. BitSet32 hitFlags = 0;
  2221. if ( !hitTest( event.mousePoint, item, hitFlags ) )
  2222. return;
  2223. if ( hitFlags.test( OnImage ) )
  2224. item->mState.set( Item::MouseOverBmp );
  2225. if ( hitFlags.test( OnText ))
  2226. item->mState.set( Item::MouseOverText );
  2227. // Always redraw the entire mouse over item, since we are distinguishing
  2228. // between the bitmap and the text:
  2229. setUpdateRegion( Point2I( mMouseOverCell.x * mCellSize.x, mMouseOverCell.y * mCellSize.y ), mCellSize );
  2230. }
  2231. }
  2232. }
  2233. void GuiTreeViewCtrl::onMiddleMouseDown(const GuiEvent & event)
  2234. {
  2235. //for debugging items
  2236. if (mDebug) {
  2237. Item* item;
  2238. BitSet32 hitFlags = 0;
  2239. hitTest( event.mousePoint, item, hitFlags );
  2240. Con::printf("debugging %d", item->mId);
  2241. Point2I pt = globalToLocalCoord(event.mousePoint);
  2242. if (item->isInspectorData() && item->getObject()) {
  2243. Con::printf("object data:");
  2244. Con::printf("name:%s",item->getObject()->getName());
  2245. Con::printf("className:%s",item->getObject()->getClassName());
  2246. }
  2247. Con::printf("contents of mSelectedItems:");
  2248. for(S32 i = 0; i < mSelectedItems.size(); i++) {
  2249. if (mSelectedItems[i]->isInspectorData()) {
  2250. Con::printf("%d",mSelectedItems[i]->getObject()->getId());
  2251. } else
  2252. Con::printf("wtf %d", mSelectedItems[i]);
  2253. }
  2254. Con::printf("contents of mSelected");
  2255. for (S32 j = 0; j < mSelected.size(); j++) {
  2256. Con::printf("%d", mSelected[j]);
  2257. }
  2258. S32 mCurrentDragCell = mMouseOverCell.y;
  2259. S32 midpCell = (mCurrentDragCell) * mItemHeight + (mItemHeight/2);
  2260. S32 currentY = pt.y;
  2261. S32 yDiff = currentY-midpCell;
  2262. 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);
  2263. }
  2264. }
  2265. void GuiTreeViewCtrl::onTouchDown(const GuiEvent & event)
  2266. {
  2267. if( !mActive || !mAwake || !mVisible )
  2268. {
  2269. Parent::onTouchDown(event);
  2270. return;
  2271. }
  2272. if ( mProfile->mCanKeyFocus )
  2273. setFirstResponder();
  2274. Item * item = 0;
  2275. BitSet32 hitFlags;
  2276. mOldDragY = 0;
  2277. mDragMidPoint = NomDragMidPoint;
  2278. //
  2279. if(!hitTest(event.mousePoint, item, hitFlags))
  2280. return;
  2281. //
  2282. if(event.modifier & SI_CTRL)
  2283. {
  2284. bool selectFlag = item->mState.test(Item::Selected);
  2285. if (selectFlag == true)
  2286. {
  2287. // already selected, so unselect it and remove it
  2288. removeSelection(item->mId);
  2289. if (item->isInspectorData() && item->getObject())
  2290. Con::executef(this,2,"onRemoveSelection",Con::getIntArg(item->getObject()->getId()));
  2291. } else
  2292. {
  2293. // otherwise select it and add it to the list
  2294. // check if it is already on the list.
  2295. /*bool newSelection = true;
  2296. for (S32 i = 0; i < mSelectedItems.size(); i++) {
  2297. if (mSelectedItems[i] == item) {
  2298. newSelection = false;
  2299. }
  2300. }*/
  2301. //if (newSelection) {
  2302. addSelection(item->mId);
  2303. if (item->isInspectorData() && item->getObject())
  2304. Con::executef(this,2,"onAddSelection",Con::getIntArg(item->getObject()->getId()));
  2305. //}
  2306. }
  2307. }
  2308. else if (event.modifier & SI_SHIFT)
  2309. {
  2310. // is something already selected?
  2311. S32 firstSelectedIndex = 0;
  2312. Item * firstItem = NULL;
  2313. if (!mSelectedItems.empty())
  2314. {
  2315. firstItem = mSelectedItems.front();
  2316. for (S32 i = 0; i < mVisibleItems.size();i++)
  2317. {
  2318. if (mVisibleItems[i] == mSelectedItems.front())
  2319. {
  2320. firstSelectedIndex = i;
  2321. break;
  2322. }
  2323. }
  2324. S32 mCurrentDragCell = mMouseOverCell.y;
  2325. if (mVisibleItems[firstSelectedIndex] != firstItem )
  2326. {
  2327. /*
  2328. Con::printf("something isn't right...");
  2329. if (mVisibleItems[firstSelectedIndex]->isInspectorData())
  2330. Con::printf("visibleItem %s",mVisibleItems[firstSelectedIndex]->getObject()->getName());
  2331. if (firstItem->isInspectorData())
  2332. Con::printf("firstItem %s",firstItem->getObject()->getName());
  2333. */
  2334. }
  2335. else
  2336. {
  2337. // select the cells
  2338. if ((mCurrentDragCell) < firstSelectedIndex)
  2339. {
  2340. //select up
  2341. for (S32 j = (mCurrentDragCell); j < firstSelectedIndex; j++) {
  2342. //if the item isn't already selected, then select it
  2343. bool newSelection = true;
  2344. Vector<Item *>::iterator k;
  2345. for(k = mSelectedItems.begin(); k != mSelectedItems.end(); k++)
  2346. {
  2347. if (mVisibleItems[j] == *(k)){
  2348. newSelection = false;
  2349. break;
  2350. }
  2351. }
  2352. if (newSelection)
  2353. {
  2354. addSelection(mVisibleItems[j]->mId);
  2355. if (mVisibleItems[j]->isInspectorData())
  2356. Con::executef(this,2,"onAddSelection",Con::getIntArg(mVisibleItems[j]->getObject()->getId()));
  2357. }
  2358. }
  2359. }
  2360. else
  2361. {
  2362. // select down
  2363. for (S32 j = firstSelectedIndex+1; j < (mCurrentDragCell+1); j++) {
  2364. bool newSelection = true;
  2365. Vector<Item *>::iterator k;
  2366. for(k = mSelectedItems.begin(); k != mSelectedItems.end(); k++) {
  2367. if (mVisibleItems[j] == *(k)){
  2368. newSelection = false;
  2369. break;
  2370. }
  2371. }
  2372. if (newSelection)
  2373. {
  2374. addSelection(mVisibleItems[j]->mId);
  2375. if (mVisibleItems[j]->isInspectorData())
  2376. Con::executef(this,2,"onAddSelection",Con::getIntArg(mVisibleItems[j]->getObject()->getId()));
  2377. }
  2378. }
  2379. }
  2380. }
  2381. }
  2382. }
  2383. else if (event.modifier & SI_ALT)
  2384. {
  2385. if (item->isInspectorData() && item->getObject())
  2386. {
  2387. setInstantGroup(item->getObject());
  2388. mInstantGroup = item->mId;
  2389. }
  2390. } else if (!hitFlags.test(OnImage))
  2391. {
  2392. // first check to see if the item is already selected
  2393. bool newSelection = true;
  2394. Vector<Item *>::iterator k;
  2395. for(k = mSelectedItems.begin(); k != mSelectedItems.end(); k++) {
  2396. if(*(k) == item)
  2397. {
  2398. newSelection = false;
  2399. break;
  2400. }
  2401. }
  2402. // if the item is not already selected then we have a
  2403. //newly selected item, so clear our list of selected items
  2404. if (newSelection)
  2405. {
  2406. clearSelection();
  2407. //Con::executef(this, 1, "onClearSelection");
  2408. //mSelectedItems.clear();
  2409. setItemSelected(item->mId,true);
  2410. }
  2411. }
  2412. if ( hitFlags.test( OnText ) && ( event.mouseClickCount > 1 ) && mAltConsoleCommand[0] )
  2413. Con::evaluate( mAltConsoleCommand );
  2414. if(!item->isParent())
  2415. return;
  2416. //
  2417. if ( mFullRowSelect || hitFlags.test( OnImage ) )
  2418. {
  2419. item->setExpanded(!item->isExpanded());
  2420. if( !item->isInspectorData() && item->mState.test(Item::VirtualParent) )
  2421. onVirtualParentExpand(item);
  2422. scrollVisible(item);
  2423. }
  2424. }
  2425. //------------------------------------------------------------------------------
  2426. void GuiTreeViewCtrl::onTouchMove( const GuiEvent &event )
  2427. {
  2428. if ( mMouseOverCell.y >= 0 && mVisibleItems.size() > mMouseOverCell.y)
  2429. mVisibleItems[mMouseOverCell.y]->mState.clear( Item::MouseOverBmp | Item::MouseOverText );
  2430. Parent::onTouchMove( event );
  2431. if ( mMouseOverCell.y >= 0 )
  2432. {
  2433. Item* item = NULL;
  2434. BitSet32 hitFlags = 0;
  2435. if ( !hitTest( event.mousePoint, item, hitFlags ) )
  2436. return;
  2437. if ( hitFlags.test( OnImage ) )
  2438. item->mState.set( Item::MouseOverBmp );
  2439. if ( hitFlags.test( OnText ))
  2440. item->mState.set( Item::MouseOverText );
  2441. // Always redraw the entire mouse over item, since we are distinguishing
  2442. // between the bitmap and the text:
  2443. setUpdateRegion( Point2I( mMouseOverCell.x * mCellSize.x, mMouseOverCell.y * mCellSize.y ), mCellSize );
  2444. }
  2445. }
  2446. //------------------------------------------------------------------------------
  2447. void GuiTreeViewCtrl::onTouchEnter( const GuiEvent &event )
  2448. {
  2449. Parent::onTouchEnter( event );
  2450. onTouchMove( event );
  2451. }
  2452. //------------------------------------------------------------------------------
  2453. void GuiTreeViewCtrl::onTouchLeave( const GuiEvent &event )
  2454. {
  2455. if ( mMouseOverCell.y >= 0 && mVisibleItems.size() > mMouseOverCell.y)
  2456. mVisibleItems[mMouseOverCell.y]->mState.clear( Item::MouseOverBmp | Item::MouseOverText );
  2457. Parent::onTouchLeave( event );
  2458. }
  2459. //------------------------------------------------------------------------------
  2460. void GuiTreeViewCtrl::onRightMouseDown(const GuiEvent & event)
  2461. {
  2462. if(!mActive)
  2463. {
  2464. Parent::onRightMouseDown(event);
  2465. return;
  2466. }
  2467. Item * item = NULL;
  2468. BitSet32 hitFlags;
  2469. //
  2470. if(!hitTest(event.mousePoint, item, hitFlags))
  2471. return;
  2472. //
  2473. char bufs[2][32];
  2474. dSprintf(bufs[0], 32, "%d", item->mId);
  2475. dSprintf(bufs[1], 32, "%d %d", event.mousePoint.x, event.mousePoint.y);
  2476. if (item->isInspectorData() && item->getObject())
  2477. Con::executef(this,4, "onRightMouseDown", bufs[0],bufs[1],Con::getIntArg(item->getObject()->getId()));
  2478. else
  2479. Con::executef(this, 3, "onRightMouseDown", bufs[0], bufs[1]);
  2480. }
  2481. //------------------------------------------------------------------------------
  2482. void GuiTreeViewCtrl::onRender(Point2I offset, const RectI &updateRect)
  2483. {
  2484. // Get all our contents drawn!
  2485. Parent::onRender(offset,updateRect);
  2486. // Deal with drawing the drag & drop line, if any...
  2487. dglSetClipRect(updateRect);
  2488. // only do it if we have a mDragMidPoint
  2489. if (mDragMidPoint == NomDragMidPoint || !mSupportMouseDragging )
  2490. return;
  2491. ColorF greyLine(0.5,0.5,0.5,1);
  2492. Point2F squarePt;
  2493. glLineWidth(2.f);
  2494. // draw mDragMidPoint lines with a diamond
  2495. if (mDragMidPoint == AbovemDragMidPoint)
  2496. {
  2497. S32 tempY = mItemHeight*mCurrentDragCell+offset.y ;
  2498. squarePt.y = (F32)tempY;
  2499. squarePt.x = (F32)(125.0f+offset.x);
  2500. dglDrawLine(0+offset.x, tempY, 250+offset.x, tempY,greyLine);
  2501. dglDraw2DSquare(squarePt, 6, 90 );
  2502. }
  2503. if (mDragMidPoint == BelowmDragMidPoint)
  2504. {
  2505. S32 tempY2 = mItemHeight*(mCurrentDragCell+1) +offset.y;
  2506. squarePt.y = (F32)tempY2;
  2507. squarePt.x = (F32)(125.0f+offset.x);
  2508. dglDrawLine(0+offset.x, tempY2, 250+offset.x, tempY2,greyLine);
  2509. dglDraw2DSquare(squarePt,6, 90 );
  2510. }
  2511. glLineWidth(1.f);
  2512. }
  2513. void GuiTreeViewCtrl::onRenderCell(Point2I offset, Point2I cell, bool, bool )
  2514. {
  2515. if( !mVisibleItems.size() )
  2516. return;
  2517. // Do some sanity checking and data retrieval.
  2518. AssertFatal(cell.y < mVisibleItems.size(), "GuiTreeViewCtrl::onRenderCell: invalid cell");
  2519. Item * item = mVisibleItems[cell.y];
  2520. // If there's no object, deal with it.
  2521. if(item->isInspectorData())
  2522. if(!item->getObject())
  2523. return;
  2524. RectI drawRect( offset, mCellSize );
  2525. dglClearBitmapModulation();
  2526. FrameAllocatorMarker txtBuff;
  2527. // Ok, we have the item. There are a few possibilities at this point:
  2528. // - We need to draw inheritance lines and a treeview-chosen icon
  2529. // OR
  2530. // - We have to draw an item-dependent icon
  2531. // - If we're mouseover, we have to highlight it.
  2532. //
  2533. // - We have to draw the text for the item
  2534. // - Taking into account various mouseover states
  2535. // - Taking into account the value (set or not)
  2536. // - If it's an inspector data, we have to do some custom rendering
  2537. // Ok, first draw the tab and icon.
  2538. // Do we draw the tree lines?
  2539. if(mFlags.test(ShowTreeLines))
  2540. {
  2541. drawRect.point.x += ( mTabSize * item->mTabLevel );
  2542. Item* parent = item->mParent;
  2543. for ( S32 i = item->mTabLevel; ( parent && i > 0 ); i-- )
  2544. {
  2545. drawRect.point.x -= mTabSize;
  2546. if ( parent->mNext )
  2547. dglDrawBitmapSR( mProfile->mTextureHandle, drawRect.point, mProfile->mBitmapArrayRects[BmpLine] );
  2548. parent = parent->mParent;
  2549. }
  2550. }
  2551. // Now, the icon...
  2552. drawRect.point.x = offset.x + mTabSize * item->mTabLevel;
  2553. // First, draw the rollover glow, if it's an inner node.
  2554. if ( item->isParent() && item->mState.test( Item::MouseOverBmp ) )
  2555. dglDrawBitmapSR( mProfile->mTextureHandle, drawRect.point, mProfile->mBitmapArrayRects[BmpGlow] );
  2556. // Now, do we draw a treeview-selected item or an item dependent one?
  2557. S32 newOffset = 0; // This is stored so we can render glow, then update render pos.
  2558. if(item->isInspectorData())
  2559. {
  2560. S32 bitmap = 0;
  2561. // Ok, draw the treeview lines as appropriate.
  2562. if ( !item->isParent() )
  2563. {
  2564. bitmap = item->mNext ? BmpChild : BmpLastChild;
  2565. }
  2566. else
  2567. {
  2568. bitmap = item->isExpanded() ? BmpExp : BmpCon;
  2569. if ( item->mParent || item->mPrevious )
  2570. bitmap += ( item->mNext ? 3 : 2 );
  2571. else
  2572. bitmap += ( item->mNext ? 1 : 0 );
  2573. }
  2574. if ( ( bitmap >= 0 ) && ( bitmap < mProfile->mBitmapArrayRects.size() ) )
  2575. {
  2576. dglDrawBitmapSR( mProfile->mTextureHandle, drawRect.point, mProfile->mBitmapArrayRects[bitmap] );
  2577. newOffset = mProfile->mBitmapArrayRects[bitmap].extent.x;
  2578. }
  2579. // draw lock icon if need be
  2580. S32 icon = Lock1;
  2581. S32 icon2 = Hidden;
  2582. if (item->getObject() && item->getObject()->isLocked())
  2583. {
  2584. if (mIconTable[icon])
  2585. {
  2586. //drawRect.point.x = offset.x + mTabSize * item->mTabLevel + mIconTable[icon].getWidth();
  2587. drawRect.point.x += mIconTable[icon].getWidth();
  2588. dglDrawBitmap( mIconTable[icon], drawRect.point, 0 );
  2589. }
  2590. }
  2591. if (item->getObject() && item->getObject()->isHidden())
  2592. {
  2593. if (mIconTable[icon2])
  2594. {
  2595. //drawRect.point.x = offset.x + mTabSize * item->mTabLevel + mIconTable[icon].getWidth();
  2596. drawRect.point.x += mIconTable[icon2].getWidth();
  2597. dglDrawBitmap( mIconTable[icon2], drawRect.point, 0 );
  2598. }
  2599. }
  2600. SimObject * pObject = item->getObject();
  2601. SimGroup * pGroup = ( pObject == NULL ) ? NULL : dynamic_cast<SimGroup*>( pObject );
  2602. // draw the icon associated with the item
  2603. if (item->isParent())
  2604. {
  2605. if ( pGroup != NULL)
  2606. {
  2607. if (item->isExpanded())
  2608. item->mIcon = SimGroup1;
  2609. else
  2610. item->mIcon = SimGroup2;
  2611. }
  2612. else
  2613. item->mIcon = SimGroup2;
  2614. }
  2615. if (mInstantGroup == item->mId)
  2616. {
  2617. if (item->isInspectorData())
  2618. {
  2619. if ( pGroup != NULL )
  2620. {
  2621. if (item->isExpanded())
  2622. item->mIcon = SimGroup3;
  2623. else
  2624. item->mIcon = SimGroup4;
  2625. }
  2626. }
  2627. }
  2628. if (item->mIcon)
  2629. {
  2630. if (mIconTable[item->mIcon])
  2631. {
  2632. S32 iconHeight = (mItemHeight - mIconTable[item->mIcon].getHeight()) / 2;
  2633. S32 oldHeight = drawRect.point.y;
  2634. if(iconHeight > 0)
  2635. drawRect.point.y += iconHeight;
  2636. drawRect.point.x += mIconTable[item->mIcon].getWidth();
  2637. dglDrawBitmap( mIconTable[item->mIcon], drawRect.point, 0 );
  2638. drawRect.point.y = oldHeight;
  2639. }
  2640. }
  2641. }
  2642. else
  2643. {
  2644. S32 bitmap = 0;
  2645. // Ok, draw the treeview lines as appropriate.
  2646. if ( !item->isParent() )
  2647. bitmap = item->mNext ? BmpChild : BmpLastChild;
  2648. else
  2649. {
  2650. bitmap = item->isExpanded() ? BmpExp : BmpCon;
  2651. if ( item->mParent || item->mPrevious )
  2652. bitmap += ( item->mNext ? 3 : 2 );
  2653. else
  2654. bitmap += ( item->mNext ? 1 : 0 );
  2655. }
  2656. if ( ( bitmap >= 0 ) && ( bitmap < mProfile->mBitmapArrayRects.size() ) )
  2657. {
  2658. dglDrawBitmapSR( mProfile->mTextureHandle, drawRect.point, mProfile->mBitmapArrayRects[bitmap] );
  2659. newOffset = mProfile->mBitmapArrayRects[bitmap].extent.x;
  2660. }
  2661. S32 icon = item->isExpanded() ? item->mScriptInfo.mExpandedImage : item->mScriptInfo.mNormalImage;
  2662. if ( icon )
  2663. {
  2664. if (mIconTable[icon])
  2665. {
  2666. S32 iconHeight = (mItemHeight - mIconTable[icon].getHeight()) / 2;
  2667. S32 oldHeight = drawRect.point.y;
  2668. if(iconHeight > 0)
  2669. drawRect.point.y += iconHeight;
  2670. drawRect.point.x += mIconTable[icon].getWidth();
  2671. dglDrawBitmap( mIconTable[icon], drawRect.point, 0 );
  2672. drawRect.point.y = oldHeight;
  2673. }
  2674. }
  2675. }
  2676. // Ok, update offset so we can render some text!
  2677. drawRect.point.x += newOffset;
  2678. // Ok, now we're off to rendering the actual data for the treeview item.
  2679. U32 bufLen = item->mDataRenderWidth + 1;
  2680. char *displayText = (char *)txtBuff.alloc(bufLen);
  2681. displayText[bufLen-1] = 0;
  2682. item->getDisplayText(bufLen, displayText);
  2683. // Draw the rollover/selected bitmap, if one was specified.
  2684. drawRect.extent.x = mProfile->getFont(mFontSizeAdjust)->getStrWidth( displayText ) + ( 2 * mTextOffset );
  2685. if ( item->mState.test( Item::Selected ) && mTexSelected )
  2686. dglDrawBitmapStretch( mTexSelected, drawRect );
  2687. else if ( item->mState.test( Item::MouseOverText ) && mTexRollover )
  2688. dglDrawBitmapStretch( mTexRollover, drawRect );
  2689. // Offset a bit so as to space text properly.
  2690. drawRect.point.x += mTextOffset;
  2691. // Determine what color the font should be.
  2692. ColorI fontColor;
  2693. fontColor = item->mState.test( Item::Selected ) ? mProfile->mFontColorSL :
  2694. ( item->mState.test( Item::MouseOverText ) ? mProfile->mFontColorHL : mProfile->mFontColor );
  2695. if (item->mState.test(Item::Selected))
  2696. {
  2697. dglDrawRectFill(drawRect, mProfile->mFillColorHL);
  2698. }
  2699. else if (item->mState.test(Item::MouseOverText))
  2700. {
  2701. dglDrawRectFill(drawRect, mProfile->mFontColorNA);
  2702. }
  2703. if( mInstantGroup == item->mId)
  2704. {
  2705. fontColor = mProfile->mFontColorHL;
  2706. }
  2707. dglSetBitmapModulation( fontColor );
  2708. // Center the text horizontally.
  2709. S32 height = (mItemHeight - mProfile->getFont(mFontSizeAdjust)->getHeight()) / 2;
  2710. if(height > 0)
  2711. drawRect.point.y += height;
  2712. // JDD - offset by two pixels or so to keep the text from rendering RIGHT ONTOP of the outline
  2713. drawRect.point.x += 2;
  2714. dglDrawText( mProfile->getFont(mFontSizeAdjust), drawRect.point, displayText, mProfile->mFontColors );
  2715. }
  2716. //------------------------------------------------------------------------------
  2717. void GuiTreeViewCtrl::clearSelection()
  2718. {
  2719. while (!mSelectedItems.empty())
  2720. {
  2721. if(!setItemSelected(mSelectedItems.last()->mId, false))
  2722. mSelectedItems.pop_back();
  2723. }
  2724. mSelectedItems.clear();
  2725. mSelected.clear();
  2726. Con::executef(this, 1, "onClearSelection");
  2727. }
  2728. void GuiTreeViewCtrl::lockSelection(bool lock)
  2729. {
  2730. for(U32 i = 0; i < (U32)mSelectedItems.size(); i++)
  2731. {
  2732. if(mSelectedItems[i]->isInspectorData())
  2733. mSelectedItems[i]->getObject()->setLocked(lock);
  2734. }
  2735. }
  2736. void GuiTreeViewCtrl::hideSelection(bool hide)
  2737. {
  2738. for (U32 i = 0; i < (U32)mSelectedItems.size(); i++)
  2739. {
  2740. if (mSelectedItems[i]->isInspectorData())
  2741. mSelectedItems[i]->getObject()->setHidden(hide);
  2742. }
  2743. }
  2744. //------------------------------------------------------------------------------
  2745. // handles icon assignments
  2746. S32 GuiTreeViewCtrl::getIcon(const char * iconString)
  2747. {
  2748. if( iconString == NULL )
  2749. return Default;
  2750. S32 icon = Default;
  2751. if (!dStrcmp(iconString, "SimGroup"))
  2752. icon = SimGroup1;
  2753. return icon;
  2754. }
  2755. void GuiTreeViewCtrl::addInspectorDataItem(Item *parent, SimObject *obj)
  2756. {
  2757. S32 icon = getIcon(obj->getClassName());
  2758. Item *item = createItem(icon);
  2759. item->mState.set(Item::InspectorData);
  2760. // Deal with child objects...
  2761. if(dynamic_cast<SimSet*>(obj))
  2762. item->mState.set(Item::VirtualParent);
  2763. // Actually store the data!
  2764. item->setObject(obj);
  2765. // Now add us to the data structure...
  2766. if(parent)
  2767. {
  2768. // Add as child of parent.
  2769. if(parent->mChild)
  2770. {
  2771. Item * traverse = parent->mChild;
  2772. while(traverse->mNext)
  2773. traverse = traverse->mNext;
  2774. traverse->mNext = item;
  2775. item->mPrevious = traverse;
  2776. }
  2777. else
  2778. parent->mChild = item;
  2779. item->mParent = parent;
  2780. }
  2781. else
  2782. {
  2783. // If no parent, add to root.
  2784. item->mNext = mRoot;
  2785. mRoot = item;
  2786. item->mParent = NULL;
  2787. }
  2788. if(!parent || parent->isExpanded())
  2789. mFlags.set(RebuildVisible);
  2790. buildVisibleTree();
  2791. }
  2792. void GuiTreeViewCtrl::unlinkItem(Item * item)
  2793. {
  2794. if (item->mPrevious)
  2795. item->mPrevious->mNext = item->mNext;
  2796. if (item->mNext)
  2797. item->mNext->mPrevious = item->mPrevious;
  2798. }
  2799. bool GuiTreeViewCtrl::childSearch(Item * item, SimObject *obj, bool yourBaby)
  2800. {
  2801. Item * temp = item->mChild;
  2802. while (temp)
  2803. {
  2804. //do you have my baby?
  2805. if (temp->isInspectorData())
  2806. {
  2807. if (temp->getObject() == obj)
  2808. yourBaby = false; //probably a child of an inner script
  2809. }
  2810. yourBaby = childSearch(temp,obj, yourBaby);
  2811. temp = temp->mNext;
  2812. }
  2813. return yourBaby;
  2814. }
  2815. void GuiTreeViewCtrl::inspectorSearch(Item * item, Item * parent, SimSet * parentSet, SimSet * newParentSet)
  2816. {
  2817. if (!parentSet||!newParentSet)
  2818. return;
  2819. if (item == parent->mNext)
  2820. return;
  2821. if (item)
  2822. {
  2823. if (item->isInspectorData())
  2824. {
  2825. // remove the object from the parentSet and add it to the newParentSet
  2826. SimObject* simObj = item->getObject();
  2827. if (parentSet->size())
  2828. {
  2829. SimObject *lastObject = parentSet->last();
  2830. parentSet->removeObject(simObj);
  2831. parentSet->reOrder(lastObject);
  2832. }
  2833. else
  2834. parentSet->removeObject(simObj);
  2835. newParentSet->addObject(simObj);
  2836. if (item->mNext)
  2837. {
  2838. inspectorSearch(item->mNext, parent, parentSet, newParentSet);
  2839. return;
  2840. }
  2841. else
  2842. {
  2843. // end of children so backing up
  2844. if (item->mParent == parent)
  2845. return;
  2846. else
  2847. {
  2848. inspectorSearch(item->mParent->mNext, parent, parentSet, newParentSet);
  2849. return;
  2850. }
  2851. }
  2852. }
  2853. if (item->mChild)
  2854. {
  2855. inspectorSearch(item->mChild, parent, parentSet, newParentSet);
  2856. return;
  2857. }
  2858. if (item->mNext)
  2859. {
  2860. inspectorSearch(item->mNext, parent, parentSet, newParentSet);
  2861. return;
  2862. }
  2863. }
  2864. }
  2865. bool GuiTreeViewCtrl::onVirtualParentBuild(Item *item, bool bForceFullUpdate)
  2866. {
  2867. if(!item->mState.test(Item::InspectorData))
  2868. return true;
  2869. // Blast an item if it doesn't have a corresponding SimObject...
  2870. if(item->mInspectorInfo.mObject == NULL)
  2871. {
  2872. removeItem(item->mId);
  2873. return false;
  2874. }
  2875. // Skip the next stuff unless we're expanded...
  2876. if(!item->isExpanded() && !bForceFullUpdate)
  2877. return true;
  2878. // Verify that we have all the kids we should in here...
  2879. SimSet *srcObj = dynamic_cast<SimSet*>(&(*item->mInspectorInfo.mObject));
  2880. // If it's not a SimSet... WTF are we doing here?
  2881. if(!srcObj)
  2882. return true;
  2883. SimSet::iterator i;
  2884. // This is slow but probably ok.
  2885. for(i = srcObj->begin(); i != srcObj->end(); i++)
  2886. {
  2887. SimObject *obj = *i;
  2888. // If we can't find it, add it.
  2889. // unless it has a parent that is a child that is a script
  2890. Item *res = item->findChildByValue(obj);
  2891. bool foundChild = true;
  2892. // search the children. if any of them are the parent of the object then don't add it.
  2893. foundChild = childSearch(item,obj,foundChild);
  2894. if(!res && foundChild)
  2895. {
  2896. if (mDebug) Con::printf("adding something");
  2897. addInspectorDataItem(item, obj);
  2898. }
  2899. }
  2900. return true;
  2901. }
  2902. bool GuiTreeViewCtrl::onVirtualParentExpand(Item *item)
  2903. {
  2904. // Do nothing...
  2905. return true;
  2906. }
  2907. bool GuiTreeViewCtrl::onVirtualParentCollapse(Item *item)
  2908. {
  2909. // Do nothing...
  2910. return true;
  2911. }
  2912. void GuiTreeViewCtrl::inspectObject(SimObject *obj, bool okToEdit)
  2913. {
  2914. destroyTree();
  2915. mFlags.set(IsEditable, okToEdit);
  2916. //build our icon table
  2917. const char * res = Con::executef(this, 1, "onDefineIcons");
  2918. if(!(dAtob(res)))
  2919. {
  2920. // if no icons were defined in script then use defaults.
  2921. buildIconTable(NULL);
  2922. }
  2923. addInspectorDataItem(NULL, obj);
  2924. }
  2925. S32 GuiTreeViewCtrl::findItemByName(const char *name)
  2926. {
  2927. for (S32 i = 0; i < mItems.size(); i++)
  2928. {
  2929. if (mItems[i] != NULL)
  2930. {
  2931. if (dStrcmp(mItems[i]->getText(),name) == 0)
  2932. return mItems[i]->mId;
  2933. }
  2934. }
  2935. return 0;
  2936. }
  2937. StringTableEntry GuiTreeViewCtrl::getTextToRoot( S32 itemId, const char * delimiter )
  2938. {
  2939. Item * item = getItem(itemId);
  2940. if(!item)
  2941. {
  2942. Con::errorf(ConsoleLogEntry::General, "GuiTreeViewCtrl::getTextToRoot: invalid start item id!");
  2943. return StringTable->EmptyString;
  2944. }
  2945. if(item->isInspectorData())
  2946. {
  2947. Con::errorf(ConsoleLogEntry::General, "GuiTreeViewCtrl::getTextToRoot: cannot get text to root of inspector data items");
  2948. return StringTable->EmptyString;
  2949. }
  2950. char bufferOne[1024];
  2951. char bufferTwo[1024];
  2952. char bufferNodeText[128];
  2953. dMemset( bufferOne, 0, sizeof(bufferOne) );
  2954. dMemset( bufferTwo, 0, sizeof(bufferTwo) );
  2955. dStrcpy( bufferOne, item->getText() );
  2956. Item *prevNode = item->mParent;
  2957. while ( prevNode )
  2958. {
  2959. dMemset( bufferNodeText, 0, sizeof(bufferNodeText) );
  2960. dStrcpy( bufferNodeText, prevNode->getText() );
  2961. dSprintf( bufferTwo, 1024, "%s%s%s",bufferNodeText, delimiter, bufferOne );
  2962. dStrcpy( bufferOne, bufferTwo );
  2963. dMemset( bufferTwo, 0, sizeof(bufferTwo) );
  2964. prevNode = prevNode->mParent;
  2965. }
  2966. // Return the result, StringTable-ized.
  2967. return StringTable->insert( bufferOne );
  2968. }
  2969. //------------------------------------------------------------------------------
  2970. ConsoleMethod(GuiTreeViewCtrl, findItemByName, S32, 3, 3, "(name) Find item by name\n"
  2971. "@param name The name of the desired object.\n"
  2972. "@return Returns the ID of the object, or -1 on failure (not found).")
  2973. {
  2974. return(object->findItemByName(argv[2]));
  2975. }
  2976. ConsoleMethod(GuiTreeViewCtrl, insertItem, S32, 4, 8, "(TreeItemId parent, name, value, icon, normalImage=0, expandedImage=0) Adds item to tree control.\n"
  2977. "@param parent The new item's parent.\n"
  2978. "@param name The name of the new item.\n"
  2979. "@param value The new item's value.\n"
  2980. "@param icon The new item's icon\n"
  2981. "@return Returns the new item's ID.")
  2982. {
  2983. S32 norm=0, expand=0;
  2984. if (argc > 6)
  2985. {
  2986. norm = dAtoi(argv[6]);
  2987. if(argc > 7)
  2988. expand = dAtoi(argv[7]);
  2989. }
  2990. return(object->insertItem(dAtoi(argv[2]), argv[3], argv[4], argv[5], norm, expand));
  2991. }
  2992. ConsoleMethod(GuiTreeViewCtrl, lockSelection, void, 2, 3, "([bool lock]) Set whether the selection is to be locked."
  2993. "@param lock Boolean flag for whether or not the current selected object should be locked\n"
  2994. "@return No return value.")
  2995. {
  2996. bool lock = true;
  2997. if(argc == 3)
  2998. lock = dAtob(argv[2]);
  2999. object->lockSelection(lock);
  3000. }
  3001. ConsoleMethod(GuiTreeViewCtrl, clearSelection, void, 2, 2, "() Clear selection\n"
  3002. "@return No return value.")
  3003. {
  3004. object->clearSelection();
  3005. }
  3006. ConsoleMethod(GuiTreeViewCtrl, deleteSelection, void, 2, 2, "() Delete all selected items.\n"
  3007. "@return No return value.\n")
  3008. {
  3009. object->deleteSelection();
  3010. }
  3011. ConsoleMethod(GuiTreeViewCtrl, addSelection, void, 3, 3, "(string ID) Select an item"
  3012. "@param ID The ID of the item to select.\n"
  3013. "@return No return value.")
  3014. {
  3015. S32 id = dAtoi(argv[2]);
  3016. object->addSelection(id);
  3017. }
  3018. ConsoleMethod(GuiTreeViewCtrl, addChildSelectionByValue, void, 4, 4, "(TreeItemId parent, value)")
  3019. {
  3020. S32 id = dAtoi(argv[2]);
  3021. GuiTreeViewCtrl::Item* parentItem = object->getItem(id);
  3022. if (parentItem)
  3023. {
  3024. GuiTreeViewCtrl::Item* child = parentItem->findChildByValue(argv[3]);
  3025. if (child)
  3026. object->addSelection(child->getID());
  3027. }
  3028. }
  3029. ConsoleMethod(GuiTreeViewCtrl, removeSelection, void, 3, 3, "(string ID) Deselects given item.\n"
  3030. "@param ID The ID of the item to deselect.\n"
  3031. "@return No return value.")
  3032. {
  3033. S32 id = dAtoi(argv[2]);
  3034. object->removeSelection(id);
  3035. }
  3036. ConsoleMethod(GuiTreeViewCtrl, removeChildSelectionByValue, void, 4, 4, "removeChildSelectionByValue(TreeItemId parent, value)")
  3037. {
  3038. S32 id = dAtoi(argv[2]);
  3039. GuiTreeViewCtrl::Item* parentItem = object->getItem(id);
  3040. if(parentItem)
  3041. {
  3042. GuiTreeViewCtrl::Item* child = parentItem->findChildByValue(argv[3]);
  3043. if(child)
  3044. object->removeSelection(child->getID());
  3045. }
  3046. }
  3047. ConsoleMethod(GuiTreeViewCtrl, selectItem, bool, 3, 4, "(TreeItemId item, [bool select=true]) Selects item.")
  3048. {
  3049. S32 id = dAtoi(argv[2]);
  3050. bool select = true;
  3051. if(argc == 4)
  3052. select = dAtob(argv[3]);
  3053. return(object->setItemSelected(id, select));
  3054. }
  3055. ConsoleMethod(GuiTreeViewCtrl, expandItem, bool, 3, 4, "(TreeItemId item, [bool expand=true]) Deselects item")
  3056. {
  3057. S32 id = dAtoi(argv[2]);
  3058. bool expand = true;
  3059. if(argc == 4)
  3060. expand = dAtob(argv[3]);
  3061. return(object->setItemExpanded(id, expand));
  3062. }
  3063. // Make the given item visible.
  3064. ConsoleMethod(GuiTreeViewCtrl, scrollVisible, void, 3, 3, "(TreeItemId item) Make the given item visible.\n"
  3065. "@param ID of the desired item.\n"
  3066. "@return No return value.")
  3067. {
  3068. object->scrollVisible(dAtoi(argv[2]));
  3069. }
  3070. ConsoleMethod(GuiTreeViewCtrl, buildIconTable, bool, 3,3, "(string icons) Icons should be designated by the bitmap/png file names (minus the file extensions) "
  3071. "and separated by colons (:). This list should be synchronized with the Icons enum.\n"
  3072. "@param icons The list of icons to add sepated by colons.\n"
  3073. "@return Returns true on success, false otherwise.")
  3074. {
  3075. const char * icons = argv[2];
  3076. return object->buildIconTable(icons);
  3077. }
  3078. 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.")
  3079. {
  3080. SimSet *treeRoot = NULL;
  3081. SimObject* target = Sim::findObject(argv[2]);
  3082. bool okToEdit = true;
  3083. if (argc == 4)
  3084. okToEdit = dAtob(argv[3]);
  3085. if (target)
  3086. treeRoot = dynamic_cast<SimSet*>(target);
  3087. if (! treeRoot)
  3088. Sim::findObject(RootGroupId, treeRoot);
  3089. object->inspectObject(treeRoot,okToEdit);
  3090. }
  3091. ConsoleMethod(GuiTreeViewCtrl, getItemText, const char *, 3, 3, "(TreeItemId item)")
  3092. {
  3093. return(object->getItemText(dAtoi(argv[2])));
  3094. }
  3095. ConsoleMethod(GuiTreeViewCtrl, getItemValue, const char *, 3, 3, "(TreeItemId item)")
  3096. {
  3097. return(object->getItemValue(dAtoi(argv[2])));
  3098. }
  3099. ConsoleMethod(GuiTreeViewCtrl, editItem, bool, 5, 5, "(TreeItemId item, string newText, string newValue)")
  3100. {
  3101. return(object->editItem(dAtoi(argv[2]), argv[3], argv[4]));
  3102. }
  3103. ConsoleMethod(GuiTreeViewCtrl, removeItem, bool, 3, 3, "(TreeItemId item)")
  3104. {
  3105. return(object->removeItem(dAtoi(argv[2])));
  3106. }
  3107. ConsoleMethod(GuiTreeViewCtrl, removeAllChildren, void, 3, 3, "removeAllChildren(TreeItemId parent)")
  3108. {
  3109. object->removeAllChildren(dAtoi(argv[2]));
  3110. }
  3111. ConsoleMethod(GuiTreeViewCtrl, clear, void, 2, 2, "() - empty tree")
  3112. {
  3113. object->removeItem(0);
  3114. }
  3115. ConsoleMethod(GuiTreeViewCtrl, getFirstRootItem, S32, 2, 2, "Get id for root item.")
  3116. {
  3117. return(object->getFirstRootItem());
  3118. }
  3119. ConsoleMethod(GuiTreeViewCtrl, getChild, S32, 3, 3, "(TreeItemId item)")
  3120. {
  3121. return(object->getChildItem(dAtoi(argv[2])));
  3122. }
  3123. ConsoleMethod(GuiTreeViewCtrl, buildVisibleTree, void, 3, 3, "Build the visible tree")
  3124. {
  3125. object->buildVisibleTree(dAtob(argv[2]));
  3126. }
  3127. ConsoleMethod(GuiTreeViewCtrl, getParent, S32, 3, 3, "(TreeItemId item)")
  3128. {
  3129. return(object->getParentItem(dAtoi(argv[2])));
  3130. }
  3131. ConsoleMethod(GuiTreeViewCtrl, getNextSibling, S32, 3, 3, "(TreeItemId item)")
  3132. {
  3133. return(object->getNextSiblingItem(dAtoi(argv[2])));
  3134. }
  3135. ConsoleMethod(GuiTreeViewCtrl, getPrevSibling, S32, 3, 3, "(TreeItemId item)")
  3136. {
  3137. return(object->getPrevSiblingItem(dAtoi(argv[2])));
  3138. }
  3139. ConsoleMethod(GuiTreeViewCtrl, getItemCount, S32, 2, 2, "() @return Returns the number of items in control")
  3140. {
  3141. return(object->getItemCount());
  3142. }
  3143. ConsoleMethod(GuiTreeViewCtrl, getSelectedItem, S32, 2, 2, "() @return Returns the ID of the selected item.")
  3144. {
  3145. return ( object->getSelectedItem() );
  3146. }
  3147. ConsoleMethod(GuiTreeViewCtrl, getSelectedObject, S32, 2, 2, "() @return Returns the currently selected simObject in inspector mode or -1")
  3148. {
  3149. GuiTreeViewCtrl::Item *item = object->getItem( object->getSelectedItem() );
  3150. if( item != NULL && item->isInspectorData() )
  3151. {
  3152. SimObject *obj = item->getObject();
  3153. if( obj != NULL )
  3154. return object->getId();
  3155. }
  3156. return -1;
  3157. }
  3158. ConsoleMethod(GuiTreeViewCtrl, moveItemUp, void, 3, 3, "(TreeItemId item)")
  3159. {
  3160. object->moveItemUp( dAtoi( argv[2] ) );
  3161. }
  3162. ConsoleMethod(GuiTreeViewCtrl, getSelectedItemsCount, S32, 2, 2, "")
  3163. {
  3164. return ( object->getSelectedItemsCount() );
  3165. }
  3166. ConsoleMethod(GuiTreeViewCtrl, moveItemDown, void, 3, 3, "(TreeItemId item)")
  3167. {
  3168. object->moveItemDown( dAtoi( argv[2] ) );
  3169. }
  3170. //-----------------------------------------------------------------------------
  3171. 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")
  3172. {
  3173. if ( argc < 4 )
  3174. {
  3175. Con::warnf("GuiTreeViewCtrl::getTextToRoot - Invalid number of arguments!");
  3176. return ("");
  3177. }
  3178. S32 itemId = dAtoi( argv[2] );
  3179. StringTableEntry delimiter = argv[3];
  3180. return object->getTextToRoot( itemId, delimiter );
  3181. }
  3182. ConsoleMethod(GuiTreeViewCtrl,getSelectedItemList,const char*, 2,2,"returns a space seperated list of mulitple item ids")
  3183. {
  3184. char* buff = Con::getReturnBuffer(1024);
  3185. dSprintf(buff,1024,"");
  3186. for(int i = 0; i < object->mSelected.size(); i++)
  3187. {
  3188. S32 id = object->mSelected[i];
  3189. //get the current length of the buffer
  3190. U32 len = dStrlen(buff);
  3191. //the start of the buffer where we want to write
  3192. char* buffPart = buff+len;
  3193. //the size of the remaining buffer (-1 cause dStrlen doesn't count the \0)
  3194. S32 size = 1024-len-1;
  3195. //write it:
  3196. if(size < 1)
  3197. {
  3198. Con::errorf("GuiTreeViewCtrl::getSelectedItemList - Not enough room to return our object list");
  3199. return buff;
  3200. }
  3201. dSprintf(buffPart,size,"%d ", id);
  3202. }
  3203. //mSelected
  3204. return buff;
  3205. }
  3206. //------------------------------------------------------------------------------
  3207. S32 GuiTreeViewCtrl::findItemByObjectId(S32 iObjId)
  3208. {
  3209. for (S32 i = 0; i < mItems.size(); i++)
  3210. {
  3211. if (mItems[i] != NULL)
  3212. {
  3213. SimObject* pObj = mItems[i]->getObject();
  3214. if(pObj && pObj->getId() == iObjId)
  3215. return mItems[i]->mId;
  3216. }
  3217. }
  3218. return -1;
  3219. }
  3220. //------------------------------------------------------------------------------
  3221. ConsoleMethod(GuiTreeViewCtrl, findItemByObjectId, S32, 3, 3, "(find item by object id and returns the mId)")
  3222. {
  3223. return(object->findItemByObjectId(dAtoi(argv[2])));
  3224. }
  3225. //------------------------------------------------------------------------------
  3226. bool GuiTreeViewCtrl::scrollVisibleByObjectId(S32 objID)
  3227. {
  3228. S32 itemID = findItemByObjectId(objID);
  3229. if(itemID == -1)
  3230. {
  3231. // we did not find the item in our current items
  3232. // we should try to find and show the parent of the item.
  3233. SimObject *obj = Sim::findObject(objID);
  3234. if(!obj || !obj->getGroup())
  3235. return false;
  3236. // if we can't show the parent, we fail.
  3237. if(! scrollVisibleByObjectId(obj->getGroup()->getId()) )
  3238. return false;
  3239. // get the parent. expand the parent. rebuild the tree. this ensures that
  3240. // we'll be able to find the child item we're targeting.
  3241. S32 parentID = findItemByObjectId(obj->getGroup()->getId());
  3242. AssertFatal(parentID != -1, "We were able to show the parent, but could not then find the parent. This should not happen.");
  3243. Item *parentItem = getItem(parentID);
  3244. parentItem->setExpanded(true);
  3245. buildVisibleTree();
  3246. // NOW we should be able to find the object. if not... something's wrong.
  3247. itemID = findItemByObjectId(objID);
  3248. AssertWarn(itemID != -1,"GuiTreeViewCtrl::scrollVisibleByObjectId() found the parent, but can't find it's immediate child. This should not happen.");
  3249. if(itemID == -1)
  3250. return false;
  3251. }
  3252. // ok, item found. scroll to it.
  3253. scrollVisible(itemID);
  3254. return true;
  3255. }
  3256. //------------------------------------------------------------------------------
  3257. ConsoleMethod(GuiTreeViewCtrl, scrollVisibleByObjectId, S32, 3, 3, "(show item by object id. returns true if sucessful.)")
  3258. {
  3259. return(object->scrollVisibleByObjectId(dAtoi(argv[2])));
  3260. }