property_editor.cpp 119 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622
  1. /*************************************************************************/
  2. /* property_editor.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md) */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "property_editor.h"
  31. #include "core/class_db.h"
  32. #include "core/io/image_loader.h"
  33. #include "core/io/marshalls.h"
  34. #include "core/io/resource_loader.h"
  35. #include "core/os/input.h"
  36. #include "core/os/keyboard.h"
  37. #include "core/pair.h"
  38. #include "core/print_string.h"
  39. #include "core/project_settings.h"
  40. #include "editor/array_property_edit.h"
  41. #include "editor/create_dialog.h"
  42. #include "editor/editor_export.h"
  43. #include "editor/editor_file_system.h"
  44. #include "editor/editor_help.h"
  45. #include "editor/editor_node.h"
  46. #include "editor/editor_settings.h"
  47. #include "editor/multi_node_edit.h"
  48. #include "editor/property_selector.h"
  49. #include "scene/gui/label.h"
  50. #include "scene/main/viewport.h"
  51. #include "scene/resources/font.h"
  52. #include "scene/resources/packed_scene.h"
  53. #include "scene/scene_string_names.h"
  54. void EditorResourceConversionPlugin::_bind_methods() {
  55. MethodInfo mi;
  56. mi.name = "_convert";
  57. mi.return_val.type = Variant::OBJECT;
  58. mi.return_val.class_name = "Resource";
  59. mi.return_val.hint = PROPERTY_HINT_RESOURCE_TYPE;
  60. mi.return_val.hint_string = "Resource";
  61. mi.arguments.push_back(mi.return_val);
  62. mi.arguments[0].name = "resource";
  63. BIND_VMETHOD(mi)
  64. mi.name = "_handles";
  65. mi.return_val = PropertyInfo(Variant::BOOL, "");
  66. BIND_VMETHOD(MethodInfo(Variant::BOOL, "_converts_to"));
  67. }
  68. String EditorResourceConversionPlugin::converts_to() const {
  69. if (get_script_instance())
  70. return get_script_instance()->call("_converts_to");
  71. return "";
  72. }
  73. bool EditorResourceConversionPlugin::handles(const Ref<Resource> &p_resource) const {
  74. if (get_script_instance())
  75. return get_script_instance()->call("_handles", p_resource);
  76. return false;
  77. }
  78. Ref<Resource> EditorResourceConversionPlugin::convert(const Ref<Resource> &p_resource) {
  79. if (get_script_instance())
  80. return get_script_instance()->call("_convert", p_resource);
  81. return Ref<Resource>();
  82. }
  83. void CustomPropertyEditor::_notification(int p_what) {
  84. if (p_what == NOTIFICATION_DRAW) {
  85. RID ci = get_canvas_item();
  86. get_stylebox("panel", "PopupMenu")->draw(ci, Rect2(Point2(), get_size()));
  87. }
  88. if (p_what == MainLoop::NOTIFICATION_WM_QUIT_REQUEST) {
  89. hide();
  90. }
  91. }
  92. void CustomPropertyEditor::_menu_option(int p_which) {
  93. switch (type) {
  94. case Variant::INT: {
  95. if (hint == PROPERTY_HINT_FLAGS) {
  96. int val = v;
  97. if (val & (1 << p_which)) {
  98. val &= ~(1 << p_which);
  99. } else {
  100. val |= (1 << p_which);
  101. }
  102. v = val;
  103. emit_signal("variant_changed");
  104. } else if (hint == PROPERTY_HINT_ENUM) {
  105. v = p_which;
  106. emit_signal("variant_changed");
  107. }
  108. } break;
  109. case Variant::STRING: {
  110. if (hint == PROPERTY_HINT_ENUM) {
  111. v = hint_text.get_slice(",", p_which);
  112. emit_signal("variant_changed");
  113. }
  114. } break;
  115. case Variant::OBJECT: {
  116. switch (p_which) {
  117. case OBJ_MENU_LOAD: {
  118. file->set_mode(EditorFileDialog::MODE_OPEN_FILE);
  119. String type = (hint == PROPERTY_HINT_RESOURCE_TYPE) ? hint_text : String();
  120. List<String> extensions;
  121. for (int i = 0; i < type.get_slice_count(","); i++) {
  122. ResourceLoader::get_recognized_extensions_for_type(type.get_slice(",", i), &extensions);
  123. }
  124. Set<String> valid_extensions;
  125. for (List<String>::Element *E = extensions.front(); E; E = E->next()) {
  126. valid_extensions.insert(E->get());
  127. }
  128. file->clear_filters();
  129. for (Set<String>::Element *E = valid_extensions.front(); E; E = E->next()) {
  130. file->add_filter("*." + E->get() + " ; " + E->get().to_upper());
  131. }
  132. file->popup_centered_ratio();
  133. } break;
  134. case OBJ_MENU_EDIT: {
  135. RefPtr RefPtr = v;
  136. if (!RefPtr.is_null()) {
  137. emit_signal("resource_edit_request");
  138. hide();
  139. }
  140. } break;
  141. case OBJ_MENU_CLEAR: {
  142. v = Variant();
  143. emit_signal("variant_changed");
  144. hide();
  145. } break;
  146. case OBJ_MENU_MAKE_UNIQUE: {
  147. RefPtr RefPtr = v;
  148. Ref<Resource> res_orig = RefPtr;
  149. if (res_orig.is_null())
  150. return;
  151. List<PropertyInfo> property_list;
  152. res_orig->get_property_list(&property_list);
  153. List<Pair<String, Variant> > propvalues;
  154. for (List<PropertyInfo>::Element *E = property_list.front(); E; E = E->next()) {
  155. Pair<String, Variant> p;
  156. PropertyInfo &pi = E->get();
  157. if (pi.usage & PROPERTY_USAGE_STORAGE) {
  158. p.first = pi.name;
  159. p.second = res_orig->get(pi.name);
  160. }
  161. propvalues.push_back(p);
  162. }
  163. String orig_type = res_orig->get_class();
  164. Object *inst = ClassDB::instance(orig_type);
  165. Ref<Resource> res = Ref<Resource>(Object::cast_to<Resource>(inst));
  166. ERR_FAIL_COND(res.is_null());
  167. for (List<Pair<String, Variant> >::Element *E = propvalues.front(); E; E = E->next()) {
  168. Pair<String, Variant> &p = E->get();
  169. res->set(p.first, p.second);
  170. }
  171. v = res.get_ref_ptr();
  172. emit_signal("variant_changed");
  173. hide();
  174. } break;
  175. case OBJ_MENU_COPY: {
  176. EditorSettings::get_singleton()->set_resource_clipboard(v);
  177. } break;
  178. case OBJ_MENU_PASTE: {
  179. v = EditorSettings::get_singleton()->get_resource_clipboard();
  180. emit_signal("variant_changed");
  181. } break;
  182. case OBJ_MENU_NEW_SCRIPT: {
  183. if (Object::cast_to<Node>(owner))
  184. EditorNode::get_singleton()->get_scene_tree_dock()->open_script_dialog(Object::cast_to<Node>(owner));
  185. } break;
  186. case OBJ_MENU_SHOW_IN_FILE_SYSTEM: {
  187. RES r = v;
  188. FileSystemDock *file_system_dock = EditorNode::get_singleton()->get_filesystem_dock();
  189. file_system_dock->navigate_to_path(r->get_path());
  190. // Ensure that the FileSystem dock is visible.
  191. TabContainer *tab_container = (TabContainer *)file_system_dock->get_parent_control();
  192. tab_container->set_current_tab(file_system_dock->get_position_in_parent());
  193. } break;
  194. default: {
  195. if (p_which >= CONVERT_BASE_ID) {
  196. int to_type = p_which - CONVERT_BASE_ID;
  197. Vector<Ref<EditorResourceConversionPlugin> > conversions = EditorNode::get_singleton()->find_resource_conversion_plugin(RES(v));
  198. ERR_FAIL_INDEX(to_type, conversions.size());
  199. Ref<Resource> new_res = conversions[to_type]->convert(v);
  200. v = new_res;
  201. emit_signal("variant_changed");
  202. break;
  203. }
  204. ERR_FAIL_COND(inheritors_array.empty());
  205. String intype = inheritors_array[p_which - TYPE_BASE_ID];
  206. if (intype == "ViewportTexture") {
  207. scene_tree->set_title(TTR("Pick a Viewport"));
  208. scene_tree->popup_centered_ratio();
  209. picking_viewport = true;
  210. return;
  211. }
  212. Object *obj = ClassDB::instance(intype);
  213. ERR_BREAK(!obj);
  214. Resource *res = Object::cast_to<Resource>(obj);
  215. ERR_BREAK(!res);
  216. if (owner && hint == PROPERTY_HINT_RESOURCE_TYPE && hint_text == "Script") {
  217. //make visual script the right type
  218. res->call("set_instance_base_type", owner->get_class());
  219. }
  220. v = Ref<Resource>(res).get_ref_ptr();
  221. emit_signal("variant_changed");
  222. } break;
  223. }
  224. } break;
  225. default: {}
  226. }
  227. }
  228. void CustomPropertyEditor::hide_menu() {
  229. menu->hide();
  230. }
  231. Variant CustomPropertyEditor::get_variant() const {
  232. return v;
  233. }
  234. String CustomPropertyEditor::get_name() const {
  235. return name;
  236. }
  237. bool CustomPropertyEditor::edit(Object *p_owner, const String &p_name, Variant::Type p_type, const Variant &p_variant, int p_hint, String p_hint_text) {
  238. owner = p_owner;
  239. updating = true;
  240. name = p_name;
  241. v = p_variant;
  242. field_names.clear();
  243. hint = p_hint;
  244. hint_text = p_hint_text;
  245. type_button->hide();
  246. if (color_picker)
  247. color_picker->hide();
  248. texture_preview->hide();
  249. inheritors_array.clear();
  250. text_edit->hide();
  251. easing_draw->hide();
  252. spinbox->hide();
  253. slider->hide();
  254. for (int i = 0; i < MAX_VALUE_EDITORS; i++) {
  255. value_editor[i]->hide();
  256. value_label[i]->hide();
  257. if (i < 4)
  258. scroll[i]->hide();
  259. }
  260. for (int i = 0; i < MAX_ACTION_BUTTONS; i++) {
  261. action_buttons[i]->hide();
  262. }
  263. checks20gc->hide();
  264. for (int i = 0; i < 20; i++)
  265. checks20[i]->hide();
  266. type = (p_variant.get_type() != Variant::NIL && p_variant.get_type() != Variant::_RID && p_type != Variant::OBJECT) ? p_variant.get_type() : p_type;
  267. switch (type) {
  268. case Variant::BOOL: {
  269. checks20gc->show();
  270. CheckBox *c = checks20[0];
  271. c->set_text("True");
  272. checks20gc->set_position(Vector2(4, 4) * EDSCALE);
  273. c->set_pressed(v);
  274. c->show();
  275. checks20gc->set_size(checks20gc->get_minimum_size());
  276. set_size(checks20gc->get_position() + checks20gc->get_size() + c->get_size() + Vector2(4, 4) * EDSCALE);
  277. } break;
  278. case Variant::INT:
  279. case Variant::REAL: {
  280. if (hint == PROPERTY_HINT_RANGE) {
  281. int c = hint_text.get_slice_count(",");
  282. float min = 0, max = 100, step = type == Variant::REAL ? .01 : 1;
  283. if (c >= 1) {
  284. if (!hint_text.get_slice(",", 0).empty())
  285. min = hint_text.get_slice(",", 0).to_double();
  286. }
  287. if (c >= 2) {
  288. if (!hint_text.get_slice(",", 1).empty())
  289. max = hint_text.get_slice(",", 1).to_double();
  290. }
  291. if (c >= 3) {
  292. if (!hint_text.get_slice(",", 2).empty())
  293. step = hint_text.get_slice(",", 2).to_double();
  294. }
  295. if (c >= 4 && hint_text.get_slice(",", 3) == "slider") {
  296. slider->set_min(min);
  297. slider->set_max(max);
  298. slider->set_step(step);
  299. slider->set_value(v);
  300. slider->show();
  301. set_size(Size2(110, 30) * EDSCALE);
  302. } else {
  303. spinbox->set_min(min);
  304. spinbox->set_max(max);
  305. spinbox->set_step(step);
  306. spinbox->set_value(v);
  307. spinbox->show();
  308. set_size(Size2(70, 35) * EDSCALE);
  309. }
  310. } else if (hint == PROPERTY_HINT_ENUM) {
  311. menu->clear();
  312. Vector<String> options = hint_text.split(",");
  313. for (int i = 0; i < options.size(); i++) {
  314. menu->add_item(options[i], i);
  315. }
  316. menu->set_position(get_position());
  317. menu->popup();
  318. hide();
  319. updating = false;
  320. return false;
  321. } else if (hint == PROPERTY_HINT_LAYERS_2D_PHYSICS || hint == PROPERTY_HINT_LAYERS_2D_RENDER || hint == PROPERTY_HINT_LAYERS_3D_PHYSICS || hint == PROPERTY_HINT_LAYERS_3D_RENDER) {
  322. String title;
  323. String basename;
  324. switch (hint) {
  325. case PROPERTY_HINT_LAYERS_2D_RENDER:
  326. basename = "layer_names/2d_render";
  327. title = "2D Render Layers";
  328. break;
  329. case PROPERTY_HINT_LAYERS_2D_PHYSICS:
  330. basename = "layer_names/2d_physics";
  331. title = "2D Physics Layers";
  332. break;
  333. case PROPERTY_HINT_LAYERS_3D_RENDER:
  334. basename = "layer_names/3d_render";
  335. title = "3D Render Layers";
  336. break;
  337. case PROPERTY_HINT_LAYERS_3D_PHYSICS:
  338. basename = "layer_names/3d_physics";
  339. title = "3D Physics Layers";
  340. break;
  341. }
  342. checks20gc->show();
  343. uint32_t flgs = v;
  344. for (int i = 0; i < 2; i++) {
  345. Point2 ofs(4, 4);
  346. ofs.y += 22 * i;
  347. for (int j = 0; j < 10; j++) {
  348. int idx = i * 10 + j;
  349. CheckBox *c = checks20[idx];
  350. c->set_text(ProjectSettings::get_singleton()->get(basename + "/layer_" + itos(idx + 1)));
  351. c->set_pressed(flgs & (1 << (i * 10 + j)));
  352. c->show();
  353. }
  354. }
  355. show();
  356. value_label[0]->set_text(title);
  357. value_label[0]->show();
  358. value_label[0]->set_position(Vector2(4, 4) * EDSCALE);
  359. checks20gc->set_position(Vector2(4, 4) * EDSCALE + Vector2(0, value_label[0]->get_size().height + 4 * EDSCALE));
  360. checks20gc->set_size(checks20gc->get_minimum_size());
  361. set_size(Vector2(4, 4) * EDSCALE + checks20gc->get_position() + checks20gc->get_size());
  362. } else if (hint == PROPERTY_HINT_EXP_EASING) {
  363. easing_draw->set_anchor_and_margin(MARGIN_LEFT, ANCHOR_BEGIN, 5 * EDSCALE);
  364. easing_draw->set_anchor_and_margin(MARGIN_RIGHT, ANCHOR_END, -5 * EDSCALE);
  365. easing_draw->set_anchor_and_margin(MARGIN_TOP, ANCHOR_BEGIN, 5 * EDSCALE);
  366. easing_draw->set_anchor_and_margin(MARGIN_BOTTOM, ANCHOR_END, -30 * EDSCALE);
  367. type_button->set_anchor_and_margin(MARGIN_LEFT, ANCHOR_BEGIN, 3 * EDSCALE);
  368. type_button->set_anchor_and_margin(MARGIN_RIGHT, ANCHOR_END, -3 * EDSCALE);
  369. type_button->set_anchor_and_margin(MARGIN_TOP, ANCHOR_END, -25 * EDSCALE);
  370. type_button->set_anchor_and_margin(MARGIN_BOTTOM, ANCHOR_END, -7 * EDSCALE);
  371. type_button->set_text(TTR("Preset.."));
  372. type_button->get_popup()->clear();
  373. type_button->get_popup()->add_item(TTR("Linear"), EASING_LINEAR);
  374. type_button->get_popup()->add_item(TTR("Ease In"), EASING_EASE_IN);
  375. type_button->get_popup()->add_item(TTR("Ease Out"), EASING_EASE_OUT);
  376. if (hint_text != "attenuation") {
  377. type_button->get_popup()->add_item(TTR("Zero"), EASING_ZERO);
  378. type_button->get_popup()->add_item(TTR("Easing In-Out"), EASING_IN_OUT);
  379. type_button->get_popup()->add_item(TTR("Easing Out-In"), EASING_OUT_IN);
  380. }
  381. type_button->show();
  382. easing_draw->show();
  383. set_size(Size2(200, 150) * EDSCALE);
  384. } else if (hint == PROPERTY_HINT_FLAGS) {
  385. menu->clear();
  386. Vector<String> flags = hint_text.split(",");
  387. for (int i = 0; i < flags.size(); i++) {
  388. String flag = flags[i];
  389. if (flag == "")
  390. continue;
  391. menu->add_check_item(flag, i);
  392. int f = v;
  393. if (f & (1 << i))
  394. menu->set_item_checked(menu->get_item_index(i), true);
  395. }
  396. menu->set_position(get_position());
  397. menu->popup();
  398. hide();
  399. updating = false;
  400. return false;
  401. } else {
  402. List<String> names;
  403. names.push_back("value:");
  404. config_value_editors(1, 1, 50, names);
  405. value_editor[0]->set_text(String::num(v));
  406. }
  407. } break;
  408. case Variant::STRING: {
  409. if (hint == PROPERTY_HINT_FILE || hint == PROPERTY_HINT_GLOBAL_FILE) {
  410. List<String> names;
  411. names.push_back(TTR("File.."));
  412. names.push_back(TTR("Clear"));
  413. config_action_buttons(names);
  414. } else if (hint == PROPERTY_HINT_DIR || hint == PROPERTY_HINT_GLOBAL_DIR) {
  415. List<String> names;
  416. names.push_back(TTR("Dir.."));
  417. names.push_back(TTR("Clear"));
  418. config_action_buttons(names);
  419. } else if (hint == PROPERTY_HINT_ENUM) {
  420. menu->clear();
  421. Vector<String> options = hint_text.split(",");
  422. for (int i = 0; i < options.size(); i++) {
  423. menu->add_item(options[i], i);
  424. }
  425. menu->set_position(get_position());
  426. menu->popup();
  427. hide();
  428. updating = false;
  429. return false;
  430. } else if (hint == PROPERTY_HINT_MULTILINE_TEXT) {
  431. text_edit->show();
  432. text_edit->set_text(v);
  433. int button_margin = get_constant("button_margin", "Dialogs");
  434. int margin = get_constant("margin", "Dialogs");
  435. action_buttons[0]->set_anchor(MARGIN_LEFT, ANCHOR_END);
  436. action_buttons[0]->set_anchor(MARGIN_TOP, ANCHOR_END);
  437. action_buttons[0]->set_anchor(MARGIN_RIGHT, ANCHOR_END);
  438. action_buttons[0]->set_anchor(MARGIN_BOTTOM, ANCHOR_END);
  439. action_buttons[0]->set_begin(Point2(-70 * EDSCALE, -button_margin + 5 * EDSCALE));
  440. action_buttons[0]->set_end(Point2(-margin, -margin));
  441. action_buttons[0]->set_text(TTR("Close"));
  442. action_buttons[0]->show();
  443. } else if (hint == PROPERTY_HINT_TYPE_STRING) {
  444. if (!create_dialog) {
  445. create_dialog = memnew(CreateDialog);
  446. create_dialog->connect("create", this, "_create_dialog_callback");
  447. add_child(create_dialog);
  448. }
  449. if (hint_text != String()) {
  450. create_dialog->set_base_type(hint_text);
  451. } else {
  452. create_dialog->set_base_type("Object");
  453. }
  454. create_dialog->popup_create(false);
  455. hide();
  456. updating = false;
  457. return false;
  458. } else if (hint == PROPERTY_HINT_METHOD_OF_VARIANT_TYPE) {
  459. #define MAKE_PROPSELECT \
  460. if (!property_select) { \
  461. property_select = memnew(PropertySelector); \
  462. property_select->connect("selected", this, "_create_selected_property"); \
  463. add_child(property_select); \
  464. } \
  465. hide();
  466. MAKE_PROPSELECT;
  467. Variant::Type type = Variant::NIL;
  468. for (int i = 0; i < Variant::VARIANT_MAX; i++) {
  469. if (hint_text == Variant::get_type_name(Variant::Type(i))) {
  470. type = Variant::Type(i);
  471. }
  472. }
  473. if (type)
  474. property_select->select_method_from_basic_type(type, v);
  475. updating = false;
  476. return false;
  477. } else if (hint == PROPERTY_HINT_METHOD_OF_BASE_TYPE) {
  478. MAKE_PROPSELECT
  479. property_select->select_method_from_base_type(hint_text, v);
  480. updating = false;
  481. return false;
  482. } else if (hint == PROPERTY_HINT_METHOD_OF_INSTANCE) {
  483. MAKE_PROPSELECT
  484. Object *instance = ObjectDB::get_instance(hint_text.to_int64());
  485. if (instance)
  486. property_select->select_method_from_instance(instance, v);
  487. updating = false;
  488. return false;
  489. } else if (hint == PROPERTY_HINT_METHOD_OF_SCRIPT) {
  490. MAKE_PROPSELECT
  491. Object *obj = ObjectDB::get_instance(hint_text.to_int64());
  492. if (Object::cast_to<Script>(obj)) {
  493. property_select->select_method_from_script(Object::cast_to<Script>(obj), v);
  494. }
  495. updating = false;
  496. return false;
  497. } else if (hint == PROPERTY_HINT_PROPERTY_OF_VARIANT_TYPE) {
  498. MAKE_PROPSELECT
  499. Variant::Type type = Variant::NIL;
  500. String tname = hint_text;
  501. if (tname.find(".") != -1)
  502. tname = tname.get_slice(".", 0);
  503. for (int i = 0; i < Variant::VARIANT_MAX; i++) {
  504. if (tname == Variant::get_type_name(Variant::Type(i))) {
  505. type = Variant::Type(Variant::Type(i));
  506. }
  507. }
  508. if (type)
  509. property_select->select_property_from_basic_type(type, v);
  510. updating = false;
  511. return false;
  512. } else if (hint == PROPERTY_HINT_PROPERTY_OF_BASE_TYPE) {
  513. MAKE_PROPSELECT
  514. property_select->select_property_from_base_type(hint_text, v);
  515. updating = false;
  516. return false;
  517. } else if (hint == PROPERTY_HINT_PROPERTY_OF_INSTANCE) {
  518. Object *instance = ObjectDB::get_instance(hint_text.to_int64());
  519. if (instance)
  520. property_select->select_property_from_instance(instance, v);
  521. updating = false;
  522. return false;
  523. } else if (hint == PROPERTY_HINT_PROPERTY_OF_SCRIPT) {
  524. MAKE_PROPSELECT
  525. Object *obj = ObjectDB::get_instance(hint_text.to_int64());
  526. if (Object::cast_to<Script>(obj)) {
  527. property_select->select_property_from_script(Object::cast_to<Script>(obj), v);
  528. }
  529. updating = false;
  530. return false;
  531. } else {
  532. List<String> names;
  533. names.push_back("string:");
  534. config_value_editors(1, 1, 50, names);
  535. value_editor[0]->set_text(v);
  536. }
  537. } break;
  538. case Variant::VECTOR2: {
  539. field_names.push_back("x");
  540. field_names.push_back("y");
  541. config_value_editors(2, 2, 10, field_names);
  542. Vector2 vec = v;
  543. value_editor[0]->set_text(String::num(vec.x));
  544. value_editor[1]->set_text(String::num(vec.y));
  545. } break;
  546. case Variant::RECT2: {
  547. field_names.push_back("x");
  548. field_names.push_back("y");
  549. field_names.push_back("w");
  550. field_names.push_back("h");
  551. config_value_editors(4, 4, 10, field_names);
  552. Rect2 r = v;
  553. value_editor[0]->set_text(String::num(r.position.x));
  554. value_editor[1]->set_text(String::num(r.position.y));
  555. value_editor[2]->set_text(String::num(r.size.x));
  556. value_editor[3]->set_text(String::num(r.size.y));
  557. } break;
  558. case Variant::VECTOR3: {
  559. field_names.push_back("x");
  560. field_names.push_back("y");
  561. field_names.push_back("z");
  562. config_value_editors(3, 3, 10, field_names);
  563. Vector3 vec = v;
  564. value_editor[0]->set_text(String::num(vec.x));
  565. value_editor[1]->set_text(String::num(vec.y));
  566. value_editor[2]->set_text(String::num(vec.z));
  567. } break;
  568. case Variant::PLANE: {
  569. field_names.push_back("x");
  570. field_names.push_back("y");
  571. field_names.push_back("z");
  572. field_names.push_back("d");
  573. config_value_editors(4, 4, 10, field_names);
  574. Plane plane = v;
  575. value_editor[0]->set_text(String::num(plane.normal.x));
  576. value_editor[1]->set_text(String::num(plane.normal.y));
  577. value_editor[2]->set_text(String::num(plane.normal.z));
  578. value_editor[3]->set_text(String::num(plane.d));
  579. } break;
  580. case Variant::QUAT: {
  581. field_names.push_back("x");
  582. field_names.push_back("y");
  583. field_names.push_back("z");
  584. field_names.push_back("w");
  585. config_value_editors(4, 4, 10, field_names);
  586. Quat q = v;
  587. value_editor[0]->set_text(String::num(q.x));
  588. value_editor[1]->set_text(String::num(q.y));
  589. value_editor[2]->set_text(String::num(q.z));
  590. value_editor[3]->set_text(String::num(q.w));
  591. } break;
  592. case Variant::RECT3: {
  593. field_names.push_back("px");
  594. field_names.push_back("py");
  595. field_names.push_back("pz");
  596. field_names.push_back("sx");
  597. field_names.push_back("sy");
  598. field_names.push_back("sz");
  599. config_value_editors(6, 3, 16, field_names);
  600. Rect3 aabb = v;
  601. value_editor[0]->set_text(String::num(aabb.position.x));
  602. value_editor[1]->set_text(String::num(aabb.position.y));
  603. value_editor[2]->set_text(String::num(aabb.position.z));
  604. value_editor[3]->set_text(String::num(aabb.size.x));
  605. value_editor[4]->set_text(String::num(aabb.size.y));
  606. value_editor[5]->set_text(String::num(aabb.size.z));
  607. } break;
  608. case Variant::TRANSFORM2D: {
  609. field_names.push_back("xx");
  610. field_names.push_back("xy");
  611. field_names.push_back("yx");
  612. field_names.push_back("yy");
  613. field_names.push_back("ox");
  614. field_names.push_back("oy");
  615. config_value_editors(6, 2, 16, field_names);
  616. Transform2D basis = v;
  617. for (int i = 0; i < 6; i++) {
  618. value_editor[i]->set_text(String::num(basis.elements[i / 2][i % 2]));
  619. }
  620. } break;
  621. case Variant::BASIS: {
  622. field_names.push_back("xx");
  623. field_names.push_back("xy");
  624. field_names.push_back("xz");
  625. field_names.push_back("yx");
  626. field_names.push_back("yy");
  627. field_names.push_back("yz");
  628. field_names.push_back("zx");
  629. field_names.push_back("zy");
  630. field_names.push_back("zz");
  631. config_value_editors(9, 3, 16, field_names);
  632. Basis basis = v;
  633. for (int i = 0; i < 9; i++) {
  634. value_editor[i]->set_text(String::num(basis.elements[i / 3][i % 3]));
  635. }
  636. } break;
  637. case Variant::TRANSFORM: {
  638. field_names.push_back("xx");
  639. field_names.push_back("xy");
  640. field_names.push_back("xz");
  641. field_names.push_back("xo");
  642. field_names.push_back("yx");
  643. field_names.push_back("yy");
  644. field_names.push_back("yz");
  645. field_names.push_back("yo");
  646. field_names.push_back("zx");
  647. field_names.push_back("zy");
  648. field_names.push_back("zz");
  649. field_names.push_back("zo");
  650. config_value_editors(12, 4, 16, field_names);
  651. Transform tr = v;
  652. for (int i = 0; i < 9; i++) {
  653. value_editor[(i / 3) * 4 + i % 3]->set_text(String::num(tr.basis.elements[i / 3][i % 3]));
  654. }
  655. value_editor[3]->set_text(String::num(tr.origin.x));
  656. value_editor[7]->set_text(String::num(tr.origin.y));
  657. value_editor[11]->set_text(String::num(tr.origin.z));
  658. } break;
  659. case Variant::COLOR: {
  660. if (!color_picker) {
  661. //late init for performance
  662. color_picker = memnew(ColorPicker);
  663. add_child(color_picker);
  664. color_picker->hide();
  665. color_picker->connect("color_changed", this, "_color_changed");
  666. }
  667. color_picker->show();
  668. color_picker->set_edit_alpha(hint != PROPERTY_HINT_COLOR_NO_ALPHA);
  669. color_picker->set_pick_color(v);
  670. color_picker->set_focus_on_line_edit();
  671. } break;
  672. case Variant::NODE_PATH: {
  673. List<String> names;
  674. names.push_back(TTR("Assign"));
  675. names.push_back(TTR("Clear"));
  676. if (owner && owner->is_class("Node") && (v.get_type() == Variant::NODE_PATH) && Object::cast_to<Node>(owner)->has_node(v))
  677. names.push_back(TTR("Select Node"));
  678. config_action_buttons(names);
  679. } break;
  680. case Variant::OBJECT: {
  681. if (hint != PROPERTY_HINT_RESOURCE_TYPE)
  682. break;
  683. menu->clear();
  684. menu->set_size(Size2(1, 1) * EDSCALE);
  685. if (p_name == "script" && hint_text == "Script" && Object::cast_to<Node>(owner)) {
  686. menu->add_icon_item(get_icon("Script", "EditorIcons"), TTR("New Script"), OBJ_MENU_NEW_SCRIPT);
  687. menu->add_separator();
  688. } else if (hint_text != "") {
  689. int idx = 0;
  690. for (int i = 0; i < hint_text.get_slice_count(","); i++) {
  691. String base = hint_text.get_slice(",", i);
  692. Set<String> valid_inheritors;
  693. valid_inheritors.insert(base);
  694. List<StringName> inheritors;
  695. ClassDB::get_inheriters_from_class(base.strip_edges(), &inheritors);
  696. List<StringName>::Element *E = inheritors.front();
  697. while (E) {
  698. valid_inheritors.insert(E->get());
  699. E = E->next();
  700. }
  701. for (Set<String>::Element *E = valid_inheritors.front(); E; E = E->next()) {
  702. String t = E->get();
  703. if (!ClassDB::can_instance(t))
  704. continue;
  705. inheritors_array.push_back(t);
  706. int id = TYPE_BASE_ID + idx;
  707. if (has_icon(t, "EditorIcons")) {
  708. menu->add_icon_item(get_icon(t, "EditorIcons"), TTR("New") + " " + t, id);
  709. } else {
  710. menu->add_item(TTR("New") + " " + t, id);
  711. }
  712. idx++;
  713. }
  714. }
  715. if (menu->get_item_count())
  716. menu->add_separator();
  717. }
  718. menu->add_icon_item(get_icon("Load", "EditorIcons"), TTR("Load"), OBJ_MENU_LOAD);
  719. if (!RES(v).is_null()) {
  720. menu->add_icon_item(get_icon("Edit", "EditorIcons"), TTR("Edit"), OBJ_MENU_EDIT);
  721. menu->add_icon_item(get_icon("Clear", "EditorIcons"), TTR("Clear"), OBJ_MENU_CLEAR);
  722. menu->add_icon_item(get_icon("Duplicate", "EditorIcons"), TTR("Make Unique"), OBJ_MENU_MAKE_UNIQUE);
  723. RES r = v;
  724. if (r.is_valid() && r->get_path().is_resource_file()) {
  725. menu->add_separator();
  726. menu->add_item(TTR("Show in File System"), OBJ_MENU_SHOW_IN_FILE_SYSTEM);
  727. }
  728. } else {
  729. }
  730. RES cb = EditorSettings::get_singleton()->get_resource_clipboard();
  731. bool paste_valid = false;
  732. if (cb.is_valid()) {
  733. if (hint_text == "")
  734. paste_valid = true;
  735. else
  736. for (int i = 0; i < hint_text.get_slice_count(","); i++)
  737. if (ClassDB::is_parent_class(cb->get_class(), hint_text.get_slice(",", i))) {
  738. paste_valid = true;
  739. break;
  740. }
  741. }
  742. if (!RES(v).is_null() || paste_valid) {
  743. menu->add_separator();
  744. if (!RES(v).is_null()) {
  745. menu->add_item(TTR("Copy"), OBJ_MENU_COPY);
  746. }
  747. if (paste_valid) {
  748. menu->add_item(TTR("Paste"), OBJ_MENU_PASTE);
  749. }
  750. }
  751. if (!RES(v).is_null()) {
  752. Vector<Ref<EditorResourceConversionPlugin> > conversions = EditorNode::get_singleton()->find_resource_conversion_plugin(RES(v));
  753. if (conversions.size()) {
  754. menu->add_separator();
  755. }
  756. for (int i = 0; i < conversions.size(); i++) {
  757. String what = conversions[i]->converts_to();
  758. Ref<Texture> icon;
  759. if (has_icon(what, "EditorIcons")) {
  760. icon = get_icon(what, "EditorIcons");
  761. } else {
  762. icon = get_icon(what, "Resource");
  763. }
  764. menu->add_icon_item(icon, vformat(TTR("Convert To %s"), what), CONVERT_BASE_ID + i);
  765. }
  766. }
  767. menu->set_position(get_position());
  768. menu->popup();
  769. hide();
  770. updating = false;
  771. return false;
  772. } break;
  773. case Variant::DICTIONARY: {
  774. } break;
  775. case Variant::POOL_BYTE_ARRAY: {
  776. } break;
  777. case Variant::POOL_INT_ARRAY: {
  778. } break;
  779. case Variant::POOL_REAL_ARRAY: {
  780. } break;
  781. case Variant::POOL_STRING_ARRAY: {
  782. } break;
  783. case Variant::POOL_VECTOR3_ARRAY: {
  784. } break;
  785. case Variant::POOL_COLOR_ARRAY: {
  786. } break;
  787. default: {}
  788. }
  789. updating = false;
  790. return true;
  791. }
  792. void CustomPropertyEditor::_file_selected(String p_file) {
  793. switch (type) {
  794. case Variant::STRING: {
  795. if (hint == PROPERTY_HINT_FILE || hint == PROPERTY_HINT_DIR) {
  796. v = ProjectSettings::get_singleton()->localize_path(p_file);
  797. emit_signal("variant_changed");
  798. hide();
  799. }
  800. if (hint == PROPERTY_HINT_GLOBAL_FILE || hint == PROPERTY_HINT_GLOBAL_DIR) {
  801. v = p_file;
  802. emit_signal("variant_changed");
  803. hide();
  804. }
  805. } break;
  806. case Variant::OBJECT: {
  807. String type = (hint == PROPERTY_HINT_RESOURCE_TYPE) ? hint_text : String();
  808. RES res = ResourceLoader::load(p_file, type);
  809. if (res.is_null()) {
  810. error->set_text(TTR("Error loading file: Not a resource!"));
  811. error->popup_centered_minsize();
  812. break;
  813. }
  814. v = res.get_ref_ptr();
  815. emit_signal("variant_changed");
  816. hide();
  817. } break;
  818. default: {}
  819. }
  820. }
  821. void CustomPropertyEditor::_type_create_selected(int p_idx) {
  822. if (type == Variant::INT || type == Variant::REAL) {
  823. float newval = 0;
  824. switch (p_idx) {
  825. case EASING_LINEAR: {
  826. newval = 1;
  827. } break;
  828. case EASING_EASE_IN: {
  829. newval = 2.0;
  830. } break;
  831. case EASING_EASE_OUT: {
  832. newval = 0.5;
  833. } break;
  834. case EASING_ZERO: {
  835. newval = 0;
  836. } break;
  837. case EASING_IN_OUT: {
  838. newval = -0.5;
  839. } break;
  840. case EASING_OUT_IN: {
  841. newval = -2.0;
  842. } break;
  843. }
  844. v = newval;
  845. emit_signal("variant_changed");
  846. easing_draw->update();
  847. } else if (type == Variant::OBJECT) {
  848. ERR_FAIL_INDEX(p_idx, inheritors_array.size());
  849. String intype = inheritors_array[p_idx];
  850. Object *obj = ClassDB::instance(intype);
  851. ERR_FAIL_COND(!obj);
  852. Resource *res = Object::cast_to<Resource>(obj);
  853. ERR_FAIL_COND(!res);
  854. v = Ref<Resource>(res).get_ref_ptr();
  855. emit_signal("variant_changed");
  856. hide();
  857. }
  858. }
  859. void CustomPropertyEditor::_color_changed(const Color &p_color) {
  860. v = p_color;
  861. emit_signal("variant_changed");
  862. }
  863. void CustomPropertyEditor::_node_path_selected(NodePath p_path) {
  864. if (picking_viewport) {
  865. Node *to_node = get_node(p_path);
  866. if (!Object::cast_to<Viewport>(to_node)) {
  867. EditorNode::get_singleton()->show_warning(TTR("Selected node is not a Viewport!"));
  868. return;
  869. }
  870. Ref<ViewportTexture> vt;
  871. vt.instance();
  872. vt->set_viewport_path_in_scene(get_tree()->get_edited_scene_root()->get_path_to(to_node));
  873. vt->setup_local_to_scene();
  874. v = vt;
  875. emit_signal("variant_changed");
  876. return;
  877. }
  878. if (hint == PROPERTY_HINT_NODE_PATH_TO_EDITED_NODE && hint_text != String()) {
  879. Node *node = get_node(hint_text);
  880. if (node) {
  881. Node *tonode = node->get_node(p_path);
  882. if (tonode) {
  883. p_path = node->get_path_to(tonode);
  884. }
  885. }
  886. } else if (owner) {
  887. Node *node = NULL;
  888. if (owner->is_class("Node"))
  889. node = Object::cast_to<Node>(owner);
  890. else if (owner->is_class("ArrayPropertyEdit"))
  891. node = Object::cast_to<ArrayPropertyEdit>(owner)->get_node();
  892. if (!node) {
  893. v = p_path;
  894. emit_signal("variant_changed");
  895. call_deferred("hide"); //to not mess with dialogs
  896. return;
  897. }
  898. Node *tonode = node->get_node(p_path);
  899. if (tonode) {
  900. p_path = node->get_path_to(tonode);
  901. }
  902. }
  903. v = p_path;
  904. emit_signal("variant_changed");
  905. call_deferred("hide"); //to not mess with dialogs
  906. }
  907. void CustomPropertyEditor::_action_pressed(int p_which) {
  908. if (updating)
  909. return;
  910. switch (type) {
  911. case Variant::BOOL: {
  912. v = checks20[0]->is_pressed();
  913. emit_signal("variant_changed");
  914. } break;
  915. case Variant::INT: {
  916. if (hint == PROPERTY_HINT_LAYERS_2D_PHYSICS || hint == PROPERTY_HINT_LAYERS_2D_RENDER || hint == PROPERTY_HINT_LAYERS_3D_PHYSICS || hint == PROPERTY_HINT_LAYERS_3D_RENDER) {
  917. uint32_t f = v;
  918. if (checks20[p_which]->is_pressed())
  919. f |= (1 << p_which);
  920. else
  921. f &= ~(1 << p_which);
  922. v = f;
  923. emit_signal("variant_changed");
  924. }
  925. } break;
  926. case Variant::STRING: {
  927. if (hint == PROPERTY_HINT_MULTILINE_TEXT) {
  928. hide();
  929. } else if (hint == PROPERTY_HINT_FILE || hint == PROPERTY_HINT_GLOBAL_FILE) {
  930. if (p_which == 0) {
  931. if (hint == PROPERTY_HINT_FILE)
  932. file->set_access(EditorFileDialog::ACCESS_RESOURCES);
  933. else
  934. file->set_access(EditorFileDialog::ACCESS_FILESYSTEM);
  935. file->set_mode(EditorFileDialog::MODE_OPEN_FILE);
  936. file->clear_filters();
  937. file->clear_filters();
  938. if (hint_text != "") {
  939. Vector<String> extensions = hint_text.split(",");
  940. for (int i = 0; i < extensions.size(); i++) {
  941. String filter = extensions[i];
  942. if (filter.begins_with("."))
  943. filter = "*" + extensions[i];
  944. else if (!filter.begins_with("*"))
  945. filter = "*." + extensions[i];
  946. file->add_filter(filter + " ; " + extensions[i].to_upper());
  947. }
  948. }
  949. file->popup_centered_ratio();
  950. } else {
  951. v = "";
  952. emit_signal("variant_changed");
  953. hide();
  954. }
  955. } else if (hint == PROPERTY_HINT_DIR || hint == PROPERTY_HINT_GLOBAL_DIR) {
  956. if (p_which == 0) {
  957. if (hint == PROPERTY_HINT_DIR)
  958. file->set_access(EditorFileDialog::ACCESS_RESOURCES);
  959. else
  960. file->set_access(EditorFileDialog::ACCESS_FILESYSTEM);
  961. file->set_mode(EditorFileDialog::MODE_OPEN_DIR);
  962. file->clear_filters();
  963. file->popup_centered_ratio();
  964. } else {
  965. v = "";
  966. emit_signal("variant_changed");
  967. hide();
  968. }
  969. }
  970. } break;
  971. case Variant::NODE_PATH: {
  972. if (p_which == 0) {
  973. picking_viewport = false;
  974. scene_tree->set_title(TTR("Pick a Node"));
  975. scene_tree->popup_centered_ratio();
  976. } else if (p_which == 1) {
  977. v = NodePath();
  978. emit_signal("variant_changed");
  979. hide();
  980. } else if (p_which == 2) {
  981. if (owner->is_class("Node") && (v.get_type() == Variant::NODE_PATH) && Object::cast_to<Node>(owner)->has_node(v)) {
  982. Node *target_node = Object::cast_to<Node>(owner)->get_node(v);
  983. EditorNode::get_singleton()->get_editor_selection()->clear();
  984. EditorNode::get_singleton()->get_scene_tree_dock()->set_selected(target_node);
  985. }
  986. hide();
  987. }
  988. } break;
  989. case Variant::OBJECT: {
  990. if (p_which == 0) {
  991. ERR_FAIL_COND(inheritors_array.empty());
  992. String intype = inheritors_array[0];
  993. if (hint == PROPERTY_HINT_RESOURCE_TYPE) {
  994. Object *obj = ClassDB::instance(intype);
  995. ERR_BREAK(!obj);
  996. Resource *res = Object::cast_to<Resource>(obj);
  997. ERR_BREAK(!res);
  998. v = Ref<Resource>(res).get_ref_ptr();
  999. emit_signal("variant_changed");
  1000. hide();
  1001. }
  1002. } else if (p_which == 1) {
  1003. file->set_access(EditorFileDialog::ACCESS_RESOURCES);
  1004. file->set_mode(EditorFileDialog::MODE_OPEN_FILE);
  1005. List<String> extensions;
  1006. String type = (hint == PROPERTY_HINT_RESOURCE_TYPE) ? hint_text : String();
  1007. ResourceLoader::get_recognized_extensions_for_type(type, &extensions);
  1008. file->clear_filters();
  1009. for (List<String>::Element *E = extensions.front(); E; E = E->next()) {
  1010. file->add_filter("*." + E->get() + " ; " + E->get().to_upper());
  1011. }
  1012. file->popup_centered_ratio();
  1013. } else if (p_which == 2) {
  1014. RefPtr RefPtr = v;
  1015. if (!RefPtr.is_null()) {
  1016. emit_signal("resource_edit_request");
  1017. hide();
  1018. }
  1019. } else if (p_which == 3) {
  1020. v = Variant();
  1021. emit_signal("variant_changed");
  1022. hide();
  1023. } else if (p_which == 4) {
  1024. RefPtr RefPtr = v;
  1025. Ref<Resource> res_orig = RefPtr;
  1026. if (res_orig.is_null())
  1027. return;
  1028. List<PropertyInfo> property_list;
  1029. res_orig->get_property_list(&property_list);
  1030. List<Pair<String, Variant> > propvalues;
  1031. for (List<PropertyInfo>::Element *E = property_list.front(); E; E = E->next()) {
  1032. Pair<String, Variant> p;
  1033. PropertyInfo &pi = E->get();
  1034. if (pi.usage & PROPERTY_USAGE_STORAGE) {
  1035. p.first = pi.name;
  1036. p.second = res_orig->get(pi.name);
  1037. }
  1038. propvalues.push_back(p);
  1039. }
  1040. Ref<Resource> res = Ref<Resource>(ClassDB::instance(res_orig->get_class()));
  1041. ERR_FAIL_COND(res.is_null());
  1042. for (List<Pair<String, Variant> >::Element *E = propvalues.front(); E; E = E->next()) {
  1043. Pair<String, Variant> &p = E->get();
  1044. res->set(p.first, p.second);
  1045. }
  1046. v = res.get_ref_ptr();
  1047. emit_signal("variant_changed");
  1048. hide();
  1049. }
  1050. } break;
  1051. default: {};
  1052. }
  1053. }
  1054. void CustomPropertyEditor::_drag_easing(const Ref<InputEvent> &p_ev) {
  1055. Ref<InputEventMouseMotion> mm = p_ev;
  1056. if (mm.is_valid() && mm->get_button_mask() & BUTTON_MASK_LEFT) {
  1057. float rel = mm->get_relative().x;
  1058. if (rel == 0)
  1059. return;
  1060. bool flip = hint_text == "attenuation";
  1061. if (flip)
  1062. rel = -rel;
  1063. float val = v;
  1064. if (val == 0)
  1065. return;
  1066. bool sg = val < 0;
  1067. val = Math::absf(val);
  1068. val = Math::log(val) / Math::log((float)2.0);
  1069. //logspace
  1070. val += rel * 0.05;
  1071. val = Math::pow(2.0f, val);
  1072. if (sg)
  1073. val = -val;
  1074. v = val;
  1075. easing_draw->update();
  1076. emit_signal("variant_changed");
  1077. }
  1078. }
  1079. void CustomPropertyEditor::_draw_easing() {
  1080. RID ci = easing_draw->get_canvas_item();
  1081. Size2 s = easing_draw->get_size();
  1082. Rect2 r(Point2(), s);
  1083. r = r.grow(3);
  1084. get_stylebox("normal", "LineEdit")->draw(ci, r);
  1085. int points = 48;
  1086. float prev = 1.0;
  1087. float exp = v;
  1088. bool flip = hint_text == "attenuation";
  1089. Ref<Font> f = get_font("font", "Label");
  1090. Color color = get_color("font_color", "Label");
  1091. for (int i = 1; i <= points; i++) {
  1092. float ifl = i / float(points);
  1093. float iflp = (i - 1) / float(points);
  1094. float h = 1.0 - Math::ease(ifl, exp);
  1095. if (flip) {
  1096. ifl = 1.0 - ifl;
  1097. iflp = 1.0 - iflp;
  1098. }
  1099. VisualServer::get_singleton()->canvas_item_add_line(ci, Point2(iflp * s.width, prev * s.height), Point2(ifl * s.width, h * s.height), color);
  1100. prev = h;
  1101. }
  1102. f->draw(ci, Point2(10, 10 + f->get_ascent()), String::num(exp, 2), color);
  1103. }
  1104. void CustomPropertyEditor::_text_edit_changed() {
  1105. v = text_edit->get_text();
  1106. emit_signal("variant_changed");
  1107. }
  1108. void CustomPropertyEditor::_create_dialog_callback() {
  1109. v = create_dialog->get_selected_type();
  1110. emit_signal("variant_changed");
  1111. }
  1112. void CustomPropertyEditor::_create_selected_property(const String &p_prop) {
  1113. v = p_prop;
  1114. emit_signal("variant_changed");
  1115. }
  1116. void CustomPropertyEditor::_modified(String p_string) {
  1117. if (updating)
  1118. return;
  1119. updating = true;
  1120. switch (type) {
  1121. case Variant::INT: {
  1122. if (evaluator)
  1123. v = evaluator->eval(value_editor[0]->get_text());
  1124. else
  1125. v = value_editor[0]->get_text().to_int();
  1126. emit_signal("variant_changed");
  1127. } break;
  1128. case Variant::REAL: {
  1129. if (hint != PROPERTY_HINT_EXP_EASING) {
  1130. if (evaluator)
  1131. v = evaluator->eval(value_editor[0]->get_text());
  1132. else
  1133. v = value_editor[0]->get_text().to_double();
  1134. emit_signal("variant_changed");
  1135. }
  1136. } break;
  1137. case Variant::STRING: {
  1138. v = value_editor[0]->get_text();
  1139. emit_signal("variant_changed");
  1140. } break;
  1141. case Variant::VECTOR2: {
  1142. Vector2 vec;
  1143. if (evaluator) {
  1144. vec.x = evaluator->eval(value_editor[0]->get_text());
  1145. vec.y = evaluator->eval(value_editor[1]->get_text());
  1146. } else {
  1147. vec.x = value_editor[0]->get_text().to_double();
  1148. vec.y = value_editor[1]->get_text().to_double();
  1149. }
  1150. v = vec;
  1151. _emit_changed_whole_or_field();
  1152. } break;
  1153. case Variant::RECT2: {
  1154. Rect2 r2;
  1155. if (evaluator) {
  1156. r2.position.x = evaluator->eval(value_editor[0]->get_text());
  1157. r2.position.y = evaluator->eval(value_editor[1]->get_text());
  1158. r2.size.x = evaluator->eval(value_editor[2]->get_text());
  1159. r2.size.y = evaluator->eval(value_editor[3]->get_text());
  1160. } else {
  1161. r2.position.x = value_editor[0]->get_text().to_double();
  1162. r2.position.y = value_editor[1]->get_text().to_double();
  1163. r2.size.x = value_editor[2]->get_text().to_double();
  1164. r2.size.y = value_editor[3]->get_text().to_double();
  1165. }
  1166. v = r2;
  1167. _emit_changed_whole_or_field();
  1168. } break;
  1169. case Variant::VECTOR3: {
  1170. Vector3 vec;
  1171. if (evaluator) {
  1172. vec.x = evaluator->eval(value_editor[0]->get_text());
  1173. vec.y = evaluator->eval(value_editor[1]->get_text());
  1174. vec.z = evaluator->eval(value_editor[2]->get_text());
  1175. } else {
  1176. vec.x = value_editor[0]->get_text().to_double();
  1177. vec.y = value_editor[1]->get_text().to_double();
  1178. vec.z = value_editor[2]->get_text().to_double();
  1179. }
  1180. v = vec;
  1181. _emit_changed_whole_or_field();
  1182. } break;
  1183. case Variant::PLANE: {
  1184. Plane pl;
  1185. if (evaluator) {
  1186. pl.normal.x = evaluator->eval(value_editor[0]->get_text());
  1187. pl.normal.y = evaluator->eval(value_editor[1]->get_text());
  1188. pl.normal.z = evaluator->eval(value_editor[2]->get_text());
  1189. pl.d = evaluator->eval(value_editor[3]->get_text());
  1190. } else {
  1191. pl.normal.x = value_editor[0]->get_text().to_double();
  1192. pl.normal.y = value_editor[1]->get_text().to_double();
  1193. pl.normal.z = value_editor[2]->get_text().to_double();
  1194. pl.d = value_editor[3]->get_text().to_double();
  1195. }
  1196. v = pl;
  1197. _emit_changed_whole_or_field();
  1198. } break;
  1199. case Variant::QUAT: {
  1200. Quat q;
  1201. if (evaluator) {
  1202. q.x = evaluator->eval(value_editor[0]->get_text());
  1203. q.y = evaluator->eval(value_editor[1]->get_text());
  1204. q.z = evaluator->eval(value_editor[2]->get_text());
  1205. q.w = evaluator->eval(value_editor[3]->get_text());
  1206. } else {
  1207. q.x = value_editor[0]->get_text().to_double();
  1208. q.y = value_editor[1]->get_text().to_double();
  1209. q.z = value_editor[2]->get_text().to_double();
  1210. q.w = value_editor[3]->get_text().to_double();
  1211. }
  1212. v = q;
  1213. _emit_changed_whole_or_field();
  1214. } break;
  1215. case Variant::RECT3: {
  1216. Vector3 pos;
  1217. Vector3 size;
  1218. if (evaluator) {
  1219. pos.x = evaluator->eval(value_editor[0]->get_text());
  1220. pos.y = evaluator->eval(value_editor[1]->get_text());
  1221. pos.z = evaluator->eval(value_editor[2]->get_text());
  1222. size.x = evaluator->eval(value_editor[3]->get_text());
  1223. size.y = evaluator->eval(value_editor[4]->get_text());
  1224. size.z = evaluator->eval(value_editor[5]->get_text());
  1225. } else {
  1226. pos.x = value_editor[0]->get_text().to_double();
  1227. pos.y = value_editor[1]->get_text().to_double();
  1228. pos.z = value_editor[2]->get_text().to_double();
  1229. size.x = value_editor[3]->get_text().to_double();
  1230. size.y = value_editor[4]->get_text().to_double();
  1231. size.z = value_editor[5]->get_text().to_double();
  1232. }
  1233. v = Rect3(pos, size);
  1234. _emit_changed_whole_or_field();
  1235. } break;
  1236. case Variant::TRANSFORM2D: {
  1237. Transform2D m;
  1238. for (int i = 0; i < 6; i++) {
  1239. if (evaluator) {
  1240. m.elements[i / 2][i % 2] = evaluator->eval(value_editor[i]->get_text());
  1241. } else {
  1242. m.elements[i / 2][i % 2] = value_editor[i]->get_text().to_double();
  1243. }
  1244. }
  1245. v = m;
  1246. _emit_changed_whole_or_field();
  1247. } break;
  1248. case Variant::BASIS: {
  1249. Basis m;
  1250. for (int i = 0; i < 9; i++) {
  1251. if (evaluator) {
  1252. m.elements[i / 3][i % 3] = evaluator->eval(value_editor[i]->get_text());
  1253. } else {
  1254. m.elements[i / 3][i % 3] = value_editor[i]->get_text().to_double();
  1255. }
  1256. }
  1257. v = m;
  1258. _emit_changed_whole_or_field();
  1259. } break;
  1260. case Variant::TRANSFORM: {
  1261. Basis basis;
  1262. for (int i = 0; i < 9; i++) {
  1263. if (evaluator) {
  1264. basis.elements[i / 3][i % 3] = evaluator->eval(value_editor[(i / 3) * 4 + i % 3]->get_text());
  1265. } else {
  1266. basis.elements[i / 3][i % 3] = value_editor[(i / 3) * 4 + i % 3]->get_text().to_double();
  1267. }
  1268. }
  1269. Vector3 origin;
  1270. if (evaluator) {
  1271. origin.x = evaluator->eval(value_editor[3]->get_text());
  1272. origin.y = evaluator->eval(value_editor[7]->get_text());
  1273. origin.z = evaluator->eval(value_editor[11]->get_text());
  1274. } else {
  1275. origin.x = value_editor[3]->get_text().to_double();
  1276. origin.y = value_editor[7]->get_text().to_double();
  1277. origin.z = value_editor[11]->get_text().to_double();
  1278. }
  1279. v = Transform(basis, origin);
  1280. _emit_changed_whole_or_field();
  1281. } break;
  1282. case Variant::COLOR: {
  1283. } break;
  1284. case Variant::NODE_PATH: {
  1285. v = NodePath(value_editor[0]->get_text());
  1286. emit_signal("variant_changed");
  1287. } break;
  1288. case Variant::DICTIONARY: {
  1289. } break;
  1290. case Variant::POOL_BYTE_ARRAY: {
  1291. } break;
  1292. case Variant::POOL_INT_ARRAY: {
  1293. } break;
  1294. case Variant::POOL_REAL_ARRAY: {
  1295. } break;
  1296. case Variant::POOL_STRING_ARRAY: {
  1297. } break;
  1298. case Variant::POOL_VECTOR3_ARRAY: {
  1299. } break;
  1300. case Variant::POOL_COLOR_ARRAY: {
  1301. } break;
  1302. default: {}
  1303. }
  1304. updating = false;
  1305. }
  1306. void CustomPropertyEditor::_emit_changed_whole_or_field() {
  1307. if (!Input::get_singleton()->is_key_pressed(KEY_SHIFT)) {
  1308. emit_signal("variant_changed");
  1309. } else {
  1310. emit_signal("variant_field_changed", field_names[focused_value_editor]);
  1311. }
  1312. }
  1313. void CustomPropertyEditor::_range_modified(double p_value) {
  1314. v = p_value;
  1315. emit_signal("variant_changed");
  1316. }
  1317. void CustomPropertyEditor::_focus_enter() {
  1318. switch (type) {
  1319. case Variant::REAL:
  1320. case Variant::STRING:
  1321. case Variant::VECTOR2:
  1322. case Variant::RECT2:
  1323. case Variant::VECTOR3:
  1324. case Variant::PLANE:
  1325. case Variant::QUAT:
  1326. case Variant::RECT3:
  1327. case Variant::TRANSFORM2D:
  1328. case Variant::BASIS:
  1329. case Variant::TRANSFORM: {
  1330. for (int i = 0; i < MAX_VALUE_EDITORS; ++i) {
  1331. if (value_editor[i]->has_focus()) {
  1332. focused_value_editor = i;
  1333. value_editor[i]->select_all();
  1334. break;
  1335. }
  1336. }
  1337. } break;
  1338. default: {}
  1339. }
  1340. }
  1341. void CustomPropertyEditor::_focus_exit() {
  1342. switch (type) {
  1343. case Variant::REAL:
  1344. case Variant::STRING:
  1345. case Variant::VECTOR2:
  1346. case Variant::RECT2:
  1347. case Variant::VECTOR3:
  1348. case Variant::PLANE:
  1349. case Variant::QUAT:
  1350. case Variant::RECT3:
  1351. case Variant::TRANSFORM2D:
  1352. case Variant::BASIS:
  1353. case Variant::TRANSFORM: {
  1354. for (int i = 0; i < MAX_VALUE_EDITORS; ++i) {
  1355. value_editor[i]->select(0, 0);
  1356. }
  1357. } break;
  1358. default: {}
  1359. }
  1360. }
  1361. void CustomPropertyEditor::config_action_buttons(const List<String> &p_strings) {
  1362. int cell_width = 60;
  1363. int cell_height = 25;
  1364. int cell_margin = 5;
  1365. set_size(Size2(cell_margin + (cell_width + cell_margin) * p_strings.size(), (cell_margin * 2) + cell_height) * EDSCALE);
  1366. for (int i = 0; i < MAX_ACTION_BUTTONS; i++) {
  1367. if (i < p_strings.size()) {
  1368. action_buttons[i]->show();
  1369. action_buttons[i]->set_text(p_strings[i]);
  1370. action_buttons[i]->set_position(Point2(cell_margin + (cell_width + cell_margin) * i, cell_margin) * EDSCALE);
  1371. action_buttons[i]->set_size(Size2(cell_width, cell_height - cell_margin * 2) * EDSCALE);
  1372. action_buttons[i]->set_flat(true);
  1373. } else {
  1374. action_buttons[i]->hide();
  1375. }
  1376. }
  1377. }
  1378. void CustomPropertyEditor::config_value_editors(int p_amount, int p_columns, int p_label_w, const List<String> &p_strings) {
  1379. int cell_width = 95;
  1380. int cell_height = 25;
  1381. int cell_margin = 5;
  1382. int hor_spacing = 5; // Spacing between labels and their values
  1383. int rows = ((p_amount - 1) / p_columns) + 1;
  1384. set_size(Size2(cell_margin + p_label_w + (cell_width + cell_margin + p_label_w) * p_columns, cell_margin + (cell_height + cell_margin) * rows) * EDSCALE);
  1385. for (int i = 0; i < MAX_VALUE_EDITORS; i++) {
  1386. int c = i % p_columns;
  1387. int r = i / p_columns;
  1388. if (i < p_amount) {
  1389. value_editor[i]->show();
  1390. value_label[i]->show();
  1391. value_label[i]->set_text(i < p_strings.size() ? p_strings[i] : String(""));
  1392. value_editor[i]->set_position(Point2(cell_margin + p_label_w + hor_spacing + (cell_width + cell_margin + p_label_w + hor_spacing) * c, cell_margin + (cell_height + cell_margin) * r) * EDSCALE);
  1393. value_editor[i]->set_size(Size2(cell_width, cell_height));
  1394. value_label[i]->set_position(Point2(cell_margin + (cell_width + cell_margin + p_label_w + hor_spacing) * c, cell_margin + (cell_height + cell_margin) * r) * EDSCALE);
  1395. value_editor[i]->set_editable(!read_only);
  1396. } else {
  1397. value_editor[i]->hide();
  1398. value_label[i]->hide();
  1399. }
  1400. }
  1401. }
  1402. void CustomPropertyEditor::_bind_methods() {
  1403. ClassDB::bind_method("_focus_enter", &CustomPropertyEditor::_focus_enter);
  1404. ClassDB::bind_method("_focus_exit", &CustomPropertyEditor::_focus_exit);
  1405. ClassDB::bind_method("_modified", &CustomPropertyEditor::_modified);
  1406. ClassDB::bind_method("_range_modified", &CustomPropertyEditor::_range_modified);
  1407. ClassDB::bind_method("_action_pressed", &CustomPropertyEditor::_action_pressed);
  1408. ClassDB::bind_method("_file_selected", &CustomPropertyEditor::_file_selected);
  1409. ClassDB::bind_method("_type_create_selected", &CustomPropertyEditor::_type_create_selected);
  1410. ClassDB::bind_method("_node_path_selected", &CustomPropertyEditor::_node_path_selected);
  1411. ClassDB::bind_method("_color_changed", &CustomPropertyEditor::_color_changed);
  1412. ClassDB::bind_method("_draw_easing", &CustomPropertyEditor::_draw_easing);
  1413. ClassDB::bind_method("_drag_easing", &CustomPropertyEditor::_drag_easing);
  1414. ClassDB::bind_method("_text_edit_changed", &CustomPropertyEditor::_text_edit_changed);
  1415. ClassDB::bind_method("_menu_option", &CustomPropertyEditor::_menu_option);
  1416. ClassDB::bind_method("_create_dialog_callback", &CustomPropertyEditor::_create_dialog_callback);
  1417. ClassDB::bind_method("_create_selected_property", &CustomPropertyEditor::_create_selected_property);
  1418. ADD_SIGNAL(MethodInfo("variant_changed"));
  1419. ADD_SIGNAL(MethodInfo("variant_field_changed", PropertyInfo(Variant::STRING, "field")));
  1420. ADD_SIGNAL(MethodInfo("resource_edit_request"));
  1421. }
  1422. CustomPropertyEditor::CustomPropertyEditor() {
  1423. read_only = false;
  1424. updating = false;
  1425. for (int i = 0; i < MAX_VALUE_EDITORS; i++) {
  1426. value_editor[i] = memnew(LineEdit);
  1427. add_child(value_editor[i]);
  1428. value_label[i] = memnew(Label);
  1429. add_child(value_label[i]);
  1430. value_editor[i]->hide();
  1431. value_label[i]->hide();
  1432. value_editor[i]->connect("text_entered", this, "_modified");
  1433. value_editor[i]->connect("focus_entered", this, "_focus_enter");
  1434. value_editor[i]->connect("focus_exited", this, "_focus_exit");
  1435. }
  1436. focused_value_editor = -1;
  1437. for (int i = 0; i < 4; i++) {
  1438. scroll[i] = memnew(HScrollBar);
  1439. scroll[i]->hide();
  1440. scroll[i]->set_min(0);
  1441. scroll[i]->set_max(1.0);
  1442. scroll[i]->set_step(0.01);
  1443. add_child(scroll[i]);
  1444. }
  1445. checks20gc = memnew(GridContainer);
  1446. add_child(checks20gc);
  1447. checks20gc->set_columns(11);
  1448. for (int i = 0; i < 20; i++) {
  1449. if (i == 5 || i == 15) {
  1450. Control *space = memnew(Control);
  1451. space->set_custom_minimum_size(Size2(20, 0) * EDSCALE);
  1452. checks20gc->add_child(space);
  1453. }
  1454. checks20[i] = memnew(CheckBox);
  1455. checks20[i]->set_toggle_mode(true);
  1456. checks20[i]->set_focus_mode(FOCUS_NONE);
  1457. checks20gc->add_child(checks20[i]);
  1458. checks20[i]->hide();
  1459. checks20[i]->connect("pressed", this, "_action_pressed", make_binds(i));
  1460. checks20[i]->set_tooltip(vformat(TTR("Bit %d, val %d."), i, 1 << i));
  1461. }
  1462. text_edit = memnew(TextEdit);
  1463. add_child(text_edit);
  1464. text_edit->set_anchors_and_margins_preset(Control::PRESET_WIDE, Control::PRESET_MODE_MINSIZE, 5);
  1465. text_edit->set_margin(MARGIN_BOTTOM, -30);
  1466. text_edit->hide();
  1467. text_edit->connect("text_changed", this, "_text_edit_changed");
  1468. for (int i = 0; i < MAX_ACTION_BUTTONS; i++) {
  1469. action_buttons[i] = memnew(Button);
  1470. action_buttons[i]->hide();
  1471. add_child(action_buttons[i]);
  1472. Vector<Variant> binds;
  1473. binds.push_back(i);
  1474. action_buttons[i]->connect("pressed", this, "_action_pressed", binds);
  1475. }
  1476. color_picker = NULL;
  1477. set_as_toplevel(true);
  1478. file = memnew(EditorFileDialog);
  1479. add_child(file);
  1480. file->hide();
  1481. file->connect("file_selected", this, "_file_selected");
  1482. file->connect("dir_selected", this, "_file_selected");
  1483. error = memnew(ConfirmationDialog);
  1484. error->set_title(TTR("Error!"));
  1485. add_child(error);
  1486. scene_tree = memnew(SceneTreeDialog);
  1487. add_child(scene_tree);
  1488. scene_tree->connect("selected", this, "_node_path_selected");
  1489. scene_tree->get_scene_tree()->set_show_enabled_subscene(true);
  1490. texture_preview = memnew(TextureRect);
  1491. add_child(texture_preview);
  1492. texture_preview->hide();
  1493. easing_draw = memnew(Control);
  1494. add_child(easing_draw);
  1495. easing_draw->hide();
  1496. easing_draw->connect("draw", this, "_draw_easing");
  1497. easing_draw->connect("gui_input", this, "_drag_easing");
  1498. easing_draw->set_default_cursor_shape(Control::CURSOR_MOVE);
  1499. type_button = memnew(MenuButton);
  1500. add_child(type_button);
  1501. type_button->hide();
  1502. type_button->get_popup()->connect("id_pressed", this, "_type_create_selected");
  1503. menu = memnew(PopupMenu);
  1504. add_child(menu);
  1505. menu->connect("id_pressed", this, "_menu_option");
  1506. evaluator = NULL;
  1507. spinbox = memnew(SpinBox);
  1508. add_child(spinbox);
  1509. spinbox->set_anchors_and_margins_preset(Control::PRESET_WIDE, Control::PRESET_MODE_MINSIZE, 5);
  1510. spinbox->connect("value_changed", this, "_range_modified");
  1511. slider = memnew(HSlider);
  1512. add_child(slider);
  1513. slider->set_anchors_and_margins_preset(Control::PRESET_WIDE, Control::PRESET_MODE_MINSIZE, 5);
  1514. slider->connect("value_changed", this, "_range_modified");
  1515. create_dialog = NULL;
  1516. property_select = NULL;
  1517. }
  1518. bool PropertyEditor::_might_be_in_instance() {
  1519. if (!obj)
  1520. return false;
  1521. Node *node = Object::cast_to<Node>(obj);
  1522. Node *edited_scene = EditorNode::get_singleton()->get_edited_scene();
  1523. bool might_be = false;
  1524. while (node) {
  1525. if (node->get_scene_instance_state().is_valid()) {
  1526. might_be = true;
  1527. break;
  1528. }
  1529. if (node == edited_scene) {
  1530. if (node->get_scene_inherited_state().is_valid()) {
  1531. might_be = true;
  1532. break;
  1533. }
  1534. might_be = false;
  1535. break;
  1536. }
  1537. node = node->get_owner();
  1538. }
  1539. return might_be;
  1540. }
  1541. bool PropertyEditor::_get_instanced_node_original_property(const StringName &p_prop, Variant &value) {
  1542. Node *node = Object::cast_to<Node>(obj);
  1543. if (!node)
  1544. return false;
  1545. Node *orig = node;
  1546. Node *edited_scene = EditorNode::get_singleton()->get_edited_scene();
  1547. bool found = false;
  1548. while (node) {
  1549. Ref<SceneState> ss;
  1550. if (node == edited_scene) {
  1551. ss = node->get_scene_inherited_state();
  1552. } else {
  1553. ss = node->get_scene_instance_state();
  1554. }
  1555. if (ss.is_valid()) {
  1556. NodePath np = node->get_path_to(orig);
  1557. int node_idx = ss->find_node_by_path(np);
  1558. if (node_idx >= 0) {
  1559. bool lfound = false;
  1560. Variant lvar;
  1561. lvar = ss->get_property_value(node_idx, p_prop, lfound);
  1562. if (lfound) {
  1563. found = true;
  1564. value = lvar;
  1565. }
  1566. }
  1567. }
  1568. if (node == edited_scene) {
  1569. //just in case
  1570. break;
  1571. }
  1572. node = node->get_owner();
  1573. }
  1574. return found;
  1575. }
  1576. bool PropertyEditor::_is_property_different(const Variant &p_current, const Variant &p_orig, int p_usage) {
  1577. {
  1578. Node *node = Object::cast_to<Node>(obj);
  1579. if (!node)
  1580. return false;
  1581. Node *edited_scene = EditorNode::get_singleton()->get_edited_scene();
  1582. bool found_state = false;
  1583. while (node) {
  1584. Ref<SceneState> ss;
  1585. if (node == edited_scene) {
  1586. ss = node->get_scene_inherited_state();
  1587. } else {
  1588. ss = node->get_scene_instance_state();
  1589. }
  1590. if (ss.is_valid()) {
  1591. found_state = true;
  1592. }
  1593. if (node == edited_scene) {
  1594. //just in case
  1595. break;
  1596. }
  1597. node = node->get_owner();
  1598. }
  1599. if (!found_state)
  1600. return false; //pointless to check if we are not comparing against anything.
  1601. }
  1602. if (p_orig.get_type() == Variant::NIL) {
  1603. //special cases
  1604. if (p_current.is_zero() && p_usage & PROPERTY_USAGE_STORE_IF_NONZERO)
  1605. return false;
  1606. if (p_current.is_one() && p_usage & PROPERTY_USAGE_STORE_IF_NONONE)
  1607. return false;
  1608. }
  1609. if (p_current.get_type() == Variant::REAL && p_orig.get_type() == Variant::REAL) {
  1610. float a = p_current;
  1611. float b = p_orig;
  1612. return Math::abs(a - b) > CMP_EPSILON; //this must be done because, as some scenes save as text, there might be a tiny difference in floats due to numerical error
  1613. }
  1614. return bool(Variant::evaluate(Variant::OP_NOT_EQUAL, p_current, p_orig));
  1615. }
  1616. TreeItem *PropertyEditor::find_item(TreeItem *p_item, const String &p_name) {
  1617. if (!p_item)
  1618. return NULL;
  1619. String name = p_item->get_metadata(1);
  1620. if (name == p_name) {
  1621. return p_item;
  1622. }
  1623. TreeItem *c = p_item->get_children();
  1624. while (c) {
  1625. TreeItem *found = find_item(c, p_name);
  1626. if (found)
  1627. return found;
  1628. c = c->get_next();
  1629. }
  1630. return NULL;
  1631. }
  1632. void PropertyEditor::_changed_callback(Object *p_changed, const char *p_prop) {
  1633. _changed_callbacks(p_changed, p_prop);
  1634. }
  1635. void PropertyEditor::_changed_callbacks(Object *p_changed, const String &p_prop) {
  1636. if (p_changed != obj)
  1637. return;
  1638. if (changing)
  1639. return;
  1640. if (p_prop == String())
  1641. update_tree_pending = true;
  1642. else {
  1643. pending[p_prop] = p_prop;
  1644. }
  1645. }
  1646. void PropertyEditor::update_property(const String &p_prop) {
  1647. if (obj)
  1648. _changed_callbacks(obj, p_prop);
  1649. }
  1650. void PropertyEditor::set_item_text(TreeItem *p_item, int p_type, const String &p_name, int p_hint, const String &p_hint_text) {
  1651. switch (p_type) {
  1652. case Variant::BOOL: {
  1653. p_item->set_checked(1, obj->get(p_name));
  1654. } break;
  1655. case Variant::REAL:
  1656. case Variant::INT: {
  1657. if (p_hint == PROPERTY_HINT_LAYERS_2D_PHYSICS || p_hint == PROPERTY_HINT_LAYERS_2D_RENDER || p_hint == PROPERTY_HINT_LAYERS_3D_PHYSICS || p_hint == PROPERTY_HINT_LAYERS_3D_RENDER) {
  1658. tree->update();
  1659. break;
  1660. }
  1661. if (p_hint == PROPERTY_HINT_FLAGS) {
  1662. Vector<String> values = p_hint_text.split(",");
  1663. String flags;
  1664. int val = obj->get(p_name);
  1665. for (int i = 0; i < values.size(); i++) {
  1666. String v = values[i];
  1667. if (v == "")
  1668. continue;
  1669. if (!(val & (1 << i)))
  1670. continue;
  1671. if (flags != "")
  1672. flags += ", ";
  1673. flags += v;
  1674. }
  1675. p_item->set_text(1, flags);
  1676. break;
  1677. }
  1678. if (p_hint == PROPERTY_HINT_EXP_EASING) {
  1679. p_item->set_text(1, String::num(obj->get(p_name), 2));
  1680. break;
  1681. }
  1682. if (p_type == Variant::REAL) {
  1683. p_item->set_range(1, obj->get(p_name));
  1684. } else {
  1685. /* FIXME: Why are both statements equal? */
  1686. p_item->set_range(1, obj->get(p_name));
  1687. }
  1688. p_item->set_editable(1, !read_only);
  1689. } break;
  1690. case Variant::STRING:
  1691. if (p_hint == PROPERTY_HINT_TYPE_STRING) {
  1692. p_item->set_text(1, obj->get(p_name));
  1693. }
  1694. if (p_hint == PROPERTY_HINT_METHOD_OF_VARIANT_TYPE ||
  1695. p_hint == PROPERTY_HINT_METHOD_OF_BASE_TYPE ||
  1696. p_hint == PROPERTY_HINT_METHOD_OF_INSTANCE ||
  1697. p_hint == PROPERTY_HINT_METHOD_OF_SCRIPT ||
  1698. p_hint == PROPERTY_HINT_PROPERTY_OF_VARIANT_TYPE ||
  1699. p_hint == PROPERTY_HINT_PROPERTY_OF_BASE_TYPE ||
  1700. p_hint == PROPERTY_HINT_PROPERTY_OF_INSTANCE ||
  1701. p_hint == PROPERTY_HINT_PROPERTY_OF_SCRIPT) {
  1702. p_item->set_text(1, obj->get(p_name));
  1703. }
  1704. if (p_hint == PROPERTY_HINT_ENUM) {
  1705. Vector<String> strings = p_hint_text.split(",");
  1706. String current = obj->get(p_name);
  1707. int idx = 0;
  1708. for (int x = 0; x < strings.size(); x++) {
  1709. if (strings[x] == current) {
  1710. idx = x;
  1711. break;
  1712. }
  1713. }
  1714. p_item->set_text(1, p_hint_text);
  1715. p_item->set_range(1, idx);
  1716. break;
  1717. }
  1718. case Variant::VECTOR3:
  1719. case Variant::QUAT:
  1720. case Variant::VECTOR2:
  1721. case Variant::RECT3:
  1722. case Variant::RECT2:
  1723. case Variant::TRANSFORM2D:
  1724. case Variant::BASIS:
  1725. case Variant::TRANSFORM: {
  1726. p_item->set_text(1, obj->get(p_name));
  1727. } break;
  1728. case Variant::COLOR: {
  1729. tree->update();
  1730. } break;
  1731. case Variant::NODE_PATH: {
  1732. p_item->set_text(1, obj->get(p_name));
  1733. } break;
  1734. case Variant::OBJECT: {
  1735. Ref<EncodedObjectAsID> encoded = obj->get(p_name); //for debugger and remote tools
  1736. if (encoded.is_valid()) {
  1737. p_item->set_text(1, "Object: " + itos(encoded->get_object_id()));
  1738. p_item->set_icon(1, Ref<Texture>());
  1739. p_item->set_custom_as_button(1, true);
  1740. } else if (obj->get(p_name).get_type() == Variant::NIL || obj->get(p_name).operator RefPtr().is_null()) {
  1741. p_item->set_text(1, "<null>");
  1742. p_item->set_icon(1, Ref<Texture>());
  1743. p_item->set_custom_as_button(1, false);
  1744. Dictionary d = p_item->get_metadata(0);
  1745. int hint = d.has("hint") ? d["hint"].operator int() : -1;
  1746. String hint_text = d.has("hint_text") ? d["hint_text"] : "";
  1747. if (hint == PROPERTY_HINT_RESOURCE_TYPE && hint_text == "Texture") {
  1748. p_item->set_icon(1, NULL);
  1749. }
  1750. } else {
  1751. p_item->set_custom_as_button(1, true);
  1752. RES res = obj->get(p_name).operator RefPtr();
  1753. if (res->is_class("Texture")) {
  1754. int tw = EditorSettings::get_singleton()->get("docks/property_editor/texture_preview_width");
  1755. Vector2 size(res->call("get_width"), res->call("get_height"));
  1756. if (size.width < size.height) {
  1757. tw = MAX((size.width / size.height) * tw, 1);
  1758. }
  1759. p_item->set_icon_max_width(1, tw);
  1760. p_item->set_icon(1, res);
  1761. p_item->set_text(1, "");
  1762. } else if (res->get_name() != "") {
  1763. p_item->set_text(1, res->get_name());
  1764. } else if (res->get_path() != "" && !res->get_path().begins_with("local://")) {
  1765. p_item->set_text(1, res->get_path().get_file());
  1766. } else {
  1767. p_item->set_text(1, "<" + res->get_class() + ">");
  1768. };
  1769. if (res.is_valid() && res->get_path().is_resource_file()) {
  1770. p_item->set_tooltip(1, res->get_path());
  1771. } else if (res.is_valid()) {
  1772. p_item->set_tooltip(1, res->get_name() + " (" + res->get_class() + ")");
  1773. }
  1774. if (has_icon(res->get_class(), "EditorIcons")) {
  1775. p_item->set_icon(0, get_icon(res->get_class(), "EditorIcons"));
  1776. } else {
  1777. Dictionary d = p_item->get_metadata(0);
  1778. int hint = d.has("hint") ? d["hint"].operator int() : -1;
  1779. String hint_text = d.has("hint_text") ? d["hint_text"] : "";
  1780. if (hint == PROPERTY_HINT_RESOURCE_TYPE) {
  1781. if (has_icon(hint_text, "EditorIcons")) {
  1782. p_item->set_icon(0, get_icon(hint_text, "EditorIcons"));
  1783. } else {
  1784. p_item->set_icon(0, get_icon("Object", "EditorIcons"));
  1785. }
  1786. }
  1787. }
  1788. if (res->is_class("Script")) {
  1789. p_item->set_text(1, res->get_path().get_file());
  1790. } else if (!res->is_class("Texture")) {
  1791. //texture already previews via itself
  1792. EditorResourcePreview::get_singleton()->queue_edited_resource_preview(res, this, "_resource_preview_done", p_item->get_instance_id());
  1793. }
  1794. }
  1795. } break;
  1796. default: {};
  1797. }
  1798. }
  1799. void PropertyEditor::_check_reload_status(const String &p_name, TreeItem *item) {
  1800. bool has_reload = false;
  1801. int found = -1;
  1802. bool is_disabled = false;
  1803. for (int i = 0; i < item->get_button_count(1); i++) {
  1804. if (item->get_button_id(1, i) == 3) {
  1805. found = i;
  1806. is_disabled = item->is_button_disabled(1, i);
  1807. break;
  1808. }
  1809. }
  1810. if (obj->call("property_can_revert", p_name).operator bool()) {
  1811. has_reload = true;
  1812. }
  1813. if (!has_reload && !obj->get_script().is_null()) {
  1814. Ref<Script> scr = obj->get_script();
  1815. Variant orig_value;
  1816. if (scr->get_property_default_value(p_name, orig_value)) {
  1817. if (orig_value != obj->get(p_name)) {
  1818. has_reload = true;
  1819. }
  1820. }
  1821. }
  1822. if (found != -1 && !has_reload) {
  1823. if (!is_disabled) {
  1824. item->erase_button(1, found);
  1825. if (item->get_cell_mode(1) == TreeItem::CELL_MODE_RANGE && item->get_text(1) == String()) {
  1826. item->add_button(1, get_icon("ReloadEmpty", "EditorIcons"), 3, true);
  1827. }
  1828. }
  1829. } else if (found == -1 && has_reload) {
  1830. item->add_button(1, get_icon("ReloadSmall", "EditorIcons"), 3);
  1831. } else if (found != -1 && has_reload && is_disabled) {
  1832. item->erase_button(1, found);
  1833. item->add_button(1, get_icon("ReloadSmall", "EditorIcons"), 3);
  1834. }
  1835. }
  1836. bool PropertyEditor::_is_drop_valid(const Dictionary &p_drag_data, const Dictionary &p_item_data) const {
  1837. Dictionary d = p_item_data;
  1838. if (d.has("type")) {
  1839. int type = d["type"];
  1840. if (type == Variant::OBJECT && d.has("hint") && d.has("hint_text") && int(d["hint"]) == PROPERTY_HINT_RESOURCE_TYPE) {
  1841. String allowed_type = d["hint_text"];
  1842. Dictionary drag_data = p_drag_data;
  1843. if (drag_data.has("type") && String(drag_data["type"]) == "resource") {
  1844. Ref<Resource> res = drag_data["resource"];
  1845. for (int i = 0; i < allowed_type.get_slice_count(","); i++) {
  1846. String at = allowed_type.get_slice(",", i).strip_edges();
  1847. if (res.is_valid() && ClassDB::is_parent_class(res->get_class(), at)) {
  1848. return true;
  1849. }
  1850. }
  1851. }
  1852. if (drag_data.has("type") && String(drag_data["type"]) == "files") {
  1853. Vector<String> files = drag_data["files"];
  1854. if (files.size() == 1) {
  1855. String file = files[0];
  1856. String ftype = EditorFileSystem::get_singleton()->get_file_type(file);
  1857. if (ftype != "") {
  1858. for (int i = 0; i < allowed_type.get_slice_count(","); i++) {
  1859. String at = allowed_type.get_slice(",", i).strip_edges();
  1860. if (ClassDB::is_parent_class(ftype, at)) {
  1861. return true;
  1862. }
  1863. }
  1864. }
  1865. }
  1866. }
  1867. }
  1868. }
  1869. return false;
  1870. }
  1871. void PropertyEditor::_mark_drop_fields(TreeItem *p_at) {
  1872. if (_is_drop_valid(get_viewport()->gui_get_drag_data(), p_at->get_metadata(0)))
  1873. p_at->set_custom_bg_color(1, get_color("accent_color", "Editor"), true);
  1874. if (p_at->get_children()) {
  1875. _mark_drop_fields(p_at->get_children());
  1876. }
  1877. if (p_at->get_next()) {
  1878. _mark_drop_fields(p_at->get_next());
  1879. }
  1880. }
  1881. Variant PropertyEditor::get_drag_data_fw(const Point2 &p_point, Control *p_from) {
  1882. TreeItem *item = tree->get_item_at_position(p_point);
  1883. if (!item)
  1884. return Variant();
  1885. Dictionary d = item->get_metadata(0);
  1886. if (!d.has("name"))
  1887. return Variant();
  1888. int col = tree->get_column_at_position(p_point);
  1889. if (col == 0) {
  1890. Dictionary dp;
  1891. dp["type"] = "obj_property";
  1892. dp["object"] = obj;
  1893. dp["property"] = d["name"];
  1894. dp["value"] = obj->get(d["name"]);
  1895. Label *label = memnew(Label);
  1896. label->set_text(d["name"]);
  1897. set_drag_preview(label);
  1898. return dp;
  1899. }
  1900. Variant val = obj->get(d["name"]);
  1901. if (val.get_type() == Variant::OBJECT) {
  1902. RES res = val;
  1903. if (res.is_valid()) {
  1904. custom_editor->hide_menu();
  1905. return EditorNode::get_singleton()->drag_resource(res, p_from);
  1906. }
  1907. }
  1908. return Variant();
  1909. }
  1910. bool PropertyEditor::can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) const {
  1911. TreeItem *item = tree->get_item_at_position(p_point);
  1912. if (!item)
  1913. return false;
  1914. int col = tree->get_column_at_position(p_point);
  1915. if (col != 1)
  1916. return false;
  1917. return _is_drop_valid(p_data, item->get_metadata(0));
  1918. }
  1919. void PropertyEditor::drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) {
  1920. TreeItem *item = tree->get_item_at_position(p_point);
  1921. if (!item)
  1922. return;
  1923. int col = tree->get_column_at_position(p_point);
  1924. if (col != 1)
  1925. return;
  1926. if (!_is_drop_valid(p_data, item->get_metadata(0)))
  1927. return;
  1928. Dictionary d = item->get_metadata(0);
  1929. if (!d.has("name"))
  1930. return;
  1931. String name = d["name"];
  1932. Dictionary drag_data = p_data;
  1933. if (drag_data.has("type") && String(drag_data["type"]) == "resource") {
  1934. Ref<Resource> res = drag_data["resource"];
  1935. if (res.is_valid()) {
  1936. _edit_set(name, res);
  1937. return;
  1938. }
  1939. }
  1940. if (drag_data.has("type") && String(drag_data["type"]) == "files") {
  1941. Vector<String> files = drag_data["files"];
  1942. if (files.size() == 1) {
  1943. String file = files[0];
  1944. RES res = ResourceLoader::load(file);
  1945. if (res.is_valid()) {
  1946. _edit_set(name, res);
  1947. return;
  1948. }
  1949. }
  1950. }
  1951. }
  1952. void PropertyEditor::_clear_drop_fields(TreeItem *p_at) {
  1953. Dictionary d = p_at->get_metadata(0);
  1954. if (d.has("type")) {
  1955. int type = d["type"];
  1956. if (type == Variant::OBJECT) {
  1957. p_at->clear_custom_bg_color(1);
  1958. }
  1959. }
  1960. if (p_at->get_children()) {
  1961. _clear_drop_fields(p_at->get_children());
  1962. }
  1963. if (p_at->get_next()) {
  1964. _clear_drop_fields(p_at->get_next());
  1965. }
  1966. }
  1967. void PropertyEditor::_notification(int p_what) {
  1968. if (p_what == NOTIFICATION_ENTER_TREE) {
  1969. get_tree()->connect("node_removed", this, "_node_removed");
  1970. }
  1971. if (p_what == NOTIFICATION_EXIT_TREE) {
  1972. get_tree()->disconnect("node_removed", this, "_node_removed");
  1973. edit(NULL);
  1974. }
  1975. if (p_what == NOTIFICATION_DRAG_BEGIN) {
  1976. if (is_visible_in_tree() && tree->get_root()) {
  1977. _mark_drop_fields(tree->get_root());
  1978. }
  1979. }
  1980. if (p_what == NOTIFICATION_DRAG_END) {
  1981. if (is_visible_in_tree() && tree->get_root()) {
  1982. _clear_drop_fields(tree->get_root());
  1983. }
  1984. }
  1985. if (p_what == NOTIFICATION_PHYSICS_PROCESS) {
  1986. if (refresh_countdown > 0) {
  1987. refresh_countdown -= get_physics_process_delta_time();
  1988. if (refresh_countdown <= 0) {
  1989. TreeItem *root = tree->get_root();
  1990. _refresh_item(root);
  1991. }
  1992. }
  1993. changing = true;
  1994. if (update_tree_pending) {
  1995. update_tree();
  1996. update_tree_pending = false;
  1997. } else {
  1998. const String *k = NULL;
  1999. while ((k = pending.next(k))) {
  2000. TreeItem *item = find_item(tree->get_root(), *k);
  2001. if (!item)
  2002. continue;
  2003. _check_reload_status(*k, item);
  2004. Dictionary d = item->get_metadata(0);
  2005. set_item_text(item, d["type"], d["name"], d["hint"], d["hint_text"]);
  2006. }
  2007. }
  2008. pending.clear();
  2009. changing = false;
  2010. }
  2011. if (p_what == EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED) {
  2012. update_tree();
  2013. }
  2014. }
  2015. TreeItem *PropertyEditor::get_parent_node(String p_path, HashMap<String, TreeItem *> &item_paths, TreeItem *root, TreeItem *category) {
  2016. TreeItem *item = NULL;
  2017. if (p_path == "") {
  2018. item = category ? category : root;
  2019. } else if (item_paths.has(p_path)) {
  2020. item = item_paths.get(p_path);
  2021. } else {
  2022. TreeItem *parent = get_parent_node(p_path.left(p_path.find_last("/")), item_paths, root, NULL);
  2023. item = tree->create_item(parent);
  2024. String name = (p_path.find("/") != -1) ? p_path.right(p_path.find_last("/") + 1) : p_path;
  2025. item->set_text(0, capitalize_paths ? name.capitalize() : name);
  2026. item->set_tooltip(0, p_path);
  2027. if (item->get_parent() != root) {
  2028. item->set_icon(0, get_icon("Folder", "EditorIcons"));
  2029. }
  2030. item->set_editable(0, false);
  2031. if (!subsection_selectable) {
  2032. item->set_expand_right(0, true);
  2033. }
  2034. item->set_selectable(0, subsection_selectable);
  2035. item->set_editable(1, false);
  2036. item->set_selectable(1, subsection_selectable);
  2037. if (use_folding) {
  2038. if (!obj->editor_is_section_unfolded(p_path)) {
  2039. updating_folding = true;
  2040. item->set_collapsed(true);
  2041. updating_folding = false;
  2042. }
  2043. item->set_metadata(0, p_path);
  2044. }
  2045. if (item->get_parent() == root) {
  2046. item->set_custom_bg_color(0, get_color("prop_subsection", "Editor"));
  2047. item->set_custom_bg_color(1, get_color("prop_subsection", "Editor"));
  2048. }
  2049. item_paths[p_path] = item;
  2050. }
  2051. return item;
  2052. }
  2053. void PropertyEditor::_refresh_item(TreeItem *p_item) {
  2054. if (!p_item)
  2055. return;
  2056. String name = p_item->get_metadata(1);
  2057. if (name != String()) {
  2058. _check_reload_status(name, p_item);
  2059. Dictionary d = p_item->get_metadata(0);
  2060. set_item_text(p_item, d["type"], d["name"], d["hint"], d["hint_text"]);
  2061. }
  2062. TreeItem *c = p_item->get_children();
  2063. while (c) {
  2064. _refresh_item(c);
  2065. c = c->get_next();
  2066. }
  2067. }
  2068. void PropertyEditor::refresh() {
  2069. if (refresh_countdown > 0)
  2070. return;
  2071. refresh_countdown = EditorSettings::get_singleton()->get("docks/property_editor/auto_refresh_interval");
  2072. }
  2073. void PropertyEditor::update_tree() {
  2074. tree->clear();
  2075. if (!obj)
  2076. return;
  2077. HashMap<String, TreeItem *> item_path;
  2078. TreeItem *root = tree->create_item(NULL);
  2079. tree->set_hide_root(true);
  2080. List<PropertyInfo> plist;
  2081. obj->get_property_list(&plist, true);
  2082. bool draw_red = false;
  2083. {
  2084. Node *nod = Object::cast_to<Node>(obj);
  2085. Node *es = EditorNode::get_singleton()->get_edited_scene();
  2086. if (nod && es != nod && nod->get_owner() != es) {
  2087. draw_red = true;
  2088. }
  2089. }
  2090. Color sscolor = get_color("prop_subsection", "Editor");
  2091. TreeItem *current_category = NULL;
  2092. String filter = search_box ? search_box->get_text() : "";
  2093. String group;
  2094. String group_base;
  2095. for (List<PropertyInfo>::Element *I = plist.front(); I; I = I->next()) {
  2096. PropertyInfo &p = I->get();
  2097. //make sure the property can be edited
  2098. if (p.usage & PROPERTY_USAGE_GROUP) {
  2099. group = p.name;
  2100. group_base = p.hint_string;
  2101. continue;
  2102. } else if (p.usage & PROPERTY_USAGE_CATEGORY) {
  2103. group = "";
  2104. group_base = "";
  2105. if (!show_categories)
  2106. continue;
  2107. List<PropertyInfo>::Element *N = I->next();
  2108. bool valid = true;
  2109. //if no properties in category, skip
  2110. while (N) {
  2111. if (N->get().usage & PROPERTY_USAGE_EDITOR)
  2112. break;
  2113. if (N->get().usage & PROPERTY_USAGE_CATEGORY) {
  2114. valid = false;
  2115. break;
  2116. }
  2117. N = N->next();
  2118. }
  2119. if (!valid)
  2120. continue; //empty, ignore
  2121. TreeItem *sep = tree->create_item(root);
  2122. current_category = sep;
  2123. String type = p.name;
  2124. //*
  2125. if (has_icon(type, "EditorIcons"))
  2126. sep->set_icon(0, get_icon(type, "EditorIcons"));
  2127. else
  2128. sep->set_icon(0, get_icon("Object", "EditorIcons"));
  2129. //*/
  2130. sep->set_text(0, type);
  2131. sep->set_expand_right(0, true);
  2132. sep->set_selectable(0, false);
  2133. sep->set_selectable(1, false);
  2134. sep->set_custom_bg_color(0, get_color("prop_category", "Editor"));
  2135. sep->set_custom_bg_color(1, get_color("prop_category", "Editor"));
  2136. sep->set_text_align(0, TreeItem::ALIGN_CENTER);
  2137. sep->set_disable_folding(true);
  2138. if (use_doc_hints) {
  2139. StringName type = p.name;
  2140. if (!class_descr_cache.has(type)) {
  2141. String descr;
  2142. DocData *dd = EditorHelp::get_doc_data();
  2143. Map<String, DocData::ClassDoc>::Element *E = dd->class_list.find(type);
  2144. if (E) {
  2145. descr = E->get().brief_description;
  2146. }
  2147. class_descr_cache[type] = descr.word_wrap(80);
  2148. }
  2149. sep->set_tooltip(0, TTR("Class:") + " " + p.name + ":\n\n" + class_descr_cache[type]);
  2150. }
  2151. continue;
  2152. } else if (!(p.usage & PROPERTY_USAGE_EDITOR))
  2153. continue;
  2154. if (hide_script && p.name == "script")
  2155. continue;
  2156. String basename = p.name;
  2157. if (group != "") {
  2158. if (group_base != "") {
  2159. if (basename.begins_with(group_base)) {
  2160. basename = basename.replace_first(group_base, "");
  2161. } else if (group_base.begins_with(basename)) {
  2162. //keep it, this is used pretty often
  2163. } else {
  2164. group = ""; //no longer using group base, clear
  2165. }
  2166. }
  2167. }
  2168. if (group != "") {
  2169. basename = group + "/" + basename;
  2170. }
  2171. String name = (basename.find("/") != -1) ? basename.right(basename.find_last("/") + 1) : basename;
  2172. if (capitalize_paths) {
  2173. int dot = name.find(".");
  2174. if (dot != -1) {
  2175. String ov = name.right(dot);
  2176. name = name.substr(0, dot);
  2177. name = name.camelcase_to_underscore().capitalize();
  2178. name += ov;
  2179. } else {
  2180. name = name.camelcase_to_underscore().capitalize();
  2181. }
  2182. }
  2183. String path = basename.left(basename.find_last("/"));
  2184. if (use_filter && filter != "") {
  2185. String cat = path;
  2186. if (capitalize_paths)
  2187. cat = cat.capitalize();
  2188. if (!filter.is_subsequence_ofi(cat) && !filter.is_subsequence_ofi(name))
  2189. continue;
  2190. }
  2191. TreeItem *parent = get_parent_node(path, item_path, root, current_category);
  2192. int level = 0;
  2193. if (parent != root) {
  2194. level++;
  2195. TreeItem *parent_lev = parent;
  2196. while (parent_lev->get_parent() != root) {
  2197. parent_lev = parent_lev->get_parent();
  2198. level++;
  2199. }
  2200. }
  2201. if (level > 4)
  2202. level = 4;
  2203. Color col = sscolor;
  2204. col.a = (level / 4.0) * 0.7;
  2205. TreeItem *item = tree->create_item(parent);
  2206. if (level > 0) {
  2207. item->set_custom_bg_color(0, col);
  2208. }
  2209. item->set_editable(0, false);
  2210. item->set_selectable(0, property_selectable);
  2211. if (p.usage & PROPERTY_USAGE_CHECKABLE) {
  2212. item->set_cell_mode(0, TreeItem::CELL_MODE_CHECK);
  2213. item->set_selectable(0, true);
  2214. item->set_editable(0, true);
  2215. item->set_checked(0, p.usage & PROPERTY_USAGE_CHECKED);
  2216. }
  2217. item->set_text(0, name);
  2218. item->set_tooltip(0, p.name);
  2219. if (name.find(".") != -1) {
  2220. Color textcol = get_color("font_color", "Tree");
  2221. textcol.a *= 0.5;
  2222. //override :D
  2223. item->set_custom_color(0, textcol);
  2224. item->set_custom_color(1, textcol);
  2225. Color iconcol(1, 1, 1, 0.6);
  2226. item->set_icon_color(0, iconcol);
  2227. item->set_icon_color(1, iconcol);
  2228. }
  2229. if (use_doc_hints) {
  2230. StringName setter;
  2231. StringName type;
  2232. if (ClassDB::get_setter_and_type_for_property(obj->get_class_name(), p.name, type, setter)) {
  2233. String descr;
  2234. bool found = false;
  2235. Map<StringName, Map<StringName, String> >::Element *E = descr_cache.find(type);
  2236. if (E) {
  2237. Map<StringName, String>::Element *F = E->get().find(setter);
  2238. if (F) {
  2239. found = true;
  2240. descr = F->get();
  2241. }
  2242. }
  2243. if (!found) {
  2244. DocData *dd = EditorHelp::get_doc_data();
  2245. Map<String, DocData::ClassDoc>::Element *E = dd->class_list.find(type);
  2246. if (E) {
  2247. for (int i = 0; i < E->get().methods.size(); i++) {
  2248. if (E->get().methods[i].name == setter.operator String()) {
  2249. descr = E->get().methods[i].description.strip_edges().word_wrap(80);
  2250. }
  2251. }
  2252. }
  2253. descr_cache[type][setter] = descr;
  2254. }
  2255. item->set_tooltip(0, TTR("Property:") + " " + p.name + "\n\n" + descr);
  2256. }
  2257. }
  2258. Dictionary d;
  2259. d["name"] = p.name;
  2260. d["type"] = (int)p.type;
  2261. d["hint"] = (int)p.hint;
  2262. d["hint_text"] = p.hint_string;
  2263. d["usage"] = (int)p.usage;
  2264. item->set_metadata(0, d);
  2265. item->set_metadata(1, p.name);
  2266. if (draw_red)
  2267. item->set_custom_color(0, get_color("error_color", "Editor"));
  2268. if (p.name == selected_property) {
  2269. item->select(1);
  2270. }
  2271. switch (p.type) {
  2272. case Variant::BOOL: {
  2273. item->set_cell_mode(1, TreeItem::CELL_MODE_CHECK);
  2274. item->set_text(1, TTR("On"));
  2275. item->set_tooltip(1, obj->get(p.name) ? "True" : "False");
  2276. item->set_checked(1, obj->get(p.name));
  2277. if (show_type_icons)
  2278. item->set_icon(0, get_icon("Bool", "EditorIcons"));
  2279. item->set_editable(1, !read_only);
  2280. } break;
  2281. case Variant::REAL:
  2282. case Variant::INT: {
  2283. if (p.hint == PROPERTY_HINT_EXP_EASING) {
  2284. item->set_cell_mode(1, TreeItem::CELL_MODE_CUSTOM);
  2285. item->set_text(1, String::num(obj->get(p.name), 2));
  2286. item->set_editable(1, !read_only);
  2287. if (show_type_icons)
  2288. item->set_icon(0, get_icon("Curve", "EditorIcons"));
  2289. break;
  2290. }
  2291. if (p.hint == PROPERTY_HINT_LAYERS_2D_PHYSICS || p.hint == PROPERTY_HINT_LAYERS_2D_RENDER || p.hint == PROPERTY_HINT_LAYERS_3D_PHYSICS || p.hint == PROPERTY_HINT_LAYERS_3D_RENDER) {
  2292. item->set_cell_mode(1, TreeItem::CELL_MODE_CUSTOM);
  2293. item->set_editable(1, !read_only);
  2294. item->set_custom_draw(1, this, "_draw_flags");
  2295. break;
  2296. }
  2297. if (p.hint == PROPERTY_HINT_FLAGS) {
  2298. item->set_cell_mode(1, TreeItem::CELL_MODE_CUSTOM);
  2299. item->set_editable(1, !read_only);
  2300. Vector<String> values = p.hint_string.split(",");
  2301. String flags;
  2302. int val = obj->get(p.name);
  2303. for (int i = 0; i < values.size(); i++) {
  2304. String v = values[i];
  2305. if (v == "")
  2306. continue;
  2307. if (!(val & (1 << i)))
  2308. continue;
  2309. if (flags != "")
  2310. flags += ", ";
  2311. flags += v;
  2312. }
  2313. item->set_text(1, flags);
  2314. break;
  2315. }
  2316. if (p.hint == PROPERTY_HINT_ENUM)
  2317. item->set_cell_mode(1, TreeItem::CELL_MODE_RANGE);
  2318. else
  2319. item->set_cell_mode(1, TreeItem::CELL_MODE_RANGE_EXPRESSION);
  2320. if (p.hint == PROPERTY_HINT_SPRITE_FRAME || p.hint == PROPERTY_HINT_RANGE || p.hint == PROPERTY_HINT_EXP_RANGE) {
  2321. int c = p.hint_string.get_slice_count(",");
  2322. float min = 0, max = 100, step = p.type == Variant::REAL ? .01 : 1;
  2323. if (c >= 1) {
  2324. min = p.hint_string.get_slice(",", 0).to_double();
  2325. }
  2326. if (c >= 2) {
  2327. max = p.hint_string.get_slice(",", 1).to_double();
  2328. }
  2329. if (p.hint != PROPERTY_HINT_SPRITE_FRAME && c >= 3) {
  2330. step = p.hint_string.get_slice(",", 2).to_double();
  2331. }
  2332. item->set_range_config(1, min, max, step, p.hint == PROPERTY_HINT_EXP_RANGE);
  2333. } else if (p.hint == PROPERTY_HINT_ENUM) {
  2334. item->set_text(1, p.hint_string);
  2335. if (show_type_icons)
  2336. item->set_icon(0, get_icon("Enum", "EditorIcons"));
  2337. item->set_range(1, obj->get(p.name));
  2338. item->set_editable(1, !read_only);
  2339. break;
  2340. } else if (p.hint == PROPERTY_HINT_OBJECT_ID) {
  2341. item->set_cell_mode(1, TreeItem::CELL_MODE_CUSTOM);
  2342. String type = p.hint_string;
  2343. if (type == "")
  2344. type = "Object";
  2345. ObjectID id = obj->get(p.name);
  2346. if (id != 0) {
  2347. item->set_text(1, type + " ID: " + itos(id));
  2348. item->add_button(1, get_icon("EditResource", "EditorIcons"));
  2349. } else {
  2350. item->set_text(1, "[Empty]");
  2351. }
  2352. if (has_icon(p.hint_string, "EditorIcons")) {
  2353. type = p.hint_string;
  2354. } else {
  2355. type = "Object";
  2356. }
  2357. item->set_icon(0, get_icon(type, "EditorIcons"));
  2358. break;
  2359. } else {
  2360. if (p.type == Variant::REAL) {
  2361. item->set_range_config(1, -16777216, 16777216, 0.001);
  2362. } else {
  2363. item->set_range_config(1, -2147483647, 2147483647, 1);
  2364. }
  2365. };
  2366. if (p.type == Variant::REAL) {
  2367. if (show_type_icons)
  2368. item->set_icon(0, get_icon("Real", "EditorIcons"));
  2369. item->set_range(1, obj->get(p.name));
  2370. } else {
  2371. if (show_type_icons)
  2372. item->set_icon(0, get_icon("Integer", "EditorIcons"));
  2373. item->set_range(1, obj->get(p.name));
  2374. }
  2375. item->set_editable(1, !read_only);
  2376. } break;
  2377. case Variant::STRING: {
  2378. switch (p.hint) {
  2379. case PROPERTY_HINT_DIR:
  2380. case PROPERTY_HINT_FILE:
  2381. case PROPERTY_HINT_GLOBAL_DIR:
  2382. case PROPERTY_HINT_GLOBAL_FILE: {
  2383. item->set_cell_mode(1, TreeItem::CELL_MODE_STRING);
  2384. item->set_editable(1, !read_only);
  2385. if (show_type_icons)
  2386. item->set_icon(0, get_icon("File", "EditorIcons"));
  2387. item->set_text(1, obj->get(p.name));
  2388. item->add_button(1, get_icon("Folder", "EditorIcons"));
  2389. } break;
  2390. case PROPERTY_HINT_ENUM: {
  2391. item->set_cell_mode(1, TreeItem::CELL_MODE_RANGE);
  2392. Vector<String> strings = p.hint_string.split(",");
  2393. String current = obj->get(p.name);
  2394. int idx = 0;
  2395. for (int x = 0; x < strings.size(); x++) {
  2396. if (strings[x] == current) {
  2397. idx = x;
  2398. break;
  2399. }
  2400. }
  2401. item->set_text(1, p.hint_string);
  2402. item->set_range(1, idx);
  2403. item->set_editable(1, !read_only);
  2404. if (show_type_icons)
  2405. item->set_icon(0, get_icon("Enum", "EditorIcons"));
  2406. } break;
  2407. case PROPERTY_HINT_METHOD_OF_VARIANT_TYPE: ///< a property of a type
  2408. case PROPERTY_HINT_METHOD_OF_BASE_TYPE: ///< a method of a base type
  2409. case PROPERTY_HINT_METHOD_OF_INSTANCE: ///< a method of an instance
  2410. case PROPERTY_HINT_METHOD_OF_SCRIPT: ///< a method of a script & base
  2411. case PROPERTY_HINT_PROPERTY_OF_VARIANT_TYPE: ///< a property of a type
  2412. case PROPERTY_HINT_PROPERTY_OF_BASE_TYPE: ///< a property of a base type
  2413. case PROPERTY_HINT_PROPERTY_OF_INSTANCE: ///< a property of an instance
  2414. case PROPERTY_HINT_PROPERTY_OF_SCRIPT: ///< a property of a script & base
  2415. case PROPERTY_HINT_TYPE_STRING: {
  2416. item->set_cell_mode(1, TreeItem::CELL_MODE_CUSTOM);
  2417. item->set_editable(1, !read_only);
  2418. if (show_type_icons)
  2419. item->set_icon(0, get_icon("String", "EditorIcons"));
  2420. item->set_text(1, obj->get(p.name));
  2421. } break;
  2422. default: {
  2423. item->set_cell_mode(1, TreeItem::CELL_MODE_STRING);
  2424. item->set_editable(1, !read_only);
  2425. if (show_type_icons)
  2426. item->set_icon(0, get_icon("String", "EditorIcons"));
  2427. item->set_text(1, obj->get(p.name));
  2428. if (p.hint == PROPERTY_HINT_MULTILINE_TEXT)
  2429. item->add_button(1, get_icon("MultiLine", "EditorIcons"));
  2430. } break;
  2431. }
  2432. } break;
  2433. case Variant::ARRAY: {
  2434. item->set_cell_mode(1, TreeItem::CELL_MODE_CUSTOM);
  2435. item->add_button(1, get_icon("EditResource", "EditorIcons"));
  2436. Variant v = obj->get(p.name);
  2437. String type_name = "Array";
  2438. String type_name_suffix = "";
  2439. String hint = p.hint_string;
  2440. while (hint.begins_with(itos(Variant::ARRAY) + ":")) {
  2441. type_name += "<Array";
  2442. type_name_suffix += ">";
  2443. hint = hint.substr(2, hint.size() - 2);
  2444. }
  2445. if (hint.find(":") >= 0) {
  2446. hint = hint.substr(0, hint.find(":"));
  2447. if (hint.find("/") >= 0) {
  2448. hint = hint.substr(0, hint.find("/"));
  2449. }
  2450. type_name += "<" + Variant::get_type_name(Variant::Type(hint.to_int()));
  2451. type_name_suffix += ">";
  2452. }
  2453. type_name += type_name_suffix;
  2454. if (v.is_array())
  2455. item->set_text(1, type_name + "[" + itos(v.call("size")) + "]");
  2456. else
  2457. item->set_text(1, type_name + "[]");
  2458. if (show_type_icons)
  2459. item->set_icon(0, get_icon("ArrayData", "EditorIcons"));
  2460. } break;
  2461. case Variant::DICTIONARY: {
  2462. item->set_cell_mode(1, TreeItem::CELL_MODE_STRING);
  2463. item->set_editable(1, false);
  2464. item->set_text(1, obj->get(p.name).operator String());
  2465. } break;
  2466. case Variant::POOL_INT_ARRAY: {
  2467. item->set_cell_mode(1, TreeItem::CELL_MODE_CUSTOM);
  2468. item->add_button(1, get_icon("EditResource", "EditorIcons"));
  2469. Variant v = obj->get(p.name);
  2470. if (v.is_array())
  2471. item->set_text(1, "IntArray[" + itos(v.call("size")) + "]");
  2472. else
  2473. item->set_text(1, "IntArray[]");
  2474. if (show_type_icons)
  2475. item->set_icon(0, get_icon("ArrayInt", "EditorIcons"));
  2476. } break;
  2477. case Variant::POOL_REAL_ARRAY: {
  2478. item->set_cell_mode(1, TreeItem::CELL_MODE_CUSTOM);
  2479. item->add_button(1, get_icon("EditResource", "EditorIcons"));
  2480. Variant v = obj->get(p.name);
  2481. if (v.is_array())
  2482. item->set_text(1, "FloatArray[" + itos(v.call("size")) + "]");
  2483. else
  2484. item->set_text(1, "FloatArray[]");
  2485. if (show_type_icons)
  2486. item->set_icon(0, get_icon("ArrayReal", "EditorIcons"));
  2487. } break;
  2488. case Variant::POOL_STRING_ARRAY: {
  2489. item->set_cell_mode(1, TreeItem::CELL_MODE_CUSTOM);
  2490. item->add_button(1, get_icon("EditResource", "EditorIcons"));
  2491. Variant v = obj->get(p.name);
  2492. if (v.is_array())
  2493. item->set_text(1, "String[" + itos(v.call("size")) + "]");
  2494. else
  2495. item->set_text(1, "String[]");
  2496. if (show_type_icons)
  2497. item->set_icon(0, get_icon("ArrayString", "EditorIcons"));
  2498. } break;
  2499. case Variant::POOL_BYTE_ARRAY: {
  2500. item->set_cell_mode(1, TreeItem::CELL_MODE_CUSTOM);
  2501. item->add_button(1, get_icon("EditResource", "EditorIcons"));
  2502. Variant v = obj->get(p.name);
  2503. if (v.is_array())
  2504. item->set_text(1, "Byte[" + itos(v.call("size")) + "]");
  2505. else
  2506. item->set_text(1, "Byte[]");
  2507. if (show_type_icons)
  2508. item->set_icon(0, get_icon("ArrayData", "EditorIcons"));
  2509. } break;
  2510. case Variant::POOL_VECTOR2_ARRAY: {
  2511. item->set_cell_mode(1, TreeItem::CELL_MODE_CUSTOM);
  2512. item->add_button(1, get_icon("EditResource", "EditorIcons"));
  2513. Variant v = obj->get(p.name);
  2514. if (v.is_array())
  2515. item->set_text(1, "Vector2[" + itos(v.call("size")) + "]");
  2516. else
  2517. item->set_text(1, "Vector2[]");
  2518. if (show_type_icons)
  2519. item->set_icon(0, get_icon("Vector2", "EditorIcons"));
  2520. } break;
  2521. case Variant::POOL_VECTOR3_ARRAY: {
  2522. item->set_cell_mode(1, TreeItem::CELL_MODE_CUSTOM);
  2523. item->add_button(1, get_icon("EditResource", "EditorIcons"));
  2524. Variant v = obj->get(p.name);
  2525. if (v.is_array())
  2526. item->set_text(1, "Vector3[" + itos(v.call("size")) + "]");
  2527. else
  2528. item->set_text(1, "Vector3[]");
  2529. if (show_type_icons)
  2530. item->set_icon(0, get_icon("Vector", "EditorIcons"));
  2531. } break;
  2532. case Variant::POOL_COLOR_ARRAY: {
  2533. item->set_cell_mode(1, TreeItem::CELL_MODE_CUSTOM);
  2534. item->add_button(1, get_icon("EditResource", "EditorIcons"));
  2535. Variant v = obj->get(p.name);
  2536. if (v.is_array())
  2537. item->set_text(1, "Color[" + itos(v.call("size")) + "]");
  2538. else
  2539. item->set_text(1, "Color[]");
  2540. if (show_type_icons)
  2541. item->set_icon(0, get_icon("Color", "EditorIcons"));
  2542. } break;
  2543. case Variant::VECTOR2: {
  2544. item->set_cell_mode(1, TreeItem::CELL_MODE_CUSTOM);
  2545. item->set_editable(1, true);
  2546. item->set_text(1, obj->get(p.name));
  2547. if (show_type_icons)
  2548. item->set_icon(0, get_icon("Vector2", "EditorIcons"));
  2549. } break;
  2550. case Variant::RECT2: {
  2551. item->set_cell_mode(1, TreeItem::CELL_MODE_CUSTOM);
  2552. item->set_editable(1, true);
  2553. item->set_text(1, obj->get(p.name));
  2554. if (show_type_icons)
  2555. item->set_icon(0, get_icon("Rect2", "EditorIcons"));
  2556. } break;
  2557. case Variant::VECTOR3: {
  2558. item->set_cell_mode(1, TreeItem::CELL_MODE_CUSTOM);
  2559. item->set_editable(1, true);
  2560. item->set_text(1, obj->get(p.name));
  2561. if (show_type_icons)
  2562. item->set_icon(0, get_icon("Vector", "EditorIcons"));
  2563. } break;
  2564. case Variant::TRANSFORM2D:
  2565. case Variant::BASIS: {
  2566. item->set_cell_mode(1, TreeItem::CELL_MODE_CUSTOM);
  2567. item->set_editable(1, true);
  2568. item->set_text(1, obj->get(p.name));
  2569. } break;
  2570. case Variant::TRANSFORM: {
  2571. item->set_cell_mode(1, TreeItem::CELL_MODE_CUSTOM);
  2572. item->set_editable(1, true);
  2573. item->set_text(1, obj->get(p.name));
  2574. if (show_type_icons)
  2575. item->set_icon(0, get_icon("Matrix", "EditorIcons"));
  2576. } break;
  2577. case Variant::PLANE: {
  2578. item->set_cell_mode(1, TreeItem::CELL_MODE_CUSTOM);
  2579. item->set_editable(1, true);
  2580. item->set_text(1, obj->get(p.name));
  2581. if (show_type_icons)
  2582. item->set_icon(0, get_icon("Plane", "EditorIcons"));
  2583. } break;
  2584. case Variant::RECT3: {
  2585. item->set_cell_mode(1, TreeItem::CELL_MODE_CUSTOM);
  2586. item->set_editable(1, true);
  2587. item->set_text(1, "Rect3");
  2588. if (show_type_icons)
  2589. item->set_icon(0, get_icon("Rect3", "EditorIcons"));
  2590. } break;
  2591. case Variant::QUAT: {
  2592. item->set_cell_mode(1, TreeItem::CELL_MODE_CUSTOM);
  2593. item->set_editable(1, true);
  2594. item->set_text(1, obj->get(p.name));
  2595. if (show_type_icons)
  2596. item->set_icon(0, get_icon("Quat", "EditorIcons"));
  2597. } break;
  2598. case Variant::COLOR: {
  2599. item->set_cell_mode(1, TreeItem::CELL_MODE_CUSTOM);
  2600. item->set_editable(1, !read_only);
  2601. item->set_custom_draw(1, this, "_draw_transparency");
  2602. if (show_type_icons)
  2603. item->set_icon(0, get_icon("Color", "EditorIcons"));
  2604. } break;
  2605. case Variant::NODE_PATH: {
  2606. item->set_cell_mode(1, TreeItem::CELL_MODE_STRING);
  2607. item->set_editable(1, !read_only);
  2608. item->set_text(1, obj->get(p.name));
  2609. item->add_button(1, get_icon("CopyNodePath", "EditorIcons"));
  2610. } break;
  2611. case Variant::OBJECT: {
  2612. item->set_cell_mode(1, TreeItem::CELL_MODE_CUSTOM);
  2613. item->set_editable(1, !read_only);
  2614. String type;
  2615. if (p.hint == PROPERTY_HINT_RESOURCE_TYPE)
  2616. type = p.hint_string;
  2617. RES res = obj->get(p.name).operator RefPtr();
  2618. Ref<EncodedObjectAsID> encoded = obj->get(p.name); //for debugger and remote tools
  2619. if (encoded.is_valid()) {
  2620. item->set_text(1, "Object: " + itos(encoded->get_object_id()));
  2621. item->set_icon(1, Ref<Texture>());
  2622. item->set_custom_as_button(1, true);
  2623. item->set_editable(1, true);
  2624. } else if (obj->get(p.name).get_type() == Variant::NIL || res.is_null()) {
  2625. item->set_text(1, "<null>");
  2626. item->set_icon(1, Ref<Texture>());
  2627. item->set_custom_as_button(1, false);
  2628. } else if (res.is_valid()) {
  2629. item->set_custom_as_button(1, true);
  2630. if (res->is_class("Texture")) {
  2631. int tw = EditorSettings::get_singleton()->get("docks/property_editor/texture_preview_width");
  2632. Vector2 size(res->call("get_width"), res->call("get_height"));
  2633. if (size.width < size.height) {
  2634. tw = MAX((size.width / size.height) * tw, 1);
  2635. }
  2636. item->set_icon_max_width(1, tw);
  2637. item->set_icon(1, res);
  2638. item->set_text(1, "");
  2639. } else if (res->get_name() != "") {
  2640. item->set_text(1, res->get_name());
  2641. } else if (res->get_path() != "" && !res->get_path().begins_with("local://")) {
  2642. item->set_text(1, res->get_path().get_file());
  2643. } else {
  2644. item->set_text(1, "<" + res->get_class() + ">");
  2645. }
  2646. if (has_icon(res->get_class(), "EditorIcons")) {
  2647. type = res->get_class();
  2648. }
  2649. if (res.is_valid() && res->get_path().is_resource_file()) {
  2650. item->set_tooltip(1, res->get_path());
  2651. } else if (res.is_valid()) {
  2652. item->set_tooltip(1, res->get_name() + " (" + res->get_class() + ")");
  2653. }
  2654. if (res->is_class("Script")) {
  2655. item->set_text(1, res->get_path().get_file());
  2656. } else if (!res->is_class("Texture")) {
  2657. //texture already previews via itself
  2658. EditorResourcePreview::get_singleton()->queue_edited_resource_preview(res, this, "_resource_preview_done", item->get_instance_id());
  2659. }
  2660. }
  2661. if (type != String()) {
  2662. if (type.find(",") != -1)
  2663. type = type.get_slice(",", 0);
  2664. if (has_icon(type, "EditorIcons"))
  2665. item->set_icon(0, get_icon(type, "EditorIcons"));
  2666. else
  2667. item->set_icon(0, get_icon("Object", "EditorIcons"));
  2668. }
  2669. } break;
  2670. default: {};
  2671. }
  2672. if (keying) {
  2673. if (p.hint == PROPERTY_HINT_SPRITE_FRAME) {
  2674. item->add_button(1, get_icon("KeyNext", "EditorIcons"), 5);
  2675. } else {
  2676. item->add_button(1, get_icon("Key", "EditorIcons"), 2);
  2677. }
  2678. }
  2679. bool has_reload = false;
  2680. bool mbi = _might_be_in_instance();
  2681. if (mbi) {
  2682. Variant vorig;
  2683. Dictionary d = item->get_metadata(0);
  2684. int usage = d.has("usage") ? int(int(d["usage"]) & (PROPERTY_USAGE_STORE_IF_NONONE | PROPERTY_USAGE_STORE_IF_NONZERO)) : 0;
  2685. if (_get_instanced_node_original_property(p.name, vorig) || usage) {
  2686. Variant v = obj->get(p.name);
  2687. if (_is_property_different(v, vorig, usage)) {
  2688. item->add_button(1, get_icon("ReloadSmall", "EditorIcons"), 3);
  2689. has_reload = true;
  2690. }
  2691. }
  2692. }
  2693. if (obj->call("property_can_revert", p.name).operator bool()) {
  2694. item->add_button(1, get_icon("ReloadSmall", "EditorIcons"), 3);
  2695. has_reload = true;
  2696. }
  2697. if (!has_reload && !obj->get_script().is_null()) {
  2698. Ref<Script> scr = obj->get_script();
  2699. Variant orig_value;
  2700. if (scr->get_property_default_value(p.name, orig_value)) {
  2701. if (orig_value != obj->get(p.name)) {
  2702. item->add_button(1, get_icon("ReloadSmall", "EditorIcons"), 3);
  2703. has_reload = true;
  2704. }
  2705. }
  2706. }
  2707. if (mbi && !has_reload && item->get_cell_mode(1) == TreeItem::CELL_MODE_RANGE && item->get_text(1) == String()) {
  2708. item->add_button(1, get_icon("ReloadEmpty", "EditorIcons"), 3, true);
  2709. }
  2710. }
  2711. }
  2712. void PropertyEditor::_draw_transparency(Object *t, const Rect2 &p_rect) {
  2713. TreeItem *ti = Object::cast_to<TreeItem>(t);
  2714. if (!ti)
  2715. return;
  2716. Color color = obj->get(ti->get_metadata(1));
  2717. Ref<Texture> arrow = tree->get_icon("select_option");
  2718. // make a little space between consecutive color fields
  2719. Rect2 area = p_rect;
  2720. area.position.y += 1;
  2721. area.size.height -= 2;
  2722. area.size.width -= arrow->get_size().width + 5;
  2723. tree->draw_texture_rect(get_icon("GuiMiniCheckerboard", "EditorIcons"), area, true);
  2724. tree->draw_rect(area, color);
  2725. }
  2726. void PropertyEditor::_item_folded(Object *item_obj) {
  2727. if (updating_folding)
  2728. return;
  2729. TreeItem *item = Object::cast_to<TreeItem>(item_obj);
  2730. obj->editor_set_section_unfold(item->get_metadata(0), !item->is_collapsed());
  2731. }
  2732. void PropertyEditor::_item_selected() {
  2733. TreeItem *item = tree->get_selected();
  2734. ERR_FAIL_COND(!item);
  2735. selected_property = item->get_metadata(1);
  2736. }
  2737. void PropertyEditor::_item_rmb_edited() {
  2738. _custom_editor_request(true);
  2739. }
  2740. void PropertyEditor::_edit_set(const String &p_name, const Variant &p_value, bool p_refresh_all, const String &p_changed_field) {
  2741. if (autoclear) {
  2742. TreeItem *item = tree->get_selected();
  2743. if (item && item->get_cell_mode(0) == TreeItem::CELL_MODE_CHECK) {
  2744. item->set_checked(0, true);
  2745. }
  2746. }
  2747. if (!undo_redo || Object::cast_to<ArrayPropertyEdit>(obj)) { //kind of hacky
  2748. obj->set(p_name, p_value);
  2749. if (p_refresh_all)
  2750. _changed_callbacks(obj, "");
  2751. else
  2752. _changed_callbacks(obj, p_name);
  2753. emit_signal(_prop_edited, p_name);
  2754. } else if (Object::cast_to<MultiNodeEdit>(obj)) {
  2755. Object::cast_to<MultiNodeEdit>(obj)->set_property_field(p_name, p_value, p_changed_field);
  2756. _changed_callbacks(obj, p_name);
  2757. emit_signal(_prop_edited, p_name);
  2758. } else {
  2759. undo_redo->create_action(TTR("Set") + " " + p_name, UndoRedo::MERGE_ENDS);
  2760. undo_redo->add_do_property(obj, p_name, p_value);
  2761. undo_redo->add_undo_property(obj, p_name, obj->get(p_name));
  2762. if (p_refresh_all) {
  2763. undo_redo->add_do_method(this, "_changed_callback", obj, "");
  2764. undo_redo->add_undo_method(this, "_changed_callback", obj, "");
  2765. } else {
  2766. undo_redo->add_do_method(this, "_changed_callback", obj, p_name);
  2767. undo_redo->add_undo_method(this, "_changed_callback", obj, p_name);
  2768. }
  2769. Resource *r = Object::cast_to<Resource>(obj);
  2770. if (r) {
  2771. if (!r->is_edited() && String(p_name) != "resource/edited") {
  2772. undo_redo->add_do_method(r, "set_edited", true);
  2773. undo_redo->add_undo_method(r, "set_edited", false);
  2774. }
  2775. if (String(p_name) == "resource_local_to_scene") {
  2776. bool prev = obj->get(p_name);
  2777. bool next = p_value;
  2778. if (next) {
  2779. undo_redo->add_do_method(this, "setup_local_to_scene");
  2780. }
  2781. if (prev) {
  2782. undo_redo->add_undo_method(this, "setup_local_to_scene");
  2783. }
  2784. }
  2785. }
  2786. undo_redo->add_do_method(this, "emit_signal", _prop_edited, p_name);
  2787. undo_redo->add_undo_method(this, "emit_signal", _prop_edited, p_name);
  2788. undo_redo->commit_action();
  2789. }
  2790. }
  2791. void PropertyEditor::_item_edited() {
  2792. TreeItem *item = tree->get_edited();
  2793. if (!item)
  2794. return; //it all happened too fast..
  2795. Dictionary d = item->get_metadata(0);
  2796. String name = d["name"];
  2797. if (tree->get_edited_column() == 0) {
  2798. //property checked
  2799. if (autoclear) {
  2800. if (!item->is_checked(0)) {
  2801. obj->set(name, Variant());
  2802. update_property(name);
  2803. } else {
  2804. Variant::CallError ce;
  2805. obj->set(name, Variant::construct(Variant::Type(int(d["type"])), NULL, 0, ce));
  2806. }
  2807. } else {
  2808. emit_signal("property_toggled", name, item->is_checked(0));
  2809. }
  2810. return;
  2811. }
  2812. if (autoclear && item->get_cell_mode(0) == TreeItem::CELL_MODE_CHECK && item->get_cell_mode(1) != TreeItem::CELL_MODE_CUSTOM) {
  2813. item->set_checked(0, true);
  2814. }
  2815. int type = d["type"];
  2816. int hint = d["hint"];
  2817. int usage = d["usage"];
  2818. bool refresh_all = usage & PROPERTY_USAGE_UPDATE_ALL_IF_MODIFIED;
  2819. String hint_text = d["hint_text"];
  2820. switch (type) {
  2821. case Variant::NIL: {
  2822. } break;
  2823. case Variant::BOOL: {
  2824. item->set_tooltip(1, item->is_checked(1) ? "True" : "False");
  2825. _edit_set(name, item->is_checked(1), refresh_all);
  2826. } break;
  2827. case Variant::INT:
  2828. case Variant::REAL: {
  2829. if (hint == PROPERTY_HINT_LAYERS_2D_PHYSICS || hint == PROPERTY_HINT_LAYERS_2D_RENDER || hint == PROPERTY_HINT_LAYERS_3D_PHYSICS || hint == PROPERTY_HINT_LAYERS_3D_RENDER)
  2830. break;
  2831. if (hint == PROPERTY_HINT_EXP_EASING)
  2832. break;
  2833. if (hint == PROPERTY_HINT_FLAGS)
  2834. break;
  2835. if (type == Variant::INT)
  2836. _edit_set(name, int64_t(round(item->get_range(1))), refresh_all);
  2837. else
  2838. _edit_set(name, item->get_range(1), refresh_all);
  2839. } break;
  2840. case Variant::STRING: {
  2841. if (hint == PROPERTY_HINT_ENUM) {
  2842. int idx = item->get_range(1);
  2843. Vector<String> strings = hint_text.split(",");
  2844. String txt;
  2845. if (idx >= 0 && idx < strings.size()) {
  2846. txt = strings[idx];
  2847. }
  2848. _edit_set(name, txt, refresh_all);
  2849. } else {
  2850. _edit_set(name, item->get_text(1), refresh_all);
  2851. }
  2852. } break;
  2853. // math types
  2854. case Variant::VECTOR3: {
  2855. } break;
  2856. case Variant::PLANE: {
  2857. } break;
  2858. case Variant::QUAT: {
  2859. } break;
  2860. case Variant::RECT3: {
  2861. } break;
  2862. case Variant::BASIS: {
  2863. } break;
  2864. case Variant::TRANSFORM: {
  2865. } break;
  2866. case Variant::COLOR: {
  2867. } break;
  2868. case Variant::NODE_PATH: {
  2869. _edit_set(name, NodePath(item->get_text(1)), refresh_all);
  2870. } break;
  2871. case Variant::OBJECT: {
  2872. if (!item->is_custom_set_as_button(1))
  2873. break;
  2874. Ref<EncodedObjectAsID> encoded = obj->get(name); //for debugger and remote tools
  2875. if (encoded.is_valid()) {
  2876. emit_signal("object_id_selected", encoded->get_object_id());
  2877. }
  2878. RES res = obj->get(name);
  2879. if (res.is_valid()) {
  2880. emit_signal("resource_selected", res.get_ref_ptr(), name);
  2881. }
  2882. } break;
  2883. case Variant::DICTIONARY: {
  2884. } break;
  2885. // arrays
  2886. case Variant::POOL_BYTE_ARRAY: {
  2887. } break;
  2888. case Variant::POOL_INT_ARRAY: {
  2889. } break;
  2890. case Variant::POOL_REAL_ARRAY: {
  2891. } break;
  2892. case Variant::POOL_STRING_ARRAY: {
  2893. } break;
  2894. case Variant::POOL_VECTOR3_ARRAY: {
  2895. } break;
  2896. case Variant::POOL_COLOR_ARRAY: {
  2897. } break;
  2898. };
  2899. }
  2900. void PropertyEditor::_resource_edit_request() {
  2901. RES res = custom_editor->get_variant();
  2902. if (res.is_null())
  2903. return;
  2904. String name = custom_editor->get_name();
  2905. emit_signal("resource_selected", res.get_ref_ptr(), name);
  2906. }
  2907. void PropertyEditor::_custom_editor_edited() {
  2908. if (!obj)
  2909. return;
  2910. _edit_set(custom_editor->get_name(), custom_editor->get_variant());
  2911. }
  2912. void PropertyEditor::_custom_editor_edited_field(const String &p_field_name) {
  2913. ERR_FAIL_COND(p_field_name == "");
  2914. if (!obj)
  2915. return;
  2916. _edit_set(custom_editor->get_name(), custom_editor->get_variant(), false, p_field_name);
  2917. }
  2918. void PropertyEditor::_custom_editor_request(bool p_arrow) {
  2919. TreeItem *item = tree->get_edited();
  2920. ERR_FAIL_COND(!item);
  2921. Dictionary d = item->get_metadata(0);
  2922. String name = d["name"];
  2923. Variant::Type type = Variant::NIL;
  2924. if (d.has("type"))
  2925. type = (Variant::Type)((int)(d["type"]));
  2926. Variant v = obj->get(name);
  2927. int hint = d.has("hint") ? d["hint"].operator int() : -1;
  2928. String hint_text = d.has("hint_text") ? d["hint_text"] : "";
  2929. Rect2 where = tree->get_custom_popup_rect();
  2930. custom_editor->set_position(where.position);
  2931. if (custom_editor->edit(obj, name, type, v, hint, hint_text)) {
  2932. custom_editor->popup();
  2933. }
  2934. }
  2935. void PropertyEditor::edit(Object *p_object) {
  2936. if (obj == p_object)
  2937. return;
  2938. if (obj) {
  2939. obj->remove_change_receptor(this);
  2940. }
  2941. obj = p_object;
  2942. evaluator->edit(p_object);
  2943. update_tree();
  2944. if (obj) {
  2945. obj->add_change_receptor(this);
  2946. }
  2947. }
  2948. void PropertyEditor::_set_range_def(Object *p_item, String prop, float p_frame) {
  2949. TreeItem *ti = Object::cast_to<TreeItem>(p_item);
  2950. ERR_FAIL_COND(!ti);
  2951. ti->call_deferred("set_range", 1, p_frame);
  2952. obj->call_deferred("set", prop, p_frame);
  2953. }
  2954. void PropertyEditor::_edit_button(Object *p_item, int p_column, int p_button) {
  2955. TreeItem *ti = Object::cast_to<TreeItem>(p_item);
  2956. ERR_FAIL_COND(!ti);
  2957. Dictionary d = ti->get_metadata(0);
  2958. if (p_button == 2) {
  2959. if (!d.has("name"))
  2960. return;
  2961. String prop = d["name"];
  2962. emit_signal("property_keyed", prop, obj->get(prop), false);
  2963. } else if (p_button == 5) {
  2964. print_line("PB5");
  2965. if (!d.has("name"))
  2966. return;
  2967. String prop = d["name"];
  2968. emit_signal("property_keyed", prop, obj->get(prop), true);
  2969. call_deferred("_set_range_def", ti, prop, ti->get_range(p_column) + 1.0);
  2970. } else if (p_button == 3) {
  2971. if (!d.has("name"))
  2972. return;
  2973. String prop = d["name"];
  2974. Variant vorig;
  2975. if (_might_be_in_instance() && _get_instanced_node_original_property(prop, vorig)) {
  2976. _edit_set(prop, vorig);
  2977. return;
  2978. }
  2979. if (obj->call("property_can_revert", prop).operator bool()) {
  2980. Variant rev = obj->call("property_get_revert", prop);
  2981. _edit_set(prop, rev);
  2982. }
  2983. if (!obj->get_script().is_null()) {
  2984. Ref<Script> scr = obj->get_script();
  2985. Variant orig_value;
  2986. if (scr->get_property_default_value(prop, orig_value)) {
  2987. _edit_set(prop, orig_value);
  2988. }
  2989. }
  2990. } else {
  2991. Dictionary d = ti->get_metadata(0);
  2992. if (!d.has("type"))
  2993. return;
  2994. if (!d.has("hint"))
  2995. return;
  2996. if (!d.has("name"))
  2997. return;
  2998. if (!d.has("hint_text"))
  2999. return;
  3000. int t = d["type"];
  3001. int h = d["hint"];
  3002. String n = d["name"];
  3003. String ht = d["hint_text"];
  3004. if (t == Variant::NODE_PATH) {
  3005. Variant v = obj->get(n);
  3006. custom_editor->edit(obj, n, (Variant::Type)t, v, h, ht);
  3007. Rect2 where = tree->get_item_rect(ti, 1);
  3008. where.position -= tree->get_scroll();
  3009. where.position += tree->get_global_position();
  3010. custom_editor->set_position(where.position);
  3011. custom_editor->popup();
  3012. } else if (t == Variant::STRING) {
  3013. Variant v = obj->get(n);
  3014. custom_editor->edit(obj, n, (Variant::Type)t, v, h, ht);
  3015. if (h == PROPERTY_HINT_FILE || h == PROPERTY_HINT_DIR || h == PROPERTY_HINT_GLOBAL_DIR || h == PROPERTY_HINT_GLOBAL_FILE) {
  3016. Rect2 where = tree->get_item_rect(ti, 1);
  3017. where.position -= tree->get_scroll();
  3018. where.position += tree->get_global_position();
  3019. custom_editor->set_position(where.position);
  3020. custom_editor->popup();
  3021. } else {
  3022. custom_editor->popup_centered_ratio();
  3023. }
  3024. } else if (t == Variant::OBJECT) {
  3025. RES r = obj->get(n);
  3026. if (r.is_valid()) {
  3027. emit_signal("resource_selected", r, n);
  3028. }
  3029. } else if (t == Variant::INT && h == PROPERTY_HINT_OBJECT_ID) {
  3030. emit_signal("object_id_selected", obj->get(n));
  3031. } else if (t == Variant::ARRAY || t == Variant::POOL_INT_ARRAY || t == Variant::POOL_REAL_ARRAY || t == Variant::POOL_STRING_ARRAY || t == Variant::POOL_VECTOR2_ARRAY || t == Variant::POOL_VECTOR3_ARRAY || t == Variant::POOL_COLOR_ARRAY || t == Variant::POOL_BYTE_ARRAY) {
  3032. Variant v = obj->get(n);
  3033. if (v.get_type() != t) {
  3034. Variant::CallError ce;
  3035. v = Variant::construct(Variant::Type(t), NULL, 0, ce);
  3036. }
  3037. Ref<ArrayPropertyEdit> ape = memnew(ArrayPropertyEdit);
  3038. ape->edit(obj, n, ht, Variant::Type(t));
  3039. EditorNode::get_singleton()->push_item(ape.ptr());
  3040. }
  3041. }
  3042. }
  3043. void PropertyEditor::_node_removed(Node *p_node) {
  3044. if (p_node == obj) {
  3045. edit(NULL);
  3046. }
  3047. }
  3048. void PropertyEditor::set_keying(bool p_active) {
  3049. if (keying == p_active)
  3050. return;
  3051. keying = p_active;
  3052. update_tree();
  3053. }
  3054. void PropertyEditor::_draw_flags(Object *p_object, const Rect2 &p_rect) {
  3055. TreeItem *ti = Object::cast_to<TreeItem>(p_object);
  3056. if (!ti)
  3057. return;
  3058. Dictionary d = ti->get_metadata(0);
  3059. if (!d.has("name"))
  3060. return;
  3061. uint32_t f = obj->get(d["name"]);
  3062. int bsize = (p_rect.size.height * 80 / 100) / 2;
  3063. int h = bsize * 2 + 1;
  3064. int vofs = (p_rect.size.height - h) / 2;
  3065. for (int i = 0; i < 2; i++) {
  3066. Point2 ofs(4, vofs);
  3067. if (i == 1)
  3068. ofs.y += bsize + 1;
  3069. ofs += p_rect.position;
  3070. for (int j = 0; j < 10; j++) {
  3071. Point2 o = ofs + Point2(j * (bsize + 1), 0);
  3072. if (j >= 5)
  3073. o.x += 1;
  3074. uint32_t idx = i * 10 + j;
  3075. bool on = f & (1 << idx);
  3076. tree->draw_rect(Rect2(o, Size2(bsize, bsize)), Color(0, 0, 0, on ? 0.8 : 0.3));
  3077. }
  3078. }
  3079. }
  3080. void PropertyEditor::_filter_changed(const String &p_text) {
  3081. update_tree();
  3082. }
  3083. void PropertyEditor::_resource_preview_done(const String &p_path, const Ref<Texture> &p_preview, Variant p_ud) {
  3084. if (p_preview.is_null())
  3085. return; //don't bother with empty preview
  3086. ObjectID id = p_ud;
  3087. Object *obj = ObjectDB::get_instance(id);
  3088. if (!obj)
  3089. return;
  3090. TreeItem *ti = Object::cast_to<TreeItem>(obj);
  3091. ERR_FAIL_COND(!ti);
  3092. int tw = EditorSettings::get_singleton()->get("docks/property_editor/texture_preview_width");
  3093. ti->set_icon(1, p_preview); //should be scaled I think?
  3094. ti->set_icon_max_width(1, tw);
  3095. ti->set_text(1, "");
  3096. }
  3097. void PropertyEditor::_bind_methods() {
  3098. ClassDB::bind_method("_item_edited", &PropertyEditor::_item_edited);
  3099. ClassDB::bind_method("_item_selected", &PropertyEditor::_item_selected);
  3100. ClassDB::bind_method("_item_rmb_edited", &PropertyEditor::_item_rmb_edited);
  3101. ClassDB::bind_method("_item_folded", &PropertyEditor::_item_folded);
  3102. ClassDB::bind_method("_custom_editor_request", &PropertyEditor::_custom_editor_request);
  3103. ClassDB::bind_method("_custom_editor_edited", &PropertyEditor::_custom_editor_edited);
  3104. ClassDB::bind_method("_custom_editor_edited_field", &PropertyEditor::_custom_editor_edited_field, DEFVAL(""));
  3105. ClassDB::bind_method("_resource_edit_request", &PropertyEditor::_resource_edit_request);
  3106. ClassDB::bind_method("_node_removed", &PropertyEditor::_node_removed);
  3107. ClassDB::bind_method("_edit_button", &PropertyEditor::_edit_button);
  3108. ClassDB::bind_method("_changed_callback", &PropertyEditor::_changed_callbacks);
  3109. ClassDB::bind_method("_draw_flags", &PropertyEditor::_draw_flags);
  3110. ClassDB::bind_method("_set_range_def", &PropertyEditor::_set_range_def);
  3111. ClassDB::bind_method("_filter_changed", &PropertyEditor::_filter_changed);
  3112. ClassDB::bind_method("update_tree", &PropertyEditor::update_tree);
  3113. ClassDB::bind_method("_resource_preview_done", &PropertyEditor::_resource_preview_done);
  3114. ClassDB::bind_method("refresh", &PropertyEditor::refresh);
  3115. ClassDB::bind_method("_draw_transparency", &PropertyEditor::_draw_transparency);
  3116. ClassDB::bind_method(D_METHOD("get_drag_data_fw"), &PropertyEditor::get_drag_data_fw);
  3117. ClassDB::bind_method(D_METHOD("can_drop_data_fw"), &PropertyEditor::can_drop_data_fw);
  3118. ClassDB::bind_method(D_METHOD("drop_data_fw"), &PropertyEditor::drop_data_fw);
  3119. ADD_SIGNAL(MethodInfo("property_toggled", PropertyInfo(Variant::STRING, "property"), PropertyInfo(Variant::BOOL, "value")));
  3120. ADD_SIGNAL(MethodInfo("resource_selected", PropertyInfo(Variant::OBJECT, "res"), PropertyInfo(Variant::STRING, "prop")));
  3121. ADD_SIGNAL(MethodInfo("object_id_selected", PropertyInfo(Variant::INT, "id")));
  3122. ADD_SIGNAL(MethodInfo("property_keyed", PropertyInfo(Variant::STRING, "property")));
  3123. ADD_SIGNAL(MethodInfo("property_edited", PropertyInfo(Variant::STRING, "property")));
  3124. }
  3125. Tree *PropertyEditor::get_scene_tree() {
  3126. return tree;
  3127. }
  3128. Label *PropertyEditor::get_top_label() {
  3129. return top_label;
  3130. }
  3131. void PropertyEditor::hide_top_label() {
  3132. top_label->hide();
  3133. tree->set_begin(Point2(0, 0));
  3134. }
  3135. String PropertyEditor::get_selected_path() const {
  3136. TreeItem *ti = tree->get_selected();
  3137. if (!ti)
  3138. return "";
  3139. Dictionary d = ti->get_metadata(0);
  3140. if (d.has("name"))
  3141. return d["name"];
  3142. else
  3143. return "";
  3144. }
  3145. bool PropertyEditor::is_capitalize_paths_enabled() const {
  3146. return capitalize_paths;
  3147. }
  3148. void PropertyEditor::set_enable_capitalize_paths(bool p_capitalize) {
  3149. capitalize_paths = p_capitalize;
  3150. update_tree_pending = true;
  3151. }
  3152. void PropertyEditor::set_autoclear(bool p_enable) {
  3153. autoclear = p_enable;
  3154. }
  3155. void PropertyEditor::set_show_categories(bool p_show) {
  3156. show_categories = p_show;
  3157. update_tree();
  3158. }
  3159. void PropertyEditor::set_use_filter(bool p_use) {
  3160. if (p_use == use_filter)
  3161. return;
  3162. use_filter = p_use;
  3163. update_tree();
  3164. }
  3165. void PropertyEditor::register_text_enter(Node *p_line_edit) {
  3166. ERR_FAIL_NULL(p_line_edit);
  3167. search_box = Object::cast_to<LineEdit>(p_line_edit);
  3168. if (search_box)
  3169. search_box->connect("text_changed", this, "_filter_changed");
  3170. }
  3171. void PropertyEditor::set_property_selectable(bool p_selectable) {
  3172. property_selectable = p_selectable;
  3173. update_tree();
  3174. }
  3175. void PropertyEditor::set_subsection_selectable(bool p_selectable) {
  3176. if (p_selectable == subsection_selectable)
  3177. return;
  3178. subsection_selectable = p_selectable;
  3179. update_tree();
  3180. }
  3181. void PropertyEditor::set_use_folding(bool p_enable) {
  3182. use_folding = p_enable;
  3183. tree->set_hide_folding(false);
  3184. }
  3185. PropertyEditor::PropertyEditor() {
  3186. _prop_edited = "property_edited";
  3187. hide_script = true;
  3188. use_folding = false;
  3189. undo_redo = NULL;
  3190. obj = NULL;
  3191. search_box = NULL;
  3192. changing = false;
  3193. update_tree_pending = false;
  3194. top_label = memnew(Label);
  3195. top_label->set_text(TTR("Properties:"));
  3196. top_label->set_anchor(MARGIN_RIGHT, ANCHOR_END);
  3197. top_label->set_begin(Point2(10, 0));
  3198. top_label->set_end(Point2(0, 12));
  3199. add_child(top_label);
  3200. tree = memnew(Tree);
  3201. tree->set_anchor(MARGIN_RIGHT, ANCHOR_END);
  3202. tree->set_anchor(MARGIN_BOTTOM, ANCHOR_END);
  3203. tree->set_begin(Point2(0, 19));
  3204. tree->set_end(Point2(0, 0));
  3205. tree->set_columns(2);
  3206. tree->set_column_expand(0, true);
  3207. tree->set_column_min_width(0, 30);
  3208. tree->set_column_expand(1, true);
  3209. tree->set_column_min_width(1, 18);
  3210. add_child(tree);
  3211. tree->connect("item_edited", this, "_item_edited", varray(), CONNECT_DEFERRED);
  3212. tree->connect("item_rmb_edited", this, "_item_rmb_edited");
  3213. tree->connect("cell_selected", this, "_item_selected");
  3214. tree->connect("item_collapsed", this, "_item_folded");
  3215. tree->set_drag_forwarding(this);
  3216. set_physics_process(true);
  3217. custom_editor = memnew(CustomPropertyEditor);
  3218. add_child(custom_editor);
  3219. tree->connect("custom_popup_edited", this, "_custom_editor_request");
  3220. tree->connect("button_pressed", this, "_edit_button");
  3221. custom_editor->connect("variant_changed", this, "_custom_editor_edited");
  3222. custom_editor->connect("variant_field_changed", this, "_custom_editor_edited_field");
  3223. custom_editor->connect("resource_edit_request", this, "_resource_edit_request", make_binds(), CONNECT_DEFERRED);
  3224. tree->set_hide_folding(true);
  3225. evaluator = memnew(PropertyValueEvaluator);
  3226. tree->set_value_evaluator(evaluator);
  3227. custom_editor->set_value_evaluator(evaluator);
  3228. capitalize_paths = true;
  3229. autoclear = false;
  3230. tree->set_column_titles_visible(false);
  3231. tree->add_constant_override("button_margin", 0);
  3232. keying = false;
  3233. read_only = false;
  3234. show_categories = false;
  3235. refresh_countdown = 0;
  3236. use_doc_hints = false;
  3237. updating_folding = true;
  3238. use_filter = false;
  3239. subsection_selectable = false;
  3240. property_selectable = false;
  3241. show_type_icons = EDITOR_DEF("interface/editor/show_type_icons", false);
  3242. }
  3243. PropertyEditor::~PropertyEditor() {
  3244. memdelete(evaluator);
  3245. }
  3246. /////////////////////////////
  3247. class SectionedPropertyEditorFilter : public Object {
  3248. GDCLASS(SectionedPropertyEditorFilter, Object);
  3249. Object *edited;
  3250. String section;
  3251. bool allow_sub;
  3252. bool _set(const StringName &p_name, const Variant &p_value) {
  3253. if (!edited)
  3254. return false;
  3255. String name = p_name;
  3256. if (section != "") {
  3257. name = section + "/" + name;
  3258. }
  3259. bool valid;
  3260. edited->set(name, p_value, &valid);
  3261. return valid;
  3262. }
  3263. bool _get(const StringName &p_name, Variant &r_ret) const {
  3264. if (!edited)
  3265. return false;
  3266. String name = p_name;
  3267. if (section != "") {
  3268. name = section + "/" + name;
  3269. }
  3270. bool valid = false;
  3271. r_ret = edited->get(name, &valid);
  3272. return valid;
  3273. }
  3274. void _get_property_list(List<PropertyInfo> *p_list) const {
  3275. if (!edited)
  3276. return;
  3277. List<PropertyInfo> pinfo;
  3278. edited->get_property_list(&pinfo);
  3279. for (List<PropertyInfo>::Element *E = pinfo.front(); E; E = E->next()) {
  3280. PropertyInfo pi = E->get();
  3281. int sp = pi.name.find("/");
  3282. if (pi.name == "resource_path" || pi.name == "resource_name" || pi.name.begins_with("script/")) //skip resource stuff
  3283. continue;
  3284. if (sp == -1) {
  3285. pi.name = "global/" + pi.name;
  3286. }
  3287. if (pi.name.begins_with(section + "/")) {
  3288. pi.name = pi.name.replace_first(section + "/", "");
  3289. if (!allow_sub && pi.name.find("/") != -1)
  3290. continue;
  3291. p_list->push_back(pi);
  3292. }
  3293. }
  3294. }
  3295. bool property_can_revert(const String &p_name) {
  3296. return edited->call("property_can_revert", section + "/" + p_name);
  3297. }
  3298. Variant property_get_revert(const String &p_name) {
  3299. return edited->call("property_get_revert", section + "/" + p_name);
  3300. }
  3301. protected:
  3302. static void _bind_methods() {
  3303. ClassDB::bind_method("property_can_revert", &SectionedPropertyEditorFilter::property_can_revert);
  3304. ClassDB::bind_method("property_get_revert", &SectionedPropertyEditorFilter::property_get_revert);
  3305. }
  3306. public:
  3307. void set_section(const String &p_section, bool p_allow_sub) {
  3308. section = p_section;
  3309. allow_sub = p_allow_sub;
  3310. _change_notify();
  3311. }
  3312. void set_edited(Object *p_edited) {
  3313. edited = p_edited;
  3314. _change_notify();
  3315. }
  3316. SectionedPropertyEditorFilter() {
  3317. edited = NULL;
  3318. }
  3319. };
  3320. void SectionedPropertyEditor::_bind_methods() {
  3321. ClassDB::bind_method("_section_selected", &SectionedPropertyEditor::_section_selected);
  3322. ClassDB::bind_method("_search_changed", &SectionedPropertyEditor::_search_changed);
  3323. ClassDB::bind_method("update_category_list", &SectionedPropertyEditor::update_category_list);
  3324. }
  3325. void SectionedPropertyEditor::_section_selected() {
  3326. if (!sections->get_selected())
  3327. return;
  3328. filter->set_section(sections->get_selected()->get_metadata(0), sections->get_selected()->get_children() == NULL);
  3329. }
  3330. void SectionedPropertyEditor::set_current_section(const String &p_section) {
  3331. if (section_map.has(p_section)) {
  3332. section_map[p_section]->select(0);
  3333. }
  3334. }
  3335. String SectionedPropertyEditor::get_current_section() const {
  3336. if (sections->get_selected())
  3337. return sections->get_selected()->get_metadata(0);
  3338. else
  3339. return "";
  3340. }
  3341. String SectionedPropertyEditor::get_full_item_path(const String &p_item) {
  3342. String base = get_current_section();
  3343. if (base != "")
  3344. return base + "/" + p_item;
  3345. else
  3346. return p_item;
  3347. }
  3348. void SectionedPropertyEditor::edit(Object *p_object) {
  3349. if (!p_object) {
  3350. obj = -1;
  3351. sections->clear();
  3352. filter->set_edited(NULL);
  3353. editor->edit(NULL);
  3354. return;
  3355. }
  3356. ObjectID id = p_object->get_instance_id();
  3357. if (obj != id) {
  3358. obj = id;
  3359. update_category_list();
  3360. filter->set_edited(p_object);
  3361. editor->edit(filter);
  3362. if (sections->get_root()->get_children()) {
  3363. sections->get_root()->get_children()->select(0);
  3364. }
  3365. } else {
  3366. update_category_list();
  3367. }
  3368. }
  3369. void SectionedPropertyEditor::update_category_list() {
  3370. String selected_category = get_current_section();
  3371. sections->clear();
  3372. Object *o = ObjectDB::get_instance(obj);
  3373. if (!o)
  3374. return;
  3375. List<PropertyInfo> pinfo;
  3376. o->get_property_list(&pinfo);
  3377. section_map.clear();
  3378. TreeItem *root = sections->create_item();
  3379. section_map[""] = root;
  3380. for (List<PropertyInfo>::Element *E = pinfo.front(); E; E = E->next()) {
  3381. PropertyInfo pi = E->get();
  3382. if (pi.usage & PROPERTY_USAGE_CATEGORY)
  3383. continue;
  3384. else if (!(pi.usage & PROPERTY_USAGE_EDITOR))
  3385. continue;
  3386. if (pi.name.find(":") != -1 || pi.name == "script" || pi.name == "resource_name" || pi.name == "resource_path")
  3387. continue;
  3388. if (search_box && search_box->get_text() != String() && pi.name.findn(search_box->get_text()) == -1)
  3389. continue;
  3390. int sp = pi.name.find("/");
  3391. if (sp == -1)
  3392. pi.name = "Global/" + pi.name;
  3393. Vector<String> sectionarr = pi.name.split("/");
  3394. String metasection;
  3395. int sc = MIN(2, sectionarr.size() - 1);
  3396. for (int i = 0; i < sc; i++) {
  3397. TreeItem *parent = section_map[metasection];
  3398. if (i > 0) {
  3399. metasection += "/" + sectionarr[i];
  3400. } else {
  3401. metasection = sectionarr[i];
  3402. }
  3403. if (!section_map.has(metasection)) {
  3404. TreeItem *ms = sections->create_item(parent);
  3405. section_map[metasection] = ms;
  3406. ms->set_text(0, sectionarr[i].capitalize());
  3407. ms->set_metadata(0, metasection);
  3408. ms->set_selectable(0, false);
  3409. }
  3410. if (i == sc - 1) {
  3411. //if it has children, make selectable
  3412. section_map[metasection]->set_selectable(0, true);
  3413. }
  3414. }
  3415. }
  3416. if (section_map.has(selected_category)) {
  3417. section_map[selected_category]->select(0);
  3418. }
  3419. editor->update_tree();
  3420. }
  3421. void SectionedPropertyEditor::register_search_box(LineEdit *p_box) {
  3422. search_box = p_box;
  3423. editor->register_text_enter(p_box);
  3424. search_box->connect("text_changed", this, "_search_changed");
  3425. }
  3426. void SectionedPropertyEditor::_search_changed(const String &p_what) {
  3427. update_category_list();
  3428. }
  3429. PropertyEditor *SectionedPropertyEditor::get_property_editor() {
  3430. return editor;
  3431. }
  3432. SectionedPropertyEditor::SectionedPropertyEditor() {
  3433. obj = -1;
  3434. search_box = NULL;
  3435. VBoxContainer *left_vb = memnew(VBoxContainer);
  3436. left_vb->set_custom_minimum_size(Size2(160, 0) * EDSCALE);
  3437. add_child(left_vb);
  3438. sections = memnew(Tree);
  3439. sections->set_v_size_flags(SIZE_EXPAND_FILL);
  3440. sections->set_hide_root(true);
  3441. left_vb->add_margin_child(TTR("Sections:"), sections, true);
  3442. VBoxContainer *right_vb = memnew(VBoxContainer);
  3443. right_vb->set_h_size_flags(SIZE_EXPAND_FILL);
  3444. add_child(right_vb);
  3445. filter = memnew(SectionedPropertyEditorFilter);
  3446. editor = memnew(PropertyEditor);
  3447. editor->set_v_size_flags(SIZE_EXPAND_FILL);
  3448. right_vb->add_margin_child(TTR("Properties:"), editor, true);
  3449. editor->get_scene_tree()->set_column_titles_visible(false);
  3450. editor->hide_top_label();
  3451. sections->connect("cell_selected", this, "_section_selected");
  3452. }
  3453. SectionedPropertyEditor::~SectionedPropertyEditor() {
  3454. memdelete(filter);
  3455. }
  3456. double PropertyValueEvaluator::eval(const String &p_text) {
  3457. if (!obj || !script_language)
  3458. return _default_eval(p_text);
  3459. Ref<Script> script = Ref<Script>(script_language->create_script());
  3460. script->set_source_code(_build_script(p_text));
  3461. Error err = script->reload();
  3462. if (err) {
  3463. print_line("[PropertyValueEvaluator] Error loading script for expression: " + p_text);
  3464. return _default_eval(p_text);
  3465. }
  3466. Object dummy;
  3467. ScriptInstance *script_instance = script->instance_create(&dummy);
  3468. if (!script_instance)
  3469. return _default_eval(p_text);
  3470. Variant::CallError call_err;
  3471. Variant arg = obj;
  3472. const Variant *args[] = { &arg };
  3473. double result = script_instance->call("eval", args, 1, call_err);
  3474. if (call_err.error == Variant::CallError::CALL_OK) {
  3475. return result;
  3476. }
  3477. print_line("[PropertyValueEvaluator]: Error eval! Error code: " + itos(call_err.error));
  3478. return _default_eval(p_text);
  3479. }
  3480. void PropertyValueEvaluator::edit(Object *p_obj) {
  3481. obj = p_obj;
  3482. }
  3483. String PropertyValueEvaluator::_build_script(const String &p_text) {
  3484. String script_text = "tool\nextends Object\nfunc eval(s):\n\tself = s\n\treturn " + p_text.strip_edges() + "\n";
  3485. return script_text;
  3486. }
  3487. PropertyValueEvaluator::PropertyValueEvaluator() {
  3488. script_language = NULL;
  3489. for (int i = 0; i < ScriptServer::get_language_count(); i++) {
  3490. if (ScriptServer::get_language(i)->get_name() == "GDScript") {
  3491. script_language = ScriptServer::get_language(i);
  3492. }
  3493. }
  3494. }
  3495. PropertyValueEvaluator::~PropertyValueEvaluator() {
  3496. }