editor_inspector.cpp 137 KB

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