editor_inspector.cpp 137 KB

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