action_map_editor.h 7.3 KB

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