1 | /**************************************************************************/ |
2 | /* openxr_interaction_profile_editor.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 "openxr_interaction_profile_editor.h" |
32 | |
33 | #include "editor/editor_string_names.h" |
34 | #include "scene/gui/box_container.h" |
35 | #include "scene/gui/button.h" |
36 | #include "scene/gui/label.h" |
37 | #include "scene/gui/line_edit.h" |
38 | #include "scene/gui/panel_container.h" |
39 | #include "scene/gui/separator.h" |
40 | #include "scene/gui/text_edit.h" |
41 | |
42 | /////////////////////////////////////////////////////////////////////////// |
43 | // Interaction profile editor base |
44 | |
45 | void OpenXRInteractionProfileEditorBase::_bind_methods() { |
46 | ClassDB::bind_method(D_METHOD("_add_binding" , "action" , "path" ), &OpenXRInteractionProfileEditorBase::_add_binding); |
47 | ClassDB::bind_method(D_METHOD("_remove_binding" , "action" , "path" ), &OpenXRInteractionProfileEditorBase::_remove_binding); |
48 | ClassDB::bind_method(D_METHOD("_update_interaction_profile" ), &OpenXRInteractionProfileEditorBase::_update_interaction_profile); |
49 | } |
50 | |
51 | void OpenXRInteractionProfileEditorBase::_notification(int p_what) { |
52 | switch (p_what) { |
53 | case NOTIFICATION_ENTER_TREE: { |
54 | _update_interaction_profile(); |
55 | } break; |
56 | |
57 | case NOTIFICATION_THEME_CHANGED: { |
58 | _theme_changed(); |
59 | } break; |
60 | } |
61 | } |
62 | |
63 | void OpenXRInteractionProfileEditorBase::_do_update_interaction_profile() { |
64 | if (!is_dirty) { |
65 | is_dirty = true; |
66 | call_deferred("_update_interaction_profile" ); |
67 | } |
68 | } |
69 | |
70 | void OpenXRInteractionProfileEditorBase::_add_binding(const String p_action, const String p_path) { |
71 | ERR_FAIL_COND(action_map.is_null()); |
72 | ERR_FAIL_COND(interaction_profile.is_null()); |
73 | |
74 | Ref<OpenXRAction> action = action_map->get_action(p_action); |
75 | ERR_FAIL_COND(action.is_null()); |
76 | |
77 | Ref<OpenXRIPBinding> binding = interaction_profile->get_binding_for_action(action); |
78 | if (binding.is_null()) { |
79 | // create a new binding |
80 | binding.instantiate(); |
81 | binding->set_action(action); |
82 | interaction_profile->add_binding(binding); |
83 | interaction_profile->set_edited(true); |
84 | } |
85 | |
86 | binding->add_path(p_path); |
87 | binding->set_edited(true); |
88 | |
89 | // Update our toplevel paths |
90 | action->set_toplevel_paths(action_map->get_top_level_paths(action)); |
91 | |
92 | _do_update_interaction_profile(); |
93 | } |
94 | |
95 | void OpenXRInteractionProfileEditorBase::_remove_binding(const String p_action, const String p_path) { |
96 | ERR_FAIL_COND(action_map.is_null()); |
97 | ERR_FAIL_COND(interaction_profile.is_null()); |
98 | |
99 | Ref<OpenXRAction> action = action_map->get_action(p_action); |
100 | ERR_FAIL_COND(action.is_null()); |
101 | |
102 | Ref<OpenXRIPBinding> binding = interaction_profile->get_binding_for_action(action); |
103 | if (binding.is_valid()) { |
104 | binding->remove_path(p_path); |
105 | binding->set_edited(true); |
106 | |
107 | if (binding->get_path_count() == 0) { |
108 | interaction_profile->remove_binding(binding); |
109 | interaction_profile->set_edited(true); |
110 | } |
111 | |
112 | // Update our toplevel paths |
113 | action->set_toplevel_paths(action_map->get_top_level_paths(action)); |
114 | |
115 | _do_update_interaction_profile(); |
116 | } |
117 | } |
118 | |
119 | void OpenXRInteractionProfileEditorBase::remove_all_bindings_for_action(Ref<OpenXRAction> p_action) { |
120 | Ref<OpenXRIPBinding> binding = interaction_profile->get_binding_for_action(p_action); |
121 | if (binding.is_valid()) { |
122 | String action_name = p_action->get_name_with_set(); |
123 | |
124 | // for our undo/redo we process all paths |
125 | undo_redo->create_action(TTR("Remove action from interaction profile" )); |
126 | PackedStringArray paths = binding->get_paths(); |
127 | for (const String &path : paths) { |
128 | undo_redo->add_do_method(this, "_remove_binding" , action_name, path); |
129 | undo_redo->add_undo_method(this, "_add_binding" , action_name, path); |
130 | } |
131 | undo_redo->commit_action(false); |
132 | |
133 | // but we take a shortcut here :) |
134 | interaction_profile->remove_binding(binding); |
135 | interaction_profile->set_edited(true); |
136 | |
137 | // Update our toplevel paths |
138 | p_action->set_toplevel_paths(action_map->get_top_level_paths(p_action)); |
139 | |
140 | _do_update_interaction_profile(); |
141 | } |
142 | } |
143 | |
144 | OpenXRInteractionProfileEditorBase::OpenXRInteractionProfileEditorBase(Ref<OpenXRActionMap> p_action_map, Ref<OpenXRInteractionProfile> p_interaction_profile) { |
145 | undo_redo = EditorUndoRedoManager::get_singleton(); |
146 | |
147 | action_map = p_action_map; |
148 | interaction_profile = p_interaction_profile; |
149 | String profile_path = interaction_profile->get_interaction_profile_path(); |
150 | String profile_name = profile_path; |
151 | |
152 | profile_def = OpenXRInteractionProfileMetadata::get_singleton()->get_profile(profile_path); |
153 | if (profile_def != nullptr) { |
154 | profile_name = profile_def->display_name; |
155 | } |
156 | |
157 | set_name(profile_name); |
158 | set_h_size_flags(SIZE_EXPAND_FILL); |
159 | set_v_size_flags(SIZE_EXPAND_FILL); |
160 | |
161 | // Make sure it is updated when it enters the tree... |
162 | is_dirty = true; |
163 | } |
164 | |
165 | /////////////////////////////////////////////////////////////////////////// |
166 | // Default interaction profile editor |
167 | |
168 | void OpenXRInteractionProfileEditor::select_action_for(const String p_io_path) { |
169 | selecting_for_io_path = p_io_path; |
170 | select_action_dialog->open(); |
171 | } |
172 | |
173 | void OpenXRInteractionProfileEditor::action_selected(const String p_action) { |
174 | undo_redo->create_action(TTR("Add binding" )); |
175 | undo_redo->add_do_method(this, "_add_binding" , p_action, selecting_for_io_path); |
176 | undo_redo->add_undo_method(this, "_remove_binding" , p_action, selecting_for_io_path); |
177 | undo_redo->commit_action(true); |
178 | |
179 | selecting_for_io_path = "" ; |
180 | } |
181 | |
182 | void OpenXRInteractionProfileEditor::_on_remove_pressed(const String p_action, const String p_for_io_path) { |
183 | undo_redo->create_action(TTR("Remove binding" )); |
184 | undo_redo->add_do_method(this, "_remove_binding" , p_action, p_for_io_path); |
185 | undo_redo->add_undo_method(this, "_add_binding" , p_action, p_for_io_path); |
186 | undo_redo->commit_action(true); |
187 | } |
188 | |
189 | void OpenXRInteractionProfileEditor::_add_io_path(VBoxContainer *p_container, const OpenXRInteractionProfileMetadata::IOPath *p_io_path) { |
190 | HBoxContainer *path_hb = memnew(HBoxContainer); |
191 | path_hb->set_h_size_flags(Control::SIZE_EXPAND_FILL); |
192 | p_container->add_child(path_hb); |
193 | |
194 | Label *path_label = memnew(Label); |
195 | path_label->set_text(p_io_path->display_name); |
196 | path_label->set_h_size_flags(Control::SIZE_EXPAND_FILL); |
197 | path_hb->add_child(path_label); |
198 | |
199 | Label *type_label = memnew(Label); |
200 | switch (p_io_path->action_type) { |
201 | case OpenXRAction::OPENXR_ACTION_BOOL: { |
202 | type_label->set_text(TTR("Boolean" )); |
203 | } break; |
204 | case OpenXRAction::OPENXR_ACTION_FLOAT: { |
205 | type_label->set_text(TTR("Float" )); |
206 | } break; |
207 | case OpenXRAction::OPENXR_ACTION_VECTOR2: { |
208 | type_label->set_text(TTR("Vector2" )); |
209 | } break; |
210 | case OpenXRAction::OPENXR_ACTION_POSE: { |
211 | type_label->set_text(TTR("Pose" )); |
212 | } break; |
213 | case OpenXRAction::OPENXR_ACTION_HAPTIC: { |
214 | type_label->set_text(TTR("Haptic" )); |
215 | } break; |
216 | default: { |
217 | type_label->set_text(TTR("Unknown" )); |
218 | } break; |
219 | } |
220 | type_label->set_custom_minimum_size(Size2(50.0, 0.0)); |
221 | path_hb->add_child(type_label); |
222 | |
223 | Button *path_add = memnew(Button); |
224 | path_add->set_icon(get_theme_icon(SNAME("Add" ), EditorStringName(EditorIcons))); |
225 | path_add->set_flat(true); |
226 | path_add->connect("pressed" , callable_mp(this, &OpenXRInteractionProfileEditor::select_action_for).bind(String(p_io_path->openxr_path))); |
227 | path_hb->add_child(path_add); |
228 | |
229 | if (interaction_profile.is_valid()) { |
230 | String io_path = String(p_io_path->openxr_path); |
231 | Array bindings = interaction_profile->get_bindings(); |
232 | for (int i = 0; i < bindings.size(); i++) { |
233 | Ref<OpenXRIPBinding> binding = bindings[i]; |
234 | if (binding->has_path(io_path)) { |
235 | Ref<OpenXRAction> action = binding->get_action(); |
236 | |
237 | HBoxContainer *action_hb = memnew(HBoxContainer); |
238 | action_hb->set_h_size_flags(Control::SIZE_EXPAND_FILL); |
239 | p_container->add_child(action_hb); |
240 | |
241 | Control *indent_node = memnew(Control); |
242 | indent_node->set_custom_minimum_size(Size2(10.0, 0.0)); |
243 | action_hb->add_child(indent_node); |
244 | |
245 | Label *action_label = memnew(Label); |
246 | action_label->set_text(action->get_name_with_set() + ": " + action->get_localized_name()); |
247 | action_label->set_h_size_flags(Control::SIZE_EXPAND_FILL); |
248 | action_hb->add_child(action_label); |
249 | |
250 | Button *action_rem = memnew(Button); |
251 | action_rem->set_flat(true); |
252 | action_rem->set_icon(get_theme_icon(SNAME("Remove" ), EditorStringName(EditorIcons))); |
253 | action_rem->connect("pressed" , callable_mp((OpenXRInteractionProfileEditor *)this, &OpenXRInteractionProfileEditor::_on_remove_pressed).bind(action->get_name_with_set(), String(p_io_path->openxr_path))); |
254 | action_hb->add_child(action_rem); |
255 | } |
256 | } |
257 | } |
258 | } |
259 | |
260 | void OpenXRInteractionProfileEditor::_update_interaction_profile() { |
261 | ERR_FAIL_NULL(profile_def); |
262 | |
263 | if (!is_dirty) { |
264 | // no need to update |
265 | return; |
266 | } |
267 | |
268 | // out with the old... |
269 | while (main_hb->get_child_count() > 0) { |
270 | memdelete(main_hb->get_child(0)); |
271 | } |
272 | |
273 | // in with the new... |
274 | |
275 | // Determine toplevel paths |
276 | Vector<String> top_level_paths; |
277 | for (int i = 0; i < profile_def->io_paths.size(); i++) { |
278 | const OpenXRInteractionProfileMetadata::IOPath *io_path = &profile_def->io_paths[i]; |
279 | |
280 | if (!top_level_paths.has(io_path->top_level_path)) { |
281 | top_level_paths.push_back(io_path->top_level_path); |
282 | } |
283 | } |
284 | |
285 | for (int i = 0; i < top_level_paths.size(); i++) { |
286 | PanelContainer *panel = memnew(PanelContainer); |
287 | panel->set_v_size_flags(Control::SIZE_EXPAND_FILL); |
288 | main_hb->add_child(panel); |
289 | panel->add_theme_style_override("panel" , get_theme_stylebox(SNAME("panel" ), SNAME("TabContainer" ))); |
290 | |
291 | VBoxContainer *container = memnew(VBoxContainer); |
292 | panel->add_child(container); |
293 | |
294 | Label *label = memnew(Label); |
295 | label->set_text(OpenXRInteractionProfileMetadata::get_singleton()->get_top_level_name(top_level_paths[i])); |
296 | container->add_child(label); |
297 | |
298 | for (int j = 0; j < profile_def->io_paths.size(); j++) { |
299 | const OpenXRInteractionProfileMetadata::IOPath *io_path = &profile_def->io_paths[j]; |
300 | if (io_path->top_level_path == top_level_paths[i]) { |
301 | _add_io_path(container, io_path); |
302 | } |
303 | } |
304 | } |
305 | |
306 | // and we've updated it... |
307 | is_dirty = false; |
308 | } |
309 | |
310 | void OpenXRInteractionProfileEditor::_theme_changed() { |
311 | for (int i = 0; i < main_hb->get_child_count(); i++) { |
312 | Control *panel = Object::cast_to<Control>(main_hb->get_child(i)); |
313 | if (panel) { |
314 | panel->add_theme_style_override("panel" , get_theme_stylebox(SNAME("panel" ), SNAME("TabContainer" ))); |
315 | } |
316 | } |
317 | } |
318 | |
319 | OpenXRInteractionProfileEditor::OpenXRInteractionProfileEditor(Ref<OpenXRActionMap> p_action_map, Ref<OpenXRInteractionProfile> p_interaction_profile) : |
320 | OpenXRInteractionProfileEditorBase(p_action_map, p_interaction_profile) { |
321 | main_hb = memnew(HBoxContainer); |
322 | add_child(main_hb); |
323 | |
324 | select_action_dialog = memnew(OpenXRSelectActionDialog(p_action_map)); |
325 | select_action_dialog->connect("action_selected" , callable_mp(this, &OpenXRInteractionProfileEditor::action_selected)); |
326 | add_child(select_action_dialog); |
327 | } |
328 | |