ランダムに整列

This commit is contained in:
naninunenoy 2020-11-19 01:39:32 +09:00
parent a55f46e7c3
commit ff6b37420a
15 changed files with 132 additions and 76 deletions

View File

@ -1,13 +0,0 @@
using System.Collections.Generic;
namespace AsmdefHelper.DependencyGraph.Editor {
public class AsmdefDependency {
public string DependsFrom { get; }
public IEnumerable<string> DependsTo { get; }
public AsmdefDependency(string key, IEnumerable<string> value) {
DependsFrom = key;
DependsTo = value;
}
}
}

View File

@ -1,11 +0,0 @@
fileFormatVersion: 2
guid: de8afe620d6667940a15a8871781c62d
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -13,16 +13,8 @@ namespace AsmdefHelper.DependencyGraph.Editor {
void OnEnable() {
// .asmdefをすべて取得
var asmdefs = CompilationPipeline.GetAssemblies();
var allDependencies = new List<AsmdefDependency>();
foreach (var asmdef in asmdefs) {
allDependencies.Add(
new AsmdefDependency(
asmdef.name,
asmdef.assemblyReferences?.Select(x => x.name) ?? new string[0])
);
}
// viewの作成
var graphView = new AsmdefGraphView(allDependencies) {
var graphView = new AsmdefGraphView(asmdefs) {
style = { flexGrow = 1 }
};
rootVisualElement.Add(graphView);

View File

@ -1,10 +1,17 @@
using System.Collections.Generic;
using System.Linq;
using AsmdefHelper.DependencyGraph.Editor.DependencyNode;
using AsmdefHelper.DependencyGraph.Editor.DependencyNode.Sort;
using AsmdefHelper.DependencyGraph.Editor.NodeView;
using UnityEditor.Compilation;
using UnityEditor.Experimental.GraphView;
using UnityEngine;
using UnityEngine.UIElements;
namespace AsmdefHelper.DependencyGraph.Editor {
public class AsmdefGraphView : GraphView {
public AsmdefGraphView(IEnumerable<AsmdefDependency> asmdefs) : base() {
public sealed class AsmdefGraphView : GraphView {
public AsmdefGraphView(IEnumerable<Assembly> assemblies) {
var assemblyArr = assemblies.ToArray();
// zoom可能に
SetupZoom(ContentZoomer.DefaultMinScale, ContentZoomer.DefaultMaxScale);
// 背景を黒に
@ -12,24 +19,50 @@ namespace AsmdefHelper.DependencyGraph.Editor {
// ドラッグによる移動可能に
this.AddManipulator(new SelectionDragger());
// ノードの追加
var asmdefNodeDict = new Dictionary<string, AsmdefNode>();
foreach (var asmdef in asmdefs) {
var node = new AsmdefNode(asmdef.DependsFrom);
var asmdefNodeDict = new Dictionary<string, IAsmdefNodeView>();
foreach (var asm in assemblyArr) {
var node = new AsmdefNode(asm.name, contentContainer);
AddElement(node);
asmdefNodeDict.Add(node.title, node);
}
// 依存先にラインを追加
var nodeApapter = new NodeAdapter();
foreach (var asmdef in asmdefs) {
if (!asmdefNodeDict.TryGetValue(asmdef.DependsFrom, out var fromNode)) {
// 依存の整理
var nodeProfiles = assemblyArr
.Select((x, i) => new NodeProfile(new NodeId(i), x.name))
.ToDictionary(x => x.Name);
var dependencies = new List<IDependencyNode>(nodeProfiles.Count);
// 依存先の設定
foreach (var asm in assemblyArr) {
if (nodeProfiles.TryGetValue(asm.name, out var profile)) {
var requireProfiles = asm.assemblyReferences
.Where(x => nodeProfiles.ContainsKey(x.name))
.Select(x => nodeProfiles[x.name]);
var dep = new HashSetDependencyNode(profile);
dep.SetRequireNodes(requireProfiles);
dependencies.Add(dep);
}
}
// 依存元の設定
NodeProcessor.SetBeRequiredNodes(dependencies);
// 依存先にのみラインを追加
foreach (var dep in dependencies) {
if (!asmdefNodeDict.TryGetValue(dep.Profile.Name, out var fromNode)) {
continue;
}
foreach (var dependents in asmdef.DependsTo) {
if (!asmdefNodeDict.TryGetValue(dependents, out var toNode)) {
foreach (var dest in dep.Destinations) {
if (!asmdefNodeDict.TryGetValue(dest.Name, out var toNode)) {
continue;
}
var edge = fromNode.RightPort.ConnectTo(toNode.LeftPort);
contentContainer.Add(edge);// これが無いと線が表示されない
fromNode.RightPort.Connect(toNode.LeftPort);
}
}
// ノードの場所を整列
var sortStrategy = new RandomSortStrategy(Vector2.zero, 100, 600, 100);
var sortedNode = sortStrategy.Sort(dependencies);
foreach (var node in sortedNode) {
if (asmdefNodeDict.TryGetValue(node.Profile.Name, out var nodeView)) {
nodeView.SetPositionXY(node.Position);
}
}
}

View File

@ -1,20 +1,20 @@
using UnityEditor.Experimental.GraphView;
using AsmdefHelper.DependencyGraph.Editor.NodeView;
using UnityEditor.Experimental.GraphView;
using UnityEngine.UIElements;
namespace AsmdefHelper.DependencyGraph.Editor {
public class AsmdefNode : Node {
public readonly Port LeftPort;
public readonly Port RightPort;
public class AsmdefNode : UiElementsNodeView, IAsmdefNodeView {
public IPort LeftPort { get; }
public IPort RightPort { get; }
public AsmdefNode(string nodeName) {
title = nodeName;
public AsmdefNode(string nodeName, VisualElement parentContentContainer) {
Label = nodeName;
LeftPort = Port.Create<Edge>(Orientation.Horizontal, Direction.Output, Port.Capacity.Multi, typeof(Port));
LeftPort.portName = "Ref By";
outputContainer.Add(LeftPort); // as right side
LeftPort = new GraphViewPort(parentContentContainer, Direction.Input) { Label = "Ref By" };
inputContainer.Add(LeftPort as Port); // as right side
RightPort = Port.Create<Edge>(Orientation.Horizontal, Direction.Input, Port.Capacity.Multi, typeof(Port));
RightPort.portName = "Ref To";
inputContainer.Add(RightPort); // as left side
RightPort = new GraphViewPort(parentContentContainer, Direction.Output) { Label = "Ref To" };
outputContainer.Add(RightPort as Port); // as left side
}
}
}

View File

@ -3,12 +3,16 @@ using UnityEngine;
namespace AsmdefHelper.DependencyGraph.Editor.DependencyNode.Sort {
public class NodeGrid {
public readonly float GridSize;
public readonly float GridWidth;
public readonly float GridHeight;
public readonly float Distance;
readonly int gridSideCount;
public readonly int GridCount;
public NodeGrid(float nodeWidth, float nodeDistance, int nodeCount) {
GridSize = nodeDistance + nodeWidth / 2.0F;
public NodeGrid(float nodeWidth, float nodeHeight, float nodeDistance, int nodeCount) {
GridWidth = nodeWidth;
GridHeight = nodeHeight;
Distance = nodeDistance;
gridSideCount = SquareValueProvider.ProvideNearestSquareValue(nodeCount);
GridCount = gridSideCount * gridSideCount;
}
@ -21,10 +25,10 @@ namespace AsmdefHelper.DependencyGraph.Editor.DependencyNode.Sort {
var x = 0.0F;
for (var j = 0; j < gridSideCount; j++) {
grids[index] = new Vector2(x, y);
x += GridSize;
x += (GridWidth/2.0F) + Distance;
index++;
}
y += GridSize;
y += (GridHeight/2.0F) + Distance;;
}
return grids;
}

View File

@ -19,7 +19,7 @@ namespace AsmdefHelper.DependencyGraph.Editor.DependencyNode.Sort {
var nodeArr = nodes
.Select(x => new SortedNode { Profile = x.Profile, Position = originPosition })
.ToArray();
var nodeGrid = new NodeGrid(nodeWidth, basicDistance, nodeArr.Length);
var nodeGrid = new NodeGrid(nodeWidth, nodeHeight, basicDistance, nodeArr.Length);
var positions = nodeGrid.GridCenterPositions();
var indexes = Enumerable.Range(0, positions.Count).ToList();

View File

@ -10,9 +10,8 @@ namespace AsmdefHelper.DependencyGraph.Editor.DependencyNode.Tests {
[Test]
public void TestNodeGrid() {
const float e = 0.000001F;
var nodeGrid = new NodeGrid(10.0F, 10.0F, 4);
var nodeGrid = new NodeGrid(10.0F, 10.0F, 10.0F, 4);
Assert.That(nodeGrid.GridCount, Is.EqualTo(4));
Assert.That(nodeGrid.GridSize, Is.EqualTo(15.0F).Within(e));
var grids = nodeGrid.GridCenterPositions();
Assert.That(grids[0].x, Is.EqualTo(0.0F).Within(e));
Assert.That(grids[0].y, Is.EqualTo(0.0F).Within(e));

View File

@ -0,0 +1,8 @@
using AsmdefHelper.DependencyGraph.Editor.NodeView;
namespace AsmdefHelper.DependencyGraph.Editor {
public interface IAsmdefNodeView : INodeView {
IPort LeftPort { get; }
IPort RightPort { get; }
}
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: b6072eb937a646a4b9cd860534bd9d43
timeCreated: 1605710938

View File

@ -0,0 +1,24 @@
using UnityEditor.Experimental.GraphView;
using UnityEngine;
using UnityEngine.UIElements;
namespace AsmdefHelper.DependencyGraph.Editor.NodeView {
public class GraphViewPort : Port, IPort {
readonly VisualElement parentContentContainer;
public GraphViewPort(VisualElement contentContainer, Direction directionType) : base(Orientation.Horizontal,
directionType, Capacity.Multi, typeof(Port)) {
parentContentContainer = contentContainer;
}
public string Label { set => portName = value; get => portName; }
public Vector2 Position => new Vector2(GetPosition().x, GetPosition().y);
public void Connect(IPort port) {
if (port is Port graphViewPort) {
parentContentContainer.Add(ConnectTo(graphViewPort));
}
}
}
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 44bf77af3f4d4781a1f63e6227256bec
timeCreated: 1605711889

View File

@ -0,0 +1,10 @@
using UnityEditor.Experimental.GraphView;
using UnityEngine;
namespace AsmdefHelper.DependencyGraph.Editor.NodeView {
public interface IPort {
string Label { set; get; }
Vector2 Position { get; }
void Connect(IPort port);
}
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 21b3446cef7248c8b533162402f8429f
timeCreated: 1605711702

View File

@ -1,25 +1,26 @@
using UnityEditor.Graphs;
using UnityEditor.Experimental.GraphView;
using UnityEngine;
namespace AsmdefHelper.DependencyGraph.Editor.NodeView {
public class UiElementsNodeView : Node, INodeView {
string INodeView.Label {
public string Label {
get => title;
set => title = value;
}
float IRect.PositionX {
get => position.x;
set => position.x = value;
public float PositionX {
get => transform.position.x;
set => transform.position = new Vector3(value, PositionY, transform.position.z);
}
float IRect.PositionY {
get => position.y;
set => position.y = value;
public float PositionY {
get => transform.position.y;
set => transform.position = new Vector3(PositionX, value, transform.position.z);
}
float IRect.Height => position.height;
public float Height => contentRect.height;
float IRect.Width => position.width;
public float Width => contentRect.width;
}
}