2
0

guiTreeViewCtrl.cpp 170 KB

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