1
0
mirror of https://github.com/Siccity/xNode.git synced 2026-03-26 22:49:02 +08:00
xNode/Scripts/Editor/Drawers/Odin/AsStaticPortAttributes.cs
2019-11-03 07:44:04 -05:00

100 lines
2.8 KiB
C#

#if UNITY_EDITOR && ODIN_INSPECTOR
using Sirenix.OdinInspector.Editor;
using System;
using UnityEditor;
using UnityEngine;
using XNode;
namespace XNodeEditor.Odin
{
internal struct AsStaticPortScope : IDisposable
{
public AsStaticPortScope( NodePort port )
{
EditorGUILayout.BeginVertical();
var rect = GUILayoutUtility.GetRect( 0f, float.MaxValue, 0f, 0f, GUI.skin.label, GUILayout.ExpandWidth( true ) );
if ( port != null && NodeEditor.isNodeEditor )
{
if ( port.IsInput )
{
NodeEditorGUILayout.PortField( new Vector2( rect.xMin - 18, rect.center.y + 2 ), port );
}
else
{
NodeEditorGUILayout.PortField( new Vector2( rect.xMax + 2, rect.center.y + 2 ), port );
}
}
EditorGUILayout.BeginHorizontal();
EditorGUILayout.BeginVertical();
}
public void Dispose()
{
EditorGUILayout.EndVertical();
EditorGUILayout.EndHorizontal();
EditorGUILayout.EndVertical();
}
}
[DrawerPriority( 0.4, 0, 0 )]
internal class InputAttributeDrawer<T> : OdinAttributeDrawer<Node.InputAttribute, T>
{
protected override bool CanDrawAttributeValueProperty( InspectorProperty property )
{
var attr = property.GetAttribute<Node.InputAttribute>();
if ( attr != null )
return !attr.dynamicPortList;
return false;
}
protected bool drawData = false;
protected override void DrawPropertyLayout( GUIContent label )
{
NodePort port = ( Property.Tree.UnitySerializedObject.targetObject as Node ).GetInputPort( Property.Name );
if ( Event.current.type == EventType.Layout )
drawData = Attribute.backingValue == Node.ShowBackingValue.Always || Attribute.backingValue == Node.ShowBackingValue.Unconnected && !port.IsConnected;
using ( new AsStaticPortScope( port ) )
{
if ( drawData )
CallNextDrawer( label );
else
EditorGUILayout.LabelField( label );
}
}
}
[DrawerPriority( 0.4, 0, 0 )]
internal class OutputAttributeDrawer<T> : OdinAttributeDrawer<Node.OutputAttribute, T>
{
protected override bool CanDrawAttributeValueProperty( InspectorProperty property )
{
var attr = property.GetAttribute<Node.OutputAttribute>();
if ( attr != null )
return !attr.dynamicPortList;
return false;
}
protected bool drawData = false;
protected override void DrawPropertyLayout( GUIContent label )
{
NodePort port = ( Property.Tree.UnitySerializedObject.targetObject as Node ).GetOutputPort( Property.Name );
if ( Event.current.type == EventType.Layout )
drawData = Attribute.backingValue == Node.ShowBackingValue.Always || Attribute.backingValue == Node.ShowBackingValue.Unconnected && !port.IsConnected;
using ( new AsStaticPortScope( port ) )
{
if ( drawData )
CallNextDrawer( label );
else
EditorGUILayout.LabelField( label );
}
}
}
}
#endif