using System; using System.Collections.Generic; using System.Windows.Media; using JD.AssetBrowser.ViewModel; using QuickGraph; namespace JD.AssetDependencies.Graph { public class DependencyGraph : BidirectionalGraph { private static readonly SolidColorBrush SelectedColor = new SolidColorBrush(Color.FromRgb(199, 234, 215)); private static readonly SolidColorBrush ChildDependencyColor = new SolidColorBrush(Color.FromRgb(219, 219, 219)); private static readonly SolidColorBrush ParentDependencyColor = new SolidColorBrush(Color.FromRgb(197, 208, 221)); private static readonly SolidColorBrush ErrorColor = Brushes.LightCoral; public enum DependencyType { Parent, Children } private readonly Dictionary _dictionary = new Dictionary(); private DataVertex GetOrCreate(AssetViewModel fileEntry) { if (_dictionary.ContainsKey(fileEntry)) return _dictionary[fileEntry]; var vertex = new DataVertex { File = fileEntry }; _dictionary.Add(fileEntry, vertex); return vertex; } public static DependencyGraph FromFile(AssetViewModel asset, int parentsMaxDepth = 3, int childrenMaxDepth = 3, Predicate filterFunc = null) { var graph = new DependencyGraph(); if (asset != null) { var root = graph.GetOrCreate(asset); root.Color = SelectedColor; graph.AddVertex(root); graph.AddDependencies(root, DependencyType.Children, childrenMaxDepth, filterFunc); graph.AddDependencies(root, DependencyType.Parent, parentsMaxDepth, filterFunc); } return graph; } public void AddDependencies(DataVertex vertex, DependencyType dependencyType, int maxDepth, Predicate filterFunc = null) { if (maxDepth <= 0) return; var dependencies = dependencyType == DependencyType.Parent ? vertex.File.Model.ReferencedBy.AsFiles() : vertex.File.Model.Dependencies.AsFiles(); foreach (var file in dependencies) { if (filterFunc != null && !filterFunc(file)) { continue; } var dependencyVertex = GetOrCreate((AssetViewModel)AssetDatabaseViewModel.Instance.GetOrCreateViewModel(file.Path)); AddVertex(dependencyVertex); if (dependencyType == DependencyType.Children) { dependencyVertex.Color = ChildDependencyColor; AddEdge(new DataEdge { Source = vertex, Target = dependencyVertex }); } else { dependencyVertex.Color = ParentDependencyColor; AddEdge(new DataEdge { Source = dependencyVertex, Target = vertex }); } AddDependencies(dependencyVertex, dependencyType, maxDepth - 1, filterFunc); } } } }