diff --git a/src/Artemis.Core/VisualScripting/Pin.cs b/src/Artemis.Core/VisualScripting/Pin.cs index a562f7961..8258ed4bc 100644 --- a/src/Artemis.Core/VisualScripting/Pin.cs +++ b/src/Artemis.Core/VisualScripting/Pin.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; +using System.Threading.Tasks; using Artemis.Core.Events; namespace Artemis.Core; @@ -143,30 +144,20 @@ public abstract class Pin : CorePropertyChanged, IPin /// The backing field of the current type of the pin. protected void ChangeType(Type type, ref Type currentType) { - // Enums are a special case that disconnect and, if still compatible, reconnect - if (type.IsEnum && currentType.IsEnum) - { - List connections = new(ConnectedTo); - DisconnectAll(); + if (currentType == type) + return; - // Change the type - SetAndNotify(ref currentType, type, nameof(Type)); - IsNumeric = type == typeof(Numeric); + bool changingEnums = type.IsEnum && currentType.IsEnum; - foreach (IPin pin in connections.Where(p => p.IsTypeCompatible(type))) - ConnectTo(pin); - } - // Disconnect pins incompatible with the new type - else - { - List toDisconnect = ConnectedTo.Where(p => !p.IsTypeCompatible(type, false)).ToList(); - foreach (IPin pin in toDisconnect) - DisconnectFrom(pin); + List connections = new(ConnectedTo); + DisconnectAll(); - // Change the type - SetAndNotify(ref currentType, type, nameof(Type)); - IsNumeric = type == typeof(Numeric); - } + // Change the type + SetAndNotify(ref currentType, type, nameof(Type)); + IsNumeric = type == typeof(Numeric); + + foreach (IPin pin in connections.Where(p => p.IsTypeCompatible(type, changingEnums))) + ConnectTo(pin); } #endregion diff --git a/src/Artemis.UI/Screens/ProfileEditor/Panels/ProfileTree/Dialogs/LayerHintsDialogViewModel.cs b/src/Artemis.UI/Screens/ProfileEditor/Panels/ProfileTree/Dialogs/LayerHintsDialogViewModel.cs index de7375ccb..baac1b81f 100644 --- a/src/Artemis.UI/Screens/ProfileEditor/Panels/ProfileTree/Dialogs/LayerHintsDialogViewModel.cs +++ b/src/Artemis.UI/Screens/ProfileEditor/Panels/ProfileTree/Dialogs/LayerHintsDialogViewModel.cs @@ -32,7 +32,7 @@ public class LayerHintsDialogViewModel : DialogViewModelBase .Subscribe(c => AdaptionHints.Add(CreateHintViewModel(c.EventArgs.AdaptionHint))) .DisposeWith(d); Observable.FromEventPattern(x => layer.Adapter.AdapterHintRemoved += x, x => layer.Adapter.AdapterHintRemoved -= x) - .Subscribe(c => AdaptionHints.Remove(AdaptionHints.FirstOrDefault(h => h.AdaptionHint == c.EventArgs.AdaptionHint)!)) + .Subscribe(c => AdaptionHints.RemoveMany(AdaptionHints.Where(h => h.AdaptionHint == c.EventArgs.AdaptionHint))) .DisposeWith(d); AdaptionHints.AddRange(Layer.Adapter.AdaptionHints.Select(CreateHintViewModel)); diff --git a/src/Artemis.UI/Screens/ProfileEditor/Panels/Properties/Timeline/TimelinePropertyViewModel.cs b/src/Artemis.UI/Screens/ProfileEditor/Panels/Properties/Timeline/TimelinePropertyViewModel.cs index 0b754b23b..211c91130 100644 --- a/src/Artemis.UI/Screens/ProfileEditor/Panels/Properties/Timeline/TimelinePropertyViewModel.cs +++ b/src/Artemis.UI/Screens/ProfileEditor/Panels/Properties/Timeline/TimelinePropertyViewModel.cs @@ -42,7 +42,7 @@ public class TimelinePropertyViewModel : ActivatableViewModelBase, ITimelineP .Subscribe(e => _keyframes.Add((LayerPropertyKeyframe) e.EventArgs.Keyframe)) .DisposeWith(d); Observable.FromEventPattern(x => LayerProperty.KeyframeRemoved += x, x => LayerProperty.KeyframeRemoved -= x) - .Subscribe(e => _keyframes.Remove((LayerPropertyKeyframe) e.EventArgs.Keyframe)) + .Subscribe(e => _keyframes.RemoveMany(_keyframes.Items.Where(k => k == e.EventArgs.Keyframe))) .DisposeWith(d); _keyframes.Edit(k => diff --git a/src/Artemis.UI/Screens/Sidebar/SidebarCategoryViewModel.cs b/src/Artemis.UI/Screens/Sidebar/SidebarCategoryViewModel.cs index 61278c700..36f373bda 100644 --- a/src/Artemis.UI/Screens/Sidebar/SidebarCategoryViewModel.cs +++ b/src/Artemis.UI/Screens/Sidebar/SidebarCategoryViewModel.cs @@ -64,7 +64,7 @@ public class SidebarCategoryViewModel : ActivatableViewModelBase .Subscribe(e => profileConfigurations.Add(e.EventArgs.ProfileConfiguration)) .DisposeWith(d); Observable.FromEventPattern(x => profileCategory.ProfileConfigurationRemoved += x, x => profileCategory.ProfileConfigurationRemoved -= x) - .Subscribe(e => profileConfigurations.Remove(e.EventArgs.ProfileConfiguration)) + .Subscribe(e => profileConfigurations.RemoveMany(profileConfigurations.Items.Where(c => c == e.EventArgs.ProfileConfiguration))) .DisposeWith(d); profileEditorService.ProfileConfiguration.Subscribe(p => SelectedProfileConfiguration = ProfileConfigurations.FirstOrDefault(c => ReferenceEquals(c.ProfileConfiguration, p))) diff --git a/src/Artemis.UI/Screens/VisualScripting/NodeScriptViewModel.cs b/src/Artemis.UI/Screens/VisualScripting/NodeScriptViewModel.cs index 6bbdb5ac0..039d5f4fe 100644 --- a/src/Artemis.UI/Screens/VisualScripting/NodeScriptViewModel.cs +++ b/src/Artemis.UI/Screens/VisualScripting/NodeScriptViewModel.cs @@ -50,10 +50,10 @@ public class NodeScriptViewModel : ActivatableViewModelBase this.WhenActivated(d => { Observable.FromEventPattern>(x => NodeScript.NodeAdded += x, x => NodeScript.NodeAdded -= x) - .Subscribe(e => HandleNodeAdded(e.EventArgs)) + .Subscribe(e => _nodeViewModels.Add(_nodeVmFactory.NodeViewModel(this, e.EventArgs.Value))) .DisposeWith(d); Observable.FromEventPattern>(x => NodeScript.NodeRemoved += x, x => NodeScript.NodeRemoved -= x) - .Subscribe(e => HandleNodeRemoved(e.EventArgs)) + .Subscribe(e => _nodeViewModels.RemoveMany(_nodeViewModels.Items.Where(n => n.Node == e.EventArgs.Value))) .DisposeWith(d); }); @@ -276,16 +276,4 @@ public class NodeScriptViewModel : ActivatableViewModelBase private void ExecutePasteSelected() { } - - private void HandleNodeAdded(SingleValueEventArgs eventArgs) - { - _nodeViewModels.Add(_nodeVmFactory.NodeViewModel(this, eventArgs.Value)); - } - - private void HandleNodeRemoved(SingleValueEventArgs eventArgs) - { - NodeViewModel? toRemove = NodeViewModels.FirstOrDefault(vm => ReferenceEquals(vm.Node, eventArgs.Value)); - if (toRemove != null) - _nodeViewModels.Remove(toRemove); - } } \ No newline at end of file diff --git a/src/Artemis.UI/Screens/VisualScripting/NodeViewModel.cs b/src/Artemis.UI/Screens/VisualScripting/NodeViewModel.cs index e203356b7..e8777e411 100644 --- a/src/Artemis.UI/Screens/VisualScripting/NodeViewModel.cs +++ b/src/Artemis.UI/Screens/VisualScripting/NodeViewModel.cs @@ -61,10 +61,10 @@ public class NodeViewModel : ActivatableViewModelBase nodePins.Connect().Merge(nodePinCollections.Connect().TransformMany(c => c.PinViewModels)).Bind(out ReadOnlyObservableCollection pins).Subscribe(); PinViewModels = pins; - + DeleteNode = ReactiveCommand.Create(ExecuteDeleteNode, this.WhenAnyValue(vm => vm.IsStaticNode).Select(v => !v)); ShowBrokenState = ReactiveCommand.Create(ExecuteShowBrokenState); - + this.WhenActivated(d => { _isStaticNode = Node.WhenAnyValue(n => n.IsDefaultNode, n => n.IsExitNode) @@ -93,7 +93,7 @@ public class NodeViewModel : ActivatableViewModelBase }) .DisposeWith(d); Observable.FromEventPattern>(x => Node.PinRemoved += x, x => Node.PinRemoved -= x) - .Subscribe(p => nodePins!.Remove(nodePins.Items.FirstOrDefault(vm => vm.Pin == p.EventArgs.Value))) + .Subscribe(p => nodePins.RemoveMany(nodePins.Items.Where(vm => vm.Pin == p.EventArgs.Value))) .DisposeWith(d); nodePins.Edit(l => { @@ -118,7 +118,7 @@ public class NodeViewModel : ActivatableViewModelBase }) .DisposeWith(d); Observable.FromEventPattern>(x => Node.PinCollectionRemoved += x, x => Node.PinCollectionRemoved -= x) - .Subscribe(p => nodePinCollections!.Remove(nodePinCollections.Items.FirstOrDefault(vm => vm.PinCollection == p.EventArgs.Value))) + .Subscribe(p => nodePinCollections.RemoveMany(nodePinCollections.Items.Where(vm => vm.PinCollection == p.EventArgs.Value))) .DisposeWith(d); nodePinCollections.Edit(l => { @@ -160,8 +160,8 @@ public class NodeViewModel : ActivatableViewModelBase get => _isSelected; set => RaiseAndSetIfChanged(ref _isSelected, value); } - - public ReactiveCommand ShowBrokenState { get; } + + public ReactiveCommand ShowBrokenState { get; } public ReactiveCommand DeleteNode { get; } public void StartDrag(Point mouseStartPosition) @@ -195,7 +195,7 @@ public class NodeViewModel : ActivatableViewModelBase { _nodeEditorService.ExecuteCommand(NodeScriptViewModel.NodeScript, new DeleteNode(NodeScriptViewModel.NodeScript, Node)); } - + private void ExecuteShowBrokenState() { if (Node.BrokenState != null && Node.BrokenStateException != null) diff --git a/src/Artemis.UI/Screens/VisualScripting/Pins/PinCollectionViewModel.cs b/src/Artemis.UI/Screens/VisualScripting/Pins/PinCollectionViewModel.cs index 782e80131..d6e160dc4 100644 --- a/src/Artemis.UI/Screens/VisualScripting/Pins/PinCollectionViewModel.cs +++ b/src/Artemis.UI/Screens/VisualScripting/Pins/PinCollectionViewModel.cs @@ -30,7 +30,7 @@ public abstract class PinCollectionViewModel : ActivatableViewModelBase .Subscribe(e => PinViewModels.Add(CreatePinViewModel(e.EventArgs.Value))) .DisposeWith(d); Observable.FromEventPattern>(x => PinCollection.PinRemoved += x, x => PinCollection.PinRemoved -= x) - .Subscribe(e => PinViewModels.RemoveMany(PinViewModels.Where(p => p.Pin == e.EventArgs.Value).ToList())) + .Subscribe(e => PinViewModels.RemoveMany(PinViewModels.Where(p => p.Pin == e.EventArgs.Value))) .DisposeWith(d); }); diff --git a/src/Artemis.UI/Screens/VisualScripting/Pins/PinViewModel.cs b/src/Artemis.UI/Screens/VisualScripting/Pins/PinViewModel.cs index 8eae87607..37542daca 100644 --- a/src/Artemis.UI/Screens/VisualScripting/Pins/PinViewModel.cs +++ b/src/Artemis.UI/Screens/VisualScripting/Pins/PinViewModel.cs @@ -1,4 +1,5 @@ using System; +using System.Linq; using System.Reactive; using System.Reactive.Linq; using Artemis.Core; @@ -38,7 +39,7 @@ public abstract class PinViewModel : ActivatableViewModelBase .Subscribe(e => connectedPins.Add(e.EventArgs.Value)) .DisposeWith(d); Observable.FromEventPattern>(x => Pin.PinDisconnected += x, x => Pin.PinDisconnected -= x) - .Subscribe(e => connectedPins.Remove(e.EventArgs.Value)) + .Subscribe(e => connectedPins.RemoveMany(connectedPins.Items.Where(p => p == e.EventArgs.Value))) .DisposeWith(d); Pin.WhenAnyValue(p => p.Type).Subscribe(_ => UpdatePinColor()).DisposeWith(d); });