editor_inspector.cpp 135 KB

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