Skip to content
This repository has been archived by the owner on Oct 31, 2023. It is now read-only.

Commit

Permalink
MERGED MASTER CHANGES
Browse files Browse the repository at this point in the history
- Clean API for ITypeDeclaration (now IConnectionTypeDeclaration!)
- Removed redundant lines of code
- Added ConnectionTypes.GetTypeData (Type)
- Improved ConnectionTypes handling

Additionally, Calculation is now optional (virtual) and can be overridden only if needed!

NOTE: This is a preparation to merge develop into master. Planned with a test period of 1-4 days to make sure if works! But develop is currently actually much more stable than master.
  • Loading branch information
Seneral committed Jun 15, 2016
1 parent da3d8b9 commit 2b05064
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 67 deletions.
86 changes: 52 additions & 34 deletions Node_Editor/Framework/ConnectionTypes.cs
Expand Up @@ -22,15 +22,15 @@ public static class ConnectionTypes
private static Dictionary<string, TypeData> types;

/// <summary>
/// Gets the Type the specified type name representates, if declared
/// Gets the Type the specified type name representates or creates it if not defined
/// </summary>
public static Type GetType (string typeName)
{
return GetTypeData (typeName).Type ?? NullType;
}

/// <summary>
/// Gets the type data for the specified type name, if declared
/// Gets the type data for the specified type name or creates it if not defined
/// </summary>
public static TypeData GetTypeData (string typeName)
{
Expand All @@ -47,96 +47,114 @@ public static TypeData GetTypeData (string typeName)
}
else
{
//typeData = types.Values.First ((TypeData data) => data.isValid () && data.Type == type);
typeData = types.Values.ToList ().Find ((TypeData data) => data.isValid () && data.Type == type);
typeData = types.Values.Count <= 0? null : types.Values.First ((TypeData data) => data.isValid () && data.Type == type);
if (typeData == null)
types.Add (typeName, typeData = new TypeData (type));
}
}
return typeData;
}

/// <summary>
/// Gets the type data for the specified type or creates it if not defined
/// </summary>
public static TypeData GetTypeData (Type type)
{
if (types == null || types.Count == 0)
FetchTypes ();
TypeData typeData = types.Values.Count <= 0? null : types.Values.First ((TypeData data) => data.isValid () && data.Type == type);
if (typeData == null)
types.Add (type.Name, typeData = new TypeData (type));
return typeData;
}

/// <summary>
/// Fetches every Type Declaration in the script assemblies and the executing one, if the NodeEditor is packed into a .dll
/// </summary>
internal static void FetchTypes ()
{
types = new Dictionary<string, TypeData> ();
types = new Dictionary<string, TypeData> { { "None", new TypeData (typeof(System.Object)) } };

IEnumerable<Assembly> scriptAssemblies = AppDomain.CurrentDomain.GetAssemblies ().Where ((Assembly assembly) => assembly.FullName.Contains ("Assembly"));
foreach (Assembly assembly in scriptAssemblies)
{
foreach (Type type in assembly.GetTypes ().Where (T => T.IsClass && !T.IsAbstract && T.GetInterfaces ().Contains (typeof (ITypeDeclaration))))
foreach (Type type in assembly.GetTypes ().Where (T => T.IsClass && !T.IsAbstract && T.GetInterfaces ().Contains (typeof (IConnectionTypeDeclaration))))
{
ITypeDeclaration typeDecl = assembly.CreateInstance (type.FullName) as ITypeDeclaration;
IConnectionTypeDeclaration typeDecl = assembly.CreateInstance (type.FullName) as IConnectionTypeDeclaration;
if (typeDecl == null)
throw new UnityException ("Error with Type Declaration " + type.FullName);
types.Add (typeDecl.name, new TypeData (typeDecl));
types.Add (typeDecl.Identifier, new TypeData (typeDecl));
}
}
}
}

public class TypeData
{
public ITypeDeclaration declaration;
public Type Type;
public Color col;
public Texture2D InputKnob;
public Texture2D OutputKnob;
private IConnectionTypeDeclaration declaration;
public string Identifier { get; private set; }
public Type Type { get; private set; }
public Color Color { get; private set; }
public Texture2D InKnobTex { get; private set; }
public Texture2D OutKnobTex { get; private set; }

public TypeData (ITypeDeclaration typeDecl)
internal TypeData (IConnectionTypeDeclaration typeDecl)
{
Identifier = typeDecl.Identifier;
declaration = typeDecl;
Type = declaration.Type;
col = declaration.col;
Color = declaration.Color;

InputKnob = ResourceManager.GetTintedTexture (declaration.InputKnob_TexPath, col);
OutputKnob = ResourceManager.GetTintedTexture (declaration.OutputKnob_TexPath, col);
InKnobTex = ResourceManager.GetTintedTexture (declaration.InKnobTex, Color);
OutKnobTex = ResourceManager.GetTintedTexture (declaration.OutKnobTex, Color);

if (!isValid ())
throw new DataMisalignedException ("Type Declaration " + typeDecl.name + " contains invalid data!");
throw new DataMisalignedException ("Type Declaration " + typeDecl.Identifier + " contains invalid data!");
}

public TypeData (Type type)
{
Identifier = type.Name;
declaration = null;
Type = type;
col = Color.white;//(float)type.GetHashCode() / (int.MaxValue/3);
Color = Color.white;//(float)type.GetHashCode() / (int.MaxValue/3);

// TODO: Experimental: Create colors randomly from hashcode of type
// right now everything is roughly the same color unfortunately

// int - 3x float
// int -> 3x float
int srcInt = type.GetHashCode ();
byte[] bytes = BitConverter.GetBytes (srcInt);
//Debug.Log ("hash " + srcInt + " from type " + type.FullName + " has byte count of " + bytes.Length);
col = new Color (Mathf.Pow (((float)bytes[0])/255, 0.5f), Mathf.Pow (((float)bytes[1])/255, 0.5f), Mathf.Pow (((float)bytes[2])/255, 0.5f));
Color = new Color (Mathf.Pow (((float)bytes[0])/255, 0.5f), Mathf.Pow (((float)bytes[1])/255, 0.5f), Mathf.Pow (((float)bytes[2])/255, 0.5f));
//Debug.Log ("Color " + col.ToString ());

InputKnob = ResourceManager.GetTintedTexture ("Textures/In_Knob.png", col);
OutputKnob = ResourceManager.GetTintedTexture ("Textures/Out_Knob.png", col);
InKnobTex = ResourceManager.GetTintedTexture ("Textures/In_Knob.png", Color);
OutKnobTex = ResourceManager.GetTintedTexture ("Textures/Out_Knob.png", Color);
}

public bool isValid ()
{
return Type != null && InputKnob != null && OutputKnob != null;
return Type != null && InKnobTex != null && OutKnobTex != null;
}
}

public interface ITypeDeclaration
public interface IConnectionTypeDeclaration
{
string name { get; }
Color col { get; }
string InputKnob_TexPath { get; }
string OutputKnob_TexPath { get; }
string Identifier { get; }
Type Type { get; }
Color Color { get; }
string InKnobTex { get; }
string OutKnobTex { get; }
}

// TODO: Node Editor: Built-In Connection Types
public class FloatType : ITypeDeclaration
public class FloatType : IConnectionTypeDeclaration
{
public string name { get { return "Float"; } }
public Color col { get { return Color.cyan; } }
public string InputKnob_TexPath { get { return "Textures/In_Knob.png"; } }
public string OutputKnob_TexPath { get { return "Textures/Out_Knob.png"; } }
public string Identifier { get { return "Float"; } }
public Type Type { get { return typeof(float); } }
public Color Color { get { return Color.cyan; } }
public string InKnobTex { get { return "Textures/In_Knob.png"; } }
public string OutKnobTex { get { return "Textures/Out_Knob.png"; } }
}
}
41 changes: 16 additions & 25 deletions Node_Editor/Framework/Node.cs
Expand Up @@ -121,9 +121,9 @@ internal void CheckNodeKnobMigration ()

#endregion

#region Undeclared Members
#region Dynamic Members

#region Node Type methods (abstract)
#region Node Type Methods

/// <summary>
/// Get the ID of the Node
Expand All @@ -139,13 +139,19 @@ internal void CheckNodeKnobMigration ()
/// Draw the Node immediately
/// </summary>
protected internal abstract void NodeGUI ();

/// <summary>
/// Used to display a custom node property editor in the side window of the NodeEditorWindow
/// Optionally override this to implement
/// </summary>
public virtual void DrawNodePropertyEditor () { }

/// <summary>
/// Calculate the outputs of this Node
/// Return Success/Fail
/// Might be dependant on previous nodes
/// </summary>
public abstract bool Calculate ();
public virtual bool Calculate () { return true; }

#endregion

Expand Down Expand Up @@ -261,17 +267,11 @@ protected internal virtual void DrawConnections ()
startDir,
input.GetGUIKnob ().center,
input.GetDirection (),
output.typeData.col);
output.typeData.Color);
}
}
}

/// <summary>
/// Used to display a custom node property editor in the side window of the NodeEditorWindow
/// Optionally override this to implement
/// </summary>
public virtual void DrawNodePropertyEditor() { }

#endregion

#region Calculation Utility
Expand Down Expand Up @@ -476,13 +476,10 @@ internal bool isInLoop ()
for (int cnt = 0; cnt < Inputs.Count; cnt++)
{
NodeOutput connection = Inputs [cnt].connection;
if (connection != null)
if (connection != null && connection.body.isInLoop ())
{
if (connection.body.isInLoop ())
{
StopRecursiveSearchLoop ();
return true;
}
StopRecursiveSearchLoop ();
return true;
}
}
EndRecursiveSearchLoop ();
Expand All @@ -504,16 +501,10 @@ internal bool allowsLoopRecursion (Node otherNode)
for (int cnt = 0; cnt < Inputs.Count; cnt++)
{
NodeOutput connection = Inputs [cnt].connection;
if (connection != null)
if (connection != null && connection.body.allowsLoopRecursion (otherNode))
{
if (connection.body != startRecursiveSearchNode)
{
if (connection.body.allowsLoopRecursion (otherNode))
{
StopRecursiveSearchLoop ();
return true;
}
}
StopRecursiveSearchLoop ();
return true;
}
}
EndRecursiveSearchLoop ();
Expand Down
2 changes: 1 addition & 1 deletion Node_Editor/Framework/NodeEditor.cs
Expand Up @@ -186,7 +186,7 @@ private static void DrawSubCanvas (NodeCanvas nodeCanvas, NodeEditorState editor
Vector2 endPos = Event.current.mousePosition;
// There is no specific direction of the end knob so we pick the best according to the relative position
Vector2 endDir = NodeEditorGUI.GetSecondConnectionVector (startPos, endPos, startDir);
NodeEditorGUI.DrawConnection (startPos, startDir, endPos, endDir, output.typeData.col);
NodeEditorGUI.DrawConnection (startPos, startDir, endPos, endDir, output.typeData.Color);
RepaintClients ();
}

Expand Down
2 changes: 1 addition & 1 deletion Node_Editor/Framework/NodeInput.cs
Expand Up @@ -78,7 +78,7 @@ protected internal override void CopyScriptableObjects (System.Func<ScriptableOb
protected override void ReloadTexture ()
{
CheckType ();
knobTexture = typeData.InputKnob;
knobTexture = typeData.InKnobTex;
}

private void CheckType ()
Expand Down
2 changes: 1 addition & 1 deletion Node_Editor/Framework/NodeOutput.cs
Expand Up @@ -81,7 +81,7 @@ protected internal override void CopyScriptableObjects (System.Func<ScriptableOb
protected override void ReloadTexture ()
{
CheckType ();
knobTexture = typeData.OutputKnob;
knobTexture = typeData.OutKnobTex;
}

private void CheckType ()
Expand Down
10 changes: 5 additions & 5 deletions Node_Editor/Nodes/Example/FlowNode.cs
Expand Up @@ -66,12 +66,12 @@ public override bool Calculate ()
}

// Connection Type only for visual purposes
public class FlowType : ITypeDeclaration
public class FlowType : IConnectionTypeDeclaration
{
public string name { get { return "Flow"; } }
public Color col { get { return Color.red; } }
public string InputKnob_TexPath { get { return "Textures/In_Knob.png"; } }
public string OutputKnob_TexPath { get { return "Textures/Out_Knob.png"; } }
public string Identifier { get { return "Flow"; } }
public Type Type { get { return typeof(void); } }
public Color Color { get { return Color.red; } }
public string InKnobTex { get { return "Textures/In_Knob.png"; } }
public string OutKnobTex { get { return "Textures/Out_Knob.png"; } }
}
}

0 comments on commit 2b05064

Please sign in to comment.