script_editor_debugger.cpp 59 KB

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