Нет описания
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

ValueConnectionWidget.cs 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. using System;
  2. using UnityEditor;
  3. using UnityEngine;
  4. namespace Unity.VisualScripting
  5. {
  6. [Widget(typeof(ValueConnection))]
  7. public sealed class ValueConnectionWidget : UnitConnectionWidget<ValueConnection>
  8. {
  9. public ValueConnectionWidget(FlowCanvas canvas, ValueConnection connection) : base(canvas, connection) { }
  10. private new ValueConnection.DebugData ConnectionDebugData => GetDebugData<ValueConnection.DebugData>();
  11. #region Drawing
  12. public override Color color => DetermineColor(connection.source.type, connection.destination.type);
  13. protected override bool colorIfActive => !BoltFlow.Configuration.animateControlConnections || !BoltFlow.Configuration.animateValueConnections;
  14. public override void DrawForeground()
  15. {
  16. base.DrawForeground();
  17. if (BoltFlow.Configuration.showConnectionValues)
  18. {
  19. var showLastValue = EditorApplication.isPlaying && ConnectionDebugData.assignedLastValue;
  20. var showPredictedvalue = BoltFlow.Configuration.predictConnectionValues && !EditorApplication.isPlaying && Flow.CanPredict(connection.source, reference);
  21. if (showLastValue || showPredictedvalue)
  22. {
  23. var previousIconSize = EditorGUIUtility.GetIconSize();
  24. EditorGUIUtility.SetIconSize(new Vector2(IconSize.Small, IconSize.Small));
  25. object value;
  26. if (showLastValue)
  27. {
  28. value = ConnectionDebugData.lastValue;
  29. }
  30. else // if (showPredictedvalue)
  31. {
  32. value = Flow.Predict(connection.source, reference);
  33. }
  34. var label = new GUIContent(value.ToShortString(), Icons.Type(value?.GetType())?[IconSize.Small]);
  35. var labelSize = Styles.prediction.CalcSize(label);
  36. var labelPosition = new Rect(position.position - labelSize / 2, labelSize);
  37. BeginDim();
  38. GUI.Label(labelPosition, label, Styles.prediction);
  39. EndDim();
  40. EditorGUIUtility.SetIconSize(previousIconSize);
  41. }
  42. }
  43. }
  44. public static Color DetermineColor(Type source, Type destination)
  45. {
  46. if (destination == typeof(object))
  47. {
  48. return DetermineColor(source);
  49. }
  50. return DetermineColor(destination);
  51. }
  52. public static Color DetermineColor(Type type)
  53. {
  54. if (type == null)
  55. {
  56. return new Color(0.8f, 0.8f, 0.8f);
  57. }
  58. if (type == typeof(string))
  59. {
  60. return new Color(1.0f, 0.62f, 0.35f);
  61. }
  62. if (type == typeof(bool))
  63. {
  64. return new Color(0.86f, 0.55f, 0.92f);
  65. }
  66. if (type == typeof(char))
  67. {
  68. return new Color(1.0f, 0.90f, 0.40f);
  69. }
  70. if (type.IsEnum)
  71. {
  72. return new Color(1.0f, 0.63f, 0.66f);
  73. }
  74. if (type.IsNumeric())
  75. {
  76. return new Color(0.45f, 0.78f, 1f);
  77. }
  78. if (type.IsNumericConstruct())
  79. {
  80. return new Color(0.45f, 1.00f, 0.82f);
  81. }
  82. return new Color(0.60f, 0.88f, 0.00f);
  83. }
  84. #endregion
  85. #region Droplets
  86. protected override bool showDroplets => BoltFlow.Configuration.animateValueConnections;
  87. protected override Vector2 GetDropletSize()
  88. {
  89. return BoltFlow.Icons.valuePortConnected?[12].Size() ?? 12 * Vector3.one;
  90. }
  91. protected override void DrawDroplet(Rect position)
  92. {
  93. if (BoltFlow.Icons.valuePortConnected != null)
  94. {
  95. GUI.DrawTexture(position, BoltFlow.Icons.valuePortConnected?[12]);
  96. }
  97. }
  98. #endregion
  99. private static class Styles
  100. {
  101. static Styles()
  102. {
  103. prediction = new GUIStyle(EditorStyles.label);
  104. prediction.normal.textColor = Color.white;
  105. prediction.fontSize = 9;
  106. prediction.normal.background = new Color(0, 0, 0, 0.25f).GetPixel();
  107. prediction.padding = new RectOffset(4, 6, 3, 3);
  108. prediction.margin = new RectOffset(0, 0, 0, 0);
  109. prediction.alignment = TextAnchor.MiddleCenter;
  110. }
  111. public static readonly GUIStyle prediction;
  112. }
  113. }
  114. }