editor_path.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /*************************************************************************/
  2. /* editor_path.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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<Texture> icon = EditorNode::get_singleton()->get_object_icon(obj);
  50. int index = popup->get_item_count();
  51. popup->add_icon_item(icon, E->get().name.capitalize(), objects.size());
  52. 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::_gui_input(const Ref<InputEvent> &p_event) {
  58. Ref<InputEventMouseButton> mb = p_event;
  59. if (mb.is_valid() && mb->get_button_index() == BUTTON_LEFT && mb->is_pressed()) {
  60. Object *obj = ObjectDB::get_instance(history->get_path_object(history->get_path_size() - 1));
  61. if (!obj)
  62. return;
  63. objects.clear();
  64. popup->clear();
  65. _add_children_to_popup(obj);
  66. popup->set_position(get_global_position() + Vector2(0, get_size().height));
  67. popup->set_size(Size2(get_size().width, 1));
  68. popup->popup();
  69. }
  70. }
  71. void EditorPath::_notification(int p_what) {
  72. switch (p_what) {
  73. case NOTIFICATION_MOUSE_ENTER: {
  74. mouse_over = true;
  75. update();
  76. } break;
  77. case NOTIFICATION_MOUSE_EXIT: {
  78. mouse_over = false;
  79. update();
  80. } break;
  81. case NOTIFICATION_DRAW: {
  82. RID ci = get_canvas_item();
  83. Ref<Font> label_font = get_font("font", "Label");
  84. Size2i size = get_size();
  85. Ref<Texture> sn = get_icon("SmallNext", "EditorIcons");
  86. Ref<StyleBox> sb = get_stylebox("pressed", "Button");
  87. int ofs = sb->get_margin(MARGIN_LEFT);
  88. if (mouse_over) {
  89. draw_style_box(sb, Rect2(Point2(), get_size()));
  90. }
  91. for (int i = 0; i < history->get_path_size(); i++) {
  92. Object *obj = ObjectDB::get_instance(history->get_path_object(i));
  93. if (!obj)
  94. continue;
  95. String type = obj->get_class();
  96. Ref<Texture> icon = EditorNode::get_singleton()->get_object_icon(obj);
  97. icon->draw(ci, Point2i(ofs, (size.height - icon->get_height()) / 2));
  98. ofs += icon->get_width();
  99. if (i == history->get_path_size() - 1) {
  100. //add name
  101. ofs += 4;
  102. int left = size.width - ofs;
  103. if (left < 0)
  104. continue;
  105. String name;
  106. if (Object::cast_to<Resource>(obj)) {
  107. Resource *r = Object::cast_to<Resource>(obj);
  108. if (r->get_path().is_resource_file())
  109. name = r->get_path().get_file();
  110. else
  111. name = r->get_name();
  112. if (name == "")
  113. name = r->get_class();
  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. set_tooltip(obj->get_class());
  123. label_font->draw(ci, Point2i(ofs, (size.height - label_font->get_height()) / 2 + label_font->get_ascent()), name, get_color("font_color", "Label"), left);
  124. } else {
  125. //add arrow
  126. //sn->draw(ci,Point2i(ofs,(size.height-sn->get_height())/2));
  127. //ofs+=sn->get_width();
  128. ofs += 5; //just looks better! somehow
  129. }
  130. }
  131. } break;
  132. }
  133. }
  134. void EditorPath::update_path() {
  135. update();
  136. }
  137. void EditorPath::_popup_select(int p_idx) {
  138. ERR_FAIL_INDEX(p_idx, objects.size());
  139. Object *obj = ObjectDB::get_instance(objects[p_idx]);
  140. if (!obj)
  141. return;
  142. EditorNode::get_singleton()->push_item(obj);
  143. }
  144. void EditorPath::_bind_methods() {
  145. ClassDB::bind_method("_gui_input", &EditorPath::_gui_input);
  146. ClassDB::bind_method("_popup_select", &EditorPath::_popup_select);
  147. }
  148. EditorPath::EditorPath(EditorHistory *p_history) {
  149. history = p_history;
  150. mouse_over = false;
  151. popup = memnew(PopupMenu);
  152. popup->connect("id_pressed", this, "_popup_select");
  153. add_child(popup);
  154. }