connections_dialog.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791
  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-2014 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. class ConnectDialogBinds : public Object {
  35. OBJ_TYPE( ConnectDialogBinds, Object );
  36. public:
  37. Vector<Variant> params;
  38. bool _set(const StringName& p_name, const Variant& p_value) {
  39. String name=p_name;
  40. if (name.begins_with("bind/")) {
  41. int which = name.get_slice("/",1).to_int()-1;
  42. ERR_FAIL_INDEX_V(which,params.size(),false);
  43. params[which]=p_value;
  44. } else
  45. return false;
  46. return true;
  47. }
  48. bool _get(const StringName& p_name,Variant &r_ret) const {
  49. String name=p_name;
  50. if (name.begins_with("bind/")) {
  51. int which = name.get_slice("/",1).to_int()-1;
  52. ERR_FAIL_INDEX_V(which,params.size(),false);
  53. r_ret = params[which];
  54. } else
  55. return false;
  56. return true;
  57. }
  58. void _get_property_list(List<PropertyInfo> *p_list) const {
  59. for(int i=0;i<params.size();i++) {
  60. p_list->push_back( PropertyInfo( params[i].get_type(), "bind/"+itos(i+1)) );
  61. }
  62. }
  63. void notify_changed() {
  64. _change_notify();
  65. }
  66. ConnectDialogBinds() {
  67. }
  68. };
  69. void ConnectDialog::_notification(int p_what) {
  70. if (p_what==NOTIFICATION_DRAW) {
  71. RID ci = get_canvas_item();
  72. get_stylebox("panel","PopupMenu")->draw(ci,Rect2(Point2(),get_size()));
  73. }
  74. }
  75. void ConnectDialog::_tree_node_selected() {
  76. //dst_method_list->get_popup()->clear();
  77. Node *current=tree->get_selected();
  78. if (!current) {
  79. make_callback->hide();
  80. return;
  81. }
  82. if (current->get_script().is_null())
  83. make_callback->hide();
  84. else
  85. make_callback->show();
  86. #if 0
  87. List<MethodInfo> methods;
  88. current->get_method_list(&methods);
  89. for (List<MethodInfo>::Element *E=methods.front();E;E=E->next()) {
  90. if (E->get().name.length() && E->get().name[0]=='_')
  91. continue; // hidden method, not show!
  92. if (ObjectTypeDB::has_method(node->get_type(),"Node") || ObjectTypeDB::has_method(node->get_type(),"Control",true))
  93. continue; //avoid too much unnecesary stuff
  94. String method=E->get().name+"(";
  95. for(int i=0;i<E->get().arguments.size();i++) {
  96. if (i!=0)
  97. method+=", ";
  98. method+=Variant::get_type_name(E->get().arguments[i].type);
  99. if (E->get().arguments[i].name.length()) {
  100. method+=" ";
  101. method+=E->get().arguments[i].name;
  102. }
  103. }
  104. method+=")";
  105. //dst_method_list->get_popup()->add_item(method);
  106. }
  107. #endif
  108. dst_path->set_text(node->get_path_to(current));
  109. }
  110. void ConnectDialog::_dst_method_list_selected(int p_idx) {
  111. //dst_method->set_text( dst_method_list->get_popup()->get_item_text(p_idx));
  112. }
  113. void ConnectDialog::edit(Node *p_node) {
  114. node=p_node;
  115. //dst_method_list->get_popup()->clear();
  116. tree->set_selected(NULL);
  117. tree->set_marked(node,true);
  118. dst_path->set_text("");
  119. dst_method->set_text("");
  120. deferred->set_pressed(false);
  121. cdbinds->params.clear();
  122. cdbinds->notify_changed();
  123. }
  124. void ConnectDialog::ok_pressed() {
  125. if (dst_method->get_text()=="") {
  126. error->set_text("Method in target Node must be specified!");
  127. error->popup_centered(Size2(300,80));
  128. return;
  129. }
  130. emit_signal("connected");
  131. hide();
  132. }
  133. void ConnectDialog::_cancel_pressed() {
  134. hide();
  135. }
  136. NodePath ConnectDialog::get_dst_path() const {
  137. return dst_path->get_text();
  138. }
  139. bool ConnectDialog::get_deferred() const {
  140. return deferred->is_pressed();
  141. }
  142. StringName ConnectDialog::get_dst_method() const {
  143. String txt=dst_method->get_text();
  144. if (txt.find("(")!=-1)
  145. txt=txt.left( txt.find("(")).strip_edges();
  146. return txt;
  147. }
  148. Vector<Variant> ConnectDialog::get_binds() const {
  149. return cdbinds->params;
  150. }
  151. void ConnectDialog::_add_bind() {
  152. if (cdbinds->params.size() >= VARIANT_ARG_MAX)
  153. return;
  154. Variant::Type vt = (Variant::Type)type_list->get_item_ID(type_list->get_selected());
  155. Variant value;
  156. switch(vt) {
  157. case Variant::BOOL: value = false ; break;
  158. case Variant::INT: value = 0; break;
  159. case Variant::REAL: value = 0.0; break;
  160. case Variant::STRING: value = ""; break;
  161. case Variant::VECTOR2: value = Vector2(); break;
  162. case Variant::RECT2: value = Rect2(); break;
  163. case Variant::VECTOR3: value = Vector3(); break;
  164. case Variant::PLANE: value = Plane(); break;
  165. case Variant::QUAT: value = Quat(); break;
  166. case Variant::_AABB: value = AABB(); break;
  167. case Variant::MATRIX3: value = Matrix3(); break;
  168. case Variant::TRANSFORM: value = Transform(); break;
  169. case Variant::COLOR: value = Color(); break;
  170. case Variant::IMAGE: value = Image(); break;
  171. default: { ERR_FAIL(); } break;
  172. }
  173. ERR_FAIL_COND(value.get_type()==Variant::NIL);
  174. cdbinds->params.push_back(value);
  175. cdbinds->notify_changed();
  176. }
  177. void ConnectDialog::_remove_bind() {
  178. String st = bind_editor->get_selected_path();
  179. if (st=="")
  180. return;
  181. int idx = st.get_slice("/",1).to_int()-1;
  182. ERR_FAIL_INDEX(idx,cdbinds->params.size());
  183. cdbinds->params.remove(idx);
  184. cdbinds->notify_changed();
  185. }
  186. void ConnectDialog::set_dst_node(Node* p_node) {
  187. tree->set_selected(p_node);
  188. }
  189. void ConnectDialog::set_dst_method(const StringName& p_method) {
  190. dst_method->set_text(p_method);
  191. }
  192. void ConnectDialog::_bind_methods() {
  193. //ObjectTypeDB::bind_method("_ok",&ConnectDialog::_ok_pressed);
  194. ObjectTypeDB::bind_method("_cancel",&ConnectDialog::_cancel_pressed);
  195. //ObjectTypeDB::bind_method("_dst_method_list_selected",&ConnectDialog::_dst_method_list_selected);
  196. ObjectTypeDB::bind_method("_tree_node_selected",&ConnectDialog::_tree_node_selected);
  197. ObjectTypeDB::bind_method("_add_bind",&ConnectDialog::_add_bind);
  198. ObjectTypeDB::bind_method("_remove_bind",&ConnectDialog::_remove_bind);
  199. ADD_SIGNAL( MethodInfo("connected") );
  200. }
  201. ConnectDialog::ConnectDialog() {
  202. int margin = get_constant("margin","Dialogs");
  203. int button_margin = get_constant("button_margin","Dialogs");
  204. Label * label = memnew( Label );
  205. label->set_pos( Point2( 8,11) );
  206. label->set_text("Connect To Node:");
  207. add_child(label);
  208. label = memnew( Label );
  209. label->set_anchor( MARGIN_LEFT, ANCHOR_RATIO );
  210. label->set_pos( Point2( 0.5,11) );
  211. label->set_text("Binds (Extra Params):");
  212. add_child(label);
  213. tree = memnew(SceneTreeEditor(false));
  214. tree->set_anchor( MARGIN_RIGHT, ANCHOR_RATIO );
  215. tree->set_anchor( MARGIN_BOTTOM, ANCHOR_END );
  216. tree->set_begin( Point2( 15,32) );
  217. tree->set_end( Point2( 0.5,127 ) );
  218. add_child(tree);
  219. bind_editor = memnew( PropertyEditor );
  220. bind_editor->set_anchor( MARGIN_RIGHT, ANCHOR_END );
  221. bind_editor->set_anchor( MARGIN_LEFT, ANCHOR_RATIO );
  222. bind_editor->set_anchor( MARGIN_BOTTOM, ANCHOR_END );
  223. bind_editor->set_begin( Point2( 0.51,42) );
  224. bind_editor->set_end( Point2( 15,127 ) );
  225. bind_editor->get_top_label()->hide();
  226. add_child(bind_editor);
  227. type_list = memnew( OptionButton );
  228. type_list->set_anchor( MARGIN_RIGHT, ANCHOR_RATIO );
  229. type_list->set_anchor( MARGIN_LEFT, ANCHOR_RATIO );
  230. type_list->set_begin( Point2( 0.51,32) );
  231. type_list->set_end( Point2( 0.75,33 ) );
  232. add_child(type_list);
  233. type_list->add_item("bool",Variant::BOOL);
  234. type_list->add_item("int",Variant::INT);
  235. type_list->add_item("real",Variant::REAL);
  236. type_list->add_item("string",Variant::STRING);
  237. //type_list->add_separator();
  238. type_list->add_item("Vector2",Variant::VECTOR2);
  239. type_list->add_item("Rect2",Variant::RECT2);
  240. type_list->add_item("Vector3",Variant::VECTOR3);
  241. type_list->add_item("Plane",Variant::PLANE);
  242. type_list->add_item("Quat",Variant::QUAT);
  243. type_list->add_item("AABB",Variant::_AABB);
  244. type_list->add_item("Matrix3",Variant::MATRIX3);
  245. type_list->add_item("Transform",Variant::TRANSFORM);
  246. //type_list->add_separator();
  247. type_list->add_item("Color",Variant::COLOR);
  248. type_list->add_item("Image",Variant::IMAGE);
  249. type_list->select(0);
  250. Button *add_bind = memnew( Button );
  251. add_bind->set_anchor( MARGIN_RIGHT, ANCHOR_RATIO );
  252. add_bind->set_anchor( MARGIN_LEFT, ANCHOR_RATIO );
  253. add_bind->set_begin( Point2( 0.76,32) );
  254. add_bind->set_end( Point2( 0.84,33 ) );
  255. add_bind->set_text("Add");
  256. add_child(add_bind);
  257. add_bind->connect("pressed",this,"_add_bind");
  258. Button *del_bind = memnew( Button );
  259. del_bind->set_anchor( MARGIN_RIGHT, ANCHOR_END );
  260. del_bind->set_anchor( MARGIN_LEFT, ANCHOR_RATIO );
  261. del_bind->set_begin( Point2( 0.85,32) );
  262. del_bind->set_end( Point2( 15,33 ) );
  263. del_bind->set_text("Remove");
  264. add_child(del_bind);
  265. del_bind->connect("pressed",this,"_remove_bind");
  266. label = memnew( Label );
  267. label->set_anchor( MARGIN_TOP, ANCHOR_END );
  268. label->set_begin( Point2( 8,124) );
  269. label->set_end( Point2( 15,99) );
  270. label->set_text("Path To Node:");
  271. add_child(label);
  272. dst_path = memnew(LineEdit);
  273. dst_path->set_anchor( MARGIN_TOP, ANCHOR_END );
  274. dst_path->set_anchor( MARGIN_RIGHT, ANCHOR_END );
  275. dst_path->set_anchor( MARGIN_BOTTOM, ANCHOR_END );
  276. dst_path->set_begin( Point2( 15,105) );
  277. dst_path->set_end( Point2( 15,80 ) );
  278. add_child(dst_path);
  279. label = memnew( Label );
  280. label->set_anchor( MARGIN_TOP, ANCHOR_END );
  281. label->set_anchor( MARGIN_RIGHT, ANCHOR_END );
  282. label->set_anchor( MARGIN_BOTTOM, ANCHOR_END );
  283. label->set_begin( Point2( 8,78 ) );
  284. label->set_end( Point2( 15,52 ) );
  285. label->set_text("Method In Node:");
  286. add_child(label);
  287. HBoxContainer *dstm_hb = memnew( HBoxContainer );
  288. dstm_hb->set_anchor( MARGIN_TOP, ANCHOR_END );
  289. dstm_hb->set_anchor( MARGIN_RIGHT, ANCHOR_END );
  290. dstm_hb->set_anchor( MARGIN_BOTTOM, ANCHOR_END );
  291. dstm_hb->set_begin( Point2( 15,59) );
  292. dstm_hb->set_end( Point2( 15,39 ) );
  293. add_child(dstm_hb);
  294. dst_method = memnew(LineEdit);
  295. dst_method->set_h_size_flags(SIZE_EXPAND_FILL);
  296. dstm_hb->add_child(dst_method);
  297. /*dst_method_list = memnew( MenuButton );
  298. dst_method_list->set_text("List..");
  299. dst_method_list->set_anchor( MARGIN_RIGHT, ANCHOR_END );
  300. dst_method_list->set_anchor( MARGIN_LEFT, ANCHOR_END );
  301. dst_method_list->set_anchor( MARGIN_TOP, ANCHOR_END );
  302. dst_method_list->set_anchor( MARGIN_BOTTOM, ANCHOR_END );
  303. dst_method_list->set_begin( Point2( 70,59) );
  304. dst_method_list->set_end( Point2( 15,39 ) );
  305. */
  306. //add_child(dst_method_list);
  307. make_callback = memnew( CheckButton );
  308. make_callback->set_toggle_mode(true);
  309. make_callback->set_pressed( EDITOR_DEF("text_editor/create_signal_callbacks",true));
  310. make_callback->set_text("Make Function ");
  311. dstm_hb->add_child(make_callback);
  312. deferred = memnew( CheckButton );
  313. deferred->set_toggle_mode(true);
  314. deferred->set_pressed(true);
  315. deferred->set_text("Deferred");
  316. dstm_hb->add_child(deferred);
  317. /*
  318. realtime = memnew( CheckButton );
  319. realtime->set_anchor( MARGIN_TOP, ANCHOR_END );
  320. realtime->set_anchor( MARGIN_BOTTOM, ANCHOR_END );
  321. realtime->set_anchor( MARGIN_RIGHT, ANCHOR_END );
  322. realtime->set_begin( Point2( 120, button_margin-10 ) );
  323. realtime->set_end( Point2( 80, margin ) );
  324. realtime->set_text("Realtime");
  325. add_child(realtime);
  326. */
  327. // dst_method_list->get_popup()->connect("item_pressed", this,"_dst_method_list_selected");
  328. tree->connect("node_selected", this,"_tree_node_selected");
  329. set_as_toplevel(true);
  330. cdbinds = memnew( ConnectDialogBinds );
  331. bind_editor->edit(cdbinds);
  332. error = memnew( ConfirmationDialog );
  333. add_child(error);
  334. error->get_ok()->set_text("Close");
  335. get_ok()->set_text("Connect");
  336. // error->get_cancel()->set_text("Close");
  337. }
  338. ConnectDialog::~ConnectDialog()
  339. {
  340. memdelete( cdbinds );
  341. }
  342. void ConnectionsDialog::_notification(int p_what) {
  343. if (p_what==NOTIFICATION_DRAW) {
  344. RID ci = get_canvas_item();
  345. get_stylebox("panel","PopupMenu")->draw(ci,Rect2(Point2(),get_size()));
  346. }
  347. }
  348. void ConnectionsDialog::_close() {
  349. hide();
  350. }
  351. void ConnectionsDialog::_connect() {
  352. TreeItem *it = tree->get_selected();
  353. ERR_FAIL_COND(!it);
  354. String signal=it->get_metadata(0).operator Dictionary()["name"];
  355. NodePath dst_path=connect_dialog->get_dst_path();
  356. Node *target = node->get_node(dst_path);
  357. ERR_FAIL_COND(!target);
  358. StringName dst_method=connect_dialog->get_dst_method();
  359. bool defer=connect_dialog->get_deferred();
  360. Vector<Variant> binds = connect_dialog->get_binds();
  361. StringArray args = it->get_metadata(0).operator Dictionary()["args"];
  362. undo_redo->create_action("Connect '"+signal+"' to '"+String(dst_method)+"'");
  363. undo_redo->add_do_method(node,"connect",signal,target,dst_method,binds,CONNECT_PERSIST | (defer?CONNECT_DEFERRED:0));
  364. undo_redo->add_undo_method(node,"disconnect",signal,target,dst_method);
  365. undo_redo->add_do_method(this,"update_tree");
  366. undo_redo->add_undo_method(this,"update_tree");
  367. undo_redo->commit_action();
  368. if (connect_dialog->get_make_callback()) {
  369. print_line("request connect");
  370. editor->emit_signal("script_add_function_request",target,dst_method,args);
  371. hide();
  372. }
  373. update_tree();
  374. }
  375. void ConnectionsDialog::ok_pressed() {
  376. TreeItem *item = tree->get_selected();
  377. if (!item) {
  378. //no idea how this happened, but disable
  379. get_ok()->set_disabled(true);
  380. return;
  381. }
  382. if (item->get_parent()==tree->get_root()) {
  383. //a signal - connect
  384. String signal=item->get_metadata(0).operator Dictionary()["name"];
  385. String signalname=signal;
  386. String midname=node->get_name();
  387. for(int i=0;i<midname.length();i++) {
  388. CharType c = midname[i];
  389. if ((c>='a' && c<='z') || (c>='A' && c<='Z') || (c>='0' && c<='9') || c=='_') {
  390. //all good
  391. } else if (c==' ') {
  392. c='_';
  393. } else {
  394. midname.remove(i);
  395. i--;
  396. continue;
  397. }
  398. midname[i]=c;
  399. }
  400. connect_dialog->edit(node);
  401. connect_dialog->popup_centered_ratio();
  402. connect_dialog->set_dst_method("_on_"+midname+"_"+signal);
  403. connect_dialog->set_dst_node(node->get_owner()?node->get_owner():node);
  404. } else {
  405. //a slot- disconnect
  406. Connection c=item->get_metadata(0);
  407. ERR_FAIL_COND(c.source!=node); //shouldn't happen but...bugcheck
  408. undo_redo->create_action("Create Subscription");
  409. undo_redo->add_do_method(node,"disconnect",c.signal,c.target,c.method);
  410. undo_redo->add_undo_method(node,"connect",c.signal,c.target,c.method,Vector<Variant>(),c.flags);
  411. undo_redo->add_do_method(this,"update_tree");
  412. undo_redo->add_undo_method(this,"update_tree");
  413. undo_redo->commit_action();
  414. c.source->disconnect(c.signal,c.target,c.method);
  415. update_tree();
  416. }
  417. }
  418. /*
  419. void ConnectionsDialog::_remove() {
  420. if (!tree->get_selected())
  421. return;
  422. TreeItem *selected=tree->get_selected();
  423. if (!selected)
  424. return;
  425. Dictionary meta=selected->get_metadata(0);
  426. remove_confirm->set_text(String()+"Remove Connection \""+meta["from_event"].operator String()+"\" ?");
  427. remove_confirm->popup_centered(Size2(340,80));
  428. }
  429. */
  430. /*
  431. void ConnectionsDialog::_remove_confirm() {
  432. if (!tree->get_selected())
  433. return;
  434. TreeItem *selected=tree->get_selected();
  435. if (!selected)
  436. return;
  437. Dictionary meta=selected->get_metadata(0);
  438. undo_redo->create_action("Remove Subscription");
  439. undo_redo->add_do_method(node,"unsubscribe_path_event",meta["from_event"].operator String(),meta["from_path"].operator NodePath(),meta["to_method"].operator String());
  440. 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);
  441. undo_redo->add_do_method(this,"update_tree");
  442. undo_redo->add_undo_method(this,"update_tree");
  443. undo_redo->commit_action();
  444. }
  445. */
  446. void ConnectionsDialog::update_tree() {
  447. if (!is_visible())
  448. return; //don't update if not visible, of course
  449. tree->clear();
  450. if (!node)
  451. return;
  452. TreeItem *root=tree->create_item();
  453. List<MethodInfo> node_signals;
  454. node->get_signal_list(&node_signals);
  455. for(List<MethodInfo>::Element *E=node_signals.front();E;E=E->next()) {
  456. MethodInfo &mi =E->get();
  457. String signaldesc;
  458. signaldesc=mi.name+"(";
  459. StringArray argnames;
  460. if (mi.arguments.size()) {
  461. signaldesc+=" ";
  462. for(int i=0;i<mi.arguments.size();i++) {
  463. PropertyInfo &pi = mi.arguments[i];
  464. if (i>0)
  465. signaldesc+=", ";
  466. signaldesc+=Variant::get_type_name(pi.type)+" "+(pi.name==""?String("arg "+itos(i)):pi.name);
  467. argnames.push_back(pi.name);
  468. }
  469. signaldesc+=" ";
  470. }
  471. signaldesc+=")";
  472. TreeItem *item=tree->create_item(root);
  473. item->set_text(0,signaldesc);
  474. Dictionary sinfo;
  475. sinfo["name"]=mi.name;
  476. sinfo["args"]=argnames;
  477. item->set_metadata(0,sinfo);
  478. item->set_icon(0,get_icon("Signal","EditorIcons"));
  479. List<Object::Connection> connections;
  480. node->get_signal_connection_list(mi.name,&connections);
  481. for(List<Object::Connection>::Element *F=connections.front();F;F=F->next()) {
  482. Object::Connection&c = F->get();
  483. if (!(c.flags&CONNECT_PERSIST))
  484. continue;
  485. Node *target = c.target->cast_to<Node>();
  486. if (!target)
  487. continue;
  488. String path = String(node->get_path_to(target))+" :: "+c.method+"()";
  489. if (c.flags&CONNECT_DEFERRED)
  490. path+=" (deferred)";
  491. if (c.binds.size()) {
  492. path+=" binds( ";
  493. for(int i=0;i<c.binds.size();i++) {
  494. if (i>0)
  495. path+=", ";
  496. path+=c.binds[i].operator String();
  497. }
  498. path+=" )";
  499. }
  500. TreeItem *item2=tree->create_item(item);
  501. item2->set_text(0,path);
  502. item2->set_metadata(0,c);
  503. item2->set_icon(0,get_icon("Slot","EditorIcons"));
  504. }
  505. }
  506. get_ok()->set_text("Connect");
  507. get_ok()->set_disabled(true);
  508. }
  509. void ConnectionsDialog::set_node(Node* p_node) {
  510. node=p_node;
  511. update_tree();
  512. }
  513. void ConnectionsDialog::_something_selected() {
  514. TreeItem *item = tree->get_selected();
  515. if (!item) {
  516. //no idea how this happened, but disable
  517. get_ok()->set_text("Connect..");
  518. get_ok()->set_disabled(true);
  519. } else if (item->get_parent()==tree->get_root()) {
  520. //a signal - connect
  521. get_ok()->set_text("Connect..");
  522. get_ok()->set_disabled(false);
  523. } else {
  524. //a slot- disconnect
  525. get_ok()->set_text("Disconnect");
  526. get_ok()->set_disabled(false);
  527. }
  528. }
  529. void ConnectionsDialog::_bind_methods() {
  530. ObjectTypeDB::bind_method("_connect",&ConnectionsDialog::_connect);
  531. ObjectTypeDB::bind_method("_something_selected",&ConnectionsDialog::_something_selected);
  532. ObjectTypeDB::bind_method("_close",&ConnectionsDialog::_close);
  533. // ObjectTypeDB::bind_method("_remove_confirm",&ConnectionsDialog::_remove_confirm);
  534. ObjectTypeDB::bind_method("update_tree",&ConnectionsDialog::update_tree);
  535. }
  536. ConnectionsDialog::ConnectionsDialog(EditorNode *p_editor) {
  537. editor=p_editor;
  538. set_title("Edit Connections..");
  539. set_hide_on_ok(false);
  540. VBoxContainer *vbc = memnew( VBoxContainer );
  541. add_child(vbc);
  542. set_child_rect(vbc);
  543. tree = memnew( Tree );
  544. tree->set_columns(1);
  545. tree->set_select_mode(Tree::SELECT_ROW);
  546. tree->set_hide_root(true);
  547. vbc->add_margin_child("Connections:",tree,true);
  548. // add_child(tree);
  549. connect_dialog = memnew( ConnectDialog );
  550. connect_dialog->set_as_toplevel(true);
  551. add_child(connect_dialog);
  552. remove_confirm = memnew( ConfirmationDialog );
  553. remove_confirm->set_as_toplevel(true);
  554. add_child(remove_confirm);
  555. /*
  556. node_only->set_anchor( MARGIN_TOP, ANCHOR_END );
  557. node_only->set_anchor( MARGIN_BOTTOM, ANCHOR_END );
  558. node_only->set_anchor( MARGIN_RIGHT, ANCHOR_END );
  559. node_only->set_begin( Point2( 20,51) );
  560. node_only->set_end( Point2( 10,44) );
  561. */
  562. remove_confirm->connect("confirmed", this,"_remove_confirm");
  563. connect_dialog->connect("connected", this,"_connect");
  564. tree->connect("item_selected", this,"_something_selected");
  565. get_cancel()->set_text("Close");
  566. }
  567. ConnectionsDialog::~ConnectionsDialog()
  568. {
  569. }