openxr_select_interaction_profile_dialog.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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) 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_select_interaction_profile_dialog.h"
  31. void OpenXRSelectInteractionProfileDialog::_bind_methods() {
  32. ADD_SIGNAL(MethodInfo("interaction_profile_selected", PropertyInfo(Variant::STRING, "interaction_profile")));
  33. }
  34. void OpenXRSelectInteractionProfileDialog::_notification(int p_what) {
  35. switch (p_what) {
  36. case NOTIFICATION_THEME_CHANGED: {
  37. scroll->add_theme_style_override("bg", get_theme_stylebox(SNAME("bg"), SNAME("Tree")));
  38. } break;
  39. }
  40. }
  41. void OpenXRSelectInteractionProfileDialog::_on_select_interaction_profile(const String p_interaction_profile) {
  42. if (selected_interaction_profile != "") {
  43. NodePath button_path = ip_buttons[selected_interaction_profile];
  44. Button *button = static_cast<Button *>(get_node(button_path));
  45. if (button != nullptr) {
  46. button->set_flat(true);
  47. }
  48. }
  49. selected_interaction_profile = p_interaction_profile;
  50. if (selected_interaction_profile != "") {
  51. NodePath button_path = ip_buttons[selected_interaction_profile];
  52. Button *button = static_cast<Button *>(get_node(button_path));
  53. if (button != nullptr) {
  54. button->set_flat(false);
  55. }
  56. }
  57. }
  58. void OpenXRSelectInteractionProfileDialog::open(PackedStringArray p_do_not_include) {
  59. int available_count = 0;
  60. // out with the old...
  61. while (main_vb->get_child_count() > 0) {
  62. memdelete(main_vb->get_child(0));
  63. }
  64. selected_interaction_profile = "";
  65. ip_buttons.clear();
  66. // in with the new
  67. PackedStringArray interaction_profiles = OpenXRDefs::get_interaction_profile_paths();
  68. for (int i = 0; i < interaction_profiles.size(); i++) {
  69. String path = interaction_profiles[i];
  70. if (!p_do_not_include.has(path)) {
  71. Button *ip_button = memnew(Button);
  72. ip_button->set_flat(true);
  73. ip_button->set_text(OpenXRDefs::get_profile(path)->display_name);
  74. ip_button->connect("pressed", callable_mp(this, &OpenXRSelectInteractionProfileDialog::_on_select_interaction_profile).bind(path));
  75. main_vb->add_child(ip_button);
  76. ip_buttons[path] = ip_button->get_path();
  77. available_count++;
  78. }
  79. }
  80. if (available_count == 0) {
  81. // give warning that we have all profiles selected
  82. } else {
  83. // TODO maybe if we only have one, auto select it?
  84. popup_centered();
  85. }
  86. }
  87. void OpenXRSelectInteractionProfileDialog::ok_pressed() {
  88. if (selected_interaction_profile == "") {
  89. return;
  90. }
  91. emit_signal("interaction_profile_selected", selected_interaction_profile);
  92. hide();
  93. }
  94. OpenXRSelectInteractionProfileDialog::OpenXRSelectInteractionProfileDialog() {
  95. set_title("Select an interaction profile");
  96. scroll = memnew(ScrollContainer);
  97. scroll->set_custom_minimum_size(Size2(600.0, 400.0));
  98. add_child(scroll);
  99. main_vb = memnew(VBoxContainer);
  100. // main_vb->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  101. scroll->add_child(main_vb);
  102. }