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

Commit

Permalink
First clean release with all WIP stuff removed and structure improved
Browse files Browse the repository at this point in the history
-> Action Node, UnityFunc and State system removed
-> removed redundancies
-> improved structure of files and nodes
-> Fixed bugs regarding for loops replaced with foreach loops
  • Loading branch information
Seneral committed Feb 24, 2016
1 parent 7f8699a commit 70a06e8
Show file tree
Hide file tree
Showing 24 changed files with 767 additions and 489 deletions.
47 changes: 2 additions & 45 deletions Editor/Node_Editor/NodeEditorWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,20 +64,14 @@ public static bool AutoOpenCanvas (int instanceID, int line)
public void OnDestroy ()
{
NodeEditor.ClientRepaints -= _editor.Repaint;
//SaveCache ();

#if UNITY_EDITOR
// Remove callbacks
//EditorLoadingControl.beforeEnteringPlayMode -= SaveCache;
EditorLoadingControl.lateEnteredPlayMode -= LoadCache;
//EditorLoadingControl.beforeLeavingPlayMode -= SaveCache;
EditorLoadingControl.justLeftPlayMode -= LoadCache;
EditorLoadingControl.justOpenedNewScene -= LoadCache;

NodeEditorCallbacks.OnAddNode -= SaveNewNode;
NodeEditorCallbacks.OnAddTransition -= SaveNewTransition;

// TODO: BeforeOpenedScene to save Cache, aswell as assembly reloads...
#endif
}

Expand All @@ -90,13 +84,9 @@ private void OnEnable ()

#if UNITY_EDITOR
// This makes sure the Node Editor is reinitiated after the Playmode changed
//EditorLoadingControl.beforeEnteringPlayMode -= SaveCache;
//EditorLoadingControl.beforeEnteringPlayMode += SaveCache;
EditorLoadingControl.lateEnteredPlayMode -= LoadCache;
EditorLoadingControl.lateEnteredPlayMode += LoadCache;

//EditorLoadingControl.beforeLeavingPlayMode -= SaveCache;
//EditorLoadingControl.beforeLeavingPlayMode += SaveCache;
EditorLoadingControl.justLeftPlayMode -= LoadCache;
EditorLoadingControl.justLeftPlayMode += LoadCache;

Expand All @@ -105,10 +95,6 @@ private void OnEnable ()

NodeEditorCallbacks.OnAddNode -= SaveNewNode;
NodeEditorCallbacks.OnAddNode += SaveNewNode;
NodeEditorCallbacks.OnAddTransition -= SaveNewTransition;
NodeEditorCallbacks.OnAddTransition += SaveNewTransition;

// TODO: BeforeOpenedScene to save Cache, aswell as assembly reloads...
#endif
}

Expand Down Expand Up @@ -192,9 +178,6 @@ private void DrawSideWindow ()
if (GUILayout.Button ("Force Re-Init"))
NodeEditor.ReInit (true);

if (NodeEditor.isTransitioning (mainNodeCanvas) && GUILayout.Button ("Stop Transitioning"))
NodeEditor.StopTransitioning (mainNodeCanvas);

NodeEditorGUI.knobSize = EditorGUILayout.IntSlider (new GUIContent ("Handle Size", "The size of the Node Input/Output handles"), NodeEditorGUI.knobSize, 12, 20);
mainEditorState.zoom = EditorGUILayout.Slider (new GUIContent ("Zoom", "Use the Mousewheel. Seriously."), mainEditorState.zoom, 0.6f, 2);

Expand All @@ -215,34 +198,15 @@ private void SaveNewNode (Node node)
if (AssetDatabase.GetAssetPath (mainNodeCanvas) != path)
throw new UnityException ("Cache system error: Current Canvas is not saved as the temporary cache!");
NodeEditorSaveManager.AddSubAsset (node, path);
for (int knobCnt = 0; knobCnt < node.nodeKnobs.Count; knobCnt++)
NodeEditorSaveManager.AddSubAsset (node.nodeKnobs [knobCnt], path);
for (int transCnt = 0; transCnt < node.transitions.Count; transCnt++)
{
if (node.transitions[transCnt].startNode == node)
NodeEditorSaveManager.AddSubAsset (node.transitions [transCnt], path);
}

AssetDatabase.SaveAssets ();
AssetDatabase.Refresh ();
}

private void SaveNewTransition (Transition transition)
{
if (!mainNodeCanvas.nodes.Contains (transition.startNode) || !mainNodeCanvas.nodes.Contains (transition.endNode))
throw new UnityException ("Cache system: Writing new Transition to save file failed as Node members are not part of the Cache!");
string path = tempSessionPath + "/LastSession.asset";
if (AssetDatabase.GetAssetPath (mainNodeCanvas) != path)
throw new UnityException ("Cache system error: Current Canvas is not saved as the temporary cache!");
NodeEditorSaveManager.AddSubAsset (transition, path);
foreach (NodeKnob knob in node.nodeKnobs)
NodeEditorSaveManager.AddSubAsset (knob, path);

AssetDatabase.SaveAssets ();
AssetDatabase.Refresh ();
}

private void SaveCache ()
{
//DeleteCache (); // Delete old cache
string canvasName = mainNodeCanvas.name;
EditorPrefs.SetString ("NodeEditorLastSession", canvasName);
NodeEditorSaveManager.SaveNodeCanvas (tempSessionPath + "/LastSession.asset", false, mainNodeCanvas, mainEditorState);
Expand Down Expand Up @@ -296,7 +260,6 @@ private void DeleteCache ()
public void SaveNodeCanvas (string path)
{
NodeEditorSaveManager.SaveNodeCanvas (path, true, mainNodeCanvas, mainEditorState);
//SaveCache ();
Repaint ();
}

Expand All @@ -305,9 +268,6 @@ public void SaveNodeCanvas (string path)
/// </summary>
public void LoadNodeCanvas (string path)
{
// Else it will be stuck forever
NodeEditor.StopTransitioning (mainNodeCanvas);

// Load the NodeCanvas
mainNodeCanvas = NodeEditorSaveManager.LoadNodeCanvas (path, true);
if (mainNodeCanvas == null)
Expand Down Expand Up @@ -342,9 +302,6 @@ public void LoadNodeCanvas (string path)
/// </summary>
public void NewNodeCanvas ()
{
// Else it will be stuck forever
NodeEditor.StopTransitioning (mainNodeCanvas);

// New NodeCanvas
mainNodeCanvas = CreateInstance<NodeCanvas> ();
mainNodeCanvas.name = "New Canvas";
Expand Down
149 changes: 109 additions & 40 deletions Node_Editor/Framework/ConnectionTypes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,30 +21,67 @@ 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 identifier representates or, if not declared and checked, creates a new type data for the passed type
/// </summary>
public static Type GetType (string typeName)
public static Type GetType (string typeName, bool createIfNotDeclared)
{
return GetTypeData (typeName).Type ?? NullType;
TypeData data = GetTypeData (typeName, createIfNotDeclared);
return data != null? data.Type : NullType;
}

/// <summary>
/// Gets the type data for the specified type name, if declared
/// Gets the type data with the specified identifier or, if not declared and checked, creates a new one when a valid type name with namespace is passed
/// </summary>
public static TypeData GetTypeData (string typeName)
public static TypeData GetTypeData (string typeName, bool createIfNotDeclared)
{
if (types == null || types.Count == 0)
NodeEditor.ReInit (false);
TypeData typeData;
if (!types.TryGetValue (typeName, out typeData))
{
Debug.LogError ("No TypeData defined for: " + typeName);
typeData = types.First ().Value;
if (createIfNotDeclared)
{
Type type = Type.GetType (typeName);
if (type == null)
{
typeData = types.First ().Value;
Debug.LogError ("No TypeData defined for: " + typeName + " and type could not be found either!");
}
else
{
typeData = new TypeData (type);
types.Add (typeName, typeData);
}
}
else
{
typeData = types.First ().Value;
Debug.LogError ("No TypeData defined for: " + typeName + "!");
}
}
if (typeData.declaration == null || typeData.InputKnob == null || typeData.OutputKnob == null)
{
return typeData;
}

/// <summary>
/// Gets the type data for the specified type or, if not declared and checked, creates a new one for that type
/// </summary>
public static TypeData GetTypeData (Type type, bool createIfNotDeclared)
{
if (types == null || types.Count == 0)
NodeEditor.ReInit (false);
typeData = GetTypeData (typeName);
TypeData typeData = types.Values.First ((TypeData tData) => tData.Type == type);
if (typeData == null)
{
if (createIfNotDeclared)
{
typeData = new TypeData (type);
types.Add (type.FullName, typeData);
}
else
{
typeData = types.First ().Value;
Debug.LogError ("No TypeData defined for: " + type.FullName + "!");
}
}
return typeData;
}
Expand All @@ -54,60 +91,92 @@ public static TypeData GetTypeData (string typeName)
/// </summary>
internal static void FetchTypes ()
{
types = new Dictionary<string, TypeData> ();
types = new Dictionary<string, TypeData> { { "None", new TypeData () } };

List<Assembly> scriptAssemblies = AppDomain.CurrentDomain.GetAssemblies ().Where ((Assembly assembly) => assembly.FullName.Contains ("Assembly")).ToList ();
if (!scriptAssemblies.Contains (Assembly.GetExecutingAssembly ()))
scriptAssemblies.Add (Assembly.GetExecutingAssembly ());
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))))
{
ITypeDeclaration typeDecl = assembly.CreateInstance (type.FullName) as ITypeDeclaration;
{ // Iterate through each script assembly
IEnumerable<Type> typeDeclarations = assembly.GetTypes ().Where (T => T.IsClass && !T.IsAbstract && T.GetInterface (typeof (IConnectionTypeDeclaration).FullName) != null);
foreach (Type type in typeDeclarations)
{ // get all type declarations and create a typeData for them
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 struct TypeData
public class TypeData
{
public ITypeDeclaration declaration;
public Type Type;
public Color col;
public Texture2D InputKnob;
public Texture2D OutputKnob;
public TypeData (ITypeDeclaration typeDecl)
private IConnectionTypeDeclaration declaration;
public Type Type { get; private set; }
public Color Color { get; private set; }
public Texture2D InKnobTex { get; private set; }
public Texture2D OutKnobTex { get; private set; }

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

InKnobTex = ResourceManager.GetTintedTexture (declaration.InKnobTex, Color);
OutKnobTex = ResourceManager.GetTintedTexture (declaration.OutKnobTex, Color);

if (InKnobTex == null || InKnobTex == null)
throw new UnityException ("Invalid textures for default typeData " + declaration.Identifier + "!");
}

internal TypeData (Type type)
{
declaration = null;
Type = type;
Color = Color.white;//(float)type.GetHashCode() / (int.MaxValue/3);

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

if (InKnobTex == null || InKnobTex == null)
throw new UnityException ("Invalid textures for default typeData " + type.ToString () + "!");
}

internal TypeData ()
{
declaration = null;
Type = typeof(object);
Color = Color.white;
InKnobTex = ResourceManager.LoadTexture ("Textures/In_Knob.png");
OutKnobTex = ResourceManager.LoadTexture ("Textures/Out_Knob.png");

if (InKnobTex == null || InKnobTex == null)
throw new UnityException ("Invalid textures for default typeData!");
}

public bool isValid ()
{
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"; } }
}


Expand Down
Loading

0 comments on commit 70a06e8

Please sign in to comment.