editor_inspector.cpp 66 KB

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