editor_inspector.cpp 149 KB

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