editor_inspector.cpp 136 KB

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