2
0

guiTreeViewCtrl.cc 116 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899
  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->mFont );
  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->mFont );
  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->mFont );
  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->mFont.isNull() )
  510. {
  511. S32 width = ( tabLevel + 1 ) * mTabSize + item->getDisplayTextWidth(mProfile->mFont);
  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 = "common/gui/images/default:"
  928. "common/gui/images/simgroup:"
  929. "common/gui/images/simgroupClosed:"
  930. "common/gui/images/simgroupSelected:"
  931. "common/gui/images/simgroupSelectedClosed:";
  932. }
  933. // Figure the size of the buffer we need...
  934. const char* temp = dStrchr( icons, '\t' );
  935. U32 textLen = temp ? (U32)( temp - icons ) : dStrlen( icons );
  936. // Allocate temporary space.
  937. FrameAllocatorMarker txtBuff;
  938. char* drawText = (char*)txtBuff.alloc(sizeof(char) * (textLen + 4));
  939. dStrncpy( drawText, icons, textLen );
  940. drawText[textLen] = '\0';
  941. U32 numIcons = 0;
  942. char *buf = (char*)txtBuff.alloc(sizeof(char) * 256);
  943. char* token = dStrtok( drawText, ":" );
  944. // Count the number of icons and store them.
  945. while (token && numIcons < MaxIcons)
  946. {
  947. dSprintf( buf, sizeof( char ) * 256, "%s", token );
  948. mIconTable[numIcons] = TextureHandle( buf, TextureHandle::BitmapKeepTexture );
  949. token = dStrtok( NULL, ":" );
  950. numIcons++;
  951. }
  952. return true;
  953. }
  954. //------------------------------------------------------------------------------
  955. void GuiTreeViewCtrl::onPreRender()
  956. {
  957. Parent::onPreRender();
  958. S32 nRootItemId = getFirstRootItem();
  959. if( nRootItemId == 0 )
  960. return;
  961. Item *pRootItem = getItem( nRootItemId );
  962. if( pRootItem == NULL )
  963. return;
  964. mTicksPassed++;
  965. if( mTicksPassed > mTreeRefreshInterval )
  966. {
  967. // Update every render in case new objects are added
  968. buildVisibleTree();
  969. mTicksPassed = 0;
  970. }
  971. }
  972. //------------------------------------------------------------------------------
  973. bool GuiTreeViewCtrl::hitTest(const Point2I & pnt, Item* & item, BitSet32 & flags)
  974. {
  975. // Initialize some things.
  976. const Point2I pos = globalToLocalCoord(pnt);
  977. flags.clear();
  978. item = 0;
  979. // get the hit cell
  980. Point2I cell((pos.x < 0 ? -1 : pos.x / mCellSize.x),
  981. (pos.y < 0 ? -1 : pos.y / mCellSize.y));
  982. // valid?
  983. if((cell.x < 0 || cell.x >= mSize.x) ||
  984. (cell.y < 0 || cell.y >= mSize.y))
  985. return false;
  986. flags.set(OnRow);
  987. // Grab the cell.
  988. if (cell.y >= mVisibleItems.size())
  989. return false; //Invalid cell, so don't do anything
  990. item = mVisibleItems[cell.y];
  991. S32 min = mTabSize * item->mTabLevel;
  992. // left of icon/text?
  993. if(pos.x < min)
  994. {
  995. flags.set(OnIndent);
  996. return true;
  997. }
  998. // check image
  999. S32 image = BmpChild;
  1000. if(item->isInspectorData())
  1001. image = item->isExpanded() ? BmpExp : BmpCon;
  1002. else
  1003. image = item->isExpanded() ? item->getExpandedImage() : item->getNormalImage();
  1004. if((image >= 0) && (image < mProfile->mBitmapArrayRects.size()))
  1005. min += mProfile->mBitmapArrayRects[image].extent.x;
  1006. // Is it on the image?
  1007. if(pos.x < min)
  1008. {
  1009. flags.set(OnImage);
  1010. return(true);
  1011. }
  1012. // Bump over to the start of the text.
  1013. min += mTextOffset;
  1014. // Check against the text.
  1015. FrameAllocatorMarker txtAlloc;
  1016. U32 bufLen = item->getDisplayTextLength();
  1017. char *buf = (char*)txtAlloc.alloc(bufLen);
  1018. item->getDisplayText(bufLen, buf);
  1019. min += mProfile->mFont->getStrWidth(buf);
  1020. if(pos.x < min)
  1021. flags.set(OnText);
  1022. return true;
  1023. }
  1024. void GuiTreeViewCtrl::setInstantGroup(SimObject * obj)
  1025. {
  1026. // make sure we're talking about a group.
  1027. SimGroup * grp = dynamic_cast<SimGroup*>(obj);
  1028. // Set the instant group variable.
  1029. if(grp)
  1030. {
  1031. Con::setVariable("instantGroup", grp->getIdString());
  1032. // Notify Script
  1033. Con::executef(this,2,"onInstantGroupSelected",Con::getIntArg(grp->getId()));
  1034. }
  1035. }
  1036. void GuiTreeViewCtrl::syncSelection()
  1037. {
  1038. // for each visible item check to see if it is on the mSelected list.
  1039. // if it is then make sure that it is on the mSelectedItems list as well.
  1040. for (S32 i = 0; i < mVisibleItems.size(); i++)
  1041. {
  1042. for (S32 j = 0; j < mSelected.size(); j++)
  1043. {
  1044. if (mVisibleItems[i]->mId == mSelected[j])
  1045. {
  1046. // check to see if it is on the visible items list.
  1047. bool addToSelectedItems = true;
  1048. for (S32 k = 0; k < mSelectedItems.size(); k++)
  1049. {
  1050. if (mSelected[j] == mSelectedItems[k]->mId)
  1051. {
  1052. // don't add it
  1053. addToSelectedItems = false;
  1054. }
  1055. }
  1056. if (addToSelectedItems)
  1057. {
  1058. mVisibleItems[i]->mState.set(Item::Selected, true);
  1059. mSelectedItems.push_front(mVisibleItems[i]);
  1060. break;
  1061. }
  1062. }
  1063. else if (mVisibleItems[i]->isInspectorData())
  1064. {
  1065. if (mVisibleItems[i]->getObject() && mVisibleItems[i]->getObject()->getId() == mSelected[j])
  1066. {
  1067. // check to see if it is on the visible items list.
  1068. bool addToSelectedItems = true;
  1069. for (S32 k = 0; k < mSelectedItems.size(); k++)
  1070. {
  1071. if (mSelectedItems[k]->isInspectorData())
  1072. {
  1073. if (mSelected[j] == mSelectedItems[k]->getObject()->getId())
  1074. {
  1075. // don't add it
  1076. addToSelectedItems = false;
  1077. }
  1078. }
  1079. else
  1080. {
  1081. if (mSelected[j] == mSelectedItems[k]->mId)
  1082. {
  1083. // don't add it
  1084. addToSelectedItems = false;
  1085. }
  1086. }
  1087. }
  1088. if (addToSelectedItems)
  1089. {
  1090. mVisibleItems[i]->mState.set(Item::Selected, true);
  1091. mSelectedItems.push_front(mVisibleItems[i]);
  1092. break;
  1093. }
  1094. }
  1095. }
  1096. }
  1097. }
  1098. }
  1099. void GuiTreeViewCtrl::removeSelection(S32 itemId)
  1100. {
  1101. if (mDebug)
  1102. Con::printf("removeSelection called");
  1103. Item * item = getItem(itemId);
  1104. // the item may have been selected at one point but was never created/visible in the tree
  1105. // so remove it.
  1106. for (S32 j = 0; j <mSelected.size(); j++)
  1107. {
  1108. if (item)
  1109. {
  1110. if (item->isInspectorData())
  1111. {
  1112. if (item->getObject() && item->getObject()->getId() == mSelected[j])
  1113. {
  1114. mSelected.erase(j);
  1115. break;
  1116. }
  1117. }
  1118. }
  1119. if (mSelected[j] == itemId)
  1120. {
  1121. mSelected.erase(j);
  1122. break;
  1123. }
  1124. }
  1125. if(!item)
  1126. {
  1127. // maybe what we were passed wasn't an item id but an object id.
  1128. for (S32 i = 0; i <mItems.size(); i++)
  1129. {
  1130. if (mItems[i] != NULL)
  1131. {
  1132. if (mItems[i]->isInspectorData())
  1133. {
  1134. if (mItems[i]->getObject()->getId() == itemId)
  1135. {
  1136. item = mItems[i];
  1137. break;
  1138. }
  1139. }
  1140. }
  1141. }
  1142. if (!item)
  1143. {
  1144. //Con::errorf(ConsoleLogEntry::General, "GuiTreeViewCtrl::removeSelection: invalid item id! Perhaps it isn't visible yet");
  1145. return;
  1146. }
  1147. }
  1148. item->mState.set(Item::Selected, false);
  1149. for (S32 i = 0; i < mSelectedItems.size(); i++)
  1150. {
  1151. if (mSelectedItems[i] == item)
  1152. {
  1153. mSelectedItems.erase(i);
  1154. break;
  1155. }
  1156. }
  1157. // Callback - onRemoveSelection( %itemID )
  1158. if (item->getObject())
  1159. Con::executef(this, 2, "onRemoveSelection", Con::getIntArg(item->getObject()->getId()));
  1160. }
  1161. void GuiTreeViewCtrl::addSelection(S32 itemId)
  1162. {
  1163. if (mDebug)
  1164. Con::printf("addSelection called");
  1165. Item * item = getItem(itemId);
  1166. S32 itr; // used to loop thru items
  1167. bool foundMatch = false;
  1168. if(!item)
  1169. {
  1170. // maybe what we were passed wasn't an item id but an object id.
  1171. for (itr = 0; itr <mItems.size(); itr++)
  1172. {
  1173. if (mItems[itr] != NULL)
  1174. {
  1175. if (mItems[itr]->isInspectorData())
  1176. {
  1177. if ( mItems[itr]->getObject() && mItems[itr]->getObject()->getId() == itemId)
  1178. {
  1179. item = mItems[itr];
  1180. //looks like it is. check to see if it is on the list
  1181. bool alreadySelected = false;
  1182. Vector<Item *>::iterator i;
  1183. for(i = mSelectedItems.begin(); i != mSelectedItems.end(); i++)
  1184. {
  1185. if (*(i) == item)
  1186. {
  1187. //already a selected item which means this call should be ignored
  1188. alreadySelected = true;
  1189. return;
  1190. }
  1191. }
  1192. break;
  1193. }
  1194. }
  1195. }
  1196. }
  1197. if (!item)
  1198. {
  1199. // Do we want to allow more than one selected item?
  1200. if( !mMultipleSelections )
  1201. clearSelection();
  1202. // rdbnote: this isn't going to be very fast, but we need to make sure we're not adding things to this list twice!!
  1203. // in a perfect world, code would be cleanly written enough to not have this problem..
  1204. foundMatch = false;
  1205. for (itr = 0; itr < mSelected.size(); itr++)
  1206. {
  1207. if (mSelected[itr] == itemId)
  1208. foundMatch = true;
  1209. }
  1210. //Con::errorf(ConsoleLogEntry::General, "GuiTreeViewCtrl::addSelection: invalid item id! Perhaps it isn't visible yet.");
  1211. if (!foundMatch)
  1212. mSelected.push_front(itemId);
  1213. return;
  1214. }
  1215. }
  1216. else
  1217. {
  1218. // Do we want to allow more than one selected item?
  1219. if( !mMultipleSelections )
  1220. clearSelection();
  1221. // rdbnote: this isn't going to be very fast, but we need to make sure we're not adding things to this list twice!!
  1222. // in a perfect world, code would be cleanly written enough to not have this problem..
  1223. foundMatch = false;
  1224. for (itr = 0; itr < mSelected.size(); itr++)
  1225. {
  1226. if (mSelected[itr] == itemId)
  1227. foundMatch = true;
  1228. }
  1229. // regardless of whether we found an item, we keep track of the Id that was passed
  1230. // as the item may simply not have been created/visible yet.
  1231. if (!foundMatch)
  1232. mSelected.push_front(itemId);
  1233. }
  1234. item->mState.set(Item::Selected, true);
  1235. // Also make it so we can see it if we didn't already.
  1236. scrollVisible(item);
  1237. // Do we want to allow more than one selected item?
  1238. //if( !mMultipleSelections )
  1239. // clearSelection();
  1240. // rdbnote: this isn't going to be very fast, but we need to make sure we're not adding things to this list twice!!
  1241. // in a perfect world, code would be cleanly written enough to not have this problem..
  1242. foundMatch = false;
  1243. for (itr = 0; itr < mSelectedItems.size(); itr++)
  1244. {
  1245. if (mSelectedItems[itr]->mId == item->mId)
  1246. foundMatch = true;
  1247. }
  1248. if (!foundMatch)
  1249. mSelectedItems.push_front(item);
  1250. // Callback - onAddSelection( %itemID )
  1251. if (item->getObject())
  1252. Con::executef(this, 2, "onAddSelection", Con::getIntArg(item->getObject()->getId()));
  1253. }
  1254. void GuiTreeViewCtrl::onItemSelected( Item *item )
  1255. {
  1256. char buf[16];
  1257. dSprintf(buf, 16, "%d", item->mId);
  1258. if (item->isInspectorData())
  1259. {
  1260. if(item->getObject())
  1261. Con::executef(this, 2, "onSelect", Con::getIntArg(item->getObject()->getId()));
  1262. if (!(item->isParent()) && item->getObject())
  1263. Con::executef(this, 2, "onInspect", Con::getIntArg(item->getObject()->getId()));
  1264. }
  1265. else
  1266. {
  1267. Con::executef(this, 2, "onSelect", buf);
  1268. if (!(item->isParent()))
  1269. Con::executef(this, 2, "onInspect", buf);
  1270. }
  1271. mSelectedItem = item->getID();
  1272. }
  1273. bool GuiTreeViewCtrl::setItemSelected(S32 itemId, bool select)
  1274. {
  1275. Item * item = getItem(itemId);
  1276. if (select)
  1277. {
  1278. if (mDebug) Con::printf("setItemSelected called true");
  1279. // rdbnote: this isn't going to be very fast, but we need to make sure we're not adding things to this list twice!!
  1280. // in a perfect world, code would be cleanly written enough to not have this problem..
  1281. bool foundMatch = false;
  1282. for (S32 itr = 0; itr < mSelected.size(); itr++)
  1283. {
  1284. if (mSelected[itr] == itemId)
  1285. foundMatch = true;
  1286. }
  1287. if (!foundMatch)
  1288. mSelected.push_front(itemId);
  1289. }
  1290. else
  1291. {
  1292. if (mDebug) Con::printf("setItemSelected called false");
  1293. // remove it from the mSelected list
  1294. for (S32 j = 0; j <mSelected.size(); j++)
  1295. {
  1296. if (item)
  1297. {
  1298. if (item->isInspectorData())
  1299. {
  1300. if (item->getObject())
  1301. {
  1302. if(item->getObject()->getId() == mSelected[j])
  1303. {
  1304. mSelected.erase(j);
  1305. break;
  1306. }
  1307. }
  1308. else
  1309. {
  1310. // Zombie, kill it!
  1311. mSelected.erase(j);
  1312. j--;
  1313. }
  1314. }
  1315. }
  1316. if (mSelected[j] == itemId)
  1317. {
  1318. mSelected.erase(j);
  1319. break;
  1320. }
  1321. }
  1322. }
  1323. if(!item)
  1324. {
  1325. // maybe what we were passed wasn't an item id but an object id.
  1326. for (S32 i = 0; i <mItems.size(); i++)
  1327. {
  1328. if (mItems[i] != NULL)
  1329. {
  1330. if (mItems[i]->isInspectorData())
  1331. {
  1332. if (mItems[i]->getObject())
  1333. {
  1334. if(mItems[i]->getObject()->getId() == itemId)
  1335. {
  1336. item = mItems[i];
  1337. break;
  1338. }
  1339. }
  1340. else
  1341. {
  1342. // It's a zombie, blast it.
  1343. mItems.erase(i);
  1344. i--;
  1345. }
  1346. }
  1347. }
  1348. }
  1349. if (!item)
  1350. {
  1351. //Con::errorf(ConsoleLogEntry::General, "GuiTreeViewCtrl::setItemSelected: invalid item id! Perhaps it isn't visible yet.");
  1352. return(false);
  1353. }
  1354. }
  1355. if(select)
  1356. {
  1357. addSelection( item->mId );
  1358. onItemSelected( item );
  1359. }
  1360. else
  1361. {
  1362. // unselect the item, if it's present.
  1363. item->mState.set(Item::Selected, false);
  1364. if (item->isInspectorData() && item->getObject())
  1365. Con::executef(this, 2, "onUnSelect", Con::getIntArg(item->getObject()->getId()));
  1366. else
  1367. Con::executef(this, 2, "onUnSelect", Con::getIntArg(item->mId));
  1368. // remove it from the selected items list
  1369. for (S32 i = 0; i < mSelectedItems.size(); i++)
  1370. {
  1371. if (mSelectedItems[i] == item)
  1372. {
  1373. mSelectedItems.erase(i);
  1374. break;
  1375. }
  1376. }
  1377. }
  1378. setUpdate();
  1379. return(true);
  1380. }
  1381. // Given an item's index in the selection list, return its itemId
  1382. S32 GuiTreeViewCtrl::getSelectedItem(S32 index)
  1383. {
  1384. if(index >= 0 && index < getSelectedItemsCount())
  1385. {
  1386. return mSelectedItems[index]->mId;
  1387. }
  1388. return -1;
  1389. }
  1390. bool GuiTreeViewCtrl::setItemExpanded(S32 itemId, bool expand)
  1391. {
  1392. Item * item = getItem(itemId);
  1393. if(!item)
  1394. {
  1395. Con::errorf(ConsoleLogEntry::General, "GuiTreeViewCtrl::setItemExpanded: invalid item id!");
  1396. return(false);
  1397. }
  1398. if(item->isExpanded() == expand)
  1399. return(true);
  1400. // expand parents
  1401. if(expand)
  1402. {
  1403. while(item)
  1404. {
  1405. if(item->mState.test(Item::VirtualParent))
  1406. onVirtualParentExpand(item);
  1407. item->setExpanded(true);
  1408. item = item->mParent;
  1409. }
  1410. }
  1411. else
  1412. {
  1413. if(item->mState.test(Item::VirtualParent))
  1414. onVirtualParentCollapse(item);
  1415. item->setExpanded(false);
  1416. }
  1417. return(true);
  1418. }
  1419. bool GuiTreeViewCtrl::setItemValue(S32 itemId, StringTableEntry Value)
  1420. {
  1421. Item * item = getItem(itemId);
  1422. if(!item)
  1423. {
  1424. Con::errorf(ConsoleLogEntry::General, "GuiTreeViewCtrl::setItemValue: invalid item id!");
  1425. return(false);
  1426. }
  1427. item->setValue( ( Value ) ? Value : "" );
  1428. return(true);
  1429. }
  1430. const char * GuiTreeViewCtrl::getItemText(S32 itemId)
  1431. {
  1432. Item * item = getItem(itemId);
  1433. if(!item)
  1434. {
  1435. Con::errorf(ConsoleLogEntry::General, "GuiTreeViewCtrl::getItemText: invalid item id!");
  1436. return("");
  1437. }
  1438. return(item->getText() ? item->getText() : "");
  1439. }
  1440. const char * GuiTreeViewCtrl::getItemValue(S32 itemId)
  1441. {
  1442. Item * item = getItem(itemId);
  1443. if(!item)
  1444. {
  1445. Con::errorf(ConsoleLogEntry::General, "GuiTreeViewCtrl::getItemValue: invalid item id!");
  1446. return("");
  1447. }
  1448. if(item->mState.test(Item::InspectorData))
  1449. {
  1450. // If it's InspectorData, we let people use this call to get an object reference.
  1451. return item->mInspectorInfo.mObject->getIdString();
  1452. }
  1453. else
  1454. {
  1455. // Just return the script value...
  1456. return(item->getValue() ? item->getValue() : "");
  1457. }
  1458. }
  1459. bool GuiTreeViewCtrl::editItem( S32 itemId, const char* newText, const char* newValue )
  1460. {
  1461. Item* item = getItem( itemId );
  1462. if ( !item )
  1463. {
  1464. Con::errorf(ConsoleLogEntry::General, "GuiTreeViewCtrl::editItem: invalid item id!");
  1465. return false;
  1466. }
  1467. if ( item->mState.test(Item::InspectorData) )
  1468. {
  1469. Con::errorf(ConsoleLogEntry::General, "GuiTreeViewCtrl::editItem: item %d is inspector data and may not be modified!", itemId);
  1470. return false;
  1471. }
  1472. delete [] item->getText();
  1473. item->setText (new char[dStrlen( newText ) + 1]);
  1474. dStrcpy( item->getText(), newText );
  1475. delete [] item->getValue();
  1476. item->setValue( new char[dStrlen( newValue ) + 1] );
  1477. dStrcpy( item->getValue(), newValue );
  1478. // Update the widths and such:
  1479. buildVisibleTree();
  1480. return true;
  1481. }
  1482. void GuiTreeViewCtrl::deleteSelection()
  1483. {
  1484. Con::executef(this, 1, "onDeleteSelection");
  1485. if (mSelectedItems.empty())
  1486. {
  1487. for (S32 i = 0; i < mSelected.size(); i++)
  1488. {
  1489. S32 objectId = mSelected[i];
  1490. // find the object
  1491. SimObject* obj = Sim::findObject(objectId);
  1492. obj->deleteObject();
  1493. }
  1494. }
  1495. else
  1496. {
  1497. Vector<Item*> delSelection;
  1498. delSelection = mSelectedItems;
  1499. mSelectedItems.clear();
  1500. while (!delSelection.empty())
  1501. {
  1502. Item * item = delSelection.front();
  1503. setItemSelected(item->mId,false);
  1504. if ( item->mParent )
  1505. deleteItem( item );
  1506. delSelection.pop_front();
  1507. }
  1508. }
  1509. mSelected.clear();
  1510. mSelectedItems.clear();
  1511. Con::executef( this, 1, "onObjectDeleteCompleted");
  1512. }
  1513. //------------------------------------------------------------------------------
  1514. // keyboard movement of items is restricted to just one item at a time
  1515. // if more than one item is selected then movement operations are not performed
  1516. bool GuiTreeViewCtrl::onKeyDown( const GuiEvent& event )
  1517. {
  1518. if ( !mVisible || !mActive || !mAwake )
  1519. return true;
  1520. // All the keyboard functionality requires a selected item, so if none exists...
  1521. // Deal with enter and delete
  1522. if ( event.modifier == 0 )
  1523. {
  1524. if ( event.keyCode == KEY_RETURN )
  1525. {
  1526. if ( mAltConsoleCommand[0] )
  1527. Con::evaluate( mAltConsoleCommand );
  1528. return true;
  1529. }
  1530. if ( event.keyCode == KEY_DELETE && mDeleteObjectAllowed )
  1531. {
  1532. // Don't delete the root!
  1533. if (mSelectedItems.empty())
  1534. return true;
  1535. //this may be fighting with the world editor delete
  1536. deleteSelection();
  1537. return true;
  1538. }
  1539. //call a generic bit of script that will let the subclass know that a key was pressed
  1540. Con::executef(this, 3, "onKeyDown", Con::getIntArg(event.modifier), Con::getIntArg(event.keyCode));
  1541. }
  1542. // only do operations if only one item is selected
  1543. if ( mSelectedItems.empty() || (mSelectedItems.size() > 1))
  1544. return true;
  1545. Item* item = mSelectedItems.first();
  1546. if ( !item )
  1547. return true;
  1548. // The Alt key lets you move items around!
  1549. if ( mFlags.test(IsEditable) && event.modifier & SI_ALT )
  1550. {
  1551. switch ( event.keyCode )
  1552. {
  1553. case KEY_UP:
  1554. // Move us up.
  1555. if ( item->mPrevious )
  1556. {
  1557. moveItemUp( item->mId );
  1558. scrollVisible(item);
  1559. }
  1560. return true;
  1561. case KEY_DOWN:
  1562. // Move the item under us up.
  1563. if ( item->mNext )
  1564. {
  1565. moveItemUp( item->mNext->mId );
  1566. scrollVisible(item);
  1567. }
  1568. return true;
  1569. case KEY_LEFT:
  1570. if ( item->mParent )
  1571. {
  1572. if ( item->mParent->mParent )
  1573. {
  1574. // Ok, we have both an immediate parent, and a grandparent.
  1575. // The goal of left-arrow alt is to become the child of our
  1576. // grandparent, ie, to become a sibling of our parent.
  1577. // First, unlink item from its siblings.
  1578. if ( item->mPrevious )
  1579. item->mPrevious->mNext = item->mNext;
  1580. else
  1581. item->mParent->mChild = item->mNext;
  1582. if ( item->mNext )
  1583. item->mNext->mPrevious = item->mPrevious;
  1584. // Now, relink as the next sibling of our parent.
  1585. item->mPrevious = item->mParent;
  1586. item->mNext = item->mParent->mNext;
  1587. // If there was already a next sibling, deal with that case.
  1588. if ( item->mNext )
  1589. item->mNext->mPrevious = item;
  1590. item->mParent->mNext = item;
  1591. // Snag the current parent set if any...
  1592. SimSet *parentSet = NULL;
  1593. if(item->mParent->isInspectorData())
  1594. parentSet = dynamic_cast<SimSet*>(item->mParent->getObject());
  1595. else
  1596. {
  1597. // parent is probably script data so we search up the tree for a
  1598. // set to put our object in
  1599. Item * temp = item->mParent;
  1600. while (!temp->isInspectorData())
  1601. temp = temp->mParent;
  1602. // found a ancestor who is an inspectorData
  1603. if (temp->isInspectorData())
  1604. parentSet = dynamic_cast<SimSet*>(temp->getObject());
  1605. else parentSet = NULL;
  1606. }
  1607. // Get our active SimObject if any
  1608. SimObject *simObj = NULL;
  1609. if(item->isInspectorData())
  1610. simObj = item->getObject();
  1611. // Remove from the old parentset...
  1612. if(simObj && parentSet) {
  1613. if (parentSet->size()>0)
  1614. {
  1615. SimObject *lastObject = parentSet->last();
  1616. parentSet->removeObject(simObj);
  1617. parentSet->reOrder(lastObject);
  1618. } else
  1619. parentSet->removeObject(simObj);
  1620. }
  1621. // And finally, update our item
  1622. item->mParent = item->mParent->mParent;
  1623. // Snag the newparent set if any...
  1624. SimSet *newParentSet = NULL;
  1625. if(item->mParent->isInspectorData())
  1626. newParentSet = dynamic_cast<SimSet*>(item->mParent->getObject());
  1627. else
  1628. {
  1629. // parent is probably script data so we search up the tree for a
  1630. // set to put our object in
  1631. Item * temp = item->mParent;
  1632. while (!temp->isInspectorData())
  1633. temp = temp->mParent;
  1634. // found a ancestor who is an inspectorData
  1635. if (temp->isInspectorData())
  1636. newParentSet = dynamic_cast<SimSet*>(temp->getObject());
  1637. else newParentSet = NULL;
  1638. }
  1639. if(simObj && newParentSet)
  1640. {
  1641. newParentSet->addObject(simObj);
  1642. Item * temp = item->mNext;
  1643. // item->mNext may be script, so find an inspector item to reorder with if any
  1644. if (temp) {
  1645. do {
  1646. if (temp->isInspectorData())
  1647. break;
  1648. temp = temp->mNext;
  1649. } while (temp);
  1650. if (temp && item->getObject() && temp->getObject()) //do we still have a item->mNext? If not then don't bother reordering
  1651. newParentSet->reOrder(item->getObject(), temp->getObject());
  1652. }
  1653. } else if (!simObj&&newParentSet) {
  1654. // our current item is script data. but it may have children who
  1655. // is inspector data who need an updated set
  1656. if (item->mChild)
  1657. inspectorSearch(item->mChild, item, parentSet, newParentSet);
  1658. }
  1659. // And update everything hurrah.
  1660. buildVisibleTree();
  1661. scrollVisible(item);
  1662. }
  1663. }
  1664. return true;
  1665. case KEY_RIGHT:
  1666. if ( item->mPrevious )
  1667. {
  1668. // Make the item the last child of its previous sibling.
  1669. // First, unlink from the current position in the list
  1670. item->mPrevious->mNext = item->mNext;
  1671. if ( item->mNext )
  1672. item->mNext->mPrevious = item->mPrevious;
  1673. // Get the object we're poking with.
  1674. SimObject *simObj = NULL;
  1675. SimSet *parentSet = NULL;
  1676. if(item->isInspectorData())
  1677. simObj = item->getObject();
  1678. if(item->mParent->isInspectorData())
  1679. parentSet = dynamic_cast<SimSet*>(item->mParent->getObject());
  1680. else {
  1681. // parent is probably script data so we search up the tree for a
  1682. // set to put our object in
  1683. Item * temp = item->mParent;
  1684. while (!temp->isInspectorData())
  1685. temp = temp->mParent;
  1686. // found an ancestor who is an inspectorData
  1687. if (temp->isInspectorData())
  1688. parentSet = dynamic_cast<SimSet*>(temp->getObject());
  1689. }
  1690. // If appropriate, remove from the current SimSet.
  1691. if(parentSet && simObj) {
  1692. if (parentSet->size()>0)
  1693. {
  1694. SimObject *lastObject = parentSet->last();
  1695. parentSet->removeObject(simObj);
  1696. parentSet->reOrder(lastObject);
  1697. } else
  1698. parentSet->removeObject(simObj);
  1699. }
  1700. // Now, make our previous sibling our parent...
  1701. item->mParent = item->mPrevious;
  1702. item->mNext = NULL;
  1703. // And sink us down to the end of its siblings, if appropriate.
  1704. if ( item->mParent->mChild )
  1705. {
  1706. Item* temp = item->mParent->mChild;
  1707. while ( temp->mNext )
  1708. temp = temp->mNext;
  1709. temp->mNext = item;
  1710. item->mPrevious = temp;
  1711. }
  1712. else
  1713. {
  1714. // only child...<sniff>
  1715. item->mParent->mChild = item;
  1716. item->mPrevious = NULL;
  1717. }
  1718. // Make sure the new parent is expanded:
  1719. if ( !item->mParent->mState.test( Item::Expanded ) )
  1720. setItemExpanded( item->mParent->mId, true );
  1721. // Snag the new parent simset if any.
  1722. SimSet *newParentSet = NULL;
  1723. // new parent might be script. so figure out what set we need to add it to.
  1724. if(item->mParent->isInspectorData())
  1725. newParentSet = dynamic_cast<SimSet*>(item->mParent->getObject());
  1726. else
  1727. {
  1728. // parent is probably script data so we search up the tree for a
  1729. // set to put our object in
  1730. if (mDebug) Con::printf("oh nos my parent is script!");
  1731. Item * temp = item->mParent;
  1732. while (!temp->isInspectorData())
  1733. temp = temp->mParent;
  1734. // found a ancestor who is an inspectorData
  1735. if (temp->isInspectorData())
  1736. newParentSet = dynamic_cast<SimSet*>(temp->getObject());
  1737. else newParentSet = NULL;
  1738. }
  1739. // Add the item's SimObject to the new parent simset, at the end.
  1740. if(newParentSet && simObj)
  1741. newParentSet->addObject(simObj);
  1742. else if (!simObj&&newParentSet&&parentSet) {
  1743. // our current item is script data. but it may have children who
  1744. // is inspector data who need an updated set
  1745. if (item->mChild) {
  1746. inspectorSearch(item->mChild, item, parentSet, newParentSet);
  1747. }
  1748. }
  1749. scrollVisible(item);
  1750. }
  1751. return true;
  1752. }
  1753. }
  1754. // Explorer-esque navigation...
  1755. switch( event.keyCode )
  1756. {
  1757. case KEY_UP:
  1758. // Select previous visible item:
  1759. if ( item->mPrevious )
  1760. {
  1761. item = item->mPrevious;
  1762. while ( item->isParent() && item->isExpanded() )
  1763. {
  1764. item = item->mChild;
  1765. while ( item->mNext )
  1766. item = item->mNext;
  1767. }
  1768. setItemSelected(mSelectedItems.first()->mId,false);
  1769. setItemSelected( item->mId, true );
  1770. scrollVisible(item);
  1771. return true;
  1772. }
  1773. // or select parent:
  1774. if ( item->mParent )
  1775. {
  1776. setItemSelected(mSelectedItems.first()->mId,false);
  1777. setItemSelected( item->mParent->mId, true );
  1778. scrollVisible(item->mParent);
  1779. return true;
  1780. }
  1781. return false;
  1782. break;
  1783. case KEY_DOWN:
  1784. // Selected child if it is visible:
  1785. if ( item->isParent() && item->isExpanded() )
  1786. {
  1787. setItemSelected(mSelectedItems.first()->mId,false);
  1788. setItemSelected( item->mChild->mId, true );
  1789. scrollVisible(item->mChild);
  1790. return true;
  1791. }
  1792. // or select next sibling (recursively):
  1793. do
  1794. {
  1795. if ( item->mNext )
  1796. {
  1797. setItemSelected(mSelectedItems.first()->mId,false);
  1798. setItemSelected(item->mNext->mId, true );
  1799. scrollVisible(item->mNext);
  1800. return true;
  1801. }
  1802. item = item->mParent;
  1803. } while ( item );
  1804. return false;
  1805. break;
  1806. case KEY_LEFT:
  1807. // Contract current menu:
  1808. if ( item->isExpanded() )
  1809. {
  1810. setItemExpanded( item->mId, false );
  1811. scrollVisible(item);
  1812. return true;
  1813. }
  1814. // or select parent:
  1815. if ( item->mParent )
  1816. {
  1817. setItemSelected(mSelectedItems.first()->mId,false);
  1818. setItemSelected( item->mParent->mId, true );
  1819. scrollVisible(item->mParent);
  1820. return true;
  1821. }
  1822. return false;
  1823. break;
  1824. case KEY_RIGHT:
  1825. // Expand selected item:
  1826. if ( item->isParent() )
  1827. {
  1828. if ( !item->isExpanded() )
  1829. {
  1830. setItemExpanded( item->mId, true );
  1831. scrollVisible(item);
  1832. return true;
  1833. }
  1834. // or select child:
  1835. setItemSelected(mSelectedItems.first()->mId,false);
  1836. setItemSelected( item->mChild->mId, true );
  1837. scrollVisible(item->mChild);
  1838. return true;
  1839. }
  1840. return false;
  1841. break;
  1842. }
  1843. // Not processed, so pass the event on:
  1844. return Parent::onKeyDown( event );
  1845. }
  1846. //------------------------------------------------------------------------------
  1847. // on mouse up look at the current item and check to see if it is valid
  1848. // to move the selected item(s) to it.
  1849. void GuiTreeViewCtrl::onMouseUp(const GuiEvent &event)
  1850. {
  1851. if( !mActive || !mAwake || !mVisible )
  1852. return;
  1853. mouseUnlock();
  1854. if ( mSelectedItems.empty())
  1855. {
  1856. mDragMidPoint = NomDragMidPoint;
  1857. return;
  1858. }
  1859. if (!mMouseDragged)
  1860. return;
  1861. else
  1862. mMouseDragged = false;
  1863. Item* newItem = NULL;
  1864. Item* newItem2 = NULL;
  1865. if (mFlags.test(IsEditable))
  1866. {
  1867. Parent::onMouseMove( event );
  1868. if (mOldDragY != mMouseOverCell.y)
  1869. {
  1870. mOldDragY = mMouseOverCell.y;
  1871. BitSet32 hitFlags = 0;
  1872. if ( !hitTest( event.mousePoint, newItem2, hitFlags ) )
  1873. {
  1874. mDragMidPoint = NomDragMidPoint;
  1875. return;
  1876. }
  1877. newItem2->mState.clear(Item::MouseOverBmp | Item::MouseOverText );
  1878. // if the newItem isn't in the mSelectedItemList then continue.
  1879. Vector<Item *>::iterator k;
  1880. for(k = mSelectedItems.begin(); k != mSelectedItems.end(); k++)
  1881. {
  1882. newItem = newItem2;
  1883. if (*(k) == newItem)
  1884. {
  1885. mDragMidPoint = NomDragMidPoint;
  1886. return;
  1887. }
  1888. Item * temp = *(k);
  1889. Item * grandpaTemp = newItem->mParent;
  1890. // grandpa check, kick out if an item would be its own ancestor
  1891. while (grandpaTemp)
  1892. {
  1893. if (temp == grandpaTemp)
  1894. {
  1895. if (mDebug)
  1896. {
  1897. Con::printf("grandpa check");
  1898. if (temp->isInspectorData())
  1899. Con::printf("temp's name: %s",temp->getObject()->getName());
  1900. if (grandpaTemp->isInspectorData())
  1901. Con::printf("grandpa's name: %s",grandpaTemp->getObject()->getName());
  1902. }
  1903. mDragMidPoint = NomDragMidPoint;
  1904. return;
  1905. }
  1906. grandpaTemp = grandpaTemp->mParent;
  1907. }
  1908. }
  1909. for (S32 i = 0; i <mSelectedItems.size();i++)
  1910. {
  1911. newItem = newItem2;
  1912. Item * item = mSelectedItems[i];
  1913. if (mDebug) Con::printf("----------------------------");
  1914. // clear old highlighting of the item
  1915. item->mState.clear(Item::MouseOverBmp | Item::MouseOverText );
  1916. // move the selected item to the newItem
  1917. Item* oldParent = item->mParent;
  1918. // Snag the current parent set if any for future reference
  1919. SimSet *parentSet = NULL;
  1920. if(oldParent->isInspectorData())
  1921. parentSet = dynamic_cast<SimSet*>(oldParent->getObject());
  1922. else
  1923. {
  1924. // parent is probably script data so we search up the tree for a
  1925. // set to put our object in
  1926. Item * temp = oldParent;
  1927. while (temp)
  1928. {
  1929. if (temp->isInspectorData())
  1930. break;
  1931. temp = temp->mParent;
  1932. }
  1933. // found an ancestor who is an inspectorData
  1934. if (temp)
  1935. {
  1936. if (temp->isInspectorData())
  1937. parentSet = dynamic_cast<SimSet*>(temp->getObject());
  1938. }
  1939. }
  1940. // unlink from the current position in the list
  1941. unlinkItem(item);
  1942. // update the parent's children
  1943. // check if we an only child
  1944. if (item->mParent->mChild == item)
  1945. {
  1946. if (item->mNext)
  1947. item->mParent->mChild = item->mNext;
  1948. else
  1949. item->mParent->mChild = NULL;
  1950. }
  1951. if (mDragMidPoint != NomDragMidPoint)
  1952. {
  1953. //if it is below an expanded tree, place as last item in the tree
  1954. //if it is below a parent who isn't expanded put below it
  1955. // position the item above or below another item
  1956. if (mDragMidPoint == AbovemDragMidPoint)
  1957. {
  1958. // easier to treat everything as "Below the mDragMidPoint" so make some adjustments
  1959. if (mDebug) Con::printf("adding item above mDragMidPoint");
  1960. // above the mid point of an item, so grab either the parent
  1961. // or the previous sibling
  1962. // does the item have a previous sibling?
  1963. if (newItem->mPrevious)
  1964. {
  1965. newItem = newItem->mPrevious;
  1966. if (mDebug) Con::printf("treating as if below an item that isn't expanded");
  1967. // otherwise add below that item as a sibling
  1968. item->mParent = newItem->mParent;
  1969. item->mPrevious = newItem;
  1970. item->mNext = newItem->mNext;
  1971. if (newItem->mNext)
  1972. newItem->mNext->mPrevious = item;
  1973. newItem->mNext = item;
  1974. }
  1975. else
  1976. {
  1977. if (mDebug) Con::printf("treating as if adding below the parent of the item");
  1978. // instead we add as the first item below the newItem's parent
  1979. item->mParent = newItem->mParent;
  1980. item->mNext = newItem;
  1981. item->mPrevious = NULL;
  1982. newItem->mPrevious = item;
  1983. item->mParent->mChild = item;
  1984. }
  1985. }
  1986. else if (mDragMidPoint == BelowmDragMidPoint)
  1987. {
  1988. if ((newItem->isParent())&&(newItem->isExpanded()))
  1989. {
  1990. if (mDebug) Con::printf("adding item to an expanded parent below the mDragMidPoint");
  1991. item->mParent = newItem;
  1992. // then add the new item as a child
  1993. item->mNext = newItem->mChild;
  1994. if (newItem->mChild)
  1995. newItem->mChild->mPrevious = item;
  1996. item->mParent->mChild = item;
  1997. item->mPrevious = NULL;
  1998. }
  1999. else if ((!newItem->mNext)&&(newItem->mParent)&&(newItem->mParent->mParent))
  2000. {
  2001. // add below it's parent.
  2002. if (mDebug) Con::printf("adding below a tree");
  2003. item->mParent = newItem->mParent->mParent;
  2004. item->mNext = newItem->mParent->mNext;
  2005. item->mPrevious = newItem->mParent;
  2006. if (newItem->mParent->mNext)
  2007. newItem->mParent->mNext->mPrevious = item;
  2008. newItem->mParent->mNext = item;
  2009. }
  2010. else
  2011. {
  2012. // adding below item not as a child
  2013. if (mDebug) Con::printf("adding item below the mDragMidPoint of an item");
  2014. item->mParent = newItem->mParent;
  2015. // otherwise the item is a sibling
  2016. if (newItem->mNext)
  2017. newItem->mNext->mPrevious = item;
  2018. item->mNext = newItem->mNext;
  2019. item->mPrevious = newItem;
  2020. newItem->mNext = item;
  2021. }
  2022. }
  2023. }
  2024. // if we're not allowed to add to items, then try to add to the parent of the hit item.
  2025. // if we are, just add to the item we hit.
  2026. else
  2027. {
  2028. if (mDebug)
  2029. {
  2030. if (item->isInspectorData() && item->getObject())
  2031. Con::printf("Item: %i",item->getObject()->getId());
  2032. if (newItem->isInspectorData() && newItem->getObject())
  2033. Con::printf("Parent: %i",newItem->getObject()->getId());
  2034. Con::printf("dragged onto an item");
  2035. }
  2036. // if the hit item is not already a group, and we're not allowed to drag to items,
  2037. // then try to add to the parent.
  2038. if(!mDragToItemAllowed && !newItem->isParent())
  2039. {
  2040. // add to the item's parent.
  2041. if(!newItem->mParent || !newItem->mParent->isParent())
  2042. {
  2043. if(mDebug)
  2044. Con::printf("could not find the parent of that item. dragging to an item is not allowed, kicking out.");
  2045. mDragMidPoint = NomDragMidPoint;
  2046. return;
  2047. }
  2048. newItem = newItem->mParent;
  2049. }
  2050. // new parent is the item in the current cell
  2051. item->mParent = newItem;
  2052. // adjust children if any
  2053. if (newItem->mChild)
  2054. {
  2055. if (mDebug) Con::printf("not the first child");
  2056. // put it at the top of the list (easier to find if there are many children)
  2057. if (newItem->mChild)
  2058. newItem->mChild->mPrevious = item;
  2059. item->mNext = newItem->mChild;
  2060. newItem->mChild = item;
  2061. item->mPrevious = NULL;
  2062. }
  2063. else
  2064. {
  2065. if (mDebug) Con::printf("first child");
  2066. // only child
  2067. newItem->mChild = item;
  2068. item->mNext = NULL;
  2069. item->mPrevious = NULL;
  2070. }
  2071. }
  2072. // expand the item we added to, if it isn't expanded already
  2073. if ( !item->mParent->mState.test( Item::Expanded ) )
  2074. setItemExpanded( item->mParent->mId, true );
  2075. //----------------------------------------------------------------
  2076. // handle objects
  2077. // Get our active SimObject if any
  2078. SimObject *simObj = NULL;
  2079. if(item->isInspectorData())
  2080. {
  2081. simObj = item->getObject();
  2082. }
  2083. // Remove from the old parentset
  2084. if((simObj && parentSet)&&(oldParent != item->mParent))
  2085. {
  2086. if (mDebug) Con::printf("removing item from old parentset");
  2087. // hack to get around the way removeObject takes the last item of the set
  2088. // and moves it into the place of the object we removed
  2089. if (parentSet->size()>0)
  2090. {
  2091. SimObject *lastObject = parentSet->last();
  2092. parentSet->removeObject(simObj);
  2093. parentSet->reOrder(lastObject);
  2094. }
  2095. else
  2096. {
  2097. parentSet->removeObject(simObj);
  2098. }
  2099. }
  2100. // Snag the newparent set if any...
  2101. SimSet *newParentSet = NULL;
  2102. if(item->mParent->isInspectorData())
  2103. {
  2104. if (mDebug) Con::printf("getting a new parent set");
  2105. SimObject * tmpObj = item->mParent->getObject();
  2106. newParentSet = dynamic_cast<SimSet*>(tmpObj);
  2107. }
  2108. else
  2109. {
  2110. // parent is probably script data so we search up the tree for a
  2111. // set to put our object in
  2112. if (mDebug) Con::printf("oh nos my parent is script!");
  2113. Item * temp = item->mParent;
  2114. while (temp)
  2115. {
  2116. if (temp->isInspectorData())
  2117. break;
  2118. temp = temp->mParent;
  2119. }
  2120. // found a ancestor who is an inspectorData
  2121. if (temp)
  2122. {
  2123. if (temp->isInspectorData())
  2124. newParentSet = dynamic_cast<SimSet*>(temp->getObject());
  2125. }
  2126. else
  2127. {
  2128. newParentSet = NULL;
  2129. }
  2130. }
  2131. if(simObj && newParentSet)
  2132. {
  2133. if (mDebug) Con::printf("simobj and new ParentSet");
  2134. if (oldParent != item->mParent)
  2135. newParentSet->addObject(simObj);
  2136. //order the objects in the simset according to their
  2137. //order in the tree view control
  2138. if(!item->mNext)
  2139. {
  2140. if(!item->mPrevious) break;
  2141. //bring to the end of the set
  2142. SimObject *prevObject = item->mPrevious->getObject();
  2143. if (prevObject && item->getObject())
  2144. {
  2145. newParentSet->reOrder(item->getObject(), prevObject);
  2146. }
  2147. }
  2148. else
  2149. {
  2150. //reorder within the set
  2151. SimObject *nextObject = item->mNext->getObject();
  2152. if(nextObject && item->getObject())
  2153. {
  2154. newParentSet->reOrder(item->getObject(), nextObject);
  2155. }
  2156. }
  2157. }
  2158. else if (!simObj&&newParentSet)
  2159. {
  2160. // our current item is script data. but it may have children who
  2161. // is inspector data who need an updated set
  2162. if (mDebug) Con::printf("no simobj but new parentSet");
  2163. if (item->mChild)
  2164. inspectorSearch(item->mChild, item, parentSet, newParentSet);
  2165. }
  2166. else if (simObj&&!newParentSet)
  2167. {
  2168. if (mDebug) Con::printf("simobject and no new parent set");
  2169. }
  2170. else
  2171. if (mDebug) Con::printf("no simobject and no new parent set");
  2172. }
  2173. // And update everything.
  2174. scrollVisible(newItem);
  2175. }
  2176. }
  2177. mDragMidPoint = NomDragMidPoint;
  2178. }
  2179. //------------------------------------------------------------------------------
  2180. void GuiTreeViewCtrl::onMouseDragged(const GuiEvent &event)
  2181. {
  2182. if(!mSupportMouseDragging) return;
  2183. if( !mActive || !mAwake || !mVisible )
  2184. return;
  2185. if (mSelectedItems.size() == 0)
  2186. return;
  2187. Point2I pt = globalToLocalCoord(event.mousePoint);
  2188. Parent::onMouseMove(event);
  2189. mouseLock();
  2190. mMouseDragged = true;
  2191. // whats our mDragMidPoint?
  2192. mCurrentDragCell = mMouseOverCell.y;
  2193. S32 midpCell = mCurrentDragCell * mItemHeight + (mItemHeight/2);
  2194. S32 currentY = pt.y;
  2195. S32 yDiff = currentY-midpCell;
  2196. S32 variance = (mItemHeight/5);
  2197. if (mPreviousDragCell >= 0)
  2198. mVisibleItems[mPreviousDragCell]->mState.clear( Item::MouseOverBmp | Item::MouseOverText );
  2199. if (mAbs(yDiff) > variance)
  2200. {
  2201. //above or below an item?
  2202. if (yDiff < 0)
  2203. mDragMidPoint = AbovemDragMidPoint;
  2204. else
  2205. mDragMidPoint = BelowmDragMidPoint;
  2206. } else
  2207. {
  2208. mDragMidPoint = NomDragMidPoint;
  2209. // highlight the current item
  2210. // hittest to detect whether we are on an item
  2211. // ganked from onMouseMouse
  2212. // used for tracking what our last cell was so we can clear it.
  2213. mPreviousDragCell = mCurrentDragCell;
  2214. if (mCurrentDragCell >= 0)
  2215. {
  2216. Item* item = NULL;
  2217. BitSet32 hitFlags = 0;
  2218. if ( !hitTest( event.mousePoint, item, hitFlags ) )
  2219. return;
  2220. if ( hitFlags.test( OnImage ) )
  2221. item->mState.set( Item::MouseOverBmp );
  2222. if ( hitFlags.test( OnText ))
  2223. item->mState.set( Item::MouseOverText );
  2224. // Always redraw the entire mouse over item, since we are distinguishing
  2225. // between the bitmap and the text:
  2226. setUpdateRegion( Point2I( mMouseOverCell.x * mCellSize.x, mMouseOverCell.y * mCellSize.y ), mCellSize );
  2227. }
  2228. }
  2229. }
  2230. void GuiTreeViewCtrl::onMiddleMouseDown(const GuiEvent & event)
  2231. {
  2232. //for debugging items
  2233. if (mDebug) {
  2234. Item* item;
  2235. BitSet32 hitFlags = 0;
  2236. hitTest( event.mousePoint, item, hitFlags );
  2237. Con::printf("debugging %d", item->mId);
  2238. Point2I pt = globalToLocalCoord(event.mousePoint);
  2239. if (item->isInspectorData() && item->getObject()) {
  2240. Con::printf("object data:");
  2241. Con::printf("name:%s",item->getObject()->getName());
  2242. Con::printf("className:%s",item->getObject()->getClassName());
  2243. }
  2244. Con::printf("contents of mSelectedItems:");
  2245. for(S32 i = 0; i < mSelectedItems.size(); i++) {
  2246. if (mSelectedItems[i]->isInspectorData()) {
  2247. Con::printf("%d",mSelectedItems[i]->getObject()->getId());
  2248. } else
  2249. Con::printf("wtf %d", mSelectedItems[i]);
  2250. }
  2251. Con::printf("contents of mSelected");
  2252. for (S32 j = 0; j < mSelected.size(); j++) {
  2253. Con::printf("%d", mSelected[j]);
  2254. }
  2255. S32 mCurrentDragCell = mMouseOverCell.y;
  2256. S32 midpCell = (mCurrentDragCell) * mItemHeight + (mItemHeight/2);
  2257. S32 currentY = pt.y;
  2258. S32 yDiff = currentY-midpCell;
  2259. 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);
  2260. }
  2261. }
  2262. void GuiTreeViewCtrl::onMouseDown(const GuiEvent & event)
  2263. {
  2264. if( !mActive || !mAwake || !mVisible )
  2265. {
  2266. Parent::onMouseDown(event);
  2267. return;
  2268. }
  2269. if ( mProfile->mCanKeyFocus )
  2270. setFirstResponder();
  2271. Item * item = 0;
  2272. BitSet32 hitFlags;
  2273. mOldDragY = 0;
  2274. mDragMidPoint = NomDragMidPoint;
  2275. //
  2276. if(!hitTest(event.mousePoint, item, hitFlags))
  2277. return;
  2278. //
  2279. if(event.modifier & SI_CTRL)
  2280. {
  2281. bool selectFlag = item->mState.test(Item::Selected);
  2282. if (selectFlag == true)
  2283. {
  2284. // already selected, so unselect it and remove it
  2285. removeSelection(item->mId);
  2286. if (item->isInspectorData() && item->getObject())
  2287. Con::executef(this,2,"onRemoveSelection",Con::getIntArg(item->getObject()->getId()));
  2288. } else
  2289. {
  2290. // otherwise select it and add it to the list
  2291. // check if it is already on the list.
  2292. /*bool newSelection = true;
  2293. for (S32 i = 0; i < mSelectedItems.size(); i++) {
  2294. if (mSelectedItems[i] == item) {
  2295. newSelection = false;
  2296. }
  2297. }*/
  2298. //if (newSelection) {
  2299. addSelection(item->mId);
  2300. if (item->isInspectorData() && item->getObject())
  2301. Con::executef(this,2,"onAddSelection",Con::getIntArg(item->getObject()->getId()));
  2302. //}
  2303. }
  2304. }
  2305. else if (event.modifier & SI_SHIFT)
  2306. {
  2307. // is something already selected?
  2308. S32 firstSelectedIndex = 0;
  2309. Item * firstItem = NULL;
  2310. if (!mSelectedItems.empty())
  2311. {
  2312. firstItem = mSelectedItems.front();
  2313. for (S32 i = 0; i < mVisibleItems.size();i++)
  2314. {
  2315. if (mVisibleItems[i] == mSelectedItems.front())
  2316. {
  2317. firstSelectedIndex = i;
  2318. break;
  2319. }
  2320. }
  2321. S32 mCurrentDragCell = mMouseOverCell.y;
  2322. if (mVisibleItems[firstSelectedIndex] != firstItem )
  2323. {
  2324. /*
  2325. Con::printf("something isn't right...");
  2326. if (mVisibleItems[firstSelectedIndex]->isInspectorData())
  2327. Con::printf("visibleItem %s",mVisibleItems[firstSelectedIndex]->getObject()->getName());
  2328. if (firstItem->isInspectorData())
  2329. Con::printf("firstItem %s",firstItem->getObject()->getName());
  2330. */
  2331. }
  2332. else
  2333. {
  2334. // select the cells
  2335. if ((mCurrentDragCell) < firstSelectedIndex)
  2336. {
  2337. //select up
  2338. for (S32 j = (mCurrentDragCell); j < firstSelectedIndex; j++) {
  2339. //if the item isn't already selected, then select it
  2340. bool newSelection = true;
  2341. Vector<Item *>::iterator k;
  2342. for(k = mSelectedItems.begin(); k != mSelectedItems.end(); k++)
  2343. {
  2344. if (mVisibleItems[j] == *(k)){
  2345. newSelection = false;
  2346. break;
  2347. }
  2348. }
  2349. if (newSelection)
  2350. {
  2351. addSelection(mVisibleItems[j]->mId);
  2352. if (mVisibleItems[j]->isInspectorData())
  2353. Con::executef(this,2,"onAddSelection",Con::getIntArg(mVisibleItems[j]->getObject()->getId()));
  2354. }
  2355. }
  2356. }
  2357. else
  2358. {
  2359. // select down
  2360. for (S32 j = firstSelectedIndex+1; j < (mCurrentDragCell+1); j++) {
  2361. bool newSelection = true;
  2362. Vector<Item *>::iterator k;
  2363. for(k = mSelectedItems.begin(); k != mSelectedItems.end(); k++) {
  2364. if (mVisibleItems[j] == *(k)){
  2365. newSelection = false;
  2366. break;
  2367. }
  2368. }
  2369. if (newSelection)
  2370. {
  2371. addSelection(mVisibleItems[j]->mId);
  2372. if (mVisibleItems[j]->isInspectorData())
  2373. Con::executef(this,2,"onAddSelection",Con::getIntArg(mVisibleItems[j]->getObject()->getId()));
  2374. }
  2375. }
  2376. }
  2377. }
  2378. }
  2379. }
  2380. else if (event.modifier & SI_ALT)
  2381. {
  2382. if (item->isInspectorData() && item->getObject())
  2383. {
  2384. setInstantGroup(item->getObject());
  2385. mInstantGroup = item->mId;
  2386. }
  2387. } else if (!hitFlags.test(OnImage))
  2388. {
  2389. // first check to see if the item is already selected
  2390. bool newSelection = true;
  2391. Vector<Item *>::iterator k;
  2392. for(k = mSelectedItems.begin(); k != mSelectedItems.end(); k++) {
  2393. if(*(k) == item)
  2394. {
  2395. newSelection = false;
  2396. break;
  2397. }
  2398. }
  2399. // if the item is not already selected then we have a
  2400. //newly selected item, so clear our list of selected items
  2401. if (newSelection)
  2402. {
  2403. clearSelection();
  2404. //Con::executef(this, 1, "onClearSelection");
  2405. //mSelectedItems.clear();
  2406. setItemSelected(item->mId,true);
  2407. }
  2408. }
  2409. if ( hitFlags.test( OnText ) && ( event.mouseClickCount > 1 ) && mAltConsoleCommand[0] )
  2410. Con::evaluate( mAltConsoleCommand );
  2411. if(!item->isParent())
  2412. return;
  2413. //
  2414. if ( mFullRowSelect || hitFlags.test( OnImage ) )
  2415. {
  2416. item->setExpanded(!item->isExpanded());
  2417. if( !item->isInspectorData() && item->mState.test(Item::VirtualParent) )
  2418. onVirtualParentExpand(item);
  2419. scrollVisible(item);
  2420. }
  2421. }
  2422. //------------------------------------------------------------------------------
  2423. void GuiTreeViewCtrl::onMouseMove( const GuiEvent &event )
  2424. {
  2425. if ( mMouseOverCell.y >= 0 && mVisibleItems.size() > mMouseOverCell.y)
  2426. mVisibleItems[mMouseOverCell.y]->mState.clear( Item::MouseOverBmp | Item::MouseOverText );
  2427. Parent::onMouseMove( event );
  2428. if ( mMouseOverCell.y >= 0 )
  2429. {
  2430. Item* item = NULL;
  2431. BitSet32 hitFlags = 0;
  2432. if ( !hitTest( event.mousePoint, item, hitFlags ) )
  2433. return;
  2434. if ( hitFlags.test( OnImage ) )
  2435. item->mState.set( Item::MouseOverBmp );
  2436. if ( hitFlags.test( OnText ))
  2437. item->mState.set( Item::MouseOverText );
  2438. // Always redraw the entire mouse over item, since we are distinguishing
  2439. // between the bitmap and the text:
  2440. setUpdateRegion( Point2I( mMouseOverCell.x * mCellSize.x, mMouseOverCell.y * mCellSize.y ), mCellSize );
  2441. }
  2442. }
  2443. //------------------------------------------------------------------------------
  2444. void GuiTreeViewCtrl::onMouseEnter( const GuiEvent &event )
  2445. {
  2446. Parent::onMouseEnter( event );
  2447. onMouseMove( event );
  2448. }
  2449. //------------------------------------------------------------------------------
  2450. void GuiTreeViewCtrl::onMouseLeave( const GuiEvent &event )
  2451. {
  2452. if ( mMouseOverCell.y >= 0 && mVisibleItems.size() > mMouseOverCell.y)
  2453. mVisibleItems[mMouseOverCell.y]->mState.clear( Item::MouseOverBmp | Item::MouseOverText );
  2454. Parent::onMouseLeave( event );
  2455. }
  2456. //------------------------------------------------------------------------------
  2457. void GuiTreeViewCtrl::onRightMouseDown(const GuiEvent & event)
  2458. {
  2459. if(!mActive)
  2460. {
  2461. Parent::onRightMouseDown(event);
  2462. return;
  2463. }
  2464. Item * item = NULL;
  2465. BitSet32 hitFlags;
  2466. //
  2467. if(!hitTest(event.mousePoint, item, hitFlags))
  2468. return;
  2469. //
  2470. char bufs[2][32];
  2471. dSprintf(bufs[0], 32, "%d", item->mId);
  2472. dSprintf(bufs[1], 32, "%d %d", event.mousePoint.x, event.mousePoint.y);
  2473. if (item->isInspectorData() && item->getObject())
  2474. Con::executef(this,4, "onRightMouseDown", bufs[0],bufs[1],Con::getIntArg(item->getObject()->getId()));
  2475. else
  2476. Con::executef(this, 3, "onRightMouseDown", bufs[0], bufs[1]);
  2477. }
  2478. //------------------------------------------------------------------------------
  2479. void GuiTreeViewCtrl::onRender(Point2I offset, const RectI &updateRect)
  2480. {
  2481. // Get all our contents drawn!
  2482. Parent::onRender(offset,updateRect);
  2483. // Deal with drawing the drag & drop line, if any...
  2484. dglSetClipRect(updateRect);
  2485. // only do it if we have a mDragMidPoint
  2486. if (mDragMidPoint == NomDragMidPoint || !mSupportMouseDragging )
  2487. return;
  2488. ColorF greyLine(0.5,0.5,0.5,1);
  2489. Point2F squarePt;
  2490. glLineWidth(2.f);
  2491. // draw mDragMidPoint lines with a diamond
  2492. if (mDragMidPoint == AbovemDragMidPoint)
  2493. {
  2494. S32 tempY = mItemHeight*mCurrentDragCell+offset.y ;
  2495. squarePt.y = (F32)tempY;
  2496. squarePt.x = (F32)(125.0f+offset.x);
  2497. dglDrawLine(0+offset.x, tempY, 250+offset.x, tempY,greyLine);
  2498. dglDraw2DSquare(squarePt, 6, 90 );
  2499. }
  2500. if (mDragMidPoint == BelowmDragMidPoint)
  2501. {
  2502. S32 tempY2 = mItemHeight*(mCurrentDragCell+1) +offset.y;
  2503. squarePt.y = (F32)tempY2;
  2504. squarePt.x = (F32)(125.0f+offset.x);
  2505. dglDrawLine(0+offset.x, tempY2, 250+offset.x, tempY2,greyLine);
  2506. dglDraw2DSquare(squarePt,6, 90 );
  2507. }
  2508. glLineWidth(1.f);
  2509. }
  2510. void GuiTreeViewCtrl::onRenderCell(Point2I offset, Point2I cell, bool, bool )
  2511. {
  2512. if( !mVisibleItems.size() )
  2513. return;
  2514. // Do some sanity checking and data retrieval.
  2515. AssertFatal(cell.y < mVisibleItems.size(), "GuiTreeViewCtrl::onRenderCell: invalid cell");
  2516. Item * item = mVisibleItems[cell.y];
  2517. // If there's no object, deal with it.
  2518. if(item->isInspectorData())
  2519. if(!item->getObject())
  2520. return;
  2521. RectI drawRect( offset, mCellSize );
  2522. dglClearBitmapModulation();
  2523. FrameAllocatorMarker txtBuff;
  2524. // Ok, we have the item. There are a few possibilities at this point:
  2525. // - We need to draw inheritance lines and a treeview-chosen icon
  2526. // OR
  2527. // - We have to draw an item-dependent icon
  2528. // - If we're mouseover, we have to highlight it.
  2529. //
  2530. // - We have to draw the text for the item
  2531. // - Taking into account various mouseover states
  2532. // - Taking into account the value (set or not)
  2533. // - If it's an inspector data, we have to do some custom rendering
  2534. // Ok, first draw the tab and icon.
  2535. // Do we draw the tree lines?
  2536. if(mFlags.test(ShowTreeLines))
  2537. {
  2538. drawRect.point.x += ( mTabSize * item->mTabLevel );
  2539. Item* parent = item->mParent;
  2540. for ( S32 i = item->mTabLevel; ( parent && i > 0 ); i-- )
  2541. {
  2542. drawRect.point.x -= mTabSize;
  2543. if ( parent->mNext )
  2544. dglDrawBitmapSR( mProfile->mTextureHandle, drawRect.point, mProfile->mBitmapArrayRects[BmpLine] );
  2545. parent = parent->mParent;
  2546. }
  2547. }
  2548. // Now, the icon...
  2549. drawRect.point.x = offset.x + mTabSize * item->mTabLevel;
  2550. // First, draw the rollover glow, if it's an inner node.
  2551. if ( item->isParent() && item->mState.test( Item::MouseOverBmp ) )
  2552. dglDrawBitmapSR( mProfile->mTextureHandle, drawRect.point, mProfile->mBitmapArrayRects[BmpGlow] );
  2553. // Now, do we draw a treeview-selected item or an item dependent one?
  2554. S32 newOffset = 0; // This is stored so we can render glow, then update render pos.
  2555. if(item->isInspectorData())
  2556. {
  2557. S32 bitmap = 0;
  2558. // Ok, draw the treeview lines as appropriate.
  2559. if ( !item->isParent() )
  2560. {
  2561. bitmap = item->mNext ? BmpChild : BmpLastChild;
  2562. }
  2563. else
  2564. {
  2565. bitmap = item->isExpanded() ? BmpExp : BmpCon;
  2566. if ( item->mParent || item->mPrevious )
  2567. bitmap += ( item->mNext ? 3 : 2 );
  2568. else
  2569. bitmap += ( item->mNext ? 1 : 0 );
  2570. }
  2571. if ( ( bitmap >= 0 ) && ( bitmap < mProfile->mBitmapArrayRects.size() ) )
  2572. {
  2573. dglDrawBitmapSR( mProfile->mTextureHandle, drawRect.point, mProfile->mBitmapArrayRects[bitmap] );
  2574. newOffset = mProfile->mBitmapArrayRects[bitmap].extent.x;
  2575. }
  2576. // draw lock icon if need be
  2577. S32 icon = Lock1;
  2578. S32 icon2 = Hidden;
  2579. if (item->getObject() && item->getObject()->isLocked())
  2580. {
  2581. if (mIconTable[icon])
  2582. {
  2583. //drawRect.point.x = offset.x + mTabSize * item->mTabLevel + mIconTable[icon].getWidth();
  2584. drawRect.point.x += mIconTable[icon].getWidth();
  2585. dglDrawBitmap( mIconTable[icon], drawRect.point, 0 );
  2586. }
  2587. }
  2588. if (item->getObject() && item->getObject()->isHidden())
  2589. {
  2590. if (mIconTable[icon2])
  2591. {
  2592. //drawRect.point.x = offset.x + mTabSize * item->mTabLevel + mIconTable[icon].getWidth();
  2593. drawRect.point.x += mIconTable[icon2].getWidth();
  2594. dglDrawBitmap( mIconTable[icon2], drawRect.point, 0 );
  2595. }
  2596. }
  2597. SimObject * pObject = item->getObject();
  2598. SimGroup * pGroup = ( pObject == NULL ) ? NULL : dynamic_cast<SimGroup*>( pObject );
  2599. // draw the icon associated with the item
  2600. if (item->isParent())
  2601. {
  2602. if ( pGroup != NULL)
  2603. {
  2604. if (item->isExpanded())
  2605. item->mIcon = SimGroup1;
  2606. else
  2607. item->mIcon = SimGroup2;
  2608. }
  2609. else
  2610. item->mIcon = SimGroup2;
  2611. }
  2612. if (mInstantGroup == item->mId)
  2613. {
  2614. if (item->isInspectorData())
  2615. {
  2616. if ( pGroup != NULL )
  2617. {
  2618. if (item->isExpanded())
  2619. item->mIcon = SimGroup3;
  2620. else
  2621. item->mIcon = SimGroup4;
  2622. }
  2623. }
  2624. }
  2625. if (item->mIcon)
  2626. {
  2627. if (mIconTable[item->mIcon])
  2628. {
  2629. S32 iconHeight = (mItemHeight - mIconTable[item->mIcon].getHeight()) / 2;
  2630. S32 oldHeight = drawRect.point.y;
  2631. if(iconHeight > 0)
  2632. drawRect.point.y += iconHeight;
  2633. drawRect.point.x += mIconTable[item->mIcon].getWidth();
  2634. dglDrawBitmap( mIconTable[item->mIcon], drawRect.point, 0 );
  2635. drawRect.point.y = oldHeight;
  2636. }
  2637. }
  2638. }
  2639. else
  2640. {
  2641. S32 bitmap = 0;
  2642. // Ok, draw the treeview lines as appropriate.
  2643. if ( !item->isParent() )
  2644. bitmap = item->mNext ? BmpChild : BmpLastChild;
  2645. else
  2646. {
  2647. bitmap = item->isExpanded() ? BmpExp : BmpCon;
  2648. if ( item->mParent || item->mPrevious )
  2649. bitmap += ( item->mNext ? 3 : 2 );
  2650. else
  2651. bitmap += ( item->mNext ? 1 : 0 );
  2652. }
  2653. if ( ( bitmap >= 0 ) && ( bitmap < mProfile->mBitmapArrayRects.size() ) )
  2654. {
  2655. dglDrawBitmapSR( mProfile->mTextureHandle, drawRect.point, mProfile->mBitmapArrayRects[bitmap] );
  2656. newOffset = mProfile->mBitmapArrayRects[bitmap].extent.x;
  2657. }
  2658. S32 icon = item->isExpanded() ? item->mScriptInfo.mExpandedImage : item->mScriptInfo.mNormalImage;
  2659. if ( icon )
  2660. {
  2661. if (mIconTable[icon])
  2662. {
  2663. S32 iconHeight = (mItemHeight - mIconTable[icon].getHeight()) / 2;
  2664. S32 oldHeight = drawRect.point.y;
  2665. if(iconHeight > 0)
  2666. drawRect.point.y += iconHeight;
  2667. drawRect.point.x += mIconTable[icon].getWidth();
  2668. dglDrawBitmap( mIconTable[icon], drawRect.point, 0 );
  2669. drawRect.point.y = oldHeight;
  2670. }
  2671. }
  2672. }
  2673. // Ok, update offset so we can render some text!
  2674. drawRect.point.x += newOffset;
  2675. // Ok, now we're off to rendering the actual data for the treeview item.
  2676. U32 bufLen = item->mDataRenderWidth + 1;
  2677. char *displayText = (char *)txtBuff.alloc(bufLen);
  2678. displayText[bufLen-1] = 0;
  2679. item->getDisplayText(bufLen, displayText);
  2680. // Draw the rollover/selected bitmap, if one was specified.
  2681. drawRect.extent.x = mProfile->mFont->getStrWidth( displayText ) + ( 2 * mTextOffset );
  2682. if ( item->mState.test( Item::Selected ) && mTexSelected )
  2683. dglDrawBitmapStretch( mTexSelected, drawRect );
  2684. else if ( item->mState.test( Item::MouseOverText ) && mTexRollover )
  2685. dglDrawBitmapStretch( mTexRollover, drawRect );
  2686. // Offset a bit so as to space text properly.
  2687. drawRect.point.x += mTextOffset;
  2688. // Determine what color the font should be.
  2689. ColorI fontColor;
  2690. fontColor = item->mState.test( Item::Selected ) ? mProfile->mFontColorSL :
  2691. ( item->mState.test( Item::MouseOverText ) ? mProfile->mFontColorHL : mProfile->mFontColor );
  2692. if (item->mState.test(Item::Selected))
  2693. {
  2694. dglDrawRectFill(drawRect, mProfile->mFillColorHL);
  2695. }
  2696. else if (item->mState.test(Item::MouseOverText))
  2697. {
  2698. dglDrawRectFill(drawRect, mProfile->mFontColorNA);
  2699. }
  2700. if( mInstantGroup == item->mId)
  2701. {
  2702. fontColor = mProfile->mFontColorHL;
  2703. }
  2704. dglSetBitmapModulation( fontColor );
  2705. // Center the text horizontally.
  2706. S32 height = (mItemHeight - mProfile->mFont->getHeight()) / 2;
  2707. if(height > 0)
  2708. drawRect.point.y += height;
  2709. // JDD - offset by two pixels or so to keep the text from rendering RIGHT ONTOP of the outline
  2710. drawRect.point.x += 2;
  2711. dglDrawText( mProfile->mFont, drawRect.point, displayText, mProfile->mFontColors );
  2712. }
  2713. //------------------------------------------------------------------------------
  2714. void GuiTreeViewCtrl::clearSelection()
  2715. {
  2716. while (!mSelectedItems.empty())
  2717. {
  2718. if(!setItemSelected(mSelectedItems.last()->mId, false))
  2719. mSelectedItems.pop_back();
  2720. }
  2721. mSelectedItems.clear();
  2722. mSelected.clear();
  2723. Con::executef(this, 1, "onClearSelection");
  2724. }
  2725. void GuiTreeViewCtrl::lockSelection(bool lock)
  2726. {
  2727. for(U32 i = 0; i < (U32)mSelectedItems.size(); i++)
  2728. {
  2729. if(mSelectedItems[i]->isInspectorData())
  2730. mSelectedItems[i]->getObject()->setLocked(lock);
  2731. }
  2732. }
  2733. void GuiTreeViewCtrl::hideSelection(bool hide)
  2734. {
  2735. for(U32 i = 0; i < (U32)mSelectedItems.size(); i++)
  2736. {
  2737. if(mSelectedItems[i]->isInspectorData())
  2738. mSelectedItems[i]->getObject()->setHidden(hide);
  2739. }
  2740. }
  2741. //------------------------------------------------------------------------------
  2742. // handles icon assignments
  2743. S32 GuiTreeViewCtrl::getIcon(const char * iconString)
  2744. {
  2745. if( iconString == NULL )
  2746. return Default;
  2747. S32 icon = Default;
  2748. if (!dStrcmp(iconString, "SimGroup"))
  2749. icon = SimGroup1;
  2750. return icon;
  2751. }
  2752. void GuiTreeViewCtrl::addInspectorDataItem(Item *parent, SimObject *obj)
  2753. {
  2754. S32 icon = getIcon(obj->getClassName());
  2755. Item *item = createItem(icon);
  2756. item->mState.set(Item::InspectorData);
  2757. // Deal with child objects...
  2758. if(dynamic_cast<SimSet*>(obj))
  2759. item->mState.set(Item::VirtualParent);
  2760. // Actually store the data!
  2761. item->setObject(obj);
  2762. // Now add us to the data structure...
  2763. if(parent)
  2764. {
  2765. // Add as child of parent.
  2766. if(parent->mChild)
  2767. {
  2768. Item * traverse = parent->mChild;
  2769. while(traverse->mNext)
  2770. traverse = traverse->mNext;
  2771. traverse->mNext = item;
  2772. item->mPrevious = traverse;
  2773. }
  2774. else
  2775. parent->mChild = item;
  2776. item->mParent = parent;
  2777. }
  2778. else
  2779. {
  2780. // If no parent, add to root.
  2781. item->mNext = mRoot;
  2782. mRoot = item;
  2783. item->mParent = NULL;
  2784. }
  2785. if(!parent || parent->isExpanded())
  2786. mFlags.set(RebuildVisible);
  2787. buildVisibleTree();
  2788. }
  2789. void GuiTreeViewCtrl::unlinkItem(Item * item)
  2790. {
  2791. if (item->mPrevious)
  2792. item->mPrevious->mNext = item->mNext;
  2793. if (item->mNext)
  2794. item->mNext->mPrevious = item->mPrevious;
  2795. }
  2796. bool GuiTreeViewCtrl::childSearch(Item * item, SimObject *obj, bool yourBaby)
  2797. {
  2798. Item * temp = item->mChild;
  2799. while (temp)
  2800. {
  2801. //do you have my baby?
  2802. if (temp->isInspectorData())
  2803. {
  2804. if (temp->getObject() == obj)
  2805. yourBaby = false; //probably a child of an inner script
  2806. }
  2807. yourBaby = childSearch(temp,obj, yourBaby);
  2808. temp = temp->mNext;
  2809. }
  2810. return yourBaby;
  2811. }
  2812. void GuiTreeViewCtrl::inspectorSearch(Item * item, Item * parent, SimSet * parentSet, SimSet * newParentSet)
  2813. {
  2814. if (!parentSet||!newParentSet)
  2815. return;
  2816. if (item == parent->mNext)
  2817. return;
  2818. if (item)
  2819. {
  2820. if (item->isInspectorData())
  2821. {
  2822. // remove the object from the parentSet and add it to the newParentSet
  2823. SimObject* simObj = item->getObject();
  2824. if (parentSet->size())
  2825. {
  2826. SimObject *lastObject = parentSet->last();
  2827. parentSet->removeObject(simObj);
  2828. parentSet->reOrder(lastObject);
  2829. }
  2830. else
  2831. parentSet->removeObject(simObj);
  2832. newParentSet->addObject(simObj);
  2833. if (item->mNext)
  2834. {
  2835. inspectorSearch(item->mNext, parent, parentSet, newParentSet);
  2836. return;
  2837. }
  2838. else
  2839. {
  2840. // end of children so backing up
  2841. if (item->mParent == parent)
  2842. return;
  2843. else
  2844. {
  2845. inspectorSearch(item->mParent->mNext, parent, parentSet, newParentSet);
  2846. return;
  2847. }
  2848. }
  2849. }
  2850. if (item->mChild)
  2851. {
  2852. inspectorSearch(item->mChild, parent, parentSet, newParentSet);
  2853. return;
  2854. }
  2855. if (item->mNext)
  2856. {
  2857. inspectorSearch(item->mNext, parent, parentSet, newParentSet);
  2858. return;
  2859. }
  2860. }
  2861. }
  2862. bool GuiTreeViewCtrl::onVirtualParentBuild(Item *item, bool bForceFullUpdate)
  2863. {
  2864. if(!item->mState.test(Item::InspectorData))
  2865. return true;
  2866. // Blast an item if it doesn't have a corresponding SimObject...
  2867. if(item->mInspectorInfo.mObject == NULL)
  2868. {
  2869. removeItem(item->mId);
  2870. return false;
  2871. }
  2872. // Skip the next stuff unless we're expanded...
  2873. if(!item->isExpanded() && !bForceFullUpdate)
  2874. return true;
  2875. // Verify that we have all the kids we should in here...
  2876. SimSet *srcObj = dynamic_cast<SimSet*>(&(*item->mInspectorInfo.mObject));
  2877. // If it's not a SimSet... WTF are we doing here?
  2878. if(!srcObj)
  2879. return true;
  2880. SimSet::iterator i;
  2881. // This is slow but probably ok.
  2882. for(i = srcObj->begin(); i != srcObj->end(); i++)
  2883. {
  2884. SimObject *obj = *i;
  2885. // If we can't find it, add it.
  2886. // unless it has a parent that is a child that is a script
  2887. Item *res = item->findChildByValue(obj);
  2888. bool foundChild = true;
  2889. // search the children. if any of them are the parent of the object then don't add it.
  2890. foundChild = childSearch(item,obj,foundChild);
  2891. if(!res && foundChild)
  2892. {
  2893. if (mDebug) Con::printf("adding something");
  2894. addInspectorDataItem(item, obj);
  2895. }
  2896. }
  2897. return true;
  2898. }
  2899. bool GuiTreeViewCtrl::onVirtualParentExpand(Item *item)
  2900. {
  2901. // Do nothing...
  2902. return true;
  2903. }
  2904. bool GuiTreeViewCtrl::onVirtualParentCollapse(Item *item)
  2905. {
  2906. // Do nothing...
  2907. return true;
  2908. }
  2909. void GuiTreeViewCtrl::inspectObject(SimObject *obj, bool okToEdit)
  2910. {
  2911. destroyTree();
  2912. mFlags.set(IsEditable, okToEdit);
  2913. //build our icon table
  2914. const char * res = Con::executef(this, 1, "onDefineIcons");
  2915. if(!(dAtob(res)))
  2916. {
  2917. // if no icons were defined in script then use defaults.
  2918. buildIconTable(NULL);
  2919. }
  2920. addInspectorDataItem(NULL, obj);
  2921. }
  2922. S32 GuiTreeViewCtrl::findItemByName(const char *name)
  2923. {
  2924. for (S32 i = 0; i < mItems.size(); i++)
  2925. {
  2926. if (mItems[i] != NULL)
  2927. {
  2928. if (dStrcmp(mItems[i]->getText(),name) == 0)
  2929. return mItems[i]->mId;
  2930. }
  2931. }
  2932. return 0;
  2933. }
  2934. StringTableEntry GuiTreeViewCtrl::getTextToRoot( S32 itemId, const char * delimiter )
  2935. {
  2936. Item * item = getItem(itemId);
  2937. if(!item)
  2938. {
  2939. Con::errorf(ConsoleLogEntry::General, "GuiTreeViewCtrl::getTextToRoot: invalid start item id!");
  2940. return StringTable->EmptyString;
  2941. }
  2942. if(item->isInspectorData())
  2943. {
  2944. Con::errorf(ConsoleLogEntry::General, "GuiTreeViewCtrl::getTextToRoot: cannot get text to root of inspector data items");
  2945. return StringTable->EmptyString;
  2946. }
  2947. char bufferOne[1024];
  2948. char bufferTwo[1024];
  2949. char bufferNodeText[128];
  2950. dMemset( bufferOne, 0, sizeof(bufferOne) );
  2951. dMemset( bufferTwo, 0, sizeof(bufferTwo) );
  2952. dStrcpy( bufferOne, item->getText() );
  2953. Item *prevNode = item->mParent;
  2954. while ( prevNode )
  2955. {
  2956. dMemset( bufferNodeText, 0, sizeof(bufferNodeText) );
  2957. dStrcpy( bufferNodeText, prevNode->getText() );
  2958. dSprintf( bufferTwo, 1024, "%s%s%s",bufferNodeText, delimiter, bufferOne );
  2959. dStrcpy( bufferOne, bufferTwo );
  2960. dMemset( bufferTwo, 0, sizeof(bufferTwo) );
  2961. prevNode = prevNode->mParent;
  2962. }
  2963. // Return the result, StringTable-ized.
  2964. return StringTable->insert( bufferOne );
  2965. }
  2966. //------------------------------------------------------------------------------
  2967. ConsoleMethod(GuiTreeViewCtrl, findItemByName, S32, 3, 3, "(name) Find item by name\n"
  2968. "@param name The name of the desired object.\n"
  2969. "@return Returns the ID of the object, or -1 on failure (not found).")
  2970. {
  2971. return(object->findItemByName(argv[2]));
  2972. }
  2973. ConsoleMethod(GuiTreeViewCtrl, insertItem, S32, 4, 8, "(TreeItemId parent, name, value, icon, normalImage=0, expandedImage=0) Adds item to tree control.\n"
  2974. "@param parent The new item's parent.\n"
  2975. "@param name The name of the new item.\n"
  2976. "@param value The new item's value.\n"
  2977. "@param icon The new item's icon\n"
  2978. "@return Returns the new item's ID.")
  2979. {
  2980. S32 norm=0, expand=0;
  2981. if (argc > 6)
  2982. {
  2983. norm = dAtoi(argv[6]);
  2984. if(argc > 7)
  2985. expand = dAtoi(argv[7]);
  2986. }
  2987. return(object->insertItem(dAtoi(argv[2]), argv[3], argv[4], argv[5], norm, expand));
  2988. }
  2989. ConsoleMethod(GuiTreeViewCtrl, lockSelection, void, 2, 3, "([bool lock]) Set whether the selection is to be locked."
  2990. "@param lock Boolean flag for whether or not the current selected object should be locked\n"
  2991. "@return No return value.")
  2992. {
  2993. bool lock = true;
  2994. if(argc == 3)
  2995. lock = dAtob(argv[2]);
  2996. object->lockSelection(lock);
  2997. }
  2998. ConsoleMethod(GuiTreeViewCtrl, clearSelection, void, 2, 2, "() Clear selection\n"
  2999. "@return No return value.")
  3000. {
  3001. object->clearSelection();
  3002. }
  3003. ConsoleMethod(GuiTreeViewCtrl, deleteSelection, void, 2, 2, "() Delete all selected items.\n"
  3004. "@return No return value.\n")
  3005. {
  3006. object->deleteSelection();
  3007. }
  3008. ConsoleMethod(GuiTreeViewCtrl, addSelection, void, 3, 3, "(string ID) Select an item"
  3009. "@param ID The ID of the item to select.\n"
  3010. "@return No return value.")
  3011. {
  3012. S32 id = dAtoi(argv[2]);
  3013. object->addSelection(id);
  3014. }
  3015. ConsoleMethod(GuiTreeViewCtrl, addChildSelectionByValue, void, 4, 4, "(TreeItemId parent, value)")
  3016. {
  3017. S32 id = dAtoi(argv[2]);
  3018. GuiTreeViewCtrl::Item* parentItem = object->getItem(id);
  3019. if (parentItem)
  3020. {
  3021. GuiTreeViewCtrl::Item* child = parentItem->findChildByValue(argv[3]);
  3022. if (child)
  3023. object->addSelection(child->getID());
  3024. }
  3025. }
  3026. ConsoleMethod(GuiTreeViewCtrl, removeSelection, void, 3, 3, "(string ID) Deselects given item.\n"
  3027. "@param ID The ID of the item to deselect.\n"
  3028. "@return No return value.")
  3029. {
  3030. S32 id = dAtoi(argv[2]);
  3031. object->removeSelection(id);
  3032. }
  3033. ConsoleMethod(GuiTreeViewCtrl, removeChildSelectionByValue, void, 4, 4, "removeChildSelectionByValue(TreeItemId parent, value)")
  3034. {
  3035. S32 id = dAtoi(argv[2]);
  3036. GuiTreeViewCtrl::Item* parentItem = object->getItem(id);
  3037. if(parentItem)
  3038. {
  3039. GuiTreeViewCtrl::Item* child = parentItem->findChildByValue(argv[3]);
  3040. if(child)
  3041. object->removeSelection(child->getID());
  3042. }
  3043. }
  3044. ConsoleMethod(GuiTreeViewCtrl, selectItem, bool, 3, 4, "(TreeItemId item, [bool select=true]) Selects item.")
  3045. {
  3046. S32 id = dAtoi(argv[2]);
  3047. bool select = true;
  3048. if(argc == 4)
  3049. select = dAtob(argv[3]);
  3050. return(object->setItemSelected(id, select));
  3051. }
  3052. ConsoleMethod(GuiTreeViewCtrl, expandItem, bool, 3, 4, "(TreeItemId item, [bool expand=true]) Deselects item")
  3053. {
  3054. S32 id = dAtoi(argv[2]);
  3055. bool expand = true;
  3056. if(argc == 4)
  3057. expand = dAtob(argv[3]);
  3058. return(object->setItemExpanded(id, expand));
  3059. }
  3060. // Make the given item visible.
  3061. ConsoleMethod(GuiTreeViewCtrl, scrollVisible, void, 3, 3, "(TreeItemId item) Make the given item visible.\n"
  3062. "@param ID of the desired item.\n"
  3063. "@return No return value.")
  3064. {
  3065. object->scrollVisible(dAtoi(argv[2]));
  3066. }
  3067. ConsoleMethod(GuiTreeViewCtrl, buildIconTable, bool, 3,3, "(string icons) Icons should be designated by the bitmap/png file names (minus the file extensions) "
  3068. "and separated by colons (:). This list should be synchronized with the Icons enum.\n"
  3069. "@param icons The list of icons to add sepated by colons.\n"
  3070. "@return Returns true on success, false otherwise.")
  3071. {
  3072. const char * icons = argv[2];
  3073. return object->buildIconTable(icons);
  3074. }
  3075. 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.")
  3076. {
  3077. SimSet *treeRoot = NULL;
  3078. SimObject* target = Sim::findObject(argv[2]);
  3079. bool okToEdit = true;
  3080. if (argc == 4)
  3081. okToEdit = dAtob(argv[3]);
  3082. if (target)
  3083. treeRoot = dynamic_cast<SimSet*>(target);
  3084. if (! treeRoot)
  3085. Sim::findObject(RootGroupId, treeRoot);
  3086. object->inspectObject(treeRoot,okToEdit);
  3087. }
  3088. ConsoleMethod(GuiTreeViewCtrl, getItemText, const char *, 3, 3, "(TreeItemId item)")
  3089. {
  3090. return(object->getItemText(dAtoi(argv[2])));
  3091. }
  3092. ConsoleMethod(GuiTreeViewCtrl, getItemValue, const char *, 3, 3, "(TreeItemId item)")
  3093. {
  3094. return(object->getItemValue(dAtoi(argv[2])));
  3095. }
  3096. ConsoleMethod(GuiTreeViewCtrl, editItem, bool, 5, 5, "(TreeItemId item, string newText, string newValue)")
  3097. {
  3098. return(object->editItem(dAtoi(argv[2]), argv[3], argv[4]));
  3099. }
  3100. ConsoleMethod(GuiTreeViewCtrl, removeItem, bool, 3, 3, "(TreeItemId item)")
  3101. {
  3102. return(object->removeItem(dAtoi(argv[2])));
  3103. }
  3104. ConsoleMethod(GuiTreeViewCtrl, removeAllChildren, void, 3, 3, "removeAllChildren(TreeItemId parent)")
  3105. {
  3106. object->removeAllChildren(dAtoi(argv[2]));
  3107. }
  3108. ConsoleMethod(GuiTreeViewCtrl, clear, void, 2, 2, "() - empty tree")
  3109. {
  3110. object->removeItem(0);
  3111. }
  3112. ConsoleMethod(GuiTreeViewCtrl, getFirstRootItem, S32, 2, 2, "Get id for root item.")
  3113. {
  3114. return(object->getFirstRootItem());
  3115. }
  3116. ConsoleMethod(GuiTreeViewCtrl, getChild, S32, 3, 3, "(TreeItemId item)")
  3117. {
  3118. return(object->getChildItem(dAtoi(argv[2])));
  3119. }
  3120. ConsoleMethod(GuiTreeViewCtrl, buildVisibleTree, void, 3, 3, "Build the visible tree")
  3121. {
  3122. object->buildVisibleTree(dAtob(argv[2]));
  3123. }
  3124. ConsoleMethod(GuiTreeViewCtrl, getParent, S32, 3, 3, "(TreeItemId item)")
  3125. {
  3126. return(object->getParentItem(dAtoi(argv[2])));
  3127. }
  3128. ConsoleMethod(GuiTreeViewCtrl, getNextSibling, S32, 3, 3, "(TreeItemId item)")
  3129. {
  3130. return(object->getNextSiblingItem(dAtoi(argv[2])));
  3131. }
  3132. ConsoleMethod(GuiTreeViewCtrl, getPrevSibling, S32, 3, 3, "(TreeItemId item)")
  3133. {
  3134. return(object->getPrevSiblingItem(dAtoi(argv[2])));
  3135. }
  3136. ConsoleMethod(GuiTreeViewCtrl, getItemCount, S32, 2, 2, "() @return Returns the number of items in control")
  3137. {
  3138. return(object->getItemCount());
  3139. }
  3140. ConsoleMethod(GuiTreeViewCtrl, getSelectedItem, S32, 2, 2, "() @return Returns the ID of the selected item.")
  3141. {
  3142. return ( object->getSelectedItem() );
  3143. }
  3144. ConsoleMethod(GuiTreeViewCtrl, getSelectedObject, S32, 2, 2, "() @return Returns the currently selected simObject in inspector mode or -1")
  3145. {
  3146. GuiTreeViewCtrl::Item *item = object->getItem( object->getSelectedItem() );
  3147. if( item != NULL && item->isInspectorData() )
  3148. {
  3149. SimObject *obj = item->getObject();
  3150. if( obj != NULL )
  3151. return object->getId();
  3152. }
  3153. return -1;
  3154. }
  3155. ConsoleMethod(GuiTreeViewCtrl, moveItemUp, void, 3, 3, "(TreeItemId item)")
  3156. {
  3157. object->moveItemUp( dAtoi( argv[2] ) );
  3158. }
  3159. ConsoleMethod(GuiTreeViewCtrl, getSelectedItemsCount, S32, 2, 2, "")
  3160. {
  3161. return ( object->getSelectedItemsCount() );
  3162. }
  3163. ConsoleMethod(GuiTreeViewCtrl, moveItemDown, void, 3, 3, "(TreeItemId item)")
  3164. {
  3165. object->moveItemDown( dAtoi( argv[2] ) );
  3166. }
  3167. //-----------------------------------------------------------------------------
  3168. 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")
  3169. {
  3170. if ( argc < 4 )
  3171. {
  3172. Con::warnf("GuiTreeViewCtrl::getTextToRoot - Invalid number of arguments!");
  3173. return ("");
  3174. }
  3175. S32 itemId = dAtoi( argv[2] );
  3176. StringTableEntry delimiter = argv[3];
  3177. return object->getTextToRoot( itemId, delimiter );
  3178. }
  3179. ConsoleMethod(GuiTreeViewCtrl,getSelectedItemList,const char*, 2,2,"returns a space seperated list of mulitple item ids")
  3180. {
  3181. char* buff = Con::getReturnBuffer(1024);
  3182. dSprintf(buff,1024,"");
  3183. for(int i = 0; i < object->mSelected.size(); i++)
  3184. {
  3185. S32 id = object->mSelected[i];
  3186. //get the current length of the buffer
  3187. U32 len = dStrlen(buff);
  3188. //the start of the buffer where we want to write
  3189. char* buffPart = buff+len;
  3190. //the size of the remaining buffer (-1 cause dStrlen doesn't count the \0)
  3191. S32 size = 1024-len-1;
  3192. //write it:
  3193. if(size < 1)
  3194. {
  3195. Con::errorf("GuiTreeViewCtrl::getSelectedItemList - Not enough room to return our object list");
  3196. return buff;
  3197. }
  3198. dSprintf(buffPart,size,"%d ", id);
  3199. }
  3200. //mSelected
  3201. return buff;
  3202. }
  3203. //------------------------------------------------------------------------------
  3204. S32 GuiTreeViewCtrl::findItemByObjectId(S32 iObjId)
  3205. {
  3206. for (S32 i = 0; i < mItems.size(); i++)
  3207. {
  3208. if (mItems[i] != NULL)
  3209. {
  3210. SimObject* pObj = mItems[i]->getObject();
  3211. if(pObj && pObj->getId() == iObjId)
  3212. return mItems[i]->mId;
  3213. }
  3214. }
  3215. return -1;
  3216. }
  3217. //------------------------------------------------------------------------------
  3218. ConsoleMethod(GuiTreeViewCtrl, findItemByObjectId, S32, 3, 3, "(find item by object id and returns the mId)")
  3219. {
  3220. return(object->findItemByObjectId(dAtoi(argv[2])));
  3221. }
  3222. //------------------------------------------------------------------------------
  3223. bool GuiTreeViewCtrl::scrollVisibleByObjectId(S32 objID)
  3224. {
  3225. S32 itemID = findItemByObjectId(objID);
  3226. if(itemID == -1)
  3227. {
  3228. // we did not find the item in our current items
  3229. // we should try to find and show the parent of the item.
  3230. SimObject *obj = Sim::findObject(objID);
  3231. if(!obj || !obj->getGroup())
  3232. return false;
  3233. // if we can't show the parent, we fail.
  3234. if(! scrollVisibleByObjectId(obj->getGroup()->getId()) )
  3235. return false;
  3236. // get the parent. expand the parent. rebuild the tree. this ensures that
  3237. // we'll be able to find the child item we're targeting.
  3238. S32 parentID = findItemByObjectId(obj->getGroup()->getId());
  3239. AssertFatal(parentID != -1, "We were able to show the parent, but could not then find the parent. This should not happen.");
  3240. Item *parentItem = getItem(parentID);
  3241. parentItem->setExpanded(true);
  3242. buildVisibleTree();
  3243. // NOW we should be able to find the object. if not... something's wrong.
  3244. itemID = findItemByObjectId(objID);
  3245. AssertWarn(itemID != -1,"GuiTreeViewCtrl::scrollVisibleByObjectId() found the parent, but can't find it's immediate child. This should not happen.");
  3246. if(itemID == -1)
  3247. return false;
  3248. }
  3249. // ok, item found. scroll to it.
  3250. scrollVisible(itemID);
  3251. return true;
  3252. }
  3253. //------------------------------------------------------------------------------
  3254. ConsoleMethod(GuiTreeViewCtrl, scrollVisibleByObjectId, S32, 3, 3, "(show item by object id. returns true if sucessful.)")
  3255. {
  3256. return(object->scrollVisibleByObjectId(dAtoi(argv[2])));
  3257. }