openxr_interaction_profile_editor.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  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 "scene/gui/box_container.h"
  32. #include "scene/gui/button.h"
  33. #include "scene/gui/label.h"
  34. #include "scene/gui/line_edit.h"
  35. #include "scene/gui/panel_container.h"
  36. #include "scene/gui/separator.h"
  37. #include "scene/gui/text_edit.h"
  38. ///////////////////////////////////////////////////////////////////////////
  39. // Interaction profile editor base
  40. void OpenXRInteractionProfileEditorBase::_bind_methods() {
  41. ClassDB::bind_method(D_METHOD("_add_binding", "action", "path"), &OpenXRInteractionProfileEditorBase::_add_binding);
  42. ClassDB::bind_method(D_METHOD("_remove_binding", "action", "path"), &OpenXRInteractionProfileEditorBase::_remove_binding);
  43. ClassDB::bind_method(D_METHOD("_update_interaction_profile"), &OpenXRInteractionProfileEditorBase::_update_interaction_profile);
  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::_add_binding(const String p_action, const String p_path) {
  56. ERR_FAIL_COND(action_map.is_null());
  57. ERR_FAIL_COND(interaction_profile.is_null());
  58. Ref<OpenXRAction> action = action_map->get_action(p_action);
  59. ERR_FAIL_COND(action.is_null());
  60. Ref<OpenXRIPBinding> binding = interaction_profile->get_binding_for_action(action);
  61. if (binding.is_null()) {
  62. // create a new binding
  63. binding.instantiate();
  64. binding->set_action(action);
  65. interaction_profile->add_binding(binding);
  66. }
  67. binding->add_path(p_path);
  68. // Update our toplevel paths
  69. action->set_toplevel_paths(action_map->get_top_level_paths(action));
  70. call_deferred("_update_interaction_profile");
  71. }
  72. void OpenXRInteractionProfileEditorBase::_remove_binding(const String p_action, const String p_path) {
  73. ERR_FAIL_COND(action_map.is_null());
  74. ERR_FAIL_COND(interaction_profile.is_null());
  75. Ref<OpenXRAction> action = action_map->get_action(p_action);
  76. ERR_FAIL_COND(action.is_null());
  77. Ref<OpenXRIPBinding> binding = interaction_profile->get_binding_for_action(action);
  78. if (binding.is_valid()) {
  79. binding->remove_path(p_path);
  80. if (binding->get_path_count() == 0) {
  81. interaction_profile->remove_binding(binding);
  82. }
  83. // Update our toplevel paths
  84. action->set_toplevel_paths(action_map->get_top_level_paths(action));
  85. call_deferred("_update_interaction_profile");
  86. }
  87. }
  88. OpenXRInteractionProfileEditorBase::OpenXRInteractionProfileEditorBase(Ref<OpenXRActionMap> p_action_map, Ref<OpenXRInteractionProfile> p_interaction_profile) {
  89. action_map = p_action_map;
  90. interaction_profile = p_interaction_profile;
  91. String profile_path = interaction_profile->get_interaction_profile_path();
  92. String profile_name = profile_path;
  93. profile_def = OpenXRDefs::get_profile(profile_path);
  94. if (profile_def != nullptr) {
  95. profile_name = profile_def->display_name;
  96. }
  97. set_name(profile_name);
  98. set_h_size_flags(SIZE_EXPAND_FILL);
  99. set_v_size_flags(SIZE_EXPAND_FILL);
  100. }
  101. ///////////////////////////////////////////////////////////////////////////
  102. // Default interaction profile editor
  103. void OpenXRInteractionProfileEditor::select_action_for(const String p_io_path) {
  104. selecting_for_io_path = p_io_path;
  105. select_action_dialog->open();
  106. }
  107. void OpenXRInteractionProfileEditor::action_selected(const String p_action) {
  108. _add_binding(p_action, selecting_for_io_path);
  109. selecting_for_io_path = "";
  110. }
  111. void OpenXRInteractionProfileEditor::_add_io_path(VBoxContainer *p_container, const OpenXRDefs::IOPath *p_io_path) {
  112. HBoxContainer *path_hb = memnew(HBoxContainer);
  113. path_hb->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  114. p_container->add_child(path_hb);
  115. Label *path_label = memnew(Label);
  116. path_label->set_text(p_io_path->display_name);
  117. path_label->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  118. path_hb->add_child(path_label);
  119. Label *type_label = memnew(Label);
  120. switch (p_io_path->action_type) {
  121. case OpenXRAction::OPENXR_ACTION_BOOL: {
  122. type_label->set_text(TTR("Boolean"));
  123. } break;
  124. case OpenXRAction::OPENXR_ACTION_FLOAT: {
  125. type_label->set_text(TTR("Float"));
  126. } break;
  127. case OpenXRAction::OPENXR_ACTION_VECTOR2: {
  128. type_label->set_text(TTR("Vector2"));
  129. } break;
  130. case OpenXRAction::OPENXR_ACTION_POSE: {
  131. type_label->set_text(TTR("Pose"));
  132. } break;
  133. case OpenXRAction::OPENXR_ACTION_HAPTIC: {
  134. type_label->set_text(TTR("Haptic"));
  135. } break;
  136. default: {
  137. type_label->set_text(TTR("Unknown"));
  138. } break;
  139. }
  140. type_label->set_custom_minimum_size(Size2(50.0, 0.0));
  141. path_hb->add_child(type_label);
  142. Button *path_add = memnew(Button);
  143. path_add->set_icon(get_theme_icon(SNAME("Add"), SNAME("EditorIcons")));
  144. path_add->set_flat(true);
  145. Vector<Variant> add_binds;
  146. add_binds.push_back(String(p_io_path->openxr_path));
  147. path_add->connect("pressed", callable_mp(this, &OpenXRInteractionProfileEditor::select_action_for), add_binds);
  148. path_hb->add_child(path_add);
  149. if (interaction_profile.is_valid()) {
  150. String io_path = String(p_io_path->openxr_path);
  151. Array bindings = interaction_profile->get_bindings();
  152. for (int i = 0; i < bindings.size(); i++) {
  153. Ref<OpenXRIPBinding> binding = bindings[i];
  154. if (binding->has_path(io_path)) {
  155. Ref<OpenXRAction> action = binding->get_action();
  156. HBoxContainer *action_hb = memnew(HBoxContainer);
  157. action_hb->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  158. p_container->add_child(action_hb);
  159. Control *indent_node = memnew(Control);
  160. indent_node->set_custom_minimum_size(Size2(10.0, 0.0));
  161. action_hb->add_child(indent_node);
  162. Label *action_label = memnew(Label);
  163. action_label->set_text(action->get_name_with_set() + ": " + action->get_localized_name());
  164. action_label->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  165. action_hb->add_child(action_label);
  166. Button *action_rem = memnew(Button);
  167. action_rem->set_flat(true);
  168. action_rem->set_icon(get_theme_icon(SNAME("Remove"), SNAME("EditorIcons")));
  169. Vector<Variant> remove_binds;
  170. remove_binds.push_back(action->get_name_with_set());
  171. remove_binds.push_back(String(p_io_path->openxr_path));
  172. action_rem->connect("pressed", callable_mp((OpenXRInteractionProfileEditorBase *)this, &OpenXRInteractionProfileEditorBase::_remove_binding), remove_binds);
  173. action_hb->add_child(action_rem);
  174. }
  175. }
  176. }
  177. }
  178. void OpenXRInteractionProfileEditor::_update_interaction_profile() {
  179. ERR_FAIL_NULL(profile_def);
  180. // out with the old...
  181. while (main_hb->get_child_count() > 0) {
  182. memdelete(main_hb->get_child(0));
  183. }
  184. // in with the new...
  185. // Determine toplevel paths
  186. Vector<const OpenXRDefs::TopLevelPath *> top_level_paths;
  187. for (int i = 0; i < profile_def->io_path_count; i++) {
  188. const OpenXRDefs::IOPath *io_path = &profile_def->io_paths[i];
  189. if (!top_level_paths.has(io_path->top_level_path)) {
  190. top_level_paths.push_back(io_path->top_level_path);
  191. }
  192. }
  193. for (int i = 0; i < top_level_paths.size(); i++) {
  194. PanelContainer *panel = memnew(PanelContainer);
  195. panel->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  196. main_hb->add_child(panel);
  197. panel->add_theme_style_override("panel", get_theme_stylebox(SNAME("panel"), SNAME("TabContainer")));
  198. VBoxContainer *container = memnew(VBoxContainer);
  199. panel->add_child(container);
  200. Label *label = memnew(Label);
  201. label->set_text(top_level_paths[i]->display_name);
  202. container->add_child(label);
  203. for (int j = 0; j < profile_def->io_path_count; j++) {
  204. const OpenXRDefs::IOPath *io_path = &profile_def->io_paths[j];
  205. if (io_path->top_level_path == top_level_paths[i]) {
  206. _add_io_path(container, io_path);
  207. }
  208. }
  209. }
  210. }
  211. void OpenXRInteractionProfileEditor::_theme_changed() {
  212. for (int i = 0; i < main_hb->get_child_count(); i++) {
  213. Control *panel = static_cast<Control *>(main_hb->get_child(i));
  214. if (panel) {
  215. panel->add_theme_style_override("panel", get_theme_stylebox(SNAME("panel"), SNAME("TabContainer")));
  216. }
  217. }
  218. }
  219. OpenXRInteractionProfileEditor::OpenXRInteractionProfileEditor(Ref<OpenXRActionMap> p_action_map, Ref<OpenXRInteractionProfile> p_interaction_profile) :
  220. OpenXRInteractionProfileEditorBase(p_action_map, p_interaction_profile) {
  221. // TODO background of scrollbox should be darker with our VBoxContainers we're adding in _update_interaction_profile the normal color
  222. main_hb = memnew(HBoxContainer);
  223. add_child(main_hb);
  224. select_action_dialog = memnew(OpenXRSelectActionDialog(p_action_map));
  225. select_action_dialog->connect("action_selected", callable_mp(this, &OpenXRInteractionProfileEditor::action_selected));
  226. add_child(select_action_dialog);
  227. _update_interaction_profile();
  228. }