editor_path.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /*************************************************************************/
  2. /* editor_path.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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_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 (const PropertyInfo &E : pinfo) {
  40. if (!(E.usage & PROPERTY_USAGE_EDITOR)) {
  41. continue;
  42. }
  43. if (E.hint != PROPERTY_HINT_RESOURCE_TYPE) {
  44. continue;
  45. }
  46. Variant value = p_obj->get(E.name);
  47. if (value.get_type() != Variant::OBJECT) {
  48. continue;
  49. }
  50. Object *obj = value;
  51. if (!obj) {
  52. continue;
  53. }
  54. Ref<Texture2D> icon = EditorNode::get_singleton()->get_object_icon(obj);
  55. String proper_name = "";
  56. Vector<String> name_parts = E.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_screen_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->take_mouse_focus();
  79. sub_objects_menu->popup();
  80. }
  81. void EditorPath::_about_to_show() {
  82. Object *obj = ObjectDB::get_instance(history->get_path_object(history->get_path_size() - 1));
  83. if (!obj) {
  84. return;
  85. }
  86. objects.clear();
  87. _add_children_to_popup(obj);
  88. if (sub_objects_menu->get_item_count() == 0) {
  89. sub_objects_menu->add_item(TTR("No sub-resources found."));
  90. sub_objects_menu->set_item_disabled(0, true);
  91. }
  92. }
  93. void EditorPath::update_path() {
  94. for (int i = 0; i < history->get_path_size(); i++) {
  95. Object *obj = ObjectDB::get_instance(history->get_path_object(i));
  96. if (!obj) {
  97. continue;
  98. }
  99. Ref<Texture2D> icon = EditorNode::get_singleton()->get_object_icon(obj);
  100. if (icon.is_valid()) {
  101. current_object_icon->set_texture(icon);
  102. }
  103. if (i == history->get_path_size() - 1) {
  104. String name;
  105. if (Object::cast_to<Resource>(obj)) {
  106. Resource *r = Object::cast_to<Resource>(obj);
  107. if (r->get_path().is_resource_file()) {
  108. name = r->get_path().get_file();
  109. } else {
  110. name = r->get_name();
  111. }
  112. if (name == "") {
  113. name = r->get_class();
  114. }
  115. } else if (obj->is_class("EditorDebuggerRemoteObject")) {
  116. name = obj->call("get_title");
  117. } else if (Object::cast_to<Node>(obj)) {
  118. name = Object::cast_to<Node>(obj)->get_name();
  119. } else if (Object::cast_to<Resource>(obj) && Object::cast_to<Resource>(obj)->get_name() != "") {
  120. name = Object::cast_to<Resource>(obj)->get_name();
  121. } else {
  122. name = obj->get_class();
  123. }
  124. current_object_label->set_text(" " + name); // An extra space so the text is not too close of the icon.
  125. set_tooltip(obj->get_class());
  126. }
  127. }
  128. }
  129. void EditorPath::clear_path() {
  130. set_disabled(true);
  131. set_tooltip("");
  132. current_object_label->set_text("");
  133. current_object_icon->set_texture(nullptr);
  134. sub_objects_icon->set_visible(false);
  135. }
  136. void EditorPath::enable_path() {
  137. set_disabled(false);
  138. sub_objects_icon->set_visible(true);
  139. }
  140. void EditorPath::_id_pressed(int p_idx) {
  141. ERR_FAIL_INDEX(p_idx, objects.size());
  142. Object *obj = ObjectDB::get_instance(objects[p_idx]);
  143. if (!obj) {
  144. return;
  145. }
  146. EditorNode::get_singleton()->push_item(obj);
  147. }
  148. void EditorPath::_notification(int p_what) {
  149. switch (p_what) {
  150. case NOTIFICATION_ENTER_TREE:
  151. case NOTIFICATION_THEME_CHANGED: {
  152. update_path();
  153. sub_objects_icon->set_texture(get_theme_icon(SNAME("select_arrow"), SNAME("Tree")));
  154. current_object_label->add_theme_font_override("font", get_theme_font(SNAME("main"), SNAME("EditorFonts")));
  155. } break;
  156. case NOTIFICATION_READY: {
  157. connect("pressed", callable_mp(this, &EditorPath::_show_popup));
  158. } break;
  159. }
  160. }
  161. void EditorPath::_bind_methods() {
  162. }
  163. EditorPath::EditorPath(EditorHistory *p_history) {
  164. history = p_history;
  165. MarginContainer *main_mc = memnew(MarginContainer);
  166. main_mc->set_anchors_and_offsets_preset(PRESET_WIDE);
  167. main_mc->add_theme_constant_override("margin_left", 4 * EDSCALE);
  168. main_mc->add_theme_constant_override("margin_right", 6 * EDSCALE);
  169. add_child(main_mc);
  170. HBoxContainer *main_hb = memnew(HBoxContainer);
  171. main_mc->add_child(main_hb);
  172. current_object_icon = memnew(TextureRect);
  173. current_object_icon->set_stretch_mode(TextureRect::STRETCH_KEEP_CENTERED);
  174. main_hb->add_child(current_object_icon);
  175. current_object_label = memnew(Label);
  176. current_object_label->set_clip_text(true);
  177. current_object_label->set_align(Label::ALIGN_LEFT);
  178. current_object_label->set_h_size_flags(SIZE_EXPAND_FILL);
  179. main_hb->add_child(current_object_label);
  180. sub_objects_icon = memnew(TextureRect);
  181. sub_objects_icon->set_visible(false);
  182. sub_objects_icon->set_stretch_mode(TextureRect::STRETCH_KEEP_CENTERED);
  183. main_hb->add_child(sub_objects_icon);
  184. sub_objects_menu = memnew(PopupMenu);
  185. add_child(sub_objects_menu);
  186. sub_objects_menu->connect("about_to_popup", callable_mp(this, &EditorPath::_about_to_show));
  187. sub_objects_menu->connect("id_pressed", callable_mp(this, &EditorPath::_id_pressed));
  188. set_tooltip(TTR("Open a list of sub-resources."));
  189. }