2
0

action_map_editor.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /*************************************************************************/
  2. /* action_map_editor.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
  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 ACTION_MAP_EDITOR_H
  31. #define ACTION_MAP_EDITOR_H
  32. #include "editor/editor_data.h"
  33. #include <scene/gui/color_rect.h>
  34. // Confirmation Dialog used when configuring an input event.
  35. // Separate from ActionMapEditor for code cleanliness and separation of responsibilities.
  36. class InputEventConfigurationDialog : public ConfirmationDialog {
  37. GDCLASS(InputEventConfigurationDialog, ConfirmationDialog);
  38. public:
  39. enum InputType {
  40. INPUT_KEY = 1,
  41. INPUT_MOUSE_BUTTON = 2,
  42. INPUT_JOY_BUTTON = 4,
  43. INPUT_JOY_MOTION = 8
  44. };
  45. private:
  46. struct IconCache {
  47. Ref<Texture2D> keyboard;
  48. Ref<Texture2D> mouse;
  49. Ref<Texture2D> joypad_button;
  50. Ref<Texture2D> joypad_axis;
  51. } icon_cache;
  52. Ref<InputEvent> event = Ref<InputEvent>();
  53. TabContainer *tab_container;
  54. // Listening for input
  55. Label *event_as_text;
  56. ColorRect *mouse_detection_rect;
  57. // List of All Key/Mouse/Joypad input options.
  58. int allowed_input_types;
  59. Tree *input_list_tree;
  60. LineEdit *input_list_search;
  61. // Additional Options, shown depending on event selected
  62. VBoxContainer *additional_options_container;
  63. HBoxContainer *device_container;
  64. OptionButton *device_id_option;
  65. HBoxContainer *mod_container; // Contains the subcontainer and the store command checkbox.
  66. enum ModCheckbox {
  67. MOD_ALT,
  68. MOD_SHIFT,
  69. MOD_COMMAND,
  70. MOD_CTRL,
  71. MOD_META,
  72. MOD_MAX
  73. };
  74. String mods[MOD_MAX] = { "Alt", "Shift", "Command", "Ctrl", "Metakey" };
  75. CheckBox *mod_checkboxes[MOD_MAX];
  76. CheckBox *store_command_checkbox;
  77. CheckBox *physical_key_checkbox;
  78. void _set_event(const Ref<InputEvent> &p_event);
  79. void _tab_selected(int p_tab);
  80. void _listen_window_input(const Ref<InputEvent> &p_event);
  81. void _search_term_updated(const String &p_term);
  82. void _update_input_list();
  83. void _input_list_item_selected();
  84. void _mod_toggled(bool p_checked, int p_index);
  85. void _store_command_toggled(bool p_checked);
  86. void _physical_keycode_toggled(bool p_checked);
  87. void _set_current_device(int i_device);
  88. int _get_current_device() const;
  89. String _get_device_string(int i_device) const;
  90. protected:
  91. void _notification(int p_what);
  92. public:
  93. // Pass an existing event to configure it. Alternatively, pass no event to start with a blank configuration.
  94. void popup_and_configure(const Ref<InputEvent> &p_event = Ref<InputEvent>());
  95. Ref<InputEvent> get_event() const;
  96. String get_event_text(const Ref<InputEvent> &p_event);
  97. void set_allowed_input_types(int p_type_masks);
  98. InputEventConfigurationDialog();
  99. };
  100. class ActionMapEditor : public Control {
  101. GDCLASS(ActionMapEditor, Control);
  102. public:
  103. struct ActionInfo {
  104. String name = String();
  105. Dictionary action = Dictionary();
  106. Ref<Texture2D> icon = Ref<Texture2D>();
  107. bool editable = true;
  108. };
  109. private:
  110. enum ItemButton {
  111. BUTTON_ADD_EVENT,
  112. BUTTON_EDIT_EVENT,
  113. BUTTON_REMOVE_ACTION,
  114. BUTTON_REMOVE_EVENT,
  115. };
  116. Vector<ActionInfo> actions_cache;
  117. Tree *action_tree;
  118. // Storing which action/event is currently being edited in the InputEventConfigurationDialog.
  119. Dictionary current_action = Dictionary();
  120. String current_action_name = String();
  121. int current_action_event_index = -1;
  122. // Popups
  123. InputEventConfigurationDialog *event_config_dialog;
  124. AcceptDialog *message;
  125. // Filtering and Adding actions
  126. bool show_builtin_actions;
  127. CheckButton *show_builtin_actions_checkbutton;
  128. LineEdit *action_list_search;
  129. HBoxContainer *add_hbox;
  130. LineEdit *add_edit;
  131. void _event_config_confirmed();
  132. void _add_action_pressed();
  133. void _add_action(const String &p_name);
  134. void _action_edited();
  135. void _tree_button_pressed(Object *p_item, int p_column, int p_id);
  136. void _tree_item_activated();
  137. void _search_term_updated(const String &p_search_term);
  138. Variant get_drag_data_fw(const Point2 &p_point, Control *p_from);
  139. bool can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) const;
  140. void drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from);
  141. protected:
  142. void _notification(int p_what);
  143. static void _bind_methods();
  144. public:
  145. LineEdit *get_search_box() const;
  146. InputEventConfigurationDialog *get_configuration_dialog();
  147. // Dictionary represents an Action with "events" (Array) and "deadzone" (float) items. Pass with no param to update list from cached action map.
  148. void update_action_list(const Vector<ActionInfo> &p_action_infos = Vector<ActionInfo>());
  149. void show_message(const String &p_message);
  150. void set_show_builtin_actions(bool p_show);
  151. void use_external_search_box(LineEdit *p_searchbox);
  152. ActionMapEditor();
  153. };
  154. #endif