editor_path.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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. Size2 EditorPath::get_minimum_size() const {
  36. Ref<Font> font = get_theme_font(SNAME("font"));
  37. int font_size = get_theme_font_size(SNAME("font_size"));
  38. return Button::get_minimum_size() + Size2(0, font->get_height(font_size));
  39. }
  40. void EditorPath::_add_children_to_popup(Object *p_obj, int p_depth) {
  41. if (p_depth > 8) {
  42. return;
  43. }
  44. List<PropertyInfo> pinfo;
  45. p_obj->get_property_list(&pinfo);
  46. for (const PropertyInfo &E : pinfo) {
  47. if (!(E.usage & PROPERTY_USAGE_EDITOR)) {
  48. continue;
  49. }
  50. if (E.hint != PROPERTY_HINT_RESOURCE_TYPE) {
  51. continue;
  52. }
  53. Variant value = p_obj->get(E.name);
  54. if (value.get_type() != Variant::OBJECT) {
  55. continue;
  56. }
  57. Object *obj = value;
  58. if (!obj) {
  59. continue;
  60. }
  61. Ref<Texture2D> obj_icon = EditorNode::get_singleton()->get_object_icon(obj);
  62. String proper_name = "";
  63. Vector<String> name_parts = E.name.split("/");
  64. for (int i = 0; i < name_parts.size(); i++) {
  65. if (i > 0) {
  66. proper_name += " > ";
  67. }
  68. proper_name += name_parts[i].capitalize();
  69. }
  70. int index = sub_objects_menu->get_item_count();
  71. sub_objects_menu->add_icon_item(obj_icon, proper_name, objects.size());
  72. sub_objects_menu->set_item_indent(index, p_depth);
  73. objects.push_back(obj->get_instance_id());
  74. _add_children_to_popup(obj, p_depth + 1);
  75. }
  76. }
  77. void EditorPath::_show_popup() {
  78. if (sub_objects_menu->is_visible()) {
  79. sub_objects_menu->hide();
  80. return;
  81. }
  82. sub_objects_menu->clear();
  83. Size2 size = get_size();
  84. Point2 gp = get_screen_position();
  85. gp.y += size.y;
  86. sub_objects_menu->set_position(gp);
  87. sub_objects_menu->set_size(Size2(size.width, 1));
  88. sub_objects_menu->set_parent_rect(Rect2(Point2(gp - sub_objects_menu->get_position()), size));
  89. sub_objects_menu->take_mouse_focus();
  90. sub_objects_menu->popup();
  91. }
  92. void EditorPath::_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 EditorPath::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 EditorPath::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 EditorPath::enable_path() {
  155. set_disabled(false);
  156. sub_objects_icon->show();
  157. }
  158. void EditorPath::_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 EditorPath::_notification(int p_what) {
  167. switch (p_what) {
  168. case NOTIFICATION_ENTER_TREE:
  169. case NOTIFICATION_THEME_CHANGED: {
  170. update_path();
  171. sub_objects_icon->set_texture(get_theme_icon(SNAME("arrow"), SNAME("OptionButton")));
  172. current_object_label->add_theme_font_override("font", get_theme_font(SNAME("main"), SNAME("EditorFonts")));
  173. } break;
  174. case NOTIFICATION_READY: {
  175. connect("pressed", callable_mp(this, &EditorPath::_show_popup));
  176. } break;
  177. }
  178. }
  179. void EditorPath::_bind_methods() {
  180. }
  181. EditorPath::EditorPath(EditorSelectionHistory *p_history) {
  182. history = p_history;
  183. MarginContainer *main_mc = memnew(MarginContainer);
  184. main_mc->set_anchors_and_offsets_preset(PRESET_FULL_RECT);
  185. main_mc->add_theme_constant_override("margin_left", 4 * EDSCALE);
  186. main_mc->add_theme_constant_override("margin_right", 6 * EDSCALE);
  187. add_child(main_mc);
  188. HBoxContainer *main_hb = memnew(HBoxContainer);
  189. main_mc->add_child(main_hb);
  190. current_object_icon = memnew(TextureRect);
  191. current_object_icon->set_stretch_mode(TextureRect::STRETCH_KEEP_CENTERED);
  192. main_hb->add_child(current_object_icon);
  193. current_object_label = memnew(Label);
  194. current_object_label->set_text_overrun_behavior(TextServer::OVERRUN_TRIM_ELLIPSIS);
  195. current_object_label->set_h_size_flags(SIZE_EXPAND_FILL);
  196. current_object_label->set_auto_translate(false);
  197. main_hb->add_child(current_object_label);
  198. sub_objects_icon = memnew(TextureRect);
  199. sub_objects_icon->hide();
  200. sub_objects_icon->set_stretch_mode(TextureRect::STRETCH_KEEP_CENTERED);
  201. main_hb->add_child(sub_objects_icon);
  202. sub_objects_menu = memnew(PopupMenu);
  203. sub_objects_menu->set_auto_translate(false);
  204. add_child(sub_objects_menu);
  205. sub_objects_menu->connect("about_to_popup", callable_mp(this, &EditorPath::_about_to_show));
  206. sub_objects_menu->connect("id_pressed", callable_mp(this, &EditorPath::_id_pressed));
  207. set_tooltip_text(TTR("Open a list of sub-resources."));
  208. }