2
0

editor_inspector.cpp 137 KB

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