script_editor_debugger.cpp 62 KB

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