editor_path.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /*************************************************************************/
  2. /* editor_path.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  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_path.h"
  31. #include "editor/editor_data.h"
  32. #include "editor/editor_node.h"
  33. #include "editor/editor_scale.h"
  34. void EditorPath::_add_children_to_popup(Object *p_obj, int p_depth) {
  35. if (p_depth > 8) {
  36. return;
  37. }
  38. List<PropertyInfo> pinfo;
  39. p_obj->get_property_list(&pinfo);
  40. for (const PropertyInfo &E : pinfo) {
  41. if (!(E.usage & PROPERTY_USAGE_EDITOR)) {
  42. continue;
  43. }
  44. if (E.hint != PROPERTY_HINT_RESOURCE_TYPE) {
  45. continue;
  46. }
  47. Variant value = p_obj->get(E.name);
  48. if (value.get_type() != Variant::OBJECT) {
  49. continue;
  50. }
  51. Object *obj = value;
  52. if (!obj) {
  53. continue;
  54. }
  55. Ref<Texture2D> icon = EditorNode::get_singleton()->get_object_icon(obj);
  56. String proper_name = "";
  57. Vector<String> name_parts = E.name.split("/");
  58. for (int i = 0; i < name_parts.size(); i++) {
  59. if (i > 0) {
  60. proper_name += " > ";
  61. }
  62. proper_name += name_parts[i].capitalize();
  63. }
  64. int index = sub_objects_menu->get_item_count();
  65. sub_objects_menu->add_icon_item(icon, proper_name, objects.size());
  66. sub_objects_menu->set_item_indent(index, p_depth);
  67. objects.push_back(obj->get_instance_id());
  68. _add_children_to_popup(obj, p_depth + 1);
  69. }
  70. }
  71. void EditorPath::_show_popup() {
  72. if (sub_objects_menu->is_visible()) {
  73. sub_objects_menu->hide();
  74. return;
  75. }
  76. sub_objects_menu->clear();
  77. Size2 size = get_size();
  78. Point2 gp = get_screen_position();
  79. gp.y += size.y;
  80. sub_objects_menu->set_position(gp);
  81. sub_objects_menu->set_size(Size2(size.width, 1));
  82. sub_objects_menu->set_parent_rect(Rect2(Point2(gp - sub_objects_menu->get_position()), size));
  83. sub_objects_menu->take_mouse_focus();
  84. sub_objects_menu->popup();
  85. }
  86. void EditorPath::_about_to_show() {
  87. Object *obj = ObjectDB::get_instance(history->get_path_object(history->get_path_size() - 1));
  88. if (!obj) {
  89. return;
  90. }
  91. objects.clear();
  92. _add_children_to_popup(obj);
  93. if (sub_objects_menu->get_item_count() == 0) {
  94. sub_objects_menu->add_item(TTR("No sub-resources found."));
  95. sub_objects_menu->set_item_disabled(0, true);
  96. }
  97. }
  98. void EditorPath::update_path() {
  99. for (int i = 0; i < history->get_path_size(); i++) {
  100. Object *obj = ObjectDB::get_instance(history->get_path_object(i));
  101. if (!obj) {
  102. continue;
  103. }
  104. Ref<Texture2D> icon = EditorNode::get_singleton()->get_object_icon(obj);
  105. if (icon.is_valid()) {
  106. current_object_icon->set_texture(icon);
  107. }
  108. if (i == history->get_path_size() - 1) {
  109. String name;
  110. if (Object::cast_to<Resource>(obj)) {
  111. Resource *r = Object::cast_to<Resource>(obj);
  112. if (r->get_path().is_resource_file()) {
  113. name = r->get_path().get_file();
  114. } else {
  115. name = r->get_name();
  116. }
  117. if (name.is_empty()) {
  118. name = r->get_class();
  119. }
  120. } else if (obj->is_class("EditorDebuggerRemoteObject")) {
  121. name = obj->call("get_title");
  122. } else if (Object::cast_to<Node>(obj)) {
  123. name = Object::cast_to<Node>(obj)->get_name();
  124. } else if (Object::cast_to<Resource>(obj) && !Object::cast_to<Resource>(obj)->get_name().is_empty()) {
  125. name = Object::cast_to<Resource>(obj)->get_name();
  126. } else {
  127. name = obj->get_class();
  128. }
  129. current_object_label->set_text(" " + name); // An extra space so the text is not too close of the icon.
  130. set_tooltip_text(obj->get_class());
  131. }
  132. }
  133. }
  134. void EditorPath::clear_path() {
  135. set_disabled(true);
  136. set_tooltip_text("");
  137. current_object_label->set_text("");
  138. current_object_icon->set_texture(nullptr);
  139. sub_objects_icon->set_visible(false);
  140. }
  141. void EditorPath::enable_path() {
  142. set_disabled(false);
  143. sub_objects_icon->set_visible(true);
  144. }
  145. void EditorPath::_id_pressed(int p_idx) {
  146. ERR_FAIL_INDEX(p_idx, objects.size());
  147. Object *obj = ObjectDB::get_instance(objects[p_idx]);
  148. if (!obj) {
  149. return;
  150. }
  151. EditorNode::get_singleton()->push_item(obj);
  152. }
  153. void EditorPath::_notification(int p_what) {
  154. switch (p_what) {
  155. case NOTIFICATION_ENTER_TREE:
  156. case NOTIFICATION_THEME_CHANGED: {
  157. update_path();
  158. sub_objects_icon->set_texture(get_theme_icon(SNAME("select_arrow"), SNAME("Tree")));
  159. current_object_label->add_theme_font_override("font", get_theme_font(SNAME("main"), SNAME("EditorFonts")));
  160. } break;
  161. case NOTIFICATION_READY: {
  162. connect("pressed", callable_mp(this, &EditorPath::_show_popup));
  163. } break;
  164. }
  165. }
  166. void EditorPath::_bind_methods() {
  167. }
  168. EditorPath::EditorPath(EditorSelectionHistory *p_history) {
  169. history = p_history;
  170. MarginContainer *main_mc = memnew(MarginContainer);
  171. main_mc->set_anchors_and_offsets_preset(PRESET_FULL_RECT);
  172. main_mc->add_theme_constant_override("margin_left", 4 * EDSCALE);
  173. main_mc->add_theme_constant_override("margin_right", 6 * EDSCALE);
  174. add_child(main_mc);
  175. HBoxContainer *main_hb = memnew(HBoxContainer);
  176. main_mc->add_child(main_hb);
  177. current_object_icon = memnew(TextureRect);
  178. current_object_icon->set_stretch_mode(TextureRect::STRETCH_KEEP_CENTERED);
  179. main_hb->add_child(current_object_icon);
  180. current_object_label = memnew(Label);
  181. current_object_label->set_clip_text(true);
  182. current_object_label->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_LEFT);
  183. current_object_label->set_h_size_flags(SIZE_EXPAND_FILL);
  184. main_hb->add_child(current_object_label);
  185. sub_objects_icon = memnew(TextureRect);
  186. sub_objects_icon->set_visible(false);
  187. sub_objects_icon->set_stretch_mode(TextureRect::STRETCH_KEEP_CENTERED);
  188. main_hb->add_child(sub_objects_icon);
  189. sub_objects_menu = memnew(PopupMenu);
  190. add_child(sub_objects_menu);
  191. sub_objects_menu->connect("about_to_popup", callable_mp(this, &EditorPath::_about_to_show));
  192. sub_objects_menu->connect("id_pressed", callable_mp(this, &EditorPath::_id_pressed));
  193. set_tooltip_text(TTR("Open a list of sub-resources."));
  194. }