connections_dialog.h 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. /**************************************************************************/
  2. /* connections_dialog.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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. #ifndef CONNECTIONS_DIALOG_H
  31. #define CONNECTIONS_DIALOG_H
  32. #include "scene/gui/check_button.h"
  33. #include "scene/gui/dialogs.h"
  34. #include "scene/gui/tree.h"
  35. class Button;
  36. class CheckBox;
  37. class ConnectDialogBinds;
  38. class EditorInspector;
  39. class Label;
  40. class LineEdit;
  41. class OptionButton;
  42. class PopupMenu;
  43. class SceneTreeEditor;
  44. class SpinBox;
  45. class ConnectDialog : public ConfirmationDialog {
  46. GDCLASS(ConnectDialog, ConfirmationDialog);
  47. public:
  48. struct ConnectionData {
  49. Node *source = nullptr;
  50. Node *target = nullptr;
  51. StringName signal;
  52. StringName method;
  53. uint32_t flags = 0;
  54. int unbinds = 0;
  55. Vector<Variant> binds;
  56. ConnectionData() {}
  57. ConnectionData(const Connection &p_connection) {
  58. source = Object::cast_to<Node>(p_connection.signal.get_object());
  59. signal = p_connection.signal.get_name();
  60. target = Object::cast_to<Node>(p_connection.callable.get_object());
  61. flags = p_connection.flags;
  62. Callable base_callable;
  63. if (p_connection.callable.is_custom()) {
  64. CallableCustomBind *ccb = dynamic_cast<CallableCustomBind *>(p_connection.callable.get_custom());
  65. if (ccb) {
  66. binds = ccb->get_binds();
  67. base_callable = ccb->get_callable();
  68. }
  69. CallableCustomUnbind *ccu = dynamic_cast<CallableCustomUnbind *>(p_connection.callable.get_custom());
  70. if (ccu) {
  71. unbinds = ccu->get_unbinds();
  72. base_callable = ccu->get_callable();
  73. }
  74. } else {
  75. base_callable = p_connection.callable;
  76. }
  77. method = base_callable.get_method();
  78. }
  79. Callable get_callable() const {
  80. if (unbinds > 0) {
  81. return Callable(target, method).unbind(unbinds);
  82. } else if (!binds.is_empty()) {
  83. const Variant **argptrs = (const Variant **)alloca(sizeof(Variant *) * binds.size());
  84. for (int i = 0; i < binds.size(); i++) {
  85. argptrs[i] = &binds[i];
  86. }
  87. return Callable(target, method).bindp(argptrs, binds.size());
  88. } else {
  89. return Callable(target, method);
  90. }
  91. }
  92. };
  93. private:
  94. Label *connect_to_label = nullptr;
  95. LineEdit *from_signal = nullptr;
  96. LineEdit *filter_nodes = nullptr;
  97. Node *source = nullptr;
  98. ConnectionData source_connection_data;
  99. StringName signal;
  100. PackedStringArray signal_args;
  101. LineEdit *dst_method = nullptr;
  102. ConnectDialogBinds *cdbinds = nullptr;
  103. bool edit_mode = false;
  104. bool first_popup = true;
  105. NodePath dst_path;
  106. VBoxContainer *vbc_right = nullptr;
  107. SceneTreeEditor *tree = nullptr;
  108. AcceptDialog *error = nullptr;
  109. Button *open_method_tree = nullptr;
  110. AcceptDialog *method_popup = nullptr;
  111. Tree *method_tree = nullptr;
  112. Label *empty_tree_label = nullptr;
  113. LineEdit *method_search = nullptr;
  114. CheckButton *script_methods_only = nullptr;
  115. CheckButton *compatible_methods_only = nullptr;
  116. SpinBox *unbind_count = nullptr;
  117. EditorInspector *bind_editor = nullptr;
  118. OptionButton *type_list = nullptr;
  119. CheckBox *deferred = nullptr;
  120. CheckBox *one_shot = nullptr;
  121. CheckButton *advanced = nullptr;
  122. Vector<Control *> bind_controls;
  123. Label *warning_label = nullptr;
  124. Label *error_label = nullptr;
  125. void ok_pressed() override;
  126. void _cancel_pressed();
  127. void _item_activated();
  128. void _tree_node_selected();
  129. void _focus_currently_connected();
  130. void _method_selected();
  131. void _create_method_tree_items(const List<MethodInfo> &p_methods, TreeItem *p_parent_item);
  132. List<MethodInfo> _filter_method_list(const List<MethodInfo> &p_methods, const MethodInfo &p_signal, const String &p_search_string) const;
  133. void _update_method_tree();
  134. void _method_check_button_pressed(const CheckButton *p_button);
  135. void _open_method_popup();
  136. void _unbind_count_changed(double p_count);
  137. void _add_bind();
  138. void _remove_bind();
  139. void _advanced_pressed();
  140. void _update_ok_enabled();
  141. void _update_warning_label();
  142. protected:
  143. virtual void _post_popup() override;
  144. void _notification(int p_what);
  145. static void _bind_methods();
  146. public:
  147. static StringName generate_method_callback_name(Node *p_source, const String &p_signal_name, Node *p_target);
  148. Node *get_source() const;
  149. ConnectionData get_source_connection_data() const;
  150. StringName get_signal_name() const;
  151. PackedStringArray get_signal_args() const;
  152. NodePath get_dst_path() const;
  153. void set_dst_node(Node *p_node);
  154. StringName get_dst_method_name() const;
  155. void set_dst_method(const StringName &p_method);
  156. int get_unbinds() const;
  157. Vector<Variant> get_binds() const;
  158. String get_signature(const MethodInfo &p_method, PackedStringArray *r_arg_names = nullptr);
  159. bool get_deferred() const;
  160. bool get_one_shot() const;
  161. bool is_editing() const;
  162. virtual void shortcut_input(const Ref<InputEvent> &p_event) override;
  163. void init(const ConnectionData &p_cd, const PackedStringArray &p_signal_args, bool p_edit = false);
  164. void popup_dialog(const String &p_for_signal);
  165. ConnectDialog();
  166. ~ConnectDialog();
  167. };
  168. //////////////////////////////////////////
  169. // Custom `Tree` needed to use `EditorHelpBit` to display signal documentation.
  170. class ConnectionsDockTree : public Tree {
  171. virtual Control *make_custom_tooltip(const String &p_text) const;
  172. };
  173. class ConnectionsDock : public VBoxContainer {
  174. GDCLASS(ConnectionsDock, VBoxContainer);
  175. enum TreeItemType {
  176. TREE_ITEM_TYPE_ROOT,
  177. TREE_ITEM_TYPE_CLASS,
  178. TREE_ITEM_TYPE_SIGNAL,
  179. TREE_ITEM_TYPE_CONNECTION,
  180. };
  181. // Right-click context menu options.
  182. enum ClassMenuOption {
  183. CLASS_MENU_OPEN_DOCS,
  184. };
  185. enum SignalMenuOption {
  186. SIGNAL_MENU_CONNECT,
  187. SIGNAL_MENU_DISCONNECT_ALL,
  188. SIGNAL_MENU_COPY_NAME,
  189. SIGNAL_MENU_OPEN_DOCS,
  190. };
  191. enum SlotMenuOption {
  192. SLOT_MENU_EDIT,
  193. SLOT_MENU_GO_TO_METHOD,
  194. SLOT_MENU_DISCONNECT,
  195. };
  196. Node *selected_node = nullptr;
  197. ConnectionsDockTree *tree = nullptr;
  198. ConfirmationDialog *disconnect_all_dialog = nullptr;
  199. ConnectDialog *connect_dialog = nullptr;
  200. Button *connect_button = nullptr;
  201. PopupMenu *class_menu = nullptr;
  202. String class_menu_doc_class_name;
  203. PopupMenu *signal_menu = nullptr;
  204. PopupMenu *slot_menu = nullptr;
  205. LineEdit *search_box = nullptr;
  206. void _filter_changed(const String &p_text);
  207. void _make_or_edit_connection();
  208. void _connect(const ConnectDialog::ConnectionData &p_cd);
  209. void _disconnect(const ConnectDialog::ConnectionData &p_cd);
  210. void _disconnect_all();
  211. void _tree_item_selected();
  212. void _tree_item_activated();
  213. TreeItemType _get_item_type(const TreeItem &p_item) const;
  214. bool _is_connection_inherited(Connection &p_connection);
  215. void _open_connection_dialog(TreeItem &p_item);
  216. void _open_edit_connection_dialog(TreeItem &p_item);
  217. void _go_to_method(TreeItem &p_item);
  218. void _handle_class_menu_option(int p_option);
  219. void _class_menu_about_to_popup();
  220. void _handle_signal_menu_option(int p_option);
  221. void _signal_menu_about_to_popup();
  222. void _handle_slot_menu_option(int p_option);
  223. void _slot_menu_about_to_popup();
  224. void _tree_gui_input(const Ref<InputEvent> &p_event);
  225. void _close();
  226. protected:
  227. void _connect_pressed();
  228. void _notification(int p_what);
  229. static void _bind_methods();
  230. public:
  231. void set_node(Node *p_node);
  232. void update_tree();
  233. ConnectionsDock();
  234. ~ConnectionsDock();
  235. };
  236. #endif // CONNECTIONS_DIALOG_H