script_editor_debugger.cpp 77 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626
  1. /*************************************************************************/
  2. /* script_editor_debugger.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 "script_editor_debugger.h"
  31. #include "core/io/marshalls.h"
  32. #include "core/project_settings.h"
  33. #include "core/ustring.h"
  34. #include "editor/plugins/canvas_item_editor_plugin.h"
  35. #include "editor/plugins/spatial_editor_plugin.h"
  36. #include "editor_log.h"
  37. #include "editor_network_profiler.h"
  38. #include "editor_node.h"
  39. #include "editor_profiler.h"
  40. #include "editor_scale.h"
  41. #include "editor_settings.h"
  42. #include "main/performance.h"
  43. #include "property_editor.h"
  44. #include "scene/gui/dialogs.h"
  45. #include "scene/gui/label.h"
  46. #include "scene/gui/line_edit.h"
  47. #include "scene/gui/margin_container.h"
  48. #include "scene/gui/rich_text_label.h"
  49. #include "scene/gui/separator.h"
  50. #include "scene/gui/split_container.h"
  51. #include "scene/gui/tab_container.h"
  52. #include "scene/gui/texture_button.h"
  53. #include "scene/gui/tree.h"
  54. #include "scene/resources/packed_scene.h"
  55. class ScriptEditorDebuggerVariables : public Object {
  56. GDCLASS(ScriptEditorDebuggerVariables, Object);
  57. List<PropertyInfo> props;
  58. Map<StringName, Variant> values;
  59. protected:
  60. bool _set(const StringName &p_name, const Variant &p_value) {
  61. return false;
  62. }
  63. bool _get(const StringName &p_name, Variant &r_ret) const {
  64. if (!values.has(p_name))
  65. return false;
  66. r_ret = values[p_name];
  67. return true;
  68. }
  69. void _get_property_list(List<PropertyInfo> *p_list) const {
  70. for (const List<PropertyInfo>::Element *E = props.front(); E; E = E->next())
  71. p_list->push_back(E->get());
  72. }
  73. public:
  74. void clear() {
  75. props.clear();
  76. values.clear();
  77. }
  78. String get_var_value(const String &p_var) const {
  79. for (Map<StringName, Variant>::Element *E = values.front(); E; E = E->next()) {
  80. String v = E->key().operator String().get_slice("/", 1);
  81. if (v == p_var)
  82. return E->get();
  83. }
  84. return "";
  85. }
  86. void add_property(const String &p_name, const Variant &p_value, const PropertyHint &p_hint, const String p_hint_string) {
  87. PropertyInfo pinfo;
  88. pinfo.name = p_name;
  89. pinfo.type = p_value.get_type();
  90. pinfo.hint = p_hint;
  91. pinfo.hint_string = p_hint_string;
  92. props.push_back(pinfo);
  93. values[p_name] = p_value;
  94. }
  95. void update() {
  96. _change_notify();
  97. }
  98. ScriptEditorDebuggerVariables() {
  99. }
  100. };
  101. class ScriptEditorDebuggerInspectedObject : public Object {
  102. GDCLASS(ScriptEditorDebuggerInspectedObject, Object);
  103. protected:
  104. bool _set(const StringName &p_name, const Variant &p_value) {
  105. if (!prop_values.has(p_name) || String(p_name).begins_with("Constants/"))
  106. return false;
  107. prop_values[p_name] = p_value;
  108. emit_signal("value_edited", p_name, p_value);
  109. return true;
  110. }
  111. bool _get(const StringName &p_name, Variant &r_ret) const {
  112. if (!prop_values.has(p_name))
  113. return false;
  114. r_ret = prop_values[p_name];
  115. return true;
  116. }
  117. void _get_property_list(List<PropertyInfo> *p_list) const {
  118. p_list->clear(); //sorry, no want category
  119. for (const List<PropertyInfo>::Element *E = prop_list.front(); E; E = E->next()) {
  120. p_list->push_back(E->get());
  121. }
  122. }
  123. static void _bind_methods() {
  124. ClassDB::bind_method(D_METHOD("get_title"), &ScriptEditorDebuggerInspectedObject::get_title);
  125. ClassDB::bind_method(D_METHOD("get_variant"), &ScriptEditorDebuggerInspectedObject::get_variant);
  126. ClassDB::bind_method(D_METHOD("clear"), &ScriptEditorDebuggerInspectedObject::clear);
  127. ClassDB::bind_method(D_METHOD("get_remote_object_id"), &ScriptEditorDebuggerInspectedObject::get_remote_object_id);
  128. ADD_SIGNAL(MethodInfo("value_edited"));
  129. }
  130. public:
  131. String type_name;
  132. ObjectID remote_object_id;
  133. List<PropertyInfo> prop_list;
  134. Map<StringName, Variant> prop_values;
  135. ObjectID get_remote_object_id() {
  136. return remote_object_id;
  137. }
  138. String get_title() {
  139. if (remote_object_id)
  140. return TTR("Remote ") + String(type_name) + ": " + itos(remote_object_id);
  141. else
  142. return "<null>";
  143. }
  144. Variant get_variant(const StringName &p_name) {
  145. Variant var;
  146. _get(p_name, var);
  147. return var;
  148. }
  149. void clear() {
  150. prop_list.clear();
  151. prop_values.clear();
  152. }
  153. void update() {
  154. _change_notify();
  155. }
  156. void update_single(const char *p_prop) {
  157. _change_notify(p_prop);
  158. }
  159. ScriptEditorDebuggerInspectedObject() {
  160. remote_object_id = 0;
  161. }
  162. };
  163. void ScriptEditorDebugger::debug_copy() {
  164. String msg = reason->get_text();
  165. if (msg == "") return;
  166. OS::get_singleton()->set_clipboard(msg);
  167. }
  168. void ScriptEditorDebugger::debug_skip_breakpoints() {
  169. skip_breakpoints_value = !skip_breakpoints_value;
  170. if (skip_breakpoints_value)
  171. skip_breakpoints->set_icon(get_icon("DebugSkipBreakpointsOn", "EditorIcons"));
  172. else
  173. skip_breakpoints->set_icon(get_icon("DebugSkipBreakpointsOff", "EditorIcons"));
  174. if (connection.is_valid()) {
  175. Array msg;
  176. msg.push_back("set_skip_breakpoints");
  177. msg.push_back(skip_breakpoints_value);
  178. ppeer->put_var(msg);
  179. }
  180. }
  181. void ScriptEditorDebugger::debug_next() {
  182. ERR_FAIL_COND(!breaked);
  183. ERR_FAIL_COND(connection.is_null());
  184. ERR_FAIL_COND(!connection->is_connected_to_host());
  185. Array msg;
  186. msg.push_back("next");
  187. ppeer->put_var(msg);
  188. _clear_execution();
  189. stack_dump->clear();
  190. }
  191. void ScriptEditorDebugger::debug_step() {
  192. ERR_FAIL_COND(!breaked);
  193. ERR_FAIL_COND(connection.is_null());
  194. ERR_FAIL_COND(!connection->is_connected_to_host());
  195. Array msg;
  196. msg.push_back("step");
  197. ppeer->put_var(msg);
  198. _clear_execution();
  199. stack_dump->clear();
  200. }
  201. void ScriptEditorDebugger::debug_break() {
  202. ERR_FAIL_COND(breaked);
  203. ERR_FAIL_COND(connection.is_null());
  204. ERR_FAIL_COND(!connection->is_connected_to_host());
  205. Array msg;
  206. msg.push_back("break");
  207. ppeer->put_var(msg);
  208. }
  209. void ScriptEditorDebugger::debug_continue() {
  210. ERR_FAIL_COND(!breaked);
  211. ERR_FAIL_COND(connection.is_null());
  212. ERR_FAIL_COND(!connection->is_connected_to_host());
  213. OS::get_singleton()->enable_for_stealing_focus(EditorNode::get_singleton()->get_child_process_id());
  214. Array msg;
  215. _clear_execution();
  216. msg.push_back("continue");
  217. ppeer->put_var(msg);
  218. }
  219. void ScriptEditorDebugger::_scene_tree_folded(Object *obj) {
  220. if (updating_scene_tree) {
  221. return;
  222. }
  223. TreeItem *item = Object::cast_to<TreeItem>(obj);
  224. if (!item)
  225. return;
  226. ObjectID id = item->get_metadata(0);
  227. if (unfold_cache.has(id)) {
  228. unfold_cache.erase(id);
  229. } else {
  230. unfold_cache.insert(id);
  231. }
  232. }
  233. void ScriptEditorDebugger::_scene_tree_selected() {
  234. if (updating_scene_tree) {
  235. return;
  236. }
  237. TreeItem *item = inspect_scene_tree->get_selected();
  238. if (!item) {
  239. return;
  240. }
  241. inspected_object_id = item->get_metadata(0);
  242. Array msg;
  243. msg.push_back("inspect_object");
  244. msg.push_back(inspected_object_id);
  245. ppeer->put_var(msg);
  246. }
  247. void ScriptEditorDebugger::_scene_tree_rmb_selected(const Vector2 &p_position) {
  248. TreeItem *item = inspect_scene_tree->get_item_at_position(p_position);
  249. if (!item)
  250. return;
  251. item->select(0);
  252. item_menu->clear();
  253. item_menu->add_icon_item(get_icon("CreateNewSceneFrom", "EditorIcons"), TTR("Save Branch as Scene"), ITEM_MENU_SAVE_REMOTE_NODE);
  254. item_menu->add_icon_item(get_icon("CopyNodePath", "EditorIcons"), TTR("Copy Node Path"), ITEM_MENU_COPY_NODE_PATH);
  255. item_menu->set_global_position(get_global_mouse_position());
  256. item_menu->popup();
  257. }
  258. void ScriptEditorDebugger::_file_selected(const String &p_file) {
  259. switch (file_dialog_mode) {
  260. case SAVE_NODE: {
  261. Array msg;
  262. msg.push_back("save_node");
  263. msg.push_back(inspected_object_id);
  264. msg.push_back(p_file);
  265. ppeer->put_var(msg);
  266. } break;
  267. case SAVE_CSV: {
  268. Error err;
  269. FileAccessRef file = FileAccess::open(p_file, FileAccess::WRITE, &err);
  270. if (err != OK) {
  271. ERR_PRINTS("Failed to open " + p_file);
  272. return;
  273. }
  274. Vector<String> line;
  275. line.resize(Performance::MONITOR_MAX);
  276. // signatures
  277. for (int i = 0; i < Performance::MONITOR_MAX; i++) {
  278. line.write[i] = Performance::get_singleton()->get_monitor_name(Performance::Monitor(i));
  279. }
  280. file->store_csv_line(line);
  281. // values
  282. List<Vector<float> >::Element *E = perf_history.back();
  283. while (E) {
  284. Vector<float> &perf_data = E->get();
  285. for (int i = 0; i < perf_data.size(); i++) {
  286. line.write[i] = String::num_real(perf_data[i]);
  287. }
  288. file->store_csv_line(line);
  289. E = E->prev();
  290. }
  291. file->store_string("\n");
  292. Vector<Vector<String> > profiler_data = profiler->get_data_as_csv();
  293. for (int i = 0; i < profiler_data.size(); i++) {
  294. file->store_csv_line(profiler_data[i]);
  295. }
  296. } break;
  297. }
  298. }
  299. void ScriptEditorDebugger::_scene_tree_property_value_edited(const String &p_prop, const Variant &p_value) {
  300. Array msg;
  301. msg.push_back("set_object_property");
  302. msg.push_back(inspected_object_id);
  303. msg.push_back(p_prop);
  304. msg.push_back(p_value);
  305. ppeer->put_var(msg);
  306. inspect_edited_object_timeout = 0.7; //avoid annoyance, don't request soon after editing
  307. }
  308. void ScriptEditorDebugger::_scene_tree_property_select_object(ObjectID p_object) {
  309. inspected_object_id = p_object;
  310. Array msg;
  311. msg.push_back("inspect_object");
  312. msg.push_back(inspected_object_id);
  313. ppeer->put_var(msg);
  314. }
  315. void ScriptEditorDebugger::_scene_tree_request() {
  316. ERR_FAIL_COND(connection.is_null());
  317. ERR_FAIL_COND(!connection->is_connected_to_host());
  318. Array msg;
  319. msg.push_back("request_scene_tree");
  320. ppeer->put_var(msg);
  321. }
  322. /// Populates inspect_scene_tree recursively given data in nodes.
  323. /// Nodes is an array containing 4 elements for each node, it follows this pattern:
  324. /// nodes[i] == number of direct children of this node
  325. /// nodes[i + 1] == node name
  326. /// nodes[i + 2] == node class
  327. /// nodes[i + 3] == node instance id
  328. ///
  329. /// Returns the number of items parsed in nodes from current_index.
  330. ///
  331. /// Given a nodes array like [R,A,B,C,D,E] the following Tree will be generated, assuming
  332. /// filter is an empty String, R and A child count are 2, B is 1 and C, D and E are 0.
  333. ///
  334. /// R
  335. /// |-A
  336. /// | |-B
  337. /// | | |-C
  338. /// | |
  339. /// | |-D
  340. /// |
  341. /// |-E
  342. ///
  343. int ScriptEditorDebugger::_update_scene_tree(TreeItem *parent, const Array &nodes, int current_index) {
  344. String filter = EditorNode::get_singleton()->get_scene_tree_dock()->get_filter();
  345. String item_text = nodes[current_index + 1];
  346. String item_type = nodes[current_index + 2];
  347. bool keep = filter.is_subsequence_ofi(item_text);
  348. TreeItem *item = inspect_scene_tree->create_item(parent);
  349. item->set_text(0, item_text);
  350. item->set_tooltip(0, TTR("Type:") + " " + item_type);
  351. ObjectID id = ObjectID(nodes[current_index + 3]);
  352. Ref<Texture> icon = EditorNode::get_singleton()->get_class_icon(nodes[current_index + 2], "");
  353. if (icon.is_valid()) {
  354. item->set_icon(0, icon);
  355. }
  356. item->set_metadata(0, id);
  357. if (id == inspected_object_id) {
  358. TreeItem *cti = item->get_parent();
  359. while (cti) {
  360. cti->set_collapsed(false);
  361. cti = cti->get_parent();
  362. }
  363. item->select(0);
  364. }
  365. // Set current item as collapsed if necessary
  366. if (parent) {
  367. if (!unfold_cache.has(id)) {
  368. item->set_collapsed(true);
  369. }
  370. }
  371. int children_count = nodes[current_index];
  372. // Tracks the total number of items parsed in nodes, this is used to skips nodes that
  373. // are not direct children of the current node since we can't know in advance the total
  374. // number of children, direct and not, of a node without traversing the nodes array previously.
  375. // Keeping track of this allows us to build our remote scene tree by traversing the node
  376. // array just once.
  377. int items_count = 1;
  378. for (int i = 0; i < children_count; i++) {
  379. // Called for each direct child of item.
  380. // Direct children of current item might not be adjacent so items_count must
  381. // be incremented by the number of items parsed until now, otherwise we would not
  382. // be able to access the next child of the current item.
  383. // items_count is multiplied by 4 since that's the number of elements in the nodes
  384. // array needed to represent a single node.
  385. items_count += _update_scene_tree(item, nodes, current_index + items_count * 4);
  386. }
  387. // If item has not children and should not be kept delete it
  388. if (!keep && !item->get_children() && parent) {
  389. parent->remove_child(item);
  390. memdelete(item);
  391. }
  392. return items_count;
  393. }
  394. void ScriptEditorDebugger::_video_mem_request() {
  395. ERR_FAIL_COND(connection.is_null());
  396. ERR_FAIL_COND(!connection->is_connected_to_host());
  397. Array msg;
  398. msg.push_back("request_video_mem");
  399. ppeer->put_var(msg);
  400. }
  401. Size2 ScriptEditorDebugger::get_minimum_size() const {
  402. Size2 ms = MarginContainer::get_minimum_size();
  403. ms.y = MAX(ms.y, 250 * EDSCALE);
  404. return ms;
  405. }
  406. void ScriptEditorDebugger::_parse_message(const String &p_msg, const Array &p_data) {
  407. if (p_msg == "debug_enter") {
  408. Array msg;
  409. msg.push_back("get_stack_dump");
  410. ppeer->put_var(msg);
  411. ERR_FAIL_COND(p_data.size() != 2);
  412. bool can_continue = p_data[0];
  413. String error = p_data[1];
  414. step->set_disabled(!can_continue);
  415. next->set_disabled(!can_continue);
  416. _set_reason_text(error, MESSAGE_ERROR);
  417. copy->set_disabled(false);
  418. breaked = true;
  419. dobreak->set_disabled(true);
  420. docontinue->set_disabled(false);
  421. emit_signal("breaked", true, can_continue);
  422. OS::get_singleton()->move_window_to_foreground();
  423. if (error != "") {
  424. tabs->set_current_tab(0);
  425. }
  426. profiler->set_enabled(false);
  427. EditorNode::get_singleton()->get_pause_button()->set_pressed(true);
  428. EditorNode::get_singleton()->make_bottom_panel_item_visible(this);
  429. _clear_remote_objects();
  430. } else if (p_msg == "debug_exit") {
  431. breaked = false;
  432. _clear_execution();
  433. copy->set_disabled(true);
  434. step->set_disabled(true);
  435. next->set_disabled(true);
  436. reason->set_text("");
  437. reason->set_tooltip("");
  438. back->set_disabled(true);
  439. forward->set_disabled(true);
  440. dobreak->set_disabled(false);
  441. docontinue->set_disabled(true);
  442. emit_signal("breaked", false, false, Variant());
  443. profiler->set_enabled(true);
  444. profiler->disable_seeking();
  445. EditorNode::get_singleton()->get_pause_button()->set_pressed(false);
  446. } else if (p_msg == "message:click_ctrl") {
  447. clicked_ctrl->set_text(p_data[0]);
  448. clicked_ctrl_type->set_text(p_data[1]);
  449. } else if (p_msg == "message:scene_tree") {
  450. inspect_scene_tree->clear();
  451. Map<int, TreeItem *> lv;
  452. updating_scene_tree = true;
  453. _update_scene_tree(NULL, p_data, 0);
  454. updating_scene_tree = false;
  455. le_clear->set_disabled(false);
  456. le_set->set_disabled(false);
  457. } else if (p_msg == "message:inspect_object") {
  458. ScriptEditorDebuggerInspectedObject *debugObj = NULL;
  459. ObjectID id = p_data[0];
  460. String type = p_data[1];
  461. Array properties = p_data[2];
  462. if (remote_objects.has(id)) {
  463. debugObj = remote_objects[id];
  464. } else {
  465. debugObj = memnew(ScriptEditorDebuggerInspectedObject);
  466. debugObj->remote_object_id = id;
  467. debugObj->type_name = type;
  468. remote_objects[id] = debugObj;
  469. debugObj->connect("value_edited", this, "_scene_tree_property_value_edited");
  470. }
  471. int old_prop_size = debugObj->prop_list.size();
  472. debugObj->prop_list.clear();
  473. int new_props_added = 0;
  474. Set<String> changed;
  475. for (int i = 0; i < properties.size(); i++) {
  476. Array prop = properties[i];
  477. if (prop.size() != 6)
  478. continue;
  479. PropertyInfo pinfo;
  480. pinfo.name = prop[0];
  481. pinfo.type = Variant::Type(int(prop[1]));
  482. pinfo.hint = PropertyHint(int(prop[2]));
  483. pinfo.hint_string = prop[3];
  484. pinfo.usage = PropertyUsageFlags(int(prop[4]));
  485. Variant var = prop[5];
  486. if (pinfo.type == Variant::OBJECT) {
  487. if (var.is_zero()) {
  488. var = RES();
  489. } else if (var.get_type() == Variant::STRING) {
  490. String path = var;
  491. if (path.find("::") != -1) {
  492. // built-in resource
  493. String base_path = path.get_slice("::", 0);
  494. if (ResourceLoader::get_resource_type(base_path) == "PackedScene") {
  495. if (!EditorNode::get_singleton()->is_scene_open(base_path)) {
  496. EditorNode::get_singleton()->load_scene(base_path);
  497. }
  498. } else {
  499. EditorNode::get_singleton()->load_resource(base_path);
  500. }
  501. }
  502. var = ResourceLoader::load(path);
  503. if (pinfo.hint_string == "Script") {
  504. if (debugObj->get_script() != var) {
  505. debugObj->set_script(RefPtr());
  506. Ref<Script> script(var);
  507. if (!script.is_null()) {
  508. ScriptInstance *script_instance = script->placeholder_instance_create(debugObj);
  509. debugObj->set_script_and_instance(var, script_instance);
  510. }
  511. }
  512. }
  513. } else if (var.get_type() == Variant::OBJECT) {
  514. if (((Object *)var)->is_class("EncodedObjectAsID")) {
  515. var = Object::cast_to<EncodedObjectAsID>(var)->get_object_id();
  516. pinfo.type = var.get_type();
  517. pinfo.hint = PROPERTY_HINT_OBJECT_ID;
  518. pinfo.hint_string = "Object";
  519. }
  520. }
  521. }
  522. //always add the property, since props may have been added or removed
  523. debugObj->prop_list.push_back(pinfo);
  524. if (!debugObj->prop_values.has(pinfo.name)) {
  525. new_props_added++;
  526. debugObj->prop_values[pinfo.name] = var;
  527. } else {
  528. if (bool(Variant::evaluate(Variant::OP_NOT_EQUAL, debugObj->prop_values[pinfo.name], var))) {
  529. debugObj->prop_values[pinfo.name] = var;
  530. changed.insert(pinfo.name);
  531. }
  532. }
  533. }
  534. if (editor->get_editor_history()->get_current() != debugObj->get_instance_id()) {
  535. editor->push_item(debugObj, "");
  536. } else {
  537. if (old_prop_size == debugObj->prop_list.size() && new_props_added == 0) {
  538. //only some may have changed, if so, then update those, if exist
  539. for (Set<String>::Element *E = changed.front(); E; E = E->next()) {
  540. EditorNode::get_singleton()->get_inspector()->update_property(E->get());
  541. }
  542. } else {
  543. //full update, because props were added or removed
  544. debugObj->update();
  545. }
  546. }
  547. } else if (p_msg == "message:video_mem") {
  548. vmem_tree->clear();
  549. TreeItem *root = vmem_tree->create_item();
  550. int total = 0;
  551. for (int i = 0; i < p_data.size(); i += 4) {
  552. TreeItem *it = vmem_tree->create_item(root);
  553. String type = p_data[i + 1];
  554. int bytes = p_data[i + 3].operator int();
  555. it->set_text(0, p_data[i + 0]); //path
  556. it->set_text(1, type); //type
  557. it->set_text(2, p_data[i + 2]); //type
  558. it->set_text(3, String::humanize_size(bytes)); //type
  559. total += bytes;
  560. if (has_icon(type, "EditorIcons"))
  561. it->set_icon(0, get_icon(type, "EditorIcons"));
  562. }
  563. vmem_total->set_tooltip(TTR("Bytes:") + " " + itos(total));
  564. vmem_total->set_text(String::humanize_size(total));
  565. } else if (p_msg == "stack_dump") {
  566. stack_dump->clear();
  567. TreeItem *r = stack_dump->create_item();
  568. for (int i = 0; i < p_data.size(); i++) {
  569. Dictionary d = p_data[i];
  570. ERR_CONTINUE(!d.has("function"));
  571. ERR_CONTINUE(!d.has("file"));
  572. ERR_CONTINUE(!d.has("line"));
  573. ERR_CONTINUE(!d.has("id"));
  574. TreeItem *s = stack_dump->create_item(r);
  575. d["frame"] = i;
  576. s->set_metadata(0, d);
  577. String line = itos(i) + " - " + String(d["file"]) + ":" + itos(d["line"]) + " - at function: " + d["function"];
  578. s->set_text(0, line);
  579. if (i == 0)
  580. s->select(0);
  581. }
  582. } else if (p_msg == "stack_frame_vars") {
  583. variables->clear();
  584. int ofs = 0;
  585. int mcount = p_data[ofs];
  586. ofs++;
  587. for (int i = 0; i < mcount; i++) {
  588. String n = p_data[ofs + i * 2 + 0];
  589. Variant v = p_data[ofs + i * 2 + 1];
  590. PropertyHint h = PROPERTY_HINT_NONE;
  591. String hs = String();
  592. if (v.get_type() == Variant::OBJECT) {
  593. v = Object::cast_to<EncodedObjectAsID>(v)->get_object_id();
  594. h = PROPERTY_HINT_OBJECT_ID;
  595. hs = "Object";
  596. }
  597. variables->add_property("Locals/" + n, v, h, hs);
  598. }
  599. ofs += mcount * 2;
  600. mcount = p_data[ofs];
  601. ofs++;
  602. for (int i = 0; i < mcount; i++) {
  603. String n = p_data[ofs + i * 2 + 0];
  604. Variant v = p_data[ofs + i * 2 + 1];
  605. PropertyHint h = PROPERTY_HINT_NONE;
  606. String hs = String();
  607. if (v.get_type() == Variant::OBJECT) {
  608. v = Object::cast_to<EncodedObjectAsID>(v)->get_object_id();
  609. h = PROPERTY_HINT_OBJECT_ID;
  610. hs = "Object";
  611. }
  612. variables->add_property("Members/" + n, v, h, hs);
  613. if (n == "self") {
  614. _scene_tree_property_select_object(v);
  615. }
  616. }
  617. ofs += mcount * 2;
  618. mcount = p_data[ofs];
  619. ofs++;
  620. for (int i = 0; i < mcount; i++) {
  621. String n = p_data[ofs + i * 2 + 0];
  622. Variant v = p_data[ofs + i * 2 + 1];
  623. PropertyHint h = PROPERTY_HINT_NONE;
  624. String hs = String();
  625. if (v.get_type() == Variant::OBJECT) {
  626. v = Object::cast_to<EncodedObjectAsID>(v)->get_object_id();
  627. h = PROPERTY_HINT_OBJECT_ID;
  628. hs = "Object";
  629. }
  630. variables->add_property("Globals/" + n, v, h, hs);
  631. }
  632. variables->update();
  633. inspector->edit(variables);
  634. } else if (p_msg == "output") {
  635. //OUT
  636. for (int i = 0; i < p_data.size(); i++) {
  637. String t = p_data[i];
  638. //LOG
  639. if (!EditorNode::get_log()->is_visible()) {
  640. if (EditorNode::get_singleton()->are_bottom_panels_hidden()) {
  641. if (EDITOR_GET("run/output/always_open_output_on_play")) {
  642. EditorNode::get_singleton()->make_bottom_panel_item_visible(EditorNode::get_log());
  643. }
  644. }
  645. }
  646. EditorNode::get_log()->add_message(t);
  647. }
  648. } else if (p_msg == "performance") {
  649. Array arr = p_data[0];
  650. Vector<float> p;
  651. p.resize(arr.size());
  652. for (int i = 0; i < arr.size(); i++) {
  653. p.write[i] = arr[i];
  654. if (i < perf_items.size()) {
  655. float v = p[i];
  656. String vs = rtos(v);
  657. String tt = vs;
  658. switch (Performance::MonitorType((int)perf_items[i]->get_metadata(1))) {
  659. case Performance::MONITOR_TYPE_MEMORY: {
  660. vs = String::humanize_size(v);
  661. tt = vs;
  662. } break;
  663. case Performance::MONITOR_TYPE_TIME: {
  664. tt += " seconds";
  665. vs += " s";
  666. } break;
  667. default: {
  668. tt += " " + perf_items[i]->get_text(0);
  669. } break;
  670. }
  671. perf_items[i]->set_text(1, vs);
  672. perf_items[i]->set_tooltip(1, tt);
  673. if (p[i] > perf_max[i])
  674. perf_max.write[i] = p[i];
  675. }
  676. }
  677. perf_history.push_front(p);
  678. perf_draw->update();
  679. } else if (p_msg == "error") {
  680. // Should have at least two elements, error array and stack items count.
  681. ERR_FAIL_COND_MSG(p_data.size() < 2, "Malformed error message from script debugger.");
  682. // Error or warning data.
  683. Array err = p_data[0];
  684. ERR_FAIL_COND_MSG(err.size() < 10, "Malformed error message from script debugger.");
  685. // Format time.
  686. Array time_vals;
  687. time_vals.push_back(err[0]);
  688. time_vals.push_back(err[1]);
  689. time_vals.push_back(err[2]);
  690. time_vals.push_back(err[3]);
  691. bool e;
  692. String time = String("%d:%02d:%02d.%03d").sprintf(time_vals, &e);
  693. // Rest of the error data.
  694. String method = err[4];
  695. String source_file = err[5];
  696. String source_line = err[6];
  697. String error_cond = err[7];
  698. String error_msg = err[8];
  699. bool is_warning = err[9];
  700. bool has_method = !method.empty();
  701. bool has_error_msg = !error_msg.empty();
  702. bool source_is_project_file = source_file.begins_with("res://");
  703. // Metadata to highlight error line in scripts.
  704. Array source_meta;
  705. source_meta.push_back(source_file);
  706. source_meta.push_back(source_line);
  707. // Create error tree to display above error or warning details.
  708. TreeItem *r = error_tree->get_root();
  709. if (!r) {
  710. r = error_tree->create_item();
  711. }
  712. // Also provide the relevant details as tooltip to quickly check without
  713. // uncollapsing the tree.
  714. String tooltip = is_warning ? TTR("Warning:") : TTR("Error:");
  715. TreeItem *error = error_tree->create_item(r);
  716. error->set_collapsed(true);
  717. error->set_icon(0, get_icon(is_warning ? "Warning" : "Error", "EditorIcons"));
  718. error->set_text(0, time);
  719. error->set_text_align(0, TreeItem::ALIGN_LEFT);
  720. String error_title;
  721. // Include method name, when given, in error title.
  722. if (has_method)
  723. error_title += method + ": ";
  724. // If we have a (custom) error message, use it as title, and add a C++ Error
  725. // item with the original error condition.
  726. error_title += error_msg.empty() ? error_cond : error_msg;
  727. error->set_text(1, error_title);
  728. tooltip += " " + error_title + "\n";
  729. if (has_error_msg) {
  730. // Add item for C++ error condition.
  731. TreeItem *cpp_cond = error_tree->create_item(error);
  732. cpp_cond->set_text(0, "<" + TTR("C++ Error") + ">");
  733. cpp_cond->set_text(1, error_cond);
  734. cpp_cond->set_text_align(0, TreeItem::ALIGN_LEFT);
  735. tooltip += TTR("C++ Error:") + " " + error_cond + "\n";
  736. if (source_is_project_file)
  737. cpp_cond->set_metadata(0, source_meta);
  738. }
  739. // Source of the error.
  740. String source_txt = (source_is_project_file ? source_file.get_file() : source_file) + ":" + source_line;
  741. if (has_method)
  742. source_txt += " @ " + method + "()";
  743. TreeItem *cpp_source = error_tree->create_item(error);
  744. cpp_source->set_text(0, "<" + (source_is_project_file ? TTR("Source") : TTR("C++ Source")) + ">");
  745. cpp_source->set_text(1, source_txt);
  746. cpp_source->set_text_align(0, TreeItem::ALIGN_LEFT);
  747. tooltip += (source_is_project_file ? TTR("Source:") : TTR("C++ Source:")) + " " + source_txt + "\n";
  748. // Set metadata to highlight error line in scripts.
  749. if (source_is_project_file) {
  750. error->set_metadata(0, source_meta);
  751. cpp_source->set_metadata(0, source_meta);
  752. }
  753. error->set_tooltip(0, tooltip);
  754. error->set_tooltip(1, tooltip);
  755. // Format stack trace.
  756. // stack_items_count is the number of elements to parse, with 3 items per frame
  757. // of the stack trace (script, method, line).
  758. int stack_items_count = p_data[1];
  759. for (int i = 0; i < stack_items_count; i += 3) {
  760. String script = p_data[2 + i];
  761. String method2 = p_data[3 + i];
  762. int line = p_data[4 + i];
  763. TreeItem *stack_trace = error_tree->create_item(error);
  764. Array meta;
  765. meta.push_back(script);
  766. meta.push_back(line);
  767. stack_trace->set_metadata(0, meta);
  768. if (i == 0) {
  769. stack_trace->set_text(0, "<" + TTR("Stack Trace") + ">");
  770. stack_trace->set_text_align(0, TreeItem::ALIGN_LEFT);
  771. error->set_metadata(0, meta);
  772. }
  773. stack_trace->set_text(1, script.get_file() + ":" + itos(line) + " @ " + method2 + "()");
  774. }
  775. if (is_warning)
  776. warning_count++;
  777. else
  778. error_count++;
  779. } else if (p_msg == "profile_sig") {
  780. //cache a signature
  781. profiler_signature[p_data[1]] = p_data[0];
  782. } else if (p_msg == "profile_frame" || p_msg == "profile_total") {
  783. EditorProfiler::Metric metric;
  784. metric.valid = true;
  785. metric.frame_number = p_data[0];
  786. metric.frame_time = p_data[1];
  787. metric.idle_time = p_data[2];
  788. metric.physics_time = p_data[3];
  789. metric.physics_frame_time = p_data[4];
  790. int frame_data_amount = p_data[6];
  791. int frame_function_amount = p_data[7];
  792. if (frame_data_amount) {
  793. EditorProfiler::Metric::Category frame_time;
  794. frame_time.signature = "category_frame_time";
  795. frame_time.name = "Frame Time";
  796. frame_time.total_time = metric.frame_time;
  797. EditorProfiler::Metric::Category::Item item;
  798. item.calls = 1;
  799. item.line = 0;
  800. item.name = "Physics Time";
  801. item.total = metric.physics_time;
  802. item.self = item.total;
  803. item.signature = "physics_time";
  804. frame_time.items.push_back(item);
  805. item.name = "Idle Time";
  806. item.total = metric.idle_time;
  807. item.self = item.total;
  808. item.signature = "idle_time";
  809. frame_time.items.push_back(item);
  810. item.name = "Physics Frame Time";
  811. item.total = metric.physics_frame_time;
  812. item.self = item.total;
  813. item.signature = "physics_frame_time";
  814. frame_time.items.push_back(item);
  815. metric.categories.push_back(frame_time);
  816. }
  817. int idx = 8;
  818. for (int i = 0; i < frame_data_amount; i++) {
  819. EditorProfiler::Metric::Category c;
  820. String name = p_data[idx++];
  821. Array values = p_data[idx++];
  822. c.name = name.capitalize();
  823. c.items.resize(values.size() / 2);
  824. c.total_time = 0;
  825. c.signature = "categ::" + name;
  826. for (int j = 0; j < values.size(); j += 2) {
  827. EditorProfiler::Metric::Category::Item item;
  828. item.calls = 1;
  829. item.line = 0;
  830. item.name = values[j];
  831. item.self = values[j + 1];
  832. item.total = item.self;
  833. item.signature = "categ::" + name + "::" + item.name;
  834. item.name = item.name.capitalize();
  835. c.total_time += item.total;
  836. c.items.write[j / 2] = item;
  837. }
  838. metric.categories.push_back(c);
  839. }
  840. EditorProfiler::Metric::Category funcs;
  841. funcs.total_time = p_data[5]; //script time
  842. funcs.items.resize(frame_function_amount);
  843. funcs.name = "Script Functions";
  844. funcs.signature = "script_functions";
  845. for (int i = 0; i < frame_function_amount; i++) {
  846. int signature = p_data[idx++];
  847. int calls = p_data[idx++];
  848. float total = p_data[idx++];
  849. float self = p_data[idx++];
  850. EditorProfiler::Metric::Category::Item item;
  851. if (profiler_signature.has(signature)) {
  852. item.signature = profiler_signature[signature];
  853. String name = profiler_signature[signature];
  854. Vector<String> strings = name.split("::");
  855. if (strings.size() == 3) {
  856. item.name = strings[2];
  857. item.script = strings[0];
  858. item.line = strings[1].to_int();
  859. } else if (strings.size() == 4) { //Built-in scripts have an :: in their name
  860. item.name = strings[3];
  861. item.script = strings[0] + "::" + strings[1];
  862. item.line = strings[2].to_int();
  863. }
  864. } else {
  865. item.name = "SigErr " + itos(signature);
  866. }
  867. item.calls = calls;
  868. item.self = self;
  869. item.total = total;
  870. funcs.items.write[i] = item;
  871. }
  872. metric.categories.push_back(funcs);
  873. if (p_msg == "profile_frame")
  874. profiler->add_frame_metric(metric, false);
  875. else
  876. profiler->add_frame_metric(metric, true);
  877. } else if (p_msg == "network_profile") {
  878. int frame_size = 6;
  879. for (int i = 0; i < p_data.size(); i += frame_size) {
  880. MultiplayerAPI::ProfilingInfo pi;
  881. pi.node = p_data[i + 0];
  882. pi.node_path = p_data[i + 1];
  883. pi.incoming_rpc = p_data[i + 2];
  884. pi.incoming_rset = p_data[i + 3];
  885. pi.outgoing_rpc = p_data[i + 4];
  886. pi.outgoing_rset = p_data[i + 5];
  887. network_profiler->add_node_frame_data(pi);
  888. }
  889. } else if (p_msg == "network_bandwidth") {
  890. network_profiler->set_bandwidth(p_data[0], p_data[1]);
  891. } else if (p_msg == "kill_me") {
  892. editor->call_deferred("stop_child_process");
  893. }
  894. }
  895. void ScriptEditorDebugger::_set_reason_text(const String &p_reason, MessageType p_type) {
  896. switch (p_type) {
  897. case MESSAGE_ERROR:
  898. reason->add_color_override("font_color", get_color("error_color", "Editor"));
  899. break;
  900. case MESSAGE_WARNING:
  901. reason->add_color_override("font_color", get_color("warning_color", "Editor"));
  902. break;
  903. default:
  904. reason->add_color_override("font_color", get_color("success_color", "Editor"));
  905. }
  906. reason->set_text(p_reason);
  907. reason->set_tooltip(p_reason.word_wrap(80));
  908. }
  909. void ScriptEditorDebugger::_performance_select() {
  910. perf_draw->update();
  911. }
  912. void ScriptEditorDebugger::_performance_draw() {
  913. Vector<int> which;
  914. for (int i = 0; i < perf_items.size(); i++) {
  915. if (perf_items[i]->is_checked(0))
  916. which.push_back(i);
  917. }
  918. if (which.empty()) {
  919. info_message->show();
  920. return;
  921. }
  922. info_message->hide();
  923. Ref<StyleBox> graph_sb = get_stylebox("normal", "TextEdit");
  924. Ref<Font> graph_font = get_font("font", "TextEdit");
  925. int cols = Math::ceil(Math::sqrt((float)which.size()));
  926. int rows = Math::ceil((float)which.size() / cols);
  927. if (which.size() == 1)
  928. rows = 1;
  929. int margin = 3;
  930. int point_sep = 5;
  931. Size2i s = Size2i(perf_draw->get_size()) / Size2i(cols, rows);
  932. for (int i = 0; i < which.size(); i++) {
  933. Point2i p(i % cols, i / cols);
  934. Rect2i r(p * s, s);
  935. r.position += Point2(margin, margin);
  936. r.size -= Point2(margin, margin) * 2.0;
  937. perf_draw->draw_style_box(graph_sb, r);
  938. r.position += graph_sb->get_offset();
  939. r.size -= graph_sb->get_minimum_size();
  940. int pi = which[i];
  941. Color c = get_color("accent_color", "Editor");
  942. float h = (float)which[i] / (float)(perf_items.size());
  943. // Use a darker color on light backgrounds for better visibility
  944. float value_multiplier = EditorSettings::get_singleton()->is_dark_theme() ? 1.4 : 0.55;
  945. c.set_hsv(Math::fmod(h + 0.4, 0.9), c.get_s() * 0.9, c.get_v() * value_multiplier);
  946. c.a = 0.6;
  947. perf_draw->draw_string(graph_font, r.position + Point2(0, graph_font->get_ascent()), perf_items[pi]->get_text(0), c, r.size.x);
  948. c.a = 0.9;
  949. perf_draw->draw_string(graph_font, r.position + Point2(0, graph_font->get_ascent() + graph_font->get_height()), perf_items[pi]->get_text(1), c, r.size.y);
  950. float spacing = point_sep / float(cols);
  951. float from = r.size.width;
  952. List<Vector<float> >::Element *E = perf_history.front();
  953. float prev = -1;
  954. while (from >= 0 && E) {
  955. float m = perf_max[pi];
  956. if (m == 0)
  957. m = 0.00001;
  958. float h2 = E->get()[pi] / m;
  959. h2 = (1.0 - h2) * r.size.y;
  960. if (E != perf_history.front())
  961. perf_draw->draw_line(r.position + Point2(from, h2), r.position + Point2(from + spacing, prev), c, Math::round(EDSCALE), true);
  962. prev = h2;
  963. E = E->next();
  964. from -= spacing;
  965. }
  966. }
  967. }
  968. void ScriptEditorDebugger::_notification(int p_what) {
  969. switch (p_what) {
  970. case NOTIFICATION_ENTER_TREE: {
  971. inspector->edit(variables);
  972. skip_breakpoints->set_icon(get_icon("DebugSkipBreakpointsOff", "EditorIcons"));
  973. copy->set_icon(get_icon("ActionCopy", "EditorIcons"));
  974. step->set_icon(get_icon("DebugStep", "EditorIcons"));
  975. next->set_icon(get_icon("DebugNext", "EditorIcons"));
  976. back->set_icon(get_icon("Back", "EditorIcons"));
  977. forward->set_icon(get_icon("Forward", "EditorIcons"));
  978. dobreak->set_icon(get_icon("Pause", "EditorIcons"));
  979. docontinue->set_icon(get_icon("DebugContinue", "EditorIcons"));
  980. le_set->connect("pressed", this, "_live_edit_set");
  981. le_clear->connect("pressed", this, "_live_edit_clear");
  982. error_tree->connect("item_selected", this, "_error_selected");
  983. error_tree->connect("item_activated", this, "_error_activated");
  984. vmem_refresh->set_icon(get_icon("Reload", "EditorIcons"));
  985. reason->add_color_override("font_color", get_color("error_color", "Editor"));
  986. } break;
  987. case NOTIFICATION_PROCESS: {
  988. if (connection.is_valid()) {
  989. inspect_scene_tree_timeout -= get_process_delta_time();
  990. if (inspect_scene_tree_timeout < 0) {
  991. inspect_scene_tree_timeout = EditorSettings::get_singleton()->get("debugger/remote_scene_tree_refresh_interval");
  992. if (inspect_scene_tree->is_visible_in_tree()) {
  993. _scene_tree_request();
  994. }
  995. }
  996. inspect_edited_object_timeout -= get_process_delta_time();
  997. if (inspect_edited_object_timeout < 0) {
  998. inspect_edited_object_timeout = EditorSettings::get_singleton()->get("debugger/remote_inspect_refresh_interval");
  999. if (inspected_object_id) {
  1000. if (ScriptEditorDebuggerInspectedObject *obj = Object::cast_to<ScriptEditorDebuggerInspectedObject>(ObjectDB::get_instance(editor->get_editor_history()->get_current()))) {
  1001. if (obj->remote_object_id == inspected_object_id) {
  1002. //take the chance and re-inspect selected object
  1003. Array msg;
  1004. msg.push_back("inspect_object");
  1005. msg.push_back(inspected_object_id);
  1006. ppeer->put_var(msg);
  1007. }
  1008. }
  1009. }
  1010. }
  1011. if (camera_override == OVERRIDE_2D) {
  1012. CanvasItemEditor *editor = CanvasItemEditor::get_singleton();
  1013. Dictionary state = editor->get_state();
  1014. float zoom = state["zoom"];
  1015. Point2 offset = state["ofs"];
  1016. Transform2D transform;
  1017. transform.scale_basis(Size2(zoom, zoom));
  1018. transform.elements[2] = -offset * zoom;
  1019. Array msg;
  1020. msg.push_back("override_camera_2D:transform");
  1021. msg.push_back(transform);
  1022. ppeer->put_var(msg);
  1023. } else if (camera_override >= OVERRIDE_3D_1) {
  1024. int viewport_idx = camera_override - OVERRIDE_3D_1;
  1025. SpatialEditorViewport *viewport = SpatialEditor::get_singleton()->get_editor_viewport(viewport_idx);
  1026. Camera *const cam = viewport->get_camera();
  1027. Array msg;
  1028. msg.push_back("override_camera_3D:transform");
  1029. msg.push_back(cam->get_camera_transform());
  1030. if (cam->get_projection() == Camera::PROJECTION_ORTHOGONAL) {
  1031. msg.push_back(false);
  1032. msg.push_back(cam->get_size());
  1033. } else {
  1034. msg.push_back(true);
  1035. msg.push_back(cam->get_fov());
  1036. }
  1037. msg.push_back(cam->get_znear());
  1038. msg.push_back(cam->get_zfar());
  1039. ppeer->put_var(msg);
  1040. }
  1041. }
  1042. if (error_count != last_error_count || warning_count != last_warning_count) {
  1043. if (error_count == 0 && warning_count == 0) {
  1044. errors_tab->set_name(TTR("Errors"));
  1045. debugger_button->set_text(TTR("Debugger"));
  1046. debugger_button->set_icon(Ref<Texture>());
  1047. tabs->set_tab_icon(errors_tab->get_index(), Ref<Texture>());
  1048. } else {
  1049. errors_tab->set_name(TTR("Errors") + " (" + itos(error_count + warning_count) + ")");
  1050. debugger_button->set_text(TTR("Debugger") + " (" + itos(error_count + warning_count) + ")");
  1051. if (error_count == 0) {
  1052. debugger_button->set_icon(get_icon("Warning", "EditorIcons"));
  1053. tabs->set_tab_icon(errors_tab->get_index(), get_icon("Warning", "EditorIcons"));
  1054. } else {
  1055. debugger_button->set_icon(get_icon("Error", "EditorIcons"));
  1056. tabs->set_tab_icon(errors_tab->get_index(), get_icon("Error", "EditorIcons"));
  1057. }
  1058. }
  1059. last_error_count = error_count;
  1060. last_warning_count = warning_count;
  1061. }
  1062. if (server->is_connection_available()) {
  1063. if (connection.is_valid()) {
  1064. // We already have a valid connection. Disconnecting any new connecting client to prevent it from hanging.
  1065. // (If we don't keep a reference to the connection it will be destroyed and disconnect_from_host will be called internally)
  1066. server->take_connection();
  1067. } else {
  1068. // We just got the first connection.
  1069. connection = server->take_connection();
  1070. if (connection.is_null())
  1071. break;
  1072. EditorNode::get_log()->add_message("--- Debugging process started ---", EditorLog::MSG_TYPE_EDITOR);
  1073. ppeer->set_stream_peer(connection);
  1074. //EditorNode::get_singleton()->make_bottom_panel_item_visible(this);
  1075. //emit_signal("show_debugger",true);
  1076. dobreak->set_disabled(false);
  1077. tabs->set_current_tab(0);
  1078. _set_reason_text(TTR("Child process connected."), MESSAGE_SUCCESS);
  1079. profiler->clear();
  1080. inspect_scene_tree->clear();
  1081. le_set->set_disabled(true);
  1082. le_clear->set_disabled(false);
  1083. error_tree->clear();
  1084. error_count = 0;
  1085. warning_count = 0;
  1086. profiler_signature.clear();
  1087. //live_edit_root->set_text("/root");
  1088. EditorNode::get_singleton()->get_pause_button()->set_pressed(false);
  1089. EditorNode::get_singleton()->get_pause_button()->set_disabled(false);
  1090. update_live_edit_root();
  1091. if (profiler->is_profiling()) {
  1092. _profiler_activate(true);
  1093. }
  1094. if (network_profiler->is_profiling()) {
  1095. _network_profiler_activate(true);
  1096. }
  1097. }
  1098. }
  1099. if (connection.is_null())
  1100. break;
  1101. if (!connection->is_connected_to_host()) {
  1102. stop();
  1103. editor->notify_child_process_exited(); //somehow, exited
  1104. break;
  1105. };
  1106. if (ppeer->get_available_packet_count() <= 0) {
  1107. break;
  1108. };
  1109. const uint64_t until = OS::get_singleton()->get_ticks_msec() + 20;
  1110. while (ppeer->get_available_packet_count() > 0) {
  1111. if (pending_in_queue) {
  1112. int todo = MIN(ppeer->get_available_packet_count(), pending_in_queue);
  1113. for (int i = 0; i < todo; i++) {
  1114. Variant cmd;
  1115. Error ret = ppeer->get_var(cmd);
  1116. if (ret != OK) {
  1117. stop();
  1118. ERR_FAIL_COND(ret != OK);
  1119. }
  1120. message.push_back(cmd);
  1121. pending_in_queue--;
  1122. }
  1123. if (pending_in_queue == 0) {
  1124. _parse_message(message_type, message);
  1125. message.clear();
  1126. }
  1127. } else {
  1128. if (ppeer->get_available_packet_count() >= 2) {
  1129. Variant cmd;
  1130. Error ret = ppeer->get_var(cmd);
  1131. if (ret != OK) {
  1132. stop();
  1133. ERR_FAIL_COND(ret != OK);
  1134. }
  1135. if (cmd.get_type() != Variant::STRING) {
  1136. stop();
  1137. ERR_FAIL_COND(cmd.get_type() != Variant::STRING);
  1138. }
  1139. message_type = cmd;
  1140. ret = ppeer->get_var(cmd);
  1141. if (ret != OK) {
  1142. stop();
  1143. ERR_FAIL_COND(ret != OK);
  1144. }
  1145. if (cmd.get_type() != Variant::INT) {
  1146. stop();
  1147. ERR_FAIL_COND(cmd.get_type() != Variant::INT);
  1148. }
  1149. pending_in_queue = cmd;
  1150. if (pending_in_queue == 0) {
  1151. _parse_message(message_type, Array());
  1152. message.clear();
  1153. }
  1154. } else {
  1155. break;
  1156. }
  1157. }
  1158. if (OS::get_singleton()->get_ticks_msec() > until)
  1159. break;
  1160. }
  1161. } break;
  1162. case EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED: {
  1163. add_constant_override("margin_left", -EditorNode::get_singleton()->get_gui_base()->get_stylebox("BottomPanelDebuggerOverride", "EditorStyles")->get_margin(MARGIN_LEFT));
  1164. add_constant_override("margin_right", -EditorNode::get_singleton()->get_gui_base()->get_stylebox("BottomPanelDebuggerOverride", "EditorStyles")->get_margin(MARGIN_RIGHT));
  1165. tabs->add_style_override("panel", editor->get_gui_base()->get_stylebox("DebuggerPanel", "EditorStyles"));
  1166. tabs->add_style_override("tab_fg", editor->get_gui_base()->get_stylebox("DebuggerTabFG", "EditorStyles"));
  1167. tabs->add_style_override("tab_bg", editor->get_gui_base()->get_stylebox("DebuggerTabBG", "EditorStyles"));
  1168. copy->set_icon(get_icon("ActionCopy", "EditorIcons"));
  1169. step->set_icon(get_icon("DebugStep", "EditorIcons"));
  1170. next->set_icon(get_icon("DebugNext", "EditorIcons"));
  1171. back->set_icon(get_icon("Back", "EditorIcons"));
  1172. forward->set_icon(get_icon("Forward", "EditorIcons"));
  1173. dobreak->set_icon(get_icon("Pause", "EditorIcons"));
  1174. docontinue->set_icon(get_icon("DebugContinue", "EditorIcons"));
  1175. vmem_refresh->set_icon(get_icon("Reload", "EditorIcons"));
  1176. } break;
  1177. }
  1178. }
  1179. void ScriptEditorDebugger::_clear_execution() {
  1180. TreeItem *ti = stack_dump->get_selected();
  1181. if (!ti)
  1182. return;
  1183. Dictionary d = ti->get_metadata(0);
  1184. stack_script = ResourceLoader::load(d["file"]);
  1185. emit_signal("clear_execution", stack_script);
  1186. stack_script.unref();
  1187. }
  1188. void ScriptEditorDebugger::start() {
  1189. stop();
  1190. if (is_visible_in_tree()) {
  1191. EditorNode::get_singleton()->make_bottom_panel_item_visible(this);
  1192. }
  1193. perf_history.clear();
  1194. for (int i = 0; i < Performance::MONITOR_MAX; i++) {
  1195. perf_max.write[i] = 0;
  1196. }
  1197. int remote_port = (int)EditorSettings::get_singleton()->get("network/debug/remote_port");
  1198. if (server->listen(remote_port) != OK) {
  1199. EditorNode::get_log()->add_message(String("Error listening on port ") + itos(remote_port), EditorLog::MSG_TYPE_ERROR);
  1200. return;
  1201. }
  1202. EditorNode::get_singleton()->get_scene_tree_dock()->show_tab_buttons();
  1203. auto_switch_remote_scene_tree = (bool)EditorSettings::get_singleton()->get("debugger/auto_switch_to_remote_scene_tree");
  1204. if (auto_switch_remote_scene_tree) {
  1205. EditorNode::get_singleton()->get_scene_tree_dock()->show_remote_tree();
  1206. }
  1207. set_process(true);
  1208. breaked = false;
  1209. camera_override = OVERRIDE_NONE;
  1210. }
  1211. void ScriptEditorDebugger::pause() {
  1212. }
  1213. void ScriptEditorDebugger::unpause() {
  1214. }
  1215. void ScriptEditorDebugger::stop() {
  1216. set_process(false);
  1217. breaked = false;
  1218. _clear_execution();
  1219. server->stop();
  1220. _clear_remote_objects();
  1221. ppeer->set_stream_peer(Ref<StreamPeer>());
  1222. if (connection.is_valid()) {
  1223. EditorNode::get_log()->add_message("--- Debugging process stopped ---", EditorLog::MSG_TYPE_EDITOR);
  1224. connection.unref();
  1225. reason->set_text("");
  1226. reason->set_tooltip("");
  1227. }
  1228. pending_in_queue = 0;
  1229. message.clear();
  1230. node_path_cache.clear();
  1231. res_path_cache.clear();
  1232. profiler_signature.clear();
  1233. le_clear->set_disabled(false);
  1234. le_set->set_disabled(true);
  1235. profiler->set_enabled(true);
  1236. inspect_scene_tree->clear();
  1237. inspector->edit(NULL);
  1238. EditorNode::get_singleton()->get_pause_button()->set_pressed(false);
  1239. EditorNode::get_singleton()->get_pause_button()->set_disabled(true);
  1240. EditorNode::get_singleton()->get_scene_tree_dock()->hide_remote_tree();
  1241. EditorNode::get_singleton()->get_scene_tree_dock()->hide_tab_buttons();
  1242. if (hide_on_stop) {
  1243. if (is_visible_in_tree())
  1244. EditorNode::get_singleton()->hide_bottom_panel();
  1245. emit_signal("show_debugger", false);
  1246. }
  1247. }
  1248. void ScriptEditorDebugger::_profiler_activate(bool p_enable) {
  1249. if (!connection.is_valid())
  1250. return;
  1251. if (p_enable) {
  1252. profiler_signature.clear();
  1253. Array msg;
  1254. msg.push_back("start_profiling");
  1255. int max_funcs = EditorSettings::get_singleton()->get("debugger/profiler_frame_max_functions");
  1256. max_funcs = CLAMP(max_funcs, 16, 512);
  1257. msg.push_back(max_funcs);
  1258. ppeer->put_var(msg);
  1259. print_verbose("Starting profiling.");
  1260. } else {
  1261. Array msg;
  1262. msg.push_back("stop_profiling");
  1263. ppeer->put_var(msg);
  1264. print_verbose("Ending profiling.");
  1265. }
  1266. }
  1267. void ScriptEditorDebugger::_network_profiler_activate(bool p_enable) {
  1268. if (!connection.is_valid())
  1269. return;
  1270. if (p_enable) {
  1271. Array msg;
  1272. msg.push_back("start_network_profiling");
  1273. ppeer->put_var(msg);
  1274. print_verbose("Starting network profiling.");
  1275. } else {
  1276. Array msg;
  1277. msg.push_back("stop_network_profiling");
  1278. ppeer->put_var(msg);
  1279. print_verbose("Ending network profiling.");
  1280. }
  1281. }
  1282. void ScriptEditorDebugger::_profiler_seeked() {
  1283. if (!connection.is_valid() || !connection->is_connected_to_host())
  1284. return;
  1285. if (breaked)
  1286. return;
  1287. debug_break();
  1288. }
  1289. void ScriptEditorDebugger::_stack_dump_frame_selected() {
  1290. TreeItem *ti = stack_dump->get_selected();
  1291. if (!ti)
  1292. return;
  1293. Dictionary d = ti->get_metadata(0);
  1294. stack_script = ResourceLoader::load(d["file"]);
  1295. emit_signal("goto_script_line", stack_script, int(d["line"]) - 1);
  1296. emit_signal("set_execution", stack_script, int(d["line"]) - 1);
  1297. stack_script.unref();
  1298. if (connection.is_valid() && connection->is_connected_to_host()) {
  1299. Array msg;
  1300. msg.push_back("get_stack_frame_vars");
  1301. msg.push_back(d["frame"]);
  1302. ppeer->put_var(msg);
  1303. } else {
  1304. inspector->edit(NULL);
  1305. }
  1306. }
  1307. void ScriptEditorDebugger::_output_clear() {
  1308. //output->clear();
  1309. //output->push_color(Color(0,0,0));
  1310. }
  1311. void ScriptEditorDebugger::_export_csv() {
  1312. file_dialog->set_mode(EditorFileDialog::MODE_SAVE_FILE);
  1313. file_dialog_mode = SAVE_CSV;
  1314. file_dialog->popup_centered_ratio();
  1315. }
  1316. String ScriptEditorDebugger::get_var_value(const String &p_var) const {
  1317. if (!breaked)
  1318. return String();
  1319. return variables->get_var_value(p_var);
  1320. }
  1321. int ScriptEditorDebugger::_get_node_path_cache(const NodePath &p_path) {
  1322. const int *r = node_path_cache.getptr(p_path);
  1323. if (r)
  1324. return *r;
  1325. last_path_id++;
  1326. node_path_cache[p_path] = last_path_id;
  1327. Array msg;
  1328. msg.push_back("live_node_path");
  1329. msg.push_back(p_path);
  1330. msg.push_back(last_path_id);
  1331. ppeer->put_var(msg);
  1332. return last_path_id;
  1333. }
  1334. int ScriptEditorDebugger::_get_res_path_cache(const String &p_path) {
  1335. Map<String, int>::Element *E = res_path_cache.find(p_path);
  1336. if (E)
  1337. return E->get();
  1338. last_path_id++;
  1339. res_path_cache[p_path] = last_path_id;
  1340. Array msg;
  1341. msg.push_back("live_res_path");
  1342. msg.push_back(p_path);
  1343. msg.push_back(last_path_id);
  1344. ppeer->put_var(msg);
  1345. return last_path_id;
  1346. }
  1347. void ScriptEditorDebugger::_method_changed(Object *p_base, const StringName &p_name, VARIANT_ARG_DECLARE) {
  1348. if (!p_base || !live_debug || !connection.is_valid() || !editor->get_edited_scene())
  1349. return;
  1350. Node *node = Object::cast_to<Node>(p_base);
  1351. VARIANT_ARGPTRS
  1352. for (int i = 0; i < VARIANT_ARG_MAX; i++) {
  1353. //no pointers, sorry
  1354. if (argptr[i] && (argptr[i]->get_type() == Variant::OBJECT || argptr[i]->get_type() == Variant::_RID))
  1355. return;
  1356. }
  1357. if (node) {
  1358. NodePath path = editor->get_edited_scene()->get_path_to(node);
  1359. int pathid = _get_node_path_cache(path);
  1360. Array msg;
  1361. msg.push_back("live_node_call");
  1362. msg.push_back(pathid);
  1363. msg.push_back(p_name);
  1364. for (int i = 0; i < VARIANT_ARG_MAX; i++) {
  1365. //no pointers, sorry
  1366. msg.push_back(*argptr[i]);
  1367. }
  1368. ppeer->put_var(msg);
  1369. return;
  1370. }
  1371. Resource *res = Object::cast_to<Resource>(p_base);
  1372. if (res && res->get_path() != String()) {
  1373. String respath = res->get_path();
  1374. int pathid = _get_res_path_cache(respath);
  1375. Array msg;
  1376. msg.push_back("live_res_call");
  1377. msg.push_back(pathid);
  1378. msg.push_back(p_name);
  1379. for (int i = 0; i < VARIANT_ARG_MAX; i++) {
  1380. //no pointers, sorry
  1381. msg.push_back(*argptr[i]);
  1382. }
  1383. ppeer->put_var(msg);
  1384. return;
  1385. }
  1386. }
  1387. void ScriptEditorDebugger::_property_changed(Object *p_base, const StringName &p_property, const Variant &p_value) {
  1388. if (!p_base || !live_debug || !connection.is_valid() || !editor->get_edited_scene())
  1389. return;
  1390. Node *node = Object::cast_to<Node>(p_base);
  1391. if (node) {
  1392. NodePath path = editor->get_edited_scene()->get_path_to(node);
  1393. int pathid = _get_node_path_cache(path);
  1394. if (p_value.is_ref()) {
  1395. Ref<Resource> res = p_value;
  1396. if (res.is_valid() && res->get_path() != String()) {
  1397. Array msg;
  1398. msg.push_back("live_node_prop_res");
  1399. msg.push_back(pathid);
  1400. msg.push_back(p_property);
  1401. msg.push_back(res->get_path());
  1402. ppeer->put_var(msg);
  1403. }
  1404. } else {
  1405. Array msg;
  1406. msg.push_back("live_node_prop");
  1407. msg.push_back(pathid);
  1408. msg.push_back(p_property);
  1409. msg.push_back(p_value);
  1410. ppeer->put_var(msg);
  1411. }
  1412. return;
  1413. }
  1414. Resource *res = Object::cast_to<Resource>(p_base);
  1415. if (res && res->get_path() != String()) {
  1416. String respath = res->get_path();
  1417. int pathid = _get_res_path_cache(respath);
  1418. if (p_value.is_ref()) {
  1419. Ref<Resource> res2 = p_value;
  1420. if (res2.is_valid() && res2->get_path() != String()) {
  1421. Array msg;
  1422. msg.push_back("live_res_prop_res");
  1423. msg.push_back(pathid);
  1424. msg.push_back(p_property);
  1425. msg.push_back(res2->get_path());
  1426. ppeer->put_var(msg);
  1427. }
  1428. } else {
  1429. Array msg;
  1430. msg.push_back("live_res_prop");
  1431. msg.push_back(pathid);
  1432. msg.push_back(p_property);
  1433. msg.push_back(p_value);
  1434. ppeer->put_var(msg);
  1435. }
  1436. return;
  1437. }
  1438. }
  1439. void ScriptEditorDebugger::_method_changeds(void *p_ud, Object *p_base, const StringName &p_name, VARIANT_ARG_DECLARE) {
  1440. ScriptEditorDebugger *sed = (ScriptEditorDebugger *)p_ud;
  1441. sed->_method_changed(p_base, p_name, VARIANT_ARG_PASS);
  1442. }
  1443. void ScriptEditorDebugger::_property_changeds(void *p_ud, Object *p_base, const StringName &p_property, const Variant &p_value) {
  1444. ScriptEditorDebugger *sed = (ScriptEditorDebugger *)p_ud;
  1445. sed->_property_changed(p_base, p_property, p_value);
  1446. }
  1447. void ScriptEditorDebugger::set_live_debugging(bool p_enable) {
  1448. live_debug = p_enable;
  1449. }
  1450. void ScriptEditorDebugger::_live_edit_set() {
  1451. if (!connection.is_valid())
  1452. return;
  1453. TreeItem *ti = inspect_scene_tree->get_selected();
  1454. if (!ti)
  1455. return;
  1456. String path;
  1457. while (ti) {
  1458. String lp = ti->get_text(0);
  1459. path = "/" + lp + path;
  1460. ti = ti->get_parent();
  1461. }
  1462. NodePath np = path;
  1463. editor->get_editor_data().set_edited_scene_live_edit_root(np);
  1464. update_live_edit_root();
  1465. }
  1466. void ScriptEditorDebugger::_live_edit_clear() {
  1467. NodePath np = NodePath("/root");
  1468. editor->get_editor_data().set_edited_scene_live_edit_root(np);
  1469. update_live_edit_root();
  1470. }
  1471. void ScriptEditorDebugger::update_live_edit_root() {
  1472. NodePath np = editor->get_editor_data().get_edited_scene_live_edit_root();
  1473. if (connection.is_valid()) {
  1474. Array msg;
  1475. msg.push_back("live_set_root");
  1476. msg.push_back(np);
  1477. if (editor->get_edited_scene())
  1478. msg.push_back(editor->get_edited_scene()->get_filename());
  1479. else
  1480. msg.push_back("");
  1481. ppeer->put_var(msg);
  1482. }
  1483. live_edit_root->set_text(np);
  1484. }
  1485. void ScriptEditorDebugger::live_debug_create_node(const NodePath &p_parent, const String &p_type, const String &p_name) {
  1486. if (live_debug && connection.is_valid()) {
  1487. Array msg;
  1488. msg.push_back("live_create_node");
  1489. msg.push_back(p_parent);
  1490. msg.push_back(p_type);
  1491. msg.push_back(p_name);
  1492. ppeer->put_var(msg);
  1493. }
  1494. }
  1495. void ScriptEditorDebugger::live_debug_instance_node(const NodePath &p_parent, const String &p_path, const String &p_name) {
  1496. if (live_debug && connection.is_valid()) {
  1497. Array msg;
  1498. msg.push_back("live_instance_node");
  1499. msg.push_back(p_parent);
  1500. msg.push_back(p_path);
  1501. msg.push_back(p_name);
  1502. ppeer->put_var(msg);
  1503. }
  1504. }
  1505. void ScriptEditorDebugger::live_debug_remove_node(const NodePath &p_at) {
  1506. if (live_debug && connection.is_valid()) {
  1507. Array msg;
  1508. msg.push_back("live_remove_node");
  1509. msg.push_back(p_at);
  1510. ppeer->put_var(msg);
  1511. }
  1512. }
  1513. void ScriptEditorDebugger::live_debug_remove_and_keep_node(const NodePath &p_at, ObjectID p_keep_id) {
  1514. if (live_debug && connection.is_valid()) {
  1515. Array msg;
  1516. msg.push_back("live_remove_and_keep_node");
  1517. msg.push_back(p_at);
  1518. msg.push_back(p_keep_id);
  1519. ppeer->put_var(msg);
  1520. }
  1521. }
  1522. void ScriptEditorDebugger::live_debug_restore_node(ObjectID p_id, const NodePath &p_at, int p_at_pos) {
  1523. if (live_debug && connection.is_valid()) {
  1524. Array msg;
  1525. msg.push_back("live_restore_node");
  1526. msg.push_back(p_id);
  1527. msg.push_back(p_at);
  1528. msg.push_back(p_at_pos);
  1529. ppeer->put_var(msg);
  1530. }
  1531. }
  1532. void ScriptEditorDebugger::live_debug_duplicate_node(const NodePath &p_at, const String &p_new_name) {
  1533. if (live_debug && connection.is_valid()) {
  1534. Array msg;
  1535. msg.push_back("live_duplicate_node");
  1536. msg.push_back(p_at);
  1537. msg.push_back(p_new_name);
  1538. ppeer->put_var(msg);
  1539. }
  1540. }
  1541. void ScriptEditorDebugger::live_debug_reparent_node(const NodePath &p_at, const NodePath &p_new_place, const String &p_new_name, int p_at_pos) {
  1542. if (live_debug && connection.is_valid()) {
  1543. Array msg;
  1544. msg.push_back("live_reparent_node");
  1545. msg.push_back(p_at);
  1546. msg.push_back(p_new_place);
  1547. msg.push_back(p_new_name);
  1548. msg.push_back(p_at_pos);
  1549. ppeer->put_var(msg);
  1550. }
  1551. }
  1552. ScriptEditorDebugger::CameraOverride ScriptEditorDebugger::get_camera_override() const {
  1553. return camera_override;
  1554. }
  1555. void ScriptEditorDebugger::set_camera_override(CameraOverride p_override) {
  1556. if (p_override == OVERRIDE_2D && camera_override != OVERRIDE_2D) {
  1557. if (connection.is_valid()) {
  1558. Array msg;
  1559. msg.push_back("override_camera_2D:set");
  1560. msg.push_back(true);
  1561. ppeer->put_var(msg);
  1562. }
  1563. } else if (p_override != OVERRIDE_2D && camera_override == OVERRIDE_2D) {
  1564. if (connection.is_valid()) {
  1565. Array msg;
  1566. msg.push_back("override_camera_2D:set");
  1567. msg.push_back(false);
  1568. ppeer->put_var(msg);
  1569. }
  1570. } else if (p_override >= OVERRIDE_3D_1 && camera_override < OVERRIDE_3D_1) {
  1571. if (connection.is_valid()) {
  1572. Array msg;
  1573. msg.push_back("override_camera_3D:set");
  1574. msg.push_back(true);
  1575. ppeer->put_var(msg);
  1576. }
  1577. } else if (p_override < OVERRIDE_3D_1 && camera_override >= OVERRIDE_3D_1) {
  1578. if (connection.is_valid()) {
  1579. Array msg;
  1580. msg.push_back("override_camera_3D:set");
  1581. msg.push_back(false);
  1582. ppeer->put_var(msg);
  1583. }
  1584. }
  1585. camera_override = p_override;
  1586. }
  1587. void ScriptEditorDebugger::set_breakpoint(const String &p_path, int p_line, bool p_enabled) {
  1588. if (connection.is_valid()) {
  1589. Array msg;
  1590. msg.push_back("breakpoint");
  1591. msg.push_back(p_path);
  1592. msg.push_back(p_line);
  1593. msg.push_back(p_enabled);
  1594. ppeer->put_var(msg);
  1595. }
  1596. }
  1597. void ScriptEditorDebugger::reload_scripts() {
  1598. if (connection.is_valid()) {
  1599. Array msg;
  1600. msg.push_back("reload_scripts");
  1601. ppeer->put_var(msg);
  1602. }
  1603. }
  1604. bool ScriptEditorDebugger::is_skip_breakpoints() {
  1605. return skip_breakpoints_value;
  1606. }
  1607. void ScriptEditorDebugger::_error_activated() {
  1608. TreeItem *selected = error_tree->get_selected();
  1609. TreeItem *ci = selected->get_children();
  1610. if (ci) {
  1611. selected->set_collapsed(!selected->is_collapsed());
  1612. }
  1613. }
  1614. void ScriptEditorDebugger::_error_selected() {
  1615. TreeItem *selected = error_tree->get_selected();
  1616. Array meta = selected->get_metadata(0);
  1617. if (meta.size() == 0) {
  1618. return;
  1619. }
  1620. Ref<Script> s = ResourceLoader::load(meta[0]);
  1621. emit_signal("goto_script_line", s, int(meta[1]) - 1);
  1622. }
  1623. void ScriptEditorDebugger::_expand_errors_list() {
  1624. TreeItem *root = error_tree->get_root();
  1625. if (!root)
  1626. return;
  1627. TreeItem *item = root->get_children();
  1628. while (item) {
  1629. item->set_collapsed(false);
  1630. item = item->get_next();
  1631. }
  1632. }
  1633. void ScriptEditorDebugger::_collapse_errors_list() {
  1634. TreeItem *root = error_tree->get_root();
  1635. if (!root)
  1636. return;
  1637. TreeItem *item = root->get_children();
  1638. while (item) {
  1639. item->set_collapsed(true);
  1640. item = item->get_next();
  1641. }
  1642. }
  1643. void ScriptEditorDebugger::set_hide_on_stop(bool p_hide) {
  1644. hide_on_stop = p_hide;
  1645. }
  1646. bool ScriptEditorDebugger::get_debug_with_external_editor() const {
  1647. return enable_external_editor;
  1648. }
  1649. void ScriptEditorDebugger::set_debug_with_external_editor(bool p_enabled) {
  1650. enable_external_editor = p_enabled;
  1651. }
  1652. Ref<Script> ScriptEditorDebugger::get_dump_stack_script() const {
  1653. return stack_script;
  1654. }
  1655. void ScriptEditorDebugger::_paused() {
  1656. ERR_FAIL_COND(connection.is_null());
  1657. ERR_FAIL_COND(!connection->is_connected_to_host());
  1658. if (!breaked && EditorNode::get_singleton()->get_pause_button()->is_pressed()) {
  1659. debug_break();
  1660. }
  1661. if (breaked && !EditorNode::get_singleton()->get_pause_button()->is_pressed()) {
  1662. debug_continue();
  1663. }
  1664. }
  1665. void ScriptEditorDebugger::_set_remote_object(ObjectID p_id, ScriptEditorDebuggerInspectedObject *p_obj) {
  1666. if (remote_objects.has(p_id))
  1667. memdelete(remote_objects[p_id]);
  1668. remote_objects[p_id] = p_obj;
  1669. }
  1670. void ScriptEditorDebugger::_clear_remote_objects() {
  1671. for (Map<ObjectID, ScriptEditorDebuggerInspectedObject *>::Element *E = remote_objects.front(); E; E = E->next()) {
  1672. if (editor->get_editor_history()->get_current() == E->value()->get_instance_id()) {
  1673. editor->push_item(NULL);
  1674. }
  1675. memdelete(E->value());
  1676. }
  1677. remote_objects.clear();
  1678. }
  1679. void ScriptEditorDebugger::_clear_errors_list() {
  1680. error_tree->clear();
  1681. error_count = 0;
  1682. warning_count = 0;
  1683. _notification(NOTIFICATION_PROCESS);
  1684. }
  1685. // Right click on specific file(s) or folder(s).
  1686. void ScriptEditorDebugger::_error_tree_item_rmb_selected(const Vector2 &p_pos) {
  1687. item_menu->clear();
  1688. item_menu->set_size(Size2(1, 1));
  1689. if (error_tree->is_anything_selected()) {
  1690. item_menu->add_icon_item(get_icon("ActionCopy", "EditorIcons"), TTR("Copy Error"), ITEM_MENU_COPY_ERROR);
  1691. }
  1692. if (item_menu->get_item_count() > 0) {
  1693. item_menu->set_position(error_tree->get_global_position() + p_pos);
  1694. item_menu->popup();
  1695. }
  1696. }
  1697. void ScriptEditorDebugger::_item_menu_id_pressed(int p_option) {
  1698. switch (p_option) {
  1699. case ITEM_MENU_COPY_ERROR: {
  1700. TreeItem *ti = error_tree->get_selected();
  1701. while (ti->get_parent() != error_tree->get_root())
  1702. ti = ti->get_parent();
  1703. String type;
  1704. if (ti->get_icon(0) == get_icon("Warning", "EditorIcons")) {
  1705. type = "W ";
  1706. } else if (ti->get_icon(0) == get_icon("Error", "EditorIcons")) {
  1707. type = "E ";
  1708. }
  1709. String text = ti->get_text(0) + " ";
  1710. int rpad_len = text.length();
  1711. text = type + text + ti->get_text(1) + "\n";
  1712. TreeItem *ci = ti->get_children();
  1713. while (ci) {
  1714. text += " " + ci->get_text(0).rpad(rpad_len) + ci->get_text(1) + "\n";
  1715. ci = ci->get_next();
  1716. }
  1717. OS::get_singleton()->set_clipboard(text);
  1718. } break;
  1719. case ITEM_MENU_SAVE_REMOTE_NODE: {
  1720. file_dialog->set_access(EditorFileDialog::ACCESS_RESOURCES);
  1721. file_dialog->set_mode(EditorFileDialog::MODE_SAVE_FILE);
  1722. file_dialog_mode = SAVE_NODE;
  1723. List<String> extensions;
  1724. Ref<PackedScene> sd = memnew(PackedScene);
  1725. ResourceSaver::get_recognized_extensions(sd, &extensions);
  1726. file_dialog->clear_filters();
  1727. for (int i = 0; i < extensions.size(); i++) {
  1728. file_dialog->add_filter("*." + extensions[i] + " ; " + extensions[i].to_upper());
  1729. }
  1730. file_dialog->popup_centered_ratio();
  1731. } break;
  1732. case ITEM_MENU_COPY_NODE_PATH: {
  1733. TreeItem *ti = inspect_scene_tree->get_selected();
  1734. String text = ti->get_text(0);
  1735. if (ti->get_parent() == NULL) {
  1736. text = ".";
  1737. } else if (ti->get_parent()->get_parent() == NULL) {
  1738. text = ".";
  1739. } else {
  1740. while (ti->get_parent()->get_parent() != inspect_scene_tree->get_root()) {
  1741. ti = ti->get_parent();
  1742. text = ti->get_text(0) + "/" + text;
  1743. }
  1744. }
  1745. OS::get_singleton()->set_clipboard(text);
  1746. } break;
  1747. }
  1748. }
  1749. void ScriptEditorDebugger::_bind_methods() {
  1750. ClassDB::bind_method(D_METHOD("_stack_dump_frame_selected"), &ScriptEditorDebugger::_stack_dump_frame_selected);
  1751. ClassDB::bind_method(D_METHOD("debug_skip_breakpoints"), &ScriptEditorDebugger::debug_skip_breakpoints);
  1752. ClassDB::bind_method(D_METHOD("debug_copy"), &ScriptEditorDebugger::debug_copy);
  1753. ClassDB::bind_method(D_METHOD("debug_next"), &ScriptEditorDebugger::debug_next);
  1754. ClassDB::bind_method(D_METHOD("debug_step"), &ScriptEditorDebugger::debug_step);
  1755. ClassDB::bind_method(D_METHOD("debug_break"), &ScriptEditorDebugger::debug_break);
  1756. ClassDB::bind_method(D_METHOD("debug_continue"), &ScriptEditorDebugger::debug_continue);
  1757. ClassDB::bind_method(D_METHOD("_output_clear"), &ScriptEditorDebugger::_output_clear);
  1758. ClassDB::bind_method(D_METHOD("_export_csv"), &ScriptEditorDebugger::_export_csv);
  1759. ClassDB::bind_method(D_METHOD("_performance_draw"), &ScriptEditorDebugger::_performance_draw);
  1760. ClassDB::bind_method(D_METHOD("_performance_select"), &ScriptEditorDebugger::_performance_select);
  1761. ClassDB::bind_method(D_METHOD("_scene_tree_request"), &ScriptEditorDebugger::_scene_tree_request);
  1762. ClassDB::bind_method(D_METHOD("_video_mem_request"), &ScriptEditorDebugger::_video_mem_request);
  1763. ClassDB::bind_method(D_METHOD("_live_edit_set"), &ScriptEditorDebugger::_live_edit_set);
  1764. ClassDB::bind_method(D_METHOD("_live_edit_clear"), &ScriptEditorDebugger::_live_edit_clear);
  1765. ClassDB::bind_method(D_METHOD("_error_selected"), &ScriptEditorDebugger::_error_selected);
  1766. ClassDB::bind_method(D_METHOD("_error_activated"), &ScriptEditorDebugger::_error_activated);
  1767. ClassDB::bind_method(D_METHOD("_expand_errors_list"), &ScriptEditorDebugger::_expand_errors_list);
  1768. ClassDB::bind_method(D_METHOD("_collapse_errors_list"), &ScriptEditorDebugger::_collapse_errors_list);
  1769. ClassDB::bind_method(D_METHOD("_profiler_activate"), &ScriptEditorDebugger::_profiler_activate);
  1770. ClassDB::bind_method(D_METHOD("_network_profiler_activate"), &ScriptEditorDebugger::_network_profiler_activate);
  1771. ClassDB::bind_method(D_METHOD("_profiler_seeked"), &ScriptEditorDebugger::_profiler_seeked);
  1772. ClassDB::bind_method(D_METHOD("_clear_errors_list"), &ScriptEditorDebugger::_clear_errors_list);
  1773. ClassDB::bind_method(D_METHOD("_error_tree_item_rmb_selected"), &ScriptEditorDebugger::_error_tree_item_rmb_selected);
  1774. ClassDB::bind_method(D_METHOD("_item_menu_id_pressed"), &ScriptEditorDebugger::_item_menu_id_pressed);
  1775. ClassDB::bind_method(D_METHOD("_paused"), &ScriptEditorDebugger::_paused);
  1776. ClassDB::bind_method(D_METHOD("_scene_tree_selected"), &ScriptEditorDebugger::_scene_tree_selected);
  1777. ClassDB::bind_method(D_METHOD("_scene_tree_folded"), &ScriptEditorDebugger::_scene_tree_folded);
  1778. ClassDB::bind_method(D_METHOD("_scene_tree_rmb_selected"), &ScriptEditorDebugger::_scene_tree_rmb_selected);
  1779. ClassDB::bind_method(D_METHOD("_file_selected"), &ScriptEditorDebugger::_file_selected);
  1780. ClassDB::bind_method(D_METHOD("live_debug_create_node"), &ScriptEditorDebugger::live_debug_create_node);
  1781. ClassDB::bind_method(D_METHOD("live_debug_instance_node"), &ScriptEditorDebugger::live_debug_instance_node);
  1782. ClassDB::bind_method(D_METHOD("live_debug_remove_node"), &ScriptEditorDebugger::live_debug_remove_node);
  1783. ClassDB::bind_method(D_METHOD("live_debug_remove_and_keep_node"), &ScriptEditorDebugger::live_debug_remove_and_keep_node);
  1784. ClassDB::bind_method(D_METHOD("live_debug_restore_node"), &ScriptEditorDebugger::live_debug_restore_node);
  1785. ClassDB::bind_method(D_METHOD("live_debug_duplicate_node"), &ScriptEditorDebugger::live_debug_duplicate_node);
  1786. ClassDB::bind_method(D_METHOD("live_debug_reparent_node"), &ScriptEditorDebugger::live_debug_reparent_node);
  1787. ClassDB::bind_method(D_METHOD("_scene_tree_property_select_object"), &ScriptEditorDebugger::_scene_tree_property_select_object);
  1788. ClassDB::bind_method(D_METHOD("_scene_tree_property_value_edited"), &ScriptEditorDebugger::_scene_tree_property_value_edited);
  1789. ADD_SIGNAL(MethodInfo("goto_script_line"));
  1790. ADD_SIGNAL(MethodInfo("set_execution", PropertyInfo("script"), PropertyInfo(Variant::INT, "line")));
  1791. ADD_SIGNAL(MethodInfo("clear_execution", PropertyInfo("script")));
  1792. ADD_SIGNAL(MethodInfo("breaked", PropertyInfo(Variant::BOOL, "reallydid"), PropertyInfo(Variant::BOOL, "can_debug")));
  1793. ADD_SIGNAL(MethodInfo("show_debugger", PropertyInfo(Variant::BOOL, "reallydid")));
  1794. }
  1795. ScriptEditorDebugger::ScriptEditorDebugger(EditorNode *p_editor) {
  1796. add_constant_override("margin_left", -EditorNode::get_singleton()->get_gui_base()->get_stylebox("BottomPanelDebuggerOverride", "EditorStyles")->get_margin(MARGIN_LEFT));
  1797. add_constant_override("margin_right", -EditorNode::get_singleton()->get_gui_base()->get_stylebox("BottomPanelDebuggerOverride", "EditorStyles")->get_margin(MARGIN_RIGHT));
  1798. ppeer = Ref<PacketPeerStream>(memnew(PacketPeerStream));
  1799. ppeer->set_input_buffer_max_size((1024 * 1024 * 8) - 4); // 8 MiB should be enough, minus 4 bytes for separator.
  1800. editor = p_editor;
  1801. editor->get_inspector()->connect("object_id_selected", this, "_scene_tree_property_select_object");
  1802. tabs = memnew(TabContainer);
  1803. tabs->set_tab_align(TabContainer::ALIGN_LEFT);
  1804. tabs->add_style_override("panel", editor->get_gui_base()->get_stylebox("DebuggerPanel", "EditorStyles"));
  1805. tabs->add_style_override("tab_fg", editor->get_gui_base()->get_stylebox("DebuggerTabFG", "EditorStyles"));
  1806. tabs->add_style_override("tab_bg", editor->get_gui_base()->get_stylebox("DebuggerTabBG", "EditorStyles"));
  1807. add_child(tabs);
  1808. { //debugger
  1809. VBoxContainer *vbc = memnew(VBoxContainer);
  1810. vbc->set_name(TTR("Debugger"));
  1811. //tabs->add_child(vbc);
  1812. Control *dbg = vbc;
  1813. HBoxContainer *hbc = memnew(HBoxContainer);
  1814. vbc->add_child(hbc);
  1815. reason = memnew(Label);
  1816. reason->set_text("");
  1817. hbc->add_child(reason);
  1818. reason->set_h_size_flags(SIZE_EXPAND_FILL);
  1819. reason->set_autowrap(true);
  1820. reason->set_max_lines_visible(3);
  1821. reason->set_mouse_filter(Control::MOUSE_FILTER_PASS);
  1822. hbc->add_child(memnew(VSeparator));
  1823. skip_breakpoints = memnew(ToolButton);
  1824. hbc->add_child(skip_breakpoints);
  1825. skip_breakpoints->set_tooltip(TTR("Skip Breakpoints"));
  1826. skip_breakpoints->connect("pressed", this, "debug_skip_breakpoints");
  1827. hbc->add_child(memnew(VSeparator));
  1828. copy = memnew(ToolButton);
  1829. hbc->add_child(copy);
  1830. copy->set_tooltip(TTR("Copy Error"));
  1831. copy->connect("pressed", this, "debug_copy");
  1832. hbc->add_child(memnew(VSeparator));
  1833. step = memnew(ToolButton);
  1834. hbc->add_child(step);
  1835. step->set_tooltip(TTR("Step Into"));
  1836. step->set_shortcut(ED_GET_SHORTCUT("debugger/step_into"));
  1837. step->connect("pressed", this, "debug_step");
  1838. next = memnew(ToolButton);
  1839. hbc->add_child(next);
  1840. next->set_tooltip(TTR("Step Over"));
  1841. next->set_shortcut(ED_GET_SHORTCUT("debugger/step_over"));
  1842. next->connect("pressed", this, "debug_next");
  1843. hbc->add_child(memnew(VSeparator));
  1844. dobreak = memnew(ToolButton);
  1845. hbc->add_child(dobreak);
  1846. dobreak->set_tooltip(TTR("Break"));
  1847. dobreak->set_shortcut(ED_GET_SHORTCUT("debugger/break"));
  1848. dobreak->connect("pressed", this, "debug_break");
  1849. docontinue = memnew(ToolButton);
  1850. hbc->add_child(docontinue);
  1851. docontinue->set_tooltip(TTR("Continue"));
  1852. docontinue->set_shortcut(ED_GET_SHORTCUT("debugger/continue"));
  1853. docontinue->connect("pressed", this, "debug_continue");
  1854. back = memnew(Button);
  1855. hbc->add_child(back);
  1856. back->set_tooltip(TTR("Inspect Previous Instance"));
  1857. back->hide();
  1858. forward = memnew(Button);
  1859. hbc->add_child(forward);
  1860. forward->set_tooltip(TTR("Inspect Next Instance"));
  1861. forward->hide();
  1862. HSplitContainer *sc = memnew(HSplitContainer);
  1863. vbc->add_child(sc);
  1864. sc->set_v_size_flags(SIZE_EXPAND_FILL);
  1865. stack_dump = memnew(Tree);
  1866. stack_dump->set_allow_reselect(true);
  1867. stack_dump->set_columns(1);
  1868. stack_dump->set_column_titles_visible(true);
  1869. stack_dump->set_column_title(0, TTR("Stack Frames"));
  1870. stack_dump->set_h_size_flags(SIZE_EXPAND_FILL);
  1871. stack_dump->set_hide_root(true);
  1872. stack_dump->connect("cell_selected", this, "_stack_dump_frame_selected");
  1873. sc->add_child(stack_dump);
  1874. inspector = memnew(EditorInspector);
  1875. inspector->set_h_size_flags(SIZE_EXPAND_FILL);
  1876. inspector->set_enable_capitalize_paths(false);
  1877. inspector->set_read_only(true);
  1878. inspector->connect("object_id_selected", this, "_scene_tree_property_select_object");
  1879. sc->add_child(inspector);
  1880. server.instance();
  1881. pending_in_queue = 0;
  1882. variables = memnew(ScriptEditorDebuggerVariables);
  1883. breaked = false;
  1884. tabs->add_child(dbg);
  1885. }
  1886. { //errors
  1887. errors_tab = memnew(VBoxContainer);
  1888. errors_tab->set_name(TTR("Errors"));
  1889. HBoxContainer *errhb = memnew(HBoxContainer);
  1890. errors_tab->add_child(errhb);
  1891. Button *expand_all = memnew(Button);
  1892. expand_all->set_text(TTR("Expand All"));
  1893. expand_all->connect("pressed", this, "_expand_errors_list");
  1894. errhb->add_child(expand_all);
  1895. Button *collapse_all = memnew(Button);
  1896. collapse_all->set_text(TTR("Collapse All"));
  1897. collapse_all->connect("pressed", this, "_collapse_errors_list");
  1898. errhb->add_child(collapse_all);
  1899. Control *space = memnew(Control);
  1900. space->set_h_size_flags(SIZE_EXPAND_FILL);
  1901. errhb->add_child(space);
  1902. clearbutton = memnew(Button);
  1903. clearbutton->set_text(TTR("Clear"));
  1904. clearbutton->set_h_size_flags(0);
  1905. clearbutton->connect("pressed", this, "_clear_errors_list");
  1906. errhb->add_child(clearbutton);
  1907. error_tree = memnew(Tree);
  1908. error_tree->set_columns(2);
  1909. error_tree->set_column_expand(0, false);
  1910. error_tree->set_column_min_width(0, 140);
  1911. error_tree->set_column_expand(1, true);
  1912. error_tree->set_select_mode(Tree::SELECT_ROW);
  1913. error_tree->set_hide_root(true);
  1914. error_tree->set_v_size_flags(SIZE_EXPAND_FILL);
  1915. error_tree->set_allow_rmb_select(true);
  1916. error_tree->connect("item_rmb_selected", this, "_error_tree_item_rmb_selected");
  1917. errors_tab->add_child(error_tree);
  1918. item_menu = memnew(PopupMenu);
  1919. item_menu->connect("id_pressed", this, "_item_menu_id_pressed");
  1920. error_tree->add_child(item_menu);
  1921. tabs->add_child(errors_tab);
  1922. }
  1923. { // remote scene tree
  1924. inspect_scene_tree = memnew(Tree);
  1925. EditorNode::get_singleton()->get_scene_tree_dock()->add_remote_tree_editor(inspect_scene_tree);
  1926. EditorNode::get_singleton()->get_scene_tree_dock()->connect("remote_tree_selected", this, "_scene_tree_selected");
  1927. inspect_scene_tree->set_v_size_flags(SIZE_EXPAND_FILL);
  1928. inspect_scene_tree->connect("cell_selected", this, "_scene_tree_selected");
  1929. inspect_scene_tree->connect("item_collapsed", this, "_scene_tree_folded");
  1930. inspect_scene_tree->set_allow_rmb_select(true);
  1931. inspect_scene_tree->connect("item_rmb_selected", this, "_scene_tree_rmb_selected");
  1932. auto_switch_remote_scene_tree = EDITOR_DEF("debugger/auto_switch_to_remote_scene_tree", false);
  1933. inspect_scene_tree_timeout = EDITOR_DEF("debugger/remote_scene_tree_refresh_interval", 1.0);
  1934. inspect_edited_object_timeout = EDITOR_DEF("debugger/remote_inspect_refresh_interval", 0.2);
  1935. inspected_object_id = 0;
  1936. updating_scene_tree = false;
  1937. }
  1938. { // File dialog
  1939. file_dialog = memnew(EditorFileDialog);
  1940. file_dialog->connect("file_selected", this, "_file_selected");
  1941. add_child(file_dialog);
  1942. }
  1943. { //profiler
  1944. profiler = memnew(EditorProfiler);
  1945. profiler->set_name(TTR("Profiler"));
  1946. tabs->add_child(profiler);
  1947. profiler->connect("enable_profiling", this, "_profiler_activate");
  1948. profiler->connect("break_request", this, "_profiler_seeked");
  1949. }
  1950. { //network profiler
  1951. network_profiler = memnew(EditorNetworkProfiler);
  1952. network_profiler->set_name(TTR("Network Profiler"));
  1953. tabs->add_child(network_profiler);
  1954. network_profiler->connect("enable_profiling", this, "_network_profiler_activate");
  1955. }
  1956. { //monitors
  1957. HSplitContainer *hsp = memnew(HSplitContainer);
  1958. perf_monitors = memnew(Tree);
  1959. perf_monitors->set_columns(2);
  1960. perf_monitors->set_column_title(0, TTR("Monitor"));
  1961. perf_monitors->set_column_title(1, TTR("Value"));
  1962. perf_monitors->set_column_titles_visible(true);
  1963. perf_monitors->connect("item_edited", this, "_performance_select");
  1964. hsp->add_child(perf_monitors);
  1965. perf_draw = memnew(Control);
  1966. perf_draw->set_clip_contents(true);
  1967. perf_draw->connect("draw", this, "_performance_draw");
  1968. hsp->add_child(perf_draw);
  1969. hsp->set_name(TTR("Monitors"));
  1970. hsp->set_split_offset(340 * EDSCALE);
  1971. tabs->add_child(hsp);
  1972. perf_max.resize(Performance::MONITOR_MAX);
  1973. Map<String, TreeItem *> bases;
  1974. TreeItem *root = perf_monitors->create_item();
  1975. perf_monitors->set_hide_root(true);
  1976. for (int i = 0; i < Performance::MONITOR_MAX; i++) {
  1977. String n = Performance::get_singleton()->get_monitor_name(Performance::Monitor(i));
  1978. Performance::MonitorType mtype = Performance::get_singleton()->get_monitor_type(Performance::Monitor(i));
  1979. String base = n.get_slice("/", 0);
  1980. String name = n.get_slice("/", 1);
  1981. if (!bases.has(base)) {
  1982. TreeItem *b = perf_monitors->create_item(root);
  1983. b->set_text(0, base.capitalize());
  1984. b->set_editable(0, false);
  1985. b->set_selectable(0, false);
  1986. b->set_expand_right(0, true);
  1987. bases[base] = b;
  1988. }
  1989. TreeItem *it = perf_monitors->create_item(bases[base]);
  1990. it->set_metadata(1, mtype);
  1991. it->set_cell_mode(0, TreeItem::CELL_MODE_CHECK);
  1992. it->set_editable(0, true);
  1993. it->set_selectable(0, false);
  1994. it->set_selectable(1, false);
  1995. it->set_text(0, name.capitalize());
  1996. perf_items.push_back(it);
  1997. perf_max.write[i] = 0;
  1998. }
  1999. info_message = memnew(Label);
  2000. info_message->set_text(TTR("Pick one or more items from the list to display the graph."));
  2001. info_message->set_valign(Label::VALIGN_CENTER);
  2002. info_message->set_align(Label::ALIGN_CENTER);
  2003. info_message->set_autowrap(true);
  2004. info_message->set_custom_minimum_size(Size2(100 * EDSCALE, 0));
  2005. info_message->set_anchors_and_margins_preset(PRESET_WIDE, PRESET_MODE_KEEP_SIZE, 8 * EDSCALE);
  2006. perf_draw->add_child(info_message);
  2007. }
  2008. { //vmem inspect
  2009. VBoxContainer *vmem_vb = memnew(VBoxContainer);
  2010. HBoxContainer *vmem_hb = memnew(HBoxContainer);
  2011. Label *vmlb = memnew(Label(TTR("List of Video Memory Usage by Resource:") + " "));
  2012. vmlb->set_h_size_flags(SIZE_EXPAND_FILL);
  2013. vmem_hb->add_child(vmlb);
  2014. vmem_hb->add_child(memnew(Label(TTR("Total:") + " ")));
  2015. vmem_total = memnew(LineEdit);
  2016. vmem_total->set_editable(false);
  2017. vmem_total->set_custom_minimum_size(Size2(100, 0) * EDSCALE);
  2018. vmem_hb->add_child(vmem_total);
  2019. vmem_refresh = memnew(ToolButton);
  2020. vmem_hb->add_child(vmem_refresh);
  2021. vmem_vb->add_child(vmem_hb);
  2022. vmem_refresh->connect("pressed", this, "_video_mem_request");
  2023. VBoxContainer *vmmc = memnew(VBoxContainer);
  2024. vmem_tree = memnew(Tree);
  2025. vmem_tree->set_v_size_flags(SIZE_EXPAND_FILL);
  2026. vmem_tree->set_h_size_flags(SIZE_EXPAND_FILL);
  2027. vmmc->add_child(vmem_tree);
  2028. vmmc->set_v_size_flags(SIZE_EXPAND_FILL);
  2029. vmem_vb->add_child(vmmc);
  2030. vmem_vb->set_name(TTR("Video Mem"));
  2031. vmem_tree->set_columns(4);
  2032. vmem_tree->set_column_titles_visible(true);
  2033. vmem_tree->set_column_title(0, TTR("Resource Path"));
  2034. vmem_tree->set_column_expand(0, true);
  2035. vmem_tree->set_column_expand(1, false);
  2036. vmem_tree->set_column_title(1, TTR("Type"));
  2037. vmem_tree->set_column_min_width(1, 100);
  2038. vmem_tree->set_column_expand(2, false);
  2039. vmem_tree->set_column_title(2, TTR("Format"));
  2040. vmem_tree->set_column_min_width(2, 150);
  2041. vmem_tree->set_column_expand(3, false);
  2042. vmem_tree->set_column_title(3, TTR("Usage"));
  2043. vmem_tree->set_column_min_width(3, 80);
  2044. vmem_tree->set_hide_root(true);
  2045. tabs->add_child(vmem_vb);
  2046. }
  2047. { // misc
  2048. VBoxContainer *misc = memnew(VBoxContainer);
  2049. misc->set_name(TTR("Misc"));
  2050. tabs->add_child(misc);
  2051. GridContainer *info_left = memnew(GridContainer);
  2052. info_left->set_columns(2);
  2053. misc->add_child(info_left);
  2054. clicked_ctrl = memnew(LineEdit);
  2055. clicked_ctrl->set_h_size_flags(SIZE_EXPAND_FILL);
  2056. info_left->add_child(memnew(Label(TTR("Clicked Control:"))));
  2057. info_left->add_child(clicked_ctrl);
  2058. clicked_ctrl_type = memnew(LineEdit);
  2059. info_left->add_child(memnew(Label(TTR("Clicked Control Type:"))));
  2060. info_left->add_child(clicked_ctrl_type);
  2061. live_edit_root = memnew(LineEdit);
  2062. live_edit_root->set_h_size_flags(SIZE_EXPAND_FILL);
  2063. {
  2064. HBoxContainer *lehb = memnew(HBoxContainer);
  2065. Label *l = memnew(Label(TTR("Live Edit Root:")));
  2066. info_left->add_child(l);
  2067. lehb->add_child(live_edit_root);
  2068. le_set = memnew(Button(TTR("Set From Tree")));
  2069. lehb->add_child(le_set);
  2070. le_clear = memnew(Button(TTR("Clear")));
  2071. lehb->add_child(le_clear);
  2072. info_left->add_child(lehb);
  2073. le_set->set_disabled(true);
  2074. le_clear->set_disabled(true);
  2075. }
  2076. misc->add_child(memnew(VSeparator));
  2077. HBoxContainer *buttons = memnew(HBoxContainer);
  2078. export_csv = memnew(Button(TTR("Export measures as CSV")));
  2079. export_csv->connect("pressed", this, "_export_csv");
  2080. buttons->add_child(export_csv);
  2081. misc->add_child(buttons);
  2082. }
  2083. msgdialog = memnew(AcceptDialog);
  2084. add_child(msgdialog);
  2085. p_editor->get_undo_redo()->set_method_notify_callback(_method_changeds, this);
  2086. p_editor->get_undo_redo()->set_property_notify_callback(_property_changeds, this);
  2087. live_debug = true;
  2088. camera_override = OVERRIDE_NONE;
  2089. last_path_id = false;
  2090. error_count = 0;
  2091. warning_count = 0;
  2092. hide_on_stop = true;
  2093. enable_external_editor = false;
  2094. last_error_count = 0;
  2095. last_warning_count = 0;
  2096. EditorNode::get_singleton()->get_pause_button()->connect("pressed", this, "_paused");
  2097. }
  2098. ScriptEditorDebugger::~ScriptEditorDebugger() {
  2099. memdelete(variables);
  2100. ppeer->set_stream_peer(Ref<StreamPeer>());
  2101. server->stop();
  2102. _clear_remote_objects();
  2103. }