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