@ -1,116 +1,27 @@
using System ;
using System ;
using System.Collections ;
using System.Collections.Generic ;
using System.Collections.Generic ;
using System.Linq ;
using System.Linq ;
using System.Reflection ;
using System.Reflection ;
using TNode.BaseViews ;
using TNode.Cache ;
using TNode.Cache ;
using TNode.Editor.GraphBlackboard ;
using TNode.Editor ;
using TNode.Editor.Inspector ;
using TNode.Editor.Inspector ;
using TNode.Editor.Model ;
using TNode.Editor.Model ;
using TNode.Editor.NodeGraphView ;
using TNode.Editor.NodeViews ;
using TNode.Editor.Search ;
using TNode.Editor.Tools.NodeCreator ;
using TNode.Editor.Tools.NodeCreator ;
using TNode.Models ;
using TNode.Models ;
using Unity.VisualScripting ;
using TNodeGraphViewImpl.Editor.GraphBlackboard ;
using TNodeGraphViewImpl.Editor.GraphBlackboard.BlackboardProperty ;
using UnityEditor ;
using UnityEditor ;
using UnityEditor.Experimental.GraphView ;
using UnityEditor.Experimental.GraphView ;
using UnityEngine ;
using UnityEngine ;
using UnityEngine.UIElements ;
using UnityEngine.UIElements ;
using Edge = UnityEditor . Experimental . GraphView . Edge ;
using Edge = UnityEditor . Experimental . GraphView . Edge ;
namespace TNode.Editor.BaseViews {
namespace TNodeGraphViewImpl.Editor.NodeGraphView {
/ *
public abstract class BaseDataGraphView < T > : GraphView , IBaseDataGraphView where T : GraphData {
public class DialogueGraphView : DataGraphView < DialogueGraph > {
public Action < DialogueNodeView > onNodeAdded ;
public Action < DialogueNodeView > onNodeSelected ;
public Action < DialogueNodeView > onNodeRemoved ;
public Action < DialogueNodeView > onNodeUnselected ;
// public DialogueGraphView(DialogueGraph graph):base(){
// this.Data = graph;
/ /
// //Set background to a bit of darker
//
//
// //Register a data context change callback
//
// }
public override void OnGraphViewCreate ( ) {
AddNode ( GenerateEntryPoint ( ) ) ;
RegisterCallback < ContextualMenuPopulateEvent > ( evt = > {
var pos = evt . mousePosition ;
evt . menu . AppendAction ( "Add NodeAttribute" , ( dropMenuAction ) = > {
DialogueNodeView nodeView = new DialogueNodeView {
GUID = Guid . NewGuid ( ) . ToString ( ) ,
title = "New NodeAttribute"
} ;
// make it a 200x100 box
nodeView . SetPosition ( new Rect ( pos . x - 1 0 0 , pos . y - 5 0 , 2 0 0 , 1 0 0 ) ) ;
AddNode ( nodeView ) ;
} , DropdownMenuAction . AlwaysEnabled ) ;
} ) ;
this . OnDataChanged + = OnOnDataChanged ;
}
private void OnOnDataChanged ( object sender , DataChangedEventArgs < DialogueGraph > e ) {
//clean all nodes from the graphview
foreach ( var graphViewNode in nodes ) {
RemoveElement ( graphViewNode ) ;
}
foreach ( var edge in edges ) {
RemoveElement ( edge ) ;
}
//add all nodes from the new graph
foreach ( var node in e . NewData . nodes ) {
//AddNode(node);
}
}
public void AddNode ( DialogueNodeData dialogueNodeData ) {
var res = InstantiateFromDialogueNodeData ( dialogueNodeData ) ;
AddNode ( res ) ;
}
public void AddNode ( DialogueNodeView nodeView ) {
AddElement ( nodeView ) ;
onNodeAdded ? . Invoke ( nodeView ) ;
//Register nodeView selection callback
nodeView . RegisterCallback < MouseDownEvent > ( evt = > {
if ( evt . clickCount = = 1 ) {
onNodeSelected ? . Invoke ( nodeView ) ;
}
} ) ;
nodeView . OnUnselect + = ( ) = > { onNodeUnselected ? . Invoke ( nodeView ) ; } ;
}
public override List < Port > GetCompatiblePorts ( Port startPort , NodeAdapter nodeAdapter ) = > this . ports . ToList ( )
. Where ( x = > x ! = startPort & &
x . direction ! = startPort . direction ) . ToList ( ) ;
public DialogueNodeView GenerateEntryPoint ( ) {
var entryPoint = new DialogueNodeView {
title = "Entry Point" ,
GUID = Guid . NewGuid ( ) . ToString ( ) ,
EntryPoint = true
} ;
//Add output port to the nodeView
entryPoint . AddPort ( Orientation . Horizontal , Direction . Output , "Next" ) ;
//Set nodeView position to top center side of screen
entryPoint . SetPosition ( new Rect ( this . layout . width / 2 - 1 0 0 , 0 , 2 0 0 , 2 0 0 ) ) ;
return entryPoint ;
}
protected DialogueNodeView InstantiateFromDialogueNodeData ( DialogueNodeData dialogueNodeData ) {
var node = new DialogueNodeView ( ) ;
node . title = dialogueNodeData . nodeName ;
node . GUID = Guid . NewGuid ( ) . ToString ( ) ;
//TODO:after completing the separation of the node data and the node editor data,this should be switch to the node editor data
//node.SetPosition(dialogueNodeData.rect);
this . AddNode ( node ) ;
return node ;
}
}
* /
public abstract class DataGraphView < T > : GraphView , IDataGraphView where T : GraphData {
#region variables and properties
#region variables and properties
private T _d ata ;
private T _d ata ;
private bool _ isInspectorOn ;
private bool _ isInspectorOn ;
@ -136,11 +47,11 @@ namespace TNode.Editor.BaseViews{
# endregion
# endregion
//A Constructor for the DataGraphView ,never to override it
//A Constructor for the Base DataGraphView ,never to override it
#region construct default behaviour
#region construct default behaviour
public DataGraphView ( ) {
public Base DataGraphView( ) {
styleSheets . Add ( Resources . Load < StyleSheet > ( "GraphViewBackground" ) ) ;
styleSheets . Add ( Resources . Load < StyleSheet > ( "GraphViewBackground" ) ) ;
var grid = new GridBackground ( ) ;
var grid = new GridBackground ( ) ;
Insert ( 0 , grid ) ;
Insert ( 0 , grid ) ;
@ -281,20 +192,11 @@ namespace TNode.Editor.BaseViews{
}
}
public virtual void CreateBlackboard ( ) {
public virtual void CreateBlackboard ( ) {
_ blackboard = new Blackboard ( ) ;
//Blackboard add "Add Node" button
_ blackboard = NodeEditorExtensions . CreateBlackboardWithGraphData ( typeof ( T ) ) ;
// blackboard.Add(new BlackboardSection(){
// title = "Hello World",
// });
// blackboard.addItemRequested = (item) => {
// //Create a sub window for the blackboard to show the selection
// var subWindow = ScriptableObject.CreateNodeComponentFromGenericType<NodeSearchWindowProvider>();
// };
/ /
//Set black board to left side of the view
_ blackboard . SetPosition ( new Rect ( 0 , 0 , 2 0 0 , 6 0 0 ) ) ;
_ blackboard . SetPosition ( new Rect ( 0 , 0 , 2 0 0 , 6 0 0 ) ) ;
Add ( _ blackboard ) ;
Add ( _ blackboard ) ;
//Check the type of the blackboard
OnDataChanged + = ( sender , e ) = > { BlackboardUpdate ( ) ; } ;
OnDataChanged + = ( sender , e ) = > { BlackboardUpdate ( ) ; } ;
@ -311,9 +213,25 @@ namespace TNode.Editor.BaseViews{
foreach ( var field in _d ata . blackboardData . GetType ( )
foreach ( var field in _d ata . blackboardData . GetType ( )
. GetFields ( BindingFlags . Public | BindingFlags . NonPublic | BindingFlags . Instance ) ) {
. GetFields ( BindingFlags . Public | BindingFlags . NonPublic | BindingFlags . Instance ) ) {
//if the field is MonoBehaviour,add a property field for blackboard
//if the field is MonoBehaviour,add a property field for blackboard
//skip if the field is a list or Ilist
if ( ! typeof ( IList ) . IsAssignableFrom ( field . FieldType ) ) {
var propertyField = new BlackboardPropertyField ( new BlackboardProperty ( field . Name , field . FieldType ) ) ;
var propertyField = new BlackboardPropertyField ( new BlackboardProperty ( field . Name , field . FieldType ) ) ;
_ blackboard . Add ( propertyField ) ;
_ blackboard . Add ( propertyField ) ;
}
}
}
_ blackboard . addItemRequested = ( sender ) = > {
var res = ScriptableObject . CreateInstance < BlackboardSearchWindowProvider > ( ) ;
//Get right top corner of the blackboard
var blackboardPos = _ blackboard . GetPosition ( ) . position ;
var searchWindowContext = new SearchWindowContext ( blackboardPos , 2 0 0 , 2 0 0 ) ;
//Call search window
res . Setup ( typeof ( T ) , this , Owner ) ;
SearchWindow . Open ( searchWindowContext , res ) ;
} ;
}
}
public virtual void DestroyInspector ( ) {
public virtual void DestroyInspector ( ) {
@ -341,7 +259,7 @@ namespace TNode.Editor.BaseViews{
var nodeEditorData = new GraphElementEditorData {
var nodeEditorData = new GraphElementEditorData {
pos = node . GetPosition ( ) ,
pos = node . GetPosition ( ) ,
} ;
} ;
if ( node is INodeView nodeView ) {
if ( node is IBase NodeView nodeView ) {
nodeEditorData . guid = nodeView . GetNodeData ( ) . id ;
nodeEditorData . guid = nodeView . GetNodeData ( ) . id ;
}
}
graphEditorData . graphElementsData . Add ( nodeEditorData ) ;
graphEditorData . graphElementsData . Add ( nodeEditorData ) ;
@ -357,7 +275,7 @@ namespace TNode.Editor.BaseViews{
private void SaveNode ( ) {
private void SaveNode ( ) {
foreach ( var node in nodes ) {
foreach ( var node in nodes ) {
if ( node is INodeView nodeView ) {
if ( node is IBase NodeView nodeView ) {
var nodeData = nodeView . GetNodeData ( ) ;
var nodeData = nodeView . GetNodeData ( ) ;
if ( ! _d ata . NodeDictionary . ContainsKey ( nodeData . id ) ) {
if ( ! _d ata . NodeDictionary . ContainsKey ( nodeData . id ) ) {
_d ata . NodeDictionary . Add ( nodeData . id , nodeData ) ;
_d ata . NodeDictionary . Add ( nodeData . id , nodeData ) ;
@ -368,8 +286,8 @@ namespace TNode.Editor.BaseViews{
private void SaveEdge ( ) {
private void SaveEdge ( ) {
var links = new List < NodeLink > ( ) ;
var links = new List < NodeLink > ( ) ;
foreach ( var edge in edges ) {
foreach ( var edge in edges ) {
var inputNode = edge . input . node as INodeView ;
var inputNode = edge . input . node as IBase NodeView ;
var outputNode = edge . output . node as INodeView ;
var outputNode = edge . output . node as IBase NodeView ;
if ( inputNode ! = null & & outputNode ! = null ) {
if ( inputNode ! = null & & outputNode ! = null ) {
var inputNodeData = inputNode . GetNodeData ( ) ;
var inputNodeData = inputNode . GetNodeData ( ) ;
var outputNodeData = outputNode . GetNodeData ( ) ;
var outputNodeData = outputNode . GetNodeData ( ) ;
@ -408,7 +326,7 @@ namespace TNode.Editor.BaseViews{
public virtual void OnGraphViewDestroy ( ) {
public virtual void OnGraphViewDestroy ( ) {
}
}
~ DataGraphView ( ) {
~ Base DataGraphView( ) {
OnGraphViewDestroy ( ) ;
OnGraphViewDestroy ( ) ;
}
}
@ -425,11 +343,11 @@ namespace TNode.Editor.BaseViews{
if ( evt . clickCount = = 1 ) {
if ( evt . clickCount = = 1 ) {
if ( _ isInspectorOn ) {
if ( _ isInspectorOn ) {
_ nodeInspector . Data = nodeData ;
_ nodeInspector . Data = nodeData ;
_ nodeInspector . NodeView = nodeView as INodeView ;
_ nodeInspector . Base NodeView = nodeView as IBase NodeView ;
}
}
}
}
} ) ;
} ) ;
if ( nodeView is INodeView nodeViewInterface ) {
if ( nodeView is IBase NodeView nodeViewInterface ) {
nodeViewInterface . SetNodeData ( nodeData ) ;
nodeViewInterface . SetNodeData ( nodeData ) ;
}
}
_ nodeDict . Add ( nodeData . id , nodeView ) ;
_ nodeDict . Add ( nodeData . id , nodeView ) ;
@ -439,7 +357,7 @@ namespace TNode.Editor.BaseViews{
var menu = new GenericMenu ( ) ;
var menu = new GenericMenu ( ) ;
menu . AddItem ( new GUIContent ( "Delete" ) , false , ( ) = > {
menu . AddItem ( new GUIContent ( "Delete" ) , false , ( ) = > {
RemoveElement ( nodeView ) ;
RemoveElement ( nodeView ) ;
if ( nodeView is INodeView tNodeView ) {
if ( nodeView is IBase NodeView tNodeView ) {
RemoveTNode ( tNodeView . GetNodeData ( ) ) ;
RemoveTNode ( tNodeView . GetNodeData ( ) ) ;
}
}
} ) ;
} ) ;
@ -468,12 +386,6 @@ namespace TNode.Editor.BaseViews{
}
}
}
}
public interface IDataGraphView {
public void AddTNode ( NodeData nodeData , Rect rect ) ;
public void RemoveTNode ( NodeData nodeData ) ;
public BlackboardData GetBlackboardData ( ) ;
}
public class DataChangedEventArgs < T > {
public class DataChangedEventArgs < T > {
public DataChangedEventArgs ( T data ) {
public DataChangedEventArgs ( T data ) {