editor_path.cpp 6.5 KB

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