connections_dialog.h 9.4 KB

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