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.

ValueInput.cs 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using UnityEngine;
  5. using UnityObject = UnityEngine.Object;
  6. namespace Unity.VisualScripting
  7. {
  8. public sealed class ValueInput : UnitPort<ValueOutput, IUnitOutputPort, ValueConnection>, IUnitValuePort, IUnitInputPort
  9. {
  10. public ValueInput(string key, Type type) : base(key)
  11. {
  12. Ensure.That(nameof(type)).IsNotNull(type);
  13. this.type = type;
  14. }
  15. public Type type { get; }
  16. public bool hasDefaultValue => unit.defaultValues.ContainsKey(key);
  17. public override IEnumerable<ValueConnection> validConnections => unit?.graph?.valueConnections.WithDestination(this) ?? Enumerable.Empty<ValueConnection>();
  18. public override IEnumerable<InvalidConnection> invalidConnections => unit?.graph?.invalidConnections.WithDestination(this) ?? Enumerable.Empty<InvalidConnection>();
  19. public override IEnumerable<ValueOutput> validConnectedPorts => validConnections.Select(c => c.source);
  20. public override IEnumerable<IUnitOutputPort> invalidConnectedPorts => invalidConnections.Select(c => c.source);
  21. // Use for inspector metadata
  22. [DoNotSerialize]
  23. internal object _defaultValue
  24. {
  25. get
  26. {
  27. return unit.defaultValues[key];
  28. }
  29. set
  30. {
  31. unit.defaultValues[key] = value;
  32. }
  33. }
  34. public bool nullMeansSelf { get; private set; }
  35. public bool allowsNull { get; private set; }
  36. public ValueConnection connection => unit.graph?.valueConnections.SingleOrDefaultWithDestination(this);
  37. public override bool hasValidConnection => connection != null;
  38. public void SetDefaultValue(object value)
  39. {
  40. Ensure.That(nameof(value)).IsOfType(value, type);
  41. if (!SupportsDefaultValue(type))
  42. {
  43. return;
  44. }
  45. if (unit.defaultValues.ContainsKey(key))
  46. {
  47. unit.defaultValues[key] = value;
  48. }
  49. else
  50. {
  51. unit.defaultValues.Add(key, value);
  52. }
  53. }
  54. public override bool CanConnectToValid(ValueOutput port)
  55. {
  56. var source = port;
  57. var destination = this;
  58. return source.type.IsConvertibleTo(destination.type, false);
  59. }
  60. public override void ConnectToValid(ValueOutput port)
  61. {
  62. var source = port;
  63. var destination = this;
  64. destination.Disconnect();
  65. unit.graph.valueConnections.Add(new ValueConnection(source, destination));
  66. }
  67. public override void ConnectToInvalid(IUnitOutputPort port)
  68. {
  69. ConnectInvalid(port, this);
  70. }
  71. public override void DisconnectFromValid(ValueOutput port)
  72. {
  73. var connection = validConnections.SingleOrDefault(c => c.source == port);
  74. if (connection != null)
  75. {
  76. unit.graph.valueConnections.Remove(connection);
  77. }
  78. }
  79. public override void DisconnectFromInvalid(IUnitOutputPort port)
  80. {
  81. DisconnectInvalid(port, this);
  82. }
  83. public ValueInput NullMeansSelf()
  84. {
  85. if (ComponentHolderProtocol.IsComponentHolderType(type))
  86. {
  87. nullMeansSelf = true;
  88. }
  89. return this;
  90. }
  91. public ValueInput AllowsNull()
  92. {
  93. if (type.IsNullable())
  94. {
  95. allowsNull = true;
  96. }
  97. return this;
  98. }
  99. private static readonly HashSet<Type> typesWithDefaultValues = new HashSet<Type>()
  100. {
  101. typeof(Vector2),
  102. typeof(Vector3),
  103. typeof(Vector4),
  104. typeof(Color),
  105. typeof(AnimationCurve),
  106. typeof(Rect),
  107. typeof(Ray),
  108. typeof(Ray2D),
  109. typeof(Type),
  110. #if PACKAGE_INPUT_SYSTEM_EXISTS
  111. typeof(UnityEngine.InputSystem.InputAction),
  112. #endif
  113. };
  114. public static bool SupportsDefaultValue(Type type)
  115. {
  116. return
  117. typesWithDefaultValues.Contains(type) ||
  118. typesWithDefaultValues.Contains(Nullable.GetUnderlyingType(type)) ||
  119. type.IsBasic() ||
  120. typeof(UnityObject).IsAssignableFrom(type);
  121. }
  122. public override IUnitPort CompatiblePort(IUnit unit)
  123. {
  124. if (unit == this.unit) return null;
  125. return unit.CompatibleValueOutput(type);
  126. }
  127. }
  128. }