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