openxr_select_interaction_profile_dialog.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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_ENTER_TREE:
  40. case NOTIFICATION_THEME_CHANGED: {
  41. scroll->add_theme_style_override(SceneStringName(panel), get_theme_stylebox(SceneStringName(panel), SNAME("Tree")));
  42. } break;
  43. }
  44. }
  45. void OpenXRSelectInteractionProfileDialog::_on_select_interaction_profile(const String p_interaction_profile) {
  46. if (selected_interaction_profile != "") {
  47. NodePath button_path = ip_buttons[selected_interaction_profile];
  48. Button *button = Object::cast_to<Button>(get_node(button_path));
  49. if (button != nullptr) {
  50. button->set_flat(true);
  51. }
  52. }
  53. selected_interaction_profile = p_interaction_profile;
  54. if (selected_interaction_profile != "") {
  55. NodePath button_path = ip_buttons[selected_interaction_profile];
  56. Button *button = Object::cast_to<Button>(get_node(button_path));
  57. if (button != nullptr) {
  58. button->set_flat(false);
  59. }
  60. }
  61. }
  62. void OpenXRSelectInteractionProfileDialog::open(PackedStringArray p_do_not_include) {
  63. int available_count = 0;
  64. OpenXRInteractionProfileMetadata *meta_data = OpenXRInteractionProfileMetadata::get_singleton();
  65. ERR_FAIL_NULL(meta_data);
  66. // Out with the old.
  67. while (main_vb->get_child_count() > 1) {
  68. memdelete(main_vb->get_child(1));
  69. }
  70. PackedStringArray requested_extensions = OpenXRAPI::get_all_requested_extensions();
  71. selected_interaction_profile = "";
  72. ip_buttons.clear();
  73. // In with the new.
  74. PackedStringArray interaction_profiles = meta_data->get_interaction_profile_paths();
  75. for (const String &path : interaction_profiles) {
  76. const String extension = meta_data->get_interaction_profile_extension(path);
  77. if (!p_do_not_include.has(path) && (extension.is_empty() || requested_extensions.has(extension))) {
  78. Button *ip_button = memnew(Button);
  79. ip_button->set_flat(true);
  80. ip_button->set_text(meta_data->get_profile(path)->display_name);
  81. ip_button->set_text_alignment(HORIZONTAL_ALIGNMENT_LEFT);
  82. ip_button->connect(SceneStringName(pressed), callable_mp(this, &OpenXRSelectInteractionProfileDialog::_on_select_interaction_profile).bind(path));
  83. main_vb->add_child(ip_button);
  84. ip_buttons[path] = ip_button->get_path();
  85. available_count++;
  86. }
  87. }
  88. all_selected->set_visible(available_count == 0);
  89. get_cancel_button()->set_visible(available_count > 0);
  90. popup_centered();
  91. }
  92. void OpenXRSelectInteractionProfileDialog::ok_pressed() {
  93. if (selected_interaction_profile != "") {
  94. emit_signal("interaction_profile_selected", selected_interaction_profile);
  95. }
  96. hide();
  97. }
  98. OpenXRSelectInteractionProfileDialog::OpenXRSelectInteractionProfileDialog() {
  99. set_title(TTR("Select an interaction profile"));
  100. scroll = memnew(ScrollContainer);
  101. scroll->set_custom_minimum_size(Size2(600.0 * EDSCALE, 400.0 * EDSCALE));
  102. add_child(scroll);
  103. main_vb = memnew(VBoxContainer);
  104. main_vb->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  105. scroll->add_child(main_vb);
  106. all_selected = memnew(Label);
  107. all_selected->set_text(TTR("All interaction profiles have been added to the action map."));
  108. main_vb->add_child(all_selected);
  109. }