script_editor_debugger.cpp 64 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257
  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-2018 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2018 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_node.h"
  35. #include "editor_profiler.h"
  36. #include "editor_settings.h"
  37. #include "main/performance.h"
  38. #include "property_editor.h"
  39. #include "scene/gui/dialogs.h"
  40. #include "scene/gui/label.h"
  41. #include "scene/gui/line_edit.h"
  42. #include "scene/gui/margin_container.h"
  43. #include "scene/gui/rich_text_label.h"
  44. #include "scene/gui/separator.h"
  45. #include "scene/gui/split_container.h"
  46. #include "scene/gui/tab_container.h"
  47. #include "scene/gui/texture_button.h"
  48. #include "scene/gui/tree.h"
  49. #include "scene/resources/packed_scene.h"
  50. class ScriptEditorDebuggerVariables : public Object {
  51. GDCLASS(ScriptEditorDebuggerVariables, Object);
  52. List<PropertyInfo> props;
  53. Map<StringName, Variant> values;
  54. protected:
  55. bool _set(const StringName &p_name, const Variant &p_value) {
  56. return false;
  57. }
  58. bool _get(const StringName &p_name, Variant &r_ret) const {
  59. if (!values.has(p_name))
  60. return false;
  61. r_ret = values[p_name];
  62. return true;
  63. }
  64. void _get_property_list(List<PropertyInfo> *p_list) const {
  65. for (const List<PropertyInfo>::Element *E = props.front(); E; E = E->next())
  66. p_list->push_back(E->get());
  67. }
  68. public:
  69. void clear() {
  70. props.clear();
  71. values.clear();
  72. }
  73. String get_var_value(const String &p_var) const {
  74. for (Map<StringName, Variant>::Element *E = values.front(); E; E = E->next()) {
  75. String v = E->key().operator String().get_slice("/", 1);
  76. if (v == p_var)
  77. return E->get();
  78. }
  79. return "";
  80. }
  81. void add_property(const String &p_name, const Variant &p_value, const PropertyHint &p_hint, const String p_hint_string) {
  82. PropertyInfo pinfo;
  83. pinfo.name = p_name;
  84. pinfo.type = p_value.get_type();
  85. pinfo.hint = p_hint;
  86. pinfo.hint_string = p_hint_string;
  87. props.push_back(pinfo);
  88. values[p_name] = p_value;
  89. }
  90. void update() {
  91. _change_notify();
  92. }
  93. ScriptEditorDebuggerVariables() {
  94. }
  95. };
  96. class ScriptEditorDebuggerInspectedObject : public Object {
  97. GDCLASS(ScriptEditorDebuggerInspectedObject, Object);
  98. protected:
  99. bool _set(const StringName &p_name, const Variant &p_value) {
  100. if (!prop_values.has(p_name) || String(p_name).begins_with("Constants/"))
  101. return false;
  102. emit_signal("value_edited", p_name, p_value);
  103. prop_values[p_name] = p_value;
  104. return true;
  105. }
  106. bool _get(const StringName &p_name, Variant &r_ret) const {
  107. if (!prop_values.has(p_name))
  108. return false;
  109. r_ret = prop_values[p_name];
  110. return true;
  111. }
  112. void _get_property_list(List<PropertyInfo> *p_list) const {
  113. p_list->clear(); //sorry, no want category
  114. for (const List<PropertyInfo>::Element *E = prop_list.front(); E; E = E->next()) {
  115. p_list->push_back(E->get());
  116. }
  117. }
  118. static void _bind_methods() {
  119. ClassDB::bind_method(D_METHOD("get_title"), &ScriptEditorDebuggerInspectedObject::get_title);
  120. ClassDB::bind_method(D_METHOD("get_variant"), &ScriptEditorDebuggerInspectedObject::get_variant);
  121. ClassDB::bind_method(D_METHOD("clear"), &ScriptEditorDebuggerInspectedObject::clear);
  122. ClassDB::bind_method(D_METHOD("get_remote_object_id"), &ScriptEditorDebuggerInspectedObject::get_remote_object_id);
  123. ADD_SIGNAL(MethodInfo("value_edited"));
  124. }
  125. public:
  126. String type_name;
  127. ObjectID remote_object_id;
  128. List<PropertyInfo> prop_list;
  129. Map<StringName, Variant> prop_values;
  130. ObjectID get_remote_object_id() {
  131. return remote_object_id;
  132. }
  133. String get_title() {
  134. if (remote_object_id)
  135. return TTR("Remote ") + String(type_name) + ": " + itos(remote_object_id);
  136. else
  137. return "<null>";
  138. }
  139. Variant get_variant(const StringName &p_name) {
  140. Variant var;
  141. _get(p_name, var);
  142. return var;
  143. }
  144. void clear() {
  145. prop_list.clear();
  146. prop_values.clear();
  147. }
  148. void update() {
  149. _change_notify();
  150. }
  151. void update_single(const char *p_prop) {
  152. _change_notify(p_prop);
  153. }
  154. ScriptEditorDebuggerInspectedObject() {
  155. remote_object_id = 0;
  156. }
  157. };
  158. void ScriptEditorDebugger::debug_copy() {
  159. String msg = reason->get_text();
  160. if (msg == "") return;
  161. OS::get_singleton()->set_clipboard(msg);
  162. }
  163. void ScriptEditorDebugger::debug_next() {
  164. ERR_FAIL_COND(!breaked);
  165. ERR_FAIL_COND(connection.is_null());
  166. ERR_FAIL_COND(!connection->is_connected_to_host());
  167. Array msg;
  168. msg.push_back("next");
  169. ppeer->put_var(msg);
  170. stack_dump->clear();
  171. inspector->edit(NULL);
  172. }
  173. void ScriptEditorDebugger::debug_step() {
  174. ERR_FAIL_COND(!breaked);
  175. ERR_FAIL_COND(connection.is_null());
  176. ERR_FAIL_COND(!connection->is_connected_to_host());
  177. Array msg;
  178. msg.push_back("step");
  179. ppeer->put_var(msg);
  180. stack_dump->clear();
  181. inspector->edit(NULL);
  182. }
  183. void ScriptEditorDebugger::debug_break() {
  184. ERR_FAIL_COND(breaked);
  185. ERR_FAIL_COND(connection.is_null());
  186. ERR_FAIL_COND(!connection->is_connected_to_host());
  187. Array msg;
  188. msg.push_back("break");
  189. ppeer->put_var(msg);
  190. }
  191. void ScriptEditorDebugger::debug_continue() {
  192. ERR_FAIL_COND(!breaked);
  193. ERR_FAIL_COND(connection.is_null());
  194. ERR_FAIL_COND(!connection->is_connected_to_host());
  195. OS::get_singleton()->enable_for_stealing_focus(EditorNode::get_singleton()->get_child_process_id());
  196. Array msg;
  197. msg.push_back("continue");
  198. ppeer->put_var(msg);
  199. }
  200. void ScriptEditorDebugger::_scene_tree_folded(Object *obj) {
  201. if (updating_scene_tree) {
  202. return;
  203. }
  204. TreeItem *item = Object::cast_to<TreeItem>(obj);
  205. if (!item)
  206. return;
  207. ObjectID id = item->get_metadata(0);
  208. if (item->is_collapsed()) {
  209. unfold_cache.erase(id);
  210. } else {
  211. unfold_cache.insert(id);
  212. }
  213. }
  214. void ScriptEditorDebugger::_scene_tree_selected() {
  215. if (updating_scene_tree) {
  216. return;
  217. }
  218. TreeItem *item = inspect_scene_tree->get_selected();
  219. if (!item) {
  220. return;
  221. }
  222. inspected_object_id = item->get_metadata(0);
  223. Array msg;
  224. msg.push_back("inspect_object");
  225. msg.push_back(inspected_object_id);
  226. ppeer->put_var(msg);
  227. }
  228. void ScriptEditorDebugger::_scene_tree_rmb_selected(const Vector2 &p_position) {
  229. TreeItem *item = inspect_scene_tree->get_item_at_position(p_position);
  230. if (!item)
  231. return;
  232. item->select(0);
  233. item_menu->clear();
  234. item_menu->add_icon_item(get_icon("CreateNewSceneFrom", "EditorIcons"), TTR("Save Branch as Scene"), ITEM_MENU_SAVE_REMOTE_NODE);
  235. item_menu->set_global_position(get_global_mouse_position());
  236. item_menu->popup();
  237. }
  238. void ScriptEditorDebugger::_file_selected(const String &p_file) {
  239. if (file_dialog->get_mode() == EditorFileDialog::MODE_SAVE_FILE) {
  240. Array msg;
  241. msg.push_back("save_node");
  242. msg.push_back(inspected_object_id);
  243. msg.push_back(p_file);
  244. ppeer->put_var(msg);
  245. }
  246. }
  247. void ScriptEditorDebugger::_scene_tree_property_value_edited(const String &p_prop, const Variant &p_value) {
  248. Array msg;
  249. msg.push_back("set_object_property");
  250. msg.push_back(inspected_object_id);
  251. msg.push_back(p_prop);
  252. msg.push_back(p_value);
  253. ppeer->put_var(msg);
  254. inspect_edited_object_timeout = 0.7; //avoid annoyance, don't request soon after editing
  255. }
  256. void ScriptEditorDebugger::_scene_tree_property_select_object(ObjectID p_object) {
  257. inspected_object_id = p_object;
  258. Array msg;
  259. msg.push_back("inspect_object");
  260. msg.push_back(inspected_object_id);
  261. ppeer->put_var(msg);
  262. }
  263. void ScriptEditorDebugger::_scene_tree_request() {
  264. ERR_FAIL_COND(connection.is_null());
  265. ERR_FAIL_COND(!connection->is_connected_to_host());
  266. Array msg;
  267. msg.push_back("request_scene_tree");
  268. ppeer->put_var(msg);
  269. }
  270. void ScriptEditorDebugger::_video_mem_request() {
  271. ERR_FAIL_COND(connection.is_null());
  272. ERR_FAIL_COND(!connection->is_connected_to_host());
  273. Array msg;
  274. msg.push_back("request_video_mem");
  275. ppeer->put_var(msg);
  276. }
  277. Size2 ScriptEditorDebugger::get_minimum_size() const {
  278. Size2 ms = Control::get_minimum_size();
  279. ms.y = MAX(ms.y, 250);
  280. return ms;
  281. }
  282. void ScriptEditorDebugger::_parse_message(const String &p_msg, const Array &p_data) {
  283. if (p_msg == "debug_enter") {
  284. Array msg;
  285. msg.push_back("get_stack_dump");
  286. ppeer->put_var(msg);
  287. ERR_FAIL_COND(p_data.size() != 2);
  288. bool can_continue = p_data[0];
  289. String error = p_data[1];
  290. step->set_disabled(!can_continue);
  291. next->set_disabled(!can_continue);
  292. _set_reason_text(error, MESSAGE_ERROR);
  293. copy->set_disabled(false);
  294. breaked = true;
  295. dobreak->set_disabled(true);
  296. docontinue->set_disabled(false);
  297. emit_signal("breaked", true, can_continue);
  298. OS::get_singleton()->move_window_to_foreground();
  299. if (error != "") {
  300. tabs->set_current_tab(0);
  301. }
  302. profiler->set_enabled(false);
  303. EditorNode::get_singleton()->get_pause_button()->set_pressed(true);
  304. EditorNode::get_singleton()->make_bottom_panel_item_visible(this);
  305. _clear_remote_objects();
  306. } else if (p_msg == "debug_exit") {
  307. breaked = false;
  308. copy->set_disabled(true);
  309. step->set_disabled(true);
  310. next->set_disabled(true);
  311. reason->set_text("");
  312. reason->set_tooltip("");
  313. back->set_disabled(true);
  314. forward->set_disabled(true);
  315. dobreak->set_disabled(false);
  316. docontinue->set_disabled(true);
  317. emit_signal("breaked", false, false, Variant());
  318. profiler->set_enabled(true);
  319. profiler->disable_seeking();
  320. inspector->edit(NULL);
  321. EditorNode::get_singleton()->get_pause_button()->set_pressed(false);
  322. } else if (p_msg == "message:click_ctrl") {
  323. clicked_ctrl->set_text(p_data[0]);
  324. clicked_ctrl_type->set_text(p_data[1]);
  325. } else if (p_msg == "message:scene_tree") {
  326. inspect_scene_tree->clear();
  327. Map<int, TreeItem *> lv;
  328. updating_scene_tree = true;
  329. for (int i = 0; i < p_data.size(); i += 4) {
  330. TreeItem *p;
  331. int level = p_data[i];
  332. if (level == 0) {
  333. p = NULL;
  334. } else {
  335. ERR_CONTINUE(!lv.has(level - 1));
  336. p = lv[level - 1];
  337. }
  338. TreeItem *it = inspect_scene_tree->create_item(p);
  339. ObjectID id = ObjectID(p_data[i + 3]);
  340. it->set_text(0, p_data[i + 1]);
  341. Ref<Texture> icon = EditorNode::get_singleton()->get_class_icon(p_data[i + 2], "");
  342. if (icon.is_valid())
  343. it->set_icon(0, icon);
  344. it->set_metadata(0, id);
  345. if (id == inspected_object_id) {
  346. TreeItem *cti = it->get_parent(); //ensure selected is always uncollapsed
  347. while (cti) {
  348. cti->set_collapsed(false);
  349. cti = cti->get_parent();
  350. }
  351. it->select(0);
  352. }
  353. if (p) {
  354. if (!unfold_cache.has(id)) {
  355. it->set_collapsed(true);
  356. }
  357. } else {
  358. if (unfold_cache.has(id)) { //reverse for root
  359. it->set_collapsed(true);
  360. }
  361. }
  362. lv[level] = it;
  363. }
  364. updating_scene_tree = false;
  365. le_clear->set_disabled(false);
  366. le_set->set_disabled(false);
  367. } else if (p_msg == "message:inspect_object") {
  368. ScriptEditorDebuggerInspectedObject *debugObj = NULL;
  369. ObjectID id = p_data[0];
  370. String type = p_data[1];
  371. Array properties = p_data[2];
  372. bool is_new_object = false;
  373. if (remote_objects.has(id)) {
  374. debugObj = remote_objects[id];
  375. } else {
  376. debugObj = memnew(ScriptEditorDebuggerInspectedObject);
  377. debugObj->remote_object_id = id;
  378. debugObj->type_name = type;
  379. remote_objects[id] = debugObj;
  380. is_new_object = true;
  381. debugObj->connect("value_edited", this, "_scene_tree_property_value_edited");
  382. }
  383. for (int i = 0; i < properties.size(); i++) {
  384. Array prop = properties[i];
  385. if (prop.size() != 6)
  386. continue;
  387. PropertyInfo pinfo;
  388. pinfo.name = prop[0];
  389. pinfo.type = Variant::Type(int(prop[1]));
  390. pinfo.hint = PropertyHint(int(prop[2]));
  391. pinfo.hint_string = prop[3];
  392. pinfo.usage = PropertyUsageFlags(int(prop[4]));
  393. Variant var = prop[5];
  394. if (pinfo.type == Variant::OBJECT) {
  395. if (var.is_zero()) {
  396. var = RES();
  397. } else if (var.get_type() == Variant::STRING) {
  398. var = ResourceLoader::load(var);
  399. if (pinfo.hint_string == "Script")
  400. debugObj->set_script(var);
  401. } else if (var.get_type() == Variant::OBJECT) {
  402. if (((Object *)var)->is_class("EncodedObjectAsID")) {
  403. var = Object::cast_to<EncodedObjectAsID>(var)->get_object_id();
  404. pinfo.type = var.get_type();
  405. pinfo.hint = PROPERTY_HINT_OBJECT_ID;
  406. pinfo.hint_string = "Object";
  407. }
  408. }
  409. }
  410. if (is_new_object) {
  411. //don't update.. it's the same, instead refresh
  412. debugObj->prop_list.push_back(pinfo);
  413. }
  414. debugObj->prop_values[pinfo.name] = var;
  415. }
  416. if (editor->get_editor_history()->get_current() != debugObj->get_instance_id()) {
  417. editor->push_item(debugObj, "");
  418. } else {
  419. debugObj->update();
  420. }
  421. } else if (p_msg == "message:video_mem") {
  422. vmem_tree->clear();
  423. TreeItem *root = vmem_tree->create_item();
  424. int total = 0;
  425. for (int i = 0; i < p_data.size(); i += 4) {
  426. TreeItem *it = vmem_tree->create_item(root);
  427. String type = p_data[i + 1];
  428. int bytes = p_data[i + 3].operator int();
  429. it->set_text(0, p_data[i + 0]); //path
  430. it->set_text(1, type); //type
  431. it->set_text(2, p_data[i + 2]); //type
  432. it->set_text(3, String::humanize_size(bytes)); //type
  433. total += bytes;
  434. if (has_icon(type, "EditorIcons"))
  435. it->set_icon(0, get_icon(type, "EditorIcons"));
  436. }
  437. vmem_total->set_tooltip(TTR("Bytes:") + " " + itos(total));
  438. vmem_total->set_text(String::humanize_size(total));
  439. } else if (p_msg == "stack_dump") {
  440. stack_dump->clear();
  441. TreeItem *r = stack_dump->create_item();
  442. for (int i = 0; i < p_data.size(); i++) {
  443. Dictionary d = p_data[i];
  444. ERR_CONTINUE(!d.has("function"));
  445. ERR_CONTINUE(!d.has("file"));
  446. ERR_CONTINUE(!d.has("line"));
  447. ERR_CONTINUE(!d.has("id"));
  448. TreeItem *s = stack_dump->create_item(r);
  449. d["frame"] = i;
  450. s->set_metadata(0, d);
  451. //String line = itos(i)+" - "+String(d["file"])+":"+itos(d["line"])+" - at func: "+d["function"];
  452. String line = itos(i) + " - " + String(d["file"]) + ":" + itos(d["line"]);
  453. s->set_text(0, line);
  454. if (i == 0)
  455. s->select(0);
  456. }
  457. } else if (p_msg == "stack_frame_vars") {
  458. variables->clear();
  459. int ofs = 0;
  460. int mcount = p_data[ofs];
  461. ofs++;
  462. for (int i = 0; i < mcount; i++) {
  463. String n = p_data[ofs + i * 2 + 0];
  464. Variant v = p_data[ofs + i * 2 + 1];
  465. PropertyHint h = PROPERTY_HINT_NONE;
  466. String hs = String();
  467. if (v.get_type() == Variant::OBJECT) {
  468. v = Object::cast_to<EncodedObjectAsID>(v)->get_object_id();
  469. h = PROPERTY_HINT_OBJECT_ID;
  470. hs = "Object";
  471. }
  472. variables->add_property("Locals/" + n, v, h, hs);
  473. }
  474. ofs += mcount * 2;
  475. mcount = p_data[ofs];
  476. ofs++;
  477. for (int i = 0; i < mcount; i++) {
  478. String n = p_data[ofs + i * 2 + 0];
  479. Variant v = p_data[ofs + i * 2 + 1];
  480. PropertyHint h = PROPERTY_HINT_NONE;
  481. String hs = String();
  482. if (v.get_type() == Variant::OBJECT) {
  483. v = Object::cast_to<EncodedObjectAsID>(v)->get_object_id();
  484. h = PROPERTY_HINT_OBJECT_ID;
  485. hs = "Object";
  486. }
  487. variables->add_property("Members/" + n, v, h, hs);
  488. }
  489. ofs += mcount * 2;
  490. mcount = p_data[ofs];
  491. ofs++;
  492. for (int i = 0; i < mcount; i++) {
  493. String n = p_data[ofs + i * 2 + 0];
  494. Variant v = p_data[ofs + i * 2 + 1];
  495. PropertyHint h = PROPERTY_HINT_NONE;
  496. String hs = String();
  497. if (v.get_type() == Variant::OBJECT) {
  498. v = Object::cast_to<EncodedObjectAsID>(v)->get_object_id();
  499. h = PROPERTY_HINT_OBJECT_ID;
  500. hs = "Object";
  501. }
  502. variables->add_property("Globals/" + n, v, h, hs);
  503. }
  504. variables->update();
  505. inspector->edit(variables);
  506. } else if (p_msg == "output") {
  507. //OUT
  508. for (int i = 0; i < p_data.size(); i++) {
  509. String t = p_data[i];
  510. //LOG
  511. if (!EditorNode::get_log()->is_visible()) {
  512. if (EditorNode::get_singleton()->are_bottom_panels_hidden()) {
  513. if (EDITOR_GET("run/output/always_open_output_on_play")) {
  514. EditorNode::get_singleton()->make_bottom_panel_item_visible(EditorNode::get_log());
  515. }
  516. }
  517. }
  518. EditorNode::get_log()->add_message(t);
  519. }
  520. } else if (p_msg == "performance") {
  521. Array arr = p_data[0];
  522. Vector<float> p;
  523. p.resize(arr.size());
  524. for (int i = 0; i < arr.size(); i++) {
  525. p.write[i] = arr[i];
  526. if (i < perf_items.size()) {
  527. float v = p[i];
  528. String vs = rtos(v);
  529. String tt = vs;
  530. switch (Performance::MonitorType((int)perf_items[i]->get_metadata(1))) {
  531. case Performance::MONITOR_TYPE_MEMORY: {
  532. // for the time being, going above GBs is a bad sign.
  533. String unit = "B";
  534. if ((int)v > 1073741824) {
  535. unit = "GB";
  536. v /= 1073741824.0;
  537. } else if ((int)v > 1048576) {
  538. unit = "MB";
  539. v /= 1048576.0;
  540. } else if ((int)v > 1024) {
  541. unit = "KB";
  542. v /= 1024.0;
  543. }
  544. tt += " bytes";
  545. vs = String::num(v, 2) + " " + unit;
  546. } break;
  547. case Performance::MONITOR_TYPE_TIME: {
  548. tt += " seconds";
  549. vs += " s";
  550. } break;
  551. default: {
  552. tt += " " + perf_items[i]->get_text(0);
  553. } break;
  554. }
  555. perf_items[i]->set_text(1, vs);
  556. perf_items[i]->set_tooltip(1, tt);
  557. if (p[i] > perf_max[i])
  558. perf_max.write[i] = p[i];
  559. }
  560. }
  561. perf_history.push_front(p);
  562. perf_draw->update();
  563. } else if (p_msg == "error") {
  564. Array err = p_data[0];
  565. Array vals;
  566. vals.push_back(err[0]);
  567. vals.push_back(err[1]);
  568. vals.push_back(err[2]);
  569. vals.push_back(err[3]);
  570. bool warning = err[9];
  571. bool e;
  572. String time = String("%d:%02d:%02d:%04d").sprintf(vals, &e);
  573. String txt = err[8].is_zero() ? String(err[7]) : String(err[8]);
  574. TreeItem *r = error_tree->get_root();
  575. if (!r) {
  576. r = error_tree->create_item();
  577. }
  578. TreeItem *error = error_tree->create_item(r);
  579. error->set_collapsed(true);
  580. error->set_icon(0, get_icon(warning ? "Warning" : "Error", "EditorIcons"));
  581. error->set_text(0, time);
  582. error->set_text_align(0, TreeItem::ALIGN_LEFT);
  583. error->set_text(1, txt);
  584. String source(err[5]);
  585. bool source_is_project_file = source.begins_with("res://");
  586. if (source_is_project_file)
  587. txt = source.get_file() + ":" + String(err[6]);
  588. else
  589. txt = source + ":" + String(err[6]);
  590. String method = err[4];
  591. if (method.length() > 0)
  592. txt += " @ " + method + "()";
  593. TreeItem *c_info = error_tree->create_item(error);
  594. c_info->set_text(0, "<" + TTR(source_is_project_file ? "Source" : "C Source") + ">");
  595. c_info->set_text(1, txt);
  596. c_info->set_text_align(0, TreeItem::ALIGN_LEFT);
  597. if (source_is_project_file) {
  598. Array meta;
  599. meta.push_back(source);
  600. meta.push_back(err[6]);
  601. error->set_metadata(0, meta);
  602. c_info->set_metadata(0, meta);
  603. }
  604. int scc = p_data[1];
  605. for (int i = 0; i < scc; i += 3) {
  606. String script = p_data[2 + i];
  607. String method = p_data[3 + i];
  608. int line = p_data[4 + i];
  609. TreeItem *stack_trace = error_tree->create_item(error);
  610. Array meta;
  611. meta.push_back(script);
  612. meta.push_back(line);
  613. stack_trace->set_metadata(0, meta);
  614. if (i == 0) {
  615. stack_trace->set_text(0, "<" + TTR("Stack Trace") + ">");
  616. stack_trace->set_text_align(0, TreeItem::ALIGN_LEFT);
  617. error->set_metadata(0, meta);
  618. }
  619. stack_trace->set_text(1, script.get_file() + ":" + itos(line) + " @ " + method + "()");
  620. }
  621. if (warning)
  622. warning_count++;
  623. else
  624. error_count++;
  625. } else if (p_msg == "profile_sig") {
  626. //cache a signature
  627. profiler_signature[p_data[1]] = p_data[0];
  628. } else if (p_msg == "profile_frame" || p_msg == "profile_total") {
  629. EditorProfiler::Metric metric;
  630. metric.valid = true;
  631. metric.frame_number = p_data[0];
  632. metric.frame_time = p_data[1];
  633. metric.idle_time = p_data[2];
  634. metric.physics_time = p_data[3];
  635. metric.physics_frame_time = p_data[4];
  636. int frame_data_amount = p_data[6];
  637. int frame_function_amount = p_data[7];
  638. if (frame_data_amount) {
  639. EditorProfiler::Metric::Category frame_time;
  640. frame_time.signature = "category_frame_time";
  641. frame_time.name = "Frame Time";
  642. frame_time.total_time = metric.frame_time;
  643. EditorProfiler::Metric::Category::Item item;
  644. item.calls = 1;
  645. item.line = 0;
  646. item.name = "Physics Time";
  647. item.total = metric.physics_time;
  648. item.self = item.total;
  649. item.signature = "physics_time";
  650. frame_time.items.push_back(item);
  651. item.name = "Idle Time";
  652. item.total = metric.idle_time;
  653. item.self = item.total;
  654. item.signature = "idle_time";
  655. frame_time.items.push_back(item);
  656. item.name = "Physics Frame Time";
  657. item.total = metric.physics_frame_time;
  658. item.self = item.total;
  659. item.signature = "physics_frame_time";
  660. frame_time.items.push_back(item);
  661. metric.categories.push_back(frame_time);
  662. }
  663. int idx = 8;
  664. for (int i = 0; i < frame_data_amount; i++) {
  665. EditorProfiler::Metric::Category c;
  666. String name = p_data[idx++];
  667. Array values = p_data[idx++];
  668. c.name = name.capitalize();
  669. c.items.resize(values.size() / 2);
  670. c.total_time = 0;
  671. c.signature = "categ::" + name;
  672. for (int i = 0; i < values.size(); i += 2) {
  673. EditorProfiler::Metric::Category::Item item;
  674. item.calls = 1;
  675. item.line = 0;
  676. item.name = values[i];
  677. item.self = values[i + 1];
  678. item.total = item.self;
  679. item.signature = "categ::" + name + "::" + item.name;
  680. item.name = item.name.capitalize();
  681. c.total_time += item.total;
  682. c.items.write[i / 2] = item;
  683. }
  684. metric.categories.push_back(c);
  685. }
  686. EditorProfiler::Metric::Category funcs;
  687. funcs.total_time = p_data[5]; //script time
  688. funcs.items.resize(frame_function_amount);
  689. funcs.name = "Script Functions";
  690. funcs.signature = "script_functions";
  691. for (int i = 0; i < frame_function_amount; i++) {
  692. int signature = p_data[idx++];
  693. int calls = p_data[idx++];
  694. float total = p_data[idx++];
  695. float self = p_data[idx++];
  696. EditorProfiler::Metric::Category::Item item;
  697. if (profiler_signature.has(signature)) {
  698. item.signature = profiler_signature[signature];
  699. String name = profiler_signature[signature];
  700. Vector<String> strings = name.split("::");
  701. if (strings.size() == 3) {
  702. item.name = strings[2];
  703. item.script = strings[0];
  704. item.line = strings[1].to_int();
  705. }
  706. } else {
  707. item.name = "SigErr " + itos(signature);
  708. }
  709. item.calls = calls;
  710. item.self = self;
  711. item.total = total;
  712. funcs.items.write[i] = item;
  713. }
  714. metric.categories.push_back(funcs);
  715. if (p_msg == "profile_frame")
  716. profiler->add_frame_metric(metric, false);
  717. else
  718. profiler->add_frame_metric(metric, true);
  719. } else if (p_msg == "kill_me") {
  720. editor->call_deferred("stop_child_process");
  721. }
  722. }
  723. void ScriptEditorDebugger::_set_reason_text(const String &p_reason, MessageType p_type) {
  724. switch (p_type) {
  725. case MESSAGE_ERROR:
  726. reason->add_color_override("font_color", get_color("error_color", "Editor"));
  727. break;
  728. case MESSAGE_WARNING:
  729. reason->add_color_override("font_color", get_color("warning_color", "Editor"));
  730. break;
  731. default:
  732. reason->add_color_override("font_color", get_color("success_color", "Editor"));
  733. }
  734. reason->set_text(p_reason);
  735. reason->set_tooltip(p_reason.word_wrap(80));
  736. }
  737. void ScriptEditorDebugger::_performance_select() {
  738. perf_draw->update();
  739. }
  740. void ScriptEditorDebugger::_performance_draw() {
  741. Vector<int> which;
  742. for (int i = 0; i < perf_items.size(); i++) {
  743. if (perf_items[i]->is_checked(0))
  744. which.push_back(i);
  745. }
  746. Ref<Font> graph_font = get_font("font", "TextEdit");
  747. if (which.empty()) {
  748. perf_draw->draw_string(graph_font, Point2(0, graph_font->get_ascent()), TTR("Pick one or more items from the list to display the graph."), get_color("font_color", "Label"), perf_draw->get_size().x);
  749. return;
  750. }
  751. Ref<StyleBox> graph_sb = get_stylebox("normal", "TextEdit");
  752. int cols = Math::ceil(Math::sqrt((float)which.size()));
  753. int rows = Math::ceil((float)which.size() / cols);
  754. if (which.size() == 1)
  755. rows = 1;
  756. int margin = 3;
  757. int point_sep = 5;
  758. Size2i s = Size2i(perf_draw->get_size()) / Size2i(cols, rows);
  759. for (int i = 0; i < which.size(); i++) {
  760. Point2i p(i % cols, i / cols);
  761. Rect2i r(p * s, s);
  762. r.position += Point2(margin, margin);
  763. r.size -= Point2(margin, margin) * 2.0;
  764. perf_draw->draw_style_box(graph_sb, r);
  765. r.position += graph_sb->get_offset();
  766. r.size -= graph_sb->get_minimum_size();
  767. int pi = which[i];
  768. Color c = get_color("accent_color", "Editor");
  769. float h = (float)which[i] / (float)(perf_items.size());
  770. c.set_hsv(Math::fmod(h + 0.4, 0.9), c.get_s() * 0.9, c.get_v() * 1.4);
  771. c.a = 0.6;
  772. perf_draw->draw_string(graph_font, r.position + Point2(0, graph_font->get_ascent()), perf_items[pi]->get_text(0), c, r.size.x);
  773. c.a = 0.9;
  774. 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);
  775. float spacing = point_sep / float(cols);
  776. float from = r.size.width;
  777. List<Vector<float> >::Element *E = perf_history.front();
  778. float prev = -1;
  779. while (from >= 0 && E) {
  780. float m = perf_max[pi];
  781. if (m == 0)
  782. m = 0.00001;
  783. float h = E->get()[pi] / m;
  784. h = (1.0 - h) * r.size.y;
  785. c.a = 0.7;
  786. if (E != perf_history.front())
  787. perf_draw->draw_line(r.position + Point2(from, h), r.position + Point2(from + spacing, prev), c, 2.0);
  788. prev = h;
  789. E = E->next();
  790. from -= spacing;
  791. }
  792. }
  793. }
  794. void ScriptEditorDebugger::_notification(int p_what) {
  795. switch (p_what) {
  796. case NOTIFICATION_ENTER_TREE: {
  797. inspector->edit(variables);
  798. copy->set_icon(get_icon("ActionCopy", "EditorIcons"));
  799. step->set_icon(get_icon("DebugStep", "EditorIcons"));
  800. next->set_icon(get_icon("DebugNext", "EditorIcons"));
  801. back->set_icon(get_icon("Back", "EditorIcons"));
  802. forward->set_icon(get_icon("Forward", "EditorIcons"));
  803. dobreak->set_icon(get_icon("Pause", "EditorIcons"));
  804. docontinue->set_icon(get_icon("DebugContinue", "EditorIcons"));
  805. //scene_tree_refresh->set_icon( get_icon("Reload","EditorIcons"));
  806. le_set->connect("pressed", this, "_live_edit_set");
  807. le_clear->connect("pressed", this, "_live_edit_clear");
  808. error_tree->connect("item_selected", this, "_error_selected");
  809. error_tree->connect("item_activated", this, "_error_activated");
  810. vmem_refresh->set_icon(get_icon("Reload", "EditorIcons"));
  811. reason->add_color_override("font_color", get_color("error_color", "Editor"));
  812. bool enable_rl = EditorSettings::get_singleton()->get("docks/scene_tree/draw_relationship_lines");
  813. Color rl_color = EditorSettings::get_singleton()->get("docks/scene_tree/relationship_line_color");
  814. if (enable_rl) {
  815. inspect_scene_tree->add_constant_override("draw_relationship_lines", 1);
  816. inspect_scene_tree->add_color_override("relationship_line_color", rl_color);
  817. } else
  818. inspect_scene_tree->add_constant_override("draw_relationship_lines", 0);
  819. } break;
  820. case NOTIFICATION_PROCESS: {
  821. if (connection.is_valid()) {
  822. inspect_scene_tree_timeout -= get_process_delta_time();
  823. if (inspect_scene_tree_timeout < 0) {
  824. inspect_scene_tree_timeout = EditorSettings::get_singleton()->get("debugger/remote_scene_tree_refresh_interval");
  825. if (inspect_scene_tree->is_visible_in_tree()) {
  826. _scene_tree_request();
  827. }
  828. }
  829. inspect_edited_object_timeout -= get_process_delta_time();
  830. if (inspect_edited_object_timeout < 0) {
  831. inspect_edited_object_timeout = EditorSettings::get_singleton()->get("debugger/remote_inspect_refresh_interval");
  832. if (inspected_object_id) {
  833. if (ScriptEditorDebuggerInspectedObject *obj = Object::cast_to<ScriptEditorDebuggerInspectedObject>(ObjectDB::get_instance(editor->get_editor_history()->get_current()))) {
  834. if (obj->remote_object_id == inspected_object_id) {
  835. //take the chance and re-inspect selected object
  836. Array msg;
  837. msg.push_back("inspect_object");
  838. msg.push_back(inspected_object_id);
  839. ppeer->put_var(msg);
  840. }
  841. }
  842. }
  843. }
  844. }
  845. if (error_count != last_error_count || warning_count != last_warning_count) {
  846. if (error_count == 0 && warning_count == 0) {
  847. error_tree->set_name(TTR("Errors"));
  848. debugger_button->set_text(TTR("Debugger"));
  849. debugger_button->set_icon(Ref<Texture>());
  850. tabs->set_tab_icon(error_tree->get_index(), Ref<Texture>());
  851. } else {
  852. error_tree->set_name(TTR("Errors") + " (" + itos(error_count + warning_count) + ")");
  853. debugger_button->set_text(TTR("Debugger") + " (" + itos(error_count + warning_count) + ")");
  854. if (error_count == 0) {
  855. debugger_button->set_icon(get_icon("Warning", "EditorIcons"));
  856. tabs->set_tab_icon(error_tree->get_index(), get_icon("Warning", "EditorIcons"));
  857. } else {
  858. debugger_button->set_icon(get_icon("Error", "EditorIcons"));
  859. tabs->set_tab_icon(error_tree->get_index(), get_icon("Error", "EditorIcons"));
  860. }
  861. }
  862. last_error_count = error_count;
  863. last_warning_count = warning_count;
  864. }
  865. if (connection.is_null()) {
  866. if (server->is_connection_available()) {
  867. connection = server->take_connection();
  868. if (connection.is_null())
  869. break;
  870. EditorNode::get_log()->add_message("** Debug Process Started **");
  871. ppeer->set_stream_peer(connection);
  872. //EditorNode::get_singleton()->make_bottom_panel_item_visible(this);
  873. //emit_signal("show_debugger",true);
  874. dobreak->set_disabled(false);
  875. tabs->set_current_tab(0);
  876. _set_reason_text(TTR("Child Process Connected"), MESSAGE_SUCCESS);
  877. profiler->clear();
  878. inspect_scene_tree->clear();
  879. le_set->set_disabled(true);
  880. le_clear->set_disabled(false);
  881. error_tree->clear();
  882. error_count = 0;
  883. warning_count = 0;
  884. profiler_signature.clear();
  885. //live_edit_root->set_text("/root");
  886. EditorNode::get_singleton()->get_pause_button()->set_pressed(false);
  887. EditorNode::get_singleton()->get_pause_button()->set_disabled(false);
  888. update_live_edit_root();
  889. if (profiler->is_profiling()) {
  890. _profiler_activate(true);
  891. }
  892. } else {
  893. break;
  894. }
  895. };
  896. if (!connection->is_connected_to_host()) {
  897. stop();
  898. editor->notify_child_process_exited(); //somehow, exited
  899. break;
  900. };
  901. if (ppeer->get_available_packet_count() <= 0) {
  902. break;
  903. };
  904. const uint64_t until = OS::get_singleton()->get_ticks_msec() + 20;
  905. while (ppeer->get_available_packet_count() > 0) {
  906. if (pending_in_queue) {
  907. int todo = MIN(ppeer->get_available_packet_count(), pending_in_queue);
  908. for (int i = 0; i < todo; i++) {
  909. Variant cmd;
  910. Error ret = ppeer->get_var(cmd);
  911. if (ret != OK) {
  912. stop();
  913. ERR_FAIL_COND(ret != OK);
  914. }
  915. message.push_back(cmd);
  916. pending_in_queue--;
  917. }
  918. if (pending_in_queue == 0) {
  919. _parse_message(message_type, message);
  920. message.clear();
  921. }
  922. } else {
  923. if (ppeer->get_available_packet_count() >= 2) {
  924. Variant cmd;
  925. Error ret = ppeer->get_var(cmd);
  926. if (ret != OK) {
  927. stop();
  928. ERR_FAIL_COND(ret != OK);
  929. }
  930. if (cmd.get_type() != Variant::STRING) {
  931. stop();
  932. ERR_FAIL_COND(cmd.get_type() != Variant::STRING);
  933. }
  934. message_type = cmd;
  935. ret = ppeer->get_var(cmd);
  936. if (ret != OK) {
  937. stop();
  938. ERR_FAIL_COND(ret != OK);
  939. }
  940. if (cmd.get_type() != Variant::INT) {
  941. stop();
  942. ERR_FAIL_COND(cmd.get_type() != Variant::INT);
  943. }
  944. pending_in_queue = cmd;
  945. if (pending_in_queue == 0) {
  946. _parse_message(message_type, Array());
  947. message.clear();
  948. }
  949. } else {
  950. break;
  951. }
  952. }
  953. if (OS::get_singleton()->get_ticks_msec() > until)
  954. break;
  955. }
  956. } break;
  957. case EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED: {
  958. tabs->add_style_override("panel", editor->get_gui_base()->get_stylebox("DebuggerPanel", "EditorStyles"));
  959. tabs->add_style_override("tab_fg", editor->get_gui_base()->get_stylebox("DebuggerTabFG", "EditorStyles"));
  960. tabs->add_style_override("tab_bg", editor->get_gui_base()->get_stylebox("DebuggerTabBG", "EditorStyles"));
  961. tabs->set_margin(MARGIN_LEFT, -EditorNode::get_singleton()->get_gui_base()->get_stylebox("BottomPanelDebuggerOverride", "EditorStyles")->get_margin(MARGIN_LEFT));
  962. tabs->set_margin(MARGIN_RIGHT, EditorNode::get_singleton()->get_gui_base()->get_stylebox("BottomPanelDebuggerOverride", "EditorStyles")->get_margin(MARGIN_RIGHT));
  963. bool enable_rl = EditorSettings::get_singleton()->get("docks/scene_tree/draw_relationship_lines");
  964. Color rl_color = EditorSettings::get_singleton()->get("docks/scene_tree/relationship_line_color");
  965. if (enable_rl) {
  966. inspect_scene_tree->add_constant_override("draw_relationship_lines", 1);
  967. inspect_scene_tree->add_color_override("relationship_line_color", rl_color);
  968. } else
  969. inspect_scene_tree->add_constant_override("draw_relationship_lines", 0);
  970. copy->set_icon(get_icon("ActionCopy", "EditorIcons"));
  971. step->set_icon(get_icon("DebugStep", "EditorIcons"));
  972. next->set_icon(get_icon("DebugNext", "EditorIcons"));
  973. back->set_icon(get_icon("Back", "EditorIcons"));
  974. forward->set_icon(get_icon("Forward", "EditorIcons"));
  975. dobreak->set_icon(get_icon("Pause", "EditorIcons"));
  976. docontinue->set_icon(get_icon("DebugContinue", "EditorIcons"));
  977. vmem_refresh->set_icon(get_icon("Reload", "EditorIcons"));
  978. } break;
  979. }
  980. }
  981. void ScriptEditorDebugger::start() {
  982. stop();
  983. if (is_visible_in_tree()) {
  984. EditorNode::get_singleton()->make_bottom_panel_item_visible(this);
  985. }
  986. perf_history.clear();
  987. for (int i = 0; i < Performance::MONITOR_MAX; i++) {
  988. perf_max.write[i] = 0;
  989. }
  990. int remote_port = (int)EditorSettings::get_singleton()->get("network/debug/remote_port");
  991. if (server->listen(remote_port) != OK) {
  992. EditorNode::get_log()->add_message(String("Error listening on port ") + itos(remote_port), EditorLog::MSG_TYPE_ERROR);
  993. return;
  994. }
  995. EditorNode::get_singleton()->get_scene_tree_dock()->show_tab_buttons();
  996. auto_switch_remote_scene_tree = (bool)EditorSettings::get_singleton()->get("debugger/auto_switch_to_remote_scene_tree");
  997. if (auto_switch_remote_scene_tree) {
  998. EditorNode::get_singleton()->get_scene_tree_dock()->show_remote_tree();
  999. }
  1000. set_process(true);
  1001. breaked = false;
  1002. }
  1003. void ScriptEditorDebugger::pause() {
  1004. }
  1005. void ScriptEditorDebugger::unpause() {
  1006. }
  1007. void ScriptEditorDebugger::stop() {
  1008. set_process(false);
  1009. breaked = false;
  1010. server->stop();
  1011. ppeer->set_stream_peer(Ref<StreamPeer>());
  1012. if (connection.is_valid()) {
  1013. EditorNode::get_log()->add_message("** Debug Process Stopped **");
  1014. connection.unref();
  1015. reason->set_text("");
  1016. reason->set_tooltip("");
  1017. }
  1018. pending_in_queue = 0;
  1019. message.clear();
  1020. node_path_cache.clear();
  1021. res_path_cache.clear();
  1022. profiler_signature.clear();
  1023. le_clear->set_disabled(false);
  1024. le_set->set_disabled(true);
  1025. profiler->set_enabled(true);
  1026. inspect_scene_tree->clear();
  1027. EditorNode::get_singleton()->get_pause_button()->set_pressed(false);
  1028. EditorNode::get_singleton()->get_pause_button()->set_disabled(true);
  1029. EditorNode::get_singleton()->get_scene_tree_dock()->hide_remote_tree();
  1030. EditorNode::get_singleton()->get_scene_tree_dock()->hide_tab_buttons();
  1031. if (hide_on_stop) {
  1032. if (is_visible_in_tree())
  1033. EditorNode::get_singleton()->hide_bottom_panel();
  1034. emit_signal("show_debugger", false);
  1035. }
  1036. }
  1037. void ScriptEditorDebugger::_profiler_activate(bool p_enable) {
  1038. if (!connection.is_valid())
  1039. return;
  1040. if (p_enable) {
  1041. profiler_signature.clear();
  1042. Array msg;
  1043. msg.push_back("start_profiling");
  1044. int max_funcs = EditorSettings::get_singleton()->get("debugger/profiler_frame_max_functions");
  1045. max_funcs = CLAMP(max_funcs, 16, 512);
  1046. msg.push_back(max_funcs);
  1047. ppeer->put_var(msg);
  1048. print_verbose("Starting profiling.");
  1049. } else {
  1050. Array msg;
  1051. msg.push_back("stop_profiling");
  1052. ppeer->put_var(msg);
  1053. print_verbose("Ending profiling.");
  1054. }
  1055. }
  1056. void ScriptEditorDebugger::_profiler_seeked() {
  1057. if (!connection.is_valid() || !connection->is_connected_to_host())
  1058. return;
  1059. if (breaked)
  1060. return;
  1061. debug_break();
  1062. }
  1063. void ScriptEditorDebugger::_stack_dump_frame_selected() {
  1064. TreeItem *ti = stack_dump->get_selected();
  1065. if (!ti)
  1066. return;
  1067. Dictionary d = ti->get_metadata(0);
  1068. stack_script = ResourceLoader::load(d["file"]);
  1069. emit_signal("goto_script_line", stack_script, int(d["line"]) - 1);
  1070. stack_script.unref();
  1071. if (connection.is_valid() && connection->is_connected_to_host()) {
  1072. Array msg;
  1073. msg.push_back("get_stack_frame_vars");
  1074. msg.push_back(d["frame"]);
  1075. ppeer->put_var(msg);
  1076. } else {
  1077. inspector->edit(NULL);
  1078. }
  1079. }
  1080. void ScriptEditorDebugger::_output_clear() {
  1081. //output->clear();
  1082. //output->push_color(Color(0,0,0));
  1083. }
  1084. String ScriptEditorDebugger::get_var_value(const String &p_var) const {
  1085. if (!breaked)
  1086. return String();
  1087. return variables->get_var_value(p_var);
  1088. }
  1089. int ScriptEditorDebugger::_get_node_path_cache(const NodePath &p_path) {
  1090. const int *r = node_path_cache.getptr(p_path);
  1091. if (r)
  1092. return *r;
  1093. last_path_id++;
  1094. node_path_cache[p_path] = last_path_id;
  1095. Array msg;
  1096. msg.push_back("live_node_path");
  1097. msg.push_back(p_path);
  1098. msg.push_back(last_path_id);
  1099. ppeer->put_var(msg);
  1100. return last_path_id;
  1101. }
  1102. int ScriptEditorDebugger::_get_res_path_cache(const String &p_path) {
  1103. Map<String, int>::Element *E = res_path_cache.find(p_path);
  1104. if (E)
  1105. return E->get();
  1106. last_path_id++;
  1107. res_path_cache[p_path] = last_path_id;
  1108. Array msg;
  1109. msg.push_back("live_res_path");
  1110. msg.push_back(p_path);
  1111. msg.push_back(last_path_id);
  1112. ppeer->put_var(msg);
  1113. return last_path_id;
  1114. }
  1115. void ScriptEditorDebugger::_method_changed(Object *p_base, const StringName &p_name, VARIANT_ARG_DECLARE) {
  1116. if (!p_base || !live_debug || !connection.is_valid() || !editor->get_edited_scene())
  1117. return;
  1118. Node *node = Object::cast_to<Node>(p_base);
  1119. VARIANT_ARGPTRS
  1120. for (int i = 0; i < VARIANT_ARG_MAX; i++) {
  1121. //no pointers, sorry
  1122. if (argptr[i] && (argptr[i]->get_type() == Variant::OBJECT || argptr[i]->get_type() == Variant::_RID))
  1123. return;
  1124. }
  1125. if (node) {
  1126. NodePath path = editor->get_edited_scene()->get_path_to(node);
  1127. int pathid = _get_node_path_cache(path);
  1128. Array msg;
  1129. msg.push_back("live_node_call");
  1130. msg.push_back(pathid);
  1131. msg.push_back(p_name);
  1132. for (int i = 0; i < VARIANT_ARG_MAX; i++) {
  1133. //no pointers, sorry
  1134. msg.push_back(*argptr[i]);
  1135. }
  1136. ppeer->put_var(msg);
  1137. return;
  1138. }
  1139. Resource *res = Object::cast_to<Resource>(p_base);
  1140. if (res && res->get_path() != String()) {
  1141. String respath = res->get_path();
  1142. int pathid = _get_res_path_cache(respath);
  1143. Array msg;
  1144. msg.push_back("live_res_call");
  1145. msg.push_back(pathid);
  1146. msg.push_back(p_name);
  1147. for (int i = 0; i < VARIANT_ARG_MAX; i++) {
  1148. //no pointers, sorry
  1149. msg.push_back(*argptr[i]);
  1150. }
  1151. ppeer->put_var(msg);
  1152. return;
  1153. }
  1154. }
  1155. void ScriptEditorDebugger::_property_changed(Object *p_base, const StringName &p_property, const Variant &p_value) {
  1156. if (!p_base || !live_debug || !connection.is_valid() || !editor->get_edited_scene())
  1157. return;
  1158. Node *node = Object::cast_to<Node>(p_base);
  1159. if (node) {
  1160. NodePath path = editor->get_edited_scene()->get_path_to(node);
  1161. int pathid = _get_node_path_cache(path);
  1162. if (p_value.is_ref()) {
  1163. Ref<Resource> res = p_value;
  1164. if (res.is_valid() && res->get_path() != String()) {
  1165. Array msg;
  1166. msg.push_back("live_node_prop_res");
  1167. msg.push_back(pathid);
  1168. msg.push_back(p_property);
  1169. msg.push_back(res->get_path());
  1170. ppeer->put_var(msg);
  1171. }
  1172. } else {
  1173. Array msg;
  1174. msg.push_back("live_node_prop");
  1175. msg.push_back(pathid);
  1176. msg.push_back(p_property);
  1177. msg.push_back(p_value);
  1178. ppeer->put_var(msg);
  1179. }
  1180. return;
  1181. }
  1182. Resource *res = Object::cast_to<Resource>(p_base);
  1183. if (res && res->get_path() != String()) {
  1184. String respath = res->get_path();
  1185. int pathid = _get_res_path_cache(respath);
  1186. if (p_value.is_ref()) {
  1187. Ref<Resource> res = p_value;
  1188. if (res.is_valid() && res->get_path() != String()) {
  1189. Array msg;
  1190. msg.push_back("live_res_prop_res");
  1191. msg.push_back(pathid);
  1192. msg.push_back(p_property);
  1193. msg.push_back(res->get_path());
  1194. ppeer->put_var(msg);
  1195. }
  1196. } else {
  1197. Array msg;
  1198. msg.push_back("live_res_prop");
  1199. msg.push_back(pathid);
  1200. msg.push_back(p_property);
  1201. msg.push_back(p_value);
  1202. ppeer->put_var(msg);
  1203. }
  1204. return;
  1205. }
  1206. }
  1207. void ScriptEditorDebugger::_method_changeds(void *p_ud, Object *p_base, const StringName &p_name, VARIANT_ARG_DECLARE) {
  1208. ScriptEditorDebugger *sed = (ScriptEditorDebugger *)p_ud;
  1209. sed->_method_changed(p_base, p_name, VARIANT_ARG_PASS);
  1210. }
  1211. void ScriptEditorDebugger::_property_changeds(void *p_ud, Object *p_base, const StringName &p_property, const Variant &p_value) {
  1212. ScriptEditorDebugger *sed = (ScriptEditorDebugger *)p_ud;
  1213. sed->_property_changed(p_base, p_property, p_value);
  1214. }
  1215. void ScriptEditorDebugger::set_live_debugging(bool p_enable) {
  1216. live_debug = p_enable;
  1217. }
  1218. void ScriptEditorDebugger::_live_edit_set() {
  1219. if (!connection.is_valid())
  1220. return;
  1221. TreeItem *ti = inspect_scene_tree->get_selected();
  1222. if (!ti)
  1223. return;
  1224. String path;
  1225. while (ti) {
  1226. String lp = ti->get_text(0);
  1227. path = "/" + lp + path;
  1228. ti = ti->get_parent();
  1229. }
  1230. NodePath np = path;
  1231. editor->get_editor_data().set_edited_scene_live_edit_root(np);
  1232. update_live_edit_root();
  1233. }
  1234. void ScriptEditorDebugger::_live_edit_clear() {
  1235. NodePath np = NodePath("/root");
  1236. editor->get_editor_data().set_edited_scene_live_edit_root(np);
  1237. update_live_edit_root();
  1238. }
  1239. void ScriptEditorDebugger::update_live_edit_root() {
  1240. NodePath np = editor->get_editor_data().get_edited_scene_live_edit_root();
  1241. if (connection.is_valid()) {
  1242. Array msg;
  1243. msg.push_back("live_set_root");
  1244. msg.push_back(np);
  1245. if (editor->get_edited_scene())
  1246. msg.push_back(editor->get_edited_scene()->get_filename());
  1247. else
  1248. msg.push_back("");
  1249. ppeer->put_var(msg);
  1250. }
  1251. live_edit_root->set_text(np);
  1252. }
  1253. void ScriptEditorDebugger::live_debug_create_node(const NodePath &p_parent, const String &p_type, const String &p_name) {
  1254. if (live_debug && connection.is_valid()) {
  1255. Array msg;
  1256. msg.push_back("live_create_node");
  1257. msg.push_back(p_parent);
  1258. msg.push_back(p_type);
  1259. msg.push_back(p_name);
  1260. ppeer->put_var(msg);
  1261. }
  1262. }
  1263. void ScriptEditorDebugger::live_debug_instance_node(const NodePath &p_parent, const String &p_path, const String &p_name) {
  1264. if (live_debug && connection.is_valid()) {
  1265. Array msg;
  1266. msg.push_back("live_instance_node");
  1267. msg.push_back(p_parent);
  1268. msg.push_back(p_path);
  1269. msg.push_back(p_name);
  1270. ppeer->put_var(msg);
  1271. }
  1272. }
  1273. void ScriptEditorDebugger::live_debug_remove_node(const NodePath &p_at) {
  1274. if (live_debug && connection.is_valid()) {
  1275. Array msg;
  1276. msg.push_back("live_remove_node");
  1277. msg.push_back(p_at);
  1278. ppeer->put_var(msg);
  1279. }
  1280. }
  1281. void ScriptEditorDebugger::live_debug_remove_and_keep_node(const NodePath &p_at, ObjectID p_keep_id) {
  1282. if (live_debug && connection.is_valid()) {
  1283. Array msg;
  1284. msg.push_back("live_remove_and_keep_node");
  1285. msg.push_back(p_at);
  1286. msg.push_back(p_keep_id);
  1287. ppeer->put_var(msg);
  1288. }
  1289. }
  1290. void ScriptEditorDebugger::live_debug_restore_node(ObjectID p_id, const NodePath &p_at, int p_at_pos) {
  1291. if (live_debug && connection.is_valid()) {
  1292. Array msg;
  1293. msg.push_back("live_restore_node");
  1294. msg.push_back(p_id);
  1295. msg.push_back(p_at);
  1296. msg.push_back(p_at_pos);
  1297. ppeer->put_var(msg);
  1298. }
  1299. }
  1300. void ScriptEditorDebugger::live_debug_duplicate_node(const NodePath &p_at, const String &p_new_name) {
  1301. if (live_debug && connection.is_valid()) {
  1302. Array msg;
  1303. msg.push_back("live_duplicate_node");
  1304. msg.push_back(p_at);
  1305. msg.push_back(p_new_name);
  1306. ppeer->put_var(msg);
  1307. }
  1308. }
  1309. void ScriptEditorDebugger::live_debug_reparent_node(const NodePath &p_at, const NodePath &p_new_place, const String &p_new_name, int p_at_pos) {
  1310. if (live_debug && connection.is_valid()) {
  1311. Array msg;
  1312. msg.push_back("live_reparent_node");
  1313. msg.push_back(p_at);
  1314. msg.push_back(p_new_place);
  1315. msg.push_back(p_new_name);
  1316. msg.push_back(p_at_pos);
  1317. ppeer->put_var(msg);
  1318. }
  1319. }
  1320. void ScriptEditorDebugger::set_breakpoint(const String &p_path, int p_line, bool p_enabled) {
  1321. if (connection.is_valid()) {
  1322. Array msg;
  1323. msg.push_back("breakpoint");
  1324. msg.push_back(p_path);
  1325. msg.push_back(p_line);
  1326. msg.push_back(p_enabled);
  1327. ppeer->put_var(msg);
  1328. }
  1329. }
  1330. void ScriptEditorDebugger::reload_scripts() {
  1331. if (connection.is_valid()) {
  1332. Array msg;
  1333. msg.push_back("reload_scripts");
  1334. ppeer->put_var(msg);
  1335. }
  1336. }
  1337. void ScriptEditorDebugger::_error_activated() {
  1338. TreeItem *selected = error_tree->get_selected();
  1339. TreeItem *ci = selected->get_children();
  1340. if (ci) {
  1341. selected->set_collapsed(!selected->is_collapsed());
  1342. }
  1343. }
  1344. void ScriptEditorDebugger::_error_selected() {
  1345. TreeItem *selected = error_tree->get_selected();
  1346. Array meta = selected->get_metadata(0);
  1347. if (meta.size() == 0) {
  1348. return;
  1349. }
  1350. Ref<Script> s = ResourceLoader::load(meta[0]);
  1351. emit_signal("goto_script_line", s, int(meta[1]) - 1);
  1352. }
  1353. void ScriptEditorDebugger::_expand_errors_list() {
  1354. TreeItem *root = error_tree->get_root();
  1355. if (!root)
  1356. return;
  1357. TreeItem *item = root->get_children();
  1358. while (item) {
  1359. item->set_collapsed(false);
  1360. item = item->get_next();
  1361. }
  1362. }
  1363. void ScriptEditorDebugger::_collapse_errors_list() {
  1364. TreeItem *root = error_tree->get_root();
  1365. if (!root)
  1366. return;
  1367. TreeItem *item = root->get_children();
  1368. while (item) {
  1369. item->set_collapsed(true);
  1370. item = item->get_next();
  1371. }
  1372. }
  1373. void ScriptEditorDebugger::set_hide_on_stop(bool p_hide) {
  1374. hide_on_stop = p_hide;
  1375. }
  1376. bool ScriptEditorDebugger::get_debug_with_external_editor() const {
  1377. return enable_external_editor;
  1378. }
  1379. void ScriptEditorDebugger::set_debug_with_external_editor(bool p_enabled) {
  1380. enable_external_editor = p_enabled;
  1381. }
  1382. Ref<Script> ScriptEditorDebugger::get_dump_stack_script() const {
  1383. return stack_script;
  1384. }
  1385. void ScriptEditorDebugger::_paused() {
  1386. ERR_FAIL_COND(connection.is_null());
  1387. ERR_FAIL_COND(!connection->is_connected_to_host());
  1388. if (!breaked && EditorNode::get_singleton()->get_pause_button()->is_pressed()) {
  1389. debug_break();
  1390. }
  1391. if (breaked && !EditorNode::get_singleton()->get_pause_button()->is_pressed()) {
  1392. debug_continue();
  1393. }
  1394. }
  1395. void ScriptEditorDebugger::_set_remote_object(ObjectID p_id, ScriptEditorDebuggerInspectedObject *p_obj) {
  1396. if (remote_objects.has(p_id))
  1397. memdelete(remote_objects[p_id]);
  1398. remote_objects[p_id] = p_obj;
  1399. }
  1400. void ScriptEditorDebugger::_clear_remote_objects() {
  1401. for (Map<ObjectID, ScriptEditorDebuggerInspectedObject *>::Element *E = remote_objects.front(); E; E = E->next()) {
  1402. if (editor->get_editor_history()->get_current() == E->value()->get_instance_id()) {
  1403. editor->push_item(NULL);
  1404. }
  1405. memdelete(E->value());
  1406. }
  1407. remote_objects.clear();
  1408. }
  1409. void ScriptEditorDebugger::_clear_errors_list() {
  1410. error_tree->clear();
  1411. error_count = 0;
  1412. warning_count = 0;
  1413. _notification(NOTIFICATION_PROCESS);
  1414. }
  1415. // Right click on specific file(s) or folder(s).
  1416. void ScriptEditorDebugger::_error_tree_item_rmb_selected(const Vector2 &p_pos) {
  1417. item_menu->clear();
  1418. item_menu->set_size(Size2(1, 1));
  1419. if (error_tree->is_anything_selected()) {
  1420. item_menu->add_icon_item(get_icon("ActionCopy", "EditorIcons"), TTR("Copy Error"), ITEM_MENU_COPY_ERROR);
  1421. }
  1422. if (item_menu->get_item_count() > 0) {
  1423. item_menu->set_position(error_tree->get_global_position() + p_pos);
  1424. item_menu->popup();
  1425. }
  1426. }
  1427. void ScriptEditorDebugger::_item_menu_id_pressed(int p_option) {
  1428. switch (p_option) {
  1429. case ITEM_MENU_COPY_ERROR: {
  1430. TreeItem *ti = error_tree->get_selected();
  1431. while (ti->get_parent() != error_tree->get_root())
  1432. ti = ti->get_parent();
  1433. String type;
  1434. if (ti->get_icon(0) == get_icon("Warning", "EditorIcons")) {
  1435. type = "W ";
  1436. } else if (ti->get_icon(0) == get_icon("Error", "EditorIcons")) {
  1437. type = "E ";
  1438. }
  1439. String text = ti->get_text(0) + " ";
  1440. int rpad_len = text.length();
  1441. text = type + text + ti->get_text(1) + "\n";
  1442. TreeItem *ci = ti->get_children();
  1443. while (ci) {
  1444. text += " " + ci->get_text(0).rpad(rpad_len) + ci->get_text(1) + "\n";
  1445. ci = ci->get_next();
  1446. }
  1447. OS::get_singleton()->set_clipboard(text);
  1448. } break;
  1449. case ITEM_MENU_SAVE_REMOTE_NODE: {
  1450. file_dialog->set_access(EditorFileDialog::ACCESS_RESOURCES);
  1451. file_dialog->set_mode(EditorFileDialog::MODE_SAVE_FILE);
  1452. List<String> extensions;
  1453. Ref<PackedScene> sd = memnew(PackedScene);
  1454. ResourceSaver::get_recognized_extensions(sd, &extensions);
  1455. file_dialog->clear_filters();
  1456. for (int i = 0; i < extensions.size(); i++) {
  1457. file_dialog->add_filter("*." + extensions[i] + " ; " + extensions[i].to_upper());
  1458. }
  1459. file_dialog->popup_centered_ratio();
  1460. } break;
  1461. }
  1462. }
  1463. void ScriptEditorDebugger::_bind_methods() {
  1464. ClassDB::bind_method(D_METHOD("_stack_dump_frame_selected"), &ScriptEditorDebugger::_stack_dump_frame_selected);
  1465. ClassDB::bind_method(D_METHOD("debug_copy"), &ScriptEditorDebugger::debug_copy);
  1466. ClassDB::bind_method(D_METHOD("debug_next"), &ScriptEditorDebugger::debug_next);
  1467. ClassDB::bind_method(D_METHOD("debug_step"), &ScriptEditorDebugger::debug_step);
  1468. ClassDB::bind_method(D_METHOD("debug_break"), &ScriptEditorDebugger::debug_break);
  1469. ClassDB::bind_method(D_METHOD("debug_continue"), &ScriptEditorDebugger::debug_continue);
  1470. ClassDB::bind_method(D_METHOD("_output_clear"), &ScriptEditorDebugger::_output_clear);
  1471. ClassDB::bind_method(D_METHOD("_performance_draw"), &ScriptEditorDebugger::_performance_draw);
  1472. ClassDB::bind_method(D_METHOD("_performance_select"), &ScriptEditorDebugger::_performance_select);
  1473. ClassDB::bind_method(D_METHOD("_scene_tree_request"), &ScriptEditorDebugger::_scene_tree_request);
  1474. ClassDB::bind_method(D_METHOD("_video_mem_request"), &ScriptEditorDebugger::_video_mem_request);
  1475. ClassDB::bind_method(D_METHOD("_live_edit_set"), &ScriptEditorDebugger::_live_edit_set);
  1476. ClassDB::bind_method(D_METHOD("_live_edit_clear"), &ScriptEditorDebugger::_live_edit_clear);
  1477. ClassDB::bind_method(D_METHOD("_error_selected"), &ScriptEditorDebugger::_error_selected);
  1478. ClassDB::bind_method(D_METHOD("_error_activated"), &ScriptEditorDebugger::_error_activated);
  1479. ClassDB::bind_method(D_METHOD("_expand_errors_list"), &ScriptEditorDebugger::_expand_errors_list);
  1480. ClassDB::bind_method(D_METHOD("_collapse_errors_list"), &ScriptEditorDebugger::_collapse_errors_list);
  1481. ClassDB::bind_method(D_METHOD("_profiler_activate"), &ScriptEditorDebugger::_profiler_activate);
  1482. ClassDB::bind_method(D_METHOD("_profiler_seeked"), &ScriptEditorDebugger::_profiler_seeked);
  1483. ClassDB::bind_method(D_METHOD("_clear_errors_list"), &ScriptEditorDebugger::_clear_errors_list);
  1484. ClassDB::bind_method(D_METHOD("_error_tree_item_rmb_selected"), &ScriptEditorDebugger::_error_tree_item_rmb_selected);
  1485. ClassDB::bind_method(D_METHOD("_item_menu_id_pressed"), &ScriptEditorDebugger::_item_menu_id_pressed);
  1486. ClassDB::bind_method(D_METHOD("_paused"), &ScriptEditorDebugger::_paused);
  1487. ClassDB::bind_method(D_METHOD("_scene_tree_selected"), &ScriptEditorDebugger::_scene_tree_selected);
  1488. ClassDB::bind_method(D_METHOD("_scene_tree_folded"), &ScriptEditorDebugger::_scene_tree_folded);
  1489. ClassDB::bind_method(D_METHOD("_scene_tree_rmb_selected"), &ScriptEditorDebugger::_scene_tree_rmb_selected);
  1490. ClassDB::bind_method(D_METHOD("_file_selected"), &ScriptEditorDebugger::_file_selected);
  1491. ClassDB::bind_method(D_METHOD("live_debug_create_node"), &ScriptEditorDebugger::live_debug_create_node);
  1492. ClassDB::bind_method(D_METHOD("live_debug_instance_node"), &ScriptEditorDebugger::live_debug_instance_node);
  1493. ClassDB::bind_method(D_METHOD("live_debug_remove_node"), &ScriptEditorDebugger::live_debug_remove_node);
  1494. ClassDB::bind_method(D_METHOD("live_debug_remove_and_keep_node"), &ScriptEditorDebugger::live_debug_remove_and_keep_node);
  1495. ClassDB::bind_method(D_METHOD("live_debug_restore_node"), &ScriptEditorDebugger::live_debug_restore_node);
  1496. ClassDB::bind_method(D_METHOD("live_debug_duplicate_node"), &ScriptEditorDebugger::live_debug_duplicate_node);
  1497. ClassDB::bind_method(D_METHOD("live_debug_reparent_node"), &ScriptEditorDebugger::live_debug_reparent_node);
  1498. ClassDB::bind_method(D_METHOD("_scene_tree_property_select_object"), &ScriptEditorDebugger::_scene_tree_property_select_object);
  1499. ClassDB::bind_method(D_METHOD("_scene_tree_property_value_edited"), &ScriptEditorDebugger::_scene_tree_property_value_edited);
  1500. ADD_SIGNAL(MethodInfo("goto_script_line"));
  1501. ADD_SIGNAL(MethodInfo("breaked", PropertyInfo(Variant::BOOL, "reallydid"), PropertyInfo(Variant::BOOL, "can_debug")));
  1502. ADD_SIGNAL(MethodInfo("show_debugger", PropertyInfo(Variant::BOOL, "reallydid")));
  1503. }
  1504. ScriptEditorDebugger::ScriptEditorDebugger(EditorNode *p_editor) {
  1505. ppeer = Ref<PacketPeerStream>(memnew(PacketPeerStream));
  1506. ppeer->set_input_buffer_max_size(1024 * 1024 * 8); //8mb should be enough
  1507. editor = p_editor;
  1508. editor->get_inspector()->connect("object_id_selected", this, "_scene_tree_property_select_object");
  1509. tabs = memnew(TabContainer);
  1510. tabs->set_tab_align(TabContainer::ALIGN_LEFT);
  1511. tabs->add_style_override("panel", editor->get_gui_base()->get_stylebox("DebuggerPanel", "EditorStyles"));
  1512. tabs->add_style_override("tab_fg", editor->get_gui_base()->get_stylebox("DebuggerTabFG", "EditorStyles"));
  1513. tabs->add_style_override("tab_bg", editor->get_gui_base()->get_stylebox("DebuggerTabBG", "EditorStyles"));
  1514. tabs->set_anchors_and_margins_preset(Control::PRESET_WIDE);
  1515. tabs->set_margin(MARGIN_LEFT, -editor->get_gui_base()->get_stylebox("BottomPanelDebuggerOverride", "EditorStyles")->get_margin(MARGIN_LEFT));
  1516. tabs->set_margin(MARGIN_RIGHT, editor->get_gui_base()->get_stylebox("BottomPanelDebuggerOverride", "EditorStyles")->get_margin(MARGIN_RIGHT));
  1517. add_child(tabs);
  1518. { //debugger
  1519. VBoxContainer *vbc = memnew(VBoxContainer);
  1520. vbc->set_name(TTR("Debugger"));
  1521. //tabs->add_child(vbc);
  1522. Control *dbg = vbc;
  1523. HBoxContainer *hbc = memnew(HBoxContainer);
  1524. vbc->add_child(hbc);
  1525. reason = memnew(Label);
  1526. reason->set_text("");
  1527. hbc->add_child(reason);
  1528. reason->set_h_size_flags(SIZE_EXPAND_FILL);
  1529. reason->set_autowrap(true);
  1530. reason->set_max_lines_visible(3);
  1531. reason->set_mouse_filter(Control::MOUSE_FILTER_PASS);
  1532. hbc->add_child(memnew(VSeparator));
  1533. copy = memnew(ToolButton);
  1534. hbc->add_child(copy);
  1535. copy->set_tooltip(TTR("Copy Error"));
  1536. copy->connect("pressed", this, "debug_copy");
  1537. hbc->add_child(memnew(VSeparator));
  1538. step = memnew(ToolButton);
  1539. hbc->add_child(step);
  1540. step->set_tooltip(TTR("Step Into"));
  1541. step->connect("pressed", this, "debug_step");
  1542. next = memnew(ToolButton);
  1543. hbc->add_child(next);
  1544. next->set_tooltip(TTR("Step Over"));
  1545. next->connect("pressed", this, "debug_next");
  1546. hbc->add_child(memnew(VSeparator));
  1547. dobreak = memnew(ToolButton);
  1548. hbc->add_child(dobreak);
  1549. dobreak->set_tooltip(TTR("Break"));
  1550. dobreak->connect("pressed", this, "debug_break");
  1551. docontinue = memnew(ToolButton);
  1552. hbc->add_child(docontinue);
  1553. docontinue->set_tooltip(TTR("Continue"));
  1554. docontinue->connect("pressed", this, "debug_continue");
  1555. back = memnew(Button);
  1556. hbc->add_child(back);
  1557. back->set_tooltip(TTR("Inspect Previous Instance"));
  1558. back->hide();
  1559. forward = memnew(Button);
  1560. hbc->add_child(forward);
  1561. forward->set_tooltip(TTR("Inspect Next Instance"));
  1562. forward->hide();
  1563. HSplitContainer *sc = memnew(HSplitContainer);
  1564. vbc->add_child(sc);
  1565. sc->set_v_size_flags(SIZE_EXPAND_FILL);
  1566. stack_dump = memnew(Tree);
  1567. stack_dump->set_allow_reselect(true);
  1568. stack_dump->set_columns(1);
  1569. stack_dump->set_column_titles_visible(true);
  1570. stack_dump->set_column_title(0, TTR("Stack Frames"));
  1571. stack_dump->set_h_size_flags(SIZE_EXPAND_FILL);
  1572. stack_dump->set_hide_root(true);
  1573. stack_dump->connect("cell_selected", this, "_stack_dump_frame_selected");
  1574. sc->add_child(stack_dump);
  1575. inspector = memnew(EditorInspector);
  1576. inspector->set_h_size_flags(SIZE_EXPAND_FILL);
  1577. inspector->set_enable_capitalize_paths(false);
  1578. inspector->set_read_only(true);
  1579. inspector->connect("object_id_selected", this, "_scene_tree_property_select_object");
  1580. sc->add_child(inspector);
  1581. server.instance();
  1582. pending_in_queue = 0;
  1583. variables = memnew(ScriptEditorDebuggerVariables);
  1584. breaked = false;
  1585. tabs->add_child(dbg);
  1586. }
  1587. { //errors
  1588. VBoxContainer *errvb = memnew(VBoxContainer);
  1589. errvb->set_name(TTR("Errors"));
  1590. HBoxContainer *errhb = memnew(HBoxContainer);
  1591. errvb->add_child(errhb);
  1592. Button *expand_all = memnew(Button);
  1593. expand_all->set_text(TTR("Expand All"));
  1594. expand_all->connect("pressed", this, "_expand_errors_list");
  1595. errhb->add_child(expand_all);
  1596. Button *collapse_all = memnew(Button);
  1597. collapse_all->set_text(TTR("Collapse All"));
  1598. collapse_all->connect("pressed", this, "_collapse_errors_list");
  1599. errhb->add_child(collapse_all);
  1600. Control *space = memnew(Control);
  1601. space->set_h_size_flags(SIZE_EXPAND_FILL);
  1602. errhb->add_child(space);
  1603. clearbutton = memnew(Button);
  1604. clearbutton->set_text(TTR("Clear"));
  1605. clearbutton->set_h_size_flags(0);
  1606. clearbutton->connect("pressed", this, "_clear_errors_list");
  1607. errhb->add_child(clearbutton);
  1608. error_tree = memnew(Tree);
  1609. error_tree->set_columns(2);
  1610. error_tree->set_column_expand(0, false);
  1611. error_tree->set_column_min_width(0, 140);
  1612. error_tree->set_column_expand(1, true);
  1613. error_tree->set_select_mode(Tree::SELECT_ROW);
  1614. error_tree->set_hide_root(true);
  1615. error_tree->set_v_size_flags(SIZE_EXPAND_FILL);
  1616. error_tree->set_allow_rmb_select(true);
  1617. error_tree->connect("item_rmb_selected", this, "_error_tree_item_rmb_selected");
  1618. errvb->add_child(error_tree);
  1619. item_menu = memnew(PopupMenu);
  1620. item_menu->connect("id_pressed", this, "_item_menu_id_pressed");
  1621. error_tree->add_child(item_menu);
  1622. tabs->add_child(errvb);
  1623. }
  1624. { // remote scene tree
  1625. inspect_scene_tree = memnew(Tree);
  1626. EditorNode::get_singleton()->get_scene_tree_dock()->add_remote_tree_editor(inspect_scene_tree);
  1627. EditorNode::get_singleton()->get_scene_tree_dock()->connect("remote_tree_selected", this, "_scene_tree_selected");
  1628. inspect_scene_tree->set_v_size_flags(SIZE_EXPAND_FILL);
  1629. inspect_scene_tree->connect("cell_selected", this, "_scene_tree_selected");
  1630. inspect_scene_tree->connect("item_collapsed", this, "_scene_tree_folded");
  1631. inspect_scene_tree->set_allow_rmb_select(true);
  1632. inspect_scene_tree->connect("item_rmb_selected", this, "_scene_tree_rmb_selected");
  1633. auto_switch_remote_scene_tree = EDITOR_DEF("debugger/auto_switch_to_remote_scene_tree", false);
  1634. inspect_scene_tree_timeout = EDITOR_DEF("debugger/remote_scene_tree_refresh_interval", 1.0);
  1635. inspect_edited_object_timeout = EDITOR_DEF("debugger/remote_inspect_refresh_interval", 0.2);
  1636. inspected_object_id = 0;
  1637. updating_scene_tree = false;
  1638. }
  1639. { // File dialog
  1640. file_dialog = memnew(EditorFileDialog);
  1641. file_dialog->connect("file_selected", this, "_file_selected");
  1642. add_child(file_dialog);
  1643. }
  1644. { //profiler
  1645. profiler = memnew(EditorProfiler);
  1646. profiler->set_name(TTR("Profiler"));
  1647. tabs->add_child(profiler);
  1648. profiler->connect("enable_profiling", this, "_profiler_activate");
  1649. profiler->connect("break_request", this, "_profiler_seeked");
  1650. }
  1651. { //monitors
  1652. HSplitContainer *hsp = memnew(HSplitContainer);
  1653. perf_monitors = memnew(Tree);
  1654. perf_monitors->set_columns(2);
  1655. perf_monitors->set_column_title(0, TTR("Monitor"));
  1656. perf_monitors->set_column_title(1, TTR("Value"));
  1657. perf_monitors->set_column_titles_visible(true);
  1658. hsp->add_child(perf_monitors);
  1659. perf_monitors->connect("item_edited", this, "_performance_select");
  1660. perf_draw = memnew(Control);
  1661. perf_draw->connect("draw", this, "_performance_draw");
  1662. hsp->add_child(perf_draw);
  1663. hsp->set_name(TTR("Monitors"));
  1664. hsp->set_split_offset(340 * EDSCALE);
  1665. tabs->add_child(hsp);
  1666. perf_max.resize(Performance::MONITOR_MAX);
  1667. Map<String, TreeItem *> bases;
  1668. TreeItem *root = perf_monitors->create_item();
  1669. perf_monitors->set_hide_root(true);
  1670. for (int i = 0; i < Performance::MONITOR_MAX; i++) {
  1671. String n = Performance::get_singleton()->get_monitor_name(Performance::Monitor(i));
  1672. Performance::MonitorType mtype = Performance::get_singleton()->get_monitor_type(Performance::Monitor(i));
  1673. String base = n.get_slice("/", 0);
  1674. String name = n.get_slice("/", 1);
  1675. if (!bases.has(base)) {
  1676. TreeItem *b = perf_monitors->create_item(root);
  1677. b->set_text(0, base.capitalize());
  1678. b->set_editable(0, false);
  1679. b->set_selectable(0, false);
  1680. b->set_expand_right(0, true);
  1681. bases[base] = b;
  1682. }
  1683. TreeItem *it = perf_monitors->create_item(bases[base]);
  1684. it->set_metadata(1, mtype);
  1685. it->set_cell_mode(0, TreeItem::CELL_MODE_CHECK);
  1686. it->set_editable(0, true);
  1687. it->set_selectable(0, false);
  1688. it->set_selectable(1, false);
  1689. it->set_text(0, name.capitalize());
  1690. perf_items.push_back(it);
  1691. perf_max.write[i] = 0;
  1692. }
  1693. }
  1694. { //vmem inspect
  1695. VBoxContainer *vmem_vb = memnew(VBoxContainer);
  1696. HBoxContainer *vmem_hb = memnew(HBoxContainer);
  1697. Label *vmlb = memnew(Label(TTR("List of Video Memory Usage by Resource:") + " "));
  1698. vmlb->set_h_size_flags(SIZE_EXPAND_FILL);
  1699. vmem_hb->add_child(vmlb);
  1700. vmem_hb->add_child(memnew(Label(TTR("Total:") + " ")));
  1701. vmem_total = memnew(LineEdit);
  1702. vmem_total->set_editable(false);
  1703. vmem_total->set_custom_minimum_size(Size2(100, 1) * EDSCALE);
  1704. vmem_hb->add_child(vmem_total);
  1705. vmem_refresh = memnew(ToolButton);
  1706. vmem_hb->add_child(vmem_refresh);
  1707. vmem_vb->add_child(vmem_hb);
  1708. vmem_refresh->connect("pressed", this, "_video_mem_request");
  1709. VBoxContainer *vmmc = memnew(VBoxContainer);
  1710. vmem_tree = memnew(Tree);
  1711. vmem_tree->set_v_size_flags(SIZE_EXPAND_FILL);
  1712. vmem_tree->set_h_size_flags(SIZE_EXPAND_FILL);
  1713. vmmc->add_child(vmem_tree);
  1714. vmmc->set_v_size_flags(SIZE_EXPAND_FILL);
  1715. vmem_vb->add_child(vmmc);
  1716. vmem_vb->set_name(TTR("Video Mem"));
  1717. vmem_tree->set_columns(4);
  1718. vmem_tree->set_column_titles_visible(true);
  1719. vmem_tree->set_column_title(0, TTR("Resource Path"));
  1720. vmem_tree->set_column_expand(0, true);
  1721. vmem_tree->set_column_expand(1, false);
  1722. vmem_tree->set_column_title(1, TTR("Type"));
  1723. vmem_tree->set_column_min_width(1, 100);
  1724. vmem_tree->set_column_expand(2, false);
  1725. vmem_tree->set_column_title(2, TTR("Format"));
  1726. vmem_tree->set_column_min_width(2, 150);
  1727. vmem_tree->set_column_expand(3, false);
  1728. vmem_tree->set_column_title(3, TTR("Usage"));
  1729. vmem_tree->set_column_min_width(3, 80);
  1730. vmem_tree->set_hide_root(true);
  1731. tabs->add_child(vmem_vb);
  1732. }
  1733. { // misc
  1734. GridContainer *info_left = memnew(GridContainer);
  1735. info_left->set_columns(2);
  1736. info_left->set_name(TTR("Misc"));
  1737. tabs->add_child(info_left);
  1738. clicked_ctrl = memnew(LineEdit);
  1739. clicked_ctrl->set_h_size_flags(SIZE_EXPAND_FILL);
  1740. info_left->add_child(memnew(Label(TTR("Clicked Control:"))));
  1741. info_left->add_child(clicked_ctrl);
  1742. clicked_ctrl_type = memnew(LineEdit);
  1743. info_left->add_child(memnew(Label(TTR("Clicked Control Type:"))));
  1744. info_left->add_child(clicked_ctrl_type);
  1745. live_edit_root = memnew(LineEdit);
  1746. live_edit_root->set_h_size_flags(SIZE_EXPAND_FILL);
  1747. {
  1748. HBoxContainer *lehb = memnew(HBoxContainer);
  1749. Label *l = memnew(Label(TTR("Live Edit Root:")));
  1750. info_left->add_child(l);
  1751. lehb->add_child(live_edit_root);
  1752. le_set = memnew(Button(TTR("Set From Tree")));
  1753. lehb->add_child(le_set);
  1754. le_clear = memnew(Button(TTR("Clear")));
  1755. lehb->add_child(le_clear);
  1756. info_left->add_child(lehb);
  1757. le_set->set_disabled(true);
  1758. le_clear->set_disabled(true);
  1759. }
  1760. }
  1761. msgdialog = memnew(AcceptDialog);
  1762. add_child(msgdialog);
  1763. p_editor->get_undo_redo()->set_method_notify_callback(_method_changeds, this);
  1764. p_editor->get_undo_redo()->set_property_notify_callback(_property_changeds, this);
  1765. live_debug = false;
  1766. last_path_id = false;
  1767. error_count = 0;
  1768. warning_count = 0;
  1769. hide_on_stop = true;
  1770. enable_external_editor = false;
  1771. last_error_count = 0;
  1772. last_warning_count = 0;
  1773. EditorNode::get_singleton()->get_pause_button()->connect("pressed", this, "_paused");
  1774. }
  1775. ScriptEditorDebugger::~ScriptEditorDebugger() {
  1776. memdelete(variables);
  1777. ppeer->set_stream_peer(Ref<StreamPeer>());
  1778. server->stop();
  1779. _clear_remote_objects();
  1780. }