editor_object_selector.cpp 8.6 KB

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