guiTreeViewCtrl.cpp 157 KB

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