using System; using System.Collections.Generic; using System.Linq; using System.Reactive.Linq; using System.Text; using System.Threading.Tasks; using DynamicData; using DynamicData.Aggregation; using NodeNetwork.Views; using ReactiveUI; namespace NodeNetwork.ViewModels { /// /// Represents a connection between a node input and a node output /// public class ConnectionViewModel : ReactiveObject { static ConnectionViewModel() { NNViewRegistrar.AddRegistration(() => new ConnectionView(), typeof(IViewFor)); } #region Logger private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); #endregion /// /// The network that contains this connection /// public NetworkViewModel Parent { get; } /// /// The viewmodel of the node input that is on one end of the connection. /// public NodeInputViewModel Input { get; } /// /// The viewmodel of the node output that is on one end of the connection. /// public NodeOutputViewModel Output { get; } #region CanBeRemovedByUser /// /// If false, the user cannot delete this connection. True by default. /// public bool CanBeRemovedByUser { get => _canBeRemovedByUser; set => this.RaiseAndSetIfChanged(ref _canBeRemovedByUser, value); } private bool _canBeRemovedByUser; #endregion #region IsHighlighted /// /// If true, the connection is highlighted. /// public bool IsHighlighted { get => _isHighlighted; set => this.RaiseAndSetIfChanged(ref _isHighlighted, value); } private bool _isHighlighted; #endregion #region IsInErrorState /// /// If true, the connection is displayed as being in an erroneous state. /// public bool IsInErrorState { get => _isInErrorState; set => this.RaiseAndSetIfChanged(ref _isInErrorState, value); } private bool _isInErrorState; #endregion #region IsMarkedForDelete /// /// If true, the connection is displayed as being marked for deletion. /// public bool IsMarkedForDelete => _isMarkedForDelete.Value; private ObservableAsPropertyHelper _isMarkedForDelete; #endregion public ConnectionViewModel(NetworkViewModel parent, NodeInputViewModel input, NodeOutputViewModel output) { Parent = parent; Input = input; Output = output; this.WhenAnyValue(v => v.Parent.CutLine.IntersectingConnections) .Where(l => l != null) .Select(list => list.Connect().Filter(c => c == this).Count().Select(c => c > 0)) .Switch() .ToProperty(this, vm => vm.IsMarkedForDelete, out _isMarkedForDelete); } } }