shared_controls.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /**************************************************************************/
  2. /* shared_controls.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 "shared_controls.h"
  31. #include "editor/editor_node.h"
  32. #include "editor/editor_string_names.h"
  33. #include "editor/themes/editor_scale.h"
  34. #include "scene/gui/label.h"
  35. #include "scene/gui/line_edit.h"
  36. #include "scene/gui/menu_button.h"
  37. #include "scene/resources/style_box_flat.h"
  38. SpanningHeader::SpanningHeader(const String &p_text) {
  39. Ref<StyleBoxFlat> title_sbf;
  40. title_sbf.instantiate();
  41. title_sbf->set_bg_color(EditorNode::get_singleton()->get_editor_theme()->get_color("dark_color_3", "Editor"));
  42. add_theme_style_override(SceneStringName(panel), title_sbf);
  43. set_h_size_flags(SizeFlags::SIZE_EXPAND_FILL);
  44. Label *title = memnew(Label(p_text));
  45. add_child(title);
  46. title->set_horizontal_alignment(HorizontalAlignment::HORIZONTAL_ALIGNMENT_CENTER);
  47. title->set_vertical_alignment(VerticalAlignment::VERTICAL_ALIGNMENT_CENTER);
  48. }
  49. DarkPanelContainer::DarkPanelContainer() {
  50. set_h_size_flags(SizeFlags::SIZE_EXPAND_FILL);
  51. set_v_size_flags(SizeFlags::SIZE_EXPAND_FILL);
  52. Ref<StyleBoxFlat> content_wrapper_sbf;
  53. content_wrapper_sbf.instantiate();
  54. content_wrapper_sbf->set_bg_color(EditorNode::get_singleton()->get_editor_theme()->get_color("dark_color_2", "Editor"));
  55. add_theme_style_override(SceneStringName(panel), content_wrapper_sbf);
  56. }
  57. void TreeSortAndFilterBar::_apply_filter(TreeItem *p_current_node) {
  58. if (!p_current_node) {
  59. p_current_node = managed_tree->get_root();
  60. }
  61. if (!p_current_node) {
  62. return;
  63. }
  64. // Reset ourselves to default state.
  65. p_current_node->set_visible(true);
  66. p_current_node->clear_custom_color(0);
  67. // Go through each child and filter them.
  68. bool any_child_visible = false;
  69. for (TreeItem *child = p_current_node->get_first_child(); child; child = child->get_next()) {
  70. _apply_filter(child);
  71. if (child->is_visible()) {
  72. any_child_visible = true;
  73. }
  74. }
  75. // Check if we match the filter.
  76. String filter_str = filter_edit->get_text().strip_edges(true, true).to_lower();
  77. // We are visible.
  78. bool matches_filter = false;
  79. for (int i = 0; i < managed_tree->get_columns(); i++) {
  80. if (p_current_node->get_text(i).to_lower().contains(filter_str)) {
  81. matches_filter = true;
  82. break;
  83. }
  84. }
  85. if (matches_filter || filter_str.is_empty()) {
  86. p_current_node->set_visible(true);
  87. } else if (any_child_visible) {
  88. // We have a visible child.
  89. p_current_node->set_custom_color(0, get_theme_color(SNAME("font_disabled_color"), EditorStringName(Editor)));
  90. } else {
  91. // We and our children are not visible.
  92. p_current_node->set_visible(false);
  93. }
  94. }
  95. void TreeSortAndFilterBar::_apply_sort() {
  96. if (!sort_button->is_visible()) {
  97. return;
  98. }
  99. for (int i = 0; i != sort_button->get_popup()->get_item_count(); i++) {
  100. // Update the popup buttons to be checked/unchecked.
  101. sort_button->get_popup()->set_item_checked(i, (i == (int)current_sort));
  102. }
  103. SortItem sort = sort_items[current_sort];
  104. List<TreeItem *> items_to_sort;
  105. items_to_sort.push_back(managed_tree->get_root());
  106. while (items_to_sort.size() > 0) {
  107. TreeItem *to_sort = items_to_sort.front()->get();
  108. items_to_sort.pop_front();
  109. LocalVector<TreeItemColumn> items;
  110. items.reserve(to_sort->get_child_count());
  111. for (int i = 0; i < to_sort->get_child_count(); i++) {
  112. items.push_back(TreeItemColumn(to_sort->get_child(i), sort.column));
  113. }
  114. if (sort.type == ALPHA_SORT && sort.ascending == true) {
  115. items.sort_custom<TreeItemAlphaComparator>();
  116. }
  117. if (sort.type == ALPHA_SORT && sort.ascending == false) {
  118. items.sort_custom<TreeItemAlphaComparator>();
  119. items.reverse();
  120. }
  121. if (sort.type == NUMERIC_SORT && sort.ascending == true) {
  122. items.sort_custom<TreeItemNumericComparator>();
  123. }
  124. if (sort.type == NUMERIC_SORT && sort.ascending == false) {
  125. items.sort_custom<TreeItemNumericComparator>();
  126. items.reverse();
  127. }
  128. TreeItem *previous = nullptr;
  129. for (const TreeItemColumn &item : items) {
  130. if (previous != nullptr) {
  131. item.item->move_after(previous);
  132. } else {
  133. item.item->move_before(to_sort->get_first_child());
  134. }
  135. previous = item.item;
  136. items_to_sort.push_back(item.item);
  137. }
  138. }
  139. }
  140. void TreeSortAndFilterBar::_sort_changed(int p_id) {
  141. current_sort = p_id;
  142. _apply_sort();
  143. }
  144. void TreeSortAndFilterBar::_filter_changed(const String &p_filter) {
  145. _apply_filter();
  146. }
  147. TreeSortAndFilterBar::TreeSortAndFilterBar(Tree *p_managed_tree, const String &p_filter_placeholder_text) :
  148. managed_tree(p_managed_tree) {
  149. set_h_size_flags(SizeFlags::SIZE_EXPAND_FILL);
  150. add_theme_constant_override("h_separation", 10 * EDSCALE);
  151. filter_edit = memnew(LineEdit);
  152. filter_edit->set_clear_button_enabled(true);
  153. filter_edit->set_h_size_flags(SizeFlags::SIZE_EXPAND_FILL);
  154. filter_edit->set_placeholder(p_filter_placeholder_text);
  155. add_child(filter_edit);
  156. filter_edit->connect(SceneStringName(text_changed), callable_mp(this, &TreeSortAndFilterBar::_filter_changed));
  157. sort_button = memnew(MenuButton);
  158. sort_button->set_visible(false);
  159. sort_button->set_flat(false);
  160. sort_button->set_theme_type_variation("FlatMenuButton");
  161. PopupMenu *p = sort_button->get_popup();
  162. p->connect(SceneStringName(id_pressed), callable_mp(this, &TreeSortAndFilterBar::_sort_changed));
  163. add_child(sort_button);
  164. }
  165. void TreeSortAndFilterBar::_notification(int p_what) {
  166. switch (p_what) {
  167. case NOTIFICATION_POSTINITIALIZE:
  168. case NOTIFICATION_THEME_CHANGED: {
  169. filter_edit->set_right_icon(get_editor_theme_icon(SNAME("Search")));
  170. sort_button->set_button_icon(get_editor_theme_icon(SNAME("Sort")));
  171. apply();
  172. } break;
  173. }
  174. }
  175. TreeSortAndFilterBar::SortOptionIndexes TreeSortAndFilterBar::add_sort_option(const String &p_new_option, SortType p_sort_type, int p_sort_column, bool p_is_default) {
  176. sort_button->set_visible(true);
  177. bool is_first_item = sort_items.is_empty();
  178. SortItem item_ascending(sort_items.size(), vformat(TTR("Sort By %s (Ascending)"), p_new_option), p_sort_type, true, p_sort_column);
  179. sort_items[item_ascending.id] = item_ascending;
  180. sort_button->get_popup()->add_radio_check_item(item_ascending.label, item_ascending.id);
  181. SortItem item_descending(sort_items.size(), vformat(TTR("Sort By %s (Descending)"), p_new_option), p_sort_type, false, p_sort_column);
  182. sort_items[item_descending.id] = item_descending;
  183. sort_button->get_popup()->add_radio_check_item(item_descending.label, item_descending.id);
  184. if (is_first_item) {
  185. sort_button->get_popup()->set_item_checked(0, true);
  186. }
  187. SortOptionIndexes indexes;
  188. indexes.ascending = item_ascending.id;
  189. indexes.descending = item_descending.id;
  190. return indexes;
  191. }
  192. void TreeSortAndFilterBar::clear_filter() {
  193. filter_edit->clear();
  194. }
  195. void TreeSortAndFilterBar::clear() {
  196. sort_button->set_visible(false);
  197. sort_button->get_popup()->clear();
  198. filter_edit->clear();
  199. }
  200. void TreeSortAndFilterBar::select_sort(int p_item_id) {
  201. _sort_changed(p_item_id);
  202. }
  203. void TreeSortAndFilterBar::apply() {
  204. if (!managed_tree || !managed_tree->get_root()) {
  205. return;
  206. }
  207. _apply_sort();
  208. _apply_filter();
  209. }