OutputAttributeDrawer.cs 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. #if UNITY_EDITOR && ODIN_INSPECTOR
  2. using Sirenix.OdinInspector;
  3. using Sirenix.OdinInspector.Editor;
  4. using Sirenix.Utilities.Editor;
  5. using UnityEngine;
  6. using XNode;
  7. namespace XNodeEditor {
  8. public class OutputAttributeDrawer : OdinAttributeDrawer<XNode.Node.OutputAttribute> {
  9. protected override bool CanDrawAttributeProperty(InspectorProperty property) {
  10. Node node = property.Tree.WeakTargets[0] as Node;
  11. return node != null;
  12. }
  13. protected override void DrawPropertyLayout(GUIContent label) {
  14. Node node = Property.Tree.WeakTargets[0] as Node;
  15. NodePort port = node.GetOutputPort(Property.Name);
  16. if (!NodeEditor.inNodeEditor) {
  17. if (Attribute.backingValue == XNode.Node.ShowBackingValue.Always || Attribute.backingValue == XNode.Node.ShowBackingValue.Unconnected && !port.IsConnected)
  18. CallNextDrawer(label);
  19. return;
  20. }
  21. if (Property.Tree.WeakTargets.Count > 1) {
  22. SirenixEditorGUI.WarningMessageBox("Cannot draw ports with multiple nodes selected");
  23. return;
  24. }
  25. if (port != null) {
  26. var portPropoerty = Property.Tree.GetUnityPropertyForPath(Property.UnityPropertyPath);
  27. if (portPropoerty == null) {
  28. SirenixEditorGUI.ErrorMessageBox("Port property missing at: " + Property.UnityPropertyPath);
  29. return;
  30. } else {
  31. var labelWidth = Property.GetAttribute<LabelWidthAttribute>();
  32. if (labelWidth != null)
  33. GUIHelper.PushLabelWidth(labelWidth.Width);
  34. NodeEditorGUILayout.PropertyField(portPropoerty, label == null ? GUIContent.none : label, true, GUILayout.MinWidth(30));
  35. if (labelWidth != null)
  36. GUIHelper.PopLabelWidth();
  37. }
  38. }
  39. }
  40. }
  41. }
  42. #endif