editor_object_selector.cpp 8.7 KB

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