Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/editor/gui/editor_object_selector.cpp
9903 views
1
/**************************************************************************/
2
/* editor_object_selector.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 "editor_object_selector.h"
32
33
#include "editor/editor_data.h"
34
#include "editor/editor_node.h"
35
#include "editor/editor_string_names.h"
36
#include "editor/themes/editor_scale.h"
37
#include "scene/gui/margin_container.h"
38
39
Size2 EditorObjectSelector::get_minimum_size() const {
40
Ref<Font> font = get_theme_font(SceneStringName(font));
41
int font_size = get_theme_font_size(SceneStringName(font_size));
42
return Button::get_minimum_size() + Size2(0, font->get_height(font_size));
43
}
44
45
void EditorObjectSelector::_add_children_to_popup(Object *p_obj, int p_depth) {
46
if (p_depth > 8) {
47
return;
48
}
49
50
List<PropertyInfo> pinfo;
51
p_obj->get_property_list(&pinfo);
52
for (const PropertyInfo &E : pinfo) {
53
if (!(E.usage & PROPERTY_USAGE_EDITOR)) {
54
continue;
55
}
56
if (E.hint != PROPERTY_HINT_RESOURCE_TYPE) {
57
continue;
58
}
59
60
Variant value = p_obj->get(E.name);
61
if (value.get_type() != Variant::OBJECT) {
62
continue;
63
}
64
Object *obj = value;
65
if (!obj) {
66
continue;
67
}
68
69
Ref<Texture2D> obj_icon = EditorNode::get_singleton()->get_object_icon(obj);
70
71
String proper_name = "";
72
Vector<String> name_parts = E.name.split("/");
73
74
for (int i = 0; i < name_parts.size(); i++) {
75
if (i > 0) {
76
proper_name += " > ";
77
}
78
proper_name += name_parts[i].capitalize();
79
}
80
81
int index = sub_objects_menu->get_item_count();
82
sub_objects_menu->add_icon_item(obj_icon, proper_name, objects.size());
83
sub_objects_menu->set_item_indent(index, p_depth);
84
objects.push_back(obj->get_instance_id());
85
86
_add_children_to_popup(obj, p_depth + 1);
87
}
88
}
89
90
void EditorObjectSelector::_show_popup() {
91
if (sub_objects_menu->is_visible()) {
92
sub_objects_menu->hide();
93
return;
94
}
95
96
sub_objects_menu->clear();
97
98
Size2 size = get_size();
99
Point2 gp = get_screen_position();
100
gp.y += size.y;
101
102
sub_objects_menu->popup(Rect2(gp, Size2(size.width, 0)));
103
}
104
105
void EditorObjectSelector::_about_to_show() {
106
Object *obj = ObjectDB::get_instance(history->get_path_object(history->get_path_size() - 1));
107
if (!obj) {
108
return;
109
}
110
111
objects.clear();
112
113
_add_children_to_popup(obj);
114
if (sub_objects_menu->get_item_count() == 0) {
115
sub_objects_menu->add_item(TTR("No sub-resources found."));
116
sub_objects_menu->set_item_disabled(0, true);
117
}
118
}
119
120
void EditorObjectSelector::update_path() {
121
for (int i = 0; i < history->get_path_size(); i++) {
122
Object *obj = ObjectDB::get_instance(history->get_path_object(i));
123
if (!obj) {
124
continue;
125
}
126
127
Ref<Texture2D> obj_icon = EditorNode::get_singleton()->get_object_icon(obj);
128
if (obj_icon.is_valid()) {
129
current_object_icon->set_texture(obj_icon);
130
}
131
132
if (i == history->get_path_size() - 1) {
133
String name;
134
if (obj->has_method("_get_editor_name")) {
135
name = obj->call("_get_editor_name");
136
} else if (Object::cast_to<Resource>(obj)) {
137
Resource *r = Object::cast_to<Resource>(obj);
138
if (r->get_path().is_resource_file()) {
139
name = r->get_path().get_file();
140
} else {
141
name = r->get_name();
142
}
143
144
if (name.is_empty()) {
145
name = r->get_class();
146
}
147
} else if (obj->is_class("EditorDebuggerRemoteObjects")) {
148
name = obj->call("get_title");
149
} else if (Object::cast_to<Node>(obj)) {
150
name = Object::cast_to<Node>(obj)->get_name();
151
} else if (Object::cast_to<Resource>(obj) && !Object::cast_to<Resource>(obj)->get_name().is_empty()) {
152
name = Object::cast_to<Resource>(obj)->get_name();
153
} else {
154
name = obj->get_class();
155
}
156
157
current_object_label->set_text(name);
158
set_tooltip_text(obj->get_class());
159
}
160
}
161
}
162
163
void EditorObjectSelector::clear_path() {
164
set_disabled(true);
165
set_tooltip_text("");
166
167
current_object_label->set_text("");
168
current_object_icon->set_texture(nullptr);
169
sub_objects_icon->hide();
170
}
171
172
void EditorObjectSelector::enable_path() {
173
set_disabled(false);
174
sub_objects_icon->show();
175
}
176
177
void EditorObjectSelector::_id_pressed(int p_idx) {
178
ERR_FAIL_INDEX(p_idx, objects.size());
179
180
Object *obj = ObjectDB::get_instance(objects[p_idx]);
181
if (!obj) {
182
return;
183
}
184
185
EditorNode::get_singleton()->push_item(obj);
186
}
187
188
void EditorObjectSelector::_notification(int p_what) {
189
switch (p_what) {
190
case NOTIFICATION_THEME_CHANGED: {
191
update_path();
192
193
int icon_size = get_theme_constant(SNAME("class_icon_size"), EditorStringName(Editor));
194
195
current_object_icon->set_custom_minimum_size(Size2(icon_size, icon_size));
196
current_object_label->add_theme_font_override(SceneStringName(font), get_theme_font(SNAME("main"), EditorStringName(EditorFonts)));
197
sub_objects_icon->set_texture(get_theme_icon(SNAME("arrow"), SNAME("OptionButton")));
198
sub_objects_menu->add_theme_constant_override("icon_max_width", icon_size);
199
} break;
200
201
case NOTIFICATION_READY: {
202
connect(SceneStringName(pressed), callable_mp(this, &EditorObjectSelector::_show_popup));
203
} break;
204
}
205
}
206
207
EditorObjectSelector::EditorObjectSelector(EditorSelectionHistory *p_history) {
208
history = p_history;
209
210
MarginContainer *main_mc = memnew(MarginContainer);
211
main_mc->set_anchors_and_offsets_preset(PRESET_FULL_RECT);
212
main_mc->add_theme_constant_override("margin_left", 4 * EDSCALE);
213
main_mc->add_theme_constant_override("margin_right", 6 * EDSCALE);
214
add_child(main_mc);
215
216
HBoxContainer *main_hb = memnew(HBoxContainer);
217
main_mc->add_child(main_hb);
218
219
current_object_icon = memnew(TextureRect);
220
current_object_icon->set_stretch_mode(TextureRect::STRETCH_KEEP_ASPECT_CENTERED);
221
current_object_icon->set_expand_mode(TextureRect::EXPAND_IGNORE_SIZE);
222
main_hb->add_child(current_object_icon);
223
224
current_object_label = memnew(Label);
225
current_object_label->set_focus_mode(FOCUS_ACCESSIBILITY);
226
current_object_label->set_text_overrun_behavior(TextServer::OVERRUN_TRIM_ELLIPSIS);
227
current_object_label->set_h_size_flags(SIZE_EXPAND_FILL);
228
current_object_label->set_vertical_alignment(VERTICAL_ALIGNMENT_CENTER);
229
current_object_label->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
230
main_hb->add_child(current_object_label);
231
232
sub_objects_icon = memnew(TextureRect);
233
sub_objects_icon->hide();
234
sub_objects_icon->set_stretch_mode(TextureRect::STRETCH_KEEP_CENTERED);
235
main_hb->add_child(sub_objects_icon);
236
237
sub_objects_menu = memnew(PopupMenu);
238
sub_objects_menu->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
239
add_child(sub_objects_menu);
240
sub_objects_menu->connect("about_to_popup", callable_mp(this, &EditorObjectSelector::_about_to_show));
241
sub_objects_menu->connect(SceneStringName(id_pressed), callable_mp(this, &EditorObjectSelector::_id_pressed));
242
243
set_tooltip_text(TTR("Open a list of sub-resources."));
244
}
245
246