Navigation Menu

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

Commit

Permalink
Added StateMachine feature branch.
Browse files Browse the repository at this point in the history
UnityFunc is intended to be used as a transition condition when I get it working (This will be updated along with the UnityFunc feature branch!), it currently throws odd errors.
  • Loading branch information
Seneral committed Mar 1, 2016
1 parent f1fcb30 commit 4883bec
Show file tree
Hide file tree
Showing 21 changed files with 1,593 additions and 27 deletions.
30 changes: 30 additions & 0 deletions Editor/Node_Editor/NodeEditorWindow.cs
Expand Up @@ -76,6 +76,7 @@ private void OnDestroy ()

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

// TODO: BeforeOpenedScene to save Cache, aswell as assembly reloads...
#endif
Expand Down Expand Up @@ -107,6 +108,8 @@ private void OnEnable ()
NodeEditorCallbacks.OnAddNode += SaveNewNode;
NodeEditorCallbacks.OnAddNodeKnob -= SaveNewNodeKnob;
NodeEditorCallbacks.OnAddNodeKnob += SaveNewNodeKnob;
NodeEditorCallbacks.OnAddTransition -= SaveNewTransition;
NodeEditorCallbacks.OnAddTransition += SaveNewTransition;

// TODO: BeforeOpenedScene to save Cache, aswell as assembly reloads...
#endif
Expand Down Expand Up @@ -192,6 +195,9 @@ 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 Down Expand Up @@ -222,6 +228,12 @@ private void SaveNewNode (Node node)
NodeEditorSaveManager.AddSubAsset (so, knob);
}

foreach (Transition trans in node.transitions)
{
if (trans.startNode == node)
NodeEditorSaveManager.AddSubAsset (trans, node);
}

AssetDatabase.SaveAssets ();
AssetDatabase.Refresh ();
}
Expand All @@ -233,6 +245,18 @@ private void SaveNewNodeKnob (NodeKnob knob)
NodeEditorSaveManager.AddSubAsset (so, knob);
}

private void SaveNewTransition (Transition transition)
{
CheckCurrentCache ();
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!");

NodeEditorSaveManager.AddSubAsset (transition, lastSessionPath);

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

private void SaveCache ()
{
string canvasName = mainNodeCanvas.name;
Expand Down Expand Up @@ -302,6 +326,9 @@ 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 @@ -341,6 +368,9 @@ 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
10 changes: 10 additions & 0 deletions Node_Editor/Framework/ConnectionTypes.cs
Expand Up @@ -137,4 +137,14 @@ public class FloatType : ITypeDeclaration
public string OutputKnob_TexPath { get { return "Textures/Out_Knob.png"; } }
public Type Type { get { return typeof(float); } }
}

// TODO: Node Editor: Built-In Connection Types
public class ObjectType : ITypeDeclaration
{
public string name { get { return "Object"; } }
public Color col { get { return Color.green; } }
public string InputKnob_TexPath { get { return "Textures/In_Knob.png"; } }
public string OutputKnob_TexPath { get { return "Textures/Out_Knob.png"; } }
public Type Type { get { return typeof(object); } }
}
}
45 changes: 45 additions & 0 deletions Node_Editor/Framework/Node.cs
Expand Up @@ -25,6 +25,9 @@ public abstract class Node : ScriptableObject
[HideInInspector]
[NonSerialized]
internal bool calculated = true;

// State graph
public List<Transition> transitions = new List<Transition> ();

#region General

Expand Down Expand Up @@ -70,6 +73,10 @@ public void Delete ()
if (nodeKnobs[knobCnt] != null)
DestroyImmediate (nodeKnobs[knobCnt], true);
}
for (int transCnt = 0; transCnt < transitions.Count; transCnt++)
{
transitions [transCnt].Delete ();
}
DestroyImmediate (this, true);
}

Expand Down Expand Up @@ -140,6 +147,11 @@ internal void CheckNodeKnobMigration ()
/// </summary>
public virtual bool ContinueCalculation { get { return true; } }

/// <summary>
/// Does this Node accepts Transitions?
/// </summary>
public virtual bool AcceptsTranstitions { get { return false; } }

#endregion

#region Protected Callbacks
Expand All @@ -159,6 +171,23 @@ internal void CheckNodeKnobMigration ()
/// </summary>
protected internal virtual void OnAddOutputConnection (NodeOutput output) {}

/// <summary>
/// Callback when the Transition was created
/// </summary>
protected internal virtual void OnAddTransition (Transition transition) {}


/// <summary>
/// Callback when the this Node is being transitioned to.
/// OriginTransition is the transition from which was transitioned to this node OR null if the transitioning process was started on this Node
/// </summary>
protected internal virtual void OnEnter (Transition originTransition) {}

/// <summary>
/// Callback when the this Node is transitioning to another Node through the passed Transition
/// </summary>
protected internal virtual void OnLeave (Transition transition) {}

#endregion

#region Additional Serialization
Expand Down Expand Up @@ -192,6 +221,10 @@ protected internal virtual void DrawNode ()
nodeRect.position += NodeEditor.curEditorState.zoomPanAdjust;
contentOffset = new Vector2 (0, 20);

// Mark the current transitioning node as such by outlining it
if (NodeEditor.curNodeCanvas.currentNode == this)
GUI.DrawTexture (new Rect (nodeRect.x-8, nodeRect.y-8, nodeRect.width+16, nodeRect.height+16), NodeEditorGUI.GUIBoxSelection);

// Create a headerRect out of the previous rect and draw it, marking the selected node as such by making the header bold
Rect headerRect = new Rect (nodeRect.x, nodeRect.y, nodeRect.width, contentOffset.y);
GUI.Label (headerRect, name, NodeEditor.curEditorState.selectedNode == this? NodeEditorGUI.nodeBoxBold : NodeEditorGUI.nodeBox);
Expand Down Expand Up @@ -244,6 +277,18 @@ protected internal virtual void DrawConnections ()
}
}
}

/// <summary>
/// Draws the node transitions starting from this node
/// </summary>
public void DrawTransitions ()
{
for (int cnt = 0; cnt < transitions.Count; cnt++)
{
if (transitions[cnt].startNode == this)
transitions[cnt].DrawFromStartNode ();
}
}

/// <summary>
/// Used to display a custom node property editor in the side window of the NodeEditorWindow
Expand Down
4 changes: 4 additions & 0 deletions Node_Editor/Framework/NodeCanvas.cs
Expand Up @@ -8,6 +8,10 @@ public class NodeCanvas : ScriptableObject
{ // Just contains the nodes and global canvas stuff; an associated NodeEditorState holds the actual state now
public List<Node> nodes = new List<Node> ();

// current states in the state system
public Node currentNode;
public Transition currentTransition;

This comment has been minimized.

Copy link
@Seneral

Seneral Mar 1, 2016

Owner

Key part to support multiple transitioning processes at the same time in the same canvas, as it will reveal any conflicts.

public bool livesInScene = false;
}
}

0 comments on commit 4883bec

Please sign in to comment.