editor_inspector.cpp 63 KB

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