public class GraphEditorView : VisualElement , IDisposable
{
MaterialGraphView m_GraphView ;
GraphInspectorView m_GraphInspectorView ;
private EditorWindow m_EditorWindow ;
AbstractMaterialGraph m_Graph ;
public Action onUpdateAssetClick
{
get { return m_GraphInspectorView . onUpdateAssetClick ; }
set { m_GraphInspectorView . onUpdateAssetClick = value ; }
}
public Action saveRequested { get ; set ; }
public Action onConvertToSubgraphClick
public Action convertToSubgraphRequested
public Action onShowInProjectClick
{
get { return m_GraphInspectorView . onShowInProjectClick ; }
set { m_GraphInspectorView . onShowInProjectClick = value ; }
}
public Action showInProjectRequested { get ; set ; }
public MaterialGraphView graphView
{
set { m_PreviewManager = value ; }
}
GraphInspectorView inspectorView
{
get { return m_GraphInspectorView ; }
}
public GraphEditorView ( EditorWindow editorWindow , AbstractMaterialGraph graph , string assetName )
{
m_Graph = graph ;
var toolbar = new IMGUIContainer ( ( ) = >
{
GUILayout . BeginHorizontal ( EditorStyles . toolbar ) ;
if ( GUILayout . Button ( "Save Asset" , EditorStyles . toolbarButton ) )
{
if ( saveRequested ! = null )
saveRequested ( ) ;
}
GUILayout . Space ( 6 ) ;
if ( GUILayout . Button ( "Show In Project" , EditorStyles . toolbarButton ) )
{
if ( showInProjectRequested ! = null )
showInProjectRequested ( ) ;
}
GUILayout . FlexibleSpace ( ) ;
GUILayout . EndHorizontal ( ) ;
} ) ;
Add ( toolbar ) ;
var content = new VisualElement { name = "content" } ;
{
m_GraphView = new MaterialGraphView ( graph ) { name = "GraphView" , persistenceKey = "MaterialGraphView" } ;
m_GraphView . RegisterCallback < KeyDownEvent > ( OnSpaceDown ) ;
content . Add ( m_GraphView ) ;
m_GraphInspectorView = new GraphInspectorView ( assetName , previewManager , graph ) { name = "inspector" } ;
m_GraphInspectorView . AddManipulator ( new Draggable ( OnMouseDrag , true ) ) ;
m_GraphView . RegisterCallback < PostLayoutEvent > ( OnPostLayout ) ;
m_GraphInspectorView . RegisterCallback < PostLayoutEvent > ( OnPostLayout ) ;
m_GraphView . Add ( m_GraphInspectorView ) ;
m_BlackboardProvider = new BlackboardProvider ( assetName , graph ) ;
m_GraphView . Add ( m_BlackboardProvider . blackboard ) ;
Add ( content ) ;
}
void OnPostLayout ( PostLayoutEvent evt )
{
const float minimumVisibility = 6 0f ;
Rect inspectorViewRect = m_GraphInspectorView . layout ;
float minimumXPosition = minimumVisibility - inspectorViewRect . width ;
float maximumXPosition = m_GraphView . layout . width - minimumVisibility ;
float minimumYPosition = minimumVisibility - inspectorViewRect . height ;
float maximumYPosition = m_GraphView . layout . height - minimumVisibility ;
inspectorViewRect . x = Mathf . Clamp ( inspectorViewRect . x , minimumXPosition , maximumXPosition ) ;
inspectorViewRect . y = Mathf . Clamp ( inspectorViewRect . y , minimumYPosition , maximumYPosition ) ;
inspectorViewRect . width = Mathf . Min ( inspectorViewRect . width , layout . width ) ;
inspectorViewRect . height = Mathf . Min ( inspectorViewRect . height , layout . height ) ;
m_GraphInspectorView . layout = inspectorViewRect ;
}
void OnSpaceDown ( KeyDownEvent evt )
{
if ( evt . keyCode = = KeyCode . Space )
graphView . nodeCreationRequest ( new NodeCreationContext ( ) { screenMousePosition = screenPoint } ) ;
}
}
void OnMouseDrag ( Vector2 mouseDelta )
{
Vector2 normalizedDelta = mouseDelta / 2f ;
Rect inspectorWindowRect = m_GraphInspectorView . layout ;
inspectorWindowRect . x + = normalizedDelta . x ;
inspectorWindowRect . y + = normalizedDelta . y ;
m_GraphInspectorView . layout = inspectorWindowRect ;
}
GraphViewChange GraphViewChanged ( GraphViewChange graphViewChange )
{
previewManager . HandleGraphChanges ( ) ;
previewManager . RenderPreviews ( ) ;
inspectorView . HandleGraphChanges ( ) ;
m_BlackboardProvider . HandleGraphChanges ( ) ;
foreach ( var node in m_Graph . removedNodes )
public void Dispose ( )
{
onUpdateAssetClick = null ;
onConvertToSubgraphClick = null ;
onShowInProjectClick = null ;
saveRequested = null ;
convertToSubgraphRequested = null ;
showInProjectRequested = null ;
if ( m_GraphView ! = null )
{
foreach ( var node in m_GraphView . Children ( ) . OfType < MaterialNodeView > ( ) )
if ( m_GraphInspectorView ! = null ) m_GraphInspectorView . Dispose ( ) ;
if ( previewManager ! = null )
{
previewManager . Dispose ( ) ;