openxr_select_interaction_profile_dialog.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /**************************************************************************/
  2. /* openxr_select_interaction_profile_dialog.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_select_interaction_profile_dialog.h"
  31. #include "../action_map/openxr_interaction_profile_metadata.h"
  32. #include "../openxr_api.h"
  33. #include "editor/themes/editor_scale.h"
  34. void OpenXRSelectInteractionProfileDialog::_bind_methods() {
  35. ADD_SIGNAL(MethodInfo("interaction_profile_selected", PropertyInfo(Variant::STRING, "interaction_profile")));
  36. }
  37. void OpenXRSelectInteractionProfileDialog::_notification(int p_what) {
  38. switch (p_what) {
  39. case NOTIFICATION_THEME_CHANGED: {
  40. scroll->add_theme_style_override(SceneStringName(panel), get_theme_stylebox(SceneStringName(panel), SNAME("Tree")));
  41. } break;
  42. }
  43. }
  44. void OpenXRSelectInteractionProfileDialog::_on_select_interaction_profile(const String &p_interaction_profile) {
  45. if (selected_interaction_profile != "") {
  46. NodePath button_path = ip_buttons[selected_interaction_profile];
  47. Button *button = Object::cast_to<Button>(get_node(button_path));
  48. if (button != nullptr) {
  49. button->set_flat(true);
  50. }
  51. }
  52. selected_interaction_profile = p_interaction_profile;
  53. if (selected_interaction_profile != "") {
  54. NodePath button_path = ip_buttons[selected_interaction_profile];
  55. Button *button = Object::cast_to<Button>(get_node(button_path));
  56. if (button != nullptr) {
  57. button->set_flat(false);
  58. }
  59. }
  60. }
  61. void OpenXRSelectInteractionProfileDialog::open(const PackedStringArray &p_do_not_include) {
  62. int available_count = 0;
  63. OpenXRInteractionProfileMetadata *meta_data = OpenXRInteractionProfileMetadata::get_singleton();
  64. ERR_FAIL_NULL(meta_data);
  65. // Out with the old.
  66. while (main_vb->get_child_count() > 1) {
  67. memdelete(main_vb->get_child(1));
  68. }
  69. PackedStringArray requested_extensions = OpenXRAPI::get_all_requested_extensions(0);
  70. selected_interaction_profile = "";
  71. ip_buttons.clear();
  72. // In with the new.
  73. PackedStringArray interaction_profiles = meta_data->get_interaction_profile_paths();
  74. for (const String &path : interaction_profiles) {
  75. const Vector<String> extensions = meta_data->get_interaction_profile_extensions(path).split(",", false);
  76. bool extension_is_requested = extensions.is_empty(); // If none, then yes we can use this.
  77. for (const String &extension : extensions) {
  78. extension_is_requested |= requested_extensions.has(extension);
  79. }
  80. if (!p_do_not_include.has(path) && extension_is_requested) {
  81. Button *ip_button = memnew(Button);
  82. ip_button->set_flat(true);
  83. ip_button->set_text(meta_data->get_profile(path)->display_name);
  84. ip_button->set_text_alignment(HORIZONTAL_ALIGNMENT_LEFT);
  85. ip_button->connect(SceneStringName(pressed), callable_mp(this, &OpenXRSelectInteractionProfileDialog::_on_select_interaction_profile).bind(path));
  86. main_vb->add_child(ip_button);
  87. ip_buttons[path] = ip_button->get_path();
  88. available_count++;
  89. }
  90. }
  91. all_selected->set_visible(available_count == 0);
  92. get_cancel_button()->set_visible(available_count > 0);
  93. popup_centered();
  94. }
  95. void OpenXRSelectInteractionProfileDialog::ok_pressed() {
  96. if (selected_interaction_profile != "") {
  97. emit_signal("interaction_profile_selected", selected_interaction_profile);
  98. }
  99. hide();
  100. }
  101. OpenXRSelectInteractionProfileDialog::OpenXRSelectInteractionProfileDialog() {
  102. set_title(TTR("Select an interaction profile"));
  103. scroll = memnew(ScrollContainer);
  104. scroll->set_custom_minimum_size(Size2(600.0 * EDSCALE, 400.0 * EDSCALE));
  105. add_child(scroll);
  106. main_vb = memnew(VBoxContainer);
  107. main_vb->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  108. scroll->add_child(main_vb);
  109. all_selected = memnew(Label);
  110. all_selected->set_focus_mode(Control::FOCUS_ACCESSIBILITY);
  111. all_selected->set_text(TTR("All interaction profiles have been added to the action map."));
  112. main_vb->add_child(all_selected);
  113. }