script_editor_debugger.cpp 77 KB

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