openxr_interaction_profile_editor.cpp 12 KB

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