editor_path.cpp 7.9 KB

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