editor_inspector.cpp 78 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683
  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 "dictionary_property_edit.h"
  33. #include "editor/doc_tools.h"
  34. #include "editor_feature_profile.h"
  35. #include "editor_node.h"
  36. #include "editor_scale.h"
  37. #include "multi_node_edit.h"
  38. #include "scene/resources/packed_scene.h"
  39. Size2 EditorProperty::get_minimum_size() const {
  40. Size2 ms;
  41. Ref<Font> font = get_theme_font("font", "Tree");
  42. int font_size = get_theme_font_size("font_size", "Tree");
  43. ms.height = font->get_height(font_size);
  44. for (int i = 0; i < get_child_count(); i++) {
  45. Control *c = Object::cast_to<Control>(get_child(i));
  46. if (!c) {
  47. continue;
  48. }
  49. if (c->is_set_as_top_level()) {
  50. continue;
  51. }
  52. if (!c->is_visible()) {
  53. continue;
  54. }
  55. if (c == bottom_editor) {
  56. continue;
  57. }
  58. Size2 minsize = c->get_combined_minimum_size();
  59. ms.width = MAX(ms.width, minsize.width);
  60. ms.height = MAX(ms.height, minsize.height);
  61. }
  62. if (keying) {
  63. Ref<Texture2D> key = get_theme_icon("Key", "EditorIcons");
  64. ms.width += key->get_width() + get_theme_constant("hseparator", "Tree");
  65. }
  66. if (deletable) {
  67. Ref<Texture2D> key = get_theme_icon("Close", "EditorIcons");
  68. ms.width += key->get_width() + get_theme_constant("hseparator", "Tree");
  69. }
  70. if (checkable) {
  71. Ref<Texture2D> check = get_theme_icon("checked", "CheckBox");
  72. ms.width += check->get_width() + get_theme_constant("hseparation", "CheckBox") + get_theme_constant("hseparator", "Tree");
  73. }
  74. if (bottom_editor != nullptr && bottom_editor->is_visible()) {
  75. ms.height += get_theme_constant("vseparation");
  76. Size2 bems = bottom_editor->get_combined_minimum_size();
  77. //bems.width += get_constant("item_margin", "Tree");
  78. ms.height += bems.height;
  79. ms.width = MAX(ms.width, bems.width);
  80. }
  81. return ms;
  82. }
  83. void EditorProperty::emit_changed(const StringName &p_property, const Variant &p_value, const StringName &p_field, bool p_changing) {
  84. Variant args[4] = { p_property, p_value, p_field, p_changing };
  85. const Variant *argptrs[4] = { &args[0], &args[1], &args[2], &args[3] };
  86. cache[p_property] = p_value;
  87. emit_signal("property_changed", (const Variant **)argptrs, 4);
  88. }
  89. void EditorProperty::_notification(int p_what) {
  90. if (p_what == NOTIFICATION_SORT_CHILDREN) {
  91. Size2 size = get_size();
  92. Rect2 rect;
  93. Rect2 bottom_rect;
  94. right_child_rect = Rect2();
  95. bottom_child_rect = Rect2();
  96. {
  97. int child_room = size.width * (1.0 - split_ratio);
  98. Ref<Font> font = get_theme_font("font", "Tree");
  99. int font_size = get_theme_font_size("font_size", "Tree");
  100. int height = font->get_height(font_size);
  101. bool no_children = true;
  102. //compute room needed
  103. for (int i = 0; i < get_child_count(); i++) {
  104. Control *c = Object::cast_to<Control>(get_child(i));
  105. if (!c) {
  106. continue;
  107. }
  108. if (c->is_set_as_top_level()) {
  109. continue;
  110. }
  111. if (c == bottom_editor) {
  112. continue;
  113. }
  114. Size2 minsize = c->get_combined_minimum_size();
  115. child_room = MAX(child_room, minsize.width);
  116. height = MAX(height, minsize.height);
  117. no_children = false;
  118. }
  119. if (no_children) {
  120. text_size = size.width;
  121. rect = Rect2(size.width - 1, 0, 1, height);
  122. } else {
  123. text_size = MAX(0, size.width - (child_room + 4 * EDSCALE));
  124. if (is_layout_rtl()) {
  125. rect = Rect2(1, 0, child_room, height);
  126. } else {
  127. rect = Rect2(size.width - child_room, 0, child_room, height);
  128. }
  129. }
  130. if (bottom_editor) {
  131. int m = 0; //get_constant("item_margin", "Tree");
  132. bottom_rect = Rect2(m, rect.size.height + get_theme_constant("vseparation"), size.width - m, bottom_editor->get_combined_minimum_size().height);
  133. }
  134. if (keying) {
  135. Ref<Texture2D> key;
  136. if (use_keying_next()) {
  137. key = get_theme_icon("KeyNext", "EditorIcons");
  138. } else {
  139. key = get_theme_icon("Key", "EditorIcons");
  140. }
  141. rect.size.x -= key->get_width() + get_theme_constant("hseparator", "Tree");
  142. if (is_layout_rtl()) {
  143. rect.position.x += key->get_width() + get_theme_constant("hseparator", "Tree");
  144. }
  145. if (no_children) {
  146. text_size -= key->get_width() + 4 * EDSCALE;
  147. }
  148. }
  149. if (deletable) {
  150. Ref<Texture2D> close;
  151. close = get_theme_icon("Close", "EditorIcons");
  152. rect.size.x -= close->get_width() + get_theme_constant("hseparator", "Tree");
  153. if (is_layout_rtl()) {
  154. rect.position.x += close->get_width() + get_theme_constant("hseparator", "Tree");
  155. }
  156. if (no_children) {
  157. text_size -= close->get_width() + 4 * EDSCALE;
  158. }
  159. }
  160. }
  161. //set children
  162. for (int i = 0; i < get_child_count(); i++) {
  163. Control *c = Object::cast_to<Control>(get_child(i));
  164. if (!c) {
  165. continue;
  166. }
  167. if (c->is_set_as_top_level()) {
  168. continue;
  169. }
  170. if (c == bottom_editor) {
  171. continue;
  172. }
  173. fit_child_in_rect(c, rect);
  174. right_child_rect = rect;
  175. }
  176. if (bottom_editor) {
  177. fit_child_in_rect(bottom_editor, bottom_rect);
  178. bottom_child_rect = bottom_rect;
  179. }
  180. update(); //need to redraw text
  181. }
  182. if (p_what == NOTIFICATION_DRAW) {
  183. Ref<Font> font = get_theme_font("font", "Tree");
  184. int font_size = get_theme_font_size("font_size", "Tree");
  185. Color dark_color = get_theme_color("dark_color_2", "Editor");
  186. bool rtl = is_layout_rtl();
  187. Size2 size = get_size();
  188. if (bottom_editor) {
  189. size.height = bottom_editor->get_offset(SIDE_TOP);
  190. } else if (label_reference) {
  191. size.height = label_reference->get_size().height;
  192. }
  193. Ref<StyleBox> sb;
  194. if (selected) {
  195. sb = get_theme_stylebox("bg_selected");
  196. } else {
  197. sb = get_theme_stylebox("bg");
  198. }
  199. draw_style_box(sb, Rect2(Vector2(), size));
  200. if (draw_top_bg && right_child_rect != Rect2()) {
  201. draw_rect(right_child_rect, dark_color);
  202. }
  203. if (bottom_child_rect != Rect2()) {
  204. draw_rect(bottom_child_rect, dark_color);
  205. }
  206. Color color;
  207. if (draw_red) {
  208. color = get_theme_color("error_color");
  209. } else {
  210. color = get_theme_color("property_color");
  211. }
  212. if (label.find(".") != -1) {
  213. color.a = 0.5; //this should be un-hacked honestly, as it's used for editor overrides
  214. }
  215. int ofs = get_theme_constant("font_offset");
  216. int text_limit = text_size;
  217. if (checkable) {
  218. Ref<Texture2D> checkbox;
  219. if (checked) {
  220. checkbox = get_theme_icon("GuiChecked", "EditorIcons");
  221. } else {
  222. checkbox = get_theme_icon("GuiUnchecked", "EditorIcons");
  223. }
  224. Color color2(1, 1, 1);
  225. if (check_hover) {
  226. color2.r *= 1.2;
  227. color2.g *= 1.2;
  228. color2.b *= 1.2;
  229. }
  230. check_rect = Rect2(ofs, ((size.height - checkbox->get_height()) / 2), checkbox->get_width(), checkbox->get_height());
  231. if (rtl) {
  232. draw_texture(checkbox, Vector2(size.width - check_rect.position.x - checkbox->get_width(), check_rect.position.y), color2);
  233. } else {
  234. draw_texture(checkbox, check_rect.position, color2);
  235. }
  236. ofs += get_theme_constant("hseparator", "Tree") + checkbox->get_width() + get_theme_constant("hseparation", "CheckBox");
  237. text_limit -= ofs;
  238. } else {
  239. check_rect = Rect2();
  240. }
  241. if (can_revert) {
  242. Ref<Texture2D> reload_icon = get_theme_icon("ReloadSmall", "EditorIcons");
  243. text_limit -= reload_icon->get_width() + get_theme_constant("hseparator", "Tree") * 2;
  244. revert_rect = Rect2(text_limit + get_theme_constant("hseparator", "Tree"), (size.height - reload_icon->get_height()) / 2, reload_icon->get_width(), reload_icon->get_height());
  245. Color color2(1, 1, 1);
  246. if (revert_hover) {
  247. color2.r *= 1.2;
  248. color2.g *= 1.2;
  249. color2.b *= 1.2;
  250. }
  251. if (rtl) {
  252. draw_texture(reload_icon, Vector2(size.width - revert_rect.position.x - reload_icon->get_width(), revert_rect.position.y), color2);
  253. } else {
  254. draw_texture(reload_icon, revert_rect.position, color2);
  255. }
  256. } else {
  257. revert_rect = Rect2();
  258. }
  259. int v_ofs = (size.height - font->get_height(font_size)) / 2;
  260. if (rtl) {
  261. draw_string(font, Point2(size.width - ofs - text_limit, v_ofs + font->get_ascent(font_size)), label, HALIGN_RIGHT, text_limit, font_size, color);
  262. } else {
  263. draw_string(font, Point2(ofs, v_ofs + font->get_ascent(font_size)), label, HALIGN_LEFT, text_limit, font_size, color);
  264. }
  265. if (keying) {
  266. Ref<Texture2D> key;
  267. if (use_keying_next()) {
  268. key = get_theme_icon("KeyNext", "EditorIcons");
  269. } else {
  270. key = get_theme_icon("Key", "EditorIcons");
  271. }
  272. ofs = size.width - key->get_width() - get_theme_constant("hseparator", "Tree");
  273. Color color2(1, 1, 1);
  274. if (keying_hover) {
  275. color2.r *= 1.2;
  276. color2.g *= 1.2;
  277. color2.b *= 1.2;
  278. }
  279. keying_rect = Rect2(ofs, ((size.height - key->get_height()) / 2), key->get_width(), key->get_height());
  280. if (rtl) {
  281. draw_texture(key, Vector2(size.width - keying_rect.position.x - key->get_width(), keying_rect.position.y), color2);
  282. } else {
  283. draw_texture(key, keying_rect.position, color2);
  284. }
  285. } else {
  286. keying_rect = Rect2();
  287. }
  288. if (deletable) {
  289. Ref<Texture2D> close;
  290. close = get_theme_icon("Close", "EditorIcons");
  291. ofs = size.width - close->get_width() - get_theme_constant("hseparator", "Tree");
  292. Color color2(1, 1, 1);
  293. if (delete_hover) {
  294. color2.r *= 1.2;
  295. color2.g *= 1.2;
  296. color2.b *= 1.2;
  297. }
  298. delete_rect = Rect2(ofs, ((size.height - close->get_height()) / 2), close->get_width(), close->get_height());
  299. if (rtl) {
  300. draw_texture(close, Vector2(size.width - delete_rect.position.x - close->get_width(), delete_rect.position.y), color2);
  301. } else {
  302. draw_texture(close, delete_rect.position, color2);
  303. }
  304. } else {
  305. delete_rect = Rect2();
  306. }
  307. }
  308. }
  309. void EditorProperty::set_label(const String &p_label) {
  310. label = p_label;
  311. update();
  312. }
  313. String EditorProperty::get_label() const {
  314. return label;
  315. }
  316. Object *EditorProperty::get_edited_object() {
  317. return object;
  318. }
  319. StringName EditorProperty::get_edited_property() {
  320. return property;
  321. }
  322. void EditorProperty::update_property() {
  323. if (get_script_instance()) {
  324. get_script_instance()->call("update_property");
  325. }
  326. }
  327. void EditorProperty::set_read_only(bool p_read_only) {
  328. read_only = p_read_only;
  329. }
  330. bool EditorProperty::is_read_only() const {
  331. return read_only;
  332. }
  333. bool EditorPropertyRevert::may_node_be_in_instance(Node *p_node) {
  334. Node *edited_scene = EditorNode::get_singleton()->get_edited_scene();
  335. bool might_be = false;
  336. Node *node = p_node;
  337. while (node) {
  338. if (node == edited_scene) {
  339. if (node->get_scene_inherited_state().is_valid()) {
  340. might_be = true;
  341. break;
  342. }
  343. might_be = false;
  344. break;
  345. }
  346. if (node->get_scene_instance_state().is_valid()) {
  347. might_be = true;
  348. break;
  349. }
  350. node = node->get_owner();
  351. }
  352. return might_be; // or might not be
  353. }
  354. bool EditorPropertyRevert::get_instanced_node_original_property(Node *p_node, const StringName &p_prop, Variant &value) {
  355. Node *node = p_node;
  356. Node *orig = node;
  357. Node *edited_scene = EditorNode::get_singleton()->get_edited_scene();
  358. bool found = false;
  359. while (node) {
  360. Ref<SceneState> ss;
  361. if (node == edited_scene) {
  362. ss = node->get_scene_inherited_state();
  363. } else {
  364. ss = node->get_scene_instance_state();
  365. }
  366. if (ss.is_valid()) {
  367. NodePath np = node->get_path_to(orig);
  368. int node_idx = ss->find_node_by_path(np);
  369. if (node_idx >= 0) {
  370. bool lfound = false;
  371. Variant lvar;
  372. lvar = ss->get_property_value(node_idx, p_prop, lfound);
  373. if (lfound) {
  374. found = true;
  375. value = lvar;
  376. }
  377. }
  378. }
  379. if (node == edited_scene) {
  380. //just in case
  381. break;
  382. }
  383. node = node->get_owner();
  384. }
  385. if (!found && p_node) {
  386. //if not found, try default class value
  387. Variant attempt = ClassDB::class_get_default_property_value(p_node->get_class_name(), p_prop);
  388. if (attempt.get_type() != Variant::NIL) {
  389. found = true;
  390. value = attempt;
  391. }
  392. }
  393. return found;
  394. }
  395. bool EditorPropertyRevert::is_node_property_different(Node *p_node, const Variant &p_current, const Variant &p_orig) {
  396. // this is a pretty difficult function, because a property may not be saved but may have
  397. // the flag to not save if one or if zero
  398. //make sure there is an actual state
  399. {
  400. Node *node = p_node;
  401. if (!node) {
  402. return false;
  403. }
  404. Node *edited_scene = EditorNode::get_singleton()->get_edited_scene();
  405. bool found_state = false;
  406. while (node) {
  407. Ref<SceneState> ss;
  408. if (node == edited_scene) {
  409. ss = node->get_scene_inherited_state();
  410. } else {
  411. ss = node->get_scene_instance_state();
  412. }
  413. if (ss.is_valid()) {
  414. found_state = true;
  415. }
  416. if (node == edited_scene) {
  417. //just in case
  418. break;
  419. }
  420. node = node->get_owner();
  421. }
  422. if (!found_state) {
  423. return false; //pointless to check if we are not comparing against anything.
  424. }
  425. }
  426. if (p_current.get_type() == Variant::FLOAT && p_orig.get_type() == Variant::FLOAT) {
  427. float a = p_current;
  428. float b = p_orig;
  429. return !Math::is_equal_approx(a, b); //this must be done because, as some scenes save as text, there might be a tiny difference in floats due to numerical error
  430. }
  431. return bool(Variant::evaluate(Variant::OP_NOT_EQUAL, p_current, p_orig));
  432. }
  433. bool EditorPropertyRevert::can_property_revert(Object *p_object, const StringName &p_property) {
  434. bool has_revert = false;
  435. Node *node = Object::cast_to<Node>(p_object);
  436. if (node && EditorPropertyRevert::may_node_be_in_instance(node)) {
  437. //check for difference including instantiation
  438. Variant vorig;
  439. if (EditorPropertyRevert::get_instanced_node_original_property(node, p_property, vorig)) {
  440. Variant v = p_object->get(p_property);
  441. if (EditorPropertyRevert::is_node_property_different(node, v, vorig)) {
  442. has_revert = true;
  443. }
  444. }
  445. } else {
  446. //check for difference against default class value instead
  447. Variant default_value = ClassDB::class_get_default_property_value(p_object->get_class_name(), p_property);
  448. if (default_value != Variant() && default_value != p_object->get(p_property)) {
  449. has_revert = true;
  450. }
  451. }
  452. // If the object implements property_can_revert, rely on that completely
  453. // (i.e. don't then try to revert to default value - the property_get_revert implementation
  454. // can do that if so desired)
  455. if (p_object->has_method("property_can_revert")) {
  456. has_revert = p_object->call("property_can_revert", p_property).operator bool();
  457. } else {
  458. if (!has_revert && !p_object->get_script().is_null()) {
  459. Ref<Script> scr = p_object->get_script();
  460. if (scr.is_valid()) {
  461. Variant orig_value;
  462. if (scr->get_property_default_value(p_property, orig_value)) {
  463. if (orig_value != p_object->get(p_property)) {
  464. has_revert = true;
  465. }
  466. }
  467. }
  468. }
  469. }
  470. return has_revert;
  471. }
  472. void EditorProperty::update_reload_status() {
  473. if (property == StringName()) {
  474. return; //no property, so nothing to do
  475. }
  476. bool has_reload = EditorPropertyRevert::can_property_revert(object, property);
  477. if (has_reload != can_revert) {
  478. can_revert = has_reload;
  479. update();
  480. }
  481. }
  482. bool EditorProperty::use_keying_next() const {
  483. List<PropertyInfo> plist;
  484. object->get_property_list(&plist, true);
  485. for (List<PropertyInfo>::Element *I = plist.front(); I; I = I->next()) {
  486. PropertyInfo &p = I->get();
  487. if (p.name == property) {
  488. return (p.usage & PROPERTY_USAGE_KEYING_INCREMENTS);
  489. }
  490. }
  491. return false;
  492. }
  493. void EditorProperty::set_checkable(bool p_checkable) {
  494. checkable = p_checkable;
  495. update();
  496. queue_sort();
  497. }
  498. bool EditorProperty::is_checkable() const {
  499. return checkable;
  500. }
  501. void EditorProperty::set_checked(bool p_checked) {
  502. checked = p_checked;
  503. update();
  504. }
  505. bool EditorProperty::is_checked() const {
  506. return checked;
  507. }
  508. void EditorProperty::set_draw_red(bool p_draw_red) {
  509. draw_red = p_draw_red;
  510. update();
  511. }
  512. void EditorProperty::set_keying(bool p_keying) {
  513. keying = p_keying;
  514. update();
  515. queue_sort();
  516. }
  517. void EditorProperty::set_deletable(bool p_deletable) {
  518. deletable = p_deletable;
  519. update();
  520. queue_sort();
  521. }
  522. bool EditorProperty::is_deletable() const {
  523. return deletable;
  524. }
  525. bool EditorProperty::is_keying() const {
  526. return keying;
  527. }
  528. bool EditorProperty::is_draw_red() const {
  529. return draw_red;
  530. }
  531. void EditorProperty::_focusable_focused(int p_index) {
  532. if (!selectable) {
  533. return;
  534. }
  535. bool already_selected = selected;
  536. selected = true;
  537. selected_focusable = p_index;
  538. update();
  539. if (!already_selected && selected) {
  540. emit_signal("selected", property, selected_focusable);
  541. }
  542. }
  543. void EditorProperty::add_focusable(Control *p_control) {
  544. p_control->connect("focus_entered", callable_mp(this, &EditorProperty::_focusable_focused), varray(focusables.size()));
  545. focusables.push_back(p_control);
  546. }
  547. void EditorProperty::select(int p_focusable) {
  548. bool already_selected = selected;
  549. if (p_focusable >= 0) {
  550. ERR_FAIL_INDEX(p_focusable, focusables.size());
  551. focusables[p_focusable]->grab_focus();
  552. } else {
  553. selected = true;
  554. update();
  555. }
  556. if (!already_selected && selected) {
  557. emit_signal("selected", property, selected_focusable);
  558. }
  559. }
  560. void EditorProperty::deselect() {
  561. selected = false;
  562. selected_focusable = -1;
  563. update();
  564. }
  565. bool EditorProperty::is_selected() const {
  566. return selected;
  567. }
  568. void EditorProperty::_gui_input(const Ref<InputEvent> &p_event) {
  569. if (property == StringName()) {
  570. return;
  571. }
  572. Ref<InputEventMouse> me = p_event;
  573. if (me.is_valid()) {
  574. Vector2 mpos = me->get_position();
  575. if (is_layout_rtl()) {
  576. mpos.x = get_size().x - mpos.x;
  577. }
  578. bool button_left = me->get_button_mask() & BUTTON_MASK_LEFT;
  579. bool new_keying_hover = keying_rect.has_point(mpos) && !button_left;
  580. if (new_keying_hover != keying_hover) {
  581. keying_hover = new_keying_hover;
  582. update();
  583. }
  584. bool new_delete_hover = delete_rect.has_point(mpos) && !button_left;
  585. if (new_delete_hover != delete_hover) {
  586. delete_hover = new_delete_hover;
  587. update();
  588. }
  589. bool new_revert_hover = revert_rect.has_point(mpos) && !button_left;
  590. if (new_revert_hover != revert_hover) {
  591. revert_hover = new_revert_hover;
  592. update();
  593. }
  594. bool new_check_hover = check_rect.has_point(mpos) && !button_left;
  595. if (new_check_hover != check_hover) {
  596. check_hover = new_check_hover;
  597. update();
  598. }
  599. }
  600. Ref<InputEventMouseButton> mb = p_event;
  601. if (mb.is_valid() && mb->is_pressed() && mb->get_button_index() == BUTTON_LEFT) {
  602. Vector2 mpos = mb->get_position();
  603. if (is_layout_rtl()) {
  604. mpos.x = get_size().x - mpos.x;
  605. }
  606. if (!selected && selectable) {
  607. selected = true;
  608. emit_signal("selected", property, -1);
  609. update();
  610. }
  611. if (keying_rect.has_point(mpos)) {
  612. emit_signal("property_keyed", property, use_keying_next());
  613. if (use_keying_next()) {
  614. if (property == "frame_coords" && (object->is_class("Sprite2D") || object->is_class("Sprite3D"))) {
  615. Vector2 new_coords = object->get(property);
  616. new_coords.x++;
  617. if (new_coords.x >= object->get("hframes").operator int64_t()) {
  618. new_coords.x = 0;
  619. new_coords.y++;
  620. }
  621. call_deferred("emit_changed", property, new_coords, "", false);
  622. } else {
  623. call_deferred("emit_changed", property, object->get(property).operator int64_t() + 1, "", false);
  624. }
  625. call_deferred("update_property");
  626. }
  627. }
  628. if (delete_rect.has_point(mpos)) {
  629. emit_signal("property_deleted", property);
  630. }
  631. if (revert_rect.has_point(mpos)) {
  632. Variant vorig;
  633. Node *node = Object::cast_to<Node>(object);
  634. if (node && EditorPropertyRevert::may_node_be_in_instance(node) && EditorPropertyRevert::get_instanced_node_original_property(node, property, vorig)) {
  635. emit_changed(property, vorig.duplicate(true));
  636. update_property();
  637. return;
  638. }
  639. if (object->call("property_can_revert", property).operator bool()) {
  640. Variant rev = object->call("property_get_revert", property);
  641. emit_changed(property, rev);
  642. update_property();
  643. return;
  644. }
  645. if (!object->get_script().is_null()) {
  646. Ref<Script> scr = object->get_script();
  647. if (scr.is_valid()) {
  648. Variant orig_value;
  649. if (scr->get_property_default_value(property, orig_value)) {
  650. emit_changed(property, orig_value);
  651. update_property();
  652. return;
  653. }
  654. }
  655. }
  656. Variant default_value = ClassDB::class_get_default_property_value(object->get_class_name(), property);
  657. if (default_value != Variant()) {
  658. emit_changed(property, default_value);
  659. update_property();
  660. return;
  661. }
  662. }
  663. if (check_rect.has_point(mpos)) {
  664. checked = !checked;
  665. update();
  666. emit_signal("property_checked", property, checked);
  667. }
  668. }
  669. }
  670. void EditorProperty::set_label_reference(Control *p_control) {
  671. label_reference = p_control;
  672. }
  673. void EditorProperty::set_bottom_editor(Control *p_control) {
  674. bottom_editor = p_control;
  675. }
  676. bool EditorProperty::is_cache_valid() const {
  677. if (object) {
  678. for (Map<StringName, Variant>::Element *E = cache.front(); E; E = E->next()) {
  679. bool valid;
  680. Variant value = object->get(E->key(), &valid);
  681. if (!valid || value != E->get()) {
  682. return false;
  683. }
  684. }
  685. }
  686. return true;
  687. }
  688. void EditorProperty::update_cache() {
  689. cache.clear();
  690. if (object && property != StringName()) {
  691. bool valid;
  692. Variant value = object->get(property, &valid);
  693. if (valid) {
  694. cache[property] = value;
  695. }
  696. }
  697. }
  698. Variant EditorProperty::get_drag_data(const Point2 &p_point) {
  699. if (property == StringName()) {
  700. return Variant();
  701. }
  702. Dictionary dp;
  703. dp["type"] = "obj_property";
  704. dp["object"] = object;
  705. dp["property"] = property;
  706. dp["value"] = object->get(property);
  707. Label *label = memnew(Label);
  708. label->set_text(property);
  709. set_drag_preview(label);
  710. return dp;
  711. }
  712. void EditorProperty::set_use_folding(bool p_use_folding) {
  713. use_folding = p_use_folding;
  714. }
  715. bool EditorProperty::is_using_folding() const {
  716. return use_folding;
  717. }
  718. void EditorProperty::expand_all_folding() {
  719. }
  720. void EditorProperty::collapse_all_folding() {
  721. }
  722. void EditorProperty::set_selectable(bool p_selectable) {
  723. selectable = p_selectable;
  724. }
  725. bool EditorProperty::is_selectable() const {
  726. return selectable;
  727. }
  728. void EditorProperty::set_name_split_ratio(float p_ratio) {
  729. split_ratio = p_ratio;
  730. }
  731. float EditorProperty::get_name_split_ratio() const {
  732. return split_ratio;
  733. }
  734. void EditorProperty::set_object_and_property(Object *p_object, const StringName &p_property) {
  735. object = p_object;
  736. property = p_property;
  737. }
  738. Control *EditorProperty::make_custom_tooltip(const String &p_text) const {
  739. tooltip_text = p_text;
  740. EditorHelpBit *help_bit = memnew(EditorHelpBit);
  741. //help_bit->add_theme_style_override("panel", get_theme_stylebox("panel", "TooltipPanel"));
  742. help_bit->get_rich_text()->set_fixed_size_to_width(360 * EDSCALE);
  743. String text;
  744. PackedStringArray slices = p_text.split("::", false);
  745. if (!slices.is_empty()) {
  746. String property_name = slices[0].strip_edges();
  747. text = TTR("Property:") + " [u][b]" + property_name + "[/b][/u]";
  748. if (slices.size() > 1) {
  749. String property_doc = slices[1].strip_edges();
  750. if (property_name != property_doc) {
  751. text += "\n" + property_doc;
  752. }
  753. }
  754. help_bit->set_text(text);
  755. }
  756. return help_bit;
  757. }
  758. String EditorProperty::get_tooltip_text() const {
  759. return tooltip_text;
  760. }
  761. void EditorProperty::_bind_methods() {
  762. ClassDB::bind_method(D_METHOD("set_label", "text"), &EditorProperty::set_label);
  763. ClassDB::bind_method(D_METHOD("get_label"), &EditorProperty::get_label);
  764. ClassDB::bind_method(D_METHOD("set_read_only", "read_only"), &EditorProperty::set_read_only);
  765. ClassDB::bind_method(D_METHOD("is_read_only"), &EditorProperty::is_read_only);
  766. ClassDB::bind_method(D_METHOD("set_checkable", "checkable"), &EditorProperty::set_checkable);
  767. ClassDB::bind_method(D_METHOD("is_checkable"), &EditorProperty::is_checkable);
  768. ClassDB::bind_method(D_METHOD("set_checked", "checked"), &EditorProperty::set_checked);
  769. ClassDB::bind_method(D_METHOD("is_checked"), &EditorProperty::is_checked);
  770. ClassDB::bind_method(D_METHOD("set_draw_red", "draw_red"), &EditorProperty::set_draw_red);
  771. ClassDB::bind_method(D_METHOD("is_draw_red"), &EditorProperty::is_draw_red);
  772. ClassDB::bind_method(D_METHOD("set_keying", "keying"), &EditorProperty::set_keying);
  773. ClassDB::bind_method(D_METHOD("is_keying"), &EditorProperty::is_keying);
  774. ClassDB::bind_method(D_METHOD("set_deletable", "deletable"), &EditorProperty::set_deletable);
  775. ClassDB::bind_method(D_METHOD("is_deletable"), &EditorProperty::is_deletable);
  776. ClassDB::bind_method(D_METHOD("get_edited_property"), &EditorProperty::get_edited_property);
  777. ClassDB::bind_method(D_METHOD("get_edited_object"), &EditorProperty::get_edited_object);
  778. ClassDB::bind_method(D_METHOD("_gui_input"), &EditorProperty::_gui_input);
  779. ClassDB::bind_method(D_METHOD("get_tooltip_text"), &EditorProperty::get_tooltip_text);
  780. ClassDB::bind_method(D_METHOD("add_focusable", "control"), &EditorProperty::add_focusable);
  781. ClassDB::bind_method(D_METHOD("set_bottom_editor", "editor"), &EditorProperty::set_bottom_editor);
  782. ClassDB::bind_method(D_METHOD("emit_changed", "property", "value", "field", "changing"), &EditorProperty::emit_changed, DEFVAL(StringName()), DEFVAL(false));
  783. ADD_PROPERTY(PropertyInfo(Variant::STRING, "label"), "set_label", "get_label");
  784. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "read_only"), "set_read_only", "is_read_only");
  785. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "checkable"), "set_checkable", "is_checkable");
  786. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "checked"), "set_checked", "is_checked");
  787. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "draw_red"), "set_draw_red", "is_draw_red");
  788. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "keying"), "set_keying", "is_keying");
  789. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "deletable"), "set_deletable", "is_deletable");
  790. ADD_SIGNAL(MethodInfo("property_changed", PropertyInfo(Variant::STRING_NAME, "property"), PropertyInfo(Variant::NIL, "value", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NIL_IS_VARIANT)));
  791. ADD_SIGNAL(MethodInfo("multiple_properties_changed", PropertyInfo(Variant::PACKED_STRING_ARRAY, "properties"), PropertyInfo(Variant::ARRAY, "value")));
  792. ADD_SIGNAL(MethodInfo("property_keyed", PropertyInfo(Variant::STRING_NAME, "property")));
  793. ADD_SIGNAL(MethodInfo("property_deleted", PropertyInfo(Variant::STRING_NAME, "property")));
  794. ADD_SIGNAL(MethodInfo("property_keyed_with_value", PropertyInfo(Variant::STRING_NAME, "property"), PropertyInfo(Variant::NIL, "value", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NIL_IS_VARIANT)));
  795. ADD_SIGNAL(MethodInfo("property_checked", PropertyInfo(Variant::STRING_NAME, "property"), PropertyInfo(Variant::STRING, "bool")));
  796. ADD_SIGNAL(MethodInfo("resource_selected", PropertyInfo(Variant::STRING, "path"), PropertyInfo(Variant::OBJECT, "resource", PROPERTY_HINT_RESOURCE_TYPE, "Resource")));
  797. ADD_SIGNAL(MethodInfo("object_id_selected", PropertyInfo(Variant::STRING_NAME, "property"), PropertyInfo(Variant::INT, "id")));
  798. ADD_SIGNAL(MethodInfo("selected", PropertyInfo(Variant::STRING, "path"), PropertyInfo(Variant::INT, "focusable_idx")));
  799. MethodInfo vm;
  800. vm.name = "update_property";
  801. BIND_VMETHOD(vm);
  802. }
  803. EditorProperty::EditorProperty() {
  804. draw_top_bg = true;
  805. object = nullptr;
  806. split_ratio = 0.5;
  807. selectable = true;
  808. text_size = 0;
  809. read_only = false;
  810. checkable = false;
  811. checked = false;
  812. draw_red = false;
  813. keying = false;
  814. deletable = false;
  815. keying_hover = false;
  816. revert_hover = false;
  817. check_hover = false;
  818. can_revert = false;
  819. use_folding = false;
  820. property_usage = 0;
  821. selected = false;
  822. selected_focusable = -1;
  823. label_reference = nullptr;
  824. bottom_editor = nullptr;
  825. delete_hover = false;
  826. }
  827. ////////////////////////////////////////////////
  828. ////////////////////////////////////////////////
  829. void EditorInspectorPlugin::add_custom_control(Control *control) {
  830. AddedEditor ae;
  831. ae.property_editor = control;
  832. added_editors.push_back(ae);
  833. }
  834. void EditorInspectorPlugin::add_property_editor(const String &p_for_property, Control *p_prop) {
  835. ERR_FAIL_COND(Object::cast_to<EditorProperty>(p_prop) == nullptr);
  836. AddedEditor ae;
  837. ae.properties.push_back(p_for_property);
  838. ae.property_editor = p_prop;
  839. added_editors.push_back(ae);
  840. }
  841. void EditorInspectorPlugin::add_property_editor_for_multiple_properties(const String &p_label, const Vector<String> &p_properties, Control *p_prop) {
  842. AddedEditor ae;
  843. ae.properties = p_properties;
  844. ae.property_editor = p_prop;
  845. ae.label = p_label;
  846. added_editors.push_back(ae);
  847. }
  848. bool EditorInspectorPlugin::can_handle(Object *p_object) {
  849. if (get_script_instance()) {
  850. return get_script_instance()->call("can_handle", p_object);
  851. }
  852. return false;
  853. }
  854. void EditorInspectorPlugin::parse_begin(Object *p_object) {
  855. if (get_script_instance()) {
  856. get_script_instance()->call("parse_begin", p_object);
  857. }
  858. }
  859. void EditorInspectorPlugin::parse_category(Object *p_object, const String &p_parse_category) {
  860. if (get_script_instance()) {
  861. get_script_instance()->call("parse_category", p_object, p_parse_category);
  862. }
  863. }
  864. bool EditorInspectorPlugin::parse_property(Object *p_object, Variant::Type p_type, const String &p_path, PropertyHint p_hint, const String &p_hint_text, int p_usage, bool p_wide) {
  865. if (get_script_instance()) {
  866. Variant arg[6] = {
  867. p_object, p_type, p_path, p_hint, p_hint_text, p_usage
  868. };
  869. const Variant *argptr[6] = {
  870. &arg[0], &arg[1], &arg[2], &arg[3], &arg[4], &arg[5]
  871. };
  872. Callable::CallError err;
  873. return get_script_instance()->call("parse_property", (const Variant **)&argptr, 6, err);
  874. }
  875. return false;
  876. }
  877. void EditorInspectorPlugin::parse_end() {
  878. if (get_script_instance()) {
  879. get_script_instance()->call("parse_end");
  880. }
  881. }
  882. void EditorInspectorPlugin::_bind_methods() {
  883. ClassDB::bind_method(D_METHOD("add_custom_control", "control"), &EditorInspectorPlugin::add_custom_control);
  884. ClassDB::bind_method(D_METHOD("add_property_editor", "property", "editor"), &EditorInspectorPlugin::add_property_editor);
  885. ClassDB::bind_method(D_METHOD("add_property_editor_for_multiple_properties", "label", "properties", "editor"), &EditorInspectorPlugin::add_property_editor_for_multiple_properties);
  886. MethodInfo vm;
  887. vm.name = "can_handle";
  888. vm.return_val.type = Variant::BOOL;
  889. vm.arguments.push_back(PropertyInfo(Variant::OBJECT, "object"));
  890. BIND_VMETHOD(vm);
  891. vm.name = "parse_begin";
  892. vm.return_val.type = Variant::NIL;
  893. BIND_VMETHOD(vm);
  894. vm.name = "parse_category";
  895. vm.arguments.push_back(PropertyInfo(Variant::STRING, "category"));
  896. BIND_VMETHOD(vm);
  897. vm.arguments.pop_back();
  898. vm.name = "parse_property";
  899. vm.return_val.type = Variant::BOOL;
  900. vm.arguments.push_back(PropertyInfo(Variant::INT, "type"));
  901. vm.arguments.push_back(PropertyInfo(Variant::STRING, "path"));
  902. vm.arguments.push_back(PropertyInfo(Variant::INT, "hint"));
  903. vm.arguments.push_back(PropertyInfo(Variant::STRING, "hint_text"));
  904. vm.arguments.push_back(PropertyInfo(Variant::INT, "usage"));
  905. BIND_VMETHOD(vm);
  906. vm.arguments.clear();
  907. vm.name = "parse_end";
  908. vm.return_val.type = Variant::NIL;
  909. BIND_VMETHOD(vm);
  910. }
  911. ////////////////////////////////////////////////
  912. ////////////////////////////////////////////////
  913. void EditorInspectorCategory::_notification(int p_what) {
  914. if (p_what == NOTIFICATION_DRAW) {
  915. draw_rect(Rect2(Vector2(), get_size()), bg_color);
  916. Ref<Font> font = get_theme_font("font", "Tree");
  917. int font_size = get_theme_font_size("font_size", "Tree");
  918. int hs = get_theme_constant("hseparation", "Tree");
  919. int w = font->get_string_size(label, font_size).width;
  920. if (icon.is_valid()) {
  921. w += hs + icon->get_width();
  922. }
  923. int ofs = (get_size().width - w) / 2;
  924. if (icon.is_valid()) {
  925. draw_texture(icon, Point2(ofs, (get_size().height - icon->get_height()) / 2).floor());
  926. ofs += hs + icon->get_width();
  927. }
  928. Color color = get_theme_color("font_color", "Tree");
  929. 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);
  930. }
  931. }
  932. Control *EditorInspectorCategory::make_custom_tooltip(const String &p_text) const {
  933. tooltip_text = p_text;
  934. EditorHelpBit *help_bit = memnew(EditorHelpBit);
  935. help_bit->add_theme_style_override("panel", get_theme_stylebox("panel", "TooltipPanel"));
  936. help_bit->get_rich_text()->set_fixed_size_to_width(360 * EDSCALE);
  937. PackedStringArray slices = p_text.split("::", false);
  938. if (!slices.is_empty()) {
  939. String property_name = slices[0].strip_edges();
  940. String text = "[u][b]" + property_name + "[/b][/u]";
  941. if (slices.size() > 1) {
  942. String property_doc = slices[1].strip_edges();
  943. if (property_name != property_doc) {
  944. text += "\n" + property_doc;
  945. }
  946. }
  947. help_bit->set_text(text); //hack so it uses proper theme once inside scene
  948. }
  949. return help_bit;
  950. }
  951. Size2 EditorInspectorCategory::get_minimum_size() const {
  952. Ref<Font> font = get_theme_font("font", "Tree");
  953. int font_size = get_theme_font_size("font_size", "Tree");
  954. Size2 ms;
  955. ms.width = 1;
  956. ms.height = font->get_height(font_size);
  957. if (icon.is_valid()) {
  958. ms.height = MAX(icon->get_height(), ms.height);
  959. }
  960. ms.height += get_theme_constant("vseparation", "Tree");
  961. return ms;
  962. }
  963. void EditorInspectorCategory::_bind_methods() {
  964. ClassDB::bind_method(D_METHOD("get_tooltip_text"), &EditorInspectorCategory::get_tooltip_text);
  965. }
  966. String EditorInspectorCategory::get_tooltip_text() const {
  967. return tooltip_text;
  968. }
  969. EditorInspectorCategory::EditorInspectorCategory() {
  970. }
  971. ////////////////////////////////////////////////
  972. ////////////////////////////////////////////////
  973. void EditorInspectorSection::_test_unfold() {
  974. if (!vbox_added) {
  975. add_child(vbox);
  976. vbox_added = true;
  977. }
  978. }
  979. void EditorInspectorSection::_notification(int p_what) {
  980. if (p_what == NOTIFICATION_SORT_CHILDREN) {
  981. Ref<Font> font = get_theme_font("font", "Tree");
  982. int font_size = get_theme_font_size("font_size", "Tree");
  983. Ref<Texture2D> arrow;
  984. if (foldable) {
  985. if (object->editor_is_section_unfolded(section)) {
  986. arrow = get_theme_icon("arrow", "Tree");
  987. } else {
  988. if (is_layout_rtl()) {
  989. arrow = get_theme_icon("arrow_collapsed_mirrored", "Tree");
  990. } else {
  991. arrow = get_theme_icon("arrow_collapsed", "Tree");
  992. }
  993. }
  994. }
  995. Size2 size = get_size();
  996. Point2 offset;
  997. offset.y = font->get_height(font_size);
  998. if (arrow.is_valid()) {
  999. offset.y = MAX(offset.y, arrow->get_height());
  1000. }
  1001. offset.y += get_theme_constant("vseparation", "Tree");
  1002. offset.x += get_theme_constant("inspector_margin", "Editor");
  1003. Rect2 rect(offset, size - offset);
  1004. //set children
  1005. for (int i = 0; i < get_child_count(); i++) {
  1006. Control *c = Object::cast_to<Control>(get_child(i));
  1007. if (!c) {
  1008. continue;
  1009. }
  1010. if (c->is_set_as_top_level()) {
  1011. continue;
  1012. }
  1013. if (!c->is_visible_in_tree()) {
  1014. continue;
  1015. }
  1016. fit_child_in_rect(c, rect);
  1017. }
  1018. update(); //need to redraw text
  1019. }
  1020. if (p_what == NOTIFICATION_DRAW) {
  1021. Ref<Texture2D> arrow;
  1022. bool rtl = is_layout_rtl();
  1023. if (foldable) {
  1024. if (rtl) {
  1025. arrow = get_theme_icon("arrow_collapsed_mirrored", "Tree");
  1026. } else {
  1027. arrow = get_theme_icon("arrow_collapsed", "Tree");
  1028. }
  1029. }
  1030. Ref<Font> font = get_theme_font("font", "Tree");
  1031. int font_size = get_theme_font_size("font_size", "Tree");
  1032. int h = font->get_height(font_size);
  1033. if (arrow.is_valid()) {
  1034. h = MAX(h, arrow->get_height());
  1035. }
  1036. h += get_theme_constant("vseparation", "Tree");
  1037. draw_rect(Rect2(Vector2(), Vector2(get_size().width, h)), bg_color);
  1038. const int arrow_margin = 3;
  1039. Color color = get_theme_color("font_color", "Tree");
  1040. float text_width = get_size().width - Math::round((16 + arrow_margin) * EDSCALE);
  1041. draw_string(font, Point2(rtl ? 0 : Math::round((16 + arrow_margin) * EDSCALE), font->get_ascent(font_size) + (h - font->get_height(font_size)) / 2).floor(), label, rtl ? HALIGN_RIGHT : HALIGN_LEFT, text_width, font_size, color);
  1042. if (arrow.is_valid()) {
  1043. if (rtl) {
  1044. draw_texture(arrow, Point2(get_size().width - arrow->get_width() - Math::round(arrow_margin * EDSCALE), (h - arrow->get_height()) / 2).floor());
  1045. } else {
  1046. draw_texture(arrow, Point2(Math::round(arrow_margin * EDSCALE), (h - arrow->get_height()) / 2).floor());
  1047. }
  1048. }
  1049. if (dropping && !vbox->is_visible_in_tree()) {
  1050. Color accent_color = get_theme_color("accent_color", "Editor");
  1051. draw_rect(Rect2(Point2(), get_size()), accent_color, false);
  1052. }
  1053. }
  1054. if (p_what == NOTIFICATION_DRAG_BEGIN) {
  1055. Dictionary dd = get_viewport()->gui_get_drag_data();
  1056. // Only allow dropping if the section contains properties which can take the dragged data.
  1057. bool children_can_drop = false;
  1058. for (int child_idx = 0; child_idx < vbox->get_child_count(); child_idx++) {
  1059. Control *editor_property = Object::cast_to<Control>(vbox->get_child(child_idx));
  1060. // Test can_drop_data and can_drop_data_fw, since can_drop_data only works if set up with forwarding or if script attached.
  1061. if (editor_property && (editor_property->can_drop_data(Point2(), dd) || editor_property->call("can_drop_data_fw", Point2(), dd, this))) {
  1062. children_can_drop = true;
  1063. break;
  1064. }
  1065. }
  1066. dropping = children_can_drop;
  1067. update();
  1068. }
  1069. if (p_what == NOTIFICATION_DRAG_END) {
  1070. dropping = false;
  1071. update();
  1072. }
  1073. if (p_what == NOTIFICATION_MOUSE_ENTER) {
  1074. if (dropping) {
  1075. dropping_unfold_timer->start();
  1076. }
  1077. }
  1078. if (p_what == NOTIFICATION_MOUSE_EXIT) {
  1079. if (dropping) {
  1080. dropping_unfold_timer->stop();
  1081. }
  1082. }
  1083. }
  1084. Size2 EditorInspectorSection::get_minimum_size() const {
  1085. Size2 ms;
  1086. for (int i = 0; i < get_child_count(); i++) {
  1087. Control *c = Object::cast_to<Control>(get_child(i));
  1088. if (!c) {
  1089. continue;
  1090. }
  1091. if (c->is_set_as_top_level()) {
  1092. continue;
  1093. }
  1094. if (!c->is_visible()) {
  1095. continue;
  1096. }
  1097. Size2 minsize = c->get_combined_minimum_size();
  1098. ms.width = MAX(ms.width, minsize.width);
  1099. ms.height = MAX(ms.height, minsize.height);
  1100. }
  1101. Ref<Font> font = get_theme_font("font", "Tree");
  1102. int font_size = get_theme_font_size("font_size", "Tree");
  1103. ms.height += font->get_height(font_size) + get_theme_constant("vseparation", "Tree");
  1104. ms.width += get_theme_constant("inspector_margin", "Editor");
  1105. return ms;
  1106. }
  1107. void EditorInspectorSection::setup(const String &p_section, const String &p_label, Object *p_object, const Color &p_bg_color, bool p_foldable) {
  1108. section = p_section;
  1109. label = p_label;
  1110. object = p_object;
  1111. bg_color = p_bg_color;
  1112. foldable = p_foldable;
  1113. if (!foldable && !vbox_added) {
  1114. add_child(vbox);
  1115. vbox_added = true;
  1116. }
  1117. if (foldable) {
  1118. _test_unfold();
  1119. if (object->editor_is_section_unfolded(section)) {
  1120. vbox->show();
  1121. } else {
  1122. vbox->hide();
  1123. }
  1124. }
  1125. }
  1126. void EditorInspectorSection::_gui_input(const Ref<InputEvent> &p_event) {
  1127. if (!foldable) {
  1128. return;
  1129. }
  1130. Ref<InputEventMouseButton> mb = p_event;
  1131. if (mb.is_valid() && mb->is_pressed() && mb->get_button_index() == BUTTON_LEFT) {
  1132. Ref<Font> font = get_theme_font("font", "Tree");
  1133. int font_size = get_theme_font_size("font_size", "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. }
  1144. }
  1145. VBoxContainer *EditorInspectorSection::get_vbox() {
  1146. return vbox;
  1147. }
  1148. void EditorInspectorSection::unfold() {
  1149. if (!foldable) {
  1150. return;
  1151. }
  1152. _test_unfold();
  1153. object->editor_set_section_unfold(section, true);
  1154. vbox->show();
  1155. update();
  1156. }
  1157. void EditorInspectorSection::fold() {
  1158. if (!foldable) {
  1159. return;
  1160. }
  1161. if (!vbox_added) {
  1162. return; //kinda pointless
  1163. }
  1164. object->editor_set_section_unfold(section, false);
  1165. vbox->hide();
  1166. update();
  1167. }
  1168. void EditorInspectorSection::_bind_methods() {
  1169. ClassDB::bind_method(D_METHOD("setup", "section", "label", "object", "bg_color", "foldable"), &EditorInspectorSection::setup);
  1170. ClassDB::bind_method(D_METHOD("get_vbox"), &EditorInspectorSection::get_vbox);
  1171. ClassDB::bind_method(D_METHOD("unfold"), &EditorInspectorSection::unfold);
  1172. ClassDB::bind_method(D_METHOD("fold"), &EditorInspectorSection::fold);
  1173. ClassDB::bind_method(D_METHOD("_gui_input"), &EditorInspectorSection::_gui_input);
  1174. }
  1175. EditorInspectorSection::EditorInspectorSection() {
  1176. object = nullptr;
  1177. foldable = false;
  1178. vbox = memnew(VBoxContainer);
  1179. vbox_added = false;
  1180. dropping = false;
  1181. dropping_unfold_timer = memnew(Timer);
  1182. dropping_unfold_timer->set_wait_time(0.6);
  1183. dropping_unfold_timer->set_one_shot(true);
  1184. add_child(dropping_unfold_timer);
  1185. dropping_unfold_timer->connect("timeout", callable_mp(this, &EditorInspectorSection::unfold));
  1186. }
  1187. EditorInspectorSection::~EditorInspectorSection() {
  1188. if (!vbox_added) {
  1189. memdelete(vbox);
  1190. }
  1191. }
  1192. ////////////////////////////////////////////////
  1193. ////////////////////////////////////////////////
  1194. Ref<EditorInspectorPlugin> EditorInspector::inspector_plugins[MAX_PLUGINS];
  1195. int EditorInspector::inspector_plugin_count = 0;
  1196. EditorProperty *EditorInspector::instantiate_property_editor(Object *p_object, Variant::Type p_type, const String &p_path, PropertyHint p_hint, const String &p_hint_text, int p_usage, bool p_wide) {
  1197. for (int i = inspector_plugin_count - 1; i >= 0; i--) {
  1198. inspector_plugins[i]->parse_property(p_object, p_type, p_path, p_hint, p_hint_text, p_usage, p_wide);
  1199. if (inspector_plugins[i]->added_editors.size()) {
  1200. for (int j = 1; j < inspector_plugins[i]->added_editors.size(); j++) { //only keep first one
  1201. memdelete(inspector_plugins[i]->added_editors[j].property_editor);
  1202. }
  1203. EditorProperty *prop = Object::cast_to<EditorProperty>(inspector_plugins[i]->added_editors[0].property_editor);
  1204. if (prop) {
  1205. inspector_plugins[i]->added_editors.clear();
  1206. return prop;
  1207. } else {
  1208. memdelete(inspector_plugins[i]->added_editors[0].property_editor);
  1209. inspector_plugins[i]->added_editors.clear();
  1210. }
  1211. }
  1212. }
  1213. return nullptr;
  1214. }
  1215. void EditorInspector::add_inspector_plugin(const Ref<EditorInspectorPlugin> &p_plugin) {
  1216. ERR_FAIL_COND(inspector_plugin_count == MAX_PLUGINS);
  1217. for (int i = 0; i < inspector_plugin_count; i++) {
  1218. if (inspector_plugins[i] == p_plugin) {
  1219. return; //already exists
  1220. }
  1221. }
  1222. inspector_plugins[inspector_plugin_count++] = p_plugin;
  1223. }
  1224. void EditorInspector::remove_inspector_plugin(const Ref<EditorInspectorPlugin> &p_plugin) {
  1225. ERR_FAIL_COND(inspector_plugin_count == MAX_PLUGINS);
  1226. int idx = -1;
  1227. for (int i = 0; i < inspector_plugin_count; i++) {
  1228. if (inspector_plugins[i] == p_plugin) {
  1229. idx = i;
  1230. break;
  1231. }
  1232. }
  1233. ERR_FAIL_COND_MSG(idx == -1, "Trying to remove nonexistent inspector plugin.");
  1234. for (int i = idx; i < inspector_plugin_count - 1; i++) {
  1235. inspector_plugins[i] = inspector_plugins[i + 1];
  1236. }
  1237. if (idx == inspector_plugin_count - 1) {
  1238. inspector_plugins[idx] = Ref<EditorInspectorPlugin>();
  1239. }
  1240. inspector_plugin_count--;
  1241. }
  1242. void EditorInspector::cleanup_plugins() {
  1243. for (int i = 0; i < inspector_plugin_count; i++) {
  1244. inspector_plugins[i].unref();
  1245. }
  1246. inspector_plugin_count = 0;
  1247. }
  1248. void EditorInspector::set_undo_redo(UndoRedo *p_undo_redo) {
  1249. undo_redo = p_undo_redo;
  1250. }
  1251. String EditorInspector::get_selected_path() const {
  1252. return property_selected;
  1253. }
  1254. void EditorInspector::_parse_added_editors(VBoxContainer *current_vbox, Ref<EditorInspectorPlugin> ped) {
  1255. for (List<EditorInspectorPlugin::AddedEditor>::Element *F = ped->added_editors.front(); F; F = F->next()) {
  1256. EditorProperty *ep = Object::cast_to<EditorProperty>(F->get().property_editor);
  1257. current_vbox->add_child(F->get().property_editor);
  1258. if (ep) {
  1259. ep->object = object;
  1260. ep->connect("property_changed", callable_mp(this, &EditorInspector::_property_changed));
  1261. ep->connect("property_keyed", callable_mp(this, &EditorInspector::_property_keyed));
  1262. ep->connect("property_deleted", callable_mp(this, &EditorInspector::_property_deleted), varray(), CONNECT_DEFERRED);
  1263. ep->connect("property_keyed_with_value", callable_mp(this, &EditorInspector::_property_keyed_with_value));
  1264. ep->connect("property_checked", callable_mp(this, &EditorInspector::_property_checked));
  1265. ep->connect("selected", callable_mp(this, &EditorInspector::_property_selected));
  1266. ep->connect("multiple_properties_changed", callable_mp(this, &EditorInspector::_multiple_properties_changed));
  1267. ep->connect("resource_selected", callable_mp(this, &EditorInspector::_resource_selected), varray(), CONNECT_DEFERRED);
  1268. ep->connect("object_id_selected", callable_mp(this, &EditorInspector::_object_id_selected), varray(), CONNECT_DEFERRED);
  1269. if (F->get().properties.size()) {
  1270. if (F->get().properties.size() == 1) {
  1271. //since it's one, associate:
  1272. ep->property = F->get().properties[0];
  1273. ep->property_usage = 0;
  1274. }
  1275. if (F->get().label != String()) {
  1276. ep->set_label(F->get().label);
  1277. }
  1278. for (int i = 0; i < F->get().properties.size(); i++) {
  1279. String prop = F->get().properties[i];
  1280. if (!editor_property_map.has(prop)) {
  1281. editor_property_map[prop] = List<EditorProperty *>();
  1282. }
  1283. editor_property_map[prop].push_back(ep);
  1284. }
  1285. }
  1286. ep->set_read_only(read_only);
  1287. ep->update_property();
  1288. ep->update_reload_status();
  1289. ep->set_deletable(deletable_properties);
  1290. ep->update_cache();
  1291. }
  1292. }
  1293. ped->added_editors.clear();
  1294. }
  1295. bool EditorInspector::_is_property_disabled_by_feature_profile(const StringName &p_property) {
  1296. Ref<EditorFeatureProfile> profile = EditorFeatureProfileManager::get_singleton()->get_current_profile();
  1297. if (profile.is_null()) {
  1298. return false;
  1299. }
  1300. StringName class_name = object->get_class();
  1301. while (class_name != StringName()) {
  1302. if (profile->is_class_property_disabled(class_name, p_property)) {
  1303. return true;
  1304. }
  1305. if (profile->is_class_disabled(class_name)) {
  1306. //won't see properties of a disabled class
  1307. return true;
  1308. }
  1309. class_name = ClassDB::get_parent_class(class_name);
  1310. }
  1311. return false;
  1312. }
  1313. void EditorInspector::update_tree() {
  1314. //to update properly if all is refreshed
  1315. StringName current_selected = property_selected;
  1316. int current_focusable = -1;
  1317. if (property_focusable != -1) {
  1318. //check focusable is really focusable
  1319. bool restore_focus = false;
  1320. Control *focused = get_focus_owner();
  1321. if (focused) {
  1322. Node *parent = focused->get_parent();
  1323. while (parent) {
  1324. EditorInspector *inspector = Object::cast_to<EditorInspector>(parent);
  1325. if (inspector) {
  1326. restore_focus = inspector == this; //may be owned by another inspector
  1327. break; //exit after the first inspector is found, since there may be nested ones
  1328. }
  1329. parent = parent->get_parent();
  1330. }
  1331. }
  1332. if (restore_focus) {
  1333. current_focusable = property_focusable;
  1334. }
  1335. }
  1336. _clear();
  1337. if (!object) {
  1338. return;
  1339. }
  1340. List<Ref<EditorInspectorPlugin>> valid_plugins;
  1341. for (int i = inspector_plugin_count - 1; i >= 0; i--) { //start by last, so lastly added can override newly added
  1342. if (!inspector_plugins[i]->can_handle(object)) {
  1343. continue;
  1344. }
  1345. valid_plugins.push_back(inspector_plugins[i]);
  1346. }
  1347. bool draw_red = false;
  1348. {
  1349. Node *nod = Object::cast_to<Node>(object);
  1350. Node *es = EditorNode::get_singleton()->get_edited_scene();
  1351. if (nod && es != nod && nod->get_owner() != es) {
  1352. draw_red = true;
  1353. }
  1354. }
  1355. // TreeItem *current_category = nullptr;
  1356. String filter = search_box ? search_box->get_text() : "";
  1357. String group;
  1358. String group_base;
  1359. String subgroup;
  1360. String subgroup_base;
  1361. VBoxContainer *category_vbox = nullptr;
  1362. List<PropertyInfo> plist;
  1363. object->get_property_list(&plist, true);
  1364. _update_script_class_properties(*object, plist);
  1365. HashMap<String, VBoxContainer *> item_path;
  1366. Map<VBoxContainer *, EditorInspectorSection *> section_map;
  1367. item_path[""] = main_vbox;
  1368. Color sscolor = get_theme_color("prop_subsection", "Editor");
  1369. for (List<Ref<EditorInspectorPlugin>>::Element *E = valid_plugins.front(); E; E = E->next()) {
  1370. Ref<EditorInspectorPlugin> ped = E->get();
  1371. ped->parse_begin(object);
  1372. _parse_added_editors(main_vbox, ped);
  1373. }
  1374. for (List<PropertyInfo>::Element *I = plist.front(); I; I = I->next()) {
  1375. PropertyInfo &p = I->get();
  1376. //make sure the property can be edited
  1377. if (p.usage & PROPERTY_USAGE_SUBGROUP) {
  1378. subgroup = p.name;
  1379. subgroup_base = p.hint_string;
  1380. continue;
  1381. } else if (p.usage & PROPERTY_USAGE_GROUP) {
  1382. group = p.name;
  1383. group_base = p.hint_string;
  1384. subgroup = "";
  1385. subgroup_base = "";
  1386. continue;
  1387. } else if (p.usage & PROPERTY_USAGE_CATEGORY) {
  1388. group = "";
  1389. group_base = "";
  1390. subgroup = "";
  1391. subgroup_base = "";
  1392. if (!show_categories) {
  1393. continue;
  1394. }
  1395. List<PropertyInfo>::Element *N = I->next();
  1396. bool valid = true;
  1397. //if no properties in category, skip
  1398. while (N) {
  1399. if (N->get().usage & PROPERTY_USAGE_EDITOR && (!restrict_to_basic || (N->get().usage & PROPERTY_USAGE_EDITOR_BASIC_SETTING))) {
  1400. break;
  1401. }
  1402. if (N->get().usage & PROPERTY_USAGE_CATEGORY) {
  1403. valid = false;
  1404. break;
  1405. }
  1406. N = N->next();
  1407. }
  1408. if (!valid) {
  1409. continue; //empty, ignore
  1410. }
  1411. EditorInspectorCategory *category = memnew(EditorInspectorCategory);
  1412. main_vbox->add_child(category);
  1413. category_vbox = nullptr; //reset
  1414. String type = p.name;
  1415. if (!ClassDB::class_exists(type) && !ScriptServer::is_global_class(type) && p.hint_string.length() && FileAccess::exists(p.hint_string)) {
  1416. Ref<Script> s = ResourceLoader::load(p.hint_string, "Script");
  1417. String base_type;
  1418. if (s.is_valid()) {
  1419. base_type = s->get_instance_base_type();
  1420. }
  1421. while (s.is_valid()) {
  1422. StringName name = EditorNode::get_editor_data().script_class_get_name(s->get_path());
  1423. String icon_path = EditorNode::get_editor_data().script_class_get_icon_path(name);
  1424. if (name != StringName() && icon_path.length()) {
  1425. category->icon = ResourceLoader::load(icon_path, "Texture");
  1426. break;
  1427. }
  1428. s = s->get_base_script();
  1429. }
  1430. if (category->icon.is_null() && has_theme_icon(base_type, "EditorIcons")) {
  1431. category->icon = get_theme_icon(base_type, "EditorIcons");
  1432. }
  1433. }
  1434. if (category->icon.is_null()) {
  1435. if (type != String()) { // Can happen for built-in scripts.
  1436. category->icon = EditorNode::get_singleton()->get_class_icon(type, "Object");
  1437. }
  1438. }
  1439. category->label = type;
  1440. category->bg_color = get_theme_color("prop_category", "Editor");
  1441. if (use_doc_hints) {
  1442. StringName type2 = p.name;
  1443. if (!class_descr_cache.has(type2)) {
  1444. String descr;
  1445. DocTools *dd = EditorHelp::get_doc_data();
  1446. Map<String, DocData::ClassDoc>::Element *E = dd->class_list.find(type2);
  1447. if (E) {
  1448. descr = DTR(E->get().brief_description);
  1449. }
  1450. class_descr_cache[type2] = descr;
  1451. }
  1452. category->set_tooltip(p.name + "::" + (class_descr_cache[type2] == "" ? "" : class_descr_cache[type2]));
  1453. }
  1454. for (List<Ref<EditorInspectorPlugin>>::Element *E = valid_plugins.front(); E; E = E->next()) {
  1455. Ref<EditorInspectorPlugin> ped = E->get();
  1456. ped->parse_category(object, p.name);
  1457. _parse_added_editors(main_vbox, ped);
  1458. }
  1459. continue;
  1460. } 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))) {
  1461. continue;
  1462. }
  1463. if (p.name == "script") {
  1464. category_vbox = nullptr; // script should go into its own category
  1465. }
  1466. if (p.usage & PROPERTY_USAGE_HIGH_END_GFX && RS::get_singleton()->is_low_end()) {
  1467. continue; //do not show this property in low end gfx
  1468. }
  1469. if (p.name == "script" && (hide_script || bool(object->call("_hide_script_from_inspector")))) {
  1470. continue;
  1471. }
  1472. String basename = p.name;
  1473. if (subgroup != "") {
  1474. if (subgroup_base != "") {
  1475. if (basename.begins_with(subgroup_base)) {
  1476. basename = basename.replace_first(subgroup_base, "");
  1477. } else if (subgroup_base.begins_with(basename)) {
  1478. //keep it, this is used pretty often
  1479. } else {
  1480. subgroup = ""; //no longer using subgroup base, clear
  1481. }
  1482. }
  1483. }
  1484. if (group != "") {
  1485. if (group_base != "" && subgroup == "") {
  1486. if (basename.begins_with(group_base)) {
  1487. basename = basename.replace_first(group_base, "");
  1488. } else if (group_base.begins_with(basename)) {
  1489. //keep it, this is used pretty often
  1490. } else {
  1491. group = ""; //no longer using group base, clear
  1492. subgroup = "";
  1493. }
  1494. }
  1495. }
  1496. if (subgroup != "") {
  1497. basename = subgroup + "/" + basename;
  1498. }
  1499. if (group != "") {
  1500. basename = group + "/" + basename;
  1501. }
  1502. String name = (basename.find("/") != -1) ? basename.right(basename.rfind("/") + 1) : basename;
  1503. if (capitalize_paths) {
  1504. int dot = name.find(".");
  1505. if (dot != -1) {
  1506. String ov = name.right(dot);
  1507. name = name.substr(0, dot);
  1508. name = name.capitalize();
  1509. name += ov;
  1510. } else {
  1511. name = name.capitalize();
  1512. }
  1513. }
  1514. String path = basename.left(basename.rfind("/"));
  1515. if (use_filter && filter != "") {
  1516. String cat = path;
  1517. if (capitalize_paths) {
  1518. cat = cat.capitalize();
  1519. }
  1520. if (!filter.is_subsequence_ofi(cat) && !filter.is_subsequence_ofi(name) && property_prefix.to_lower().find(filter.to_lower()) == -1) {
  1521. continue;
  1522. }
  1523. }
  1524. if (category_vbox == nullptr) {
  1525. category_vbox = memnew(VBoxContainer);
  1526. main_vbox->add_child(category_vbox);
  1527. }
  1528. VBoxContainer *current_vbox = main_vbox;
  1529. {
  1530. String acc_path = "";
  1531. int level = 1;
  1532. for (int i = 0; i < path.get_slice_count("/"); i++) {
  1533. String path_name = path.get_slice("/", i);
  1534. if (i > 0) {
  1535. acc_path += "/";
  1536. }
  1537. acc_path += path_name;
  1538. if (!item_path.has(acc_path)) {
  1539. EditorInspectorSection *section = memnew(EditorInspectorSection);
  1540. current_vbox->add_child(section);
  1541. sections.push_back(section);
  1542. if (capitalize_paths) {
  1543. path_name = path_name.capitalize();
  1544. }
  1545. Color c = sscolor;
  1546. c.a /= level;
  1547. section->setup(acc_path, path_name, object, c, use_folding);
  1548. VBoxContainer *vb = section->get_vbox();
  1549. item_path[acc_path] = vb;
  1550. section_map[vb] = section;
  1551. }
  1552. current_vbox = item_path[acc_path];
  1553. level = (MIN(level + 1, 4));
  1554. }
  1555. if (current_vbox == main_vbox) {
  1556. //do not add directly to the main vbox, given it has no spacing
  1557. if (category_vbox == nullptr) {
  1558. category_vbox = memnew(VBoxContainer);
  1559. }
  1560. current_vbox = category_vbox;
  1561. }
  1562. }
  1563. bool checkable = false;
  1564. bool checked = false;
  1565. if (p.usage & PROPERTY_USAGE_CHECKABLE) {
  1566. checkable = true;
  1567. checked = p.usage & PROPERTY_USAGE_CHECKED;
  1568. }
  1569. if (p.usage & PROPERTY_USAGE_RESTART_IF_CHANGED) {
  1570. restart_request_props.insert(p.name);
  1571. }
  1572. String doc_hint;
  1573. if (use_doc_hints) {
  1574. StringName classname = object->get_class_name();
  1575. if (object_class != String()) {
  1576. classname = object_class;
  1577. }
  1578. StringName propname = property_prefix + p.name;
  1579. String descr;
  1580. bool found = false;
  1581. Map<StringName, Map<StringName, String>>::Element *E = descr_cache.find(classname);
  1582. if (E) {
  1583. Map<StringName, String>::Element *F = E->get().find(propname);
  1584. if (F) {
  1585. found = true;
  1586. descr = F->get();
  1587. }
  1588. }
  1589. if (!found) {
  1590. DocTools *dd = EditorHelp::get_doc_data();
  1591. Map<String, DocData::ClassDoc>::Element *F = dd->class_list.find(classname);
  1592. while (F && descr == String()) {
  1593. for (int i = 0; i < F->get().properties.size(); i++) {
  1594. if (F->get().properties[i].name == propname.operator String()) {
  1595. descr = DTR(F->get().properties[i].description);
  1596. break;
  1597. }
  1598. }
  1599. Vector<String> slices = propname.operator String().split("/");
  1600. if (slices.size() == 2 && slices[0].begins_with("custom_")) {
  1601. // Likely a theme property.
  1602. for (int i = 0; i < F->get().theme_properties.size(); i++) {
  1603. if (F->get().theme_properties[i].name == slices[1]) {
  1604. descr = DTR(F->get().theme_properties[i].description);
  1605. break;
  1606. }
  1607. }
  1608. }
  1609. if (!F->get().inherits.is_empty()) {
  1610. F = dd->class_list.find(F->get().inherits);
  1611. } else {
  1612. break;
  1613. }
  1614. }
  1615. descr_cache[classname][propname] = descr;
  1616. }
  1617. doc_hint = descr;
  1618. }
  1619. for (List<Ref<EditorInspectorPlugin>>::Element *E = valid_plugins.front(); E; E = E->next()) {
  1620. Ref<EditorInspectorPlugin> ped = E->get();
  1621. bool exclusive = ped->parse_property(object, p.type, p.name, p.hint, p.hint_string, p.usage, wide_editors);
  1622. List<EditorInspectorPlugin::AddedEditor> editors = ped->added_editors; //make a copy, since plugins may be used again in a sub-inspector
  1623. ped->added_editors.clear();
  1624. for (List<EditorInspectorPlugin::AddedEditor>::Element *F = editors.front(); F; F = F->next()) {
  1625. EditorProperty *ep = Object::cast_to<EditorProperty>(F->get().property_editor);
  1626. if (ep) {
  1627. //set all this before the control gets the ENTER_TREE notification
  1628. ep->object = object;
  1629. if (F->get().properties.size()) {
  1630. if (F->get().properties.size() == 1) {
  1631. //since it's one, associate:
  1632. ep->property = F->get().properties[0];
  1633. ep->property_usage = p.usage;
  1634. //and set label?
  1635. }
  1636. if (F->get().label != String()) {
  1637. ep->set_label(F->get().label);
  1638. } else {
  1639. //use existin one
  1640. ep->set_label(name);
  1641. }
  1642. for (int i = 0; i < F->get().properties.size(); i++) {
  1643. String prop = F->get().properties[i];
  1644. if (!editor_property_map.has(prop)) {
  1645. editor_property_map[prop] = List<EditorProperty *>();
  1646. }
  1647. editor_property_map[prop].push_back(ep);
  1648. }
  1649. }
  1650. ep->set_draw_red(draw_red);
  1651. ep->set_use_folding(use_folding);
  1652. ep->set_checkable(checkable);
  1653. ep->set_checked(checked);
  1654. ep->set_keying(keying);
  1655. ep->set_read_only(read_only);
  1656. ep->set_deletable(deletable_properties);
  1657. }
  1658. current_vbox->add_child(F->get().property_editor);
  1659. if (ep) {
  1660. ep->connect("property_changed", callable_mp(this, &EditorInspector::_property_changed));
  1661. if (p.usage & PROPERTY_USAGE_UPDATE_ALL_IF_MODIFIED) {
  1662. ep->connect("property_changed", callable_mp(this, &EditorInspector::_property_changed_update_all), varray(), CONNECT_DEFERRED);
  1663. }
  1664. ep->connect("property_keyed", callable_mp(this, &EditorInspector::_property_keyed));
  1665. ep->connect("property_deleted", callable_mp(this, &EditorInspector::_property_deleted), varray(), CONNECT_DEFERRED);
  1666. ep->connect("property_keyed_with_value", callable_mp(this, &EditorInspector::_property_keyed_with_value));
  1667. ep->connect("property_checked", callable_mp(this, &EditorInspector::_property_checked));
  1668. ep->connect("selected", callable_mp(this, &EditorInspector::_property_selected));
  1669. ep->connect("multiple_properties_changed", callable_mp(this, &EditorInspector::_multiple_properties_changed));
  1670. ep->connect("resource_selected", callable_mp(this, &EditorInspector::_resource_selected), varray(), CONNECT_DEFERRED);
  1671. ep->connect("object_id_selected", callable_mp(this, &EditorInspector::_object_id_selected), varray(), CONNECT_DEFERRED);
  1672. if (doc_hint != String()) {
  1673. ep->set_tooltip(property_prefix + p.name + "::" + doc_hint);
  1674. } else {
  1675. ep->set_tooltip(property_prefix + p.name);
  1676. }
  1677. ep->update_property();
  1678. ep->update_reload_status();
  1679. ep->update_cache();
  1680. if (current_selected && ep->property == current_selected) {
  1681. ep->select(current_focusable);
  1682. }
  1683. }
  1684. }
  1685. if (exclusive) {
  1686. break;
  1687. }
  1688. }
  1689. }
  1690. for (List<Ref<EditorInspectorPlugin>>::Element *E = valid_plugins.front(); E; E = E->next()) {
  1691. Ref<EditorInspectorPlugin> ped = E->get();
  1692. ped->parse_end();
  1693. _parse_added_editors(main_vbox, ped);
  1694. }
  1695. //see if this property exists and should be kept
  1696. }
  1697. void EditorInspector::update_property(const String &p_prop) {
  1698. if (!editor_property_map.has(p_prop)) {
  1699. return;
  1700. }
  1701. for (List<EditorProperty *>::Element *E = editor_property_map[p_prop].front(); E; E = E->next()) {
  1702. E->get()->update_property();
  1703. E->get()->update_reload_status();
  1704. E->get()->update_cache();
  1705. }
  1706. }
  1707. void EditorInspector::_clear() {
  1708. while (main_vbox->get_child_count()) {
  1709. memdelete(main_vbox->get_child(0));
  1710. }
  1711. property_selected = StringName();
  1712. property_focusable = -1;
  1713. editor_property_map.clear();
  1714. sections.clear();
  1715. pending.clear();
  1716. restart_request_props.clear();
  1717. }
  1718. Object *EditorInspector::get_edited_object() {
  1719. return object;
  1720. }
  1721. void EditorInspector::edit(Object *p_object) {
  1722. if (object == p_object) {
  1723. return;
  1724. }
  1725. if (object) {
  1726. _clear();
  1727. object->disconnect("property_list_changed", callable_mp(this, &EditorInspector::_changed_callback));
  1728. }
  1729. object = p_object;
  1730. if (object) {
  1731. update_scroll_request = 0; //reset
  1732. if (scroll_cache.has(object->get_instance_id())) { //if exists, set something else
  1733. update_scroll_request = scroll_cache[object->get_instance_id()]; //done this way because wait until full size is accommodated
  1734. }
  1735. object->connect("property_list_changed", callable_mp(this, &EditorInspector::_changed_callback));
  1736. update_tree();
  1737. }
  1738. }
  1739. void EditorInspector::set_keying(bool p_active) {
  1740. if (keying == p_active) {
  1741. return;
  1742. }
  1743. keying = p_active;
  1744. update_tree();
  1745. }
  1746. void EditorInspector::set_read_only(bool p_read_only) {
  1747. read_only = p_read_only;
  1748. update_tree();
  1749. }
  1750. bool EditorInspector::is_capitalize_paths_enabled() const {
  1751. return capitalize_paths;
  1752. }
  1753. void EditorInspector::set_enable_capitalize_paths(bool p_capitalize) {
  1754. capitalize_paths = p_capitalize;
  1755. update_tree();
  1756. }
  1757. void EditorInspector::set_autoclear(bool p_enable) {
  1758. autoclear = p_enable;
  1759. }
  1760. void EditorInspector::set_show_categories(bool p_show) {
  1761. show_categories = p_show;
  1762. update_tree();
  1763. }
  1764. void EditorInspector::set_use_doc_hints(bool p_enable) {
  1765. use_doc_hints = p_enable;
  1766. update_tree();
  1767. }
  1768. void EditorInspector::set_hide_script(bool p_hide) {
  1769. hide_script = p_hide;
  1770. update_tree();
  1771. }
  1772. void EditorInspector::set_use_filter(bool p_use) {
  1773. use_filter = p_use;
  1774. update_tree();
  1775. }
  1776. void EditorInspector::register_text_enter(Node *p_line_edit) {
  1777. search_box = Object::cast_to<LineEdit>(p_line_edit);
  1778. if (search_box) {
  1779. search_box->connect("text_changed", callable_mp(this, &EditorInspector::_filter_changed));
  1780. }
  1781. }
  1782. void EditorInspector::_filter_changed(const String &p_text) {
  1783. _clear();
  1784. update_tree();
  1785. }
  1786. void EditorInspector::set_use_folding(bool p_enable) {
  1787. use_folding = p_enable;
  1788. update_tree();
  1789. }
  1790. bool EditorInspector::is_using_folding() {
  1791. return use_folding;
  1792. }
  1793. void EditorInspector::collapse_all_folding() {
  1794. for (List<EditorInspectorSection *>::Element *E = sections.front(); E; E = E->next()) {
  1795. E->get()->fold();
  1796. }
  1797. for (Map<StringName, List<EditorProperty *>>::Element *F = editor_property_map.front(); F; F = F->next()) {
  1798. for (List<EditorProperty *>::Element *E = F->get().front(); E; E = E->next()) {
  1799. E->get()->collapse_all_folding();
  1800. }
  1801. }
  1802. }
  1803. void EditorInspector::expand_all_folding() {
  1804. for (List<EditorInspectorSection *>::Element *E = sections.front(); E; E = E->next()) {
  1805. E->get()->unfold();
  1806. }
  1807. for (Map<StringName, List<EditorProperty *>>::Element *F = editor_property_map.front(); F; F = F->next()) {
  1808. for (List<EditorProperty *>::Element *E = F->get().front(); E; E = E->next()) {
  1809. E->get()->expand_all_folding();
  1810. }
  1811. }
  1812. }
  1813. void EditorInspector::set_scroll_offset(int p_offset) {
  1814. set_v_scroll(p_offset);
  1815. }
  1816. int EditorInspector::get_scroll_offset() const {
  1817. return get_v_scroll();
  1818. }
  1819. void EditorInspector::set_use_wide_editors(bool p_enable) {
  1820. wide_editors = p_enable;
  1821. }
  1822. void EditorInspector::_update_inspector_bg() {
  1823. if (sub_inspector) {
  1824. int count_subinspectors = 0;
  1825. Node *n = get_parent();
  1826. while (n) {
  1827. EditorInspector *ei = Object::cast_to<EditorInspector>(n);
  1828. if (ei && ei->sub_inspector) {
  1829. count_subinspectors++;
  1830. }
  1831. n = n->get_parent();
  1832. }
  1833. count_subinspectors = MIN(15, count_subinspectors);
  1834. add_theme_style_override("bg", get_theme_stylebox("sub_inspector_bg" + itos(count_subinspectors), "Editor"));
  1835. } else {
  1836. add_theme_style_override("bg", get_theme_stylebox("bg", "Tree"));
  1837. }
  1838. }
  1839. void EditorInspector::set_sub_inspector(bool p_enable) {
  1840. sub_inspector = p_enable;
  1841. if (!is_inside_tree()) {
  1842. return;
  1843. }
  1844. _update_inspector_bg();
  1845. }
  1846. void EditorInspector::set_use_deletable_properties(bool p_enabled) {
  1847. deletable_properties = p_enabled;
  1848. }
  1849. void EditorInspector::_edit_request_change(Object *p_object, const String &p_property) {
  1850. if (object != p_object) { //may be undoing/redoing for a non edited object, so ignore
  1851. return;
  1852. }
  1853. if (changing) {
  1854. return;
  1855. }
  1856. if (p_property == String()) {
  1857. update_tree_pending = true;
  1858. } else {
  1859. pending.insert(p_property);
  1860. }
  1861. }
  1862. void EditorInspector::_edit_set(const String &p_name, const Variant &p_value, bool p_refresh_all, const String &p_changed_field) {
  1863. if (autoclear && editor_property_map.has(p_name)) {
  1864. for (List<EditorProperty *>::Element *E = editor_property_map[p_name].front(); E; E = E->next()) {
  1865. if (E->get()->is_checkable()) {
  1866. E->get()->set_checked(true);
  1867. }
  1868. }
  1869. }
  1870. if (!undo_redo || bool(object->call("_dont_undo_redo"))) {
  1871. object->set(p_name, p_value);
  1872. if (p_refresh_all) {
  1873. _edit_request_change(object, "");
  1874. } else {
  1875. _edit_request_change(object, p_name);
  1876. }
  1877. emit_signal(_prop_edited, p_name);
  1878. } else if (Object::cast_to<MultiNodeEdit>(object)) {
  1879. Object::cast_to<MultiNodeEdit>(object)->set_property_field(p_name, p_value, p_changed_field);
  1880. _edit_request_change(object, p_name);
  1881. emit_signal(_prop_edited, p_name);
  1882. } else {
  1883. undo_redo->create_action(TTR("Set") + " " + p_name, UndoRedo::MERGE_ENDS);
  1884. undo_redo->add_do_property(object, p_name, p_value);
  1885. undo_redo->add_undo_property(object, p_name, object->get(p_name));
  1886. if (p_refresh_all) {
  1887. undo_redo->add_do_method(this, "_edit_request_change", object, "");
  1888. undo_redo->add_undo_method(this, "_edit_request_change", object, "");
  1889. } else {
  1890. undo_redo->add_do_method(this, "_edit_request_change", object, p_name);
  1891. undo_redo->add_undo_method(this, "_edit_request_change", object, p_name);
  1892. }
  1893. Resource *r = Object::cast_to<Resource>(object);
  1894. if (r) {
  1895. if (String(p_name) == "resource_local_to_scene") {
  1896. bool prev = object->get(p_name);
  1897. bool next = p_value;
  1898. if (next) {
  1899. undo_redo->add_do_method(r, "setup_local_to_scene");
  1900. }
  1901. if (prev) {
  1902. undo_redo->add_undo_method(r, "setup_local_to_scene");
  1903. }
  1904. }
  1905. }
  1906. undo_redo->add_do_method(this, "emit_signal", _prop_edited, p_name);
  1907. undo_redo->add_undo_method(this, "emit_signal", _prop_edited, p_name);
  1908. undo_redo->commit_action();
  1909. }
  1910. if (editor_property_map.has(p_name)) {
  1911. for (List<EditorProperty *>::Element *E = editor_property_map[p_name].front(); E; E = E->next()) {
  1912. E->get()->update_reload_status();
  1913. }
  1914. }
  1915. }
  1916. void EditorInspector::_property_changed(const String &p_path, const Variant &p_value, const String &p_name, bool p_changing) {
  1917. // The "changing" variable must be true for properties that trigger events as typing occurs,
  1918. // like "text_changed" signal. E.g. text property of Label, Button, RichTextLabel, etc.
  1919. if (p_changing) {
  1920. this->changing++;
  1921. }
  1922. _edit_set(p_path, p_value, false, p_name);
  1923. if (p_changing) {
  1924. this->changing--;
  1925. }
  1926. if (restart_request_props.has(p_path)) {
  1927. emit_signal("restart_requested");
  1928. }
  1929. }
  1930. void EditorInspector::_property_changed_update_all(const String &p_path, const Variant &p_value, const String &p_name, bool p_changing) {
  1931. update_tree();
  1932. }
  1933. void EditorInspector::_multiple_properties_changed(Vector<String> p_paths, Array p_values) {
  1934. ERR_FAIL_COND(p_paths.size() == 0 || p_values.size() == 0);
  1935. ERR_FAIL_COND(p_paths.size() != p_values.size());
  1936. String names;
  1937. for (int i = 0; i < p_paths.size(); i++) {
  1938. if (i > 0) {
  1939. names += ",";
  1940. }
  1941. names += p_paths[i];
  1942. }
  1943. undo_redo->create_action(TTR("Set Multiple:") + " " + names, UndoRedo::MERGE_ENDS);
  1944. for (int i = 0; i < p_paths.size(); i++) {
  1945. _edit_set(p_paths[i], p_values[i], false, "");
  1946. if (restart_request_props.has(p_paths[i])) {
  1947. emit_signal("restart_requested");
  1948. }
  1949. }
  1950. changing++;
  1951. undo_redo->commit_action();
  1952. changing--;
  1953. }
  1954. void EditorInspector::_property_keyed(const String &p_path, bool p_advance) {
  1955. if (!object) {
  1956. return;
  1957. }
  1958. emit_signal("property_keyed", p_path, object->get(p_path), p_advance); //second param is deprecated
  1959. }
  1960. void EditorInspector::_property_deleted(const String &p_path) {
  1961. print_line("deleted pressed?");
  1962. if (!object) {
  1963. return;
  1964. }
  1965. emit_signal("property_deleted", p_path); //second param is deprecated
  1966. }
  1967. void EditorInspector::_property_keyed_with_value(const String &p_path, const Variant &p_value, bool p_advance) {
  1968. if (!object) {
  1969. return;
  1970. }
  1971. emit_signal("property_keyed", p_path, p_value, p_advance); //second param is deprecated
  1972. }
  1973. void EditorInspector::_property_checked(const String &p_path, bool p_checked) {
  1974. if (!object) {
  1975. return;
  1976. }
  1977. //property checked
  1978. if (autoclear) {
  1979. if (!p_checked) {
  1980. object->set(p_path, Variant());
  1981. } else {
  1982. Variant to_create;
  1983. List<PropertyInfo> pinfo;
  1984. object->get_property_list(&pinfo);
  1985. for (List<PropertyInfo>::Element *E = pinfo.front(); E; E = E->next()) {
  1986. if (E->get().name == p_path) {
  1987. Callable::CallError ce;
  1988. Variant::construct(E->get().type, to_create, nullptr, 0, ce);
  1989. break;
  1990. }
  1991. }
  1992. object->set(p_path, to_create);
  1993. }
  1994. if (editor_property_map.has(p_path)) {
  1995. for (List<EditorProperty *>::Element *E = editor_property_map[p_path].front(); E; E = E->next()) {
  1996. E->get()->update_property();
  1997. E->get()->update_reload_status();
  1998. E->get()->update_cache();
  1999. }
  2000. }
  2001. } else {
  2002. emit_signal("property_toggled", p_path, p_checked);
  2003. }
  2004. }
  2005. void EditorInspector::_property_selected(const String &p_path, int p_focusable) {
  2006. property_selected = p_path;
  2007. property_focusable = p_focusable;
  2008. //deselect the others
  2009. for (Map<StringName, List<EditorProperty *>>::Element *F = editor_property_map.front(); F; F = F->next()) {
  2010. if (F->key() == property_selected) {
  2011. continue;
  2012. }
  2013. for (List<EditorProperty *>::Element *E = F->get().front(); E; E = E->next()) {
  2014. if (E->get()->is_selected()) {
  2015. E->get()->deselect();
  2016. }
  2017. }
  2018. }
  2019. emit_signal("property_selected", p_path);
  2020. }
  2021. void EditorInspector::_object_id_selected(const String &p_path, ObjectID p_id) {
  2022. emit_signal("object_id_selected", p_id);
  2023. }
  2024. void EditorInspector::_resource_selected(const String &p_path, RES p_resource) {
  2025. emit_signal("resource_selected", p_resource, p_path);
  2026. }
  2027. void EditorInspector::_node_removed(Node *p_node) {
  2028. if (p_node == object) {
  2029. edit(nullptr);
  2030. }
  2031. }
  2032. void EditorInspector::_notification(int p_what) {
  2033. if (p_what == NOTIFICATION_READY) {
  2034. EditorFeatureProfileManager::get_singleton()->connect("current_feature_profile_changed", callable_mp(this, &EditorInspector::_feature_profile_changed));
  2035. set_process(is_visible_in_tree());
  2036. }
  2037. if (p_what == NOTIFICATION_ENTER_TREE) {
  2038. _update_inspector_bg();
  2039. if (!sub_inspector) {
  2040. get_tree()->connect("node_removed", callable_mp(this, &EditorInspector::_node_removed));
  2041. }
  2042. }
  2043. if (p_what == NOTIFICATION_PREDELETE) {
  2044. edit(nullptr); //just in case
  2045. }
  2046. if (p_what == NOTIFICATION_EXIT_TREE) {
  2047. if (!sub_inspector) {
  2048. get_tree()->disconnect("node_removed", callable_mp(this, &EditorInspector::_node_removed));
  2049. }
  2050. edit(nullptr);
  2051. }
  2052. if (p_what == NOTIFICATION_VISIBILITY_CHANGED) {
  2053. set_process(is_visible_in_tree());
  2054. }
  2055. if (p_what == NOTIFICATION_PROCESS) {
  2056. if (update_scroll_request >= 0) {
  2057. get_v_scrollbar()->call_deferred("set_value", update_scroll_request);
  2058. update_scroll_request = -1;
  2059. }
  2060. if (refresh_countdown > 0) {
  2061. refresh_countdown -= get_process_delta_time();
  2062. if (refresh_countdown <= 0) {
  2063. for (Map<StringName, List<EditorProperty *>>::Element *F = editor_property_map.front(); F; F = F->next()) {
  2064. for (List<EditorProperty *>::Element *E = F->get().front(); E; E = E->next()) {
  2065. if (!E->get()->is_cache_valid()) {
  2066. E->get()->update_property();
  2067. E->get()->update_reload_status();
  2068. E->get()->update_cache();
  2069. }
  2070. }
  2071. }
  2072. refresh_countdown = float(EditorSettings::get_singleton()->get("docks/property_editor/auto_refresh_interval"));
  2073. }
  2074. }
  2075. changing++;
  2076. if (update_tree_pending) {
  2077. update_tree();
  2078. update_tree_pending = false;
  2079. pending.clear();
  2080. } else {
  2081. while (pending.size()) {
  2082. StringName prop = pending.front()->get();
  2083. if (editor_property_map.has(prop)) {
  2084. for (List<EditorProperty *>::Element *E = editor_property_map[prop].front(); E; E = E->next()) {
  2085. E->get()->update_property();
  2086. E->get()->update_reload_status();
  2087. E->get()->update_cache();
  2088. }
  2089. }
  2090. pending.erase(pending.front());
  2091. }
  2092. }
  2093. changing--;
  2094. }
  2095. if (p_what == EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED) {
  2096. _update_inspector_bg();
  2097. update_tree();
  2098. }
  2099. }
  2100. void EditorInspector::_changed_callback() {
  2101. //this is called when property change is notified via notify_property_list_changed()
  2102. if (object != nullptr) {
  2103. _edit_request_change(object, String());
  2104. }
  2105. }
  2106. void EditorInspector::_vscroll_changed(double p_offset) {
  2107. if (update_scroll_request >= 0) { //waiting, do nothing
  2108. return;
  2109. }
  2110. if (object) {
  2111. scroll_cache[object->get_instance_id()] = p_offset;
  2112. }
  2113. }
  2114. void EditorInspector::set_property_prefix(const String &p_prefix) {
  2115. property_prefix = p_prefix;
  2116. }
  2117. String EditorInspector::get_property_prefix() const {
  2118. return property_prefix;
  2119. }
  2120. void EditorInspector::set_object_class(const String &p_class) {
  2121. object_class = p_class;
  2122. }
  2123. String EditorInspector::get_object_class() const {
  2124. return object_class;
  2125. }
  2126. void EditorInspector::_feature_profile_changed() {
  2127. update_tree();
  2128. }
  2129. void EditorInspector::_update_script_class_properties(const Object &p_object, List<PropertyInfo> &r_list) const {
  2130. Ref<Script> script = p_object.get_script();
  2131. if (script.is_null()) {
  2132. return;
  2133. }
  2134. List<Ref<Script>> classes;
  2135. // NodeC -> NodeB -> NodeA
  2136. while (script.is_valid()) {
  2137. classes.push_front(script);
  2138. script = script->get_base_script();
  2139. }
  2140. if (classes.is_empty()) {
  2141. return;
  2142. }
  2143. // Script Variables -> to insert: NodeC..B..A -> bottom (insert_here)
  2144. List<PropertyInfo>::Element *script_variables = NULL;
  2145. List<PropertyInfo>::Element *bottom = NULL;
  2146. List<PropertyInfo>::Element *insert_here = NULL;
  2147. for (List<PropertyInfo>::Element *E = r_list.front(); E; E = E->next()) {
  2148. PropertyInfo &pi = E->get();
  2149. if (pi.name != "Script Variables") {
  2150. continue;
  2151. }
  2152. script_variables = E;
  2153. bottom = r_list.insert_after(script_variables, PropertyInfo());
  2154. insert_here = bottom;
  2155. break;
  2156. }
  2157. Set<StringName> added;
  2158. for (List<Ref<Script>>::Element *E = classes.front(); E; E = E->next()) {
  2159. Ref<Script> s = E->get();
  2160. String path = s->get_path();
  2161. String name = EditorNode::get_editor_data().script_class_get_name(path);
  2162. if (name.is_empty()) {
  2163. if (!path.is_empty() && path.find("::") == -1) {
  2164. name = path.get_file();
  2165. } else {
  2166. name = TTR("Built-in script");
  2167. }
  2168. }
  2169. List<PropertyInfo> props;
  2170. s->get_script_property_list(&props);
  2171. // Script Variables -> NodeA -> bottom (insert_here)
  2172. List<PropertyInfo>::Element *category = r_list.insert_before(insert_here, PropertyInfo(Variant::NIL, name, PROPERTY_HINT_NONE, path, PROPERTY_USAGE_CATEGORY));
  2173. // Script Variables -> NodeA -> A props... -> bottom (insert_here)
  2174. for (List<PropertyInfo>::Element *P = props.front(); P; P = P->next()) {
  2175. PropertyInfo &pi = P->get();
  2176. if (added.has(pi.name)) {
  2177. continue;
  2178. }
  2179. added.insert(pi.name);
  2180. r_list.insert_before(insert_here, pi);
  2181. }
  2182. // Script Variables -> NodeA (insert_here) -> A props... -> bottom
  2183. insert_here = category;
  2184. }
  2185. // NodeC -> C props... -> NodeB..C..
  2186. r_list.erase(script_variables);
  2187. List<PropertyInfo>::Element *to_delete = bottom->next();
  2188. while (to_delete && !(to_delete->get().usage & PROPERTY_USAGE_CATEGORY)) {
  2189. r_list.erase(to_delete);
  2190. to_delete = bottom->next();
  2191. }
  2192. r_list.erase(bottom);
  2193. }
  2194. void EditorInspector::set_restrict_to_basic_settings(bool p_restrict) {
  2195. restrict_to_basic = p_restrict;
  2196. update_tree();
  2197. }
  2198. void EditorInspector::_bind_methods() {
  2199. ClassDB::bind_method("_edit_request_change", &EditorInspector::_edit_request_change);
  2200. ADD_SIGNAL(MethodInfo("property_selected", PropertyInfo(Variant::STRING, "property")));
  2201. ADD_SIGNAL(MethodInfo("property_keyed", PropertyInfo(Variant::STRING, "property")));
  2202. ADD_SIGNAL(MethodInfo("property_deleted", PropertyInfo(Variant::STRING, "property")));
  2203. ADD_SIGNAL(MethodInfo("resource_selected", PropertyInfo(Variant::OBJECT, "res"), PropertyInfo(Variant::STRING, "prop")));
  2204. ADD_SIGNAL(MethodInfo("object_id_selected", PropertyInfo(Variant::INT, "id")));
  2205. ADD_SIGNAL(MethodInfo("property_edited", PropertyInfo(Variant::STRING, "property")));
  2206. ADD_SIGNAL(MethodInfo("property_toggled", PropertyInfo(Variant::STRING, "property"), PropertyInfo(Variant::BOOL, "checked")));
  2207. ADD_SIGNAL(MethodInfo("restart_requested"));
  2208. }
  2209. EditorInspector::EditorInspector() {
  2210. object = nullptr;
  2211. undo_redo = nullptr;
  2212. main_vbox = memnew(VBoxContainer);
  2213. main_vbox->set_h_size_flags(SIZE_EXPAND_FILL);
  2214. main_vbox->add_theme_constant_override("separation", 0);
  2215. add_child(main_vbox);
  2216. set_enable_h_scroll(false);
  2217. set_enable_v_scroll(true);
  2218. wide_editors = false;
  2219. show_categories = false;
  2220. hide_script = true;
  2221. use_doc_hints = false;
  2222. capitalize_paths = true;
  2223. use_filter = false;
  2224. autoclear = false;
  2225. changing = 0;
  2226. use_folding = false;
  2227. update_all_pending = false;
  2228. update_tree_pending = false;
  2229. read_only = false;
  2230. search_box = nullptr;
  2231. keying = false;
  2232. _prop_edited = "property_edited";
  2233. set_process(false);
  2234. property_focusable = -1;
  2235. sub_inspector = false;
  2236. deletable_properties = false;
  2237. get_v_scrollbar()->connect("value_changed", callable_mp(this, &EditorInspector::_vscroll_changed));
  2238. update_scroll_request = -1;
  2239. if (EditorSettings::get_singleton()) {
  2240. refresh_countdown = float(EditorSettings::get_singleton()->get("docks/property_editor/auto_refresh_interval"));
  2241. } else {
  2242. //used when class is created by the docgen to dump default values of everything bindable, editorsettings may not be created
  2243. refresh_countdown = 0.33;
  2244. }
  2245. }