input_event_editor_plugin.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /**************************************************************************/
  2. /* input_event_editor_plugin.cpp */
  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. #include "input_event_editor_plugin.h"
  31. #include "editor/settings/event_listener_line_edit.h"
  32. #include "editor/settings/input_event_configuration_dialog.h"
  33. void InputEventConfigContainer::_notification(int p_what) {
  34. switch (p_what) {
  35. case NOTIFICATION_THEME_CHANGED: {
  36. open_config_button->set_button_icon(get_editor_theme_icon(SNAME("Edit")));
  37. } break;
  38. }
  39. }
  40. void InputEventConfigContainer::_configure_pressed() {
  41. config_dialog->popup_and_configure(input_event);
  42. }
  43. void InputEventConfigContainer::_event_changed() {
  44. input_event_text->set_text(input_event->as_text());
  45. }
  46. void InputEventConfigContainer::_config_dialog_confirmed() {
  47. Ref<InputEvent> ie = config_dialog->get_event();
  48. input_event->copy_from(ie);
  49. _event_changed();
  50. }
  51. void InputEventConfigContainer::set_event(const Ref<InputEvent> &p_event) {
  52. Ref<InputEventKey> k = p_event;
  53. Ref<InputEventMouseButton> m = p_event;
  54. Ref<InputEventJoypadButton> jb = p_event;
  55. Ref<InputEventJoypadMotion> jm = p_event;
  56. if (k.is_valid()) {
  57. config_dialog->set_allowed_input_types(INPUT_KEY);
  58. } else if (m.is_valid()) {
  59. config_dialog->set_allowed_input_types(INPUT_MOUSE_BUTTON);
  60. } else if (jb.is_valid()) {
  61. config_dialog->set_allowed_input_types(INPUT_JOY_BUTTON);
  62. } else if (jm.is_valid()) {
  63. config_dialog->set_allowed_input_types(INPUT_JOY_MOTION);
  64. }
  65. input_event = p_event;
  66. _event_changed();
  67. input_event->connect_changed(callable_mp(this, &InputEventConfigContainer::_event_changed));
  68. }
  69. InputEventConfigContainer::InputEventConfigContainer() {
  70. input_event_text = memnew(Label);
  71. input_event_text->set_focus_mode(FOCUS_ACCESSIBILITY);
  72. input_event_text->set_h_size_flags(SIZE_EXPAND_FILL);
  73. input_event_text->set_autowrap_mode(TextServer::AutowrapMode::AUTOWRAP_WORD_SMART);
  74. input_event_text->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_CENTER);
  75. add_child(input_event_text);
  76. open_config_button = EditorInspector::create_inspector_action_button(TTR("Configure"));
  77. open_config_button->connect(SceneStringName(pressed), callable_mp(this, &InputEventConfigContainer::_configure_pressed));
  78. add_child(open_config_button);
  79. add_child(memnew(Control));
  80. config_dialog = memnew(InputEventConfigurationDialog);
  81. config_dialog->connect(SceneStringName(confirmed), callable_mp(this, &InputEventConfigContainer::_config_dialog_confirmed));
  82. add_child(config_dialog);
  83. }
  84. ///////////////////////
  85. bool EditorInspectorPluginInputEvent::can_handle(Object *p_object) {
  86. Ref<InputEventKey> k = Ref<InputEventKey>(p_object);
  87. Ref<InputEventMouseButton> m = Ref<InputEventMouseButton>(p_object);
  88. Ref<InputEventJoypadButton> jb = Ref<InputEventJoypadButton>(p_object);
  89. Ref<InputEventJoypadMotion> jm = Ref<InputEventJoypadMotion>(p_object);
  90. return k.is_valid() || m.is_valid() || jb.is_valid() || jm.is_valid();
  91. }
  92. void EditorInspectorPluginInputEvent::parse_begin(Object *p_object) {
  93. Ref<InputEvent> ie = Ref<InputEvent>(p_object);
  94. InputEventConfigContainer *picker_controls = memnew(InputEventConfigContainer);
  95. picker_controls->set_event(ie);
  96. add_custom_control(picker_controls);
  97. }
  98. ///////////////////////
  99. InputEventEditorPlugin::InputEventEditorPlugin() {
  100. Ref<EditorInspectorPluginInputEvent> plugin;
  101. plugin.instantiate();
  102. add_inspector_plugin(plugin);
  103. }