| 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_scale.h" |
| 36 | #include "editor/editor_string_names.h" |
| 37 | #include "editor/multi_node_edit.h" |
| 38 | |
| 39 | Size2 EditorObjectSelector::get_minimum_size() const { |
| 40 | Ref<Font> font = get_theme_font(SNAME("font" )); |
| 41 | int font_size = get_theme_font_size(SNAME("font_size" )); |
| 42 | return Button::get_minimum_size() + Size2(0, font->get_height(font_size)); |
| 43 | } |
| 44 | |
| 45 | void EditorObjectSelector::(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::() { |
| 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->set_position(gp); |
| 103 | sub_objects_menu->set_size(Size2(size.width, 1)); |
| 104 | |
| 105 | sub_objects_menu->take_mouse_focus(); |
| 106 | sub_objects_menu->popup(); |
| 107 | } |
| 108 | |
| 109 | void EditorObjectSelector::_about_to_show() { |
| 110 | Object *obj = ObjectDB::get_instance(history->get_path_object(history->get_path_size() - 1)); |
| 111 | if (!obj) { |
| 112 | return; |
| 113 | } |
| 114 | |
| 115 | objects.clear(); |
| 116 | |
| 117 | _add_children_to_popup(obj); |
| 118 | if (sub_objects_menu->get_item_count() == 0) { |
| 119 | sub_objects_menu->add_item(TTR("No sub-resources found." )); |
| 120 | sub_objects_menu->set_item_disabled(0, true); |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | void EditorObjectSelector::update_path() { |
| 125 | for (int i = 0; i < history->get_path_size(); i++) { |
| 126 | Object *obj = ObjectDB::get_instance(history->get_path_object(i)); |
| 127 | if (!obj) { |
| 128 | continue; |
| 129 | } |
| 130 | |
| 131 | Ref<Texture2D> obj_icon; |
| 132 | if (Object::cast_to<MultiNodeEdit>(obj)) { |
| 133 | obj_icon = EditorNode::get_singleton()->get_class_icon(Object::cast_to<MultiNodeEdit>(obj)->get_edited_class_name()); |
| 134 | } else { |
| 135 | obj_icon = EditorNode::get_singleton()->get_object_icon(obj); |
| 136 | } |
| 137 | |
| 138 | if (obj_icon.is_valid()) { |
| 139 | current_object_icon->set_texture(obj_icon); |
| 140 | } |
| 141 | |
| 142 | if (i == history->get_path_size() - 1) { |
| 143 | String name; |
| 144 | if (obj->has_method("_get_editor_name" )) { |
| 145 | name = obj->call("_get_editor_name" ); |
| 146 | } else if (Object::cast_to<Resource>(obj)) { |
| 147 | Resource *r = Object::cast_to<Resource>(obj); |
| 148 | if (r->get_path().is_resource_file()) { |
| 149 | name = r->get_path().get_file(); |
| 150 | } else { |
| 151 | name = r->get_name(); |
| 152 | } |
| 153 | |
| 154 | if (name.is_empty()) { |
| 155 | name = r->get_class(); |
| 156 | } |
| 157 | } else if (obj->is_class("EditorDebuggerRemoteObject" )) { |
| 158 | name = obj->call("get_title" ); |
| 159 | } else if (Object::cast_to<Node>(obj)) { |
| 160 | name = Object::cast_to<Node>(obj)->get_name(); |
| 161 | } else if (Object::cast_to<Resource>(obj) && !Object::cast_to<Resource>(obj)->get_name().is_empty()) { |
| 162 | name = Object::cast_to<Resource>(obj)->get_name(); |
| 163 | } else { |
| 164 | name = obj->get_class(); |
| 165 | } |
| 166 | |
| 167 | current_object_label->set_text(name); |
| 168 | set_tooltip_text(obj->get_class()); |
| 169 | } |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | void EditorObjectSelector::clear_path() { |
| 174 | set_disabled(true); |
| 175 | set_tooltip_text("" ); |
| 176 | |
| 177 | current_object_label->set_text("" ); |
| 178 | current_object_icon->set_texture(nullptr); |
| 179 | sub_objects_icon->hide(); |
| 180 | } |
| 181 | |
| 182 | void EditorObjectSelector::enable_path() { |
| 183 | set_disabled(false); |
| 184 | sub_objects_icon->show(); |
| 185 | } |
| 186 | |
| 187 | void EditorObjectSelector::_id_pressed(int p_idx) { |
| 188 | ERR_FAIL_INDEX(p_idx, objects.size()); |
| 189 | |
| 190 | Object *obj = ObjectDB::get_instance(objects[p_idx]); |
| 191 | if (!obj) { |
| 192 | return; |
| 193 | } |
| 194 | |
| 195 | EditorNode::get_singleton()->push_item(obj); |
| 196 | } |
| 197 | |
| 198 | void EditorObjectSelector::_notification(int p_what) { |
| 199 | switch (p_what) { |
| 200 | case NOTIFICATION_ENTER_TREE: |
| 201 | case NOTIFICATION_THEME_CHANGED: { |
| 202 | update_path(); |
| 203 | |
| 204 | int icon_size = get_theme_constant(SNAME("class_icon_size" ), EditorStringName(Editor)); |
| 205 | |
| 206 | current_object_icon->set_custom_minimum_size(Size2(icon_size, icon_size)); |
| 207 | current_object_label->add_theme_font_override("font" , get_theme_font(SNAME("main" ), EditorStringName(EditorFonts))); |
| 208 | sub_objects_icon->set_texture(get_theme_icon(SNAME("arrow" ), SNAME("OptionButton" ))); |
| 209 | sub_objects_menu->add_theme_constant_override("icon_max_width" , icon_size); |
| 210 | } break; |
| 211 | |
| 212 | case NOTIFICATION_READY: { |
| 213 | connect("pressed" , callable_mp(this, &EditorObjectSelector::_show_popup)); |
| 214 | } break; |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | void EditorObjectSelector::_bind_methods() { |
| 219 | } |
| 220 | |
| 221 | EditorObjectSelector::EditorObjectSelector(EditorSelectionHistory *p_history) { |
| 222 | history = p_history; |
| 223 | |
| 224 | MarginContainer *main_mc = memnew(MarginContainer); |
| 225 | main_mc->set_anchors_and_offsets_preset(PRESET_FULL_RECT); |
| 226 | main_mc->add_theme_constant_override("margin_left" , 4 * EDSCALE); |
| 227 | main_mc->add_theme_constant_override("margin_right" , 6 * EDSCALE); |
| 228 | add_child(main_mc); |
| 229 | |
| 230 | HBoxContainer *main_hb = memnew(HBoxContainer); |
| 231 | main_mc->add_child(main_hb); |
| 232 | |
| 233 | current_object_icon = memnew(TextureRect); |
| 234 | current_object_icon->set_stretch_mode(TextureRect::STRETCH_KEEP_ASPECT_CENTERED); |
| 235 | current_object_icon->set_expand_mode(TextureRect::EXPAND_IGNORE_SIZE); |
| 236 | main_hb->add_child(current_object_icon); |
| 237 | |
| 238 | current_object_label = memnew(Label); |
| 239 | current_object_label->set_text_overrun_behavior(TextServer::OVERRUN_TRIM_ELLIPSIS); |
| 240 | current_object_label->set_h_size_flags(SIZE_EXPAND_FILL); |
| 241 | current_object_label->set_auto_translate(false); |
| 242 | main_hb->add_child(current_object_label); |
| 243 | |
| 244 | sub_objects_icon = memnew(TextureRect); |
| 245 | sub_objects_icon->hide(); |
| 246 | sub_objects_icon->set_stretch_mode(TextureRect::STRETCH_KEEP_CENTERED); |
| 247 | main_hb->add_child(sub_objects_icon); |
| 248 | |
| 249 | sub_objects_menu = memnew(PopupMenu); |
| 250 | sub_objects_menu->set_auto_translate(false); |
| 251 | add_child(sub_objects_menu); |
| 252 | sub_objects_menu->connect("about_to_popup" , callable_mp(this, &EditorObjectSelector::_about_to_show)); |
| 253 | sub_objects_menu->connect("id_pressed" , callable_mp(this, &EditorObjectSelector::_id_pressed)); |
| 254 | |
| 255 | set_tooltip_text(TTR("Open a list of sub-resources." )); |
| 256 | } |
| 257 | |