editor_path.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /*************************************************************************/
  2. /* editor_path.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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. List<PropertyInfo> pinfo;
  37. p_obj->get_property_list(&pinfo);
  38. for (List<PropertyInfo>::Element *E = pinfo.front(); E; E = E->next()) {
  39. if (!(E->get().usage & PROPERTY_USAGE_EDITOR))
  40. continue;
  41. if (E->get().hint != PROPERTY_HINT_RESOURCE_TYPE)
  42. continue;
  43. Variant value = p_obj->get(E->get().name);
  44. if (value.get_type() != Variant::OBJECT)
  45. continue;
  46. Object *obj = value;
  47. if (!obj)
  48. continue;
  49. Ref<Texture2D> icon = EditorNode::get_singleton()->get_object_icon(obj);
  50. int index = get_popup()->get_item_count();
  51. get_popup()->add_icon_item(icon, E->get().name.capitalize(), objects.size());
  52. get_popup()->set_item_h_offset(index, p_depth * 10 * EDSCALE);
  53. objects.push_back(obj->get_instance_id());
  54. _add_children_to_popup(obj, p_depth + 1);
  55. }
  56. }
  57. void EditorPath::_about_to_show() {
  58. Object *obj = ObjectDB::get_instance(history->get_path_object(history->get_path_size() - 1));
  59. if (!obj)
  60. return;
  61. objects.clear();
  62. get_popup()->clear();
  63. get_popup()->set_size(Size2(get_size().width, 1));
  64. _add_children_to_popup(obj);
  65. if (get_popup()->get_item_count() == 0) {
  66. get_popup()->add_item(TTR("No sub-resources found."));
  67. get_popup()->set_item_disabled(0, true);
  68. }
  69. }
  70. void EditorPath::update_path() {
  71. for (int i = 0; i < history->get_path_size(); i++) {
  72. Object *obj = ObjectDB::get_instance(history->get_path_object(i));
  73. if (!obj)
  74. continue;
  75. Ref<Texture2D> icon = EditorNode::get_singleton()->get_object_icon(obj);
  76. if (icon.is_valid())
  77. set_icon(icon);
  78. if (i == history->get_path_size() - 1) {
  79. String name;
  80. if (Object::cast_to<Resource>(obj)) {
  81. Resource *r = Object::cast_to<Resource>(obj);
  82. if (r->get_path().is_resource_file())
  83. name = r->get_path().get_file();
  84. else
  85. name = r->get_name();
  86. if (name == "")
  87. name = r->get_class();
  88. } else if (obj->is_class("EditorDebuggerRemoteObject"))
  89. name = obj->call("get_title");
  90. else if (Object::cast_to<Node>(obj))
  91. name = Object::cast_to<Node>(obj)->get_name();
  92. else if (Object::cast_to<Resource>(obj) && Object::cast_to<Resource>(obj)->get_name() != "")
  93. name = Object::cast_to<Resource>(obj)->get_name();
  94. else
  95. name = obj->get_class();
  96. set_text(" " + name); // An extra space so the text is not too close of the icon.
  97. set_tooltip(obj->get_class());
  98. }
  99. }
  100. }
  101. void EditorPath::_id_pressed(int p_idx) {
  102. ERR_FAIL_INDEX(p_idx, objects.size());
  103. Object *obj = ObjectDB::get_instance(objects[p_idx]);
  104. if (!obj)
  105. return;
  106. EditorNode::get_singleton()->push_item(obj);
  107. }
  108. void EditorPath::_notification(int p_what) {
  109. switch (p_what) {
  110. case NOTIFICATION_THEME_CHANGED: {
  111. update_path();
  112. } break;
  113. }
  114. }
  115. void EditorPath::_bind_methods() {
  116. }
  117. EditorPath::EditorPath(EditorHistory *p_history) {
  118. history = p_history;
  119. set_clip_text(true);
  120. set_text_align(ALIGN_LEFT);
  121. get_popup()->connect("about_to_popup", callable_mp(this, &EditorPath::_about_to_show));
  122. get_popup()->connect("id_pressed", callable_mp(this, &EditorPath::_id_pressed));
  123. }