editor_inspector.cpp 160 KB

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