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