connections_dialog.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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 *error_label = nullptr;
  124. void ok_pressed() override;
  125. void _cancel_pressed();
  126. void _item_activated();
  127. void _text_submitted(const String &p_text);
  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. protected:
  142. void _notification(int p_what);
  143. static void _bind_methods();
  144. public:
  145. static StringName generate_method_callback_name(Node *p_source, String p_signal_name, Node *p_target);
  146. Node *get_source() const;
  147. ConnectionData get_source_connection_data() const;
  148. StringName get_signal_name() const;
  149. PackedStringArray get_signal_args() const;
  150. NodePath get_dst_path() const;
  151. void set_dst_node(Node *p_node);
  152. StringName get_dst_method_name() const;
  153. void set_dst_method(const StringName &p_method);
  154. int get_unbinds() const;
  155. Vector<Variant> get_binds() const;
  156. String get_signature(const MethodInfo &p_method, PackedStringArray *r_arg_names = nullptr);
  157. bool get_deferred() const;
  158. bool get_one_shot() const;
  159. bool is_editing() const;
  160. void init(const ConnectionData &p_cd, const PackedStringArray &p_signal_args, bool p_edit = false);
  161. void popup_dialog(const String p_for_signal);
  162. ConnectDialog();
  163. ~ConnectDialog();
  164. };
  165. //////////////////////////////////////////
  166. // Custom Tree needed to use a RichTextLabel as tooltip control
  167. // when display signal documentation.
  168. class ConnectionsDockTree : public Tree {
  169. virtual Control *make_custom_tooltip(const String &p_text) const;
  170. };
  171. class ConnectionsDock : public VBoxContainer {
  172. GDCLASS(ConnectionsDock, VBoxContainer);
  173. //Right-click Pop-up Menu Options.
  174. enum SignalMenuOption {
  175. CONNECT,
  176. DISCONNECT_ALL,
  177. COPY_NAME,
  178. };
  179. enum SlotMenuOption {
  180. EDIT,
  181. GO_TO_SCRIPT,
  182. DISCONNECT,
  183. };
  184. Node *selected_node = nullptr;
  185. ConnectionsDockTree *tree = nullptr;
  186. ConfirmationDialog *disconnect_all_dialog = nullptr;
  187. ConnectDialog *connect_dialog = nullptr;
  188. Button *connect_button = nullptr;
  189. PopupMenu *signal_menu = nullptr;
  190. PopupMenu *slot_menu = nullptr;
  191. LineEdit *search_box = nullptr;
  192. HashMap<StringName, HashMap<StringName, String>> descr_cache;
  193. void _filter_changed(const String &p_text);
  194. void _make_or_edit_connection();
  195. void _connect(const ConnectDialog::ConnectionData &p_cd);
  196. void _disconnect(const ConnectDialog::ConnectionData &p_cd);
  197. void _disconnect_all();
  198. void _tree_item_selected();
  199. void _tree_item_activated();
  200. bool _is_item_signal(TreeItem &p_item);
  201. bool _is_connection_inherited(Connection &p_connection);
  202. void _open_connection_dialog(TreeItem &p_item);
  203. void _open_edit_connection_dialog(TreeItem &p_item);
  204. void _go_to_script(TreeItem &p_item);
  205. void _handle_signal_menu_option(int p_option);
  206. void _signal_menu_about_to_popup();
  207. void _handle_slot_menu_option(int p_option);
  208. void _slot_menu_about_to_popup();
  209. void _rmb_pressed(Vector2 p_position, MouseButton p_button);
  210. void _close();
  211. protected:
  212. void _connect_pressed();
  213. void _notification(int p_what);
  214. static void _bind_methods();
  215. public:
  216. void set_node(Node *p_node);
  217. void update_tree();
  218. ConnectionsDock();
  219. ~ConnectionsDock();
  220. };
  221. #endif // CONNECTIONS_DIALOG_H