editor_object_selector.cpp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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. #include "editor_object_selector.h"
  31. #include "editor/debugger/editor_debugger_inspector.h"
  32. #include "editor/editor_data.h"
  33. #include "editor/editor_node.h"
  34. #include "editor/editor_string_names.h"
  35. #include "editor/multi_node_edit.h"
  36. #include "editor/themes/editor_scale.h"
  37. #include "scene/gui/margin_container.h"
  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. void EditorObjectSelector::_add_children_to_popup(Object *p_obj, int p_depth) {
  44. if (p_depth > 8) {
  45. return;
  46. }
  47. List<PropertyInfo> pinfo;
  48. p_obj->get_property_list(&pinfo);
  49. for (const PropertyInfo &E : pinfo) {
  50. if (!(E.usage & PROPERTY_USAGE_EDITOR)) {
  51. continue;
  52. }
  53. if (E.hint != PROPERTY_HINT_RESOURCE_TYPE) {
  54. continue;
  55. }
  56. Variant value = p_obj->get(E.name);
  57. if (value.get_type() != Variant::OBJECT) {
  58. continue;
  59. }
  60. Object *obj = value;
  61. if (!obj) {
  62. continue;
  63. }
  64. Ref<Texture2D> obj_icon = EditorNode::get_singleton()->get_object_icon(obj);
  65. String proper_name = "";
  66. Vector<String> name_parts = E.name.split("/");
  67. for (int i = 0; i < name_parts.size(); i++) {
  68. if (i > 0) {
  69. proper_name += " > ";
  70. }
  71. proper_name += name_parts[i].capitalize();
  72. }
  73. int index = sub_objects_menu->get_item_count();
  74. sub_objects_menu->add_icon_item(obj_icon, proper_name, objects.size());
  75. sub_objects_menu->set_item_indent(index, p_depth);
  76. objects.push_back(obj->get_instance_id());
  77. _add_children_to_popup(obj, p_depth + 1);
  78. }
  79. }
  80. void EditorObjectSelector::_show_popup() {
  81. if (sub_objects_menu->is_visible()) {
  82. sub_objects_menu->hide();
  83. return;
  84. }
  85. sub_objects_menu->clear();
  86. Size2 size = get_size();
  87. Point2 gp = get_screen_position();
  88. gp.y += size.y;
  89. sub_objects_menu->set_position(gp);
  90. sub_objects_menu->set_size(Size2(size.width, 1));
  91. sub_objects_menu->popup();
  92. }
  93. void EditorObjectSelector::_about_to_show() {
  94. Object *obj = ObjectDB::get_instance(history->get_path_object(history->get_path_size() - 1));
  95. if (!obj) {
  96. return;
  97. }
  98. objects.clear();
  99. _add_children_to_popup(obj);
  100. if (sub_objects_menu->get_item_count() == 0) {
  101. sub_objects_menu->add_item(TTR("No sub-resources found."));
  102. sub_objects_menu->set_item_disabled(0, true);
  103. }
  104. }
  105. void EditorObjectSelector::update_path() {
  106. for (int i = 0; i < history->get_path_size(); i++) {
  107. Object *obj = ObjectDB::get_instance(history->get_path_object(i));
  108. if (!obj) {
  109. continue;
  110. }
  111. Ref<Texture2D> obj_icon;
  112. if (Object::cast_to<MultiNodeEdit>(obj)) {
  113. obj_icon = EditorNode::get_singleton()->get_class_icon(Object::cast_to<MultiNodeEdit>(obj)->get_edited_class_name());
  114. } else if (Object::cast_to<EditorDebuggerRemoteObject>(obj)) {
  115. String class_name;
  116. Ref<Script> base_script = obj->get_script();
  117. if (base_script.is_valid()) {
  118. class_name = base_script->get_global_name();
  119. if (class_name.is_empty()) {
  120. // If there is no class_name in this script we just take the script path.
  121. class_name = base_script->get_path();
  122. }
  123. }
  124. obj_icon = EditorNode::get_singleton()->get_class_icon(class_name.is_empty() ? Object::cast_to<EditorDebuggerRemoteObject>(obj)->type_name : class_name);
  125. } else {
  126. obj_icon = EditorNode::get_singleton()->get_object_icon(obj);
  127. }
  128. if (obj_icon.is_valid()) {
  129. current_object_icon->set_texture(obj_icon);
  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. if (name.is_empty()) {
  143. name = r->get_class();
  144. }
  145. } else if (obj->is_class("EditorDebuggerRemoteObject")) {
  146. name = obj->call("get_title");
  147. } else if (Object::cast_to<Node>(obj)) {
  148. name = Object::cast_to<Node>(obj)->get_name();
  149. } else if (Object::cast_to<Resource>(obj) && !Object::cast_to<Resource>(obj)->get_name().is_empty()) {
  150. name = Object::cast_to<Resource>(obj)->get_name();
  151. } else {
  152. name = obj->get_class();
  153. }
  154. current_object_label->set_text(name);
  155. set_tooltip_text(obj->get_class());
  156. }
  157. }
  158. }
  159. void EditorObjectSelector::clear_path() {
  160. set_disabled(true);
  161. set_tooltip_text("");
  162. current_object_label->set_text("");
  163. current_object_icon->set_texture(nullptr);
  164. sub_objects_icon->hide();
  165. }
  166. void EditorObjectSelector::enable_path() {
  167. set_disabled(false);
  168. sub_objects_icon->show();
  169. }
  170. void EditorObjectSelector::_id_pressed(int p_idx) {
  171. ERR_FAIL_INDEX(p_idx, objects.size());
  172. Object *obj = ObjectDB::get_instance(objects[p_idx]);
  173. if (!obj) {
  174. return;
  175. }
  176. EditorNode::get_singleton()->push_item(obj);
  177. }
  178. void EditorObjectSelector::_notification(int p_what) {
  179. switch (p_what) {
  180. case NOTIFICATION_ENTER_TREE:
  181. case NOTIFICATION_THEME_CHANGED: {
  182. update_path();
  183. int icon_size = get_theme_constant(SNAME("class_icon_size"), EditorStringName(Editor));
  184. current_object_icon->set_custom_minimum_size(Size2(icon_size, icon_size));
  185. current_object_label->add_theme_font_override(SceneStringName(font), get_theme_font(SNAME("main"), EditorStringName(EditorFonts)));
  186. sub_objects_icon->set_texture(get_theme_icon(SNAME("arrow"), SNAME("OptionButton")));
  187. sub_objects_menu->add_theme_constant_override("icon_max_width", icon_size);
  188. } break;
  189. case NOTIFICATION_READY: {
  190. connect(SceneStringName(pressed), callable_mp(this, &EditorObjectSelector::_show_popup));
  191. } break;
  192. }
  193. }
  194. EditorObjectSelector::EditorObjectSelector(EditorSelectionHistory *p_history) {
  195. history = p_history;
  196. MarginContainer *main_mc = memnew(MarginContainer);
  197. main_mc->set_anchors_and_offsets_preset(PRESET_FULL_RECT);
  198. main_mc->add_theme_constant_override("margin_left", 4 * EDSCALE);
  199. main_mc->add_theme_constant_override("margin_right", 6 * EDSCALE);
  200. add_child(main_mc);
  201. HBoxContainer *main_hb = memnew(HBoxContainer);
  202. main_mc->add_child(main_hb);
  203. current_object_icon = memnew(TextureRect);
  204. current_object_icon->set_stretch_mode(TextureRect::STRETCH_KEEP_ASPECT_CENTERED);
  205. current_object_icon->set_expand_mode(TextureRect::EXPAND_IGNORE_SIZE);
  206. main_hb->add_child(current_object_icon);
  207. current_object_label = memnew(Label);
  208. current_object_label->set_text_overrun_behavior(TextServer::OVERRUN_TRIM_ELLIPSIS);
  209. current_object_label->set_h_size_flags(SIZE_EXPAND_FILL);
  210. current_object_label->set_vertical_alignment(VERTICAL_ALIGNMENT_CENTER);
  211. current_object_label->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
  212. main_hb->add_child(current_object_label);
  213. sub_objects_icon = memnew(TextureRect);
  214. sub_objects_icon->hide();
  215. sub_objects_icon->set_stretch_mode(TextureRect::STRETCH_KEEP_CENTERED);
  216. main_hb->add_child(sub_objects_icon);
  217. sub_objects_menu = memnew(PopupMenu);
  218. sub_objects_menu->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
  219. add_child(sub_objects_menu);
  220. sub_objects_menu->connect("about_to_popup", callable_mp(this, &EditorObjectSelector::_about_to_show));
  221. sub_objects_menu->connect(SceneStringName(id_pressed), callable_mp(this, &EditorObjectSelector::_id_pressed));
  222. set_tooltip_text(TTR("Open a list of sub-resources."));
  223. }