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.

ValueOutput.cs 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. namespace Unity.VisualScripting
  5. {
  6. public sealed class ValueOutput : UnitPort<ValueInput, IUnitInputPort, ValueConnection>, IUnitValuePort, IUnitOutputPort
  7. {
  8. public ValueOutput(string key, Type type, Func<Flow, object> getValue) : base(key)
  9. {
  10. Ensure.That(nameof(type)).IsNotNull(type);
  11. Ensure.That(nameof(getValue)).IsNotNull(getValue);
  12. this.type = type;
  13. this.getValue = getValue;
  14. }
  15. public ValueOutput(string key, Type type) : base(key)
  16. {
  17. Ensure.That(nameof(type)).IsNotNull(type);
  18. this.type = type;
  19. }
  20. internal readonly Func<Flow, object> getValue;
  21. internal Func<Flow, bool> canPredictValue;
  22. public bool supportsPrediction => canPredictValue != null;
  23. public bool supportsFetch => getValue != null;
  24. public Type type { get; }
  25. public override IEnumerable<ValueConnection> validConnections => unit?.graph?.valueConnections.WithSource(this) ?? Enumerable.Empty<ValueConnection>();
  26. public override IEnumerable<InvalidConnection> invalidConnections => unit?.graph?.invalidConnections.WithSource(this) ?? Enumerable.Empty<InvalidConnection>();
  27. public override IEnumerable<ValueInput> validConnectedPorts => validConnections.Select(c => c.destination);
  28. public override IEnumerable<IUnitInputPort> invalidConnectedPorts => invalidConnections.Select(c => c.destination);
  29. public override bool CanConnectToValid(ValueInput port)
  30. {
  31. var source = this;
  32. var destination = port;
  33. return source.type.IsConvertibleTo(destination.type, false);
  34. }
  35. public override void ConnectToValid(ValueInput port)
  36. {
  37. var source = this;
  38. var destination = port;
  39. destination.Disconnect();
  40. unit.graph.valueConnections.Add(new ValueConnection(source, destination));
  41. }
  42. public override void ConnectToInvalid(IUnitInputPort port)
  43. {
  44. ConnectInvalid(this, port);
  45. }
  46. public override void DisconnectFromValid(ValueInput port)
  47. {
  48. var connection = validConnections.SingleOrDefault(c => c.destination == port);
  49. if (connection != null)
  50. {
  51. unit.graph.valueConnections.Remove(connection);
  52. }
  53. }
  54. public override void DisconnectFromInvalid(IUnitInputPort port)
  55. {
  56. DisconnectInvalid(this, port);
  57. }
  58. public ValueOutput PredictableIf(Func<Flow, bool> condition)
  59. {
  60. Ensure.That(nameof(condition)).IsNotNull(condition);
  61. canPredictValue = condition;
  62. return this;
  63. }
  64. public ValueOutput Predictable()
  65. {
  66. canPredictValue = (flow) => true;
  67. return this;
  68. }
  69. public override IUnitPort CompatiblePort(IUnit unit)
  70. {
  71. if (unit == this.unit) return null;
  72. return unit.CompatibleValueInput(type);
  73. }
  74. }
  75. }