connections_dialog.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885
  1. /*************************************************************************/
  2. /* connections_dialog.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2016 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #include "connections_dialog.h"
  30. #include "scene/gui/label.h"
  31. #include "print_string.h"
  32. #include "editor_settings.h"
  33. #include "editor_node.h"
  34. #include "plugins/script_editor_plugin.h"
  35. class ConnectDialogBinds : public Object {
  36. OBJ_TYPE( ConnectDialogBinds, Object );
  37. public:
  38. Vector<Variant> params;
  39. bool _set(const StringName& p_name, const Variant& p_value) {
  40. String name=p_name;
  41. if (name.begins_with("bind/")) {
  42. int which = name.get_slice("/",1).to_int()-1;
  43. ERR_FAIL_INDEX_V(which,params.size(),false);
  44. params[which]=p_value;
  45. } else
  46. return false;
  47. return true;
  48. }
  49. bool _get(const StringName& p_name,Variant &r_ret) const {
  50. String name=p_name;
  51. if (name.begins_with("bind/")) {
  52. int which = name.get_slice("/",1).to_int()-1;
  53. ERR_FAIL_INDEX_V(which,params.size(),false);
  54. r_ret = params[which];
  55. } else
  56. return false;
  57. return true;
  58. }
  59. void _get_property_list(List<PropertyInfo> *p_list) const {
  60. for(int i=0;i<params.size();i++) {
  61. p_list->push_back( PropertyInfo( params[i].get_type(), "bind/"+itos(i+1)) );
  62. }
  63. }
  64. void notify_changed() {
  65. _change_notify();
  66. }
  67. ConnectDialogBinds() {
  68. }
  69. };
  70. void ConnectDialog::_notification(int p_what) {
  71. if (p_what==NOTIFICATION_DRAW) {
  72. //RID ci = get_canvas_item();
  73. //get_stylebox("panel","PopupMenu")->draw(ci,Rect2(Point2(),get_size()));
  74. }
  75. if (p_what==NOTIFICATION_ENTER_TREE) {
  76. bind_editor->edit(cdbinds);
  77. }
  78. }
  79. void ConnectDialog::_tree_node_selected() {
  80. //dst_method_list->get_popup()->clear();
  81. Node *current=tree->get_selected();
  82. if (!current) {
  83. make_callback->hide();
  84. return;
  85. }
  86. if (current->get_script().is_null())
  87. make_callback->hide();
  88. else
  89. make_callback->show();
  90. #if 0
  91. List<MethodInfo> methods;
  92. current->get_method_list(&methods);
  93. for (List<MethodInfo>::Element *E=methods.front();E;E=E->next()) {
  94. if (E->get().name.length() && E->get().name[0]=='_')
  95. continue; // hidden method, not show!
  96. if (ObjectTypeDB::has_method(node->get_type(),"Node") || ObjectTypeDB::has_method(node->get_type(),"Control",true))
  97. continue; //avoid too much unnecesary stuff
  98. String method=E->get().name+"(";
  99. for(int i=0;i<E->get().arguments.size();i++) {
  100. if (i!=0)
  101. method+=", ";
  102. method+=Variant::get_type_name(E->get().arguments[i].type);
  103. if (E->get().arguments[i].name.length()) {
  104. method+=" ";
  105. method+=E->get().arguments[i].name;
  106. }
  107. }
  108. method+=")";
  109. //dst_method_list->get_popup()->add_item(method);
  110. }
  111. #endif
  112. dst_path->set_text(node->get_path_to(current));
  113. }
  114. void ConnectDialog::_dst_method_list_selected(int p_idx) {
  115. //dst_method->set_text( dst_method_list->get_popup()->get_item_text(p_idx));
  116. }
  117. void ConnectDialog::edit(Node *p_node) {
  118. node=p_node;
  119. //dst_method_list->get_popup()->clear();
  120. tree->set_selected(NULL);
  121. tree->set_marked(node,true);
  122. dst_path->set_text("");
  123. dst_method->set_text("");
  124. deferred->set_pressed(false);
  125. oneshot->set_pressed(false);
  126. cdbinds->params.clear();
  127. cdbinds->notify_changed();
  128. }
  129. void ConnectDialog::ok_pressed() {
  130. if (dst_method->get_text()=="") {
  131. error->set_text(TTR("Method in target Node must be specified!"));
  132. error->popup_centered_minsize();
  133. return;
  134. }
  135. emit_signal("connected");
  136. hide();
  137. }
  138. void ConnectDialog::_cancel_pressed() {
  139. hide();
  140. }
  141. NodePath ConnectDialog::get_dst_path() const {
  142. return dst_path->get_text();
  143. }
  144. bool ConnectDialog::get_deferred() const {
  145. return deferred->is_pressed();
  146. }
  147. bool ConnectDialog::get_oneshot() const {
  148. return oneshot->is_pressed();
  149. }
  150. StringName ConnectDialog::get_dst_method() const {
  151. String txt=dst_method->get_text();
  152. if (txt.find("(")!=-1)
  153. txt=txt.left( txt.find("(")).strip_edges();
  154. return txt;
  155. }
  156. Vector<Variant> ConnectDialog::get_binds() const {
  157. return cdbinds->params;
  158. }
  159. void ConnectDialog::_add_bind() {
  160. if (cdbinds->params.size() >= VARIANT_ARG_MAX)
  161. return;
  162. Variant::Type vt = (Variant::Type)type_list->get_item_ID(type_list->get_selected());
  163. Variant value;
  164. switch(vt) {
  165. case Variant::BOOL: value = false ; break;
  166. case Variant::INT: value = 0; break;
  167. case Variant::REAL: value = 0.0; break;
  168. case Variant::STRING: value = ""; break;
  169. case Variant::VECTOR2: value = Vector2(); break;
  170. case Variant::RECT2: value = Rect2(); break;
  171. case Variant::VECTOR3: value = Vector3(); break;
  172. case Variant::PLANE: value = Plane(); break;
  173. case Variant::QUAT: value = Quat(); break;
  174. case Variant::_AABB: value = AABB(); break;
  175. case Variant::MATRIX3: value = Matrix3(); break;
  176. case Variant::TRANSFORM: value = Transform(); break;
  177. case Variant::COLOR: value = Color(); break;
  178. case Variant::IMAGE: value = Image(); break;
  179. default: { ERR_FAIL(); } break;
  180. }
  181. ERR_FAIL_COND(value.get_type()==Variant::NIL);
  182. cdbinds->params.push_back(value);
  183. cdbinds->notify_changed();
  184. }
  185. void ConnectDialog::_remove_bind() {
  186. String st = bind_editor->get_selected_path();
  187. if (st=="")
  188. return;
  189. int idx = st.get_slice("/",1).to_int()-1;
  190. ERR_FAIL_INDEX(idx,cdbinds->params.size());
  191. cdbinds->params.remove(idx);
  192. cdbinds->notify_changed();
  193. }
  194. void ConnectDialog::set_dst_node(Node* p_node) {
  195. tree->set_selected(p_node);
  196. }
  197. void ConnectDialog::set_dst_method(const StringName& p_method) {
  198. dst_method->set_text(p_method);
  199. }
  200. void ConnectDialog::_bind_methods() {
  201. //ObjectTypeDB::bind_method("_ok",&ConnectDialog::_ok_pressed);
  202. ObjectTypeDB::bind_method("_cancel",&ConnectDialog::_cancel_pressed);
  203. //ObjectTypeDB::bind_method("_dst_method_list_selected",&ConnectDialog::_dst_method_list_selected);
  204. ObjectTypeDB::bind_method("_tree_node_selected",&ConnectDialog::_tree_node_selected);
  205. ObjectTypeDB::bind_method("_add_bind",&ConnectDialog::_add_bind);
  206. ObjectTypeDB::bind_method("_remove_bind",&ConnectDialog::_remove_bind);
  207. ADD_SIGNAL( MethodInfo("connected") );
  208. }
  209. ConnectDialog::ConnectDialog() {
  210. VBoxContainer *vbc = memnew( VBoxContainer );
  211. add_child(vbc);
  212. set_child_rect(vbc);
  213. HBoxContainer *main_hb = memnew( HBoxContainer );
  214. vbc->add_child(main_hb);
  215. main_hb->set_v_size_flags(SIZE_EXPAND_FILL);
  216. VBoxContainer *vbc_left = memnew( VBoxContainer );
  217. main_hb->add_child(vbc_left);
  218. vbc_left->set_h_size_flags(SIZE_EXPAND_FILL);
  219. tree = memnew(SceneTreeEditor(false));
  220. tree->get_scene_tree()->connect("item_activated",this,"_ok");
  221. vbc_left->add_margin_child(TTR("Connect To Node:"),tree,true);
  222. VBoxContainer *vbc_right = memnew( VBoxContainer );
  223. main_hb->add_child(vbc_right);
  224. vbc_right->set_h_size_flags(SIZE_EXPAND_FILL);
  225. HBoxContainer *add_bind_hb = memnew( HBoxContainer );
  226. type_list = memnew( OptionButton );
  227. type_list->set_h_size_flags(SIZE_EXPAND_FILL);
  228. add_bind_hb->add_child(type_list);
  229. type_list->add_item("bool",Variant::BOOL);
  230. type_list->add_item("int",Variant::INT);
  231. type_list->add_item("real",Variant::REAL);
  232. type_list->add_item("string",Variant::STRING);
  233. //type_list->add_separator();
  234. type_list->add_item("Vector2",Variant::VECTOR2);
  235. type_list->add_item("Rect2",Variant::RECT2);
  236. type_list->add_item("Vector3",Variant::VECTOR3);
  237. type_list->add_item("Plane",Variant::PLANE);
  238. type_list->add_item("Quat",Variant::QUAT);
  239. type_list->add_item("AABB",Variant::_AABB);
  240. type_list->add_item("Matrix3",Variant::MATRIX3);
  241. type_list->add_item("Transform",Variant::TRANSFORM);
  242. //type_list->add_separator();
  243. type_list->add_item("Color",Variant::COLOR);
  244. type_list->add_item("Image",Variant::IMAGE);
  245. type_list->select(0);
  246. Button *add_bind = memnew( Button );
  247. add_bind->set_text(TTR("Add"));
  248. add_bind_hb->add_child(add_bind);
  249. add_bind->connect("pressed",this,"_add_bind");
  250. Button *del_bind = memnew( Button );
  251. del_bind->set_text(TTR("Remove"));
  252. add_bind_hb->add_child(del_bind);
  253. del_bind->connect("pressed",this,"_remove_bind");
  254. vbc_right->add_margin_child(TTR("Add Extra Call Argument:"),add_bind_hb);
  255. bind_editor = memnew( PropertyEditor );
  256. bind_editor->hide_top_label();
  257. vbc_right->add_margin_child(TTR("Extra Call Arguments:"),bind_editor,true);
  258. dst_path = memnew(LineEdit);
  259. vbc->add_margin_child(TTR("Path to Node:"),dst_path);
  260. HBoxContainer *dstm_hb = memnew( HBoxContainer );
  261. vbc->add_margin_child("Method In Node:",dstm_hb);
  262. dst_method = memnew(LineEdit);
  263. dst_method->set_h_size_flags(SIZE_EXPAND_FILL);
  264. dstm_hb->add_child(dst_method);
  265. /*dst_method_list = memnew( MenuButton );
  266. dst_method_list->set_text("List..");
  267. dst_method_list->set_anchor( MARGIN_RIGHT, ANCHOR_END );
  268. dst_method_list->set_anchor( MARGIN_LEFT, ANCHOR_END );
  269. dst_method_list->set_anchor( MARGIN_TOP, ANCHOR_END );
  270. dst_method_list->set_anchor( MARGIN_BOTTOM, ANCHOR_END );
  271. dst_method_list->set_begin( Point2( 70,59) );
  272. dst_method_list->set_end( Point2( 15,39 ) );
  273. */
  274. //add_child(dst_method_list);
  275. make_callback = memnew( CheckButton );
  276. make_callback->set_toggle_mode(true);
  277. make_callback->set_pressed( EDITOR_DEF("text_editor/create_signal_callbacks",true));
  278. make_callback->set_text(TTR("Make Function"));
  279. dstm_hb->add_child(make_callback);
  280. deferred = memnew( CheckButton );
  281. deferred->set_text(TTR("Deferred"));
  282. dstm_hb->add_child(deferred);
  283. oneshot = memnew( CheckButton );
  284. oneshot->set_text(TTR("Oneshot"));
  285. dstm_hb->add_child(oneshot);
  286. /*
  287. realtime = memnew( CheckButton );
  288. realtime->set_anchor( MARGIN_TOP, ANCHOR_END );
  289. realtime->set_anchor( MARGIN_BOTTOM, ANCHOR_END );
  290. realtime->set_anchor( MARGIN_RIGHT, ANCHOR_END );
  291. realtime->set_begin( Point2( 120, button_margin-10 ) );
  292. realtime->set_end( Point2( 80, margin ) );
  293. realtime->set_text("Realtime");
  294. add_child(realtime);
  295. */
  296. // dst_method_list->get_popup()->connect("item_pressed", this,"_dst_method_list_selected");
  297. tree->connect("node_selected", this,"_tree_node_selected");
  298. set_as_toplevel(true);
  299. cdbinds = memnew( ConnectDialogBinds );
  300. error = memnew( ConfirmationDialog );
  301. add_child(error);
  302. error->get_ok()->set_text(TTR("Close"));
  303. get_ok()->set_text(TTR("Connect"));
  304. // error->get_cancel()->set_text("Close");
  305. }
  306. ConnectDialog::~ConnectDialog()
  307. {
  308. memdelete( cdbinds );
  309. }
  310. void ConnectionsDock::_notification(int p_what) {
  311. if (p_what==NOTIFICATION_DRAW) {
  312. //RID ci = get_canvas_item();
  313. //get_stylebox("panel","PopupMenu")->draw(ci,Rect2(Point2(),get_size()));
  314. }
  315. }
  316. void ConnectionsDock::_close() {
  317. hide();
  318. }
  319. void ConnectionsDock::_connect() {
  320. TreeItem *it = tree->get_selected();
  321. ERR_FAIL_COND(!it);
  322. String signal=it->get_metadata(0).operator Dictionary()["name"];
  323. NodePath dst_path=connect_dialog->get_dst_path();
  324. Node *target = node->get_node(dst_path);
  325. ERR_FAIL_COND(!target);
  326. StringName dst_method=connect_dialog->get_dst_method();
  327. bool defer=connect_dialog->get_deferred();
  328. bool oshot=connect_dialog->get_oneshot();
  329. Vector<Variant> binds = connect_dialog->get_binds();
  330. StringArray args = it->get_metadata(0).operator Dictionary()["args"];
  331. int flags = CONNECT_PERSIST | (defer?CONNECT_DEFERRED:0) | (oshot?CONNECT_ONESHOT:0);
  332. undo_redo->create_action(vformat(TTR("Connect '%s' to '%s'"),signal,String(dst_method)));
  333. undo_redo->add_do_method(node,"connect",signal,target,dst_method,binds,flags);
  334. undo_redo->add_undo_method(node,"disconnect",signal,target,dst_method);
  335. undo_redo->add_do_method(this,"update_tree");
  336. undo_redo->add_undo_method(this,"update_tree");
  337. undo_redo->add_do_method(EditorNode::get_singleton()->get_scene_tree_dock()->get_tree_editor(),"update_tree"); //to force redraw of scene tree
  338. undo_redo->add_undo_method(EditorNode::get_singleton()->get_scene_tree_dock()->get_tree_editor(),"update_tree"); //to force redraw of scene tree
  339. undo_redo->commit_action();
  340. if (connect_dialog->get_make_callback()) {
  341. print_line("request connect");
  342. editor->emit_signal("script_add_function_request",target,dst_method,args);
  343. hide();
  344. }
  345. update_tree();
  346. }
  347. void ConnectionsDock::_connect_pressed() {
  348. TreeItem *item = tree->get_selected();
  349. if (!item) {
  350. //no idea how this happened, but disable
  351. connect_button->set_disabled(true);
  352. return;
  353. }
  354. if (item->get_parent()==tree->get_root() || item->get_parent()->get_parent()==tree->get_root()) {
  355. //a signal - connect
  356. String signal=item->get_metadata(0).operator Dictionary()["name"];
  357. String signalname=signal;
  358. String midname=node->get_name();
  359. for(int i=0;i<midname.length();i++) {
  360. CharType c = midname[i];
  361. if ((c>='a' && c<='z') || (c>='A' && c<='Z') || (c>='0' && c<='9') || c=='_') {
  362. //all good
  363. } else if (c==' ') {
  364. c='_';
  365. } else {
  366. midname.remove(i);
  367. i--;
  368. continue;
  369. }
  370. midname[i]=c;
  371. }
  372. connect_dialog->edit(node);
  373. connect_dialog->popup_centered_ratio();
  374. connect_dialog->set_title(TTR("Connecting Signal:")+" "+signalname);
  375. connect_dialog->set_dst_method("_on_"+midname+"_"+signal);
  376. connect_dialog->set_dst_node(node->get_owner()?node->get_owner():node);
  377. } else {
  378. //a slot- disconnect
  379. Connection c=item->get_metadata(0);
  380. ERR_FAIL_COND(c.source!=node); //shouldn't happen but...bugcheck
  381. undo_redo->create_action(TTR("Create Subscription"));
  382. undo_redo->add_do_method(node,"disconnect",c.signal,c.target,c.method);
  383. undo_redo->add_undo_method(node,"connect",c.signal,c.target,c.method,Vector<Variant>(),c.flags);
  384. undo_redo->add_do_method(this,"update_tree");
  385. undo_redo->add_undo_method(this,"update_tree");
  386. undo_redo->add_do_method(EditorNode::get_singleton()->get_scene_tree_dock()->get_tree_editor(),"update_tree"); //to force redraw of scene tree
  387. undo_redo->add_undo_method(EditorNode::get_singleton()->get_scene_tree_dock()->get_tree_editor(),"update_tree"); //to force redraw of scene tree
  388. undo_redo->commit_action();
  389. c.source->disconnect(c.signal,c.target,c.method);
  390. update_tree();
  391. }
  392. }
  393. /*
  394. void ConnectionsDock::_remove() {
  395. if (!tree->get_selected())
  396. return;
  397. TreeItem *selected=tree->get_selected();
  398. if (!selected)
  399. return;
  400. Dictionary meta=selected->get_metadata(0);
  401. remove_confirm->set_text(String()+"Remove Connection \""+meta["from_event"].operator String()+"\" ?");
  402. remove_confirm->popup_centered(Size2(340,80));
  403. }
  404. */
  405. /*
  406. void ConnectionsDock::_remove_confirm() {
  407. if (!tree->get_selected())
  408. return;
  409. TreeItem *selected=tree->get_selected();
  410. if (!selected)
  411. return;
  412. Dictionary meta=selected->get_metadata(0);
  413. undo_redo->create_action("Remove Subscription");
  414. undo_redo->add_do_method(node,"unsubscribe_path_event",meta["from_event"].operator String(),meta["from_path"].operator NodePath(),meta["to_method"].operator String());
  415. undo_redo->add_undo_method(node,"subscribe_path_event_persist",meta["from_event"].operator String(),meta["from_path"].operator NodePath(),meta["to_method"].operator String(),Array(),false);
  416. undo_redo->add_do_method(this,"update_tree");
  417. undo_redo->add_undo_method(this,"update_tree");
  418. undo_redo->commit_action();
  419. }
  420. */
  421. struct _ConnectionsDockMethodInfoSort {
  422. _FORCE_INLINE_ bool operator()(const MethodInfo& a, const MethodInfo& b) const {
  423. return a.name < b.name;
  424. }
  425. };
  426. void ConnectionsDock::update_tree() {
  427. tree->clear();
  428. if (!node)
  429. return;
  430. TreeItem *root=tree->create_item();
  431. List<MethodInfo> node_signals;
  432. node->get_signal_list(&node_signals);
  433. //node_signals.sort_custom<_ConnectionsDockMethodInfoSort>();
  434. bool did_script=false;
  435. StringName base = node->get_type();
  436. while(base) {
  437. List<MethodInfo> node_signals;
  438. Ref<Texture> icon;
  439. String name;
  440. if (!did_script) {
  441. Ref<Script> scr = node->get_script();
  442. if (scr.is_valid()) {
  443. scr->get_script_signal_list(&node_signals);
  444. if (scr->get_path().is_resource_file())
  445. name=scr->get_path().get_file();
  446. else
  447. name=scr->get_type();
  448. if (has_icon(scr->get_type(),"EditorIcons")) {
  449. icon=get_icon(scr->get_type(),"EditorIcons");
  450. }
  451. }
  452. } else {
  453. ObjectTypeDB::get_signal_list(base,&node_signals,true);
  454. if (has_icon(base,"EditorIcons")) {
  455. icon=get_icon(base,"EditorIcons");
  456. }
  457. name=base;
  458. }
  459. TreeItem *pitem = NULL;
  460. if (node_signals.size()) {
  461. pitem=tree->create_item(root);
  462. pitem->set_text(0,name);
  463. pitem->set_icon(0,icon);
  464. pitem->set_selectable(0,false);
  465. pitem->set_editable(0,false);
  466. pitem->set_custom_bg_color(0,get_color("prop_subsection","Editor"));
  467. node_signals.sort();
  468. }
  469. for(List<MethodInfo>::Element *E=node_signals.front();E;E=E->next()) {
  470. MethodInfo &mi =E->get();
  471. String signaldesc;
  472. signaldesc=mi.name+"(";
  473. StringArray argnames;
  474. if (mi.arguments.size()) {
  475. signaldesc+=" ";
  476. for(int i=0;i<mi.arguments.size();i++) {
  477. PropertyInfo &pi = mi.arguments[i];
  478. if (i>0)
  479. signaldesc+=", ";
  480. String tname="var";
  481. if (pi.type!=Variant::NIL) {
  482. tname=Variant::get_type_name(pi.type);
  483. }
  484. signaldesc+=tname+" "+(pi.name==""?String("arg "+itos(i)):pi.name);
  485. argnames.push_back(pi.name);
  486. }
  487. signaldesc+=" ";
  488. }
  489. signaldesc+=")";
  490. TreeItem *item=tree->create_item(pitem);
  491. item->set_text(0,signaldesc);
  492. Dictionary sinfo;
  493. sinfo["name"]=mi.name;
  494. sinfo["args"]=argnames;
  495. item->set_metadata(0,sinfo);
  496. item->set_icon(0,get_icon("Signal","EditorIcons"));
  497. List<Object::Connection> connections;
  498. node->get_signal_connection_list(mi.name,&connections);
  499. for(List<Object::Connection>::Element *F=connections.front();F;F=F->next()) {
  500. Object::Connection&c = F->get();
  501. if (!(c.flags&CONNECT_PERSIST))
  502. continue;
  503. Node *target = c.target->cast_to<Node>();
  504. if (!target)
  505. continue;
  506. String path = String(node->get_path_to(target))+" :: "+c.method+"()";
  507. if (c.flags&CONNECT_DEFERRED)
  508. path+=" (deferred)";
  509. if (c.flags&CONNECT_ONESHOT)
  510. path+=" (oneshot)";
  511. if (c.binds.size()) {
  512. path+=" binds( ";
  513. for(int i=0;i<c.binds.size();i++) {
  514. if (i>0)
  515. path+=", ";
  516. path+=c.binds[i].operator String();
  517. }
  518. path+=" )";
  519. }
  520. TreeItem *item2=tree->create_item(item);
  521. item2->set_text(0,path);
  522. item2->set_metadata(0,c);
  523. item2->set_icon(0,get_icon("Slot","EditorIcons"));
  524. }
  525. }
  526. if (!did_script) {
  527. did_script=true;
  528. } else {
  529. base=ObjectTypeDB::type_inherits_from(base);
  530. }
  531. }
  532. connect_button->set_text(TTR("Connect"));
  533. connect_button->set_disabled(true);
  534. }
  535. void ConnectionsDock::set_node(Node* p_node) {
  536. node=p_node;
  537. update_tree();
  538. }
  539. void ConnectionsDock::_something_selected() {
  540. TreeItem *item = tree->get_selected();
  541. if (!item) {
  542. //no idea how this happened, but disable
  543. connect_button->set_text(TTR("Connect.."));
  544. connect_button->set_disabled(true);
  545. } else if (item->get_parent()==tree->get_root() || item->get_parent()->get_parent()==tree->get_root()) {
  546. //a signal - connect
  547. connect_button->set_text(TTR("Connect.."));
  548. connect_button->set_disabled(false);
  549. } else {
  550. //a slot- disconnect
  551. connect_button->set_text(TTR("Disconnect"));
  552. connect_button->set_disabled(false);
  553. }
  554. }
  555. void ConnectionsDock::_something_activated() {
  556. TreeItem *item = tree->get_selected();
  557. if (!item)
  558. return;
  559. if (item->get_parent()==tree->get_root() || item->get_parent()->get_parent()==tree->get_root()) {
  560. // a signal - connect
  561. String signal=item->get_metadata(0).operator Dictionary()["name"];
  562. String midname=node->get_name();
  563. for(int i=0;i<midname.length();i++) {
  564. CharType c = midname[i];
  565. if ((c>='a' && c<='z') || (c>='A' && c<='Z') || (c>='0' && c<='9') || c=='_') {
  566. //all good
  567. } else if (c==' ') {
  568. c='_';
  569. } else {
  570. midname.remove(i);
  571. i--;
  572. continue;
  573. }
  574. midname[i]=c;
  575. }
  576. connect_dialog->edit(node);
  577. connect_dialog->popup_centered_ratio();
  578. connect_dialog->set_dst_method("_on_"+midname+"_"+signal);
  579. connect_dialog->set_dst_node(node->get_owner()?node->get_owner():node);
  580. } else {
  581. // a slot - go to target method
  582. Connection c=item->get_metadata(0);
  583. ERR_FAIL_COND(c.source!=node); //shouldn't happen but...bugcheck
  584. if (!c.target)
  585. return;
  586. Ref<Script> script = c.target->get_script();
  587. if (script.is_valid() && ScriptEditor::get_singleton()->script_go_to_method(script,c.method)) {
  588. editor->call("_editor_select",EditorNode::EDITOR_SCRIPT);
  589. }
  590. }
  591. }
  592. void ConnectionsDock::_bind_methods() {
  593. ObjectTypeDB::bind_method("_connect",&ConnectionsDock::_connect);
  594. ObjectTypeDB::bind_method("_something_selected",&ConnectionsDock::_something_selected);
  595. ObjectTypeDB::bind_method("_something_activated",&ConnectionsDock::_something_activated);
  596. ObjectTypeDB::bind_method("_close",&ConnectionsDock::_close);
  597. ObjectTypeDB::bind_method("_connect_pressed",&ConnectionsDock::_connect_pressed);
  598. ObjectTypeDB::bind_method("update_tree",&ConnectionsDock::update_tree);
  599. }
  600. ConnectionsDock::ConnectionsDock(EditorNode *p_editor) {
  601. editor=p_editor;
  602. set_name(TTR("Signals"));
  603. VBoxContainer *vbc = this;
  604. tree = memnew( Tree );
  605. tree->set_columns(1);
  606. tree->set_select_mode(Tree::SELECT_ROW);
  607. tree->set_hide_root(true);
  608. vbc->add_child(tree);
  609. tree->set_v_size_flags(SIZE_EXPAND_FILL);
  610. connect_button = memnew( Button );
  611. connect_button->set_text("Connect");
  612. HBoxContainer *hb = memnew( HBoxContainer);
  613. vbc->add_child(hb);
  614. hb->add_spacer();
  615. hb->add_child(connect_button);
  616. connect_button->connect("pressed",this,"_connect_pressed");
  617. // add_child(tree);
  618. connect_dialog = memnew( ConnectDialog );
  619. connect_dialog->set_as_toplevel(true);
  620. add_child(connect_dialog);
  621. remove_confirm = memnew( ConfirmationDialog );
  622. remove_confirm->set_as_toplevel(true);
  623. add_child(remove_confirm);
  624. /*
  625. node_only->set_anchor( MARGIN_TOP, ANCHOR_END );
  626. node_only->set_anchor( MARGIN_BOTTOM, ANCHOR_END );
  627. node_only->set_anchor( MARGIN_RIGHT, ANCHOR_END );
  628. node_only->set_begin( Point2( 20,51) );
  629. node_only->set_end( Point2( 10,44) );
  630. */
  631. remove_confirm->connect("confirmed", this,"_remove_confirm");
  632. connect_dialog->connect("connected", this,"_connect");
  633. tree->connect("item_selected", this,"_something_selected");
  634. tree->connect("item_activated", this,"_something_activated");
  635. add_constant_override("separation",3*EDSCALE);
  636. }
  637. ConnectionsDock::~ConnectionsDock()
  638. {
  639. }