editor_inspector.cpp 160 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721
  1. /**************************************************************************/
  2. /* editor_inspector.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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 "editor_inspector.h"
  31. #include "editor_inspector.compat.inc"
  32. #include "core/os/keyboard.h"
  33. #include "editor/add_metadata_dialog.h"
  34. #include "editor/doc_tools.h"
  35. #include "editor/editor_feature_profile.h"
  36. #include "editor/editor_main_screen.h"
  37. #include "editor/editor_node.h"
  38. #include "editor/editor_property_name_processor.h"
  39. #include "editor/editor_settings.h"
  40. #include "editor/editor_string_names.h"
  41. #include "editor/editor_undo_redo_manager.h"
  42. #include "editor/gui/editor_validation_panel.h"
  43. #include "editor/inspector_dock.h"
  44. #include "editor/multi_node_edit.h"
  45. #include "editor/plugins/script_editor_plugin.h"
  46. #include "editor/themes/editor_scale.h"
  47. #include "editor/themes/editor_theme_manager.h"
  48. #include "scene/gui/margin_container.h"
  49. #include "scene/gui/separator.h"
  50. #include "scene/gui/spin_box.h"
  51. #include "scene/gui/texture_rect.h"
  52. #include "scene/property_utils.h"
  53. #include "scene/resources/packed_scene.h"
  54. #include "scene/resources/style_box_flat.h"
  55. #include "scene/scene_string_names.h"
  56. bool EditorInspector::_property_path_matches(const String &p_property_path, const String &p_filter, EditorPropertyNameProcessor::Style p_style) {
  57. if (p_property_path.containsn(p_filter)) {
  58. return true;
  59. }
  60. const Vector<String> prop_sections = p_property_path.split("/");
  61. for (int i = 0; i < prop_sections.size(); i++) {
  62. if (p_filter.is_subsequence_ofn(EditorPropertyNameProcessor::get_singleton()->process_name(prop_sections[i], p_style, p_property_path))) {
  63. return true;
  64. }
  65. }
  66. return false;
  67. }
  68. String EditorProperty::get_tooltip_string(const String &p_string) const {
  69. // Trim to 100 characters to prevent the tooltip from being too long.
  70. constexpr int TOOLTIP_MAX_LENGTH = 100;
  71. return p_string.left(TOOLTIP_MAX_LENGTH).strip_edges() + String((p_string.length() > TOOLTIP_MAX_LENGTH) ? "..." : "");
  72. }
  73. Size2 EditorProperty::get_minimum_size() const {
  74. Size2 ms;
  75. Ref<Font> font = get_theme_font(SceneStringName(font), SNAME("Tree"));
  76. int font_size = get_theme_font_size(SceneStringName(font_size), SNAME("Tree"));
  77. ms.height = label.is_empty() ? 0 : font->get_height(font_size) + 4 * EDSCALE;
  78. for (int i = 0; i < get_child_count(); i++) {
  79. Control *c = as_sortable_control(get_child(i));
  80. if (!c) {
  81. continue;
  82. }
  83. if (c == bottom_editor) {
  84. continue;
  85. }
  86. Size2 minsize = c->get_combined_minimum_size();
  87. ms = ms.max(minsize);
  88. }
  89. if (keying) {
  90. Ref<Texture2D> key = get_editor_theme_icon(SNAME("Key"));
  91. ms.width += key->get_width() + get_theme_constant(SNAME("h_separation"), SNAME("Tree"));
  92. }
  93. if (deletable) {
  94. Ref<Texture2D> key = get_editor_theme_icon(SNAME("Close"));
  95. ms.width += key->get_width() + get_theme_constant(SNAME("h_separation"), SNAME("Tree"));
  96. }
  97. if (checkable) {
  98. Ref<Texture2D> check = get_theme_icon(SNAME("checked"), SNAME("CheckBox"));
  99. ms.width += check->get_width() + get_theme_constant(SNAME("h_separation"), SNAME("Tree"));
  100. }
  101. if (bottom_editor != nullptr && bottom_editor->is_visible()) {
  102. ms.height += label.is_empty() ? 0 : get_theme_constant(SNAME("v_separation"));
  103. Size2 bems = bottom_editor->get_combined_minimum_size();
  104. //bems.width += get_constant("item_margin", "Tree");
  105. ms.height += bems.height;
  106. ms.width = MAX(ms.width, bems.width);
  107. }
  108. return ms;
  109. }
  110. void EditorProperty::emit_changed(const StringName &p_property, const Variant &p_value, const StringName &p_field, bool p_changing) {
  111. Variant args[4] = { p_property, p_value, p_field, p_changing };
  112. const Variant *argptrs[4] = { &args[0], &args[1], &args[2], &args[3] };
  113. cache[p_property] = p_value;
  114. emit_signalp(SNAME("property_changed"), (const Variant **)argptrs, 4);
  115. }
  116. void EditorProperty::_notification(int p_what) {
  117. switch (p_what) {
  118. case NOTIFICATION_SORT_CHILDREN: {
  119. Size2 size = get_size();
  120. Rect2 rect;
  121. Rect2 bottom_rect;
  122. right_child_rect = Rect2();
  123. bottom_child_rect = Rect2();
  124. {
  125. int child_room = size.width * (1.0 - split_ratio);
  126. Ref<Font> font = get_theme_font(SceneStringName(font), SNAME("Tree"));
  127. int font_size = get_theme_font_size(SceneStringName(font_size), SNAME("Tree"));
  128. int height = label.is_empty() ? 0 : font->get_height(font_size) + 4 * EDSCALE;
  129. bool no_children = true;
  130. //compute room needed
  131. for (int i = 0; i < get_child_count(); i++) {
  132. Control *c = as_sortable_control(get_child(i));
  133. if (!c) {
  134. continue;
  135. }
  136. if (c == bottom_editor) {
  137. continue;
  138. }
  139. Size2 minsize = c->get_combined_minimum_size();
  140. child_room = MAX(child_room, minsize.width);
  141. height = MAX(height, minsize.height);
  142. no_children = false;
  143. }
  144. if (no_children) {
  145. text_size = size.width;
  146. rect = Rect2(size.width - 1, 0, 1, height);
  147. } else {
  148. text_size = MAX(0, size.width - (child_room + 4 * EDSCALE));
  149. if (is_layout_rtl()) {
  150. rect = Rect2(1, 0, child_room, height);
  151. } else {
  152. rect = Rect2(size.width - child_room, 0, child_room, height);
  153. }
  154. }
  155. if (bottom_editor) {
  156. int v_offset = label.is_empty() ? 0 : get_theme_constant(SNAME("v_separation"));
  157. bottom_rect = Rect2(0, rect.size.height + v_offset, size.width, bottom_editor->get_combined_minimum_size().height);
  158. }
  159. if (keying) {
  160. Ref<Texture2D> key;
  161. if (use_keying_next()) {
  162. key = get_editor_theme_icon(SNAME("KeyNext"));
  163. } else {
  164. key = get_editor_theme_icon(SNAME("Key"));
  165. }
  166. rect.size.x -= key->get_width() + get_theme_constant(SNAME("h_separation"), SNAME("Tree"));
  167. if (is_layout_rtl()) {
  168. rect.position.x += key->get_width() + get_theme_constant(SNAME("h_separation"), SNAME("Tree"));
  169. }
  170. if (no_children) {
  171. text_size -= key->get_width() + 4 * EDSCALE;
  172. }
  173. }
  174. if (deletable) {
  175. Ref<Texture2D> close;
  176. close = get_editor_theme_icon(SNAME("Close"));
  177. rect.size.x -= close->get_width() + get_theme_constant(SNAME("h_separation"), SNAME("Tree"));
  178. if (is_layout_rtl()) {
  179. rect.position.x += close->get_width() + get_theme_constant(SNAME("h_separation"), SNAME("Tree"));
  180. }
  181. if (no_children) {
  182. text_size -= close->get_width() + 4 * EDSCALE;
  183. }
  184. }
  185. // Account for the space needed on the outer side
  186. // when any of the icons are visible.
  187. if (keying || deletable) {
  188. int separation = get_theme_constant(SNAME("h_separation"), SNAME("Tree"));
  189. rect.size.x -= separation;
  190. if (is_layout_rtl()) {
  191. rect.position.x += separation;
  192. }
  193. }
  194. }
  195. //set children
  196. for (int i = 0; i < get_child_count(); i++) {
  197. Control *c = as_sortable_control(get_child(i));
  198. if (!c) {
  199. continue;
  200. }
  201. if (c == bottom_editor) {
  202. continue;
  203. }
  204. fit_child_in_rect(c, rect);
  205. right_child_rect = rect;
  206. }
  207. if (bottom_editor) {
  208. fit_child_in_rect(bottom_editor, bottom_rect);
  209. bottom_child_rect = bottom_rect;
  210. }
  211. queue_redraw(); //need to redraw text
  212. } break;
  213. case NOTIFICATION_DRAW: {
  214. Ref<Font> font = get_theme_font(SceneStringName(font), SNAME("Tree"));
  215. int font_size = get_theme_font_size(SceneStringName(font_size), SNAME("Tree"));
  216. bool rtl = is_layout_rtl();
  217. Size2 size = get_size();
  218. if (bottom_editor) {
  219. size.height = bottom_editor->get_offset(SIDE_TOP) - get_theme_constant(SNAME("v_separation"));
  220. } else if (label_reference) {
  221. size.height = label_reference->get_size().height;
  222. }
  223. // Only draw the label if it's not empty.
  224. if (label.is_empty()) {
  225. size.height = 0;
  226. } else {
  227. Ref<StyleBox> sb = get_theme_stylebox(selected ? SNAME("bg_selected") : SNAME("bg"));
  228. draw_style_box(sb, Rect2(Vector2(), size));
  229. }
  230. Ref<StyleBox> bg_stylebox = get_theme_stylebox(SNAME("child_bg"));
  231. if (draw_top_bg && right_child_rect != Rect2()) {
  232. draw_style_box(bg_stylebox, right_child_rect);
  233. }
  234. if (bottom_child_rect != Rect2()) {
  235. draw_style_box(bg_stylebox, bottom_child_rect);
  236. }
  237. Color color;
  238. if (draw_warning || draw_prop_warning) {
  239. color = get_theme_color(is_read_only() ? SNAME("readonly_warning_color") : SNAME("warning_color"));
  240. } else {
  241. color = get_theme_color(is_read_only() ? SNAME("readonly_color") : SNAME("property_color"));
  242. }
  243. if (label.contains_char('.')) {
  244. // FIXME: Move this to the project settings editor, as this is only used
  245. // for project settings feature tag overrides.
  246. color.a = 0.5;
  247. }
  248. int ofs = get_theme_constant(SNAME("font_offset"));
  249. int text_limit = text_size - ofs;
  250. if (checkable) {
  251. Ref<Texture2D> checkbox;
  252. if (checked) {
  253. checkbox = get_editor_theme_icon(SNAME("GuiChecked"));
  254. } else {
  255. checkbox = get_editor_theme_icon(SNAME("GuiUnchecked"));
  256. }
  257. Color color2(1, 1, 1);
  258. if (check_hover) {
  259. color2.r *= 1.2;
  260. color2.g *= 1.2;
  261. color2.b *= 1.2;
  262. }
  263. check_rect = Rect2(ofs, ((size.height - checkbox->get_height()) / 2), checkbox->get_width(), checkbox->get_height());
  264. if (rtl) {
  265. draw_texture(checkbox, Vector2(size.width - check_rect.position.x - checkbox->get_width(), check_rect.position.y), color2);
  266. } else {
  267. draw_texture(checkbox, check_rect.position, color2);
  268. }
  269. int check_ofs = checkbox->get_width() + get_theme_constant(SNAME("h_separation"), SNAME("Tree"));
  270. ofs += check_ofs;
  271. text_limit -= check_ofs;
  272. } else {
  273. check_rect = Rect2();
  274. }
  275. if (can_revert && !is_read_only()) {
  276. Ref<Texture2D> reload_icon = get_editor_theme_icon(SNAME("ReloadSmall"));
  277. text_limit -= reload_icon->get_width() + get_theme_constant(SNAME("h_separation"), SNAME("Tree"));
  278. revert_rect = Rect2(ofs + text_limit, (size.height - reload_icon->get_height()) / 2, reload_icon->get_width(), reload_icon->get_height());
  279. Color color2(1, 1, 1);
  280. if (revert_hover) {
  281. color2.r *= 1.2;
  282. color2.g *= 1.2;
  283. color2.b *= 1.2;
  284. }
  285. if (rtl) {
  286. draw_texture(reload_icon, Vector2(size.width - revert_rect.position.x - reload_icon->get_width(), revert_rect.position.y), color2);
  287. } else {
  288. draw_texture(reload_icon, revert_rect.position, color2);
  289. }
  290. } else {
  291. revert_rect = Rect2();
  292. }
  293. if (!pin_hidden && pinned) {
  294. Ref<Texture2D> pinned_icon = get_editor_theme_icon(SNAME("Pin"));
  295. int margin_w = get_theme_constant(SNAME("h_separation"), SNAME("Tree"));
  296. int total_icon_w = margin_w + pinned_icon->get_width();
  297. int text_w = font->get_string_size(label, rtl ? HORIZONTAL_ALIGNMENT_RIGHT : HORIZONTAL_ALIGNMENT_LEFT, text_limit - total_icon_w, font_size).x;
  298. int y = (size.height - pinned_icon->get_height()) / 2;
  299. if (rtl) {
  300. draw_texture(pinned_icon, Vector2(size.width - ofs - text_w - total_icon_w, y), color);
  301. } else {
  302. draw_texture(pinned_icon, Vector2(ofs + text_w + margin_w, y), color);
  303. }
  304. text_limit -= total_icon_w;
  305. }
  306. int v_ofs = (size.height - font->get_height(font_size)) / 2;
  307. if (rtl) {
  308. draw_string(font, Point2(size.width - ofs - text_limit, v_ofs + font->get_ascent(font_size)), label, HORIZONTAL_ALIGNMENT_RIGHT, text_limit, font_size, color);
  309. } else {
  310. draw_string(font, Point2(ofs, v_ofs + font->get_ascent(font_size)), label, HORIZONTAL_ALIGNMENT_LEFT, text_limit, font_size, color);
  311. }
  312. ofs = size.width;
  313. if (keying) {
  314. Ref<Texture2D> key;
  315. if (use_keying_next()) {
  316. key = get_editor_theme_icon(SNAME("KeyNext"));
  317. } else {
  318. key = get_editor_theme_icon(SNAME("Key"));
  319. }
  320. ofs -= key->get_width() + get_theme_constant(SNAME("h_separation"), SNAME("Tree"));
  321. Color color2(1, 1, 1);
  322. if (keying_hover) {
  323. color2.r *= 1.2;
  324. color2.g *= 1.2;
  325. color2.b *= 1.2;
  326. }
  327. keying_rect = Rect2(ofs, ((size.height - key->get_height()) / 2), key->get_width(), key->get_height());
  328. if (rtl) {
  329. draw_texture(key, Vector2(size.width - keying_rect.position.x - key->get_width(), keying_rect.position.y), color2);
  330. } else {
  331. draw_texture(key, keying_rect.position, color2);
  332. }
  333. } else {
  334. keying_rect = Rect2();
  335. }
  336. if (deletable) {
  337. Ref<Texture2D> close;
  338. close = get_editor_theme_icon(SNAME("Close"));
  339. ofs -= close->get_width() + get_theme_constant(SNAME("h_separation"), SNAME("Tree"));
  340. Color color2(1, 1, 1);
  341. if (delete_hover) {
  342. color2.r *= 1.2;
  343. color2.g *= 1.2;
  344. color2.b *= 1.2;
  345. }
  346. delete_rect = Rect2(ofs, ((size.height - close->get_height()) / 2), close->get_width(), close->get_height());
  347. if (rtl) {
  348. draw_texture(close, Vector2(size.width - delete_rect.position.x - close->get_width(), delete_rect.position.y), color2);
  349. } else {
  350. draw_texture(close, delete_rect.position, color2);
  351. }
  352. } else {
  353. delete_rect = Rect2();
  354. }
  355. } break;
  356. case NOTIFICATION_ENTER_TREE: {
  357. if (has_borders) {
  358. get_parent()->connect(SceneStringName(theme_changed), callable_mp(this, &EditorProperty::_update_property_bg));
  359. _update_property_bg();
  360. }
  361. } break;
  362. case NOTIFICATION_EXIT_TREE: {
  363. if (has_borders) {
  364. get_parent()->disconnect(SceneStringName(theme_changed), callable_mp(this, &EditorProperty::_update_property_bg));
  365. }
  366. } break;
  367. }
  368. }
  369. void EditorProperty::set_label(const String &p_label) {
  370. label = p_label;
  371. queue_redraw();
  372. }
  373. String EditorProperty::get_label() const {
  374. return label;
  375. }
  376. Object *EditorProperty::get_edited_object() {
  377. return object;
  378. }
  379. StringName EditorProperty::get_edited_property() const {
  380. return property;
  381. }
  382. EditorInspector *EditorProperty::get_parent_inspector() const {
  383. Node *parent = get_parent();
  384. while (parent) {
  385. EditorInspector *ei = Object::cast_to<EditorInspector>(parent);
  386. if (ei) {
  387. return ei;
  388. }
  389. parent = parent->get_parent();
  390. }
  391. return nullptr;
  392. }
  393. void EditorProperty::set_doc_path(const String &p_doc_path) {
  394. doc_path = p_doc_path;
  395. }
  396. void EditorProperty::set_internal(bool p_internal) {
  397. internal = p_internal;
  398. }
  399. void EditorProperty::update_property() {
  400. GDVIRTUAL_CALL(_update_property);
  401. }
  402. void EditorProperty::_set_read_only(bool p_read_only) {
  403. }
  404. void EditorProperty::set_read_only(bool p_read_only) {
  405. read_only = p_read_only;
  406. if (GDVIRTUAL_CALL(_set_read_only, p_read_only)) {
  407. return;
  408. }
  409. _set_read_only(p_read_only);
  410. }
  411. bool EditorProperty::is_read_only() const {
  412. return read_only;
  413. }
  414. Variant EditorPropertyRevert::get_property_revert_value(Object *p_object, const StringName &p_property, bool *r_is_valid) {
  415. if (p_object->property_can_revert(p_property)) {
  416. if (r_is_valid) {
  417. *r_is_valid = true;
  418. }
  419. return p_object->property_get_revert(p_property);
  420. }
  421. return PropertyUtils::get_property_default_value(p_object, p_property, r_is_valid);
  422. }
  423. bool EditorPropertyRevert::can_property_revert(Object *p_object, const StringName &p_property, const Variant *p_custom_current_value) {
  424. bool is_valid_revert = false;
  425. Variant revert_value = EditorPropertyRevert::get_property_revert_value(p_object, p_property, &is_valid_revert);
  426. if (!is_valid_revert) {
  427. return false;
  428. }
  429. Variant current_value = p_custom_current_value ? *p_custom_current_value : p_object->get(p_property);
  430. return PropertyUtils::is_property_value_different(p_object, current_value, revert_value);
  431. }
  432. StringName EditorProperty::_get_revert_property() const {
  433. return property;
  434. }
  435. void EditorProperty::_update_property_bg() {
  436. // This function is to be called on EditorPropertyResource, EditorPropertyArray, and EditorPropertyDictionary.
  437. // Behavior is undetermined on any other EditorProperty.
  438. if (!is_inside_tree()) {
  439. return;
  440. }
  441. begin_bulk_theme_override();
  442. if (bottom_editor) {
  443. ColorationMode nested_color_mode = (ColorationMode)(int)EDITOR_GET("interface/inspector/nested_color_mode");
  444. bool delimitate_all_container_and_resources = EDITOR_GET("interface/inspector/delimitate_all_container_and_resources");
  445. int count_subinspectors = 0;
  446. if (is_colored(nested_color_mode)) {
  447. Node *n = this;
  448. while (n) {
  449. EditorProperty *ep = Object::cast_to<EditorProperty>(n);
  450. if (ep && ep->is_colored(nested_color_mode)) {
  451. count_subinspectors++;
  452. }
  453. n = n->get_parent();
  454. }
  455. count_subinspectors = MIN(16, count_subinspectors);
  456. }
  457. add_theme_style_override(SNAME("DictionaryAddItem"), get_theme_stylebox("DictionaryAddItem" + itos(count_subinspectors), EditorStringName(EditorStyles)));
  458. add_theme_constant_override("v_separation", 0);
  459. if (delimitate_all_container_and_resources || is_colored(nested_color_mode)) {
  460. add_theme_style_override("bg_selected", get_theme_stylebox("sub_inspector_property_bg" + itos(count_subinspectors), EditorStringName(EditorStyles)));
  461. add_theme_style_override("bg", get_theme_stylebox("sub_inspector_property_bg" + itos(count_subinspectors), EditorStringName(EditorStyles)));
  462. add_theme_color_override("property_color", get_theme_color(SNAME("sub_inspector_property_color"), EditorStringName(EditorStyles)));
  463. bottom_editor->add_theme_style_override(SceneStringName(panel), get_theme_stylebox("sub_inspector_bg" + itos(count_subinspectors), EditorStringName(EditorStyles)));
  464. } else {
  465. bottom_editor->add_theme_style_override(SceneStringName(panel), get_theme_stylebox("sub_inspector_bg_no_border", EditorStringName(EditorStyles)));
  466. }
  467. } else {
  468. remove_theme_style_override("bg_selected");
  469. remove_theme_style_override("bg");
  470. remove_theme_color_override("property_color");
  471. }
  472. end_bulk_theme_override();
  473. queue_redraw();
  474. }
  475. void EditorProperty::update_editor_property_status() {
  476. if (property == StringName()) {
  477. return; //no property, so nothing to do
  478. }
  479. bool new_pinned = false;
  480. if (can_pin) {
  481. Node *node = Object::cast_to<Node>(object);
  482. CRASH_COND(!node);
  483. new_pinned = node->is_property_pinned(property);
  484. }
  485. bool new_warning = false;
  486. if (object->has_method("_get_property_warning")) {
  487. new_warning = !String(object->call("_get_property_warning", property)).is_empty();
  488. }
  489. Variant current = object->get(_get_revert_property());
  490. bool new_can_revert = EditorPropertyRevert::can_property_revert(object, property, &current) && !is_read_only();
  491. bool new_checked = checked;
  492. if (checkable) { // for properties like theme overrides.
  493. bool valid = false;
  494. Variant value = object->get(property, &valid);
  495. if (valid) {
  496. new_checked = value.get_type() != Variant::NIL;
  497. }
  498. }
  499. if (new_can_revert != can_revert || new_pinned != pinned || new_checked != checked || new_warning != draw_prop_warning) {
  500. if (new_can_revert != can_revert) {
  501. emit_signal(SNAME("property_can_revert_changed"), property, new_can_revert);
  502. }
  503. draw_prop_warning = new_warning;
  504. can_revert = new_can_revert;
  505. pinned = new_pinned;
  506. checked = new_checked;
  507. queue_redraw();
  508. }
  509. }
  510. bool EditorProperty::use_keying_next() const {
  511. List<PropertyInfo> plist;
  512. object->get_property_list(&plist, true);
  513. for (List<PropertyInfo>::Element *I = plist.front(); I; I = I->next()) {
  514. PropertyInfo &p = I->get();
  515. if (p.name == property) {
  516. return (p.usage & PROPERTY_USAGE_KEYING_INCREMENTS);
  517. }
  518. }
  519. return false;
  520. }
  521. void EditorProperty::set_checkable(bool p_checkable) {
  522. checkable = p_checkable;
  523. queue_redraw();
  524. queue_sort();
  525. }
  526. bool EditorProperty::is_checkable() const {
  527. return checkable;
  528. }
  529. void EditorProperty::set_checked(bool p_checked) {
  530. checked = p_checked;
  531. queue_redraw();
  532. }
  533. bool EditorProperty::is_checked() const {
  534. return checked;
  535. }
  536. void EditorProperty::set_draw_warning(bool p_draw_warning) {
  537. draw_warning = p_draw_warning;
  538. queue_redraw();
  539. }
  540. void EditorProperty::set_keying(bool p_keying) {
  541. keying = p_keying;
  542. queue_redraw();
  543. queue_sort();
  544. }
  545. void EditorProperty::set_deletable(bool p_deletable) {
  546. deletable = p_deletable;
  547. queue_redraw();
  548. queue_sort();
  549. }
  550. bool EditorProperty::is_deletable() const {
  551. return deletable;
  552. }
  553. bool EditorProperty::is_keying() const {
  554. return keying;
  555. }
  556. bool EditorProperty::is_draw_warning() const {
  557. return draw_warning;
  558. }
  559. void EditorProperty::_focusable_focused(int p_index) {
  560. if (!selectable) {
  561. return;
  562. }
  563. bool already_selected = selected;
  564. selected = true;
  565. selected_focusable = p_index;
  566. queue_redraw();
  567. if (!already_selected && selected) {
  568. emit_signal(SNAME("selected"), property, selected_focusable);
  569. }
  570. }
  571. void EditorProperty::add_focusable(Control *p_control) {
  572. p_control->connect(SceneStringName(focus_entered), callable_mp(this, &EditorProperty::_focusable_focused).bind(focusables.size()));
  573. focusables.push_back(p_control);
  574. }
  575. void EditorProperty::grab_focus(int p_focusable) {
  576. if (focusables.is_empty()) {
  577. return;
  578. }
  579. if (p_focusable >= 0) {
  580. ERR_FAIL_INDEX(p_focusable, focusables.size());
  581. focusables[p_focusable]->grab_focus();
  582. } else {
  583. focusables[0]->grab_focus();
  584. }
  585. }
  586. void EditorProperty::select(int p_focusable) {
  587. bool already_selected = selected;
  588. if (!selectable) {
  589. return;
  590. }
  591. if (p_focusable >= 0) {
  592. ERR_FAIL_INDEX(p_focusable, focusables.size());
  593. focusables[p_focusable]->grab_focus();
  594. } else {
  595. selected = true;
  596. queue_redraw();
  597. }
  598. if (!already_selected && selected) {
  599. emit_signal(SNAME("selected"), property, selected_focusable);
  600. }
  601. }
  602. void EditorProperty::deselect() {
  603. selected = false;
  604. selected_focusable = -1;
  605. queue_redraw();
  606. }
  607. bool EditorProperty::is_selected() const {
  608. return selected;
  609. }
  610. void EditorProperty::gui_input(const Ref<InputEvent> &p_event) {
  611. ERR_FAIL_COND(p_event.is_null());
  612. if (property == StringName()) {
  613. return;
  614. }
  615. Ref<InputEventMouse> me = p_event;
  616. if (me.is_valid()) {
  617. Vector2 mpos = me->get_position();
  618. if (is_layout_rtl()) {
  619. mpos.x = get_size().x - mpos.x;
  620. }
  621. bool button_left = me->get_button_mask().has_flag(MouseButtonMask::LEFT);
  622. bool new_keying_hover = keying_rect.has_point(mpos) && !button_left;
  623. if (new_keying_hover != keying_hover) {
  624. keying_hover = new_keying_hover;
  625. queue_redraw();
  626. }
  627. bool new_delete_hover = delete_rect.has_point(mpos) && !button_left;
  628. if (new_delete_hover != delete_hover) {
  629. delete_hover = new_delete_hover;
  630. queue_redraw();
  631. }
  632. bool new_revert_hover = revert_rect.has_point(mpos) && !button_left;
  633. if (new_revert_hover != revert_hover) {
  634. revert_hover = new_revert_hover;
  635. queue_redraw();
  636. }
  637. bool new_check_hover = check_rect.has_point(mpos) && !button_left;
  638. if (new_check_hover != check_hover) {
  639. check_hover = new_check_hover;
  640. queue_redraw();
  641. }
  642. }
  643. Ref<InputEventMouseButton> mb = p_event;
  644. if (mb.is_valid() && mb->is_pressed() && mb->get_button_index() == MouseButton::LEFT) {
  645. Vector2 mpos = mb->get_position();
  646. if (is_layout_rtl()) {
  647. mpos.x = get_size().x - mpos.x;
  648. }
  649. select();
  650. if (keying_rect.has_point(mpos)) {
  651. accept_event();
  652. emit_signal(SNAME("property_keyed"), property, use_keying_next());
  653. if (use_keying_next()) {
  654. if (property == "frame_coords" && (object->is_class("Sprite2D") || object->is_class("Sprite3D"))) {
  655. Vector2i new_coords = object->get(property);
  656. new_coords.x++;
  657. if (new_coords.x >= int64_t(object->get("hframes"))) {
  658. new_coords.x = 0;
  659. new_coords.y++;
  660. }
  661. if (new_coords.x < int64_t(object->get("hframes")) && new_coords.y < int64_t(object->get("vframes"))) {
  662. callable_mp(this, &EditorProperty::emit_changed).call_deferred(property, new_coords, "", false);
  663. }
  664. } else {
  665. if (int64_t(object->get(property)) + 1 < (int64_t(object->get("hframes")) * int64_t(object->get("vframes")))) {
  666. callable_mp(this, &EditorProperty::emit_changed).call_deferred(property, object->get(property).operator int64_t() + 1, "", false);
  667. }
  668. }
  669. callable_mp(this, &EditorProperty::update_property).call_deferred();
  670. }
  671. }
  672. if (delete_rect.has_point(mpos)) {
  673. accept_event();
  674. emit_signal(SNAME("property_deleted"), property);
  675. }
  676. if (revert_rect.has_point(mpos)) {
  677. accept_event();
  678. get_viewport()->gui_release_focus();
  679. bool is_valid_revert = false;
  680. Variant revert_value = EditorPropertyRevert::get_property_revert_value(object, property, &is_valid_revert);
  681. ERR_FAIL_COND(!is_valid_revert);
  682. emit_changed(_get_revert_property(), revert_value);
  683. update_property();
  684. }
  685. if (check_rect.has_point(mpos)) {
  686. accept_event();
  687. checked = !checked;
  688. queue_redraw();
  689. emit_signal(SNAME("property_checked"), property, checked);
  690. }
  691. } else if (mb.is_valid() && mb->is_pressed() && mb->get_button_index() == MouseButton::RIGHT) {
  692. accept_event();
  693. _update_popup();
  694. menu->set_position(get_screen_position() + get_local_mouse_position());
  695. menu->reset_size();
  696. menu->popup();
  697. select();
  698. return;
  699. }
  700. }
  701. void EditorProperty::shortcut_input(const Ref<InputEvent> &p_event) {
  702. if (!selected || !p_event->is_pressed()) {
  703. return;
  704. }
  705. const Ref<InputEventKey> k = p_event;
  706. if (k.is_valid() && k->is_pressed()) {
  707. if (ED_IS_SHORTCUT("property_editor/copy_value", p_event)) {
  708. menu_option(MENU_COPY_VALUE);
  709. accept_event();
  710. } else if (!is_read_only() && ED_IS_SHORTCUT("property_editor/paste_value", p_event)) {
  711. menu_option(MENU_PASTE_VALUE);
  712. accept_event();
  713. } else if (!internal && ED_IS_SHORTCUT("property_editor/copy_property_path", p_event)) {
  714. menu_option(MENU_COPY_PROPERTY_PATH);
  715. accept_event();
  716. }
  717. }
  718. }
  719. const Color *EditorProperty::_get_property_colors() {
  720. static Color c[4];
  721. c[0] = get_theme_color(SNAME("property_color_x"), EditorStringName(Editor));
  722. c[1] = get_theme_color(SNAME("property_color_y"), EditorStringName(Editor));
  723. c[2] = get_theme_color(SNAME("property_color_z"), EditorStringName(Editor));
  724. c[3] = get_theme_color(SNAME("property_color_w"), EditorStringName(Editor));
  725. return c;
  726. }
  727. void EditorProperty::set_label_reference(Control *p_control) {
  728. label_reference = p_control;
  729. }
  730. void EditorProperty::set_bottom_editor(Control *p_control) {
  731. bottom_editor = p_control;
  732. if (has_borders) {
  733. _update_property_bg();
  734. }
  735. }
  736. Variant EditorProperty::_get_cache_value(const StringName &p_prop, bool &r_valid) const {
  737. return object->get(p_prop, &r_valid);
  738. }
  739. bool EditorProperty::is_cache_valid() const {
  740. if (object) {
  741. for (const KeyValue<StringName, Variant> &E : cache) {
  742. bool valid;
  743. Variant value = _get_cache_value(E.key, valid);
  744. if (!valid || value != E.value) {
  745. return false;
  746. }
  747. }
  748. }
  749. return true;
  750. }
  751. void EditorProperty::update_cache() {
  752. cache.clear();
  753. if (object && property != StringName()) {
  754. bool valid;
  755. Variant value = _get_cache_value(property, valid);
  756. if (valid) {
  757. cache[property] = value;
  758. }
  759. }
  760. }
  761. Variant EditorProperty::get_drag_data(const Point2 &p_point) {
  762. if (property == StringName()) {
  763. return Variant();
  764. }
  765. Dictionary dp;
  766. dp["type"] = "obj_property";
  767. dp["object"] = object;
  768. dp["property"] = property;
  769. dp["value"] = object->get(property);
  770. Label *drag_label = memnew(Label);
  771. drag_label->set_text(property);
  772. drag_label->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED); // Don't translate raw property name.
  773. set_drag_preview(drag_label);
  774. return dp;
  775. }
  776. void EditorProperty::set_use_folding(bool p_use_folding) {
  777. use_folding = p_use_folding;
  778. }
  779. bool EditorProperty::is_using_folding() const {
  780. return use_folding;
  781. }
  782. void EditorProperty::expand_all_folding() {
  783. }
  784. void EditorProperty::collapse_all_folding() {
  785. }
  786. void EditorProperty::expand_revertable() {
  787. }
  788. void EditorProperty::set_selectable(bool p_selectable) {
  789. selectable = p_selectable;
  790. }
  791. bool EditorProperty::is_selectable() const {
  792. return selectable;
  793. }
  794. void EditorProperty::set_name_split_ratio(float p_ratio) {
  795. split_ratio = p_ratio;
  796. }
  797. float EditorProperty::get_name_split_ratio() const {
  798. return split_ratio;
  799. }
  800. void EditorProperty::set_favoritable(bool p_favoritable) {
  801. can_favorite = p_favoritable;
  802. }
  803. bool EditorProperty::is_favoritable() const {
  804. return can_favorite;
  805. }
  806. void EditorProperty::set_object_and_property(Object *p_object, const StringName &p_property) {
  807. object = p_object;
  808. property = p_property;
  809. _update_flags();
  810. }
  811. static bool _is_value_potential_override(Node *p_node, const String &p_property) {
  812. // Consider a value is potentially overriding another if either of the following is true:
  813. // a) The node is foreign (inheriting or an instance), so the original value may come from another scene.
  814. // b) The node belongs to the scene, but the original value comes from somewhere but the builtin class (i.e., a script).
  815. Node *edited_scene = EditorNode::get_singleton()->get_edited_scene();
  816. Vector<SceneState::PackState> states_stack = PropertyUtils::get_node_states_stack(p_node, edited_scene);
  817. if (states_stack.size()) {
  818. return true;
  819. } else {
  820. bool is_valid_default = false;
  821. bool is_class_default = false;
  822. PropertyUtils::get_property_default_value(p_node, p_property, &is_valid_default, &states_stack, false, nullptr, &is_class_default);
  823. return !is_class_default;
  824. }
  825. }
  826. void EditorProperty::_update_flags() {
  827. can_pin = false;
  828. pin_hidden = true;
  829. if (read_only) {
  830. return;
  831. }
  832. if (Node *node = Object::cast_to<Node>(object)) {
  833. // Avoid errors down the road by ignoring nodes which are not part of a scene
  834. if (!node->get_owner()) {
  835. bool is_scene_root = false;
  836. for (int i = 0; i < EditorNode::get_editor_data().get_edited_scene_count(); ++i) {
  837. if (EditorNode::get_editor_data().get_edited_scene_root(i) == node) {
  838. is_scene_root = true;
  839. break;
  840. }
  841. }
  842. if (!is_scene_root) {
  843. return;
  844. }
  845. }
  846. if (!_is_value_potential_override(node, property)) {
  847. return;
  848. }
  849. pin_hidden = false;
  850. {
  851. HashSet<StringName> storable_properties;
  852. node->get_storable_properties(storable_properties);
  853. if (storable_properties.has(node->get_property_store_alias(property))) {
  854. can_pin = true;
  855. }
  856. }
  857. }
  858. }
  859. Control *EditorProperty::make_custom_tooltip(const String &p_text) const {
  860. String symbol;
  861. String prologue;
  862. if (object->has_method("_get_property_warning")) {
  863. const String custom_warning = object->call("_get_property_warning", property);
  864. if (!custom_warning.is_empty()) {
  865. prologue = "[b][color=" + get_theme_color(SNAME("warning_color")).to_html(false) + "]" + custom_warning + "[/color][/b]";
  866. }
  867. }
  868. if (has_doc_tooltip) {
  869. symbol = p_text;
  870. const EditorInspector *inspector = get_parent_inspector();
  871. if (inspector) {
  872. const String custom_description = inspector->get_custom_property_description(p_text);
  873. if (!custom_description.is_empty()) {
  874. if (!prologue.is_empty()) {
  875. prologue += '\n';
  876. }
  877. prologue += custom_description;
  878. }
  879. }
  880. }
  881. if (!symbol.is_empty() || !prologue.is_empty()) {
  882. return EditorHelpBitTooltip::show_tooltip(const_cast<EditorProperty *>(this), symbol, prologue);
  883. }
  884. return nullptr;
  885. }
  886. void EditorProperty::menu_option(int p_option) {
  887. switch (p_option) {
  888. case MENU_COPY_VALUE: {
  889. InspectorDock::get_inspector_singleton()->set_property_clipboard(object->get(property));
  890. } break;
  891. case MENU_PASTE_VALUE: {
  892. emit_changed(property, InspectorDock::get_inspector_singleton()->get_property_clipboard());
  893. } break;
  894. case MENU_COPY_PROPERTY_PATH: {
  895. DisplayServer::get_singleton()->clipboard_set(property_path);
  896. } break;
  897. case MENU_FAVORITE_PROPERTY: {
  898. emit_signal(SNAME("property_favorited"), property, !favorited);
  899. queue_redraw();
  900. } break;
  901. case MENU_PIN_VALUE: {
  902. emit_signal(SNAME("property_pinned"), property, !pinned);
  903. queue_redraw();
  904. } break;
  905. case MENU_OPEN_DOCUMENTATION: {
  906. ScriptEditor::get_singleton()->goto_help(doc_path);
  907. EditorNode::get_singleton()->get_editor_main_screen()->select(EditorMainScreen::EDITOR_SCRIPT);
  908. } break;
  909. }
  910. }
  911. void EditorProperty::_bind_methods() {
  912. ClassDB::bind_method(D_METHOD("set_label", "text"), &EditorProperty::set_label);
  913. ClassDB::bind_method(D_METHOD("get_label"), &EditorProperty::get_label);
  914. ClassDB::bind_method(D_METHOD("set_read_only", "read_only"), &EditorProperty::set_read_only);
  915. ClassDB::bind_method(D_METHOD("is_read_only"), &EditorProperty::is_read_only);
  916. ClassDB::bind_method(D_METHOD("set_checkable", "checkable"), &EditorProperty::set_checkable);
  917. ClassDB::bind_method(D_METHOD("is_checkable"), &EditorProperty::is_checkable);
  918. ClassDB::bind_method(D_METHOD("set_checked", "checked"), &EditorProperty::set_checked);
  919. ClassDB::bind_method(D_METHOD("is_checked"), &EditorProperty::is_checked);
  920. ClassDB::bind_method(D_METHOD("set_draw_warning", "draw_warning"), &EditorProperty::set_draw_warning);
  921. ClassDB::bind_method(D_METHOD("is_draw_warning"), &EditorProperty::is_draw_warning);
  922. ClassDB::bind_method(D_METHOD("set_keying", "keying"), &EditorProperty::set_keying);
  923. ClassDB::bind_method(D_METHOD("is_keying"), &EditorProperty::is_keying);
  924. ClassDB::bind_method(D_METHOD("set_deletable", "deletable"), &EditorProperty::set_deletable);
  925. ClassDB::bind_method(D_METHOD("is_deletable"), &EditorProperty::is_deletable);
  926. ClassDB::bind_method(D_METHOD("get_edited_property"), &EditorProperty::get_edited_property);
  927. ClassDB::bind_method(D_METHOD("get_edited_object"), &EditorProperty::get_edited_object);
  928. ClassDB::bind_method(D_METHOD("update_property"), &EditorProperty::update_property);
  929. ClassDB::bind_method(D_METHOD("add_focusable", "control"), &EditorProperty::add_focusable);
  930. ClassDB::bind_method(D_METHOD("set_bottom_editor", "editor"), &EditorProperty::set_bottom_editor);
  931. ClassDB::bind_method(D_METHOD("set_selectable", "selectable"), &EditorProperty::set_selectable);
  932. ClassDB::bind_method(D_METHOD("is_selectable"), &EditorProperty::is_selectable);
  933. ClassDB::bind_method(D_METHOD("set_use_folding", "use_folding"), &EditorProperty::set_use_folding);
  934. ClassDB::bind_method(D_METHOD("is_using_folding"), &EditorProperty::is_using_folding);
  935. ClassDB::bind_method(D_METHOD("set_name_split_ratio", "ratio"), &EditorProperty::set_name_split_ratio);
  936. ClassDB::bind_method(D_METHOD("get_name_split_ratio"), &EditorProperty::get_name_split_ratio);
  937. ClassDB::bind_method(D_METHOD("deselect"), &EditorProperty::deselect);
  938. ClassDB::bind_method(D_METHOD("is_selected"), &EditorProperty::is_selected);
  939. ClassDB::bind_method(D_METHOD("select", "focusable"), &EditorProperty::select, DEFVAL(-1));
  940. ClassDB::bind_method(D_METHOD("set_object_and_property", "object", "property"), &EditorProperty::set_object_and_property);
  941. ClassDB::bind_method(D_METHOD("set_label_reference", "control"), &EditorProperty::set_label_reference);
  942. ClassDB::bind_method(D_METHOD("emit_changed", "property", "value", "field", "changing"), &EditorProperty::emit_changed, DEFVAL(StringName()), DEFVAL(false));
  943. ADD_PROPERTY(PropertyInfo(Variant::STRING, "label"), "set_label", "get_label");
  944. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "read_only"), "set_read_only", "is_read_only");
  945. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "checkable"), "set_checkable", "is_checkable");
  946. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "checked"), "set_checked", "is_checked");
  947. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "draw_warning"), "set_draw_warning", "is_draw_warning");
  948. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "keying"), "set_keying", "is_keying");
  949. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "deletable"), "set_deletable", "is_deletable");
  950. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "selectable"), "set_selectable", "is_selectable");
  951. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "use_folding"), "set_use_folding", "is_using_folding");
  952. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "name_split_ratio"), "set_name_split_ratio", "get_name_split_ratio");
  953. ADD_SIGNAL(MethodInfo("property_changed", PropertyInfo(Variant::STRING_NAME, "property"), PropertyInfo(Variant::NIL, "value", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NIL_IS_VARIANT), PropertyInfo(Variant::STRING_NAME, "field"), PropertyInfo(Variant::BOOL, "changing")));
  954. ADD_SIGNAL(MethodInfo("multiple_properties_changed", PropertyInfo(Variant::PACKED_STRING_ARRAY, "properties"), PropertyInfo(Variant::ARRAY, "value")));
  955. ADD_SIGNAL(MethodInfo("property_keyed", PropertyInfo(Variant::STRING_NAME, "property")));
  956. ADD_SIGNAL(MethodInfo("property_deleted", PropertyInfo(Variant::STRING_NAME, "property")));
  957. ADD_SIGNAL(MethodInfo("property_keyed_with_value", PropertyInfo(Variant::STRING_NAME, "property"), PropertyInfo(Variant::NIL, "value", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NIL_IS_VARIANT)));
  958. ADD_SIGNAL(MethodInfo("property_checked", PropertyInfo(Variant::STRING_NAME, "property"), PropertyInfo(Variant::BOOL, "checked")));
  959. ADD_SIGNAL(MethodInfo("property_favorited", PropertyInfo(Variant::STRING_NAME, "property"), PropertyInfo(Variant::BOOL, "favorited")));
  960. ADD_SIGNAL(MethodInfo("property_pinned", PropertyInfo(Variant::STRING_NAME, "property"), PropertyInfo(Variant::BOOL, "pinned")));
  961. ADD_SIGNAL(MethodInfo("property_can_revert_changed", PropertyInfo(Variant::STRING_NAME, "property"), PropertyInfo(Variant::BOOL, "can_revert")));
  962. ADD_SIGNAL(MethodInfo("resource_selected", PropertyInfo(Variant::STRING, "path"), PropertyInfo(Variant::OBJECT, "resource", PROPERTY_HINT_RESOURCE_TYPE, "Resource")));
  963. ADD_SIGNAL(MethodInfo("object_id_selected", PropertyInfo(Variant::STRING_NAME, "property"), PropertyInfo(Variant::INT, "id")));
  964. ADD_SIGNAL(MethodInfo("selected", PropertyInfo(Variant::STRING, "path"), PropertyInfo(Variant::INT, "focusable_idx")));
  965. GDVIRTUAL_BIND(_update_property)
  966. GDVIRTUAL_BIND(_set_read_only, "read_only")
  967. ClassDB::bind_method(D_METHOD("_update_editor_property_status"), &EditorProperty::update_editor_property_status);
  968. }
  969. EditorProperty::EditorProperty() {
  970. object = nullptr;
  971. split_ratio = 0.5;
  972. text_size = 0;
  973. property_usage = 0;
  974. selected_focusable = -1;
  975. label_reference = nullptr;
  976. bottom_editor = nullptr;
  977. menu = nullptr;
  978. set_process_shortcut_input(true);
  979. }
  980. void EditorProperty::_update_popup() {
  981. if (menu) {
  982. menu->clear();
  983. } else {
  984. menu = memnew(PopupMenu);
  985. add_child(menu);
  986. menu->connect(SceneStringName(id_pressed), callable_mp(this, &EditorProperty::menu_option));
  987. }
  988. menu->add_icon_shortcut(get_editor_theme_icon(SNAME("ActionCopy")), ED_GET_SHORTCUT("property_editor/copy_value"), MENU_COPY_VALUE);
  989. menu->add_icon_shortcut(get_editor_theme_icon(SNAME("ActionPaste")), ED_GET_SHORTCUT("property_editor/paste_value"), MENU_PASTE_VALUE);
  990. menu->add_icon_shortcut(get_editor_theme_icon(SNAME("CopyNodePath")), ED_GET_SHORTCUT("property_editor/copy_property_path"), MENU_COPY_PROPERTY_PATH);
  991. menu->set_item_disabled(MENU_PASTE_VALUE, is_read_only());
  992. menu->set_item_disabled(MENU_COPY_PROPERTY_PATH, internal);
  993. if (can_favorite || !pin_hidden) {
  994. menu->add_separator();
  995. }
  996. if (can_favorite) {
  997. if (favorited) {
  998. menu->add_icon_item(get_editor_theme_icon(SNAME("Unfavorite")), TTR("Unfavorite Property"), MENU_FAVORITE_PROPERTY);
  999. menu->set_item_tooltip(menu->get_item_index(MENU_FAVORITE_PROPERTY), TTR("Make this property be put back at its original place."));
  1000. } else {
  1001. menu->add_icon_item(get_editor_theme_icon(SNAME("Favorites")), TTR("Favorite Property"), MENU_FAVORITE_PROPERTY);
  1002. menu->set_item_tooltip(menu->get_item_index(MENU_FAVORITE_PROPERTY), TTR("Make this property be placed at the top for all objects of this class."));
  1003. }
  1004. }
  1005. if (!pin_hidden) {
  1006. if (can_pin) {
  1007. menu->add_icon_check_item(get_editor_theme_icon(SNAME("Pin")), TTR("Pin Value"), MENU_PIN_VALUE);
  1008. menu->set_item_checked(menu->get_item_index(MENU_PIN_VALUE), pinned);
  1009. } else {
  1010. menu->add_icon_check_item(get_editor_theme_icon(SNAME("Pin")), vformat(TTR("Pin Value [Disabled because '%s' is editor-only]"), property), MENU_PIN_VALUE);
  1011. menu->set_item_disabled(menu->get_item_index(MENU_PIN_VALUE), true);
  1012. }
  1013. menu->set_item_tooltip(menu->get_item_index(MENU_PIN_VALUE), TTR("Pinning a value forces it to be saved even if it's equal to the default."));
  1014. }
  1015. if (!doc_path.is_empty()) {
  1016. menu->add_separator();
  1017. menu->add_icon_item(get_editor_theme_icon(SNAME("Help")), TTR("Open Documentation"), MENU_OPEN_DOCUMENTATION);
  1018. }
  1019. }
  1020. ////////////////////////////////////////////////
  1021. ////////////////////////////////////////////////
  1022. void EditorInspectorPlugin::add_custom_control(Control *control) {
  1023. AddedEditor ae;
  1024. ae.property_editor = control;
  1025. added_editors.push_back(ae);
  1026. }
  1027. void EditorInspectorPlugin::add_property_editor(const String &p_for_property, Control *p_prop, bool p_add_to_end, const String &p_label) {
  1028. AddedEditor ae;
  1029. ae.properties.push_back(p_for_property);
  1030. ae.property_editor = p_prop;
  1031. ae.add_to_end = p_add_to_end;
  1032. ae.label = p_label;
  1033. added_editors.push_back(ae);
  1034. }
  1035. void EditorInspectorPlugin::add_property_editor_for_multiple_properties(const String &p_label, const Vector<String> &p_properties, Control *p_prop) {
  1036. AddedEditor ae;
  1037. ae.properties = p_properties;
  1038. ae.property_editor = p_prop;
  1039. ae.label = p_label;
  1040. added_editors.push_back(ae);
  1041. }
  1042. bool EditorInspectorPlugin::can_handle(Object *p_object) {
  1043. bool success = false;
  1044. GDVIRTUAL_CALL(_can_handle, p_object, success);
  1045. return success;
  1046. }
  1047. void EditorInspectorPlugin::parse_begin(Object *p_object) {
  1048. GDVIRTUAL_CALL(_parse_begin, p_object);
  1049. }
  1050. void EditorInspectorPlugin::parse_category(Object *p_object, const String &p_category) {
  1051. GDVIRTUAL_CALL(_parse_category, p_object, p_category);
  1052. }
  1053. void EditorInspectorPlugin::parse_group(Object *p_object, const String &p_group) {
  1054. GDVIRTUAL_CALL(_parse_group, p_object, p_group);
  1055. }
  1056. bool EditorInspectorPlugin::parse_property(Object *p_object, const Variant::Type p_type, const String &p_path, const PropertyHint p_hint, const String &p_hint_text, const BitField<PropertyUsageFlags> p_usage, const bool p_wide) {
  1057. bool ret = false;
  1058. GDVIRTUAL_CALL(_parse_property, p_object, p_type, p_path, p_hint, p_hint_text, p_usage, p_wide, ret);
  1059. return ret;
  1060. }
  1061. void EditorInspectorPlugin::parse_end(Object *p_object) {
  1062. GDVIRTUAL_CALL(_parse_end, p_object);
  1063. }
  1064. void EditorInspectorPlugin::_bind_methods() {
  1065. ClassDB::bind_method(D_METHOD("add_custom_control", "control"), &EditorInspectorPlugin::add_custom_control);
  1066. ClassDB::bind_method(D_METHOD("add_property_editor", "property", "editor", "add_to_end", "label"), &EditorInspectorPlugin::add_property_editor, DEFVAL(false), DEFVAL(String()));
  1067. ClassDB::bind_method(D_METHOD("add_property_editor_for_multiple_properties", "label", "properties", "editor"), &EditorInspectorPlugin::add_property_editor_for_multiple_properties);
  1068. GDVIRTUAL_BIND(_can_handle, "object")
  1069. GDVIRTUAL_BIND(_parse_begin, "object")
  1070. GDVIRTUAL_BIND(_parse_category, "object", "category")
  1071. GDVIRTUAL_BIND(_parse_group, "object", "group")
  1072. GDVIRTUAL_BIND(_parse_property, "object", "type", "name", "hint_type", "hint_string", "usage_flags", "wide");
  1073. GDVIRTUAL_BIND(_parse_end, "object")
  1074. }
  1075. ////////////////////////////////////////////////
  1076. ////////////////////////////////////////////////
  1077. void EditorInspectorCategory::_notification(int p_what) {
  1078. switch (p_what) {
  1079. case NOTIFICATION_THEME_CHANGED: {
  1080. if (menu) {
  1081. if (is_favorite) {
  1082. menu->set_item_icon(menu->get_item_index(EditorInspector::MENU_UNFAVORITE_ALL), get_editor_theme_icon(SNAME("Unfavorite")));
  1083. } else {
  1084. menu->set_item_icon(menu->get_item_index(MENU_OPEN_DOCS), get_editor_theme_icon(SNAME("Help")));
  1085. }
  1086. }
  1087. } break;
  1088. case NOTIFICATION_DRAW: {
  1089. Ref<StyleBox> sb = get_theme_stylebox(SNAME("bg"));
  1090. draw_style_box(sb, Rect2(Vector2(), get_size()));
  1091. Ref<Font> font = get_theme_font(SNAME("bold"), EditorStringName(EditorFonts));
  1092. int font_size = get_theme_font_size(SNAME("bold_size"), EditorStringName(EditorFonts));
  1093. int hs = get_theme_constant(SNAME("h_separation"), SNAME("Tree"));
  1094. int icon_size = get_theme_constant(SNAME("class_icon_size"), EditorStringName(Editor));
  1095. int w = font->get_string_size(label, HORIZONTAL_ALIGNMENT_LEFT, -1, font_size).width;
  1096. if (icon.is_valid()) {
  1097. w += hs + icon_size;
  1098. }
  1099. w = MIN(w, get_size().width - sb->get_minimum_size().width);
  1100. int ofs = (get_size().width - w) / 2;
  1101. float v_margin_offset = sb->get_content_margin(SIDE_TOP) - sb->get_content_margin(SIDE_BOTTOM);
  1102. if (icon.is_valid()) {
  1103. Size2 rect_size = Size2(icon_size, icon_size);
  1104. Point2 rect_pos = Point2(ofs, (get_size().height - icon_size) / 2 + v_margin_offset).round();
  1105. if (is_layout_rtl()) {
  1106. rect_pos.x = get_size().width - rect_pos.x - icon_size;
  1107. }
  1108. draw_texture_rect(icon, Rect2(rect_pos, rect_size));
  1109. ofs += hs + icon_size;
  1110. w -= hs + icon_size;
  1111. }
  1112. Color color = get_theme_color(SceneStringName(font_color), SNAME("Tree"));
  1113. if (is_layout_rtl()) {
  1114. ofs = get_size().width - ofs - w;
  1115. }
  1116. float text_pos_y = font->get_ascent(font_size) + (get_size().height - font->get_height(font_size)) / 2 + v_margin_offset;
  1117. Point2 text_pos = Point2(ofs, text_pos_y).round();
  1118. draw_string(font, text_pos, label, HORIZONTAL_ALIGNMENT_LEFT, w, font_size, color);
  1119. } break;
  1120. }
  1121. }
  1122. Control *EditorInspectorCategory::make_custom_tooltip(const String &p_text) const {
  1123. // If it's not a doc tooltip, fallback to the default one.
  1124. if (doc_class_name.is_empty()) {
  1125. return nullptr;
  1126. }
  1127. return EditorHelpBitTooltip::show_tooltip(const_cast<EditorInspectorCategory *>(this), p_text);
  1128. }
  1129. void EditorInspectorCategory::set_as_favorite(EditorInspector *p_for_inspector) {
  1130. is_favorite = true;
  1131. menu = memnew(PopupMenu);
  1132. menu->add_item(TTR("Unfavorite All"), EditorInspector::MENU_UNFAVORITE_ALL);
  1133. add_child(menu);
  1134. menu->connect(SceneStringName(id_pressed), callable_mp(p_for_inspector, &EditorInspector::_handle_menu_option));
  1135. }
  1136. Size2 EditorInspectorCategory::get_minimum_size() const {
  1137. Ref<Font> font = get_theme_font(SNAME("bold"), EditorStringName(EditorFonts));
  1138. int font_size = get_theme_font_size(SNAME("bold_size"), EditorStringName(EditorFonts));
  1139. Size2 ms;
  1140. ms.height = font->get_height(font_size);
  1141. if (icon.is_valid()) {
  1142. int icon_size = get_theme_constant(SNAME("class_icon_size"), EditorStringName(Editor));
  1143. ms.height = MAX(icon_size, ms.height);
  1144. }
  1145. ms.height += get_theme_constant(SNAME("v_separation"), SNAME("Tree"));
  1146. const Ref<StyleBox> &bg_style = get_theme_stylebox(SNAME("bg"));
  1147. ms.height += bg_style->get_content_margin(SIDE_TOP) + bg_style->get_content_margin(SIDE_BOTTOM);
  1148. return ms;
  1149. }
  1150. void EditorInspectorCategory::_handle_menu_option(int p_option) {
  1151. switch (p_option) {
  1152. case MENU_OPEN_DOCS:
  1153. ScriptEditor::get_singleton()->goto_help("class:" + doc_class_name);
  1154. EditorNode::get_singleton()->get_editor_main_screen()->select(EditorMainScreen::EDITOR_SCRIPT);
  1155. break;
  1156. }
  1157. }
  1158. void EditorInspectorCategory::gui_input(const Ref<InputEvent> &p_event) {
  1159. if (!is_favorite && doc_class_name.is_empty()) {
  1160. return;
  1161. }
  1162. const Ref<InputEventMouseButton> &mb_event = p_event;
  1163. if (mb_event.is_null() || !mb_event->is_pressed() || mb_event->get_button_index() != MouseButton::RIGHT) {
  1164. return;
  1165. }
  1166. if (!is_favorite) {
  1167. if (!menu) {
  1168. menu = memnew(PopupMenu);
  1169. menu->add_icon_item(get_editor_theme_icon(SNAME("Help")), TTR("Open Documentation"), MENU_OPEN_DOCS);
  1170. add_child(menu);
  1171. menu->connect(SceneStringName(id_pressed), callable_mp(this, &EditorInspectorCategory::_handle_menu_option));
  1172. }
  1173. menu->set_item_disabled(menu->get_item_index(MENU_OPEN_DOCS), !EditorHelp::get_doc_data()->class_list.has(doc_class_name));
  1174. }
  1175. menu->set_position(get_screen_position() + mb_event->get_position());
  1176. menu->reset_size();
  1177. menu->popup();
  1178. }
  1179. ////////////////////////////////////////////////
  1180. ////////////////////////////////////////////////
  1181. void EditorInspectorSection::_test_unfold() {
  1182. if (!vbox_added) {
  1183. add_child(vbox);
  1184. move_child(vbox, 0);
  1185. vbox_added = true;
  1186. }
  1187. }
  1188. Ref<Texture2D> EditorInspectorSection::_get_arrow() {
  1189. Ref<Texture2D> arrow;
  1190. if (foldable) {
  1191. if (object->editor_is_section_unfolded(section)) {
  1192. arrow = get_theme_icon(SNAME("arrow"), SNAME("Tree"));
  1193. } else {
  1194. if (is_layout_rtl()) {
  1195. arrow = get_theme_icon(SNAME("arrow_collapsed_mirrored"), SNAME("Tree"));
  1196. } else {
  1197. arrow = get_theme_icon(SNAME("arrow_collapsed"), SNAME("Tree"));
  1198. }
  1199. }
  1200. }
  1201. return arrow;
  1202. }
  1203. int EditorInspectorSection::_get_header_height() {
  1204. Ref<Font> font = get_theme_font(SNAME("bold"), EditorStringName(EditorFonts));
  1205. int font_size = get_theme_font_size(SNAME("bold_size"), EditorStringName(EditorFonts));
  1206. int header_height = font->get_height(font_size);
  1207. Ref<Texture2D> arrow = _get_arrow();
  1208. if (arrow.is_valid()) {
  1209. header_height = MAX(header_height, arrow->get_height());
  1210. }
  1211. header_height += get_theme_constant(SNAME("v_separation"), SNAME("Tree"));
  1212. return header_height;
  1213. }
  1214. void EditorInspectorSection::_notification(int p_what) {
  1215. switch (p_what) {
  1216. case NOTIFICATION_THEME_CHANGED: {
  1217. update_minimum_size();
  1218. bg_color = get_theme_color(SNAME("prop_subsection"), EditorStringName(Editor));
  1219. bg_color.a /= level;
  1220. } break;
  1221. case NOTIFICATION_SORT_CHILDREN: {
  1222. if (!vbox_added) {
  1223. return;
  1224. }
  1225. int inspector_margin = get_theme_constant(SNAME("inspector_margin"), EditorStringName(Editor));
  1226. int section_indent_size = get_theme_constant(SNAME("indent_size"), SNAME("EditorInspectorSection"));
  1227. if (indent_depth > 0 && section_indent_size > 0) {
  1228. inspector_margin += indent_depth * section_indent_size;
  1229. }
  1230. Ref<StyleBoxFlat> section_indent_style = get_theme_stylebox(SNAME("indent_box"), SNAME("EditorInspectorSection"));
  1231. if (indent_depth > 0 && section_indent_style.is_valid()) {
  1232. inspector_margin += section_indent_style->get_margin(SIDE_LEFT) + section_indent_style->get_margin(SIDE_RIGHT);
  1233. }
  1234. Size2 size = get_size() - Vector2(inspector_margin, 0);
  1235. int header_height = _get_header_height();
  1236. Vector2 offset = Vector2(is_layout_rtl() ? 0 : inspector_margin, header_height);
  1237. for (int i = 0; i < get_child_count(); i++) {
  1238. Control *c = as_sortable_control(get_child(i));
  1239. if (!c) {
  1240. continue;
  1241. }
  1242. fit_child_in_rect(c, Rect2(offset, size));
  1243. }
  1244. } break;
  1245. case NOTIFICATION_DRAW: {
  1246. int section_indent = 0;
  1247. int section_indent_size = get_theme_constant(SNAME("indent_size"), SNAME("EditorInspectorSection"));
  1248. if (indent_depth > 0 && section_indent_size > 0) {
  1249. section_indent = indent_depth * section_indent_size;
  1250. }
  1251. Ref<StyleBoxFlat> section_indent_style = get_theme_stylebox(SNAME("indent_box"), SNAME("EditorInspectorSection"));
  1252. if (indent_depth > 0 && section_indent_style.is_valid()) {
  1253. section_indent += section_indent_style->get_margin(SIDE_LEFT) + section_indent_style->get_margin(SIDE_RIGHT);
  1254. }
  1255. int header_width = get_size().width - section_indent;
  1256. int header_offset_x = 0.0;
  1257. bool rtl = is_layout_rtl();
  1258. if (!rtl) {
  1259. header_offset_x += section_indent;
  1260. }
  1261. // Draw header area.
  1262. int header_height = _get_header_height();
  1263. Rect2 header_rect = Rect2(Vector2(header_offset_x, 0.0), Vector2(header_width, header_height));
  1264. Color c = bg_color;
  1265. c.a *= 0.4;
  1266. if (foldable && header_rect.has_point(get_local_mouse_position())) {
  1267. c = c.lightened(Input::get_singleton()->is_mouse_button_pressed(MouseButton::LEFT) ? -0.05 : 0.2);
  1268. }
  1269. draw_rect(header_rect, c);
  1270. // Draw header title, folding arrow and count of revertable properties.
  1271. {
  1272. int outer_margin = Math::round(2 * EDSCALE);
  1273. int separation = get_theme_constant(SNAME("h_separation"), SNAME("EditorInspectorSection"));
  1274. int margin_start = section_indent + outer_margin;
  1275. int margin_end = outer_margin;
  1276. // - Arrow.
  1277. Ref<Texture2D> arrow = _get_arrow();
  1278. if (arrow.is_valid()) {
  1279. Point2 arrow_position;
  1280. if (rtl) {
  1281. arrow_position.x = get_size().width - (margin_start + arrow->get_width());
  1282. } else {
  1283. arrow_position.x = margin_start;
  1284. }
  1285. arrow_position.y = (header_height - arrow->get_height()) / 2;
  1286. draw_texture(arrow, arrow_position);
  1287. margin_start += arrow->get_width() + separation;
  1288. }
  1289. int available = get_size().width - (margin_start + margin_end);
  1290. // - Count of revertable properties.
  1291. String num_revertable_str;
  1292. int num_revertable_width = 0;
  1293. bool folded = foldable && !object->editor_is_section_unfolded(section);
  1294. Ref<Font> font = get_theme_font(SNAME("bold"), EditorStringName(EditorFonts));
  1295. int font_size = get_theme_font_size(SNAME("bold_size"), EditorStringName(EditorFonts));
  1296. Color font_color = get_theme_color(SceneStringName(font_color), EditorStringName(Editor));
  1297. if (folded && revertable_properties.size()) {
  1298. int label_width = font->get_string_size(label, HORIZONTAL_ALIGNMENT_LEFT, available, font_size, TextServer::JUSTIFICATION_KASHIDA | TextServer::JUSTIFICATION_CONSTRAIN_ELLIPSIS).x;
  1299. Ref<Font> light_font = get_theme_font(SNAME("main"), EditorStringName(EditorFonts));
  1300. int light_font_size = get_theme_font_size(SNAME("main_size"), EditorStringName(EditorFonts));
  1301. Color light_font_color = get_theme_color(SNAME("font_disabled_color"), EditorStringName(Editor));
  1302. // Can we fit the long version of the revertable count text?
  1303. num_revertable_str = vformat(TTRN("(%d change)", "(%d changes)", revertable_properties.size()), revertable_properties.size());
  1304. num_revertable_width = light_font->get_string_size(num_revertable_str, HORIZONTAL_ALIGNMENT_LEFT, -1.0f, light_font_size, TextServer::JUSTIFICATION_NONE).x;
  1305. if (label_width + outer_margin + num_revertable_width > available) {
  1306. // We'll have to use the short version.
  1307. num_revertable_str = vformat("(%d)", revertable_properties.size());
  1308. num_revertable_width = light_font->get_string_size(num_revertable_str, HORIZONTAL_ALIGNMENT_LEFT, -1.0f, light_font_size, TextServer::JUSTIFICATION_NONE).x;
  1309. }
  1310. float text_offset_y = light_font->get_ascent(light_font_size) + (header_height - light_font->get_height(light_font_size)) / 2;
  1311. Point2 text_offset = Point2(margin_end, text_offset_y).round();
  1312. if (!rtl) {
  1313. text_offset.x = get_size().width - (text_offset.x + num_revertable_width);
  1314. }
  1315. draw_string(light_font, text_offset, num_revertable_str, HORIZONTAL_ALIGNMENT_LEFT, -1.0f, light_font_size, light_font_color, TextServer::JUSTIFICATION_NONE);
  1316. margin_end += num_revertable_width + outer_margin;
  1317. available -= num_revertable_width + outer_margin;
  1318. }
  1319. // - Label.
  1320. float text_offset_y = font->get_ascent(font_size) + (header_height - font->get_height(font_size)) / 2;
  1321. Point2 text_offset = Point2(margin_start, text_offset_y).round();
  1322. if (rtl) {
  1323. text_offset.x = margin_end;
  1324. }
  1325. HorizontalAlignment text_align = rtl ? HORIZONTAL_ALIGNMENT_RIGHT : HORIZONTAL_ALIGNMENT_LEFT;
  1326. draw_string(font, text_offset, label, text_align, available, font_size, font_color, TextServer::JUSTIFICATION_KASHIDA | TextServer::JUSTIFICATION_CONSTRAIN_ELLIPSIS);
  1327. }
  1328. // Draw section indentation.
  1329. if (section_indent_style.is_valid() && section_indent > 0) {
  1330. Rect2 indent_rect = Rect2(Vector2(), Vector2(indent_depth * section_indent_size, get_size().height));
  1331. if (rtl) {
  1332. indent_rect.position.x = get_size().width - section_indent + section_indent_style->get_margin(SIDE_RIGHT);
  1333. } else {
  1334. indent_rect.position.x = section_indent_style->get_margin(SIDE_LEFT);
  1335. }
  1336. draw_style_box(section_indent_style, indent_rect);
  1337. }
  1338. } break;
  1339. case NOTIFICATION_DRAG_BEGIN: {
  1340. dropping_for_unfold = true;
  1341. } break;
  1342. case NOTIFICATION_DRAG_END: {
  1343. dropping_for_unfold = false;
  1344. } break;
  1345. case NOTIFICATION_MOUSE_ENTER: {
  1346. if (dropping_for_unfold) {
  1347. dropping_unfold_timer->start();
  1348. }
  1349. queue_redraw();
  1350. } break;
  1351. case NOTIFICATION_MOUSE_EXIT: {
  1352. if (dropping_for_unfold) {
  1353. dropping_unfold_timer->stop();
  1354. }
  1355. queue_redraw();
  1356. } break;
  1357. }
  1358. }
  1359. Size2 EditorInspectorSection::get_minimum_size() const {
  1360. Size2 ms;
  1361. for (int i = 0; i < get_child_count(); i++) {
  1362. Control *c = as_sortable_control(get_child(i));
  1363. if (!c) {
  1364. continue;
  1365. }
  1366. Size2 minsize = c->get_combined_minimum_size();
  1367. ms = ms.max(minsize);
  1368. }
  1369. Ref<Font> font = get_theme_font(SceneStringName(font), SNAME("Tree"));
  1370. int font_size = get_theme_font_size(SceneStringName(font_size), SNAME("Tree"));
  1371. ms.height += font->get_height(font_size) + get_theme_constant(SNAME("v_separation"), SNAME("Tree"));
  1372. ms.width += get_theme_constant(SNAME("inspector_margin"), EditorStringName(Editor));
  1373. int section_indent_size = get_theme_constant(SNAME("indent_size"), SNAME("EditorInspectorSection"));
  1374. if (indent_depth > 0 && section_indent_size > 0) {
  1375. ms.width += indent_depth * section_indent_size;
  1376. }
  1377. Ref<StyleBoxFlat> section_indent_style = get_theme_stylebox(SNAME("indent_box"), SNAME("EditorInspectorSection"));
  1378. if (indent_depth > 0 && section_indent_style.is_valid()) {
  1379. ms.width += section_indent_style->get_margin(SIDE_LEFT) + section_indent_style->get_margin(SIDE_RIGHT);
  1380. }
  1381. return ms;
  1382. }
  1383. void EditorInspectorSection::setup(const String &p_section, const String &p_label, Object *p_object, const Color &p_bg_color, bool p_foldable, int p_indent_depth, int p_level) {
  1384. section = p_section;
  1385. label = p_label;
  1386. object = p_object;
  1387. bg_color = p_bg_color;
  1388. foldable = p_foldable;
  1389. indent_depth = p_indent_depth;
  1390. level = p_level;
  1391. if (!foldable && !vbox_added) {
  1392. add_child(vbox);
  1393. move_child(vbox, 0);
  1394. vbox_added = true;
  1395. }
  1396. if (foldable) {
  1397. _test_unfold();
  1398. if (object->editor_is_section_unfolded(section)) {
  1399. vbox->show();
  1400. } else {
  1401. vbox->hide();
  1402. }
  1403. }
  1404. }
  1405. void EditorInspectorSection::gui_input(const Ref<InputEvent> &p_event) {
  1406. ERR_FAIL_COND(p_event.is_null());
  1407. if (!foldable) {
  1408. return;
  1409. }
  1410. Ref<InputEventMouseButton> mb = p_event;
  1411. if (mb.is_valid() && mb->is_pressed() && mb->get_button_index() == MouseButton::LEFT) {
  1412. if (object->editor_is_section_unfolded(section)) {
  1413. int header_height = _get_header_height();
  1414. if (mb->get_position().y >= header_height) {
  1415. return;
  1416. }
  1417. }
  1418. accept_event();
  1419. bool should_unfold = !object->editor_is_section_unfolded(section);
  1420. if (should_unfold) {
  1421. unfold();
  1422. } else {
  1423. fold();
  1424. }
  1425. } else if (mb.is_valid() && !mb->is_pressed()) {
  1426. queue_redraw();
  1427. }
  1428. }
  1429. String EditorInspectorSection::get_section() const {
  1430. return section;
  1431. }
  1432. VBoxContainer *EditorInspectorSection::get_vbox() {
  1433. return vbox;
  1434. }
  1435. void EditorInspectorSection::unfold() {
  1436. if (!foldable) {
  1437. return;
  1438. }
  1439. _test_unfold();
  1440. object->editor_set_section_unfold(section, true);
  1441. vbox->show();
  1442. queue_redraw();
  1443. }
  1444. void EditorInspectorSection::fold() {
  1445. if (!foldable) {
  1446. return;
  1447. }
  1448. if (!vbox_added) {
  1449. return;
  1450. }
  1451. object->editor_set_section_unfold(section, false);
  1452. vbox->hide();
  1453. queue_redraw();
  1454. }
  1455. void EditorInspectorSection::set_bg_color(const Color &p_bg_color) {
  1456. bg_color = p_bg_color;
  1457. queue_redraw();
  1458. }
  1459. bool EditorInspectorSection::has_revertable_properties() const {
  1460. return !revertable_properties.is_empty();
  1461. }
  1462. void EditorInspectorSection::property_can_revert_changed(const String &p_path, bool p_can_revert) {
  1463. bool had_revertable_properties = has_revertable_properties();
  1464. if (p_can_revert) {
  1465. revertable_properties.insert(p_path);
  1466. } else {
  1467. revertable_properties.erase(p_path);
  1468. }
  1469. if (has_revertable_properties() != had_revertable_properties) {
  1470. queue_redraw();
  1471. }
  1472. }
  1473. void EditorInspectorSection::_bind_methods() {
  1474. ClassDB::bind_method(D_METHOD("setup", "section", "label", "object", "bg_color", "foldable", "indent_depth", "level"), &EditorInspectorSection::setup, DEFVAL(0), DEFVAL(1));
  1475. ClassDB::bind_method(D_METHOD("get_vbox"), &EditorInspectorSection::get_vbox);
  1476. ClassDB::bind_method(D_METHOD("unfold"), &EditorInspectorSection::unfold);
  1477. ClassDB::bind_method(D_METHOD("fold"), &EditorInspectorSection::fold);
  1478. }
  1479. EditorInspectorSection::EditorInspectorSection() {
  1480. vbox = memnew(VBoxContainer);
  1481. dropping_unfold_timer = memnew(Timer);
  1482. dropping_unfold_timer->set_wait_time(0.6);
  1483. dropping_unfold_timer->set_one_shot(true);
  1484. add_child(dropping_unfold_timer);
  1485. dropping_unfold_timer->connect("timeout", callable_mp(this, &EditorInspectorSection::unfold));
  1486. }
  1487. EditorInspectorSection::~EditorInspectorSection() {
  1488. if (!vbox_added) {
  1489. memdelete(vbox);
  1490. }
  1491. }
  1492. ////////////////////////////////////////////////
  1493. ////////////////////////////////////////////////
  1494. int EditorInspectorArray::_get_array_count() {
  1495. if (mode == MODE_USE_MOVE_ARRAY_ELEMENT_FUNCTION) {
  1496. List<PropertyInfo> object_property_list;
  1497. object->get_property_list(&object_property_list);
  1498. return _extract_properties_as_array(object_property_list).size();
  1499. } else if (mode == MODE_USE_COUNT_PROPERTY) {
  1500. bool valid;
  1501. int count_val = object->get(count_property, &valid);
  1502. ERR_FAIL_COND_V_MSG(!valid, 0, vformat("%s is not a valid property to be used as array count.", count_property));
  1503. return count_val;
  1504. }
  1505. return 0;
  1506. }
  1507. void EditorInspectorArray::_add_button_pressed() {
  1508. _move_element(-1, -1);
  1509. }
  1510. void EditorInspectorArray::_paginator_page_changed(int p_page) {
  1511. emit_signal("page_change_request", p_page);
  1512. }
  1513. void EditorInspectorArray::_rmb_popup_id_pressed(int p_id) {
  1514. switch (p_id) {
  1515. case OPTION_MOVE_UP:
  1516. if (popup_array_index_pressed > 0) {
  1517. _move_element(popup_array_index_pressed, popup_array_index_pressed - 1);
  1518. }
  1519. break;
  1520. case OPTION_MOVE_DOWN:
  1521. if (popup_array_index_pressed < count - 1) {
  1522. _move_element(popup_array_index_pressed, popup_array_index_pressed + 2);
  1523. }
  1524. break;
  1525. case OPTION_NEW_BEFORE:
  1526. _move_element(-1, popup_array_index_pressed);
  1527. break;
  1528. case OPTION_NEW_AFTER:
  1529. _move_element(-1, popup_array_index_pressed + 1);
  1530. break;
  1531. case OPTION_REMOVE:
  1532. _move_element(popup_array_index_pressed, -1);
  1533. break;
  1534. case OPTION_CLEAR_ARRAY:
  1535. _clear_array();
  1536. break;
  1537. case OPTION_RESIZE_ARRAY:
  1538. new_size_spin_box->set_value(count);
  1539. resize_dialog->get_ok_button()->set_disabled(true);
  1540. resize_dialog->popup_centered(Size2(250, 0) * EDSCALE);
  1541. new_size_spin_box->get_line_edit()->grab_focus();
  1542. new_size_spin_box->get_line_edit()->select_all();
  1543. break;
  1544. default:
  1545. break;
  1546. }
  1547. }
  1548. void EditorInspectorArray::_control_dropping_draw() {
  1549. int drop_position = _drop_position();
  1550. if (dropping && drop_position >= 0) {
  1551. Vector2 from;
  1552. Vector2 to;
  1553. if (drop_position < elements_vbox->get_child_count()) {
  1554. Transform2D xform = Object::cast_to<Control>(elements_vbox->get_child(drop_position))->get_transform();
  1555. from = xform.xform(Vector2());
  1556. to = xform.xform(Vector2(elements_vbox->get_size().x, 0));
  1557. } else {
  1558. Control *child = Object::cast_to<Control>(elements_vbox->get_child(drop_position - 1));
  1559. Transform2D xform = child->get_transform();
  1560. from = xform.xform(Vector2(0, child->get_size().y));
  1561. to = xform.xform(Vector2(elements_vbox->get_size().x, child->get_size().y));
  1562. }
  1563. Color color = get_theme_color(SNAME("accent_color"), EditorStringName(Editor));
  1564. control_dropping->draw_line(from, to, color, 2);
  1565. }
  1566. }
  1567. void EditorInspectorArray::_vbox_visibility_changed() {
  1568. control_dropping->set_visible(vbox->is_visible_in_tree());
  1569. }
  1570. void EditorInspectorArray::_panel_draw(int p_index) {
  1571. ERR_FAIL_INDEX(p_index, (int)array_elements.size());
  1572. Ref<StyleBox> style = get_theme_stylebox(SNAME("Focus"), EditorStringName(EditorStyles));
  1573. if (!style.is_valid()) {
  1574. return;
  1575. }
  1576. if (array_elements[p_index].panel->has_focus()) {
  1577. array_elements[p_index].panel->draw_style_box(style, Rect2(Vector2(), array_elements[p_index].panel->get_size()));
  1578. }
  1579. }
  1580. void EditorInspectorArray::_panel_gui_input(Ref<InputEvent> p_event, int p_index) {
  1581. ERR_FAIL_INDEX(p_index, (int)array_elements.size());
  1582. if (read_only) {
  1583. return;
  1584. }
  1585. Ref<InputEventKey> key_ref = p_event;
  1586. if (key_ref.is_valid()) {
  1587. const InputEventKey &key = **key_ref;
  1588. if (array_elements[p_index].panel->has_focus() && key.is_pressed() && key.get_keycode() == Key::KEY_DELETE) {
  1589. _move_element(begin_array_index + p_index, -1);
  1590. array_elements[p_index].panel->accept_event();
  1591. }
  1592. }
  1593. Ref<InputEventMouseButton> mb = p_event;
  1594. if (mb.is_valid()) {
  1595. if (movable && mb->get_button_index() == MouseButton::RIGHT) {
  1596. array_elements[p_index].panel->accept_event();
  1597. popup_array_index_pressed = begin_array_index + p_index;
  1598. rmb_popup->set_item_disabled(OPTION_MOVE_UP, popup_array_index_pressed == 0);
  1599. rmb_popup->set_item_disabled(OPTION_MOVE_DOWN, popup_array_index_pressed == count - 1);
  1600. rmb_popup->set_position(get_screen_position() + mb->get_position());
  1601. rmb_popup->reset_size();
  1602. rmb_popup->popup();
  1603. }
  1604. }
  1605. }
  1606. void EditorInspectorArray::_move_element(int p_element_index, int p_to_pos) {
  1607. String action_name;
  1608. if (p_element_index < 0) {
  1609. action_name = vformat(TTR("Add element to property array with prefix %s."), array_element_prefix);
  1610. } else if (p_to_pos < 0) {
  1611. action_name = vformat(TTR("Remove element %d from property array with prefix %s."), p_element_index, array_element_prefix);
  1612. } else {
  1613. action_name = vformat(TTR("Move element %d to position %d in property array with prefix %s."), p_element_index, p_to_pos, array_element_prefix);
  1614. }
  1615. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  1616. undo_redo->create_action(action_name);
  1617. if (mode == MODE_USE_MOVE_ARRAY_ELEMENT_FUNCTION) {
  1618. // Call the function.
  1619. Callable move_function = EditorNode::get_editor_data().get_move_array_element_function(object->get_class_name());
  1620. if (move_function.is_valid()) {
  1621. move_function.call(undo_redo, object, array_element_prefix, p_element_index, p_to_pos);
  1622. } else {
  1623. WARN_PRINT(vformat("Could not find a function to move arrays elements for class %s. Register a move element function using EditorData::add_move_array_element_function", object->get_class_name()));
  1624. }
  1625. } else if (mode == MODE_USE_COUNT_PROPERTY) {
  1626. ERR_FAIL_COND(p_to_pos < -1 || p_to_pos > count);
  1627. if (!swap_method.is_empty()) {
  1628. ERR_FAIL_COND(!object->has_method(swap_method));
  1629. // Swap method was provided, use it.
  1630. if (p_element_index < 0) {
  1631. // Add an element at position
  1632. undo_redo->add_do_property(object, count_property, count + 1);
  1633. if (p_to_pos >= 0) {
  1634. for (int i = count; i > p_to_pos; i--) {
  1635. undo_redo->add_do_method(object, swap_method, i, i - 1);
  1636. }
  1637. for (int i = p_to_pos; i < count; i++) {
  1638. undo_redo->add_undo_method(object, swap_method, i, i + 1);
  1639. }
  1640. }
  1641. undo_redo->add_undo_property(object, count_property, count);
  1642. } else if (p_to_pos < 0) {
  1643. if (count > 0) {
  1644. // Remove element at position
  1645. undo_redo->add_undo_property(object, count_property, count);
  1646. List<PropertyInfo> object_property_list;
  1647. object->get_property_list(&object_property_list);
  1648. for (int i = p_element_index; i < count - 1; i++) {
  1649. undo_redo->add_do_method(object, swap_method, i, i + 1);
  1650. }
  1651. for (int i = count; i > p_element_index; i--) {
  1652. undo_redo->add_undo_method(object, swap_method, i, i - 1);
  1653. }
  1654. String erase_prefix = String(array_element_prefix) + itos(p_element_index);
  1655. for (const PropertyInfo &E : object_property_list) {
  1656. if (E.name.begins_with(erase_prefix)) {
  1657. undo_redo->add_undo_property(object, E.name, object->get(E.name));
  1658. }
  1659. }
  1660. undo_redo->add_do_property(object, count_property, count - 1);
  1661. }
  1662. } else {
  1663. if (p_to_pos > p_element_index) {
  1664. p_to_pos--;
  1665. }
  1666. if (p_to_pos < p_element_index) {
  1667. for (int i = p_element_index; i > p_to_pos; i--) {
  1668. undo_redo->add_do_method(object, swap_method, i, i - 1);
  1669. }
  1670. for (int i = p_to_pos; i < p_element_index; i++) {
  1671. undo_redo->add_undo_method(object, swap_method, i, i + 1);
  1672. }
  1673. } else if (p_to_pos > p_element_index) {
  1674. for (int i = p_element_index; i < p_to_pos; i++) {
  1675. undo_redo->add_do_method(object, swap_method, i, i + 1);
  1676. }
  1677. for (int i = p_to_pos; i > p_element_index; i--) {
  1678. undo_redo->add_undo_method(object, swap_method, i, i - 1);
  1679. }
  1680. }
  1681. }
  1682. } else {
  1683. // Use standard properties.
  1684. List<PropertyInfo> object_property_list;
  1685. object->get_property_list(&object_property_list);
  1686. Array properties_as_array = _extract_properties_as_array(object_property_list);
  1687. properties_as_array.resize(count);
  1688. // For undoing things
  1689. undo_redo->add_undo_property(object, count_property, properties_as_array.size());
  1690. for (int i = 0; i < (int)properties_as_array.size(); i++) {
  1691. Dictionary d = Dictionary(properties_as_array[i]);
  1692. Array keys = d.keys();
  1693. for (int j = 0; j < keys.size(); j++) {
  1694. String key = keys[j];
  1695. undo_redo->add_undo_property(object, vformat(key, i), d[key]);
  1696. }
  1697. }
  1698. if (p_element_index < 0) {
  1699. // Add an element.
  1700. properties_as_array.insert(p_to_pos < 0 ? properties_as_array.size() : p_to_pos, Dictionary());
  1701. } else if (p_to_pos < 0) {
  1702. // Delete the element.
  1703. properties_as_array.remove_at(p_element_index);
  1704. } else {
  1705. // Move the element.
  1706. properties_as_array.insert(p_to_pos, properties_as_array[p_element_index].duplicate());
  1707. properties_as_array.remove_at(p_to_pos < p_element_index ? p_element_index + 1 : p_element_index);
  1708. }
  1709. // Change the array size then set the properties.
  1710. undo_redo->add_do_property(object, count_property, properties_as_array.size());
  1711. for (int i = 0; i < (int)properties_as_array.size(); i++) {
  1712. Dictionary d = properties_as_array[i];
  1713. Array keys = d.keys();
  1714. for (int j = 0; j < keys.size(); j++) {
  1715. String key = keys[j];
  1716. undo_redo->add_do_property(object, vformat(key, i), d[key]);
  1717. }
  1718. }
  1719. }
  1720. }
  1721. undo_redo->commit_action();
  1722. // Handle page change and update counts.
  1723. if (p_element_index < 0) {
  1724. int added_index = p_to_pos < 0 ? count : p_to_pos;
  1725. emit_signal(SNAME("page_change_request"), added_index / page_length);
  1726. count += 1;
  1727. } else if (p_to_pos < 0) {
  1728. count -= 1;
  1729. if (page == max_page && (MAX(0, count - 1) / page_length != max_page)) {
  1730. emit_signal(SNAME("page_change_request"), max_page - 1);
  1731. }
  1732. } else if (p_to_pos == begin_array_index - 1) {
  1733. emit_signal(SNAME("page_change_request"), page - 1);
  1734. } else if (p_to_pos > end_array_index) {
  1735. emit_signal(SNAME("page_change_request"), page + 1);
  1736. }
  1737. begin_array_index = page * page_length;
  1738. end_array_index = MIN(count, (page + 1) * page_length);
  1739. max_page = MAX(0, count - 1) / page_length;
  1740. }
  1741. void EditorInspectorArray::_clear_array() {
  1742. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  1743. undo_redo->create_action(vformat(TTR("Clear Property Array with Prefix %s"), array_element_prefix));
  1744. if (mode == MODE_USE_MOVE_ARRAY_ELEMENT_FUNCTION) {
  1745. for (int i = count - 1; i >= 0; i--) {
  1746. // Call the function.
  1747. Callable move_function = EditorNode::get_editor_data().get_move_array_element_function(object->get_class_name());
  1748. if (move_function.is_valid()) {
  1749. move_function.call(undo_redo, object, array_element_prefix, i, -1);
  1750. } else {
  1751. WARN_PRINT(vformat("Could not find a function to move arrays elements for class %s. Register a move element function using EditorData::add_move_array_element_function", object->get_class_name()));
  1752. }
  1753. }
  1754. } else if (mode == MODE_USE_COUNT_PROPERTY) {
  1755. List<PropertyInfo> object_property_list;
  1756. object->get_property_list(&object_property_list);
  1757. Array properties_as_array = _extract_properties_as_array(object_property_list);
  1758. properties_as_array.resize(count);
  1759. // For undoing things
  1760. undo_redo->add_undo_property(object, count_property, count);
  1761. for (int i = 0; i < (int)properties_as_array.size(); i++) {
  1762. Dictionary d = Dictionary(properties_as_array[i]);
  1763. Array keys = d.keys();
  1764. for (int j = 0; j < keys.size(); j++) {
  1765. String key = keys[j];
  1766. undo_redo->add_undo_property(object, vformat(key, i), d[key]);
  1767. }
  1768. }
  1769. // Change the array size then set the properties.
  1770. undo_redo->add_do_property(object, count_property, 0);
  1771. }
  1772. undo_redo->commit_action();
  1773. // Handle page change and update counts.
  1774. emit_signal(SNAME("page_change_request"), 0);
  1775. count = 0;
  1776. begin_array_index = 0;
  1777. end_array_index = 0;
  1778. max_page = 0;
  1779. }
  1780. void EditorInspectorArray::_resize_array(int p_size) {
  1781. ERR_FAIL_COND(p_size < 0);
  1782. if (p_size == count) {
  1783. return;
  1784. }
  1785. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  1786. undo_redo->create_action(vformat(TTR("Resize Property Array with Prefix %s"), array_element_prefix));
  1787. if (p_size > count) {
  1788. if (mode == MODE_USE_MOVE_ARRAY_ELEMENT_FUNCTION) {
  1789. for (int i = count; i < p_size; i++) {
  1790. // Call the function.
  1791. Callable move_function = EditorNode::get_editor_data().get_move_array_element_function(object->get_class_name());
  1792. if (move_function.is_valid()) {
  1793. move_function.call(undo_redo, object, array_element_prefix, -1, -1);
  1794. } else {
  1795. WARN_PRINT(vformat("Could not find a function to move arrays elements for class %s. Register a move element function using EditorData::add_move_array_element_function", object->get_class_name()));
  1796. }
  1797. }
  1798. } else if (mode == MODE_USE_COUNT_PROPERTY) {
  1799. undo_redo->add_undo_property(object, count_property, count);
  1800. undo_redo->add_do_property(object, count_property, p_size);
  1801. }
  1802. } else {
  1803. if (mode == MODE_USE_MOVE_ARRAY_ELEMENT_FUNCTION) {
  1804. for (int i = count - 1; i > p_size - 1; i--) {
  1805. // Call the function.
  1806. Callable move_function = EditorNode::get_editor_data().get_move_array_element_function(object->get_class_name());
  1807. if (move_function.is_valid()) {
  1808. move_function.call(undo_redo, object, array_element_prefix, i, -1);
  1809. } else {
  1810. WARN_PRINT(vformat("Could not find a function to move arrays elements for class %s. Register a move element function using EditorData::add_move_array_element_function", object->get_class_name()));
  1811. }
  1812. }
  1813. } else if (mode == MODE_USE_COUNT_PROPERTY) {
  1814. List<PropertyInfo> object_property_list;
  1815. object->get_property_list(&object_property_list);
  1816. Array properties_as_array = _extract_properties_as_array(object_property_list);
  1817. properties_as_array.resize(count);
  1818. // For undoing things
  1819. undo_redo->add_undo_property(object, count_property, count);
  1820. for (int i = count - 1; i > p_size - 1; i--) {
  1821. Dictionary d = Dictionary(properties_as_array[i]);
  1822. Array keys = d.keys();
  1823. for (int j = 0; j < keys.size(); j++) {
  1824. String key = keys[j];
  1825. undo_redo->add_undo_property(object, vformat(key, i), d[key]);
  1826. }
  1827. }
  1828. // Change the array size then set the properties.
  1829. undo_redo->add_do_property(object, count_property, p_size);
  1830. }
  1831. }
  1832. undo_redo->commit_action();
  1833. // Handle page change and update counts.
  1834. emit_signal(SNAME("page_change_request"), 0);
  1835. /*
  1836. count = 0;
  1837. begin_array_index = 0;
  1838. end_array_index = 0;
  1839. max_page = 0;
  1840. */
  1841. }
  1842. Array EditorInspectorArray::_extract_properties_as_array(const List<PropertyInfo> &p_list) {
  1843. Array output;
  1844. for (const PropertyInfo &pi : p_list) {
  1845. if (!(pi.usage & PROPERTY_USAGE_EDITOR)) {
  1846. continue;
  1847. }
  1848. if (pi.name.begins_with(array_element_prefix)) {
  1849. String str = pi.name.trim_prefix(array_element_prefix);
  1850. int to_char_index = 0;
  1851. while (to_char_index < str.length()) {
  1852. if (!is_digit(str[to_char_index])) {
  1853. break;
  1854. }
  1855. to_char_index++;
  1856. }
  1857. if (to_char_index > 0) {
  1858. int array_index = str.left(to_char_index).to_int();
  1859. Error error = OK;
  1860. if (array_index >= output.size()) {
  1861. error = output.resize(array_index + 1);
  1862. }
  1863. if (error == OK) {
  1864. String format_string = String(array_element_prefix) + "%d" + str.substr(to_char_index);
  1865. Dictionary dict = output[array_index];
  1866. dict[format_string] = object->get(pi.name);
  1867. output[array_index] = dict;
  1868. } else {
  1869. WARN_PRINT(vformat("Array element %s has an index too high. Array allocation failed.", pi.name));
  1870. }
  1871. }
  1872. }
  1873. }
  1874. return output;
  1875. }
  1876. int EditorInspectorArray::_drop_position() const {
  1877. for (int i = 0; i < (int)array_elements.size(); i++) {
  1878. const ArrayElement &ae = array_elements[i];
  1879. Size2 size = ae.panel->get_size();
  1880. Vector2 mp = ae.panel->get_local_mouse_position();
  1881. if (Rect2(Vector2(), size).has_point(mp)) {
  1882. if (mp.y < size.y / 2) {
  1883. return i;
  1884. } else {
  1885. return i + 1;
  1886. }
  1887. }
  1888. }
  1889. return -1;
  1890. }
  1891. void EditorInspectorArray::_resize_dialog_confirmed() {
  1892. if (int(new_size_spin_box->get_value()) == count) {
  1893. return;
  1894. }
  1895. resize_dialog->hide();
  1896. _resize_array(int(new_size_spin_box->get_value()));
  1897. }
  1898. void EditorInspectorArray::_new_size_spin_box_value_changed(float p_value) {
  1899. resize_dialog->get_ok_button()->set_disabled(int(p_value) == count);
  1900. }
  1901. void EditorInspectorArray::_new_size_spin_box_text_submitted(const String &p_text) {
  1902. _resize_dialog_confirmed();
  1903. }
  1904. void EditorInspectorArray::_setup() {
  1905. // Setup counts.
  1906. count = _get_array_count();
  1907. begin_array_index = page * page_length;
  1908. end_array_index = MIN(count, (page + 1) * page_length);
  1909. max_page = MAX(0, count - 1) / page_length;
  1910. array_elements.resize(MAX(0, end_array_index - begin_array_index));
  1911. if (page < 0 || page > max_page) {
  1912. WARN_PRINT(vformat("Invalid page number %d", page));
  1913. page = CLAMP(page, 0, max_page);
  1914. }
  1915. Ref<Font> numbers_font;
  1916. int numbers_min_w = 0;
  1917. if (numbered) {
  1918. numbers_font = get_theme_font(SNAME("bold"), EditorStringName(EditorFonts));
  1919. int digits_found = count;
  1920. String test;
  1921. while (digits_found) {
  1922. test += "8";
  1923. digits_found /= 10;
  1924. }
  1925. numbers_min_w = numbers_font->get_string_size(test).width;
  1926. }
  1927. for (int i = 0; i < (int)array_elements.size(); i++) {
  1928. ArrayElement &ae = array_elements[i];
  1929. // Panel and its hbox.
  1930. ae.panel = memnew(PanelContainer);
  1931. ae.panel->set_focus_mode(FOCUS_ALL);
  1932. ae.panel->set_mouse_filter(MOUSE_FILTER_PASS);
  1933. SET_DRAG_FORWARDING_GCD(ae.panel, EditorInspectorArray);
  1934. int element_position = begin_array_index + i;
  1935. ae.panel->set_meta("index", element_position);
  1936. ae.panel->set_tooltip_text(vformat(TTR("Element %d: %s%d*"), element_position, array_element_prefix, element_position));
  1937. ae.panel->connect(SceneStringName(focus_entered), callable_mp((CanvasItem *)ae.panel, &PanelContainer::queue_redraw));
  1938. ae.panel->connect(SceneStringName(focus_exited), callable_mp((CanvasItem *)ae.panel, &PanelContainer::queue_redraw));
  1939. ae.panel->connect(SceneStringName(draw), callable_mp(this, &EditorInspectorArray::_panel_draw).bind(i));
  1940. ae.panel->connect(SceneStringName(gui_input), callable_mp(this, &EditorInspectorArray::_panel_gui_input).bind(i));
  1941. ae.panel->add_theme_style_override(SceneStringName(panel), i % 2 ? odd_style : even_style);
  1942. elements_vbox->add_child(ae.panel);
  1943. ae.margin = memnew(MarginContainer);
  1944. ae.margin->set_mouse_filter(MOUSE_FILTER_PASS);
  1945. if (is_inside_tree()) {
  1946. Size2 min_size = get_theme_stylebox(SNAME("Focus"), EditorStringName(EditorStyles))->get_minimum_size();
  1947. ae.margin->begin_bulk_theme_override();
  1948. ae.margin->add_theme_constant_override("margin_left", min_size.x / 2);
  1949. ae.margin->add_theme_constant_override("margin_top", min_size.y / 2);
  1950. ae.margin->add_theme_constant_override("margin_right", min_size.x / 2);
  1951. ae.margin->add_theme_constant_override("margin_bottom", min_size.y / 2);
  1952. ae.margin->end_bulk_theme_override();
  1953. }
  1954. ae.panel->add_child(ae.margin);
  1955. ae.hbox = memnew(HBoxContainer);
  1956. ae.hbox->set_h_size_flags(SIZE_EXPAND_FILL);
  1957. ae.hbox->set_v_size_flags(SIZE_EXPAND_FILL);
  1958. ae.margin->add_child(ae.hbox);
  1959. // Move button.
  1960. if (movable) {
  1961. VBoxContainer *move_vbox = memnew(VBoxContainer);
  1962. move_vbox->set_v_size_flags(SIZE_EXPAND_FILL);
  1963. move_vbox->set_alignment(BoxContainer::ALIGNMENT_CENTER);
  1964. ae.hbox->add_child(move_vbox);
  1965. if (element_position > 0) {
  1966. ae.move_up = memnew(Button);
  1967. ae.move_up->set_button_icon(get_editor_theme_icon(SNAME("MoveUp")));
  1968. ae.move_up->connect(SceneStringName(pressed), callable_mp(this, &EditorInspectorArray::_move_element).bind(element_position, element_position - 1));
  1969. move_vbox->add_child(ae.move_up);
  1970. }
  1971. ae.move_texture_rect = memnew(TextureRect);
  1972. ae.move_texture_rect->set_stretch_mode(TextureRect::STRETCH_KEEP_CENTERED);
  1973. ae.move_texture_rect->set_default_cursor_shape(Control::CURSOR_MOVE);
  1974. if (is_inside_tree()) {
  1975. ae.move_texture_rect->set_texture(get_editor_theme_icon(SNAME("TripleBar")));
  1976. }
  1977. move_vbox->add_child(ae.move_texture_rect);
  1978. if (element_position < count - 1) {
  1979. ae.move_down = memnew(Button);
  1980. ae.move_down->set_button_icon(get_editor_theme_icon(SNAME("MoveDown")));
  1981. ae.move_down->connect(SceneStringName(pressed), callable_mp(this, &EditorInspectorArray::_move_element).bind(element_position, element_position + 2));
  1982. move_vbox->add_child(ae.move_down);
  1983. }
  1984. }
  1985. if (numbered) {
  1986. ae.number = memnew(Label);
  1987. ae.number->add_theme_font_override(SceneStringName(font), numbers_font);
  1988. ae.number->set_custom_minimum_size(Size2(numbers_min_w, 0));
  1989. ae.number->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_RIGHT);
  1990. ae.number->set_vertical_alignment(VERTICAL_ALIGNMENT_CENTER);
  1991. ae.number->set_text(itos(element_position));
  1992. ae.hbox->add_child(ae.number);
  1993. }
  1994. // Right vbox.
  1995. ae.vbox = memnew(VBoxContainer);
  1996. ae.vbox->set_h_size_flags(SIZE_EXPAND_FILL);
  1997. ae.vbox->set_v_size_flags(SIZE_EXPAND_FILL);
  1998. ae.hbox->add_child(ae.vbox);
  1999. ae.erase = memnew(Button);
  2000. ae.erase->set_button_icon(get_editor_theme_icon(SNAME("Remove")));
  2001. ae.erase->set_v_size_flags(SIZE_SHRINK_CENTER);
  2002. ae.erase->connect(SceneStringName(pressed), callable_mp(this, &EditorInspectorArray::_remove_item).bind(element_position));
  2003. ae.hbox->add_child(ae.erase);
  2004. }
  2005. // Hide/show the add button.
  2006. add_button->set_visible(page == max_page);
  2007. // Add paginator if there's more than 1 page.
  2008. if (max_page > 0) {
  2009. EditorPaginator *paginator = memnew(EditorPaginator);
  2010. paginator->update(page, max_page);
  2011. paginator->connect("page_changed", callable_mp(this, &EditorInspectorArray::_paginator_page_changed));
  2012. vbox->add_child(paginator);
  2013. }
  2014. }
  2015. void EditorInspectorArray::_remove_item(int p_index) {
  2016. _move_element(p_index, -1);
  2017. }
  2018. Variant EditorInspectorArray::get_drag_data_fw(const Point2 &p_point, Control *p_from) {
  2019. if (!movable) {
  2020. return Variant();
  2021. }
  2022. int index = p_from->get_meta("index");
  2023. Dictionary dict;
  2024. dict["type"] = "property_array_element";
  2025. dict["property_array_prefix"] = array_element_prefix;
  2026. dict["index"] = index;
  2027. return dict;
  2028. }
  2029. void EditorInspectorArray::drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) {
  2030. Dictionary dict = p_data;
  2031. int to_drop = dict["index"];
  2032. int drop_position = _drop_position();
  2033. if (drop_position < 0) {
  2034. return;
  2035. }
  2036. _move_element(to_drop, begin_array_index + drop_position);
  2037. }
  2038. bool EditorInspectorArray::can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) const {
  2039. if (!movable || read_only) {
  2040. return false;
  2041. }
  2042. // First, update drawing.
  2043. control_dropping->queue_redraw();
  2044. if (p_data.get_type() != Variant::DICTIONARY) {
  2045. return false;
  2046. }
  2047. Dictionary dict = p_data;
  2048. int drop_position = _drop_position();
  2049. if (!dict.has("type") || dict["type"] != "property_array_element" || String(dict["property_array_prefix"]) != array_element_prefix || drop_position < 0) {
  2050. return false;
  2051. }
  2052. // Check in dropping at the given index does indeed move the item.
  2053. int moved_array_index = (int)dict["index"];
  2054. int drop_array_index = begin_array_index + drop_position;
  2055. return drop_array_index != moved_array_index && drop_array_index - 1 != moved_array_index;
  2056. }
  2057. void EditorInspectorArray::_notification(int p_what) {
  2058. switch (p_what) {
  2059. case NOTIFICATION_ENTER_TREE:
  2060. case NOTIFICATION_THEME_CHANGED: {
  2061. Color color = get_theme_color(SNAME("dark_color_1"), EditorStringName(Editor));
  2062. odd_style->set_bg_color(color.darkened(-0.08));
  2063. even_style->set_bg_color(color.darkened(0.08));
  2064. for (ArrayElement &ae : array_elements) {
  2065. if (ae.move_texture_rect) {
  2066. ae.move_texture_rect->set_texture(get_editor_theme_icon(SNAME("TripleBar")));
  2067. }
  2068. if (ae.move_up) {
  2069. ae.move_up->set_button_icon(get_editor_theme_icon(SNAME("MoveUp")));
  2070. }
  2071. if (ae.move_down) {
  2072. ae.move_down->set_button_icon(get_editor_theme_icon(SNAME("MoveDown")));
  2073. }
  2074. Size2 min_size = get_theme_stylebox(SNAME("Focus"), EditorStringName(EditorStyles))->get_minimum_size();
  2075. ae.margin->begin_bulk_theme_override();
  2076. ae.margin->add_theme_constant_override("margin_left", min_size.x / 2);
  2077. ae.margin->add_theme_constant_override("margin_top", min_size.y / 2);
  2078. ae.margin->add_theme_constant_override("margin_right", min_size.x / 2);
  2079. ae.margin->add_theme_constant_override("margin_bottom", min_size.y / 2);
  2080. ae.margin->end_bulk_theme_override();
  2081. if (ae.erase) {
  2082. ae.erase->set_button_icon(get_editor_theme_icon(SNAME("Remove")));
  2083. }
  2084. }
  2085. add_button->set_button_icon(get_editor_theme_icon(SNAME("Add")));
  2086. update_minimum_size();
  2087. } break;
  2088. case NOTIFICATION_DRAG_BEGIN: {
  2089. Dictionary dict = get_viewport()->gui_get_drag_data();
  2090. if (dict.has("type") && dict["type"] == "property_array_element" && String(dict["property_array_prefix"]) == array_element_prefix) {
  2091. dropping = true;
  2092. control_dropping->queue_redraw();
  2093. }
  2094. } break;
  2095. case NOTIFICATION_DRAG_END: {
  2096. if (dropping) {
  2097. dropping = false;
  2098. control_dropping->queue_redraw();
  2099. }
  2100. } break;
  2101. }
  2102. }
  2103. void EditorInspectorArray::_bind_methods() {
  2104. ADD_SIGNAL(MethodInfo("page_change_request"));
  2105. }
  2106. void EditorInspectorArray::setup_with_move_element_function(Object *p_object, const String &p_label, const StringName &p_array_element_prefix, int p_page, const Color &p_bg_color, bool p_foldable, bool p_movable, bool p_numbered, int p_page_length, const String &p_add_item_text) {
  2107. count_property = "";
  2108. mode = MODE_USE_MOVE_ARRAY_ELEMENT_FUNCTION;
  2109. array_element_prefix = p_array_element_prefix;
  2110. page = p_page;
  2111. movable = p_movable;
  2112. page_length = p_page_length;
  2113. numbered = p_numbered;
  2114. EditorInspectorSection::setup(String(p_array_element_prefix) + "_array", p_label, p_object, p_bg_color, p_foldable, 0);
  2115. _setup();
  2116. }
  2117. void EditorInspectorArray::setup_with_count_property(Object *p_object, const String &p_label, const StringName &p_count_property, const StringName &p_array_element_prefix, int p_page, const Color &p_bg_color, bool p_foldable, bool p_movable, bool p_numbered, int p_page_length, const String &p_add_item_text, const String &p_swap_method) {
  2118. count_property = p_count_property;
  2119. mode = MODE_USE_COUNT_PROPERTY;
  2120. array_element_prefix = p_array_element_prefix;
  2121. page = p_page;
  2122. movable = p_movable;
  2123. page_length = p_page_length;
  2124. numbered = p_numbered;
  2125. swap_method = p_swap_method;
  2126. add_button->set_text(p_add_item_text);
  2127. EditorInspectorSection::setup(String(count_property) + "_array", p_label, p_object, p_bg_color, p_foldable, 0);
  2128. _setup();
  2129. }
  2130. VBoxContainer *EditorInspectorArray::get_vbox(int p_index) {
  2131. if (p_index >= begin_array_index && p_index < end_array_index) {
  2132. return array_elements[p_index - begin_array_index].vbox;
  2133. } else if (p_index < 0) {
  2134. return vbox;
  2135. } else {
  2136. return nullptr;
  2137. }
  2138. }
  2139. EditorInspectorArray::EditorInspectorArray(bool p_read_only) {
  2140. read_only = p_read_only;
  2141. set_mouse_filter(Control::MOUSE_FILTER_STOP);
  2142. odd_style.instantiate();
  2143. even_style.instantiate();
  2144. rmb_popup = memnew(PopupMenu);
  2145. rmb_popup->add_item(TTR("Move Up"), OPTION_MOVE_UP);
  2146. rmb_popup->add_item(TTR("Move Down"), OPTION_MOVE_DOWN);
  2147. rmb_popup->add_separator();
  2148. rmb_popup->add_item(TTR("Insert New Before"), OPTION_NEW_BEFORE);
  2149. rmb_popup->add_item(TTR("Insert New After"), OPTION_NEW_AFTER);
  2150. rmb_popup->add_separator();
  2151. rmb_popup->add_item(TTR("Remove"), OPTION_REMOVE);
  2152. rmb_popup->add_separator();
  2153. rmb_popup->add_item(TTR("Clear Array"), OPTION_CLEAR_ARRAY);
  2154. rmb_popup->add_item(TTR("Resize Array..."), OPTION_RESIZE_ARRAY);
  2155. rmb_popup->connect(SceneStringName(id_pressed), callable_mp(this, &EditorInspectorArray::_rmb_popup_id_pressed));
  2156. add_child(rmb_popup);
  2157. elements_vbox = memnew(VBoxContainer);
  2158. elements_vbox->add_theme_constant_override("separation", 0);
  2159. vbox->add_child(elements_vbox);
  2160. add_button = EditorInspector::create_inspector_action_button(TTR("Add Element"));
  2161. add_button->connect(SceneStringName(pressed), callable_mp(this, &EditorInspectorArray::_add_button_pressed));
  2162. add_button->set_disabled(read_only);
  2163. vbox->add_child(add_button);
  2164. control_dropping = memnew(Control);
  2165. control_dropping->connect(SceneStringName(draw), callable_mp(this, &EditorInspectorArray::_control_dropping_draw));
  2166. control_dropping->set_mouse_filter(Control::MOUSE_FILTER_IGNORE);
  2167. add_child(control_dropping);
  2168. resize_dialog = memnew(AcceptDialog);
  2169. resize_dialog->set_title(TTRC("Resize Array"));
  2170. resize_dialog->add_cancel_button();
  2171. resize_dialog->connect(SceneStringName(confirmed), callable_mp(this, &EditorInspectorArray::_resize_dialog_confirmed));
  2172. add_child(resize_dialog);
  2173. VBoxContainer *resize_dialog_vbox = memnew(VBoxContainer);
  2174. resize_dialog->add_child(resize_dialog_vbox);
  2175. new_size_spin_box = memnew(SpinBox);
  2176. new_size_spin_box->set_max(16384);
  2177. new_size_spin_box->connect(SceneStringName(value_changed), callable_mp(this, &EditorInspectorArray::_new_size_spin_box_value_changed));
  2178. new_size_spin_box->get_line_edit()->connect(SceneStringName(text_submitted), callable_mp(this, &EditorInspectorArray::_new_size_spin_box_text_submitted));
  2179. new_size_spin_box->set_editable(!read_only);
  2180. resize_dialog_vbox->add_margin_child(TTRC("New Size:"), new_size_spin_box);
  2181. vbox->connect(SceneStringName(visibility_changed), callable_mp(this, &EditorInspectorArray::_vbox_visibility_changed));
  2182. }
  2183. ////////////////////////////////////////////////
  2184. ////////////////////////////////////////////////
  2185. void EditorPaginator::_first_page_button_pressed() {
  2186. emit_signal("page_changed", 0);
  2187. }
  2188. void EditorPaginator::_prev_page_button_pressed() {
  2189. emit_signal("page_changed", MAX(0, page - 1));
  2190. }
  2191. void EditorPaginator::_page_line_edit_text_submitted(const String &p_text) {
  2192. if (p_text.is_valid_int()) {
  2193. int new_page = p_text.to_int() - 1;
  2194. new_page = MIN(MAX(0, new_page), max_page);
  2195. page_line_edit->set_text(Variant(new_page));
  2196. emit_signal("page_changed", new_page);
  2197. } else {
  2198. page_line_edit->set_text(Variant(page));
  2199. }
  2200. }
  2201. void EditorPaginator::_next_page_button_pressed() {
  2202. emit_signal("page_changed", MIN(max_page, page + 1));
  2203. }
  2204. void EditorPaginator::_last_page_button_pressed() {
  2205. emit_signal("page_changed", max_page);
  2206. }
  2207. void EditorPaginator::update(int p_page, int p_max_page) {
  2208. page = p_page;
  2209. max_page = p_max_page;
  2210. // Update buttons.
  2211. first_page_button->set_disabled(page == 0);
  2212. prev_page_button->set_disabled(page == 0);
  2213. next_page_button->set_disabled(page == max_page);
  2214. last_page_button->set_disabled(page == max_page);
  2215. // Update page number and page count.
  2216. page_line_edit->set_text(vformat("%d", page + 1));
  2217. page_count_label->set_text(vformat("/ %d", max_page + 1));
  2218. }
  2219. void EditorPaginator::_notification(int p_what) {
  2220. switch (p_what) {
  2221. case NOTIFICATION_ENTER_TREE:
  2222. case NOTIFICATION_THEME_CHANGED: {
  2223. first_page_button->set_button_icon(get_editor_theme_icon(SNAME("PageFirst")));
  2224. prev_page_button->set_button_icon(get_editor_theme_icon(SNAME("PagePrevious")));
  2225. next_page_button->set_button_icon(get_editor_theme_icon(SNAME("PageNext")));
  2226. last_page_button->set_button_icon(get_editor_theme_icon(SNAME("PageLast")));
  2227. } break;
  2228. }
  2229. }
  2230. void EditorPaginator::_bind_methods() {
  2231. ADD_SIGNAL(MethodInfo("page_changed", PropertyInfo(Variant::INT, "page")));
  2232. }
  2233. EditorPaginator::EditorPaginator() {
  2234. set_h_size_flags(SIZE_EXPAND_FILL);
  2235. set_alignment(ALIGNMENT_CENTER);
  2236. first_page_button = memnew(Button);
  2237. first_page_button->set_flat(true);
  2238. first_page_button->connect(SceneStringName(pressed), callable_mp(this, &EditorPaginator::_first_page_button_pressed));
  2239. add_child(first_page_button);
  2240. prev_page_button = memnew(Button);
  2241. prev_page_button->set_flat(true);
  2242. prev_page_button->connect(SceneStringName(pressed), callable_mp(this, &EditorPaginator::_prev_page_button_pressed));
  2243. add_child(prev_page_button);
  2244. page_line_edit = memnew(LineEdit);
  2245. page_line_edit->connect(SceneStringName(text_submitted), callable_mp(this, &EditorPaginator::_page_line_edit_text_submitted));
  2246. page_line_edit->add_theme_constant_override("minimum_character_width", 2);
  2247. add_child(page_line_edit);
  2248. page_count_label = memnew(Label);
  2249. add_child(page_count_label);
  2250. next_page_button = memnew(Button);
  2251. next_page_button->set_flat(true);
  2252. next_page_button->connect(SceneStringName(pressed), callable_mp(this, &EditorPaginator::_next_page_button_pressed));
  2253. add_child(next_page_button);
  2254. last_page_button = memnew(Button);
  2255. last_page_button->set_flat(true);
  2256. last_page_button->connect(SceneStringName(pressed), callable_mp(this, &EditorPaginator::_last_page_button_pressed));
  2257. add_child(last_page_button);
  2258. }
  2259. ////////////////////////////////////////////////
  2260. ////////////////////////////////////////////////
  2261. Ref<EditorInspectorPlugin> EditorInspector::inspector_plugins[MAX_PLUGINS];
  2262. int EditorInspector::inspector_plugin_count = 0;
  2263. EditorProperty *EditorInspector::instantiate_property_editor(Object *p_object, const Variant::Type p_type, const String &p_path, PropertyHint p_hint, const String &p_hint_text, const uint32_t p_usage, const bool p_wide) {
  2264. for (int i = inspector_plugin_count - 1; i >= 0; i--) {
  2265. if (!inspector_plugins[i]->can_handle(p_object)) {
  2266. continue;
  2267. }
  2268. inspector_plugins[i]->parse_property(p_object, p_type, p_path, p_hint, p_hint_text, p_usage, p_wide);
  2269. if (inspector_plugins[i]->added_editors.size()) {
  2270. for (List<EditorInspectorPlugin::AddedEditor>::Element *E = inspector_plugins[i]->added_editors.front()->next(); E; E = E->next()) { //only keep first one
  2271. memdelete(E->get().property_editor);
  2272. }
  2273. EditorProperty *prop = Object::cast_to<EditorProperty>(inspector_plugins[i]->added_editors.front()->get().property_editor);
  2274. if (prop) {
  2275. inspector_plugins[i]->added_editors.clear();
  2276. return prop;
  2277. } else {
  2278. memdelete(inspector_plugins[i]->added_editors.front()->get().property_editor);
  2279. inspector_plugins[i]->added_editors.clear();
  2280. }
  2281. }
  2282. }
  2283. return nullptr;
  2284. }
  2285. void EditorInspector::add_inspector_plugin(const Ref<EditorInspectorPlugin> &p_plugin) {
  2286. ERR_FAIL_COND(inspector_plugin_count == MAX_PLUGINS);
  2287. for (int i = 0; i < inspector_plugin_count; i++) {
  2288. if (inspector_plugins[i] == p_plugin) {
  2289. return; //already exists
  2290. }
  2291. }
  2292. inspector_plugins[inspector_plugin_count++] = p_plugin;
  2293. }
  2294. void EditorInspector::remove_inspector_plugin(const Ref<EditorInspectorPlugin> &p_plugin) {
  2295. ERR_FAIL_COND(inspector_plugin_count == MAX_PLUGINS);
  2296. int idx = -1;
  2297. for (int i = 0; i < inspector_plugin_count; i++) {
  2298. if (inspector_plugins[i] == p_plugin) {
  2299. idx = i;
  2300. break;
  2301. }
  2302. }
  2303. ERR_FAIL_COND_MSG(idx == -1, "Trying to remove nonexistent inspector plugin.");
  2304. for (int i = idx; i < inspector_plugin_count - 1; i++) {
  2305. inspector_plugins[i] = inspector_plugins[i + 1];
  2306. }
  2307. inspector_plugins[inspector_plugin_count - 1] = Ref<EditorInspectorPlugin>();
  2308. inspector_plugin_count--;
  2309. }
  2310. void EditorInspector::cleanup_plugins() {
  2311. for (int i = 0; i < inspector_plugin_count; i++) {
  2312. inspector_plugins[i].unref();
  2313. }
  2314. inspector_plugin_count = 0;
  2315. }
  2316. Button *EditorInspector::create_inspector_action_button(const String &p_text) {
  2317. Button *button = memnew(Button);
  2318. button->set_text(p_text);
  2319. button->set_theme_type_variation(SNAME("InspectorActionButton"));
  2320. button->set_h_size_flags(SIZE_SHRINK_CENTER);
  2321. return button;
  2322. }
  2323. bool EditorInspector::is_main_editor_inspector() const {
  2324. return InspectorDock::get_singleton() && InspectorDock::get_inspector_singleton() == this;
  2325. }
  2326. String EditorInspector::get_selected_path() const {
  2327. return property_selected;
  2328. }
  2329. void EditorInspector::_parse_added_editors(VBoxContainer *current_vbox, EditorInspectorSection *p_section, Ref<EditorInspectorPlugin> ped) {
  2330. for (const EditorInspectorPlugin::AddedEditor &F : ped->added_editors) {
  2331. EditorProperty *ep = Object::cast_to<EditorProperty>(F.property_editor);
  2332. if (ep && !F.properties.is_empty() && current_favorites.has(F.properties[0])) {
  2333. ep->favorited = true;
  2334. favorites_vbox->add_child(F.property_editor);
  2335. } else {
  2336. current_vbox->add_child(F.property_editor);
  2337. }
  2338. if (ep) {
  2339. ep->object = object;
  2340. ep->connect("property_changed", callable_mp(this, &EditorInspector::_property_changed).bind(false));
  2341. ep->connect("property_keyed", callable_mp(this, &EditorInspector::_property_keyed));
  2342. ep->connect("property_deleted", callable_mp(this, &EditorInspector::_property_deleted), CONNECT_DEFERRED);
  2343. ep->connect("property_keyed_with_value", callable_mp(this, &EditorInspector::_property_keyed_with_value));
  2344. ep->connect("property_checked", callable_mp(this, &EditorInspector::_property_checked));
  2345. ep->connect("property_favorited", callable_mp(this, &EditorInspector::_set_property_favorited), CONNECT_DEFERRED);
  2346. ep->connect("property_pinned", callable_mp(this, &EditorInspector::_property_pinned));
  2347. ep->connect("selected", callable_mp(this, &EditorInspector::_property_selected));
  2348. ep->connect("multiple_properties_changed", callable_mp(this, &EditorInspector::_multiple_properties_changed));
  2349. ep->connect("resource_selected", callable_mp(this, &EditorInspector::_resource_selected), CONNECT_DEFERRED);
  2350. ep->connect("object_id_selected", callable_mp(this, &EditorInspector::_object_id_selected), CONNECT_DEFERRED);
  2351. if (F.properties.size()) {
  2352. if (F.properties.size() == 1) {
  2353. //since it's one, associate:
  2354. ep->property = F.properties[0];
  2355. ep->property_path = property_prefix + F.properties[0];
  2356. ep->property_usage = 0;
  2357. }
  2358. if (!F.label.is_empty()) {
  2359. ep->set_label(F.label);
  2360. }
  2361. for (int i = 0; i < F.properties.size(); i++) {
  2362. String prop = F.properties[i];
  2363. if (!editor_property_map.has(prop)) {
  2364. editor_property_map[prop] = List<EditorProperty *>();
  2365. }
  2366. editor_property_map[prop].push_back(ep);
  2367. }
  2368. }
  2369. Node *section_search = p_section;
  2370. while (section_search) {
  2371. EditorInspectorSection *section = Object::cast_to<EditorInspectorSection>(section_search);
  2372. if (section) {
  2373. ep->connect("property_can_revert_changed", callable_mp(section, &EditorInspectorSection::property_can_revert_changed));
  2374. }
  2375. section_search = section_search->get_parent();
  2376. if (Object::cast_to<EditorInspector>(section_search)) {
  2377. // Skip sub-resource inspectors.
  2378. break;
  2379. }
  2380. }
  2381. ep->set_read_only(read_only);
  2382. ep->update_property();
  2383. ep->_update_flags();
  2384. ep->update_editor_property_status();
  2385. ep->set_deletable(deletable_properties);
  2386. ep->update_cache();
  2387. }
  2388. }
  2389. ped->added_editors.clear();
  2390. }
  2391. bool EditorInspector::_is_property_disabled_by_feature_profile(const StringName &p_property) {
  2392. Ref<EditorFeatureProfile> profile = EditorFeatureProfileManager::get_singleton()->get_current_profile();
  2393. if (profile.is_null()) {
  2394. return false;
  2395. }
  2396. StringName class_name = object->get_class();
  2397. while (class_name != StringName()) {
  2398. if (profile->is_class_property_disabled(class_name, p_property)) {
  2399. return true;
  2400. }
  2401. if (profile->is_class_disabled(class_name)) {
  2402. //won't see properties of a disabled class
  2403. return true;
  2404. }
  2405. class_name = ClassDB::get_parent_class(class_name);
  2406. }
  2407. return false;
  2408. }
  2409. void EditorInspector::update_tree() {
  2410. // Store currently selected and focused elements to restore after the update.
  2411. // TODO: Can be useful to store more context for the focusable, such as the caret position in LineEdit.
  2412. StringName current_selected = property_selected;
  2413. int current_focusable = -1;
  2414. // Temporarily disable focus following on the root inspector to avoid jumping while the inspector is updating.
  2415. get_root_inspector()->set_follow_focus(false);
  2416. if (property_focusable != -1) {
  2417. // Check that focusable is actually focusable.
  2418. bool restore_focus = false;
  2419. Control *focused = get_viewport() ? get_viewport()->gui_get_focus_owner() : nullptr;
  2420. if (focused) {
  2421. Node *parent = focused->get_parent();
  2422. while (parent) {
  2423. EditorInspector *inspector = Object::cast_to<EditorInspector>(parent);
  2424. if (inspector) {
  2425. restore_focus = inspector == this; // May be owned by another inspector.
  2426. break; // Exit after the first inspector is found, since there may be nested ones.
  2427. }
  2428. parent = parent->get_parent();
  2429. }
  2430. }
  2431. if (restore_focus) {
  2432. current_focusable = property_focusable;
  2433. }
  2434. }
  2435. // Only hide plugins if we are not editing any object.
  2436. // This should be handled outside of the update_tree call anyway (see EditorInspector::edit), but might as well keep it safe.
  2437. _clear(!object);
  2438. if (!object) {
  2439. get_root_inspector()->set_follow_focus(true);
  2440. return;
  2441. }
  2442. List<Ref<EditorInspectorPlugin>> valid_plugins;
  2443. for (int i = inspector_plugin_count - 1; i >= 0; i--) { //start by last, so lastly added can override newly added
  2444. if (!inspector_plugins[i]->can_handle(object)) {
  2445. continue;
  2446. }
  2447. valid_plugins.push_back(inspector_plugins[i]);
  2448. }
  2449. // Decide if properties should be drawn with the warning color (yellow),
  2450. // or if the whole object should be considered read-only.
  2451. bool draw_warning = false;
  2452. bool all_read_only = false;
  2453. if (is_inside_tree()) {
  2454. if (object->has_method("_is_read_only")) {
  2455. all_read_only = object->call("_is_read_only");
  2456. }
  2457. Node *nod = Object::cast_to<Node>(object);
  2458. Node *es = EditorNode::get_singleton()->get_edited_scene();
  2459. if (nod && es != nod && nod->get_owner() != es) {
  2460. // Draw in warning color edited nodes that are not in the currently edited scene,
  2461. // as changes may be lost in the future.
  2462. draw_warning = true;
  2463. } else {
  2464. if (!all_read_only) {
  2465. Resource *res = Object::cast_to<Resource>(object);
  2466. if (res) {
  2467. all_read_only = EditorNode::get_singleton()->is_resource_read_only(res);
  2468. }
  2469. }
  2470. }
  2471. }
  2472. String filter = search_box ? search_box->get_text() : "";
  2473. String group;
  2474. String group_base;
  2475. String subgroup;
  2476. String subgroup_base;
  2477. int section_depth = 0;
  2478. bool disable_favorite = false;
  2479. VBoxContainer *category_vbox = nullptr;
  2480. List<PropertyInfo> plist;
  2481. object->get_property_list(&plist, true);
  2482. HashMap<VBoxContainer *, HashMap<String, VBoxContainer *>> vbox_per_path;
  2483. HashMap<String, EditorInspectorArray *> editor_inspector_array_per_prefix;
  2484. HashMap<String, HashMap<String, LocalVector<EditorProperty *>>> favorites_to_add;
  2485. Color sscolor = get_theme_color(SNAME("prop_subsection"), EditorStringName(Editor));
  2486. // Get the lists of editors to add the beginning.
  2487. for (Ref<EditorInspectorPlugin> &ped : valid_plugins) {
  2488. ped->parse_begin(object);
  2489. _parse_added_editors(begin_vbox, nullptr, ped);
  2490. }
  2491. if (begin_vbox->get_child_count()) {
  2492. begin_vbox->show();
  2493. }
  2494. StringName doc_name;
  2495. // Get the lists of editors for properties.
  2496. for (List<PropertyInfo>::Element *E_property = plist.front(); E_property; E_property = E_property->next()) {
  2497. PropertyInfo &p = E_property->get();
  2498. if (p.usage & PROPERTY_USAGE_SUBGROUP) {
  2499. // Setup a property sub-group.
  2500. subgroup = p.name;
  2501. Vector<String> hint_parts = p.hint_string.split(",");
  2502. subgroup_base = hint_parts[0];
  2503. if (hint_parts.size() > 1) {
  2504. section_depth = hint_parts[1].to_int();
  2505. } else {
  2506. section_depth = 0;
  2507. }
  2508. continue;
  2509. } else if (p.usage & PROPERTY_USAGE_GROUP) {
  2510. // Setup a property group.
  2511. group = p.name;
  2512. Vector<String> hint_parts = p.hint_string.split(",");
  2513. group_base = hint_parts[0];
  2514. if (hint_parts.size() > 1) {
  2515. section_depth = hint_parts[1].to_int();
  2516. } else {
  2517. section_depth = 0;
  2518. }
  2519. subgroup = "";
  2520. subgroup_base = "";
  2521. continue;
  2522. } else if (p.usage & PROPERTY_USAGE_CATEGORY) {
  2523. // Setup a property category.
  2524. group = "";
  2525. group_base = "";
  2526. subgroup = "";
  2527. subgroup_base = "";
  2528. section_depth = 0;
  2529. disable_favorite = false;
  2530. vbox_per_path.clear();
  2531. editor_inspector_array_per_prefix.clear();
  2532. // `hint_script` should contain a native class name or a script path.
  2533. // Otherwise the category was probably added via `@export_category` or `_get_property_list()`.
  2534. const bool is_custom_category = p.hint_string.is_empty();
  2535. // Iterate over remaining properties. If no properties in category, skip the category.
  2536. List<PropertyInfo>::Element *N = E_property->next();
  2537. bool valid = true;
  2538. while (N) {
  2539. if (!N->get().name.begins_with("metadata/_") && N->get().usage & PROPERTY_USAGE_EDITOR &&
  2540. (!filter.is_empty() || !restrict_to_basic || (N->get().usage & PROPERTY_USAGE_EDITOR_BASIC_SETTING))) {
  2541. break;
  2542. }
  2543. // Treat custom categories as second-level ones. Do not skip a normal category if it is followed by a custom one.
  2544. // Skip in the other 3 cases (normal -> normal, custom -> custom, custom -> normal).
  2545. if ((N->get().usage & PROPERTY_USAGE_CATEGORY) && (is_custom_category || !N->get().hint_string.is_empty())) {
  2546. valid = false;
  2547. break;
  2548. }
  2549. N = N->next();
  2550. }
  2551. if (!valid) {
  2552. continue; // Empty, ignore it.
  2553. }
  2554. String category_label;
  2555. String category_tooltip;
  2556. Ref<Texture> category_icon;
  2557. // Do not add an icon, do not change the current class (`doc_name`) for custom categories.
  2558. if (is_custom_category) {
  2559. category_label = p.name;
  2560. category_tooltip = p.name;
  2561. } else {
  2562. doc_name = p.name;
  2563. category_label = p.name;
  2564. // Use category's owner script to update some of its information.
  2565. if (!EditorNode::get_editor_data().is_type_recognized(p.name) && ResourceLoader::exists(p.hint_string, "Script")) {
  2566. Ref<Script> scr = ResourceLoader::load(p.hint_string, "Script");
  2567. if (scr.is_valid()) {
  2568. doc_name = scr->get_doc_class_name();
  2569. StringName script_name = EditorNode::get_editor_data().script_class_get_name(scr->get_path());
  2570. if (script_name != StringName()) {
  2571. category_label = script_name;
  2572. category_icon = EditorNode::get_singleton()->get_class_icon(script_name);
  2573. } else {
  2574. category_icon = EditorNode::get_singleton()->get_object_icon(scr.ptr(), "Object");
  2575. }
  2576. // Property favorites aren't compatible with built-in scripts.
  2577. if (scr->is_built_in()) {
  2578. disable_favorite = true;
  2579. }
  2580. }
  2581. }
  2582. if (category_icon.is_null() && !p.name.is_empty()) {
  2583. category_icon = EditorNode::get_singleton()->get_class_icon(p.name);
  2584. }
  2585. if (use_doc_hints) {
  2586. // `|` separators used in `EditorHelpBit`.
  2587. category_tooltip = "class|" + doc_name + "|";
  2588. }
  2589. }
  2590. if ((is_custom_category && !show_custom_categories) || (!is_custom_category && !show_standard_categories)) {
  2591. continue;
  2592. }
  2593. // Hide the "MultiNodeEdit" category for MultiNodeEdit.
  2594. if (Object::cast_to<MultiNodeEdit>(object) && p.name == "MultiNodeEdit") {
  2595. continue;
  2596. }
  2597. // Create an EditorInspectorCategory and add it to the inspector.
  2598. EditorInspectorCategory *category = memnew(EditorInspectorCategory);
  2599. main_vbox->add_child(category);
  2600. category_vbox = nullptr; // Reset.
  2601. // Set the category info.
  2602. category->label = category_label;
  2603. category->set_tooltip_text(category_tooltip);
  2604. category->icon = category_icon;
  2605. if (!is_custom_category) {
  2606. category->doc_class_name = doc_name;
  2607. }
  2608. // Add editors at the start of a category.
  2609. for (Ref<EditorInspectorPlugin> &ped : valid_plugins) {
  2610. ped->parse_category(object, p.name);
  2611. _parse_added_editors(main_vbox, nullptr, ped);
  2612. }
  2613. continue;
  2614. } else if (p.name.begins_with("metadata/_") || !(p.usage & PROPERTY_USAGE_EDITOR) || _is_property_disabled_by_feature_profile(p.name) ||
  2615. (filter.is_empty() && restrict_to_basic && !(p.usage & PROPERTY_USAGE_EDITOR_BASIC_SETTING))) {
  2616. // Ignore properties that are not supposed to be in the inspector.
  2617. continue;
  2618. }
  2619. if (p.name == "script") {
  2620. // Script should go into its own category.
  2621. category_vbox = nullptr;
  2622. }
  2623. if (p.usage & PROPERTY_USAGE_HIGH_END_GFX && RS::get_singleton()->is_low_end()) {
  2624. // Do not show this property in low end gfx.
  2625. continue;
  2626. }
  2627. if (p.name == "script" && (hide_script || bool(object->call("_hide_script_from_inspector")))) {
  2628. // Hide script variables from inspector if required.
  2629. continue;
  2630. }
  2631. if (p.name.begins_with("metadata/") && bool(object->call("_hide_metadata_from_inspector"))) {
  2632. // Hide metadata from inspector if required.
  2633. continue;
  2634. }
  2635. // Get the path for property.
  2636. String path = p.name;
  2637. // First check if we have an array that fits the prefix.
  2638. String array_prefix = "";
  2639. int array_index = -1;
  2640. for (KeyValue<String, EditorInspectorArray *> &E : editor_inspector_array_per_prefix) {
  2641. if (p.name.begins_with(E.key) && E.key.length() > array_prefix.length()) {
  2642. array_prefix = E.key;
  2643. }
  2644. }
  2645. if (!array_prefix.is_empty()) {
  2646. // If we have an array element, find the according index in array.
  2647. String str = p.name.trim_prefix(array_prefix);
  2648. int to_char_index = 0;
  2649. while (to_char_index < str.length()) {
  2650. if (!is_digit(str[to_char_index])) {
  2651. break;
  2652. }
  2653. to_char_index++;
  2654. }
  2655. if (to_char_index > 0) {
  2656. array_index = str.left(to_char_index).to_int();
  2657. } else {
  2658. array_prefix = "";
  2659. }
  2660. }
  2661. // Don't allow to favorite array items.
  2662. if (!disable_favorite) {
  2663. disable_favorite = !array_prefix.is_empty();
  2664. }
  2665. if (!array_prefix.is_empty()) {
  2666. path = path.trim_prefix(array_prefix);
  2667. int char_index = path.find_char('/');
  2668. if (char_index >= 0) {
  2669. path = path.right(-char_index - 1);
  2670. } else {
  2671. path = vformat(TTR("Element %s"), array_index);
  2672. }
  2673. } else {
  2674. // Check if we exit or not a subgroup. If there is a prefix, remove it from the property label string.
  2675. if (!subgroup.is_empty() && !subgroup_base.is_empty()) {
  2676. if (path.begins_with(subgroup_base)) {
  2677. path = path.trim_prefix(subgroup_base);
  2678. } else if (subgroup_base.begins_with(path)) {
  2679. // Keep it, this is used pretty often.
  2680. } else {
  2681. subgroup = ""; // The prefix changed, we are no longer in the subgroup.
  2682. }
  2683. }
  2684. // Check if we exit or not a group. If there is a prefix, remove it from the property label string.
  2685. if (!group.is_empty() && !group_base.is_empty() && subgroup.is_empty()) {
  2686. if (path.begins_with(group_base)) {
  2687. path = path.trim_prefix(group_base);
  2688. } else if (group_base.begins_with(path)) {
  2689. // Keep it, this is used pretty often.
  2690. } else {
  2691. group = ""; // The prefix changed, we are no longer in the group.
  2692. subgroup = "";
  2693. }
  2694. }
  2695. // Add the group and subgroup to the path.
  2696. if (!subgroup.is_empty()) {
  2697. path = subgroup + "/" + path;
  2698. }
  2699. if (!group.is_empty()) {
  2700. path = group + "/" + path;
  2701. }
  2702. }
  2703. // Get the property label's string.
  2704. String name_override = (path.contains_char('/')) ? path.substr(path.rfind_char('/') + 1) : path;
  2705. String feature_tag;
  2706. {
  2707. const int dot = name_override.find_char('.');
  2708. if (dot != -1) {
  2709. feature_tag = name_override.substr(dot);
  2710. name_override = name_override.substr(0, dot);
  2711. }
  2712. }
  2713. // Don't localize script variables.
  2714. EditorPropertyNameProcessor::Style name_style = property_name_style;
  2715. if ((p.usage & PROPERTY_USAGE_SCRIPT_VARIABLE) && name_style == EditorPropertyNameProcessor::STYLE_LOCALIZED) {
  2716. name_style = EditorPropertyNameProcessor::STYLE_CAPITALIZED;
  2717. }
  2718. const String property_label_string = EditorPropertyNameProcessor::get_singleton()->process_name(name_override, name_style, p.name, doc_name) + feature_tag;
  2719. // Remove the property from the path.
  2720. int idx = path.rfind_char('/');
  2721. if (idx > -1) {
  2722. path = path.left(idx);
  2723. } else {
  2724. path = "";
  2725. }
  2726. // Ignore properties that do not fit the filter.
  2727. if (use_filter && !filter.is_empty()) {
  2728. const String property_path = property_prefix + (path.is_empty() ? "" : path + "/") + name_override;
  2729. if (!_property_path_matches(property_path, filter, property_name_style)) {
  2730. continue;
  2731. }
  2732. }
  2733. // Recreate the category vbox if it was reset.
  2734. if (category_vbox == nullptr) {
  2735. category_vbox = memnew(VBoxContainer);
  2736. category_vbox->hide();
  2737. main_vbox->add_child(category_vbox);
  2738. }
  2739. // Find the correct section/vbox to add the property editor to.
  2740. VBoxContainer *root_vbox = array_prefix.is_empty() ? main_vbox : editor_inspector_array_per_prefix[array_prefix]->get_vbox(array_index);
  2741. if (!root_vbox) {
  2742. continue;
  2743. }
  2744. if (!vbox_per_path.has(root_vbox)) {
  2745. vbox_per_path[root_vbox] = HashMap<String, VBoxContainer *>();
  2746. vbox_per_path[root_vbox][""] = root_vbox;
  2747. }
  2748. VBoxContainer *current_vbox = root_vbox;
  2749. String acc_path = "";
  2750. int level = 1;
  2751. Vector<String> components = path.split("/");
  2752. for (int i = 0; i < components.size(); i++) {
  2753. const String &component = components[i];
  2754. acc_path += (i > 0) ? "/" + component : component;
  2755. if (!vbox_per_path[root_vbox].has(acc_path)) {
  2756. // If the section does not exists, create it.
  2757. EditorInspectorSection *section = memnew(EditorInspectorSection);
  2758. current_vbox->add_child(section);
  2759. sections.push_back(section);
  2760. String label;
  2761. String tooltip;
  2762. // Don't localize groups for script variables.
  2763. EditorPropertyNameProcessor::Style section_name_style = property_name_style;
  2764. if ((p.usage & PROPERTY_USAGE_SCRIPT_VARIABLE) && section_name_style == EditorPropertyNameProcessor::STYLE_LOCALIZED) {
  2765. section_name_style = EditorPropertyNameProcessor::STYLE_CAPITALIZED;
  2766. }
  2767. // Only process group label if this is not the group or subgroup.
  2768. if ((i == 0 && component == group) || (i == 1 && component == subgroup)) {
  2769. if (section_name_style == EditorPropertyNameProcessor::STYLE_LOCALIZED) {
  2770. label = EditorPropertyNameProcessor::get_singleton()->translate_group_name(component);
  2771. tooltip = component;
  2772. } else {
  2773. label = component;
  2774. tooltip = EditorPropertyNameProcessor::get_singleton()->translate_group_name(component);
  2775. }
  2776. } else {
  2777. label = EditorPropertyNameProcessor::get_singleton()->process_name(component, section_name_style, p.name, doc_name);
  2778. tooltip = EditorPropertyNameProcessor::get_singleton()->process_name(component, EditorPropertyNameProcessor::get_tooltip_style(section_name_style), p.name, doc_name);
  2779. }
  2780. Color c = sscolor;
  2781. c.a /= level;
  2782. section->setup(acc_path, label, object, c, use_folding, section_depth, level);
  2783. section->set_tooltip_text(tooltip);
  2784. // Add editors at the start of a group.
  2785. for (Ref<EditorInspectorPlugin> &ped : valid_plugins) {
  2786. ped->parse_group(object, path);
  2787. _parse_added_editors(section->get_vbox(), section, ped);
  2788. }
  2789. vbox_per_path[root_vbox][acc_path] = section->get_vbox();
  2790. }
  2791. current_vbox = vbox_per_path[root_vbox][acc_path];
  2792. level = (MIN(level + 1, 4));
  2793. }
  2794. // If we did not find a section to add the property to, add it to the category vbox instead (the category vbox handles margins correctly).
  2795. if (current_vbox == main_vbox) {
  2796. category_vbox->show();
  2797. current_vbox = category_vbox;
  2798. }
  2799. // Check if the property is an array counter, if so create a dedicated array editor for the array.
  2800. if (p.usage & PROPERTY_USAGE_ARRAY) {
  2801. EditorInspectorArray *editor_inspector_array = nullptr;
  2802. StringName array_element_prefix;
  2803. Color c = sscolor;
  2804. c.a /= level;
  2805. Vector<String> class_name_components = String(p.class_name).split(",");
  2806. int page_size = 5;
  2807. bool movable = true;
  2808. bool numbered = false;
  2809. bool foldable = use_folding;
  2810. String add_button_text = TTR("Add Element");
  2811. String swap_method;
  2812. for (int i = (p.type == Variant::NIL ? 1 : 2); i < class_name_components.size(); i++) {
  2813. if (class_name_components[i].begins_with("page_size") && class_name_components[i].get_slice_count("=") == 2) {
  2814. page_size = class_name_components[i].get_slice("=", 1).to_int();
  2815. } else if (class_name_components[i].begins_with("add_button_text") && class_name_components[i].get_slice_count("=") == 2) {
  2816. add_button_text = class_name_components[i].get_slice("=", 1).strip_edges();
  2817. } else if (class_name_components[i] == "static") {
  2818. movable = false;
  2819. } else if (class_name_components[i] == "numbered") {
  2820. numbered = true;
  2821. } else if (class_name_components[i] == "unfoldable") {
  2822. foldable = false;
  2823. } else if (class_name_components[i].begins_with("swap_method") && class_name_components[i].get_slice_count("=") == 2) {
  2824. swap_method = class_name_components[i].get_slice("=", 1).strip_edges();
  2825. }
  2826. }
  2827. if (p.type == Variant::NIL) {
  2828. // Setup the array to use a method to create/move/delete elements.
  2829. array_element_prefix = class_name_components[0];
  2830. editor_inspector_array = memnew(EditorInspectorArray(all_read_only));
  2831. String array_label = path.contains_char('/') ? path.substr(path.rfind_char('/') + 1) : path;
  2832. array_label = EditorPropertyNameProcessor::get_singleton()->process_name(property_label_string, property_name_style, p.name, doc_name);
  2833. int page = per_array_page.has(array_element_prefix) ? per_array_page[array_element_prefix] : 0;
  2834. editor_inspector_array->setup_with_move_element_function(object, array_label, array_element_prefix, page, c, use_folding);
  2835. editor_inspector_array->connect("page_change_request", callable_mp(this, &EditorInspector::_page_change_request).bind(array_element_prefix));
  2836. } else if (p.type == Variant::INT) {
  2837. // Setup the array to use the count property and built-in functions to create/move/delete elements.
  2838. if (class_name_components.size() >= 2) {
  2839. array_element_prefix = class_name_components[1];
  2840. editor_inspector_array = memnew(EditorInspectorArray(all_read_only));
  2841. int page = per_array_page.has(array_element_prefix) ? per_array_page[array_element_prefix] : 0;
  2842. editor_inspector_array->setup_with_count_property(object, class_name_components[0], p.name, array_element_prefix, page, c, foldable, movable, numbered, page_size, add_button_text, swap_method);
  2843. editor_inspector_array->connect("page_change_request", callable_mp(this, &EditorInspector::_page_change_request).bind(array_element_prefix));
  2844. }
  2845. }
  2846. if (editor_inspector_array) {
  2847. current_vbox->add_child(editor_inspector_array);
  2848. editor_inspector_array_per_prefix[array_element_prefix] = editor_inspector_array;
  2849. }
  2850. continue;
  2851. }
  2852. // Checkable and checked properties.
  2853. bool checkable = false;
  2854. bool checked = false;
  2855. if (p.usage & PROPERTY_USAGE_CHECKABLE) {
  2856. checkable = true;
  2857. checked = p.usage & PROPERTY_USAGE_CHECKED;
  2858. }
  2859. bool property_read_only = (p.usage & PROPERTY_USAGE_READ_ONLY) || read_only;
  2860. // Mark properties that would require an editor restart (mostly when editing editor settings).
  2861. if (p.usage & PROPERTY_USAGE_RESTART_IF_CHANGED) {
  2862. restart_request_props.insert(p.name);
  2863. }
  2864. String doc_path;
  2865. String theme_item_name;
  2866. StringName classname = doc_name;
  2867. // Build the doc hint, to use as tooltip.
  2868. if (use_doc_hints) {
  2869. if (!object_class.is_empty()) {
  2870. classname = object_class;
  2871. } else if (Object::cast_to<MultiNodeEdit>(object)) {
  2872. classname = Object::cast_to<MultiNodeEdit>(object)->get_edited_class_name();
  2873. } else if (classname == "") {
  2874. classname = object->get_class_name();
  2875. Resource *res = Object::cast_to<Resource>(object);
  2876. if (res && !res->get_script().is_null()) {
  2877. // Grab the script of this resource to get the evaluated script class.
  2878. Ref<Script> scr = res->get_script();
  2879. if (scr.is_valid()) {
  2880. Vector<DocData::ClassDoc> docs = scr->get_documentation();
  2881. if (!docs.is_empty()) {
  2882. // The documentation of a GDScript's main class is at the end of the array.
  2883. // Hacky because this isn't necessarily always guaranteed.
  2884. classname = docs[docs.size() - 1].name;
  2885. }
  2886. }
  2887. }
  2888. }
  2889. StringName propname = property_prefix + p.name;
  2890. bool found = false;
  2891. // Small hack for theme_overrides. They are listed under Control, but come from another class.
  2892. if (classname == "Control" && p.name.begins_with("theme_override_")) {
  2893. classname = get_edited_object()->get_class();
  2894. }
  2895. // Search for the doc path in the cache.
  2896. HashMap<StringName, HashMap<StringName, DocCacheInfo>>::Iterator E = doc_cache.find(classname);
  2897. if (E) {
  2898. HashMap<StringName, DocCacheInfo>::Iterator F = E->value.find(propname);
  2899. if (F) {
  2900. found = true;
  2901. doc_path = F->value.doc_path;
  2902. theme_item_name = F->value.theme_item_name;
  2903. }
  2904. }
  2905. if (!found) {
  2906. DocTools *dd = EditorHelp::get_doc_data();
  2907. // Do not cache the doc path information of scripts.
  2908. bool is_native_class = ClassDB::class_exists(classname);
  2909. HashMap<String, DocData::ClassDoc>::ConstIterator F = dd->class_list.find(classname);
  2910. while (F) {
  2911. Vector<String> slices = propname.operator String().split("/");
  2912. // Check if it's a theme item first.
  2913. if (slices.size() == 2 && slices[0].begins_with("theme_override_")) {
  2914. for (int i = 0; i < F->value.theme_properties.size(); i++) {
  2915. String doc_path_current = "class_theme_item:" + F->value.name + ":" + F->value.theme_properties[i].name;
  2916. if (F->value.theme_properties[i].name == slices[1]) {
  2917. doc_path = doc_path_current;
  2918. theme_item_name = F->value.theme_properties[i].name;
  2919. }
  2920. }
  2921. } else {
  2922. for (int i = 0; i < F->value.properties.size(); i++) {
  2923. String doc_path_current = "class_property:" + F->value.name + ":" + F->value.properties[i].name;
  2924. if (F->value.properties[i].name == propname.operator String()) {
  2925. doc_path = doc_path_current;
  2926. }
  2927. }
  2928. }
  2929. if (is_native_class) {
  2930. DocCacheInfo cache_info;
  2931. cache_info.doc_path = doc_path;
  2932. cache_info.theme_item_name = theme_item_name;
  2933. doc_cache[classname][propname] = cache_info;
  2934. }
  2935. if (!doc_path.is_empty() || F->value.inherits.is_empty()) {
  2936. break;
  2937. }
  2938. // Couldn't find the doc path in the class itself, try its super class.
  2939. F = dd->class_list.find(F->value.inherits);
  2940. }
  2941. }
  2942. }
  2943. Vector<EditorInspectorPlugin::AddedEditor> editors;
  2944. Vector<EditorInspectorPlugin::AddedEditor> late_editors;
  2945. // Search for the inspector plugin that will handle the properties. Then add the correct property editor to it.
  2946. for (Ref<EditorInspectorPlugin> &ped : valid_plugins) {
  2947. bool exclusive = ped->parse_property(object, p.type, p.name, p.hint, p.hint_string, p.usage, wide_editors);
  2948. for (const EditorInspectorPlugin::AddedEditor &F : ped->added_editors) {
  2949. if (F.add_to_end) {
  2950. late_editors.push_back(F);
  2951. } else {
  2952. editors.push_back(F);
  2953. }
  2954. }
  2955. ped->added_editors.clear();
  2956. if (exclusive) {
  2957. break;
  2958. }
  2959. }
  2960. editors.append_array(late_editors);
  2961. const Node *node = Object::cast_to<Node>(object);
  2962. Vector<SceneState::PackState> sstack;
  2963. if (node != nullptr) {
  2964. const Node *es = EditorNode::get_singleton()->get_edited_scene();
  2965. sstack = PropertyUtils::get_node_states_stack(node, es);
  2966. }
  2967. for (int i = 0; i < editors.size(); i++) {
  2968. EditorProperty *ep = Object::cast_to<EditorProperty>(editors[i].property_editor);
  2969. const Vector<String> &properties = editors[i].properties;
  2970. if (ep) {
  2971. // Set all this before the control gets the ENTER_TREE notification.
  2972. ep->object = object;
  2973. if (properties.size()) {
  2974. if (properties.size() == 1) {
  2975. // Since it's one, associate:
  2976. ep->property = properties[0];
  2977. ep->property_path = property_prefix + properties[0];
  2978. ep->property_usage = p.usage;
  2979. // And set label?
  2980. }
  2981. if (!editors[i].label.is_empty()) {
  2982. ep->set_label(editors[i].label);
  2983. } else {
  2984. // Use the existing one.
  2985. ep->set_label(property_label_string);
  2986. }
  2987. for (int j = 0; j < properties.size(); j++) {
  2988. String prop = properties[j];
  2989. if (!editor_property_map.has(prop)) {
  2990. editor_property_map[prop] = List<EditorProperty *>();
  2991. }
  2992. editor_property_map[prop].push_back(ep);
  2993. }
  2994. }
  2995. Node *section_search = current_vbox->get_parent();
  2996. while (section_search) {
  2997. EditorInspectorSection *section = Object::cast_to<EditorInspectorSection>(section_search);
  2998. if (section) {
  2999. ep->connect("property_can_revert_changed", callable_mp(section, &EditorInspectorSection::property_can_revert_changed));
  3000. }
  3001. section_search = section_search->get_parent();
  3002. if (Object::cast_to<EditorInspector>(section_search)) {
  3003. // Skip sub-resource inspectors.
  3004. break;
  3005. }
  3006. }
  3007. ep->set_draw_warning(draw_warning);
  3008. ep->set_use_folding(use_folding);
  3009. ep->set_favoritable(can_favorite && !disable_favorite);
  3010. ep->set_checkable(checkable);
  3011. ep->set_checked(checked);
  3012. ep->set_keying(keying);
  3013. ep->set_read_only(property_read_only || all_read_only);
  3014. if (p.name.begins_with("metadata/")) {
  3015. Variant _default = Variant();
  3016. if (node != nullptr) {
  3017. _default = PropertyUtils::get_property_default_value(node, p.name, nullptr, &sstack, false, nullptr, nullptr);
  3018. }
  3019. ep->set_deletable(_default == Variant());
  3020. } else {
  3021. ep->set_deletable(deletable_properties);
  3022. }
  3023. }
  3024. if (ep && ep->is_favoritable() && current_favorites.has(p.name)) {
  3025. ep->favorited = true;
  3026. favorites_to_add[group][subgroup].push_back(ep);
  3027. } else {
  3028. current_vbox->add_child(editors[i].property_editor);
  3029. }
  3030. if (ep) {
  3031. // Eventually, set other properties/signals after the property editor got added to the tree.
  3032. bool update_all = (p.usage & PROPERTY_USAGE_UPDATE_ALL_IF_MODIFIED);
  3033. ep->connect("property_changed", callable_mp(this, &EditorInspector::_property_changed).bind(update_all));
  3034. ep->connect("property_keyed", callable_mp(this, &EditorInspector::_property_keyed));
  3035. ep->connect("property_deleted", callable_mp(this, &EditorInspector::_property_deleted), CONNECT_DEFERRED);
  3036. ep->connect("property_keyed_with_value", callable_mp(this, &EditorInspector::_property_keyed_with_value));
  3037. ep->connect("property_favorited", callable_mp(this, &EditorInspector::_set_property_favorited), CONNECT_DEFERRED);
  3038. ep->connect("property_checked", callable_mp(this, &EditorInspector::_property_checked));
  3039. ep->connect("property_pinned", callable_mp(this, &EditorInspector::_property_pinned));
  3040. ep->connect("selected", callable_mp(this, &EditorInspector::_property_selected));
  3041. ep->connect("multiple_properties_changed", callable_mp(this, &EditorInspector::_multiple_properties_changed));
  3042. ep->connect("resource_selected", callable_mp(this, &EditorInspector::_resource_selected), CONNECT_DEFERRED);
  3043. ep->connect("object_id_selected", callable_mp(this, &EditorInspector::_object_id_selected), CONNECT_DEFERRED);
  3044. if (use_doc_hints) {
  3045. // `|` separators used in `EditorHelpBit`.
  3046. if (theme_item_name.is_empty()) {
  3047. if (p.name.contains("shader_parameter/")) {
  3048. ShaderMaterial *shader_material = Object::cast_to<ShaderMaterial>(object);
  3049. if (shader_material) {
  3050. ep->set_tooltip_text("property|" + shader_material->get_shader()->get_path() + "|" + property_prefix + p.name);
  3051. }
  3052. } else if (p.usage & PROPERTY_USAGE_INTERNAL) {
  3053. ep->set_tooltip_text("internal_property|" + classname + "|" + property_prefix + p.name);
  3054. } else {
  3055. ep->set_tooltip_text("property|" + classname + "|" + property_prefix + p.name);
  3056. }
  3057. } else {
  3058. ep->set_tooltip_text("theme_item|" + classname + "|" + theme_item_name);
  3059. }
  3060. ep->has_doc_tooltip = true;
  3061. }
  3062. ep->set_doc_path(doc_path);
  3063. ep->set_internal(p.usage & PROPERTY_USAGE_INTERNAL);
  3064. ep->update_property();
  3065. ep->_update_flags();
  3066. ep->update_editor_property_status();
  3067. ep->update_cache();
  3068. if (current_selected && ep->property == current_selected) {
  3069. ep->select(current_focusable);
  3070. }
  3071. }
  3072. }
  3073. }
  3074. if (!current_favorites.is_empty()) {
  3075. favorites_section->show();
  3076. // Organize the favorited properties in their sections, to keep context and differentiate from others with the same name.
  3077. bool is_localized = property_name_style == EditorPropertyNameProcessor::STYLE_LOCALIZED;
  3078. for (const KeyValue<String, HashMap<String, LocalVector<EditorProperty *>>> &KV : favorites_to_add) {
  3079. String section_name = KV.key;
  3080. String label;
  3081. String tooltip;
  3082. VBoxContainer *parent_vbox = favorites_vbox;
  3083. if (!section_name.is_empty()) {
  3084. if (is_localized) {
  3085. label = EditorPropertyNameProcessor::get_singleton()->translate_group_name(section_name);
  3086. tooltip = section_name;
  3087. } else {
  3088. label = section_name;
  3089. tooltip = EditorPropertyNameProcessor::get_singleton()->translate_group_name(section_name);
  3090. }
  3091. EditorInspectorSection *section = memnew(EditorInspectorSection);
  3092. favorites_groups_vbox->add_child(section);
  3093. parent_vbox = section->get_vbox();
  3094. section->setup("", section_name, object, sscolor, false);
  3095. section->set_tooltip_text(tooltip);
  3096. }
  3097. for (const KeyValue<String, LocalVector<EditorProperty *>> &KV2 : KV.value) {
  3098. section_name = KV2.key;
  3099. VBoxContainer *vbox = parent_vbox;
  3100. if (!section_name.is_empty()) {
  3101. if (is_localized) {
  3102. label = EditorPropertyNameProcessor::get_singleton()->translate_group_name(section_name);
  3103. tooltip = section_name;
  3104. } else {
  3105. label = section_name;
  3106. tooltip = EditorPropertyNameProcessor::get_singleton()->translate_group_name(section_name);
  3107. }
  3108. EditorInspectorSection *section = memnew(EditorInspectorSection);
  3109. vbox->add_child(section);
  3110. vbox = section->get_vbox();
  3111. section->setup("", section_name, object, sscolor, false);
  3112. section->set_tooltip_text(tooltip);
  3113. }
  3114. for (EditorProperty *ep : KV2.value) {
  3115. vbox->add_child(ep);
  3116. }
  3117. }
  3118. }
  3119. // Show a separator if there's no category to clearly divide the properties.
  3120. favorites_separator->hide();
  3121. if (main_vbox->get_child_count() > 0) {
  3122. EditorInspectorCategory *category = Object::cast_to<EditorInspectorCategory>(main_vbox->get_child(0));
  3123. if (!category) {
  3124. favorites_separator->show();
  3125. }
  3126. }
  3127. // Clean up empty sections.
  3128. for (List<EditorInspectorSection *>::Element *I = sections.back(); I; I = I->prev()) {
  3129. EditorInspectorSection *section = I->get();
  3130. if (section->get_vbox()->get_child_count() == 0) {
  3131. sections.erase(section);
  3132. vbox_per_path[main_vbox].erase(section->get_section());
  3133. memdelete(section);
  3134. }
  3135. }
  3136. }
  3137. if (!hide_metadata && !object->call("_hide_metadata_from_inspector")) {
  3138. // Add 4px of spacing between the "Add Metadata" button and the content above it.
  3139. Control *spacer = memnew(Control);
  3140. spacer->set_custom_minimum_size(Size2(0, 4) * EDSCALE);
  3141. main_vbox->add_child(spacer);
  3142. Button *add_md = EditorInspector::create_inspector_action_button(TTR("Add Metadata"));
  3143. add_md->set_button_icon(get_editor_theme_icon(SNAME("Add")));
  3144. add_md->connect(SceneStringName(pressed), callable_mp(this, &EditorInspector::_show_add_meta_dialog));
  3145. main_vbox->add_child(add_md);
  3146. if (all_read_only) {
  3147. add_md->set_disabled(true);
  3148. }
  3149. }
  3150. // Get the lists of to add at the end.
  3151. for (Ref<EditorInspectorPlugin> &ped : valid_plugins) {
  3152. ped->parse_end(object);
  3153. _parse_added_editors(main_vbox, nullptr, ped);
  3154. }
  3155. if (is_main_editor_inspector()) {
  3156. // Updating inspector might invalidate some editing owners.
  3157. EditorNode::get_singleton()->hide_unused_editors();
  3158. }
  3159. get_root_inspector()->set_follow_focus(true);
  3160. }
  3161. void EditorInspector::update_property(const String &p_prop) {
  3162. if (!editor_property_map.has(p_prop)) {
  3163. return;
  3164. }
  3165. for (EditorProperty *E : editor_property_map[p_prop]) {
  3166. E->update_property();
  3167. E->update_editor_property_status();
  3168. E->update_cache();
  3169. }
  3170. }
  3171. void EditorInspector::_clear(bool p_hide_plugins) {
  3172. begin_vbox->hide();
  3173. while (begin_vbox->get_child_count()) {
  3174. memdelete(begin_vbox->get_child(0));
  3175. }
  3176. favorites_section->hide();
  3177. while (favorites_vbox->get_child_count()) {
  3178. memdelete(favorites_vbox->get_child(0));
  3179. }
  3180. while (favorites_groups_vbox->get_child_count()) {
  3181. memdelete(favorites_groups_vbox->get_child(0));
  3182. }
  3183. while (main_vbox->get_child_count()) {
  3184. memdelete(main_vbox->get_child(0));
  3185. }
  3186. property_selected = StringName();
  3187. property_focusable = -1;
  3188. editor_property_map.clear();
  3189. sections.clear();
  3190. pending.clear();
  3191. restart_request_props.clear();
  3192. if (p_hide_plugins && is_main_editor_inspector()) {
  3193. EditorNode::get_singleton()->hide_unused_editors(this);
  3194. }
  3195. }
  3196. Object *EditorInspector::get_edited_object() {
  3197. return object;
  3198. }
  3199. Object *EditorInspector::get_next_edited_object() {
  3200. return next_object;
  3201. }
  3202. void EditorInspector::edit(Object *p_object) {
  3203. if (object == p_object) {
  3204. return;
  3205. }
  3206. next_object = p_object; // Some plugins need to know the next edited object when clearing the inspector.
  3207. if (object) {
  3208. if (likely(Variant(object).get_validated_object())) {
  3209. object->disconnect(CoreStringName(property_list_changed), callable_mp(this, &EditorInspector::_changed_callback));
  3210. }
  3211. _clear();
  3212. }
  3213. per_array_page.clear();
  3214. object = p_object;
  3215. if (object) {
  3216. update_scroll_request = 0; //reset
  3217. if (scroll_cache.has(object->get_instance_id())) { //if exists, set something else
  3218. update_scroll_request = scroll_cache[object->get_instance_id()]; //done this way because wait until full size is accommodated
  3219. }
  3220. object->connect(CoreStringName(property_list_changed), callable_mp(this, &EditorInspector::_changed_callback));
  3221. can_favorite = Object::cast_to<Node>(object) || Object::cast_to<Resource>(object);
  3222. _update_current_favorites();
  3223. update_tree();
  3224. }
  3225. // Keep it available until the end so it works with both main and sub inspectors.
  3226. next_object = nullptr;
  3227. emit_signal(SNAME("edited_object_changed"));
  3228. }
  3229. void EditorInspector::set_keying(bool p_active) {
  3230. if (keying == p_active) {
  3231. return;
  3232. }
  3233. keying = p_active;
  3234. _keying_changed();
  3235. }
  3236. void EditorInspector::_keying_changed() {
  3237. for (const KeyValue<StringName, List<EditorProperty *>> &F : editor_property_map) {
  3238. for (EditorProperty *E : F.value) {
  3239. if (E) {
  3240. E->set_keying(keying);
  3241. }
  3242. }
  3243. }
  3244. }
  3245. void EditorInspector::set_read_only(bool p_read_only) {
  3246. if (p_read_only == read_only) {
  3247. return;
  3248. }
  3249. read_only = p_read_only;
  3250. update_tree();
  3251. }
  3252. EditorPropertyNameProcessor::Style EditorInspector::get_property_name_style() const {
  3253. return property_name_style;
  3254. }
  3255. void EditorInspector::set_property_name_style(EditorPropertyNameProcessor::Style p_style) {
  3256. if (property_name_style == p_style) {
  3257. return;
  3258. }
  3259. property_name_style = p_style;
  3260. update_tree();
  3261. }
  3262. void EditorInspector::set_use_settings_name_style(bool p_enable) {
  3263. if (use_settings_name_style == p_enable) {
  3264. return;
  3265. }
  3266. use_settings_name_style = p_enable;
  3267. if (use_settings_name_style) {
  3268. set_property_name_style(EditorPropertyNameProcessor::get_singleton()->get_settings_style());
  3269. }
  3270. }
  3271. void EditorInspector::set_autoclear(bool p_enable) {
  3272. autoclear = p_enable;
  3273. }
  3274. void EditorInspector::set_show_categories(bool p_show_standard, bool p_show_custom) {
  3275. show_standard_categories = p_show_standard;
  3276. show_custom_categories = p_show_custom;
  3277. update_tree();
  3278. }
  3279. void EditorInspector::set_use_doc_hints(bool p_enable) {
  3280. use_doc_hints = p_enable;
  3281. update_tree();
  3282. }
  3283. void EditorInspector::set_hide_script(bool p_hide) {
  3284. hide_script = p_hide;
  3285. update_tree();
  3286. }
  3287. void EditorInspector::set_hide_metadata(bool p_hide) {
  3288. hide_metadata = p_hide;
  3289. update_tree();
  3290. }
  3291. void EditorInspector::set_use_filter(bool p_use) {
  3292. use_filter = p_use;
  3293. update_tree();
  3294. }
  3295. void EditorInspector::register_text_enter(Node *p_line_edit) {
  3296. search_box = Object::cast_to<LineEdit>(p_line_edit);
  3297. if (search_box) {
  3298. search_box->connect(SceneStringName(text_changed), callable_mp(this, &EditorInspector::_filter_changed));
  3299. }
  3300. }
  3301. void EditorInspector::_filter_changed(const String &p_text) {
  3302. update_tree();
  3303. }
  3304. void EditorInspector::set_use_folding(bool p_use_folding, bool p_update_tree) {
  3305. use_folding = p_use_folding;
  3306. if (p_update_tree) {
  3307. update_tree();
  3308. }
  3309. }
  3310. bool EditorInspector::is_using_folding() {
  3311. return use_folding;
  3312. }
  3313. void EditorInspector::collapse_all_folding() {
  3314. for (EditorInspectorSection *E : sections) {
  3315. E->fold();
  3316. }
  3317. for (const KeyValue<StringName, List<EditorProperty *>> &F : editor_property_map) {
  3318. for (EditorProperty *E : F.value) {
  3319. E->collapse_all_folding();
  3320. }
  3321. }
  3322. }
  3323. void EditorInspector::expand_all_folding() {
  3324. for (EditorInspectorSection *E : sections) {
  3325. E->unfold();
  3326. }
  3327. for (const KeyValue<StringName, List<EditorProperty *>> &F : editor_property_map) {
  3328. for (EditorProperty *E : F.value) {
  3329. E->expand_all_folding();
  3330. }
  3331. }
  3332. }
  3333. void EditorInspector::expand_revertable() {
  3334. HashSet<EditorInspectorSection *> sections_to_unfold[2];
  3335. for (EditorInspectorSection *E : sections) {
  3336. if (E->has_revertable_properties()) {
  3337. sections_to_unfold[0].insert(E);
  3338. }
  3339. }
  3340. // Climb up the hierarchy doing double buffering with the sets.
  3341. int a = 0;
  3342. int b = 1;
  3343. while (sections_to_unfold[a].size()) {
  3344. for (EditorInspectorSection *E : sections_to_unfold[a]) {
  3345. E->unfold();
  3346. Node *n = E->get_parent();
  3347. while (n) {
  3348. if (Object::cast_to<EditorInspector>(n)) {
  3349. break;
  3350. }
  3351. if (Object::cast_to<EditorInspectorSection>(n) && !sections_to_unfold[a].has((EditorInspectorSection *)n)) {
  3352. sections_to_unfold[b].insert((EditorInspectorSection *)n);
  3353. }
  3354. n = n->get_parent();
  3355. }
  3356. }
  3357. sections_to_unfold[a].clear();
  3358. SWAP(a, b);
  3359. }
  3360. for (const KeyValue<StringName, List<EditorProperty *>> &F : editor_property_map) {
  3361. for (EditorProperty *E : F.value) {
  3362. E->expand_revertable();
  3363. }
  3364. }
  3365. }
  3366. void EditorInspector::set_scroll_offset(int p_offset) {
  3367. set_v_scroll(p_offset);
  3368. }
  3369. int EditorInspector::get_scroll_offset() const {
  3370. return get_v_scroll();
  3371. }
  3372. void EditorInspector::set_use_wide_editors(bool p_enable) {
  3373. wide_editors = p_enable;
  3374. }
  3375. void EditorInspector::set_root_inspector(EditorInspector *p_root_inspector) {
  3376. root_inspector = p_root_inspector;
  3377. // Only the root inspector should follow focus.
  3378. set_follow_focus(false);
  3379. }
  3380. void EditorInspector::set_use_deletable_properties(bool p_enabled) {
  3381. deletable_properties = p_enabled;
  3382. }
  3383. void EditorInspector::_page_change_request(int p_new_page, const StringName &p_array_prefix) {
  3384. int prev_page = per_array_page.has(p_array_prefix) ? per_array_page[p_array_prefix] : 0;
  3385. int new_page = MAX(0, p_new_page);
  3386. if (new_page != prev_page) {
  3387. per_array_page[p_array_prefix] = new_page;
  3388. update_tree_pending = true;
  3389. }
  3390. }
  3391. void EditorInspector::_edit_request_change(Object *p_object, const String &p_property) {
  3392. if (object != p_object) { //may be undoing/redoing for a non edited object, so ignore
  3393. return;
  3394. }
  3395. if (changing) {
  3396. return;
  3397. }
  3398. if (p_property.is_empty()) {
  3399. update_tree_pending = true;
  3400. } else {
  3401. pending.insert(p_property);
  3402. }
  3403. }
  3404. void EditorInspector::_edit_set(const String &p_name, const Variant &p_value, bool p_refresh_all, const String &p_changed_field) {
  3405. if (autoclear && editor_property_map.has(p_name)) {
  3406. for (EditorProperty *E : editor_property_map[p_name]) {
  3407. if (E->is_checkable()) {
  3408. E->set_checked(true);
  3409. }
  3410. }
  3411. }
  3412. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  3413. if (bool(object->call("_dont_undo_redo"))) {
  3414. object->set(p_name, p_value);
  3415. if (p_refresh_all) {
  3416. _edit_request_change(object, "");
  3417. } else {
  3418. _edit_request_change(object, p_name);
  3419. }
  3420. emit_signal(_prop_edited, p_name);
  3421. } else if (Object::cast_to<MultiNodeEdit>(object)) {
  3422. Object::cast_to<MultiNodeEdit>(object)->set_property_field(p_name, p_value, p_changed_field);
  3423. _edit_request_change(object, p_name);
  3424. emit_signal(_prop_edited, p_name);
  3425. } else {
  3426. undo_redo->create_action(vformat(TTR("Set %s"), p_name), UndoRedo::MERGE_ENDS);
  3427. undo_redo->add_do_property(object, p_name, p_value);
  3428. bool valid = false;
  3429. Variant value = object->get(p_name, &valid);
  3430. if (valid) {
  3431. if (Object::cast_to<Control>(object) && (p_name == "anchors_preset" || p_name == "layout_mode")) {
  3432. undo_redo->add_undo_method(object, "_edit_set_state", Object::cast_to<Control>(object)->_edit_get_state());
  3433. } else {
  3434. undo_redo->add_undo_property(object, p_name, value);
  3435. }
  3436. }
  3437. List<StringName> linked_properties;
  3438. ClassDB::get_linked_properties_info(object->get_class_name(), p_name, &linked_properties);
  3439. for (const StringName &linked_prop : linked_properties) {
  3440. valid = false;
  3441. Variant undo_value = object->get(linked_prop, &valid);
  3442. if (valid) {
  3443. undo_redo->add_undo_property(object, linked_prop, undo_value);
  3444. }
  3445. }
  3446. PackedStringArray linked_properties_dynamic = object->call("_get_linked_undo_properties", p_name, p_value);
  3447. for (int i = 0; i < linked_properties_dynamic.size(); i++) {
  3448. valid = false;
  3449. Variant undo_value = object->get(linked_properties_dynamic[i], &valid);
  3450. if (valid) {
  3451. undo_redo->add_undo_property(object, linked_properties_dynamic[i], undo_value);
  3452. }
  3453. }
  3454. Variant v_undo_redo = undo_redo;
  3455. Variant v_object = object;
  3456. Variant v_name = p_name;
  3457. const Vector<Callable> &callbacks = EditorNode::get_editor_data().get_undo_redo_inspector_hook_callback();
  3458. for (int i = 0; i < callbacks.size(); i++) {
  3459. const Callable &callback = callbacks[i];
  3460. const Variant *p_arguments[] = { &v_undo_redo, &v_object, &v_name, &p_value };
  3461. Variant return_value;
  3462. Callable::CallError call_error;
  3463. callback.callp(p_arguments, 4, return_value, call_error);
  3464. if (call_error.error != Callable::CallError::CALL_OK) {
  3465. ERR_PRINT("Invalid UndoRedo callback.");
  3466. }
  3467. }
  3468. if (p_refresh_all) {
  3469. undo_redo->add_do_method(this, "_edit_request_change", object, "");
  3470. undo_redo->add_undo_method(this, "_edit_request_change", object, "");
  3471. } else {
  3472. undo_redo->add_do_method(this, "_edit_request_change", object, p_name);
  3473. undo_redo->add_undo_method(this, "_edit_request_change", object, p_name);
  3474. }
  3475. Resource *r = Object::cast_to<Resource>(object);
  3476. if (r) {
  3477. if (String(p_name) == "resource_local_to_scene") {
  3478. bool prev = object->get(p_name);
  3479. bool next = p_value;
  3480. if (next) {
  3481. undo_redo->add_do_method(r, "setup_local_to_scene");
  3482. }
  3483. if (prev) {
  3484. undo_redo->add_undo_method(r, "setup_local_to_scene");
  3485. }
  3486. }
  3487. }
  3488. undo_redo->add_do_method(this, "emit_signal", _prop_edited, p_name);
  3489. undo_redo->add_undo_method(this, "emit_signal", _prop_edited, p_name);
  3490. undo_redo->commit_action();
  3491. }
  3492. if (editor_property_map.has(p_name)) {
  3493. for (EditorProperty *E : editor_property_map[p_name]) {
  3494. E->update_editor_property_status();
  3495. }
  3496. }
  3497. }
  3498. void EditorInspector::_property_changed(const String &p_path, const Variant &p_value, const String &p_name, bool p_changing, bool p_update_all) {
  3499. // The "changing" variable must be true for properties that trigger events as typing occurs,
  3500. // like "text_changed" signal. E.g. text property of Label, Button, RichTextLabel, etc.
  3501. if (p_changing) {
  3502. changing++;
  3503. }
  3504. _edit_set(p_path, p_value, p_update_all, p_name);
  3505. if (p_changing) {
  3506. changing--;
  3507. }
  3508. if (restart_request_props.has(p_path)) {
  3509. emit_signal(SNAME("restart_requested"));
  3510. }
  3511. }
  3512. void EditorInspector::_multiple_properties_changed(const Vector<String> &p_paths, const Array &p_values, bool p_changing) {
  3513. ERR_FAIL_COND(p_paths.is_empty() || p_values.is_empty());
  3514. ERR_FAIL_COND(p_paths.size() != p_values.size());
  3515. String names;
  3516. for (int i = 0; i < p_paths.size(); i++) {
  3517. if (i > 0) {
  3518. names += ",";
  3519. }
  3520. names += p_paths[i];
  3521. }
  3522. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  3523. // TRANSLATORS: This is describing a change to multiple properties at once. The parameter is a list of property names.
  3524. undo_redo->create_action(vformat(TTR("Set Multiple: %s"), names), UndoRedo::MERGE_ENDS);
  3525. for (int i = 0; i < p_paths.size(); i++) {
  3526. _edit_set(p_paths[i], p_values[i], false, "");
  3527. if (restart_request_props.has(p_paths[i])) {
  3528. emit_signal(SNAME("restart_requested"));
  3529. }
  3530. }
  3531. if (p_changing) {
  3532. changing++;
  3533. }
  3534. undo_redo->commit_action();
  3535. if (p_changing) {
  3536. changing--;
  3537. }
  3538. }
  3539. void EditorInspector::_property_keyed(const String &p_path, bool p_advance) {
  3540. if (!object) {
  3541. return;
  3542. }
  3543. // The second parameter could be null, causing the event to fire with less arguments, so use the pointer call which preserves it.
  3544. const Variant args[3] = { p_path, object->get(p_path), p_advance };
  3545. const Variant *argp[3] = { &args[0], &args[1], &args[2] };
  3546. emit_signalp(SNAME("property_keyed"), argp, 3);
  3547. }
  3548. void EditorInspector::_property_deleted(const String &p_path) {
  3549. if (!object) {
  3550. return;
  3551. }
  3552. if (p_path.begins_with("metadata/")) {
  3553. String name = p_path.replace_first("metadata/", "");
  3554. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  3555. undo_redo->create_action(vformat(TTR("Remove metadata %s"), name));
  3556. undo_redo->add_do_method(object, "remove_meta", name);
  3557. undo_redo->add_undo_method(object, "set_meta", name, object->get_meta(name));
  3558. undo_redo->commit_action();
  3559. }
  3560. emit_signal(SNAME("property_deleted"), p_path);
  3561. }
  3562. void EditorInspector::_property_keyed_with_value(const String &p_path, const Variant &p_value, bool p_advance) {
  3563. if (!object) {
  3564. return;
  3565. }
  3566. // The second parameter could be null, causing the event to fire with less arguments, so use the pointer call which preserves it.
  3567. const Variant args[3] = { p_path, p_value, p_advance };
  3568. const Variant *argp[3] = { &args[0], &args[1], &args[2] };
  3569. emit_signalp(SNAME("property_keyed"), argp, 3);
  3570. }
  3571. void EditorInspector::_property_checked(const String &p_path, bool p_checked) {
  3572. if (!object) {
  3573. return;
  3574. }
  3575. //property checked
  3576. if (autoclear) {
  3577. if (!p_checked) {
  3578. _edit_set(p_path, Variant(), false, "");
  3579. } else {
  3580. Variant to_create;
  3581. List<PropertyInfo> pinfo;
  3582. object->get_property_list(&pinfo);
  3583. for (const PropertyInfo &E : pinfo) {
  3584. if (E.name == p_path) {
  3585. Callable::CallError ce;
  3586. Variant::construct(E.type, to_create, nullptr, 0, ce);
  3587. break;
  3588. }
  3589. }
  3590. _edit_set(p_path, to_create, false, "");
  3591. }
  3592. if (editor_property_map.has(p_path)) {
  3593. for (EditorProperty *E : editor_property_map[p_path]) {
  3594. E->set_checked(p_checked);
  3595. E->update_property();
  3596. E->update_editor_property_status();
  3597. E->update_cache();
  3598. }
  3599. }
  3600. } else {
  3601. emit_signal(SNAME("property_toggled"), p_path, p_checked);
  3602. }
  3603. }
  3604. void EditorInspector::_property_pinned(const String &p_path, bool p_pinned) {
  3605. if (!object) {
  3606. return;
  3607. }
  3608. Node *node = Object::cast_to<Node>(object);
  3609. ERR_FAIL_NULL(node);
  3610. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  3611. undo_redo->create_action(vformat(p_pinned ? TTR("Pinned %s") : TTR("Unpinned %s"), p_path));
  3612. undo_redo->add_do_method(node, "_set_property_pinned", p_path, p_pinned);
  3613. undo_redo->add_undo_method(node, "_set_property_pinned", p_path, !p_pinned);
  3614. if (editor_property_map.has(p_path)) {
  3615. for (List<EditorProperty *>::Element *E = editor_property_map[p_path].front(); E; E = E->next()) {
  3616. undo_redo->add_do_method(E->get(), "_update_editor_property_status");
  3617. undo_redo->add_undo_method(E->get(), "_update_editor_property_status");
  3618. }
  3619. }
  3620. undo_redo->commit_action();
  3621. }
  3622. void EditorInspector::_property_selected(const String &p_path, int p_focusable) {
  3623. property_selected = p_path;
  3624. property_focusable = p_focusable;
  3625. // Deselect the others.
  3626. for (const KeyValue<StringName, List<EditorProperty *>> &F : editor_property_map) {
  3627. if (F.key == property_selected) {
  3628. continue;
  3629. }
  3630. for (EditorProperty *E : F.value) {
  3631. if (E->is_selected()) {
  3632. E->deselect();
  3633. }
  3634. }
  3635. }
  3636. emit_signal(SNAME("property_selected"), p_path);
  3637. }
  3638. void EditorInspector::_object_id_selected(const String &p_path, ObjectID p_id) {
  3639. emit_signal(SNAME("object_id_selected"), p_id);
  3640. }
  3641. void EditorInspector::_resource_selected(const String &p_path, Ref<Resource> p_resource) {
  3642. emit_signal(SNAME("resource_selected"), p_resource, p_path);
  3643. }
  3644. void EditorInspector::_node_removed(Node *p_node) {
  3645. if (p_node == object) {
  3646. edit(nullptr);
  3647. }
  3648. }
  3649. void EditorInspector::_update_current_favorites() {
  3650. current_favorites.clear();
  3651. if (!can_favorite) {
  3652. return;
  3653. }
  3654. HashMap<String, PackedStringArray> favorites = EditorSettings::get_singleton()->get_favorite_properties();
  3655. // Fetch script properties.
  3656. Ref<Script> scr = object->get_script();
  3657. if (scr.is_valid()) {
  3658. List<PropertyInfo> plist;
  3659. // FIXME: Only properties from a saved script will be available, unsaved ones will be ignored.
  3660. // Can cause a little wonkiness, while nothing serious, would be nice to find a way to get
  3661. // unsaved ones without needing to get the entire property list of an object.
  3662. scr->get_script_property_list(&plist);
  3663. String path;
  3664. HashMap<String, LocalVector<String>> props;
  3665. for (PropertyInfo &p : plist) {
  3666. if (p.usage & PROPERTY_USAGE_CATEGORY) {
  3667. path = favorites.has(p.hint_string) ? p.hint_string : String();
  3668. } else if (p.usage & PROPERTY_USAGE_SCRIPT_VARIABLE && !path.is_empty()) {
  3669. props[path].push_back(p.name);
  3670. }
  3671. }
  3672. // Add favorited properties while removing invalid ones.
  3673. bool invalid_props = false;
  3674. for (const KeyValue<String, LocalVector<String>> &KV : props) {
  3675. path = KV.key;
  3676. for (int i = 0; i < favorites[path].size(); i++) {
  3677. String prop = favorites[path][i];
  3678. if (KV.value.has(prop)) {
  3679. current_favorites.append(prop);
  3680. } else {
  3681. invalid_props = true;
  3682. favorites[path].erase(prop);
  3683. i--;
  3684. }
  3685. }
  3686. if (favorites[path].is_empty()) {
  3687. favorites.erase(path);
  3688. }
  3689. }
  3690. if (invalid_props) {
  3691. EditorSettings::get_singleton()->set_favorite_properties(favorites);
  3692. }
  3693. }
  3694. // Fetch built-in properties.
  3695. StringName class_name = object->get_class_name();
  3696. for (const KeyValue<String, PackedStringArray> &KV : favorites) {
  3697. if (ClassDB::is_parent_class(class_name, KV.key)) {
  3698. current_favorites.append_array(KV.value);
  3699. }
  3700. }
  3701. }
  3702. void EditorInspector::_set_property_favorited(const String &p_path, bool p_favorited) {
  3703. if (!object) {
  3704. return;
  3705. }
  3706. StringName class_name = object->get_class_name();
  3707. while (!class_name.is_empty()) {
  3708. bool has_prop = ClassDB::has_property(class_name, p_path, true);
  3709. if (has_prop) {
  3710. break;
  3711. }
  3712. class_name = ClassDB::get_parent_class_nocheck(class_name);
  3713. }
  3714. if (class_name.is_empty()) {
  3715. Ref<Script> scr = object->get_script();
  3716. if (scr.is_valid()) {
  3717. List<PropertyInfo> plist;
  3718. scr->get_script_property_list(&plist);
  3719. String path;
  3720. for (PropertyInfo &p : plist) {
  3721. if (p.usage & PROPERTY_USAGE_CATEGORY) {
  3722. path = p.hint_string;
  3723. } else if (p.usage & PROPERTY_USAGE_SCRIPT_VARIABLE && p.name == p_path) {
  3724. class_name = path;
  3725. break;
  3726. }
  3727. }
  3728. }
  3729. ERR_FAIL_COND_MSG(class_name.is_empty(), "Can't favorite invalid property. If said property was from a script and recently renamed, try saving it first.");
  3730. }
  3731. HashMap<String, PackedStringArray> favorites = EditorSettings::get_singleton()->get_favorite_properties();
  3732. if (p_favorited) {
  3733. current_favorites.append(p_path);
  3734. favorites[class_name].append(p_path);
  3735. } else {
  3736. current_favorites.erase(p_path);
  3737. if (favorites.has(class_name) && favorites[class_name].has(p_path)) {
  3738. if (favorites[class_name].size() > 1) {
  3739. favorites[class_name].erase(p_path);
  3740. } else {
  3741. favorites.erase(class_name);
  3742. }
  3743. }
  3744. }
  3745. EditorSettings::get_singleton()->set_favorite_properties(favorites);
  3746. update_tree();
  3747. }
  3748. void EditorInspector::_clear_current_favorites() {
  3749. current_favorites.clear();
  3750. HashMap<String, PackedStringArray> favorites = EditorSettings::get_singleton()->get_favorite_properties();
  3751. Ref<Script> scr = object->get_script();
  3752. if (scr.is_valid()) {
  3753. List<PropertyInfo> plist;
  3754. scr->get_script_property_list(&plist);
  3755. for (PropertyInfo &p : plist) {
  3756. if (p.usage & PROPERTY_USAGE_CATEGORY && favorites.has(p.hint_string)) {
  3757. favorites.erase(p.hint_string);
  3758. }
  3759. }
  3760. }
  3761. StringName class_name = object->get_class_name();
  3762. while (class_name) {
  3763. if (favorites.has(class_name)) {
  3764. favorites.erase(class_name);
  3765. }
  3766. class_name = ClassDB::get_parent_class(class_name);
  3767. }
  3768. EditorSettings::get_singleton()->set_favorite_properties(favorites);
  3769. update_tree();
  3770. }
  3771. void EditorInspector::_notification(int p_what) {
  3772. switch (p_what) {
  3773. case NOTIFICATION_THEME_CHANGED: {
  3774. favorites_category->icon = get_editor_theme_icon(SNAME("Favorites"));
  3775. int separation = get_theme_constant(SNAME("v_separation"), SNAME("EditorInspector"));
  3776. base_vbox->add_theme_constant_override("separation", separation);
  3777. begin_vbox->add_theme_constant_override("separation", separation);
  3778. favorites_section->add_theme_constant_override("separation", separation);
  3779. favorites_groups_vbox->add_theme_constant_override("separation", separation);
  3780. main_vbox->add_theme_constant_override("separation", separation);
  3781. } break;
  3782. case NOTIFICATION_READY: {
  3783. ERR_FAIL_NULL(EditorFeatureProfileManager::get_singleton());
  3784. EditorFeatureProfileManager::get_singleton()->connect("current_feature_profile_changed", callable_mp(this, &EditorInspector::_feature_profile_changed));
  3785. set_process(is_visible_in_tree());
  3786. add_theme_style_override(SceneStringName(panel), get_theme_stylebox(SceneStringName(panel), SNAME("Tree")));
  3787. if (!is_sub_inspector()) {
  3788. get_tree()->connect("node_removed", callable_mp(this, &EditorInspector::_node_removed));
  3789. }
  3790. } break;
  3791. case NOTIFICATION_PREDELETE: {
  3792. if (!is_sub_inspector() && is_inside_tree()) {
  3793. get_tree()->disconnect("node_removed", callable_mp(this, &EditorInspector::_node_removed));
  3794. }
  3795. edit(nullptr);
  3796. } break;
  3797. case NOTIFICATION_VISIBILITY_CHANGED: {
  3798. set_process(is_visible_in_tree());
  3799. } break;
  3800. case NOTIFICATION_PROCESS: {
  3801. if (update_scroll_request >= 0) {
  3802. callable_mp((Range *)get_v_scroll_bar(), &Range::set_value).call_deferred(update_scroll_request);
  3803. update_scroll_request = -1;
  3804. }
  3805. if (update_tree_pending) {
  3806. refresh_countdown = float(EDITOR_GET("docks/property_editor/auto_refresh_interval"));
  3807. } else if (refresh_countdown > 0) {
  3808. refresh_countdown -= get_process_delta_time();
  3809. if (refresh_countdown <= 0) {
  3810. for (const KeyValue<StringName, List<EditorProperty *>> &F : editor_property_map) {
  3811. for (EditorProperty *E : F.value) {
  3812. if (E && !E->is_cache_valid()) {
  3813. E->update_property();
  3814. E->update_editor_property_status();
  3815. E->update_cache();
  3816. }
  3817. }
  3818. }
  3819. refresh_countdown = float(EDITOR_GET("docks/property_editor/auto_refresh_interval"));
  3820. }
  3821. }
  3822. changing++;
  3823. if (update_tree_pending) {
  3824. update_tree();
  3825. update_tree_pending = false;
  3826. pending.clear();
  3827. } else {
  3828. while (pending.size()) {
  3829. StringName prop = *pending.begin();
  3830. if (editor_property_map.has(prop)) {
  3831. for (EditorProperty *E : editor_property_map[prop]) {
  3832. E->update_property();
  3833. E->update_editor_property_status();
  3834. E->update_cache();
  3835. }
  3836. }
  3837. pending.remove(pending.begin());
  3838. }
  3839. }
  3840. changing--;
  3841. } break;
  3842. case EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED: {
  3843. bool needs_update = false;
  3844. if (!is_sub_inspector() && EditorThemeManager::is_generated_theme_outdated()) {
  3845. add_theme_style_override(SceneStringName(panel), get_theme_stylebox(SceneStringName(panel), SNAME("Tree")));
  3846. }
  3847. if (use_settings_name_style && EditorSettings::get_singleton()->check_changed_settings_in_group("interface/editor/localize_settings")) {
  3848. EditorPropertyNameProcessor::Style style = EditorPropertyNameProcessor::get_settings_style();
  3849. if (property_name_style != style) {
  3850. property_name_style = style;
  3851. needs_update = true;
  3852. }
  3853. }
  3854. if (EditorSettings::get_singleton()->check_changed_settings_in_group("interface/inspector")) {
  3855. needs_update = true;
  3856. }
  3857. if (needs_update) {
  3858. update_tree();
  3859. }
  3860. } break;
  3861. }
  3862. }
  3863. void EditorInspector::_changed_callback() {
  3864. //this is called when property change is notified via notify_property_list_changed()
  3865. if (object != nullptr) {
  3866. _update_current_favorites();
  3867. _edit_request_change(object, String());
  3868. }
  3869. }
  3870. void EditorInspector::_vscroll_changed(double p_offset) {
  3871. if (update_scroll_request >= 0) { //waiting, do nothing
  3872. return;
  3873. }
  3874. if (object) {
  3875. scroll_cache[object->get_instance_id()] = p_offset;
  3876. }
  3877. }
  3878. void EditorInspector::set_property_prefix(const String &p_prefix) {
  3879. property_prefix = p_prefix;
  3880. }
  3881. String EditorInspector::get_property_prefix() const {
  3882. return property_prefix;
  3883. }
  3884. void EditorInspector::add_custom_property_description(const String &p_class, const String &p_property, const String &p_description) {
  3885. const String key = vformat("property|%s|%s", p_class, p_property);
  3886. custom_property_descriptions[key] = p_description;
  3887. }
  3888. String EditorInspector::get_custom_property_description(const String &p_property) const {
  3889. HashMap<String, String>::ConstIterator E = custom_property_descriptions.find(p_property);
  3890. if (E) {
  3891. return E->value;
  3892. }
  3893. return "";
  3894. }
  3895. void EditorInspector::set_object_class(const String &p_class) {
  3896. object_class = p_class;
  3897. }
  3898. String EditorInspector::get_object_class() const {
  3899. return object_class;
  3900. }
  3901. void EditorInspector::_feature_profile_changed() {
  3902. update_tree();
  3903. }
  3904. void EditorInspector::set_restrict_to_basic_settings(bool p_restrict) {
  3905. restrict_to_basic = p_restrict;
  3906. update_tree();
  3907. }
  3908. void EditorInspector::set_property_clipboard(const Variant &p_value) {
  3909. property_clipboard = p_value;
  3910. }
  3911. Variant EditorInspector::get_property_clipboard() const {
  3912. return property_clipboard;
  3913. }
  3914. void EditorInspector::_show_add_meta_dialog() {
  3915. if (!add_meta_dialog) {
  3916. add_meta_dialog = memnew(AddMetadataDialog());
  3917. add_meta_dialog->connect(SceneStringName(confirmed), callable_mp(this, &EditorInspector::_add_meta_confirm));
  3918. add_child(add_meta_dialog);
  3919. }
  3920. StringName dialog_title;
  3921. Node *node = Object::cast_to<Node>(object);
  3922. // If object is derived from Node use node name, if derived from Resource use classname.
  3923. dialog_title = node ? node->get_name() : StringName(object->get_class());
  3924. List<StringName> existing_meta_keys;
  3925. object->get_meta_list(&existing_meta_keys);
  3926. add_meta_dialog->open(dialog_title, existing_meta_keys);
  3927. }
  3928. void EditorInspector::_add_meta_confirm() {
  3929. // Ensure metadata is unfolded when adding a new metadata.
  3930. object->editor_set_section_unfold("metadata", true);
  3931. String name = add_meta_dialog->get_meta_name();
  3932. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  3933. undo_redo->create_action(vformat(TTR("Add metadata %s"), name));
  3934. undo_redo->add_do_method(object, "set_meta", name, add_meta_dialog->get_meta_defval());
  3935. undo_redo->add_undo_method(object, "remove_meta", name);
  3936. undo_redo->commit_action();
  3937. }
  3938. void EditorInspector::_handle_menu_option(int p_option) {
  3939. switch (p_option) {
  3940. case MENU_UNFAVORITE_ALL:
  3941. _clear_current_favorites();
  3942. break;
  3943. }
  3944. }
  3945. void EditorInspector::_bind_methods() {
  3946. ClassDB::bind_method(D_METHOD("edit", "object"), &EditorInspector::edit);
  3947. ClassDB::bind_method("_edit_request_change", &EditorInspector::_edit_request_change);
  3948. ClassDB::bind_method("get_selected_path", &EditorInspector::get_selected_path);
  3949. ClassDB::bind_method("get_edited_object", &EditorInspector::get_edited_object);
  3950. ClassDB::bind_static_method("EditorInspector", D_METHOD("instantiate_property_editor", "object", "type", "path", "hint", "hint_text", "usage", "wide"), &EditorInspector::instantiate_property_editor, DEFVAL(false));
  3951. ADD_SIGNAL(MethodInfo("property_selected", PropertyInfo(Variant::STRING, "property")));
  3952. ADD_SIGNAL(MethodInfo("property_keyed", PropertyInfo(Variant::STRING, "property"), PropertyInfo(Variant::NIL, "value", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NIL_IS_VARIANT), PropertyInfo(Variant::BOOL, "advance")));
  3953. ADD_SIGNAL(MethodInfo("property_deleted", PropertyInfo(Variant::STRING, "property")));
  3954. ADD_SIGNAL(MethodInfo("resource_selected", PropertyInfo(Variant::OBJECT, "resource", PROPERTY_HINT_RESOURCE_TYPE, "Resource"), PropertyInfo(Variant::STRING, "path")));
  3955. ADD_SIGNAL(MethodInfo("object_id_selected", PropertyInfo(Variant::INT, "id")));
  3956. ADD_SIGNAL(MethodInfo("property_edited", PropertyInfo(Variant::STRING, "property")));
  3957. ADD_SIGNAL(MethodInfo("property_toggled", PropertyInfo(Variant::STRING, "property"), PropertyInfo(Variant::BOOL, "checked")));
  3958. ADD_SIGNAL(MethodInfo("edited_object_changed"));
  3959. ADD_SIGNAL(MethodInfo("restart_requested"));
  3960. }
  3961. EditorInspector::EditorInspector() {
  3962. object = nullptr;
  3963. base_vbox = memnew(VBoxContainer);
  3964. base_vbox->set_h_size_flags(SIZE_EXPAND_FILL);
  3965. add_child(base_vbox);
  3966. begin_vbox = memnew(VBoxContainer);
  3967. base_vbox->add_child(begin_vbox);
  3968. begin_vbox->hide();
  3969. favorites_section = memnew(VBoxContainer);
  3970. base_vbox->add_child(favorites_section);
  3971. favorites_section->hide();
  3972. favorites_category = memnew(EditorInspectorCategory);
  3973. favorites_category->set_as_favorite(this);
  3974. favorites_section->add_child(favorites_category);
  3975. favorites_category->label = TTR("Favorites");
  3976. favorites_vbox = memnew(VBoxContainer);
  3977. favorites_section->add_child(favorites_vbox);
  3978. favorites_groups_vbox = memnew(VBoxContainer);
  3979. favorites_section->add_child(favorites_groups_vbox);
  3980. favorites_separator = memnew(HSeparator);
  3981. favorites_section->add_child(favorites_separator);
  3982. favorites_separator->hide();
  3983. main_vbox = memnew(VBoxContainer);
  3984. base_vbox->add_child(main_vbox);
  3985. set_horizontal_scroll_mode(SCROLL_MODE_DISABLED);
  3986. set_follow_focus(true);
  3987. changing = 0;
  3988. search_box = nullptr;
  3989. _prop_edited = "property_edited";
  3990. set_process(false);
  3991. set_focus_mode(FocusMode::FOCUS_ALL);
  3992. property_focusable = -1;
  3993. property_clipboard = Variant();
  3994. get_v_scroll_bar()->connect(SceneStringName(value_changed), callable_mp(this, &EditorInspector::_vscroll_changed));
  3995. update_scroll_request = -1;
  3996. if (EditorSettings::get_singleton()) {
  3997. refresh_countdown = float(EDITOR_GET("docks/property_editor/auto_refresh_interval"));
  3998. } else {
  3999. //used when class is created by the docgen to dump default values of everything bindable, editorsettings may not be created
  4000. refresh_countdown = 0.33;
  4001. }
  4002. ED_SHORTCUT("property_editor/copy_value", TTRC("Copy Value"), KeyModifierMask::CMD_OR_CTRL | Key::C);
  4003. ED_SHORTCUT("property_editor/paste_value", TTRC("Paste Value"), KeyModifierMask::CMD_OR_CTRL | Key::V);
  4004. ED_SHORTCUT("property_editor/copy_property_path", TTRC("Copy Property Path"), KeyModifierMask::CMD_OR_CTRL | KeyModifierMask::SHIFT | Key::C);
  4005. // `use_settings_name_style` is true by default, set the name style accordingly.
  4006. set_property_name_style(EditorPropertyNameProcessor::get_singleton()->get_settings_style());
  4007. set_draw_focus_border(true);
  4008. }