| 1 | /**************************************************************************/ |
| 2 | /* openxr_select_action_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 | |
| 31 | #include "openxr_select_action_dialog.h" |
| 32 | |
| 33 | #include "editor/themes/editor_scale.h" |
| 34 | |
| 35 | void OpenXRSelectActionDialog::_bind_methods() { |
| 36 | ADD_SIGNAL(MethodInfo("action_selected", PropertyInfo(Variant::STRING, "action"))); |
| 37 | } |
| 38 | |
| 39 | void OpenXRSelectActionDialog::_notification(int p_what) { |
| 40 | switch (p_what) { |
| 41 | case NOTIFICATION_THEME_CHANGED: { |
| 42 | scroll->add_theme_style_override(SceneStringName(panel), get_theme_stylebox(SceneStringName(panel), SNAME("Tree"))); |
| 43 | } break; |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | void OpenXRSelectActionDialog::_on_select_action(const String &p_action) { |
| 48 | if (selected_action != "") { |
| 49 | NodePath button_path = action_buttons[selected_action]; |
| 50 | Button *button = Object::cast_to<Button>(get_node(button_path)); |
| 51 | if (button != nullptr) { |
| 52 | button->set_flat(true); |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | selected_action = p_action; |
| 57 | |
| 58 | if (selected_action != "") { |
| 59 | NodePath button_path = action_buttons[selected_action]; |
| 60 | Button *button = Object::cast_to<Button>(get_node(button_path)); |
| 61 | if (button != nullptr) { |
| 62 | button->set_flat(false); |
| 63 | } |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | void OpenXRSelectActionDialog::open() { |
| 68 | ERR_FAIL_COND(action_map.is_null()); |
| 69 | |
| 70 | // Out with the old. |
| 71 | while (main_vb->get_child_count() > 0) { |
| 72 | memdelete(main_vb->get_child(0)); |
| 73 | } |
| 74 | |
| 75 | selected_action = ""; |
| 76 | action_buttons.clear(); |
| 77 | |
| 78 | // In with the new. |
| 79 | Array action_sets = action_map->get_action_sets(); |
| 80 | for (int i = 0; i < action_sets.size(); i++) { |
| 81 | Ref<OpenXRActionSet> action_set = action_sets[i]; |
| 82 | |
| 83 | Label *action_set_label = memnew(Label); |
| 84 | action_set_label->set_focus_mode(Control::FOCUS_ACCESSIBILITY); |
| 85 | action_set_label->set_text(action_set->get_localized_name()); |
| 86 | main_vb->add_child(action_set_label); |
| 87 | |
| 88 | Array actions = action_set->get_actions(); |
| 89 | for (int j = 0; j < actions.size(); j++) { |
| 90 | Ref<OpenXRAction> action = actions[j]; |
| 91 | |
| 92 | HBoxContainer *action_hb = memnew(HBoxContainer); |
| 93 | main_vb->add_child(action_hb); |
| 94 | |
| 95 | Control *indent_node = memnew(Control); |
| 96 | indent_node->set_custom_minimum_size(Size2(10.0, 0.0)); |
| 97 | action_hb->add_child(indent_node); |
| 98 | |
| 99 | Button *action_button = memnew(Button); |
| 100 | String action_name = action->get_name_with_set(); |
| 101 | action_button->set_flat(true); |
| 102 | action_button->set_text(action->get_name() + ": " + action->get_localized_name()); |
| 103 | action_button->connect(SceneStringName(pressed), callable_mp(this, &OpenXRSelectActionDialog::_on_select_action).bind(action_name)); |
| 104 | action_hb->add_child(action_button); |
| 105 | |
| 106 | action_buttons[action_name] = action_button->get_path(); |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | popup_centered(); |
| 111 | } |
| 112 | |
| 113 | void OpenXRSelectActionDialog::ok_pressed() { |
| 114 | if (selected_action == "") { |
| 115 | return; |
| 116 | } |
| 117 | |
| 118 | emit_signal("action_selected", selected_action); |
| 119 | |
| 120 | hide(); |
| 121 | } |
| 122 | |
| 123 | OpenXRSelectActionDialog::OpenXRSelectActionDialog(const Ref<OpenXRActionMap> &p_action_map) { |
| 124 | action_map = p_action_map; |
| 125 | |
| 126 | set_title(TTR("Select an action")); |
| 127 | |
| 128 | scroll = memnew(ScrollContainer); |
| 129 | scroll->set_custom_minimum_size(Size2(600.0 * EDSCALE, 400.0 * EDSCALE)); |
| 130 | add_child(scroll); |
| 131 | |
| 132 | main_vb = memnew(VBoxContainer); |
| 133 | main_vb->set_h_size_flags(Control::SIZE_EXPAND_FILL); |
| 134 | scroll->add_child(main_vb); |
| 135 | } |