input_event_editor_plugin.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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/event_listener_line_edit.h"
  32. #include "editor/input_event_configuration_dialog.h"
  33. void InputEventConfigContainer::_notification(int p_what) {
  34. switch (p_what) {
  35. case NOTIFICATION_ENTER_TREE:
  36. case NOTIFICATION_THEME_CHANGED: {
  37. open_config_button->set_button_icon(get_editor_theme_icon(SNAME("Edit")));
  38. } break;
  39. }
  40. }
  41. void InputEventConfigContainer::_configure_pressed() {
  42. config_dialog->popup_and_configure(input_event);
  43. }
  44. void InputEventConfigContainer::_event_changed() {
  45. input_event_text->set_text(input_event->as_text());
  46. }
  47. void InputEventConfigContainer::_config_dialog_confirmed() {
  48. Ref<InputEvent> ie = config_dialog->get_event();
  49. input_event->copy_from(ie);
  50. _event_changed();
  51. }
  52. void InputEventConfigContainer::set_event(const Ref<InputEvent> &p_event) {
  53. Ref<InputEventKey> k = p_event;
  54. Ref<InputEventMouseButton> m = p_event;
  55. Ref<InputEventJoypadButton> jb = p_event;
  56. Ref<InputEventJoypadMotion> jm = p_event;
  57. if (k.is_valid()) {
  58. config_dialog->set_allowed_input_types(INPUT_KEY);
  59. } else if (m.is_valid()) {
  60. config_dialog->set_allowed_input_types(INPUT_MOUSE_BUTTON);
  61. } else if (jb.is_valid()) {
  62. config_dialog->set_allowed_input_types(INPUT_JOY_BUTTON);
  63. } else if (jm.is_valid()) {
  64. config_dialog->set_allowed_input_types(INPUT_JOY_MOTION);
  65. }
  66. input_event = p_event;
  67. _event_changed();
  68. input_event->connect_changed(callable_mp(this, &InputEventConfigContainer::_event_changed));
  69. }
  70. InputEventConfigContainer::InputEventConfigContainer() {
  71. input_event_text = memnew(Label);
  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. }