openxr_select_interaction_profile_dialog.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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(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();
  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 String extension = meta_data->get_interaction_profile_extension(path);
  76. if (!p_do_not_include.has(path) && (extension.is_empty() || requested_extensions.has(extension))) {
  77. Button *ip_button = memnew(Button);
  78. ip_button->set_flat(true);
  79. ip_button->set_text(meta_data->get_profile(path)->display_name);
  80. ip_button->set_text_alignment(HORIZONTAL_ALIGNMENT_LEFT);
  81. ip_button->connect(SceneStringName(pressed), callable_mp(this, &OpenXRSelectInteractionProfileDialog::_on_select_interaction_profile).bind(path));
  82. main_vb->add_child(ip_button);
  83. ip_buttons[path] = ip_button->get_path();
  84. available_count++;
  85. }
  86. }
  87. all_selected->set_visible(available_count == 0);
  88. get_cancel_button()->set_visible(available_count > 0);
  89. popup_centered();
  90. }
  91. void OpenXRSelectInteractionProfileDialog::ok_pressed() {
  92. if (selected_interaction_profile != "") {
  93. emit_signal("interaction_profile_selected", selected_interaction_profile);
  94. }
  95. hide();
  96. }
  97. OpenXRSelectInteractionProfileDialog::OpenXRSelectInteractionProfileDialog() {
  98. set_title(TTR("Select an interaction profile"));
  99. scroll = memnew(ScrollContainer);
  100. scroll->set_custom_minimum_size(Size2(600.0 * EDSCALE, 400.0 * EDSCALE));
  101. add_child(scroll);
  102. main_vb = memnew(VBoxContainer);
  103. main_vb->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  104. scroll->add_child(main_vb);
  105. all_selected = memnew(Label);
  106. all_selected->set_focus_mode(Control::FOCUS_ACCESSIBILITY);
  107. all_selected->set_text(TTR("All interaction profiles have been added to the action map."));
  108. main_vb->add_child(all_selected);
  109. }