No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

InvocationInspector.cs 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using Unity.VisualScripting;
  6. using UnityEditor;
  7. using UnityEngine;
  8. namespace Unity.VisualScripting
  9. {
  10. public abstract class InvocationInspector : Inspector
  11. {
  12. protected InvocationInspector(Metadata metadata) : base(metadata) { }
  13. protected Metadata argumentsMetadata => metadata["_" + nameof(MemberInvocation.arguments)];
  14. protected IList<Metadata> parameters => argumentsMetadata.children;
  15. public virtual Type expectedType { get; set; }
  16. protected virtual IEnumerable<GUIContent> compactLabels => parameters.Select(p => p.label);
  17. protected void PrepareParameterLabel(Metadata parameter, string label, string tooltip)
  18. {
  19. if (StringUtility.IsNullOrWhiteSpace(label))
  20. {
  21. label = "(?)";
  22. }
  23. parameter.label.text = label;
  24. parameter.label.tooltip = tooltip;
  25. }
  26. protected void PrepareParameterInspector(Metadata parameter, Type expectedType)
  27. {
  28. parameter.Inspector<IExpressionInspector>().expectedType = expectedType;
  29. }
  30. protected float GetParametersHeight(float width)
  31. {
  32. var height = 0f;
  33. using (LudiqGUIUtility.labelWidth.Override(GetCompactLabelsWidth(width)))
  34. {
  35. for (var i = 0; i < parameters.Count; i++)
  36. {
  37. var parameter = parameters[i];
  38. height += GetParameterHeight(parameter, width);
  39. if (i < parameters.Count - 1)
  40. {
  41. height += Styles.spaceBetweenParameters;
  42. }
  43. }
  44. }
  45. return height;
  46. }
  47. protected float GetCompactLabelsWidth(float width)
  48. {
  49. var compactLabelsWidth = 0f;
  50. foreach (var compactLabel in compactLabels)
  51. {
  52. compactLabelsWidth = Mathf.Max(compactLabelsWidth, Styles.compactLabel.CalcSize(compactLabel).x);
  53. }
  54. compactLabelsWidth = Mathf.Min(compactLabelsWidth, width / 3);
  55. return compactLabelsWidth;
  56. }
  57. protected virtual float GetParameterHeight(Metadata parameter, float width)
  58. {
  59. return InspectorGUI.GetHeight(parameter, width, parameter.label, this);
  60. }
  61. protected virtual void OnParameterGUI(Rect parameterPosition, Metadata parameter)
  62. {
  63. InspectorGUI.Field(parameter, parameterPosition, parameter.label);
  64. }
  65. protected void OnParametersGUI(Rect position)
  66. {
  67. if (parameters.Any())
  68. {
  69. using (LudiqGUIUtility.labelWidth.Override(GetCompactLabelsWidth(position.width)))
  70. {
  71. for (var i = 0; i < parameters.Count; i++)
  72. {
  73. var parameter = parameters[i];
  74. var parameterPosition = position.VerticalSection(ref y, GetParameterHeight(parameter, position.width));
  75. OnParameterGUI(parameterPosition, parameter);
  76. if (i < parameters.Count - 1)
  77. {
  78. y += Styles.spaceBetweenParameters;
  79. }
  80. }
  81. }
  82. }
  83. }
  84. public static class Styles
  85. {
  86. static Styles()
  87. {
  88. compactLabel = new GUIStyle(EditorStyles.label);
  89. var padding = compactLabel.padding;
  90. padding.right += labelPadding;
  91. compactLabel.padding = padding;
  92. }
  93. public static readonly float spaceBetweenParameters = EditorGUIUtility.standardVerticalSpacing;
  94. public static readonly float spaceAfterLabel = 0;
  95. public static readonly int labelPadding = 3;
  96. public static readonly GUIStyle compactLabel;
  97. }
  98. }
  99. }
  100. */