script_editor_debugger.cpp 60 KB

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