editor_inspector.cpp 125 KB

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