mirror of
https://github.com/Artemis-RGB/Artemis
synced 2025-12-13 05:48:35 +00:00
Visual Scripting: Fixed cables not beeing shown after changing the script
This commit is contained in:
parent
1e67c1c567
commit
51541b4e6e
@ -1,4 +1,5 @@
|
|||||||
using System;
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
using Artemis.VisualScripting.Model;
|
using Artemis.VisualScripting.Model;
|
||||||
|
|
||||||
namespace Artemis.Core.VisualScripting
|
namespace Artemis.Core.VisualScripting
|
||||||
@ -12,6 +13,8 @@ namespace Artemis.Core.VisualScripting
|
|||||||
Type Type { get; }
|
Type Type { get; }
|
||||||
object PinValue { get; }
|
object PinValue { get; }
|
||||||
|
|
||||||
|
IReadOnlyList<IPin> ConnectedTo { get; }
|
||||||
|
|
||||||
bool IsEvaluated { get; set; }
|
bool IsEvaluated { get; set; }
|
||||||
|
|
||||||
void ConnectTo(IPin pin);
|
void ConnectTo(IPin pin);
|
||||||
|
|||||||
@ -213,12 +213,15 @@ namespace Artemis.VisualScripting.Editor.Controls
|
|||||||
InitializeNode(node);
|
InitializeNode(node);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
VisualScript?.RecreateCables();
|
||||||
|
|
||||||
CenterAt(new Vector(0, 0));
|
CenterAt(new Vector(0, 0));
|
||||||
}
|
}
|
||||||
|
|
||||||
private void OnVisualScriptPropertyChanged(object sender, PropertyChangedEventArgs args)
|
private void OnVisualScriptPropertyChanged(object sender, PropertyChangedEventArgs args)
|
||||||
{
|
{
|
||||||
if (args.PropertyName == nameof(VisualScript.Cables))
|
if (args.PropertyName == nameof(VisualScript.Cables))
|
||||||
|
if (_cableList != null)
|
||||||
_cableList.ItemsSource = VisualScript.Cables;
|
_cableList.ItemsSource = VisualScript.Cables;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -44,10 +44,10 @@ namespace Artemis.VisualScripting.Editor.Controls.Wrapper
|
|||||||
|
|
||||||
public ObservableCollection<VisualScriptNode> Nodes { get; } = new();
|
public ObservableCollection<VisualScriptNode> Nodes { get; } = new();
|
||||||
|
|
||||||
public IEnumerable<VisualScriptCable> Cables => Nodes.SelectMany(n => n.InputPins.SelectMany(p => p.Connections))
|
public IEnumerable<VisualScriptCable> Cables => Nodes.SelectMany(n => n.InputPins.SelectMany(p => p.InternalConnections))
|
||||||
.Concat(Nodes.SelectMany(n => n.OutputPins.SelectMany(p => p.Connections)))
|
.Concat(Nodes.SelectMany(n => n.OutputPins.SelectMany(p => p.InternalConnections)))
|
||||||
.Concat(Nodes.SelectMany(n => n.InputPinCollections.SelectMany(p => p.Pins).SelectMany(p => p.Connections)))
|
.Concat(Nodes.SelectMany(n => n.InputPinCollections.SelectMany(p => p.Pins).SelectMany(p => p.InternalConnections)))
|
||||||
.Concat(Nodes.SelectMany(n => n.OutputPinCollections.SelectMany(p => p.Pins).SelectMany(p => p.Connections)))
|
.Concat(Nodes.SelectMany(n => n.OutputPinCollections.SelectMany(p => p.Pins).SelectMany(p => p.InternalConnections)))
|
||||||
.Distinct();
|
.Distinct();
|
||||||
|
|
||||||
public bool IsConnecting => IsConnectingPin != null;
|
public bool IsConnecting => IsConnectingPin != null;
|
||||||
@ -184,6 +184,36 @@ namespace Artemis.VisualScripting.Editor.Controls.Wrapper
|
|||||||
Script.RemoveNode(node.Node);
|
Script.RemoveNode(node.Node);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
internal void RecreateCables()
|
||||||
|
{
|
||||||
|
Dictionary<IPin, VisualScriptPin> pinMapping = Nodes.SelectMany(n => n.InputPins)
|
||||||
|
.Concat(Nodes.SelectMany(n => n.OutputPins))
|
||||||
|
.Concat(Nodes.SelectMany(n => n.InputPinCollections.SelectMany(p => p.Pins)))
|
||||||
|
.Concat(Nodes.SelectMany(n => n.OutputPinCollections.SelectMany(p => p.Pins)))
|
||||||
|
.ToDictionary(p => p.Pin, p => p);
|
||||||
|
|
||||||
|
foreach (VisualScriptPin pin in pinMapping.Values)
|
||||||
|
pin.InternalConnections.Clear();
|
||||||
|
|
||||||
|
HashSet<IPin> connectedPins = new();
|
||||||
|
foreach (IPin pin in pinMapping.Keys)
|
||||||
|
{
|
||||||
|
foreach (IPin connectedPin in pin.ConnectedTo)
|
||||||
|
if (!connectedPins.Contains(connectedPin))
|
||||||
|
{
|
||||||
|
VisualScriptPin pin1 = pinMapping[pin];
|
||||||
|
VisualScriptPin pin2 = pinMapping[connectedPin];
|
||||||
|
VisualScriptCable cable = new(pin1, pin2, false);
|
||||||
|
pin1.InternalConnections.Add(cable);
|
||||||
|
pin2.InternalConnections.Add(cable);
|
||||||
|
}
|
||||||
|
|
||||||
|
connectedPins.Add(pin);
|
||||||
|
}
|
||||||
|
|
||||||
|
OnPropertyChanged(nameof(Cables));
|
||||||
|
}
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -28,6 +28,10 @@ namespace Artemis.VisualScripting.Editor.Controls.Wrapper
|
|||||||
#region Constructors
|
#region Constructors
|
||||||
|
|
||||||
public VisualScriptCable(VisualScriptPin pin1, VisualScriptPin pin2)
|
public VisualScriptCable(VisualScriptPin pin1, VisualScriptPin pin2)
|
||||||
|
: this(pin1, pin2, true)
|
||||||
|
{ }
|
||||||
|
|
||||||
|
internal VisualScriptCable(VisualScriptPin pin1, VisualScriptPin pin2, bool connect)
|
||||||
{
|
{
|
||||||
if ((pin1.Pin.Direction == PinDirection.Input) && (pin2.Pin.Direction == PinDirection.Input))
|
if ((pin1.Pin.Direction == PinDirection.Input) && (pin2.Pin.Direction == PinDirection.Input))
|
||||||
throw new ArgumentException("Can't connect two input pins.");
|
throw new ArgumentException("Can't connect two input pins.");
|
||||||
@ -38,9 +42,12 @@ namespace Artemis.VisualScripting.Editor.Controls.Wrapper
|
|||||||
From = pin1.Pin.Direction == PinDirection.Output ? pin1 : pin2;
|
From = pin1.Pin.Direction == PinDirection.Output ? pin1 : pin2;
|
||||||
To = pin1.Pin.Direction == PinDirection.Input ? pin1 : pin2;
|
To = pin1.Pin.Direction == PinDirection.Input ? pin1 : pin2;
|
||||||
|
|
||||||
|
if (connect)
|
||||||
|
{
|
||||||
From.ConnectTo(this);
|
From.ConnectTo(this);
|
||||||
To.ConnectTo(this);
|
To.ConnectTo(this);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
|||||||
@ -26,7 +26,8 @@ namespace Artemis.VisualScripting.Editor.Controls.Wrapper
|
|||||||
public VisualScriptNode Node { get; }
|
public VisualScriptNode Node { get; }
|
||||||
public IPin Pin { get; }
|
public IPin Pin { get; }
|
||||||
|
|
||||||
public ObservableCollection<VisualScriptCable> Connections { get; } = new();
|
public IReadOnlyCollection<VisualScriptCable> Connections => new ReadOnlyCollection<VisualScriptCable>(InternalConnections);
|
||||||
|
internal ObservableCollection<VisualScriptCable> InternalConnections { get; } = new();
|
||||||
|
|
||||||
private Point _absolutePosition;
|
private Point _absolutePosition;
|
||||||
public Point AbsolutePosition
|
public Point AbsolutePosition
|
||||||
@ -39,7 +40,8 @@ namespace Artemis.VisualScripting.Editor.Controls.Wrapper
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public Point AbsoluteCableTargetPosition => Pin.Direction == PinDirection.Input ? new Point(AbsolutePosition.X - CABLE_OFFSET, AbsolutePosition.Y) : new Point(AbsolutePosition.X + CABLE_OFFSET, AbsolutePosition.Y);
|
public Point AbsoluteCableTargetPosition => Pin.Direction == PinDirection.Input ? new Point(AbsolutePosition.X - CABLE_OFFSET, AbsolutePosition.Y)
|
||||||
|
: new Point(AbsolutePosition.X + CABLE_OFFSET, AbsolutePosition.Y);
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
@ -62,7 +64,8 @@ namespace Artemis.VisualScripting.Editor.Controls.Wrapper
|
|||||||
if (_isConnectingCable != null)
|
if (_isConnectingCable != null)
|
||||||
SetConnecting(false);
|
SetConnecting(false);
|
||||||
|
|
||||||
_isConnectingPin = new VisualScriptPin(null, new IsConnectingPin(Pin.Direction == PinDirection.Input ? PinDirection.Output : PinDirection.Input)) { AbsolutePosition = AbsolutePosition };
|
_isConnectingPin = new VisualScriptPin(null, new IsConnectingPin(Pin.Direction == PinDirection.Input ? PinDirection.Output : PinDirection.Input, Pin.Type))
|
||||||
|
{ AbsolutePosition = AbsolutePosition };
|
||||||
_isConnectingCable = new VisualScriptCable(this, _isConnectingPin);
|
_isConnectingCable = new VisualScriptCable(this, _isConnectingPin);
|
||||||
Node.OnIsConnectingPinChanged(_isConnectingPin);
|
Node.OnIsConnectingPinChanged(_isConnectingPin);
|
||||||
}
|
}
|
||||||
@ -77,16 +80,16 @@ namespace Artemis.VisualScripting.Editor.Controls.Wrapper
|
|||||||
|
|
||||||
internal void ConnectTo(VisualScriptCable cable)
|
internal void ConnectTo(VisualScriptCable cable)
|
||||||
{
|
{
|
||||||
if (Connections.Contains(cable)) return;
|
if (InternalConnections.Contains(cable)) return;
|
||||||
|
|
||||||
if (Pin.Direction == PinDirection.Input)
|
if (Pin.Direction == PinDirection.Input)
|
||||||
{
|
{
|
||||||
List<VisualScriptCable> cables = Connections.ToList();
|
List<VisualScriptCable> cables = InternalConnections.ToList();
|
||||||
foreach (VisualScriptCable c in cables)
|
foreach (VisualScriptCable c in cables)
|
||||||
c.Disconnect();
|
c.Disconnect();
|
||||||
}
|
}
|
||||||
|
|
||||||
Connections.Add(cable);
|
InternalConnections.Add(cable);
|
||||||
Pin.ConnectTo(cable.GetConnectedPin(Pin));
|
Pin.ConnectTo(cable.GetConnectedPin(Pin));
|
||||||
|
|
||||||
Node?.OnPinConnected(new PinConnectedEventArgs(this, cable));
|
Node?.OnPinConnected(new PinConnectedEventArgs(this, cable));
|
||||||
@ -94,14 +97,14 @@ namespace Artemis.VisualScripting.Editor.Controls.Wrapper
|
|||||||
|
|
||||||
public void DisconnectAll()
|
public void DisconnectAll()
|
||||||
{
|
{
|
||||||
List<VisualScriptCable> cables = Connections.ToList();
|
List<VisualScriptCable> cables = InternalConnections.ToList();
|
||||||
foreach (VisualScriptCable cable in cables)
|
foreach (VisualScriptCable cable in cables)
|
||||||
cable.Disconnect();
|
cable.Disconnect();
|
||||||
}
|
}
|
||||||
|
|
||||||
internal void Disconnect(VisualScriptCable cable)
|
internal void Disconnect(VisualScriptCable cable)
|
||||||
{
|
{
|
||||||
Connections.Remove(cable);
|
InternalConnections.Remove(cable);
|
||||||
Pin.DisconnectFrom(cable.GetConnectedPin(Pin));
|
Pin.DisconnectFrom(cable.GetConnectedPin(Pin));
|
||||||
|
|
||||||
Node?.OnPinDisconnected(new PinDisconnectedEventArgs(this, cable));
|
Node?.OnPinDisconnected(new PinDisconnectedEventArgs(this, cable));
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user