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