script_editor_debugger.cpp 79 KB

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