| 1 | /**************************************************************************/ |
| 2 | /* class_view.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 "class_view.h" |
| 32 | |
| 33 | #include "shared_controls.h" |
| 34 | |
| 35 | #include "editor/editor_node.h" |
| 36 | #include "editor/themes/editor_scale.h" |
| 37 | #include "scene/gui/split_container.h" |
| 38 | |
| 39 | int ClassData::instance_count(GameStateSnapshot *p_snapshot) { |
| 40 | int count = 0; |
| 41 | for (const SnapshotDataObject *instance : instances) { |
| 42 | if (!p_snapshot || instance->snapshot == p_snapshot) { |
| 43 | count += 1; |
| 44 | } |
| 45 | } |
| 46 | return count; |
| 47 | } |
| 48 | |
| 49 | int ClassData::get_recursive_instance_count(HashMap<String, ClassData> &p_all_classes, GameStateSnapshot *p_snapshot) { |
| 50 | if (!recursive_instance_count_cache.has(p_snapshot)) { |
| 51 | recursive_instance_count_cache[p_snapshot] = instance_count(p_snapshot); |
| 52 | for (const String &child : child_classes) { |
| 53 | recursive_instance_count_cache[p_snapshot] += p_all_classes[child].get_recursive_instance_count(p_all_classes, p_snapshot); |
| 54 | } |
| 55 | } |
| 56 | return recursive_instance_count_cache[p_snapshot]; |
| 57 | } |
| 58 | |
| 59 | SnapshotClassView::SnapshotClassView() { |
| 60 | set_name(TTRC("Classes")); |
| 61 | } |
| 62 | |
| 63 | void SnapshotClassView::show_snapshot(GameStateSnapshot *p_data, GameStateSnapshot *p_diff_data) { |
| 64 | SnapshotView::show_snapshot(p_data, p_diff_data); |
| 65 | |
| 66 | set_v_size_flags(SizeFlags::SIZE_EXPAND_FILL); |
| 67 | set_h_size_flags(SizeFlags::SIZE_EXPAND_FILL); |
| 68 | |
| 69 | HSplitContainer *classes_view = memnew(HSplitContainer); |
| 70 | add_child(classes_view); |
| 71 | classes_view->set_anchors_preset(LayoutPreset::PRESET_FULL_RECT); |
| 72 | classes_view->set_v_size_flags(SizeFlags::SIZE_EXPAND_FILL); |
| 73 | classes_view->set_h_size_flags(SizeFlags::SIZE_EXPAND_FILL); |
| 74 | classes_view->set_split_offset(0); |
| 75 | |
| 76 | VBoxContainer *class_list_column = memnew(VBoxContainer); |
| 77 | class_list_column->set_v_size_flags(SizeFlags::SIZE_EXPAND_FILL); |
| 78 | class_list_column->set_h_size_flags(SizeFlags::SIZE_EXPAND_FILL); |
| 79 | classes_view->add_child(class_list_column); |
| 80 | |
| 81 | class_tree = memnew(Tree); |
| 82 | |
| 83 | TreeSortAndFilterBar *filter_bar = memnew(TreeSortAndFilterBar(class_tree, TTRC("Filter Classes"))); |
| 84 | filter_bar->add_sort_option(TTRC("Name"), TreeSortAndFilterBar::SortType::ALPHA_SORT, 0); |
| 85 | |
| 86 | TreeSortAndFilterBar::SortOptionIndexes default_sort; |
| 87 | if (!diff_data) { |
| 88 | default_sort = filter_bar->add_sort_option(TTRC("Count"), TreeSortAndFilterBar::SortType::NUMERIC_SORT, 1); |
| 89 | } else { |
| 90 | filter_bar->add_sort_option(TTRC("A Count"), TreeSortAndFilterBar::SortType::NUMERIC_SORT, 1); |
| 91 | filter_bar->add_sort_option(TTRC("B Count"), TreeSortAndFilterBar::SortType::NUMERIC_SORT, 2); |
| 92 | default_sort = filter_bar->add_sort_option(TTRC("Delta"), TreeSortAndFilterBar::SortType::NUMERIC_SORT, 3); |
| 93 | } |
| 94 | class_list_column->add_child(filter_bar); |
| 95 | |
| 96 | class_tree->set_select_mode(Tree::SelectMode::SELECT_ROW); |
| 97 | class_tree->set_custom_minimum_size(Size2(200 * EDSCALE, 0)); |
| 98 | class_tree->set_hide_folding(false); |
| 99 | class_tree->set_hide_root(true); |
| 100 | class_tree->set_columns(diff_data ? 4 : 2); |
| 101 | class_tree->set_column_titles_visible(true); |
| 102 | class_tree->set_column_title(0, TTRC("Class")); |
| 103 | class_tree->set_column_expand(0, true); |
| 104 | class_tree->set_column_custom_minimum_width(0, 200 * EDSCALE); |
| 105 | class_tree->set_column_title(1, diff_data ? TTRC("A Count") : TTRC("Count")); |
| 106 | class_tree->set_column_expand(1, false); |
| 107 | class_tree->set_theme_type_variation("TreeSecondary"); |
| 108 | |
| 109 | if (diff_data) { |
| 110 | class_tree->set_column_title_tooltip_text(1, vformat(TTR("A: %s"), snapshot_data->name)); |
| 111 | class_tree->set_column_title_tooltip_text(2, vformat(TTR("B: %s"), diff_data->name)); |
| 112 | class_tree->set_column_title(2, TTRC("B Count")); |
| 113 | class_tree->set_column_expand(2, false); |
| 114 | class_tree->set_column_title(3, TTRC("Delta")); |
| 115 | class_tree->set_column_expand(3, false); |
| 116 | } |
| 117 | |
| 118 | class_tree->connect(SceneStringName(item_selected), callable_mp(this, &SnapshotClassView::_class_selected)); |
| 119 | class_tree->set_h_size_flags(SizeFlags::SIZE_EXPAND_FILL); |
| 120 | class_tree->set_v_size_flags(SizeFlags::SIZE_EXPAND_FILL); |
| 121 | class_tree->set_anchors_preset(LayoutPreset::PRESET_FULL_RECT); |
| 122 | class_list_column->add_child(class_tree); |
| 123 | |
| 124 | VSplitContainer *object_lists = memnew(VSplitContainer); |
| 125 | classes_view->add_child(object_lists); |
| 126 | object_lists->set_custom_minimum_size(Size2(150 * EDSCALE, 0)); |
| 127 | object_lists->set_h_size_flags(SizeFlags::SIZE_EXPAND_FILL); |
| 128 | object_lists->set_v_size_flags(SizeFlags::SIZE_EXPAND_FILL); |
| 129 | if (!diff_data) { |
| 130 | object_lists->add_child(object_list = _make_object_list_tree(TTRC("Objects"))); |
| 131 | } else { |
| 132 | object_lists->add_child(object_list = _make_object_list_tree(TTRC("A Objects"))); |
| 133 | object_lists->add_child(diff_object_list = _make_object_list_tree(TTRC("B Objects"))); |
| 134 | } |
| 135 | |
| 136 | HashMap<String, ClassData> grouped_by_class; |
| 137 | grouped_by_class["Object"] = ClassData("Object", ""); |
| 138 | _add_objects_to_class_map(grouped_by_class, snapshot_data); |
| 139 | if (diff_data != nullptr) { |
| 140 | _add_objects_to_class_map(grouped_by_class, diff_data); |
| 141 | } |
| 142 | |
| 143 | grouped_by_class[""].tree_node = class_tree->create_item(); |
| 144 | List<String> classes_todo; |
| 145 | for (const String &c : grouped_by_class[""].child_classes) { |
| 146 | classes_todo.push_front(c); |
| 147 | } |
| 148 | while (classes_todo.size() > 0) { |
| 149 | String next_class_name = classes_todo.front()->get(); |
| 150 | classes_todo.pop_front(); |
| 151 | ClassData &next = grouped_by_class[next_class_name]; |
| 152 | ClassData &nexts_parent = grouped_by_class[next.parent_class_name]; |
| 153 | next.tree_node = class_tree->create_item(nexts_parent.tree_node); |
| 154 | next.tree_node->set_text(0, next_class_name + " (" + String::num_int64(next.instance_count(snapshot_data)) + ")"); |
| 155 | next.tree_node->set_auto_translate_mode(0, AUTO_TRANSLATE_MODE_DISABLED); |
| 156 | int a_count = next.get_recursive_instance_count(grouped_by_class, snapshot_data); |
| 157 | next.tree_node->set_text(1, String::num_int64(a_count)); |
| 158 | if (diff_data) { |
| 159 | int b_count = next.get_recursive_instance_count(grouped_by_class, diff_data); |
| 160 | next.tree_node->set_text(2, String::num_int64(b_count)); |
| 161 | next.tree_node->set_text(3, String::num_int64(a_count - b_count)); |
| 162 | } |
| 163 | next.tree_node->set_metadata(0, next_class_name); |
| 164 | for (const String &c : next.child_classes) { |
| 165 | classes_todo.push_front(c); |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | // Icons won't load until the frame after show_snapshot is called. Not sure why, but just defer the load. |
| 170 | callable_mp(this, &SnapshotClassView::_notification).call_deferred(NOTIFICATION_THEME_CHANGED); |
| 171 | |
| 172 | // Default to sort by descending count. Putting the biggest groups at the top is generally pretty interesting. |
| 173 | filter_bar->select_sort(default_sort.descending); |
| 174 | filter_bar->apply(); |
| 175 | } |
| 176 | |
| 177 | Tree *SnapshotClassView::_make_object_list_tree(const String &p_column_name) { |
| 178 | Tree *list = memnew(Tree); |
| 179 | list->set_select_mode(Tree::SelectMode::SELECT_ROW); |
| 180 | list->set_hide_folding(true); |
| 181 | list->set_hide_root(true); |
| 182 | list->set_columns(1); |
| 183 | list->set_column_titles_visible(true); |
| 184 | list->set_column_title(0, p_column_name); |
| 185 | list->set_column_expand(0, true); |
| 186 | list->set_theme_type_variation("TreeSecondary"); |
| 187 | list->connect(SceneStringName(item_selected), callable_mp(this, &SnapshotClassView::_object_selected).bind(list)); |
| 188 | list->set_h_size_flags(SizeFlags::SIZE_EXPAND_FILL); |
| 189 | list->set_v_size_flags(SizeFlags::SIZE_EXPAND_FILL); |
| 190 | return list; |
| 191 | } |
| 192 | |
| 193 | void SnapshotClassView::_add_objects_to_class_map(HashMap<String, ClassData> &p_class_map, GameStateSnapshot *p_objects) { |
| 194 | for (const KeyValue<ObjectID, SnapshotDataObject *> &pair : p_objects->objects) { |
| 195 | StringName class_name = pair.value->type_name; |
| 196 | StringName parent_class_name = !class_name.is_empty() && ClassDB::class_exists(class_name) ? ClassDB::get_parent_class(class_name) : ""; |
| 197 | |
| 198 | p_class_map[class_name].instances.push_back(pair.value); |
| 199 | |
| 200 | // Go up the tree and insert all parents/grandparents. |
| 201 | while (!class_name.is_empty()) { |
| 202 | if (!p_class_map.has(class_name)) { |
| 203 | p_class_map[class_name] = ClassData(class_name, parent_class_name); |
| 204 | } |
| 205 | |
| 206 | if (!p_class_map.has(parent_class_name)) { |
| 207 | // Leave our grandparent blank for now. Next iteration of the while loop will fill it in. |
| 208 | p_class_map[parent_class_name] = ClassData(parent_class_name, ""); |
| 209 | } |
| 210 | p_class_map[class_name].parent_class_name = parent_class_name; |
| 211 | p_class_map[parent_class_name].child_classes.insert(class_name); |
| 212 | |
| 213 | class_name = parent_class_name; |
| 214 | parent_class_name = !class_name.is_empty() ? ClassDB::get_parent_class(class_name) : ""; |
| 215 | } |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | void SnapshotClassView::_object_selected(Tree *p_tree) { |
| 220 | GameStateSnapshot *snapshot = snapshot_data; |
| 221 | if (diff_data) { |
| 222 | Tree *other = p_tree == diff_object_list ? object_list : diff_object_list; |
| 223 | TreeItem *selected = other->get_selected(); |
| 224 | if (selected) { |
| 225 | selected->deselect(0); |
| 226 | } |
| 227 | if (p_tree == diff_object_list) { |
| 228 | snapshot = diff_data; |
| 229 | } |
| 230 | } |
| 231 | ObjectID object_id = p_tree->get_selected()->get_metadata(0); |
| 232 | EditorNode::get_singleton()->push_item(static_cast<Object *>(snapshot->objects[object_id])); |
| 233 | } |
| 234 | |
| 235 | void SnapshotClassView::_class_selected() { |
| 236 | _update_lists(); |
| 237 | } |
| 238 | |
| 239 | void SnapshotClassView::_populate_object_list(GameStateSnapshot *p_snapshot, Tree *p_list, const String &p_name_base) { |
| 240 | p_list->clear(); |
| 241 | |
| 242 | TreeItem *selected_item = class_tree->get_selected(); |
| 243 | if (selected_item == nullptr) { |
| 244 | p_list->set_column_title(0, vformat("%s (0)", TTR(p_name_base))); |
| 245 | return; |
| 246 | } |
| 247 | |
| 248 | String class_name = selected_item->get_metadata(0); |
| 249 | TreeItem *root = p_list->create_item(); |
| 250 | int object_count = 0; |
| 251 | for (const KeyValue<ObjectID, SnapshotDataObject *> &pair : p_snapshot->objects) { |
| 252 | if (pair.value->type_name == class_name) { |
| 253 | TreeItem *item = p_list->create_item(root); |
| 254 | item->set_auto_translate_mode(0, AUTO_TRANSLATE_MODE_DISABLED); |
| 255 | item->set_text(0, pair.value->get_name()); |
| 256 | item->set_metadata(0, pair.value->remote_object_id); |
| 257 | item->set_text_overrun_behavior(0, TextServer::OverrunBehavior::OVERRUN_NO_TRIMMING); |
| 258 | object_count++; |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | p_list->set_column_title(0, vformat("%s (%d)", TTR(p_name_base), object_count)); |
| 263 | } |
| 264 | |
| 265 | void SnapshotClassView::_update_lists() { |
| 266 | if (snapshot_data == nullptr) { |
| 267 | return; |
| 268 | } |
| 269 | |
| 270 | if (!diff_data) { |
| 271 | _populate_object_list(snapshot_data, object_list, TTRC("Objects")); |
| 272 | } else { |
| 273 | _populate_object_list(snapshot_data, object_list, TTRC("A Objects")); |
| 274 | _populate_object_list(diff_data, diff_object_list, TTRC("B Objects")); |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | void SnapshotClassView::_notification(int p_what) { |
| 279 | if (p_what == NOTIFICATION_THEME_CHANGED) { |
| 280 | for (TreeItem *item : _get_children_recursive(class_tree)) { |
| 281 | item->set_icon(0, EditorNode::get_singleton()->get_class_icon(item->get_metadata(0), "")); |
| 282 | } |
| 283 | } else if (p_what == NOTIFICATION_TRANSLATION_CHANGED) { |
| 284 | _update_lists(); |
| 285 | } |
| 286 | } |