editor_inspector.cpp 147 KB

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