Debian Codegraph @

editor/gui/editor_object_selector.cpp

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