Skip to content

Commit

Permalink
Initial featureset complete, actually this time. Pre-cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
CoffeeVampir3 committed Mar 17, 2021
1 parent ac95007 commit 93ce3b5
Show file tree
Hide file tree
Showing 11 changed files with 102 additions and 68 deletions.
3 changes: 2 additions & 1 deletion Bridged/GraphExecutor/GraphExecutor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ public void Reset()
currentNode = graphController.rootNode;
nextNode = null;
previousNode = null;
EditorLinkedResetGraph(currentVirtualGraph.virtualId);
if(currentVirtualGraph != null)
EditorLinkedResetGraph(currentVirtualGraph.virtualId);
}
#endif

Expand Down
19 changes: 19 additions & 0 deletions EditorOnly/BaseGraph/CoffeeGraphView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,14 +97,19 @@ void CleanUndoRemnants()
}
if(graphModel != null)
{
graphModel.viewPosition = viewTransform.position;
graphModel.viewZoom = viewTransform.scale;
EditorUtility.SetDirty(graphModel);
EditorUtility.SetDirty(graphModel.serializedGraphController);
CleanUndoRemnants();
}
AssetDatabase.SaveAssets();
}

#endregion

#region Internal API

/// <summary>
/// Loads the provided GraphModel.
/// </summary>
Expand All @@ -128,6 +133,8 @@ protected internal virtual void LoadGraph(GraphModel modelToLoad)
}

Undo.ClearAll();
viewTransform.position = graphModel.viewPosition;
viewTransform.scale = graphModel.viewZoom;
BuildGraph();
}

Expand Down Expand Up @@ -161,6 +168,8 @@ protected internal virtual void RuntimeNodeExited(RuntimeNode node)

view.RemoveFromClassList("CurrentNode");
}

#endregion

#region Public API

Expand Down Expand Up @@ -594,9 +603,18 @@ private bool TryCreateConnection(Edge edge,
{
return false;
}

var localConnection = inModel.LinkPortTo(inputPort, outModel, outputPort);
var remoteConnection = outModel.LinkPortTo(outputPort, inModel, inputPort);

//If we're trying to reconnect a port that was already connected, discard the old connection first.
if (edgeToModel.TryGetValue(edge, out var oldEdgeModel))
{
edgeToModel.Remove(edge);
if (graphModel.edgeModels.Contains(oldEdgeModel))
DeleteEdge(edge, oldEdgeModel);
}

var modelEdge = new EdgeModel(inModel, inputPort,
outModel, outputPort,
localConnection.GUID,
Expand Down Expand Up @@ -690,6 +708,7 @@ private void ProcessEdgesToCreate(ref List<Edge> addedEdges)
}
else
{
Undo.IncrementCurrentGroup();
AddEdgeClasses(edge);
}
}
Expand Down
16 changes: 2 additions & 14 deletions EditorOnly/BaseGraph/CoffeeGraphWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ public class CoffeeGraphWindow : EditorWindow
{
[SerializeReference]
protected CoffeeGraphView graphView;
[SerializeReference]
public string currentGraphGUID;
private bool isWindowLoaded = false;
private Action OnWindowLayoutFinished = null;
private string domainSafeWorkingAssetPath;
Expand Down Expand Up @@ -47,7 +45,7 @@ private void Debug__ExpandAllItems()
}
}

#region Public API
#region Linking API

public void ResetGraph(int graphId)
{
Expand Down Expand Up @@ -89,19 +87,9 @@ private void OnEnable()
LoadGraphControllerInternal(domainSafeWorkingAssetPath);
};
}

private void OnDisable()
{
rootVisualElement.Clear();
}


private void InitializeGraph()
{
if (string.IsNullOrWhiteSpace(currentGraphGUID))
{
currentGraphGUID = Guid.NewGuid().ToString();
}

//Unloads the graph before the assembly reloads.
//This is important, otherwise unity editor will soft-lock, presumably due to
//graph view no longer existing but still being referenced.
Expand Down
7 changes: 5 additions & 2 deletions EditorOnly/DataModels/GraphModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ public class GraphModel : ScriptableObject
{
[SerializeField]
public SerializableType graphWindowType;

[SerializeField]
public Vector3 viewPosition = Vector3.zero;
[SerializeField]
public Vector3 viewZoom = Vector3.one;
[SerializeReference]
protected internal List<NodeModel> nodeModels = new List<NodeModel>();
[SerializeReference]
Expand All @@ -21,7 +24,7 @@ public class GraphModel : ScriptableObject
protected internal List<EdgeModel> edgeModels = new List<EdgeModel>();
[SerializeReference]
protected internal GraphController serializedGraphController;
[SerializeReference]
[SerializeReference]
protected internal NodeModel rootNodeModel;

/// <summary>
Expand Down
69 changes: 50 additions & 19 deletions EditorOnly/DataModels/NodeModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public void CreateRuntimeDataClone(GraphModel graphModel, RuntimeNode toCopy)

#region Ports

protected internal void CreatePortModel(FieldInfo field, Direction dir)
protected internal void CreatePortModel(FieldInfo field, Direction dir, Port.Capacity cap)
{
Action<PortModel> portCreationAction;
switch (dir)
Expand All @@ -96,53 +96,84 @@ protected internal void CreatePortModel(FieldInfo field, Direction dir)
var portValueType = field.FieldType.
GetGenericClassConstructorArguments(typeof(ValuePort<>));
var pm = new PortModel(Orientation.Horizontal, dir,
Port.Capacity.Multi, portValueType.FirstOrDefault(), field);
cap, portValueType.FirstOrDefault(), field);
portCreationAction.Invoke(pm);
}

//(typeof(RuntimeData), typeof(DirectionAttribute))
//Reflection data cache for (node, dir), List of ports to generate.
private static readonly Dictionary<(Type, Type), List<FieldInfo>> dataTypeToInfoFields
= new Dictionary<(Type, Type), List<FieldInfo>>();
//Reflection data cache for (node, dir), List of PortInfo and list of "PortMetadata"
private static readonly Dictionary<(Type, Type), PortInfoAndMetadata> dataTypeToInfoFields
= new Dictionary<(Type, Type), PortInfoAndMetadata>();

private readonly struct PortInfoAndMetadata
{
public readonly List<FieldInfo> fieldInfo;
public readonly List<Port.Capacity> caps;

public PortInfoAndMetadata(List<FieldInfo> fieldInfo, List<Port.Capacity> caps)
{
this.fieldInfo = fieldInfo;
this.caps = caps;
}
}

/// <summary>
/// Cached wrapper around GetLocalFieldWithAttribute
/// Scans the attribute for our ports and returns the field and metadata.
/// </summary>
private List<FieldInfo> GetFieldInfoFor<Attr>(Type runtimeDataType)
private PortInfoAndMetadata GetFieldInfoFor<Attr>(Type runtimeDataType)
where Attr : Attribute
{
var index = (runtimeDataType, typeof(Attr));
if (dataTypeToInfoFields.TryGetValue(index, out var fields))
if (dataTypeToInfoFields.TryGetValue(index, out var info))
{
return fields;
return info;
}

fields = runtimeDataType.GetLocalFieldsWithAttribute<Attr>();
dataTypeToInfoFields.Add(index, fields);
return fields;
var fields = runtimeDataType.GetLocalFieldsWithAttribute<Attr>(out var attribs);
List<Port.Capacity> caps = new List<Port.Capacity>();
foreach (var attr in attribs)
{
if (attr is In inAttr)
{
caps.Add(inAttr.capacity);
}
if (attr is Out outAttr)
{
caps.Add(outAttr.capacity);
}
}

PortInfoAndMetadata iacp = new PortInfoAndMetadata(fields, caps);
dataTypeToInfoFields.Add(index, iacp);
return iacp;
}

/// <summary>
/// Analyses the reflection data and creates the appropriate ports based on it automagically.
/// </summary>
/// <param name="clearCopy">Copied models should use true, clears all port links if true.</param>
protected internal void CreatePortModelsFromReflection(bool clearCopy = false)
{
var oFields = GetFieldInfoFor<Out>(RuntimeData.GetType());
var iFields = GetFieldInfoFor<In>(RuntimeData.GetType());
var oFieldAndCaps = GetFieldInfoFor<Out>(RuntimeData.GetType());
var iFieldAndCaps = GetFieldInfoFor<In>(RuntimeData.GetType());

foreach (var field in iFields)
for (int i = 0; i < iFieldAndCaps.fieldInfo.Count; i++)
{
CreatePortModel(field, Direction.Input);
var field = iFieldAndCaps.fieldInfo[i];
var cap = iFieldAndCaps.caps[i];
CreatePortModel(field, Direction.Input, cap);
if (!clearCopy) continue;
if (field.GetValue(RuntimeData) is ValuePort vp)
{
vp.links.Clear();
}
}
foreach (var field in oFields)

for (int i = 0; i < oFieldAndCaps.fieldInfo.Count; i++)
{
CreatePortModel(field, Direction.Output);
var field = oFieldAndCaps.fieldInfo[i];
var cap = oFieldAndCaps.caps[i];
CreatePortModel(field, Direction.Output, cap);
if (!clearCopy) continue;
if (field.GetValue(RuntimeData) is ValuePort vp)
{
Expand Down
4 changes: 3 additions & 1 deletion EditorOnly/Extensions/ReflectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@ namespace GraphFramework.Editor
{
public static class ReflectionExtensions
{
public static List<FieldInfo> GetLocalFieldsWithAttribute<TargetAttr>(this Type t)
public static List<FieldInfo> GetLocalFieldsWithAttribute<TargetAttr>(this Type t, out List<Attribute> fieldAttribs)
where TargetAttr : Attribute
{
var allFields = t.GetFields(BindingFlags.Public |
BindingFlags.Instance | BindingFlags.NonPublic);

var fieldList = new List<FieldInfo>();
fieldAttribs = new List<Attribute>();
foreach (var f in allFields)
{
var attribs = f.GetCustomAttributes();
Expand All @@ -22,6 +23,7 @@ public static List<FieldInfo> GetLocalFieldsWithAttribute<TargetAttr>(this Type
if (attr.GetType() != typeof(TargetAttr))
continue;
fieldList.Add(f);
fieldAttribs.Add(attr);
break;
}
}
Expand Down
8 changes: 7 additions & 1 deletion Runtime/Attributes/In.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using UnityEngine.Scripting;
using UnityEditor.Experimental.GraphView;
using UnityEngine.Scripting;

namespace GraphFramework.Attributes
{
Expand All @@ -8,5 +9,10 @@ namespace GraphFramework.Attributes
/// </summary>
public class In : PreserveAttribute
{
public readonly Port.Capacity capacity;
public In(Port.Capacity portCapacity = Port.Capacity.Single)
{
capacity = portCapacity;
}
}
}
8 changes: 7 additions & 1 deletion Runtime/Attributes/Out.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using UnityEngine.Scripting;
using UnityEditor.Experimental.GraphView;
using UnityEngine.Scripting;

namespace GraphFramework.Attributes
{
Expand All @@ -8,5 +9,10 @@ namespace GraphFramework.Attributes
/// </summary>
public class Out : PreserveAttribute
{
public readonly Port.Capacity capacity;
public Out(Port.Capacity portCapacity = Port.Capacity.Single)
{
capacity = portCapacity;
}
}
}
2 changes: 1 addition & 1 deletion Runtime/GraphController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public abstract class GraphController : ScriptableObject
protected internal List<Link> links = new List<Link>();
[NonSerialized]
private int currentVirtualGraphIndex = int.MinValue;

public VirtualGraph CreateVirtualGraph()
{
VirtualGraph vg = new VirtualGraph(this, currentVirtualGraphIndex++);
Expand Down
12 changes: 4 additions & 8 deletions Runtime/NodeIO/Link.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ internal ValuePort Bind()
public class Link
{
[SerializeReference]
public RuntimeNode linkedTo;
private RuntimeNode linkedTo;
[SerializeReference]
private LinkBinder remoteLinkBinder;
//This is only used by the editor, but is quite difficult to factor out and only saves
Expand Down Expand Up @@ -88,7 +88,7 @@ private void BindRemote()
/// of the node
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void CreateVirtualizedLinks(int graphId)
internal void CreateVirtualizedLinks(int graphId)
{
BindRemote();
Reset(graphId);
Expand Down Expand Up @@ -189,12 +189,8 @@ public T GetValueAs<T>()
}

/// <summary>
/// Returns the node this link is connected to.
/// The node this link is connected to.
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public RuntimeNode GetNode()
{
return linkedTo;
}
public RuntimeNode Node => linkedTo;
}
}
22 changes: 2 additions & 20 deletions Runtime/NodeIO/ValuePort.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public abstract class ValuePort
/// <summary>
/// The list of links this port has.
/// </summary>
public IEnumerable<Link> Links => links;
public List<Link> Links => links;

/// <summary>
/// Resets the value of a virtual port back to it's original value.
Expand Down Expand Up @@ -92,7 +92,7 @@ public T FirstValue()
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public RuntimeNode FirstNode()
{
return links.Count > 0 ? links[0].GetNode() : null;
return links.Count > 0 ? links[0].Node : null;
}

/// <summary>
Expand All @@ -103,23 +103,5 @@ public Link FirstLink()
{
return links.Count > 0 ? links[0] : null;
}

/// <summary>
/// Returns the value of the link at the given index or default.
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public T ValueOf(int index)
{
return links.Count > index ? links[index].GetValueAs<T>() : default;
}

/// <summary>
/// Returns the linked node at the given index or null.
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public RuntimeNode NodeOf(int index)
{
return links.Count > index ? links[index].GetNode() : null;
}
}
}

0 comments on commit 93ce3b5

Please sign in to comment.