2
0

guiTreeViewCtrl.cc 116 KB

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