script_editor_debugger.cpp 65 KB

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