openxr_interaction_profile_editor.cpp 16 KB

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