script_editor_debugger.cpp 65 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276
  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 "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. } else
  834. inspect_scene_tree->add_constant_override("draw_relationship_lines", 0);
  835. } break;
  836. case NOTIFICATION_PROCESS: {
  837. if (connection.is_valid()) {
  838. inspect_scene_tree_timeout -= get_process_delta_time();
  839. if (inspect_scene_tree_timeout < 0) {
  840. inspect_scene_tree_timeout = EditorSettings::get_singleton()->get("debugger/remote_scene_tree_refresh_interval");
  841. if (inspect_scene_tree->is_visible_in_tree()) {
  842. _scene_tree_request();
  843. }
  844. }
  845. inspect_edited_object_timeout -= get_process_delta_time();
  846. if (inspect_edited_object_timeout < 0) {
  847. inspect_edited_object_timeout = EditorSettings::get_singleton()->get("debugger/remote_inspect_refresh_interval");
  848. if (inspected_object_id) {
  849. if (ScriptEditorDebuggerInspectedObject *obj = Object::cast_to<ScriptEditorDebuggerInspectedObject>(ObjectDB::get_instance(editor->get_editor_history()->get_current()))) {
  850. if (obj->remote_object_id == inspected_object_id) {
  851. //take the chance and re-inspect selected object
  852. Array msg;
  853. msg.push_back("inspect_object");
  854. msg.push_back(inspected_object_id);
  855. ppeer->put_var(msg);
  856. }
  857. }
  858. }
  859. }
  860. }
  861. if (error_count != last_error_count || warning_count != last_warning_count) {
  862. if (error_count == 0 && warning_count == 0) {
  863. error_tree->set_name(TTR("Errors"));
  864. debugger_button->set_text(TTR("Debugger"));
  865. debugger_button->set_icon(Ref<Texture>());
  866. tabs->set_tab_icon(error_tree->get_index(), Ref<Texture>());
  867. } else {
  868. error_tree->set_name(TTR("Errors") + " (" + itos(error_count + warning_count) + ")");
  869. debugger_button->set_text(TTR("Debugger") + " (" + itos(error_count + warning_count) + ")");
  870. if (error_count == 0) {
  871. debugger_button->set_icon(get_icon("Warning", "EditorIcons"));
  872. tabs->set_tab_icon(error_tree->get_index(), get_icon("Warning", "EditorIcons"));
  873. } else {
  874. debugger_button->set_icon(get_icon("Error", "EditorIcons"));
  875. tabs->set_tab_icon(error_tree->get_index(), get_icon("Error", "EditorIcons"));
  876. }
  877. }
  878. last_error_count = error_count;
  879. last_warning_count = warning_count;
  880. }
  881. if (connection.is_null()) {
  882. if (server->is_connection_available()) {
  883. connection = server->take_connection();
  884. if (connection.is_null())
  885. break;
  886. EditorNode::get_log()->add_message("** Debug Process Started **");
  887. ppeer->set_stream_peer(connection);
  888. //EditorNode::get_singleton()->make_bottom_panel_item_visible(this);
  889. //emit_signal("show_debugger",true);
  890. dobreak->set_disabled(false);
  891. tabs->set_current_tab(0);
  892. _set_reason_text(TTR("Child Process Connected"), MESSAGE_SUCCESS);
  893. profiler->clear();
  894. inspect_scene_tree->clear();
  895. le_set->set_disabled(true);
  896. le_clear->set_disabled(false);
  897. error_tree->clear();
  898. error_count = 0;
  899. warning_count = 0;
  900. profiler_signature.clear();
  901. //live_edit_root->set_text("/root");
  902. EditorNode::get_singleton()->get_pause_button()->set_pressed(false);
  903. EditorNode::get_singleton()->get_pause_button()->set_disabled(false);
  904. update_live_edit_root();
  905. if (profiler->is_profiling()) {
  906. _profiler_activate(true);
  907. }
  908. } else {
  909. break;
  910. }
  911. };
  912. if (!connection->is_connected_to_host()) {
  913. stop();
  914. editor->notify_child_process_exited(); //somehow, exited
  915. break;
  916. };
  917. if (ppeer->get_available_packet_count() <= 0) {
  918. break;
  919. };
  920. const uint64_t until = OS::get_singleton()->get_ticks_msec() + 20;
  921. while (ppeer->get_available_packet_count() > 0) {
  922. if (pending_in_queue) {
  923. int todo = MIN(ppeer->get_available_packet_count(), pending_in_queue);
  924. for (int i = 0; i < todo; i++) {
  925. Variant cmd;
  926. Error ret = ppeer->get_var(cmd);
  927. if (ret != OK) {
  928. stop();
  929. ERR_FAIL_COND(ret != OK);
  930. }
  931. message.push_back(cmd);
  932. pending_in_queue--;
  933. }
  934. if (pending_in_queue == 0) {
  935. _parse_message(message_type, message);
  936. message.clear();
  937. }
  938. } else {
  939. if (ppeer->get_available_packet_count() >= 2) {
  940. Variant cmd;
  941. Error ret = ppeer->get_var(cmd);
  942. if (ret != OK) {
  943. stop();
  944. ERR_FAIL_COND(ret != OK);
  945. }
  946. if (cmd.get_type() != Variant::STRING) {
  947. stop();
  948. ERR_FAIL_COND(cmd.get_type() != Variant::STRING);
  949. }
  950. message_type = cmd;
  951. ret = ppeer->get_var(cmd);
  952. if (ret != OK) {
  953. stop();
  954. ERR_FAIL_COND(ret != OK);
  955. }
  956. if (cmd.get_type() != Variant::INT) {
  957. stop();
  958. ERR_FAIL_COND(cmd.get_type() != Variant::INT);
  959. }
  960. pending_in_queue = cmd;
  961. if (pending_in_queue == 0) {
  962. _parse_message(message_type, Array());
  963. message.clear();
  964. }
  965. } else {
  966. break;
  967. }
  968. }
  969. if (OS::get_singleton()->get_ticks_msec() > until)
  970. break;
  971. }
  972. } break;
  973. case EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED: {
  974. tabs->add_style_override("panel", editor->get_gui_base()->get_stylebox("DebuggerPanel", "EditorStyles"));
  975. tabs->add_style_override("tab_fg", editor->get_gui_base()->get_stylebox("DebuggerTabFG", "EditorStyles"));
  976. tabs->add_style_override("tab_bg", editor->get_gui_base()->get_stylebox("DebuggerTabBG", "EditorStyles"));
  977. tabs->set_margin(MARGIN_LEFT, -EditorNode::get_singleton()->get_gui_base()->get_stylebox("BottomPanelDebuggerOverride", "EditorStyles")->get_margin(MARGIN_LEFT));
  978. tabs->set_margin(MARGIN_RIGHT, EditorNode::get_singleton()->get_gui_base()->get_stylebox("BottomPanelDebuggerOverride", "EditorStyles")->get_margin(MARGIN_RIGHT));
  979. bool enable_rl = EditorSettings::get_singleton()->get("docks/scene_tree/draw_relationship_lines");
  980. Color rl_color = EditorSettings::get_singleton()->get("docks/scene_tree/relationship_line_color");
  981. if (enable_rl) {
  982. inspect_scene_tree->add_constant_override("draw_relationship_lines", 1);
  983. inspect_scene_tree->add_color_override("relationship_line_color", rl_color);
  984. } else
  985. inspect_scene_tree->add_constant_override("draw_relationship_lines", 0);
  986. copy->set_icon(get_icon("ActionCopy", "EditorIcons"));
  987. step->set_icon(get_icon("DebugStep", "EditorIcons"));
  988. next->set_icon(get_icon("DebugNext", "EditorIcons"));
  989. back->set_icon(get_icon("Back", "EditorIcons"));
  990. forward->set_icon(get_icon("Forward", "EditorIcons"));
  991. dobreak->set_icon(get_icon("Pause", "EditorIcons"));
  992. docontinue->set_icon(get_icon("DebugContinue", "EditorIcons"));
  993. vmem_refresh->set_icon(get_icon("Reload", "EditorIcons"));
  994. } break;
  995. }
  996. }
  997. void ScriptEditorDebugger::start() {
  998. stop();
  999. if (is_visible_in_tree()) {
  1000. EditorNode::get_singleton()->make_bottom_panel_item_visible(this);
  1001. }
  1002. perf_history.clear();
  1003. for (int i = 0; i < Performance::MONITOR_MAX; i++) {
  1004. perf_max.write[i] = 0;
  1005. }
  1006. int remote_port = (int)EditorSettings::get_singleton()->get("network/debug/remote_port");
  1007. if (server->listen(remote_port) != OK) {
  1008. EditorNode::get_log()->add_message(String("Error listening on port ") + itos(remote_port), EditorLog::MSG_TYPE_ERROR);
  1009. return;
  1010. }
  1011. EditorNode::get_singleton()->get_scene_tree_dock()->show_tab_buttons();
  1012. auto_switch_remote_scene_tree = (bool)EditorSettings::get_singleton()->get("debugger/auto_switch_to_remote_scene_tree");
  1013. if (auto_switch_remote_scene_tree) {
  1014. EditorNode::get_singleton()->get_scene_tree_dock()->show_remote_tree();
  1015. }
  1016. set_process(true);
  1017. breaked = false;
  1018. }
  1019. void ScriptEditorDebugger::pause() {
  1020. }
  1021. void ScriptEditorDebugger::unpause() {
  1022. }
  1023. void ScriptEditorDebugger::stop() {
  1024. set_process(false);
  1025. breaked = false;
  1026. server->stop();
  1027. _clear_remote_objects();
  1028. ppeer->set_stream_peer(Ref<StreamPeer>());
  1029. if (connection.is_valid()) {
  1030. EditorNode::get_log()->add_message("** Debug Process Stopped **");
  1031. connection.unref();
  1032. reason->set_text("");
  1033. reason->set_tooltip("");
  1034. }
  1035. pending_in_queue = 0;
  1036. message.clear();
  1037. node_path_cache.clear();
  1038. res_path_cache.clear();
  1039. profiler_signature.clear();
  1040. le_clear->set_disabled(false);
  1041. le_set->set_disabled(true);
  1042. profiler->set_enabled(true);
  1043. inspect_scene_tree->clear();
  1044. EditorNode::get_singleton()->get_pause_button()->set_pressed(false);
  1045. EditorNode::get_singleton()->get_pause_button()->set_disabled(true);
  1046. EditorNode::get_singleton()->get_scene_tree_dock()->hide_remote_tree();
  1047. EditorNode::get_singleton()->get_scene_tree_dock()->hide_tab_buttons();
  1048. if (hide_on_stop) {
  1049. if (is_visible_in_tree())
  1050. EditorNode::get_singleton()->hide_bottom_panel();
  1051. emit_signal("show_debugger", false);
  1052. }
  1053. }
  1054. void ScriptEditorDebugger::_profiler_activate(bool p_enable) {
  1055. if (!connection.is_valid())
  1056. return;
  1057. if (p_enable) {
  1058. profiler_signature.clear();
  1059. Array msg;
  1060. msg.push_back("start_profiling");
  1061. int max_funcs = EditorSettings::get_singleton()->get("debugger/profiler_frame_max_functions");
  1062. max_funcs = CLAMP(max_funcs, 16, 512);
  1063. msg.push_back(max_funcs);
  1064. ppeer->put_var(msg);
  1065. print_verbose("Starting profiling.");
  1066. } else {
  1067. Array msg;
  1068. msg.push_back("stop_profiling");
  1069. ppeer->put_var(msg);
  1070. print_verbose("Ending profiling.");
  1071. }
  1072. }
  1073. void ScriptEditorDebugger::_profiler_seeked() {
  1074. if (!connection.is_valid() || !connection->is_connected_to_host())
  1075. return;
  1076. if (breaked)
  1077. return;
  1078. debug_break();
  1079. }
  1080. void ScriptEditorDebugger::_stack_dump_frame_selected() {
  1081. TreeItem *ti = stack_dump->get_selected();
  1082. if (!ti)
  1083. return;
  1084. Dictionary d = ti->get_metadata(0);
  1085. stack_script = ResourceLoader::load(d["file"]);
  1086. emit_signal("goto_script_line", stack_script, int(d["line"]) - 1);
  1087. stack_script.unref();
  1088. if (connection.is_valid() && connection->is_connected_to_host()) {
  1089. Array msg;
  1090. msg.push_back("get_stack_frame_vars");
  1091. msg.push_back(d["frame"]);
  1092. ppeer->put_var(msg);
  1093. } else {
  1094. inspector->edit(NULL);
  1095. }
  1096. }
  1097. void ScriptEditorDebugger::_output_clear() {
  1098. //output->clear();
  1099. //output->push_color(Color(0,0,0));
  1100. }
  1101. String ScriptEditorDebugger::get_var_value(const String &p_var) const {
  1102. if (!breaked)
  1103. return String();
  1104. return variables->get_var_value(p_var);
  1105. }
  1106. int ScriptEditorDebugger::_get_node_path_cache(const NodePath &p_path) {
  1107. const int *r = node_path_cache.getptr(p_path);
  1108. if (r)
  1109. return *r;
  1110. last_path_id++;
  1111. node_path_cache[p_path] = last_path_id;
  1112. Array msg;
  1113. msg.push_back("live_node_path");
  1114. msg.push_back(p_path);
  1115. msg.push_back(last_path_id);
  1116. ppeer->put_var(msg);
  1117. return last_path_id;
  1118. }
  1119. int ScriptEditorDebugger::_get_res_path_cache(const String &p_path) {
  1120. Map<String, int>::Element *E = res_path_cache.find(p_path);
  1121. if (E)
  1122. return E->get();
  1123. last_path_id++;
  1124. res_path_cache[p_path] = last_path_id;
  1125. Array msg;
  1126. msg.push_back("live_res_path");
  1127. msg.push_back(p_path);
  1128. msg.push_back(last_path_id);
  1129. ppeer->put_var(msg);
  1130. return last_path_id;
  1131. }
  1132. void ScriptEditorDebugger::_method_changed(Object *p_base, const StringName &p_name, VARIANT_ARG_DECLARE) {
  1133. if (!p_base || !live_debug || !connection.is_valid() || !editor->get_edited_scene())
  1134. return;
  1135. Node *node = Object::cast_to<Node>(p_base);
  1136. VARIANT_ARGPTRS
  1137. for (int i = 0; i < VARIANT_ARG_MAX; i++) {
  1138. //no pointers, sorry
  1139. if (argptr[i] && (argptr[i]->get_type() == Variant::OBJECT || argptr[i]->get_type() == Variant::_RID))
  1140. return;
  1141. }
  1142. if (node) {
  1143. NodePath path = editor->get_edited_scene()->get_path_to(node);
  1144. int pathid = _get_node_path_cache(path);
  1145. Array msg;
  1146. msg.push_back("live_node_call");
  1147. msg.push_back(pathid);
  1148. msg.push_back(p_name);
  1149. for (int i = 0; i < VARIANT_ARG_MAX; i++) {
  1150. //no pointers, sorry
  1151. msg.push_back(*argptr[i]);
  1152. }
  1153. ppeer->put_var(msg);
  1154. return;
  1155. }
  1156. Resource *res = Object::cast_to<Resource>(p_base);
  1157. if (res && res->get_path() != String()) {
  1158. String respath = res->get_path();
  1159. int pathid = _get_res_path_cache(respath);
  1160. Array msg;
  1161. msg.push_back("live_res_call");
  1162. msg.push_back(pathid);
  1163. msg.push_back(p_name);
  1164. for (int i = 0; i < VARIANT_ARG_MAX; i++) {
  1165. //no pointers, sorry
  1166. msg.push_back(*argptr[i]);
  1167. }
  1168. ppeer->put_var(msg);
  1169. return;
  1170. }
  1171. }
  1172. void ScriptEditorDebugger::_property_changed(Object *p_base, const StringName &p_property, const Variant &p_value) {
  1173. if (!p_base || !live_debug || !connection.is_valid() || !editor->get_edited_scene())
  1174. return;
  1175. Node *node = Object::cast_to<Node>(p_base);
  1176. if (node) {
  1177. NodePath path = editor->get_edited_scene()->get_path_to(node);
  1178. int pathid = _get_node_path_cache(path);
  1179. if (p_value.is_ref()) {
  1180. Ref<Resource> res = p_value;
  1181. if (res.is_valid() && res->get_path() != String()) {
  1182. Array msg;
  1183. msg.push_back("live_node_prop_res");
  1184. msg.push_back(pathid);
  1185. msg.push_back(p_property);
  1186. msg.push_back(res->get_path());
  1187. ppeer->put_var(msg);
  1188. }
  1189. } else {
  1190. Array msg;
  1191. msg.push_back("live_node_prop");
  1192. msg.push_back(pathid);
  1193. msg.push_back(p_property);
  1194. msg.push_back(p_value);
  1195. ppeer->put_var(msg);
  1196. }
  1197. return;
  1198. }
  1199. Resource *res = Object::cast_to<Resource>(p_base);
  1200. if (res && res->get_path() != String()) {
  1201. String respath = res->get_path();
  1202. int pathid = _get_res_path_cache(respath);
  1203. if (p_value.is_ref()) {
  1204. Ref<Resource> res = p_value;
  1205. if (res.is_valid() && res->get_path() != String()) {
  1206. Array msg;
  1207. msg.push_back("live_res_prop_res");
  1208. msg.push_back(pathid);
  1209. msg.push_back(p_property);
  1210. msg.push_back(res->get_path());
  1211. ppeer->put_var(msg);
  1212. }
  1213. } else {
  1214. Array msg;
  1215. msg.push_back("live_res_prop");
  1216. msg.push_back(pathid);
  1217. msg.push_back(p_property);
  1218. msg.push_back(p_value);
  1219. ppeer->put_var(msg);
  1220. }
  1221. return;
  1222. }
  1223. }
  1224. void ScriptEditorDebugger::_method_changeds(void *p_ud, Object *p_base, const StringName &p_name, VARIANT_ARG_DECLARE) {
  1225. ScriptEditorDebugger *sed = (ScriptEditorDebugger *)p_ud;
  1226. sed->_method_changed(p_base, p_name, VARIANT_ARG_PASS);
  1227. }
  1228. void ScriptEditorDebugger::_property_changeds(void *p_ud, Object *p_base, const StringName &p_property, const Variant &p_value) {
  1229. ScriptEditorDebugger *sed = (ScriptEditorDebugger *)p_ud;
  1230. sed->_property_changed(p_base, p_property, p_value);
  1231. }
  1232. void ScriptEditorDebugger::set_live_debugging(bool p_enable) {
  1233. live_debug = p_enable;
  1234. }
  1235. void ScriptEditorDebugger::_live_edit_set() {
  1236. if (!connection.is_valid())
  1237. return;
  1238. TreeItem *ti = inspect_scene_tree->get_selected();
  1239. if (!ti)
  1240. return;
  1241. String path;
  1242. while (ti) {
  1243. String lp = ti->get_text(0);
  1244. path = "/" + lp + path;
  1245. ti = ti->get_parent();
  1246. }
  1247. NodePath np = path;
  1248. editor->get_editor_data().set_edited_scene_live_edit_root(np);
  1249. update_live_edit_root();
  1250. }
  1251. void ScriptEditorDebugger::_live_edit_clear() {
  1252. NodePath np = NodePath("/root");
  1253. editor->get_editor_data().set_edited_scene_live_edit_root(np);
  1254. update_live_edit_root();
  1255. }
  1256. void ScriptEditorDebugger::update_live_edit_root() {
  1257. NodePath np = editor->get_editor_data().get_edited_scene_live_edit_root();
  1258. if (connection.is_valid()) {
  1259. Array msg;
  1260. msg.push_back("live_set_root");
  1261. msg.push_back(np);
  1262. if (editor->get_edited_scene())
  1263. msg.push_back(editor->get_edited_scene()->get_filename());
  1264. else
  1265. msg.push_back("");
  1266. ppeer->put_var(msg);
  1267. }
  1268. live_edit_root->set_text(np);
  1269. }
  1270. void ScriptEditorDebugger::live_debug_create_node(const NodePath &p_parent, const String &p_type, const String &p_name) {
  1271. if (live_debug && connection.is_valid()) {
  1272. Array msg;
  1273. msg.push_back("live_create_node");
  1274. msg.push_back(p_parent);
  1275. msg.push_back(p_type);
  1276. msg.push_back(p_name);
  1277. ppeer->put_var(msg);
  1278. }
  1279. }
  1280. void ScriptEditorDebugger::live_debug_instance_node(const NodePath &p_parent, const String &p_path, const String &p_name) {
  1281. if (live_debug && connection.is_valid()) {
  1282. Array msg;
  1283. msg.push_back("live_instance_node");
  1284. msg.push_back(p_parent);
  1285. msg.push_back(p_path);
  1286. msg.push_back(p_name);
  1287. ppeer->put_var(msg);
  1288. }
  1289. }
  1290. void ScriptEditorDebugger::live_debug_remove_node(const NodePath &p_at) {
  1291. if (live_debug && connection.is_valid()) {
  1292. Array msg;
  1293. msg.push_back("live_remove_node");
  1294. msg.push_back(p_at);
  1295. ppeer->put_var(msg);
  1296. }
  1297. }
  1298. void ScriptEditorDebugger::live_debug_remove_and_keep_node(const NodePath &p_at, ObjectID p_keep_id) {
  1299. if (live_debug && connection.is_valid()) {
  1300. Array msg;
  1301. msg.push_back("live_remove_and_keep_node");
  1302. msg.push_back(p_at);
  1303. msg.push_back(p_keep_id);
  1304. ppeer->put_var(msg);
  1305. }
  1306. }
  1307. void ScriptEditorDebugger::live_debug_restore_node(ObjectID p_id, const NodePath &p_at, int p_at_pos) {
  1308. if (live_debug && connection.is_valid()) {
  1309. Array msg;
  1310. msg.push_back("live_restore_node");
  1311. msg.push_back(p_id);
  1312. msg.push_back(p_at);
  1313. msg.push_back(p_at_pos);
  1314. ppeer->put_var(msg);
  1315. }
  1316. }
  1317. void ScriptEditorDebugger::live_debug_duplicate_node(const NodePath &p_at, const String &p_new_name) {
  1318. if (live_debug && connection.is_valid()) {
  1319. Array msg;
  1320. msg.push_back("live_duplicate_node");
  1321. msg.push_back(p_at);
  1322. msg.push_back(p_new_name);
  1323. ppeer->put_var(msg);
  1324. }
  1325. }
  1326. void ScriptEditorDebugger::live_debug_reparent_node(const NodePath &p_at, const NodePath &p_new_place, const String &p_new_name, int p_at_pos) {
  1327. if (live_debug && connection.is_valid()) {
  1328. Array msg;
  1329. msg.push_back("live_reparent_node");
  1330. msg.push_back(p_at);
  1331. msg.push_back(p_new_place);
  1332. msg.push_back(p_new_name);
  1333. msg.push_back(p_at_pos);
  1334. ppeer->put_var(msg);
  1335. }
  1336. }
  1337. void ScriptEditorDebugger::set_breakpoint(const String &p_path, int p_line, bool p_enabled) {
  1338. if (connection.is_valid()) {
  1339. Array msg;
  1340. msg.push_back("breakpoint");
  1341. msg.push_back(p_path);
  1342. msg.push_back(p_line);
  1343. msg.push_back(p_enabled);
  1344. ppeer->put_var(msg);
  1345. }
  1346. }
  1347. void ScriptEditorDebugger::reload_scripts() {
  1348. if (connection.is_valid()) {
  1349. Array msg;
  1350. msg.push_back("reload_scripts");
  1351. ppeer->put_var(msg);
  1352. }
  1353. }
  1354. void ScriptEditorDebugger::_error_activated() {
  1355. TreeItem *selected = error_tree->get_selected();
  1356. TreeItem *ci = selected->get_children();
  1357. if (ci) {
  1358. selected->set_collapsed(!selected->is_collapsed());
  1359. }
  1360. }
  1361. void ScriptEditorDebugger::_error_selected() {
  1362. TreeItem *selected = error_tree->get_selected();
  1363. Array meta = selected->get_metadata(0);
  1364. if (meta.size() == 0) {
  1365. return;
  1366. }
  1367. Ref<Script> s = ResourceLoader::load(meta[0]);
  1368. emit_signal("goto_script_line", s, int(meta[1]) - 1);
  1369. }
  1370. void ScriptEditorDebugger::_expand_errors_list() {
  1371. TreeItem *root = error_tree->get_root();
  1372. if (!root)
  1373. return;
  1374. TreeItem *item = root->get_children();
  1375. while (item) {
  1376. item->set_collapsed(false);
  1377. item = item->get_next();
  1378. }
  1379. }
  1380. void ScriptEditorDebugger::_collapse_errors_list() {
  1381. TreeItem *root = error_tree->get_root();
  1382. if (!root)
  1383. return;
  1384. TreeItem *item = root->get_children();
  1385. while (item) {
  1386. item->set_collapsed(true);
  1387. item = item->get_next();
  1388. }
  1389. }
  1390. void ScriptEditorDebugger::set_hide_on_stop(bool p_hide) {
  1391. hide_on_stop = p_hide;
  1392. }
  1393. bool ScriptEditorDebugger::get_debug_with_external_editor() const {
  1394. return enable_external_editor;
  1395. }
  1396. void ScriptEditorDebugger::set_debug_with_external_editor(bool p_enabled) {
  1397. enable_external_editor = p_enabled;
  1398. }
  1399. Ref<Script> ScriptEditorDebugger::get_dump_stack_script() const {
  1400. return stack_script;
  1401. }
  1402. void ScriptEditorDebugger::_paused() {
  1403. ERR_FAIL_COND(connection.is_null());
  1404. ERR_FAIL_COND(!connection->is_connected_to_host());
  1405. if (!breaked && EditorNode::get_singleton()->get_pause_button()->is_pressed()) {
  1406. debug_break();
  1407. }
  1408. if (breaked && !EditorNode::get_singleton()->get_pause_button()->is_pressed()) {
  1409. debug_continue();
  1410. }
  1411. }
  1412. void ScriptEditorDebugger::_set_remote_object(ObjectID p_id, ScriptEditorDebuggerInspectedObject *p_obj) {
  1413. if (remote_objects.has(p_id))
  1414. memdelete(remote_objects[p_id]);
  1415. remote_objects[p_id] = p_obj;
  1416. }
  1417. void ScriptEditorDebugger::_clear_remote_objects() {
  1418. for (Map<ObjectID, ScriptEditorDebuggerInspectedObject *>::Element *E = remote_objects.front(); E; E = E->next()) {
  1419. if (editor->get_editor_history()->get_current() == E->value()->get_instance_id()) {
  1420. editor->push_item(NULL);
  1421. }
  1422. memdelete(E->value());
  1423. }
  1424. remote_objects.clear();
  1425. }
  1426. void ScriptEditorDebugger::_clear_errors_list() {
  1427. error_tree->clear();
  1428. error_count = 0;
  1429. warning_count = 0;
  1430. _notification(NOTIFICATION_PROCESS);
  1431. }
  1432. // Right click on specific file(s) or folder(s).
  1433. void ScriptEditorDebugger::_error_tree_item_rmb_selected(const Vector2 &p_pos) {
  1434. item_menu->clear();
  1435. item_menu->set_size(Size2(1, 1));
  1436. if (error_tree->is_anything_selected()) {
  1437. item_menu->add_icon_item(get_icon("ActionCopy", "EditorIcons"), TTR("Copy Error"), ITEM_MENU_COPY_ERROR);
  1438. }
  1439. if (item_menu->get_item_count() > 0) {
  1440. item_menu->set_position(error_tree->get_global_position() + p_pos);
  1441. item_menu->popup();
  1442. }
  1443. }
  1444. void ScriptEditorDebugger::_item_menu_id_pressed(int p_option) {
  1445. switch (p_option) {
  1446. case ITEM_MENU_COPY_ERROR: {
  1447. TreeItem *ti = error_tree->get_selected();
  1448. while (ti->get_parent() != error_tree->get_root())
  1449. ti = ti->get_parent();
  1450. String type;
  1451. if (ti->get_icon(0) == get_icon("Warning", "EditorIcons")) {
  1452. type = "W ";
  1453. } else if (ti->get_icon(0) == get_icon("Error", "EditorIcons")) {
  1454. type = "E ";
  1455. }
  1456. String text = ti->get_text(0) + " ";
  1457. int rpad_len = text.length();
  1458. text = type + text + ti->get_text(1) + "\n";
  1459. TreeItem *ci = ti->get_children();
  1460. while (ci) {
  1461. text += " " + ci->get_text(0).rpad(rpad_len) + ci->get_text(1) + "\n";
  1462. ci = ci->get_next();
  1463. }
  1464. OS::get_singleton()->set_clipboard(text);
  1465. } break;
  1466. case ITEM_MENU_SAVE_REMOTE_NODE: {
  1467. file_dialog->set_access(EditorFileDialog::ACCESS_RESOURCES);
  1468. file_dialog->set_mode(EditorFileDialog::MODE_SAVE_FILE);
  1469. List<String> extensions;
  1470. Ref<PackedScene> sd = memnew(PackedScene);
  1471. ResourceSaver::get_recognized_extensions(sd, &extensions);
  1472. file_dialog->clear_filters();
  1473. for (int i = 0; i < extensions.size(); i++) {
  1474. file_dialog->add_filter("*." + extensions[i] + " ; " + extensions[i].to_upper());
  1475. }
  1476. file_dialog->popup_centered_ratio();
  1477. } break;
  1478. }
  1479. }
  1480. void ScriptEditorDebugger::_bind_methods() {
  1481. ClassDB::bind_method(D_METHOD("_stack_dump_frame_selected"), &ScriptEditorDebugger::_stack_dump_frame_selected);
  1482. ClassDB::bind_method(D_METHOD("debug_copy"), &ScriptEditorDebugger::debug_copy);
  1483. ClassDB::bind_method(D_METHOD("debug_next"), &ScriptEditorDebugger::debug_next);
  1484. ClassDB::bind_method(D_METHOD("debug_step"), &ScriptEditorDebugger::debug_step);
  1485. ClassDB::bind_method(D_METHOD("debug_break"), &ScriptEditorDebugger::debug_break);
  1486. ClassDB::bind_method(D_METHOD("debug_continue"), &ScriptEditorDebugger::debug_continue);
  1487. ClassDB::bind_method(D_METHOD("_output_clear"), &ScriptEditorDebugger::_output_clear);
  1488. ClassDB::bind_method(D_METHOD("_performance_draw"), &ScriptEditorDebugger::_performance_draw);
  1489. ClassDB::bind_method(D_METHOD("_performance_select"), &ScriptEditorDebugger::_performance_select);
  1490. ClassDB::bind_method(D_METHOD("_scene_tree_request"), &ScriptEditorDebugger::_scene_tree_request);
  1491. ClassDB::bind_method(D_METHOD("_video_mem_request"), &ScriptEditorDebugger::_video_mem_request);
  1492. ClassDB::bind_method(D_METHOD("_live_edit_set"), &ScriptEditorDebugger::_live_edit_set);
  1493. ClassDB::bind_method(D_METHOD("_live_edit_clear"), &ScriptEditorDebugger::_live_edit_clear);
  1494. ClassDB::bind_method(D_METHOD("_error_selected"), &ScriptEditorDebugger::_error_selected);
  1495. ClassDB::bind_method(D_METHOD("_error_activated"), &ScriptEditorDebugger::_error_activated);
  1496. ClassDB::bind_method(D_METHOD("_expand_errors_list"), &ScriptEditorDebugger::_expand_errors_list);
  1497. ClassDB::bind_method(D_METHOD("_collapse_errors_list"), &ScriptEditorDebugger::_collapse_errors_list);
  1498. ClassDB::bind_method(D_METHOD("_profiler_activate"), &ScriptEditorDebugger::_profiler_activate);
  1499. ClassDB::bind_method(D_METHOD("_profiler_seeked"), &ScriptEditorDebugger::_profiler_seeked);
  1500. ClassDB::bind_method(D_METHOD("_clear_errors_list"), &ScriptEditorDebugger::_clear_errors_list);
  1501. ClassDB::bind_method(D_METHOD("_error_tree_item_rmb_selected"), &ScriptEditorDebugger::_error_tree_item_rmb_selected);
  1502. ClassDB::bind_method(D_METHOD("_item_menu_id_pressed"), &ScriptEditorDebugger::_item_menu_id_pressed);
  1503. ClassDB::bind_method(D_METHOD("_paused"), &ScriptEditorDebugger::_paused);
  1504. ClassDB::bind_method(D_METHOD("_scene_tree_selected"), &ScriptEditorDebugger::_scene_tree_selected);
  1505. ClassDB::bind_method(D_METHOD("_scene_tree_folded"), &ScriptEditorDebugger::_scene_tree_folded);
  1506. ClassDB::bind_method(D_METHOD("_scene_tree_rmb_selected"), &ScriptEditorDebugger::_scene_tree_rmb_selected);
  1507. ClassDB::bind_method(D_METHOD("_file_selected"), &ScriptEditorDebugger::_file_selected);
  1508. ClassDB::bind_method(D_METHOD("live_debug_create_node"), &ScriptEditorDebugger::live_debug_create_node);
  1509. ClassDB::bind_method(D_METHOD("live_debug_instance_node"), &ScriptEditorDebugger::live_debug_instance_node);
  1510. ClassDB::bind_method(D_METHOD("live_debug_remove_node"), &ScriptEditorDebugger::live_debug_remove_node);
  1511. ClassDB::bind_method(D_METHOD("live_debug_remove_and_keep_node"), &ScriptEditorDebugger::live_debug_remove_and_keep_node);
  1512. ClassDB::bind_method(D_METHOD("live_debug_restore_node"), &ScriptEditorDebugger::live_debug_restore_node);
  1513. ClassDB::bind_method(D_METHOD("live_debug_duplicate_node"), &ScriptEditorDebugger::live_debug_duplicate_node);
  1514. ClassDB::bind_method(D_METHOD("live_debug_reparent_node"), &ScriptEditorDebugger::live_debug_reparent_node);
  1515. ClassDB::bind_method(D_METHOD("_scene_tree_property_select_object"), &ScriptEditorDebugger::_scene_tree_property_select_object);
  1516. ClassDB::bind_method(D_METHOD("_scene_tree_property_value_edited"), &ScriptEditorDebugger::_scene_tree_property_value_edited);
  1517. ADD_SIGNAL(MethodInfo("goto_script_line"));
  1518. ADD_SIGNAL(MethodInfo("breaked", PropertyInfo(Variant::BOOL, "reallydid"), PropertyInfo(Variant::BOOL, "can_debug")));
  1519. ADD_SIGNAL(MethodInfo("show_debugger", PropertyInfo(Variant::BOOL, "reallydid")));
  1520. }
  1521. ScriptEditorDebugger::ScriptEditorDebugger(EditorNode *p_editor) {
  1522. ppeer = Ref<PacketPeerStream>(memnew(PacketPeerStream));
  1523. ppeer->set_input_buffer_max_size(1024 * 1024 * 8); //8mb should be enough
  1524. editor = p_editor;
  1525. editor->get_inspector()->connect("object_id_selected", this, "_scene_tree_property_select_object");
  1526. tabs = memnew(TabContainer);
  1527. tabs->set_tab_align(TabContainer::ALIGN_LEFT);
  1528. tabs->add_style_override("panel", editor->get_gui_base()->get_stylebox("DebuggerPanel", "EditorStyles"));
  1529. tabs->add_style_override("tab_fg", editor->get_gui_base()->get_stylebox("DebuggerTabFG", "EditorStyles"));
  1530. tabs->add_style_override("tab_bg", editor->get_gui_base()->get_stylebox("DebuggerTabBG", "EditorStyles"));
  1531. tabs->set_anchors_and_margins_preset(Control::PRESET_WIDE);
  1532. tabs->set_margin(MARGIN_LEFT, -editor->get_gui_base()->get_stylebox("BottomPanelDebuggerOverride", "EditorStyles")->get_margin(MARGIN_LEFT));
  1533. tabs->set_margin(MARGIN_RIGHT, editor->get_gui_base()->get_stylebox("BottomPanelDebuggerOverride", "EditorStyles")->get_margin(MARGIN_RIGHT));
  1534. add_child(tabs);
  1535. { //debugger
  1536. VBoxContainer *vbc = memnew(VBoxContainer);
  1537. vbc->set_name(TTR("Debugger"));
  1538. //tabs->add_child(vbc);
  1539. Control *dbg = vbc;
  1540. HBoxContainer *hbc = memnew(HBoxContainer);
  1541. vbc->add_child(hbc);
  1542. reason = memnew(Label);
  1543. reason->set_text("");
  1544. hbc->add_child(reason);
  1545. reason->set_h_size_flags(SIZE_EXPAND_FILL);
  1546. reason->set_autowrap(true);
  1547. reason->set_max_lines_visible(3);
  1548. reason->set_mouse_filter(Control::MOUSE_FILTER_PASS);
  1549. hbc->add_child(memnew(VSeparator));
  1550. copy = memnew(ToolButton);
  1551. hbc->add_child(copy);
  1552. copy->set_tooltip(TTR("Copy Error"));
  1553. copy->connect("pressed", this, "debug_copy");
  1554. hbc->add_child(memnew(VSeparator));
  1555. step = memnew(ToolButton);
  1556. hbc->add_child(step);
  1557. step->set_tooltip(TTR("Step Into"));
  1558. step->connect("pressed", this, "debug_step");
  1559. next = memnew(ToolButton);
  1560. hbc->add_child(next);
  1561. next->set_tooltip(TTR("Step Over"));
  1562. next->connect("pressed", this, "debug_next");
  1563. hbc->add_child(memnew(VSeparator));
  1564. dobreak = memnew(ToolButton);
  1565. hbc->add_child(dobreak);
  1566. dobreak->set_tooltip(TTR("Break"));
  1567. dobreak->connect("pressed", this, "debug_break");
  1568. docontinue = memnew(ToolButton);
  1569. hbc->add_child(docontinue);
  1570. docontinue->set_tooltip(TTR("Continue"));
  1571. docontinue->connect("pressed", this, "debug_continue");
  1572. back = memnew(Button);
  1573. hbc->add_child(back);
  1574. back->set_tooltip(TTR("Inspect Previous Instance"));
  1575. back->hide();
  1576. forward = memnew(Button);
  1577. hbc->add_child(forward);
  1578. forward->set_tooltip(TTR("Inspect Next Instance"));
  1579. forward->hide();
  1580. HSplitContainer *sc = memnew(HSplitContainer);
  1581. vbc->add_child(sc);
  1582. sc->set_v_size_flags(SIZE_EXPAND_FILL);
  1583. stack_dump = memnew(Tree);
  1584. stack_dump->set_allow_reselect(true);
  1585. stack_dump->set_columns(1);
  1586. stack_dump->set_column_titles_visible(true);
  1587. stack_dump->set_column_title(0, TTR("Stack Frames"));
  1588. stack_dump->set_h_size_flags(SIZE_EXPAND_FILL);
  1589. stack_dump->set_hide_root(true);
  1590. stack_dump->connect("cell_selected", this, "_stack_dump_frame_selected");
  1591. sc->add_child(stack_dump);
  1592. inspector = memnew(EditorInspector);
  1593. inspector->set_h_size_flags(SIZE_EXPAND_FILL);
  1594. inspector->set_enable_capitalize_paths(false);
  1595. inspector->set_read_only(true);
  1596. inspector->connect("object_id_selected", this, "_scene_tree_property_select_object");
  1597. sc->add_child(inspector);
  1598. server.instance();
  1599. pending_in_queue = 0;
  1600. variables = memnew(ScriptEditorDebuggerVariables);
  1601. breaked = false;
  1602. tabs->add_child(dbg);
  1603. }
  1604. { //errors
  1605. VBoxContainer *errvb = memnew(VBoxContainer);
  1606. errvb->set_name(TTR("Errors"));
  1607. HBoxContainer *errhb = memnew(HBoxContainer);
  1608. errvb->add_child(errhb);
  1609. Button *expand_all = memnew(Button);
  1610. expand_all->set_text(TTR("Expand All"));
  1611. expand_all->connect("pressed", this, "_expand_errors_list");
  1612. errhb->add_child(expand_all);
  1613. Button *collapse_all = memnew(Button);
  1614. collapse_all->set_text(TTR("Collapse All"));
  1615. collapse_all->connect("pressed", this, "_collapse_errors_list");
  1616. errhb->add_child(collapse_all);
  1617. Control *space = memnew(Control);
  1618. space->set_h_size_flags(SIZE_EXPAND_FILL);
  1619. errhb->add_child(space);
  1620. clearbutton = memnew(Button);
  1621. clearbutton->set_text(TTR("Clear"));
  1622. clearbutton->set_h_size_flags(0);
  1623. clearbutton->connect("pressed", this, "_clear_errors_list");
  1624. errhb->add_child(clearbutton);
  1625. error_tree = memnew(Tree);
  1626. error_tree->set_columns(2);
  1627. error_tree->set_column_expand(0, false);
  1628. error_tree->set_column_min_width(0, 140);
  1629. error_tree->set_column_expand(1, true);
  1630. error_tree->set_select_mode(Tree::SELECT_ROW);
  1631. error_tree->set_hide_root(true);
  1632. error_tree->set_v_size_flags(SIZE_EXPAND_FILL);
  1633. error_tree->set_allow_rmb_select(true);
  1634. error_tree->connect("item_rmb_selected", this, "_error_tree_item_rmb_selected");
  1635. errvb->add_child(error_tree);
  1636. item_menu = memnew(PopupMenu);
  1637. item_menu->connect("id_pressed", this, "_item_menu_id_pressed");
  1638. error_tree->add_child(item_menu);
  1639. tabs->add_child(errvb);
  1640. }
  1641. { // remote scene tree
  1642. inspect_scene_tree = memnew(Tree);
  1643. EditorNode::get_singleton()->get_scene_tree_dock()->add_remote_tree_editor(inspect_scene_tree);
  1644. EditorNode::get_singleton()->get_scene_tree_dock()->connect("remote_tree_selected", this, "_scene_tree_selected");
  1645. inspect_scene_tree->set_v_size_flags(SIZE_EXPAND_FILL);
  1646. inspect_scene_tree->connect("cell_selected", this, "_scene_tree_selected");
  1647. inspect_scene_tree->connect("item_collapsed", this, "_scene_tree_folded");
  1648. inspect_scene_tree->set_allow_rmb_select(true);
  1649. inspect_scene_tree->connect("item_rmb_selected", this, "_scene_tree_rmb_selected");
  1650. auto_switch_remote_scene_tree = EDITOR_DEF("debugger/auto_switch_to_remote_scene_tree", false);
  1651. inspect_scene_tree_timeout = EDITOR_DEF("debugger/remote_scene_tree_refresh_interval", 1.0);
  1652. inspect_edited_object_timeout = EDITOR_DEF("debugger/remote_inspect_refresh_interval", 0.2);
  1653. inspected_object_id = 0;
  1654. updating_scene_tree = false;
  1655. }
  1656. { // File dialog
  1657. file_dialog = memnew(EditorFileDialog);
  1658. file_dialog->connect("file_selected", this, "_file_selected");
  1659. add_child(file_dialog);
  1660. }
  1661. { //profiler
  1662. profiler = memnew(EditorProfiler);
  1663. profiler->set_name(TTR("Profiler"));
  1664. tabs->add_child(profiler);
  1665. profiler->connect("enable_profiling", this, "_profiler_activate");
  1666. profiler->connect("break_request", this, "_profiler_seeked");
  1667. }
  1668. { //monitors
  1669. HSplitContainer *hsp = memnew(HSplitContainer);
  1670. perf_monitors = memnew(Tree);
  1671. perf_monitors->set_columns(2);
  1672. perf_monitors->set_column_title(0, TTR("Monitor"));
  1673. perf_monitors->set_column_title(1, TTR("Value"));
  1674. perf_monitors->set_column_titles_visible(true);
  1675. hsp->add_child(perf_monitors);
  1676. perf_monitors->connect("item_edited", this, "_performance_select");
  1677. perf_draw = memnew(Control);
  1678. perf_draw->connect("draw", this, "_performance_draw");
  1679. hsp->add_child(perf_draw);
  1680. hsp->set_name(TTR("Monitors"));
  1681. hsp->set_split_offset(340 * EDSCALE);
  1682. tabs->add_child(hsp);
  1683. perf_max.resize(Performance::MONITOR_MAX);
  1684. Map<String, TreeItem *> bases;
  1685. TreeItem *root = perf_monitors->create_item();
  1686. perf_monitors->set_hide_root(true);
  1687. for (int i = 0; i < Performance::MONITOR_MAX; i++) {
  1688. String n = Performance::get_singleton()->get_monitor_name(Performance::Monitor(i));
  1689. Performance::MonitorType mtype = Performance::get_singleton()->get_monitor_type(Performance::Monitor(i));
  1690. String base = n.get_slice("/", 0);
  1691. String name = n.get_slice("/", 1);
  1692. if (!bases.has(base)) {
  1693. TreeItem *b = perf_monitors->create_item(root);
  1694. b->set_text(0, base.capitalize());
  1695. b->set_editable(0, false);
  1696. b->set_selectable(0, false);
  1697. b->set_expand_right(0, true);
  1698. bases[base] = b;
  1699. }
  1700. TreeItem *it = perf_monitors->create_item(bases[base]);
  1701. it->set_metadata(1, mtype);
  1702. it->set_cell_mode(0, TreeItem::CELL_MODE_CHECK);
  1703. it->set_editable(0, true);
  1704. it->set_selectable(0, false);
  1705. it->set_selectable(1, false);
  1706. it->set_text(0, name.capitalize());
  1707. perf_items.push_back(it);
  1708. perf_max.write[i] = 0;
  1709. }
  1710. }
  1711. { //vmem inspect
  1712. VBoxContainer *vmem_vb = memnew(VBoxContainer);
  1713. HBoxContainer *vmem_hb = memnew(HBoxContainer);
  1714. Label *vmlb = memnew(Label(TTR("List of Video Memory Usage by Resource:") + " "));
  1715. vmlb->set_h_size_flags(SIZE_EXPAND_FILL);
  1716. vmem_hb->add_child(vmlb);
  1717. vmem_hb->add_child(memnew(Label(TTR("Total:") + " ")));
  1718. vmem_total = memnew(LineEdit);
  1719. vmem_total->set_editable(false);
  1720. vmem_total->set_custom_minimum_size(Size2(100, 1) * EDSCALE);
  1721. vmem_hb->add_child(vmem_total);
  1722. vmem_refresh = memnew(ToolButton);
  1723. vmem_hb->add_child(vmem_refresh);
  1724. vmem_vb->add_child(vmem_hb);
  1725. vmem_refresh->connect("pressed", this, "_video_mem_request");
  1726. VBoxContainer *vmmc = memnew(VBoxContainer);
  1727. vmem_tree = memnew(Tree);
  1728. vmem_tree->set_v_size_flags(SIZE_EXPAND_FILL);
  1729. vmem_tree->set_h_size_flags(SIZE_EXPAND_FILL);
  1730. vmmc->add_child(vmem_tree);
  1731. vmmc->set_v_size_flags(SIZE_EXPAND_FILL);
  1732. vmem_vb->add_child(vmmc);
  1733. vmem_vb->set_name(TTR("Video Mem"));
  1734. vmem_tree->set_columns(4);
  1735. vmem_tree->set_column_titles_visible(true);
  1736. vmem_tree->set_column_title(0, TTR("Resource Path"));
  1737. vmem_tree->set_column_expand(0, true);
  1738. vmem_tree->set_column_expand(1, false);
  1739. vmem_tree->set_column_title(1, TTR("Type"));
  1740. vmem_tree->set_column_min_width(1, 100);
  1741. vmem_tree->set_column_expand(2, false);
  1742. vmem_tree->set_column_title(2, TTR("Format"));
  1743. vmem_tree->set_column_min_width(2, 150);
  1744. vmem_tree->set_column_expand(3, false);
  1745. vmem_tree->set_column_title(3, TTR("Usage"));
  1746. vmem_tree->set_column_min_width(3, 80);
  1747. vmem_tree->set_hide_root(true);
  1748. tabs->add_child(vmem_vb);
  1749. }
  1750. { // misc
  1751. GridContainer *info_left = memnew(GridContainer);
  1752. info_left->set_columns(2);
  1753. info_left->set_name(TTR("Misc"));
  1754. tabs->add_child(info_left);
  1755. clicked_ctrl = memnew(LineEdit);
  1756. clicked_ctrl->set_h_size_flags(SIZE_EXPAND_FILL);
  1757. info_left->add_child(memnew(Label(TTR("Clicked Control:"))));
  1758. info_left->add_child(clicked_ctrl);
  1759. clicked_ctrl_type = memnew(LineEdit);
  1760. info_left->add_child(memnew(Label(TTR("Clicked Control Type:"))));
  1761. info_left->add_child(clicked_ctrl_type);
  1762. live_edit_root = memnew(LineEdit);
  1763. live_edit_root->set_h_size_flags(SIZE_EXPAND_FILL);
  1764. {
  1765. HBoxContainer *lehb = memnew(HBoxContainer);
  1766. Label *l = memnew(Label(TTR("Live Edit Root:")));
  1767. info_left->add_child(l);
  1768. lehb->add_child(live_edit_root);
  1769. le_set = memnew(Button(TTR("Set From Tree")));
  1770. lehb->add_child(le_set);
  1771. le_clear = memnew(Button(TTR("Clear")));
  1772. lehb->add_child(le_clear);
  1773. info_left->add_child(lehb);
  1774. le_set->set_disabled(true);
  1775. le_clear->set_disabled(true);
  1776. }
  1777. }
  1778. msgdialog = memnew(AcceptDialog);
  1779. add_child(msgdialog);
  1780. p_editor->get_undo_redo()->set_method_notify_callback(_method_changeds, this);
  1781. p_editor->get_undo_redo()->set_property_notify_callback(_property_changeds, this);
  1782. live_debug = true;
  1783. last_path_id = false;
  1784. error_count = 0;
  1785. warning_count = 0;
  1786. hide_on_stop = true;
  1787. enable_external_editor = false;
  1788. last_error_count = 0;
  1789. last_warning_count = 0;
  1790. EditorNode::get_singleton()->get_pause_button()->connect("pressed", this, "_paused");
  1791. }
  1792. ScriptEditorDebugger::~ScriptEditorDebugger() {
  1793. memdelete(variables);
  1794. ppeer->set_stream_peer(Ref<StreamPeer>());
  1795. server->stop();
  1796. _clear_remote_objects();
  1797. }