summaryrefslogtreecommitdiffstats
path: root/modules/openxr/editor/openxr_action_set_editor.cpp
blob: 6f70a91cfd5be8c965a0393e579b476aadde5753 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/*************************************************************************/
/*  openxr_action_set_editor.cpp                                         */
/*************************************************************************/
/*                       This file is part of:                           */
/*                           GODOT ENGINE                                */
/*                      https://godotengine.org                          */
/*************************************************************************/
/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur.                 */
/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md).   */
/*                                                                       */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the       */
/* "Software"), to deal in the Software without restriction, including   */
/* without limitation the rights to use, copy, modify, merge, publish,   */
/* distribute, sublicense, and/or sell copies of the Software, and to    */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions:                                             */
/*                                                                       */
/* The above copyright notice and this permission notice shall be        */
/* included in all copies or substantial portions of the Software.       */
/*                                                                       */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,       */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF    */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY  */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,  */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE     */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.                */
/*************************************************************************/

#include "openxr_action_set_editor.h"
#include "openxr_action_editor.h"

void OpenXRActionSetEditor::_bind_methods() {
	ADD_SIGNAL(MethodInfo("remove", PropertyInfo(Variant::OBJECT, "action_set_editor")));
	ADD_SIGNAL(MethodInfo("action_removed"));
}

void OpenXRActionSetEditor::_set_fold_icon() {
	if (is_expanded) {
		fold_btn->set_icon(get_theme_icon(SNAME("GuiTreeArrowDown"), SNAME("EditorIcons")));
	} else {
		fold_btn->set_icon(get_theme_icon(SNAME("GuiTreeArrowRight"), SNAME("EditorIcons")));
	}
}

void OpenXRActionSetEditor::_notification(int p_what) {
	switch (p_what) {
		case NOTIFICATION_THEME_CHANGED: {
			_set_fold_icon();
			add_action->set_icon(get_theme_icon(SNAME("Add"), SNAME("EditorIcons")));
			rem_action_set->set_icon(get_theme_icon(SNAME("Remove"), SNAME("EditorIcons")));
			panel->add_theme_style_override("panel", get_theme_stylebox(SNAME("panel"), SNAME("TabContainer")));
		} break;
	}
}

OpenXRActionEditor *OpenXRActionSetEditor::_add_action_editor(Ref<OpenXRAction> p_action) {
	OpenXRActionEditor *action_editor = memnew(OpenXRActionEditor(p_action));
	action_editor->connect("remove", callable_mp(this, &OpenXRActionSetEditor::_on_remove_action));
	actions_vb->add_child(action_editor);

	return action_editor;
}

void OpenXRActionSetEditor::_update_actions() {
	// out with the old...
	while (actions_vb->get_child_count() > 0) {
		memdelete(actions_vb->get_child(0));
	}

	// in with the new...
	Array actions = action_set->get_actions();
	for (int i = 0; i < actions.size(); i++) {
		Ref<OpenXRAction> action = actions[i];
		_add_action_editor(action);
	}
}

void OpenXRActionSetEditor::_on_toggle_expand() {
	is_expanded = !is_expanded;
	actions_vb->set_visible(is_expanded);
	_set_fold_icon();
}

void OpenXRActionSetEditor::_on_action_set_name_changed(const String p_new_text) {
	// TODO validate if entry is allowed

	// If our localized name matches our action set name, set this too
	if (action_set->get_name() == action_set->get_localized_name()) {
		action_set->set_localized_name(p_new_text);
		action_set_localized_name->set_text(p_new_text);
	}
	action_set->set_name(p_new_text);
}

void OpenXRActionSetEditor::_on_action_set_localized_name_changed(const String p_new_text) {
	action_set->set_localized_name(p_new_text);
}

void OpenXRActionSetEditor::_on_action_set_priority_changed(const String p_new_text) {
	int64_t value = p_new_text.to_int();

	action_set->set_priority(value);
}

void OpenXRActionSetEditor::_on_add_action() {
	Ref<OpenXRAction> new_action;

	new_action.instantiate();
	new_action->set_name("New");
	new_action->set_localized_name("New");
	action_set->add_action(new_action);

	_add_action_editor(new_action);

	// TODO handle focus
}

void OpenXRActionSetEditor::_on_remove_action_set() {
	emit_signal("remove", this);
}

void OpenXRActionSetEditor::_on_remove_action(Object *p_action_editor) {
	OpenXRActionEditor *action_editor = Object::cast_to<OpenXRActionEditor>(p_action_editor);
	ERR_FAIL_NULL(action_editor);
	ERR_FAIL_COND(action_editor->get_parent() != actions_vb);
	Ref<OpenXRAction> action = action_editor->get_action();
	ERR_FAIL_COND(action.is_null());

	// TODO add undo/redo action

	// TODO find where this action is used by our interaction profiles and remove it there

	// And remove it....
	action_map->remove_action(action->get_name_with_set()); // remove it from the set and any interaction profile it relates to
	actions_vb->remove_child(action_editor);
	action_editor->queue_delete();

	// Let action map editor know so we can update our interaction profiles
	emit_signal("action_removed");
}

void OpenXRActionSetEditor::set_focus_on_entry() {
	ERR_FAIL_NULL(action_set_name);
	action_set_name->grab_focus();
}

OpenXRActionSetEditor::OpenXRActionSetEditor(Ref<OpenXRActionMap> p_action_map, Ref<OpenXRActionSet> p_action_set) {
	action_map = p_action_map;
	action_set = p_action_set;

	set_h_size_flags(Control::SIZE_EXPAND_FILL);

	panel = memnew(PanelContainer);
	panel->set_h_size_flags(Control::SIZE_EXPAND_FILL);
	add_child(panel);

	HBoxContainer *panel_hb = memnew(HBoxContainer);
	panel_hb->set_h_size_flags(Control::SIZE_EXPAND_FILL);
	panel->add_child(panel_hb);

	fold_btn = memnew(Button);
	fold_btn->set_v_size_flags(Control::SIZE_SHRINK_BEGIN);
	fold_btn->connect("pressed", callable_mp(this, &OpenXRActionSetEditor::_on_toggle_expand));
	fold_btn->set_flat(true);
	panel_hb->add_child(fold_btn);

	main_vb = memnew(VBoxContainer);
	main_vb->set_h_size_flags(Control::SIZE_EXPAND_FILL);
	panel_hb->add_child(main_vb);

	action_set_hb = memnew(HBoxContainer);
	action_set_hb->set_h_size_flags(Control::SIZE_EXPAND_FILL);
	main_vb->add_child(action_set_hb);

	action_set_name = memnew(LineEdit);
	action_set_name->set_text(action_set->get_name());
	action_set_name->set_custom_minimum_size(Size2(150.0, 0.0));
	action_set_name->connect("text_changed", callable_mp(this, &OpenXRActionSetEditor::_on_action_set_name_changed));
	action_set_hb->add_child(action_set_name);

	action_set_localized_name = memnew(LineEdit);
	action_set_localized_name->set_text(action_set->get_localized_name());
	action_set_localized_name->set_custom_minimum_size(Size2(150.0, 0.0));
	action_set_localized_name->set_h_size_flags(Control::SIZE_EXPAND_FILL);
	action_set_localized_name->connect("text_changed", callable_mp(this, &OpenXRActionSetEditor::_on_action_set_localized_name_changed));
	action_set_hb->add_child(action_set_localized_name);

	action_set_priority = memnew(TextEdit);
	action_set_priority->set_text(itos(action_set->get_priority()));
	action_set_priority->set_custom_minimum_size(Size2(50.0, 0.0));
	action_set_priority->connect("text_changed", callable_mp(this, &OpenXRActionSetEditor::_on_action_set_priority_changed));
	action_set_hb->add_child(action_set_priority);

	add_action = memnew(Button);
	add_action->set_tooltip_text("Add Action.");
	add_action->connect("pressed", callable_mp(this, &OpenXRActionSetEditor::_on_add_action));
	add_action->set_flat(true);
	action_set_hb->add_child(add_action);

	rem_action_set = memnew(Button);
	rem_action_set->set_tooltip_text("Remove Action Set.");
	rem_action_set->connect("pressed", callable_mp(this, &OpenXRActionSetEditor::_on_remove_action_set));
	rem_action_set->set_flat(true);
	action_set_hb->add_child(rem_action_set);

	actions_vb = memnew(VBoxContainer);
	actions_vb->set_h_size_flags(Control::SIZE_EXPAND_FILL);
	main_vb->add_child(actions_vb);

	_update_actions();
}