Path: blob/master/modules/objectdb_profiler/editor/data_viewers/class_view.cpp
11325 views
/**************************************************************************/1/* class_view.cpp */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#include "class_view.h"3132#include "shared_controls.h"3334#include "editor/editor_node.h"35#include "editor/themes/editor_scale.h"36#include "scene/gui/split_container.h"3738int ClassData::instance_count(GameStateSnapshot *p_snapshot) {39int count = 0;40for (const SnapshotDataObject *instance : instances) {41if (!p_snapshot || instance->snapshot == p_snapshot) {42count += 1;43}44}45return count;46}4748int ClassData::get_recursive_instance_count(HashMap<String, ClassData> &p_all_classes, GameStateSnapshot *p_snapshot) {49if (!recursive_instance_count_cache.has(p_snapshot)) {50recursive_instance_count_cache[p_snapshot] = instance_count(p_snapshot);51for (const String &child : child_classes) {52recursive_instance_count_cache[p_snapshot] += p_all_classes[child].get_recursive_instance_count(p_all_classes, p_snapshot);53}54}55return recursive_instance_count_cache[p_snapshot];56}5758SnapshotClassView::SnapshotClassView() {59set_name(TTRC("Classes"));60}6162void SnapshotClassView::show_snapshot(GameStateSnapshot *p_data, GameStateSnapshot *p_diff_data) {63SnapshotView::show_snapshot(p_data, p_diff_data);6465set_v_size_flags(SizeFlags::SIZE_EXPAND_FILL);66set_h_size_flags(SizeFlags::SIZE_EXPAND_FILL);6768HSplitContainer *classes_view = memnew(HSplitContainer);69add_child(classes_view);70classes_view->set_anchors_preset(LayoutPreset::PRESET_FULL_RECT);71classes_view->set_v_size_flags(SizeFlags::SIZE_EXPAND_FILL);72classes_view->set_h_size_flags(SizeFlags::SIZE_EXPAND_FILL);73classes_view->set_split_offset(0);7475VBoxContainer *class_list_column = memnew(VBoxContainer);76class_list_column->set_v_size_flags(SizeFlags::SIZE_EXPAND_FILL);77class_list_column->set_h_size_flags(SizeFlags::SIZE_EXPAND_FILL);78classes_view->add_child(class_list_column);7980class_tree = memnew(Tree);8182TreeSortAndFilterBar *filter_bar = memnew(TreeSortAndFilterBar(class_tree, TTRC("Filter Classes")));83filter_bar->add_sort_option(TTRC("Name"), TreeSortAndFilterBar::SortType::ALPHA_SORT, 0);8485TreeSortAndFilterBar::SortOptionIndexes default_sort;86if (!diff_data) {87default_sort = filter_bar->add_sort_option(TTRC("Count"), TreeSortAndFilterBar::SortType::NUMERIC_SORT, 1);88} else {89filter_bar->add_sort_option(TTRC("A Count"), TreeSortAndFilterBar::SortType::NUMERIC_SORT, 1);90filter_bar->add_sort_option(TTRC("B Count"), TreeSortAndFilterBar::SortType::NUMERIC_SORT, 2);91default_sort = filter_bar->add_sort_option(TTRC("Delta"), TreeSortAndFilterBar::SortType::NUMERIC_SORT, 3);92}93class_list_column->add_child(filter_bar);9495class_tree->set_select_mode(Tree::SelectMode::SELECT_ROW);96class_tree->set_custom_minimum_size(Size2(200 * EDSCALE, 0));97class_tree->set_hide_folding(false);98class_tree->set_hide_root(true);99class_tree->set_columns(diff_data ? 4 : 2);100class_tree->set_column_titles_visible(true);101class_tree->set_column_title(0, TTRC("Class"));102class_tree->set_column_expand(0, true);103class_tree->set_column_custom_minimum_width(0, 200 * EDSCALE);104class_tree->set_column_title(1, diff_data ? TTRC("A Count") : TTRC("Count"));105class_tree->set_column_expand(1, false);106if (diff_data) {107class_tree->set_column_title_tooltip_text(1, vformat(TTR("A: %s"), snapshot_data->name));108class_tree->set_column_title_tooltip_text(2, vformat(TTR("B: %s"), diff_data->name));109class_tree->set_column_title(2, TTRC("B Count"));110class_tree->set_column_expand(2, false);111class_tree->set_column_title(3, TTRC("Delta"));112class_tree->set_column_expand(3, false);113}114class_tree->connect(SceneStringName(item_selected), callable_mp(this, &SnapshotClassView::_class_selected));115class_tree->set_h_size_flags(SizeFlags::SIZE_EXPAND_FILL);116class_tree->set_v_size_flags(SizeFlags::SIZE_EXPAND_FILL);117class_tree->set_anchors_preset(LayoutPreset::PRESET_FULL_RECT);118class_list_column->add_child(class_tree);119120VSplitContainer *object_lists = memnew(VSplitContainer);121classes_view->add_child(object_lists);122object_lists->set_custom_minimum_size(Size2(150 * EDSCALE, 0));123object_lists->set_h_size_flags(SizeFlags::SIZE_EXPAND_FILL);124object_lists->set_v_size_flags(SizeFlags::SIZE_EXPAND_FILL);125if (!diff_data) {126object_lists->add_child(object_list = _make_object_list_tree(TTRC("Objects")));127} else {128object_lists->add_child(object_list = _make_object_list_tree(TTRC("A Objects")));129object_lists->add_child(diff_object_list = _make_object_list_tree(TTRC("B Objects")));130}131132HashMap<String, ClassData> grouped_by_class;133grouped_by_class["Object"] = ClassData("Object", "");134_add_objects_to_class_map(grouped_by_class, snapshot_data);135if (diff_data != nullptr) {136_add_objects_to_class_map(grouped_by_class, diff_data);137}138139grouped_by_class[""].tree_node = class_tree->create_item();140List<String> classes_todo;141for (const String &c : grouped_by_class[""].child_classes) {142classes_todo.push_front(c);143}144while (classes_todo.size() > 0) {145String next_class_name = classes_todo.front()->get();146classes_todo.pop_front();147ClassData &next = grouped_by_class[next_class_name];148ClassData &nexts_parent = grouped_by_class[next.parent_class_name];149next.tree_node = class_tree->create_item(nexts_parent.tree_node);150next.tree_node->set_text(0, next_class_name + " (" + String::num_int64(next.instance_count(snapshot_data)) + ")");151next.tree_node->set_auto_translate_mode(0, AUTO_TRANSLATE_MODE_DISABLED);152int a_count = next.get_recursive_instance_count(grouped_by_class, snapshot_data);153next.tree_node->set_text(1, String::num_int64(a_count));154if (diff_data) {155int b_count = next.get_recursive_instance_count(grouped_by_class, diff_data);156next.tree_node->set_text(2, String::num_int64(b_count));157next.tree_node->set_text(3, String::num_int64(a_count - b_count));158}159next.tree_node->set_metadata(0, next_class_name);160for (const String &c : next.child_classes) {161classes_todo.push_front(c);162}163}164165// Icons won't load until the frame after show_snapshot is called. Not sure why, but just defer the load.166callable_mp(this, &SnapshotClassView::_notification).call_deferred(NOTIFICATION_THEME_CHANGED);167168// Default to sort by descending count. Putting the biggest groups at the top is generally pretty interesting.169filter_bar->select_sort(default_sort.descending);170filter_bar->apply();171}172173Tree *SnapshotClassView::_make_object_list_tree(const String &p_column_name) {174Tree *list = memnew(Tree);175list->set_select_mode(Tree::SelectMode::SELECT_ROW);176list->set_hide_folding(true);177list->set_hide_root(true);178list->set_columns(1);179list->set_column_titles_visible(true);180list->set_column_title(0, p_column_name);181list->set_column_expand(0, true);182list->connect(SceneStringName(item_selected), callable_mp(this, &SnapshotClassView::_object_selected).bind(list));183list->set_h_size_flags(SizeFlags::SIZE_EXPAND_FILL);184list->set_v_size_flags(SizeFlags::SIZE_EXPAND_FILL);185return list;186}187188void SnapshotClassView::_add_objects_to_class_map(HashMap<String, ClassData> &p_class_map, GameStateSnapshot *p_objects) {189for (const KeyValue<ObjectID, SnapshotDataObject *> &pair : p_objects->objects) {190StringName class_name = pair.value->type_name;191StringName parent_class_name = !class_name.is_empty() && ClassDB::class_exists(class_name) ? ClassDB::get_parent_class(class_name) : "";192193p_class_map[class_name].instances.push_back(pair.value);194195// Go up the tree and insert all parents/grandparents.196while (!class_name.is_empty()) {197if (!p_class_map.has(class_name)) {198p_class_map[class_name] = ClassData(class_name, parent_class_name);199}200201if (!p_class_map.has(parent_class_name)) {202// Leave our grandparent blank for now. Next iteration of the while loop will fill it in.203p_class_map[parent_class_name] = ClassData(parent_class_name, "");204}205p_class_map[class_name].parent_class_name = parent_class_name;206p_class_map[parent_class_name].child_classes.insert(class_name);207208class_name = parent_class_name;209parent_class_name = !class_name.is_empty() ? ClassDB::get_parent_class(class_name) : "";210}211}212}213214void SnapshotClassView::_object_selected(Tree *p_tree) {215GameStateSnapshot *snapshot = snapshot_data;216if (diff_data) {217Tree *other = p_tree == diff_object_list ? object_list : diff_object_list;218TreeItem *selected = other->get_selected();219if (selected) {220selected->deselect(0);221}222if (p_tree == diff_object_list) {223snapshot = diff_data;224}225}226ObjectID object_id = p_tree->get_selected()->get_metadata(0);227EditorNode::get_singleton()->push_item(static_cast<Object *>(snapshot->objects[object_id]));228}229230void SnapshotClassView::_class_selected() {231_update_lists();232}233234void SnapshotClassView::_populate_object_list(GameStateSnapshot *p_snapshot, Tree *p_list, const String &p_name_base) {235p_list->clear();236237TreeItem *selected_item = class_tree->get_selected();238if (selected_item == nullptr) {239p_list->set_column_title(0, vformat("%s (0)", TTR(p_name_base)));240return;241}242243String class_name = selected_item->get_metadata(0);244TreeItem *root = p_list->create_item();245int object_count = 0;246for (const KeyValue<ObjectID, SnapshotDataObject *> &pair : p_snapshot->objects) {247if (pair.value->type_name == class_name) {248TreeItem *item = p_list->create_item(root);249item->set_auto_translate_mode(0, AUTO_TRANSLATE_MODE_DISABLED);250item->set_text(0, pair.value->get_name());251item->set_metadata(0, pair.value->remote_object_id);252item->set_text_overrun_behavior(0, TextServer::OverrunBehavior::OVERRUN_NO_TRIMMING);253object_count++;254}255}256257p_list->set_column_title(0, vformat("%s (%d)", TTR(p_name_base), object_count));258}259260void SnapshotClassView::_update_lists() {261if (snapshot_data == nullptr) {262return;263}264265if (!diff_data) {266_populate_object_list(snapshot_data, object_list, TTRC("Objects"));267} else {268_populate_object_list(snapshot_data, object_list, TTRC("A Objects"));269_populate_object_list(diff_data, diff_object_list, TTRC("B Objects"));270}271}272273void SnapshotClassView::_notification(int p_what) {274if (p_what == NOTIFICATION_THEME_CHANGED) {275for (TreeItem *item : _get_children_recursive(class_tree)) {276item->set_icon(0, EditorNode::get_singleton()->get_class_icon(item->get_metadata(0), ""));277}278} else if (p_what == NOTIFICATION_TRANSLATION_CHANGED) {279_update_lists();280}281}282283284