editor_inspector.cpp 141 KB

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