editor_inspector.cpp 115 KB

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