connections_dialog.h 8.9 KB

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