editor_path.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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_node.h"
  32. #include "editor_scale.h"
  33. void EditorPath::_add_children_to_popup(Object *p_obj, int p_depth) {
  34. if (p_depth > 8) {
  35. return;
  36. }
  37. List<PropertyInfo> pinfo;
  38. p_obj->get_property_list(&pinfo);
  39. for (List<PropertyInfo>::Element *E = pinfo.front(); E; E = E->next()) {
  40. if (!(E->get().usage & PROPERTY_USAGE_EDITOR)) {
  41. continue;
  42. }
  43. if (E->get().hint != PROPERTY_HINT_RESOURCE_TYPE) {
  44. continue;
  45. }
  46. Variant value = p_obj->get(E->get().name);
  47. if (value.get_type() != Variant::OBJECT) {
  48. continue;
  49. }
  50. Object *obj = value;
  51. if (!obj) {
  52. continue;
  53. }
  54. Ref<Texture> icon = EditorNode::get_singleton()->get_object_icon(obj);
  55. String proper_name = "";
  56. Vector<String> name_parts = E->get().name.split("/");
  57. for (int i = 0; i < name_parts.size(); i++) {
  58. if (i > 0) {
  59. proper_name += " > ";
  60. }
  61. proper_name += name_parts[i].capitalize();
  62. }
  63. int index = sub_objects_menu->get_item_count();
  64. sub_objects_menu->add_icon_item(icon, proper_name, objects.size());
  65. sub_objects_menu->set_item_h_offset(index, p_depth * 10 * EDSCALE);
  66. objects.push_back(obj->get_instance_id());
  67. _add_children_to_popup(obj, p_depth + 1);
  68. }
  69. }
  70. void EditorPath::_show_popup() {
  71. sub_objects_menu->clear();
  72. Size2 size = get_size();
  73. Point2 gp = get_global_position();
  74. gp.y += size.y;
  75. sub_objects_menu->set_position(gp);
  76. sub_objects_menu->set_size(Size2(size.width, 1));
  77. sub_objects_menu->set_parent_rect(Rect2(Point2(gp - sub_objects_menu->get_position()), size));
  78. sub_objects_menu->popup();
  79. }
  80. void EditorPath::_about_to_show() {
  81. Object *obj = ObjectDB::get_instance(history->get_path_object(history->get_path_size() - 1));
  82. if (!obj) {
  83. return;
  84. }
  85. objects.clear();
  86. _add_children_to_popup(obj);
  87. if (sub_objects_menu->get_item_count() == 0) {
  88. sub_objects_menu->add_item(TTR("No sub-resources found."));
  89. sub_objects_menu->set_item_disabled(0, true);
  90. }
  91. }
  92. void EditorPath::update_path() {
  93. for (int i = 0; i < history->get_path_size(); i++) {
  94. Object *obj = ObjectDB::get_instance(history->get_path_object(i));
  95. if (!obj) {
  96. continue;
  97. }
  98. Ref<Texture> icon = EditorNode::get_singleton()->get_object_icon(obj);
  99. if (icon.is_valid()) {
  100. current_object_icon->set_texture(icon);
  101. }
  102. if (i == history->get_path_size() - 1) {
  103. String name;
  104. if (Object::cast_to<Resource>(obj)) {
  105. Resource *r = Object::cast_to<Resource>(obj);
  106. if (r->get_path().is_resource_file()) {
  107. name = r->get_path().get_file();
  108. } else {
  109. name = r->get_name();
  110. }
  111. if (name == "") {
  112. name = r->get_class();
  113. }
  114. } else if (obj->is_class("ScriptEditorDebuggerInspectedObject")) {
  115. name = obj->call("get_title");
  116. } else if (Object::cast_to<Node>(obj)) {
  117. name = Object::cast_to<Node>(obj)->get_name();
  118. } else if (Object::cast_to<Resource>(obj) && Object::cast_to<Resource>(obj)->get_name() != "") {
  119. name = Object::cast_to<Resource>(obj)->get_name();
  120. } else {
  121. name = obj->get_class();
  122. }
  123. current_object_label->set_text(" " + name); // An extra space so the text is not too close of the icon.
  124. set_tooltip(obj->get_class());
  125. }
  126. }
  127. }
  128. void EditorPath::clear_path() {
  129. set_disabled(true);
  130. set_tooltip("");
  131. current_object_label->set_text("");
  132. current_object_icon->set_texture(nullptr);
  133. sub_objects_icon->set_visible(false);
  134. }
  135. void EditorPath::enable_path() {
  136. set_disabled(false);
  137. sub_objects_icon->set_visible(true);
  138. }
  139. void EditorPath::_id_pressed(int p_idx) {
  140. ERR_FAIL_INDEX(p_idx, objects.size());
  141. Object *obj = ObjectDB::get_instance(objects[p_idx]);
  142. if (!obj) {
  143. return;
  144. }
  145. EditorNode::get_singleton()->push_item(obj);
  146. }
  147. void EditorPath::_notification(int p_what) {
  148. switch (p_what) {
  149. case NOTIFICATION_ENTER_TREE:
  150. case NOTIFICATION_THEME_CHANGED: {
  151. update_path();
  152. // Button overrides Control's method, so we have to improvise.
  153. sub_objects_icon->set_texture(sub_objects_icon->get_icon("select_arrow", "Tree"));
  154. current_object_label->add_font_override("font", get_font("main", "EditorFonts"));
  155. } break;
  156. case NOTIFICATION_READY: {
  157. connect("pressed", this, "_show_popup");
  158. } break;
  159. }
  160. }
  161. void EditorPath::_bind_methods() {
  162. ClassDB::bind_method("_show_popup", &EditorPath::_show_popup);
  163. ClassDB::bind_method("_about_to_show", &EditorPath::_about_to_show);
  164. ClassDB::bind_method("_id_pressed", &EditorPath::_id_pressed);
  165. }
  166. EditorPath::EditorPath(EditorHistory *p_history) {
  167. history = p_history;
  168. MarginContainer *main_mc = memnew(MarginContainer);
  169. main_mc->set_anchors_and_margins_preset(PRESET_WIDE);
  170. main_mc->add_constant_override("margin_left", 4 * EDSCALE);
  171. main_mc->add_constant_override("margin_right", 6 * EDSCALE);
  172. main_mc->set_mouse_filter(MOUSE_FILTER_PASS);
  173. add_child(main_mc);
  174. HBoxContainer *main_hb = memnew(HBoxContainer);
  175. main_mc->add_child(main_hb);
  176. current_object_icon = memnew(TextureRect);
  177. current_object_icon->set_stretch_mode(TextureRect::STRETCH_KEEP_CENTERED);
  178. main_hb->add_child(current_object_icon);
  179. current_object_label = memnew(Label);
  180. current_object_label->set_clip_text(true);
  181. current_object_label->set_align(Label::ALIGN_LEFT);
  182. current_object_label->set_h_size_flags(SIZE_EXPAND_FILL);
  183. main_hb->add_child(current_object_label);
  184. sub_objects_icon = memnew(TextureRect);
  185. sub_objects_icon->set_visible(false);
  186. sub_objects_icon->set_stretch_mode(TextureRect::STRETCH_KEEP_CENTERED);
  187. main_hb->add_child(sub_objects_icon);
  188. sub_objects_menu = memnew(PopupMenu);
  189. add_child(sub_objects_menu);
  190. sub_objects_menu->connect("about_to_show", this, "_about_to_show");
  191. sub_objects_menu->connect("id_pressed", this, "_id_pressed");
  192. set_tooltip(TTR("Open a list of sub-resources."));
  193. }