editor_inspector.cpp 142 KB

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