script_editor_debugger.cpp 60 KB

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