editor_path.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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 (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<Texture2D> icon = EditorNode::get_singleton()->get_object_icon(obj);
  55. int index = get_popup()->get_item_count();
  56. get_popup()->add_icon_item(icon, E->get().name.capitalize(), objects.size());
  57. get_popup()->set_item_h_offset(index, p_depth * 10 * EDSCALE);
  58. objects.push_back(obj->get_instance_id());
  59. _add_children_to_popup(obj, p_depth + 1);
  60. }
  61. }
  62. void EditorPath::_about_to_show() {
  63. Object *obj = ObjectDB::get_instance(history->get_path_object(history->get_path_size() - 1));
  64. if (!obj) {
  65. return;
  66. }
  67. objects.clear();
  68. get_popup()->clear();
  69. get_popup()->set_size(Size2(get_size().width, 1));
  70. _add_children_to_popup(obj);
  71. if (get_popup()->get_item_count() == 0) {
  72. get_popup()->add_item(TTR("No sub-resources found."));
  73. get_popup()->set_item_disabled(0, true);
  74. }
  75. }
  76. void EditorPath::update_path() {
  77. for (int i = 0; i < history->get_path_size(); i++) {
  78. Object *obj = ObjectDB::get_instance(history->get_path_object(i));
  79. if (!obj) {
  80. continue;
  81. }
  82. Ref<Texture2D> icon = EditorNode::get_singleton()->get_object_icon(obj);
  83. if (icon.is_valid()) {
  84. set_icon(icon);
  85. }
  86. if (i == history->get_path_size() - 1) {
  87. String name;
  88. if (Object::cast_to<Resource>(obj)) {
  89. Resource *r = Object::cast_to<Resource>(obj);
  90. if (r->get_path().is_resource_file()) {
  91. name = r->get_path().get_file();
  92. } else {
  93. name = r->get_name();
  94. }
  95. if (name == "") {
  96. name = r->get_class();
  97. }
  98. } else if (obj->is_class("EditorDebuggerRemoteObject")) {
  99. name = obj->call("get_title");
  100. } else if (Object::cast_to<Node>(obj)) {
  101. name = Object::cast_to<Node>(obj)->get_name();
  102. } else if (Object::cast_to<Resource>(obj) && Object::cast_to<Resource>(obj)->get_name() != "") {
  103. name = Object::cast_to<Resource>(obj)->get_name();
  104. } else {
  105. name = obj->get_class();
  106. }
  107. set_text(" " + name); // An extra space so the text is not too close of the icon.
  108. set_tooltip(obj->get_class());
  109. }
  110. }
  111. }
  112. void EditorPath::_id_pressed(int p_idx) {
  113. ERR_FAIL_INDEX(p_idx, objects.size());
  114. Object *obj = ObjectDB::get_instance(objects[p_idx]);
  115. if (!obj) {
  116. return;
  117. }
  118. EditorNode::get_singleton()->push_item(obj);
  119. }
  120. void EditorPath::_notification(int p_what) {
  121. switch (p_what) {
  122. case NOTIFICATION_THEME_CHANGED: {
  123. update_path();
  124. } break;
  125. }
  126. }
  127. void EditorPath::_bind_methods() {
  128. }
  129. EditorPath::EditorPath(EditorHistory *p_history) {
  130. history = p_history;
  131. set_clip_text(true);
  132. set_text_align(ALIGN_LEFT);
  133. get_popup()->connect("about_to_popup", callable_mp(this, &EditorPath::_about_to_show));
  134. get_popup()->connect("id_pressed", callable_mp(this, &EditorPath::_id_pressed));
  135. }