guiTreeViewCtrl.cpp 166 KB

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