1
0
mirror of https://github.com/Artemis-RGB/Artemis synced 2025-12-13 05:48:35 +00:00

Node editor - Added undo/redo to event and datamodel nodes

This commit is contained in:
Robert 2022-03-25 20:21:45 +01:00
parent f824f16658
commit 06ab2c5bb6
17 changed files with 609 additions and 421 deletions

View File

@ -4,353 +4,364 @@ using System.Collections.ObjectModel;
using Ninject;
using Ninject.Parameters;
namespace Artemis.Core
namespace Artemis.Core;
/// <summary>
/// Represents a kind of node inside a <see cref="NodeScript" />
/// </summary>
public abstract class Node : CorePropertyChanged, INode
{
/// <summary>
/// Represents a kind of node inside a <see cref="NodeScript" />
/// </summary>
public abstract class Node : CorePropertyChanged, INode
/// <inheritdoc />
public event EventHandler? Resetting;
#region Properties & Fields
private string _name;
/// <inheritdoc />
public string Name
{
/// <inheritdoc />
public event EventHandler? Resetting;
get => _name;
protected set => SetAndNotify(ref _name, value);
}
#region Properties & Fields
private string _description;
private string _name;
/// <inheritdoc />
public string Description
{
get => _description;
protected set => SetAndNotify(ref _description, value);
}
/// <inheritdoc />
public string Name
private double _x;
/// <inheritdoc />
public double X
{
get => _x;
set => SetAndNotify(ref _x, value);
}
private double _y;
/// <inheritdoc />
public double Y
{
get => _y;
set => SetAndNotify(ref _y, value);
}
/// <inheritdoc />
public virtual bool IsExitNode => false;
/// <inheritdoc />
public virtual bool IsDefaultNode => false;
private readonly List<IPin> _pins = new();
/// <inheritdoc />
public IReadOnlyCollection<IPin> Pins => new ReadOnlyCollection<IPin>(_pins);
private readonly List<IPinCollection> _pinCollections = new();
/// <inheritdoc />
public IReadOnlyCollection<IPinCollection> PinCollections => new ReadOnlyCollection<IPinCollection>(_pinCollections);
#endregion
#region Construtors
/// <summary>
/// Creates a new instance of the <see cref="Node" /> class with an empty name and description
/// </summary>
protected Node()
{
_name = string.Empty;
_description = string.Empty;
}
/// <summary>
/// Creates a new instance of the <see cref="Node" /> class with the provided name and description
/// </summary>
protected Node(string name, string description)
{
_name = name;
_description = description;
}
#endregion
#region Methods
/// <summary>
/// Creates a new input pin and adds it to the <see cref="Pins" /> collection
/// </summary>
/// <param name="name">The name of the pin</param>
/// <typeparam name="T">The type of value the pin will hold</typeparam>
/// <returns>The newly created pin</returns>
protected InputPin<T> CreateInputPin<T>(string name = "")
{
InputPin<T> pin = new(this, name);
_pins.Add(pin);
OnPropertyChanged(nameof(Pins));
return pin;
}
/// <summary>
/// Creates a new input pin and adds it to the <see cref="Pins" /> collection
/// </summary>
/// <param name="type">The type of value the pin will hold</param>
/// <param name="name">The name of the pin</param>
/// <returns>The newly created pin</returns>
protected InputPin CreateInputPin(Type type, string name = "")
{
InputPin pin = new(this, type, name);
_pins.Add(pin);
OnPropertyChanged(nameof(Pins));
return pin;
}
/// <summary>
/// Creates a new output pin and adds it to the <see cref="Pins" /> collection
/// </summary>
/// <param name="name">The name of the pin</param>
/// <typeparam name="T">The type of value the pin will hold</typeparam>
/// <returns>The newly created pin</returns>
protected OutputPin<T> CreateOutputPin<T>(string name = "")
{
OutputPin<T> pin = new(this, name);
_pins.Add(pin);
OnPropertyChanged(nameof(Pins));
return pin;
}
/// <summary>
/// Creates a new output pin and adds it to the <see cref="Pins" /> collection
/// </summary>
/// <param name="type">The type of value the pin will hold</param>
/// <param name="name">The name of the pin</param>
/// <returns>The newly created pin</returns>
protected OutputPin CreateOutputPin(Type type, string name = "")
{
OutputPin pin = new(this, type, name);
_pins.Add(pin);
OnPropertyChanged(nameof(Pins));
return pin;
}
/// <summary>
/// Removes the provided <paramref name="pin" /> from the node and it's <see cref="Pins" /> collection
/// </summary>
/// <param name="pin">The pin to remove</param>
/// <returns><see langword="true" /> if the pin was removed; otherwise <see langword="false" />.</returns>
protected bool RemovePin(Pin pin)
{
bool isRemoved = _pins.Remove(pin);
if (isRemoved)
{
get => _name;
protected set => SetAndNotify(ref _name, value);
}
private string _description;
/// <inheritdoc />
public string Description
{
get => _description;
protected set => SetAndNotify(ref _description, value);
}
private double _x;
/// <inheritdoc />
public double X
{
get => _x;
set => SetAndNotify(ref _x, value);
}
private double _y;
/// <inheritdoc />
public double Y
{
get => _y;
set => SetAndNotify(ref _y, value);
}
/// <inheritdoc />
public virtual bool IsExitNode => false;
/// <inheritdoc />
public virtual bool IsDefaultNode => false;
private readonly List<IPin> _pins = new();
/// <inheritdoc />
public IReadOnlyCollection<IPin> Pins => new ReadOnlyCollection<IPin>(_pins);
private readonly List<IPinCollection> _pinCollections = new();
/// <inheritdoc />
public IReadOnlyCollection<IPinCollection> PinCollections => new ReadOnlyCollection<IPinCollection>(_pinCollections);
#endregion
#region Construtors
/// <summary>
/// Creates a new instance of the <see cref="Node" /> class with an empty name and description
/// </summary>
protected Node()
{
_name = string.Empty;
_description = string.Empty;
}
/// <summary>
/// Creates a new instance of the <see cref="Node" /> class with the provided name and description
/// </summary>
protected Node(string name, string description)
{
_name = name;
_description = description;
}
#endregion
#region Methods
/// <summary>
/// Creates a new input pin and adds it to the <see cref="Pins" /> collection
/// </summary>
/// <param name="name">The name of the pin</param>
/// <typeparam name="T">The type of value the pin will hold</typeparam>
/// <returns>The newly created pin</returns>
protected InputPin<T> CreateInputPin<T>(string name = "")
{
InputPin<T> pin = new(this, name);
_pins.Add(pin);
pin.DisconnectAll();
OnPropertyChanged(nameof(Pins));
return pin;
}
/// <summary>
/// Creates a new input pin and adds it to the <see cref="Pins" /> collection
/// </summary>
/// <param name="type">The type of value the pin will hold</param>
/// <param name="name">The name of the pin</param>
/// <returns>The newly created pin</returns>
protected InputPin CreateInputPin(Type type, string name = "")
{
InputPin pin = new(this, type, name);
_pins.Add(pin);
OnPropertyChanged(nameof(Pins));
return pin;
}
return isRemoved;
}
/// <summary>
/// Creates a new output pin and adds it to the <see cref="Pins" /> collection
/// </summary>
/// <param name="name">The name of the pin</param>
/// <typeparam name="T">The type of value the pin will hold</typeparam>
/// <returns>The newly created pin</returns>
protected OutputPin<T> CreateOutputPin<T>(string name = "")
{
OutputPin<T> pin = new(this, name);
_pins.Add(pin);
OnPropertyChanged(nameof(Pins));
return pin;
}
/// <summary>
/// Creates a new input pin collection and adds it to the <see cref="PinCollections" /> collection
/// </summary>
/// <typeparam name="T">The type of value the pins of this collection will hold</typeparam>
/// <param name="name">The name of the pin collection</param>
/// <param name="initialCount">The amount of pins to initially add to the collection</param>
/// <returns>The resulting input pin collection</returns>
protected InputPinCollection<T> CreateInputPinCollection<T>(string name = "", int initialCount = 1)
{
InputPinCollection<T> pin = new(this, name, initialCount);
_pinCollections.Add(pin);
OnPropertyChanged(nameof(PinCollections));
return pin;
}
/// <summary>
/// Creates a new output pin and adds it to the <see cref="Pins" /> collection
/// </summary>
/// <param name="type">The type of value the pin will hold</param>
/// <param name="name">The name of the pin</param>
/// <returns>The newly created pin</returns>
protected OutputPin CreateOutputPin(Type type, string name = "")
{
OutputPin pin = new(this, type, name);
_pins.Add(pin);
OnPropertyChanged(nameof(Pins));
return pin;
}
/// <summary>
/// Creates a new input pin collection and adds it to the <see cref="PinCollections" /> collection
/// </summary>
/// <param name="type">The type of value the pins of this collection will hold</param>
/// <param name="name">The name of the pin collection</param>
/// <param name="initialCount">The amount of pins to initially add to the collection</param>
/// <returns>The resulting input pin collection</returns>
protected InputPinCollection CreateInputPinCollection(Type type, string name = "", int initialCount = 1)
{
InputPinCollection pin = new(this, type, name, initialCount);
_pinCollections.Add(pin);
OnPropertyChanged(nameof(PinCollections));
return pin;
}
/// <summary>
/// Removes the provided <paramref name="pin" /> from the node and it's <see cref="Pins" /> collection
/// </summary>
/// <param name="pin">The pin to remove</param>
/// <returns><see langword="true" /> if the pin was removed; otherwise <see langword="false" />.</returns>
protected bool RemovePin(Pin pin)
/// <summary>
/// Creates a new output pin collection and adds it to the <see cref="PinCollections" /> collection
/// </summary>
/// <typeparam name="T">The type of value the pins of this collection will hold</typeparam>
/// <param name="name">The name of the pin collection</param>
/// <param name="initialCount">The amount of pins to initially add to the collection</param>
/// <returns>The resulting output pin collection</returns>
protected OutputPinCollection<T> CreateOutputPinCollection<T>(string name = "", int initialCount = 1)
{
OutputPinCollection<T> pin = new(this, name, initialCount);
_pinCollections.Add(pin);
OnPropertyChanged(nameof(PinCollections));
return pin;
}
/// <summary>
/// Removes the provided <paramref name="pinCollection" /> from the node and it's <see cref="PinCollections" />
/// collection
/// </summary>
/// <param name="pinCollection">The pin collection to remove</param>
/// <returns><see langword="true" /> if the pin collection was removed; otherwise <see langword="false" />.</returns>
protected bool RemovePinCollection(PinCollection pinCollection)
{
bool isRemoved = _pinCollections.Remove(pinCollection);
if (isRemoved)
{
bool isRemoved = _pins.Remove(pin);
if (isRemoved)
{
foreach (IPin pin in pinCollection)
pin.DisconnectAll();
OnPropertyChanged(nameof(Pins));
}
return isRemoved;
}
/// <summary>
/// Creates a new input pin collection and adds it to the <see cref="PinCollections" /> collection
/// </summary>
/// <typeparam name="T">The type of value the pins of this collection will hold</typeparam>
/// <param name="name">The name of the pin collection</param>
/// <param name="initialCount">The amount of pins to initially add to the collection</param>
/// <returns>The resulting input pin collection</returns>
protected InputPinCollection<T> CreateInputPinCollection<T>(string name = "", int initialCount = 1)
{
InputPinCollection<T> pin = new(this, name, initialCount);
_pinCollections.Add(pin);
OnPropertyChanged(nameof(PinCollections));
return pin;
}
/// <summary>
/// Creates a new input pin collection and adds it to the <see cref="PinCollections" /> collection
/// </summary>
/// <param name="type">The type of value the pins of this collection will hold</param>
/// <param name="name">The name of the pin collection</param>
/// <param name="initialCount">The amount of pins to initially add to the collection</param>
/// <returns>The resulting input pin collection</returns>
protected InputPinCollection CreateInputPinCollection(Type type, string name = "", int initialCount = 1)
{
InputPinCollection pin = new(this, type, name, initialCount);
_pinCollections.Add(pin);
OnPropertyChanged(nameof(PinCollections));
return pin;
}
return isRemoved;
}
/// <summary>
/// Creates a new output pin collection and adds it to the <see cref="PinCollections" /> collection
/// </summary>
/// <typeparam name="T">The type of value the pins of this collection will hold</typeparam>
/// <param name="name">The name of the pin collection</param>
/// <param name="initialCount">The amount of pins to initially add to the collection</param>
/// <returns>The resulting output pin collection</returns>
protected OutputPinCollection<T> CreateOutputPinCollection<T>(string name = "", int initialCount = 1)
{
OutputPinCollection<T> pin = new(this, name, initialCount);
_pinCollections.Add(pin);
OnPropertyChanged(nameof(PinCollections));
return pin;
}
/// <inheritdoc />
public virtual void Initialize(INodeScript script)
{
}
/// <summary>
/// Removes the provided <paramref name="pinCollection" /> from the node and it's <see cref="PinCollections" />
/// collection
/// </summary>
/// <param name="pinCollection">The pin collection to remove</param>
/// <returns><see langword="true" /> if the pin collection was removed; otherwise <see langword="false" />.</returns>
protected bool RemovePinCollection(PinCollection pinCollection)
{
bool isRemoved = _pinCollections.Remove(pinCollection);
if (isRemoved)
{
foreach (IPin pin in pinCollection)
pin.DisconnectAll();
OnPropertyChanged(nameof(PinCollections));
}
/// <inheritdoc />
public abstract void Evaluate();
return isRemoved;
}
/// <inheritdoc />
public virtual void Reset()
{
foreach (IPin pin in _pins)
pin.Reset();
foreach (IPinCollection pinCollection in _pinCollections)
pinCollection.Reset();
/// <inheritdoc />
public virtual void Initialize(INodeScript script)
{
}
/// <inheritdoc />
public abstract void Evaluate();
/// <inheritdoc />
public virtual void Reset()
{
foreach (IPin pin in _pins)
pin.Reset();
foreach (IPinCollection pinCollection in _pinCollections)
pinCollection.Reset();
Resetting?.Invoke(this, EventArgs.Empty);
}
/// <summary>
/// Called whenever the node must show it's custom view model, if <see langword="null" />, no custom view model is used
/// </summary>
/// <returns>The custom view model, if <see langword="null" />, no custom view model is used</returns>
public virtual ICustomNodeViewModel? GetCustomViewModel()
{
return null;
}
/// <summary>
/// Serializes the <see cref="Storage" /> object into a string
/// </summary>
/// <returns>The serialized object</returns>
internal virtual string SerializeStorage()
{
return string.Empty;
}
/// <summary>
/// Deserializes the <see cref="Storage" /> object and sets it
/// </summary>
/// <param name="serialized">The serialized object</param>
internal virtual void DeserializeStorage(string serialized)
{
}
#endregion
Resetting?.Invoke(this, EventArgs.Empty);
}
/// <summary>
/// Represents a kind of node inside a <see cref="NodeScript" /> containing storage value of type
/// <typeparamref name="TStorage" />.
/// Called whenever the node must show it's custom view model, if <see langword="null" />, no custom view model is used
/// </summary>
/// <typeparam name="TStorage">The type of value the node stores</typeparam>
public abstract class Node<TStorage> : Node
/// <param name="nodeScript"></param>
/// <returns>The custom view model, if <see langword="null" />, no custom view model is used</returns>
public virtual ICustomNodeViewModel? GetCustomViewModel(NodeScript nodeScript)
{
private TStorage? _storage;
return null;
}
/// <inheritdoc />
protected Node()
{
}
/// <summary>
/// Serializes the <see cref="Storage" /> object into a string
/// </summary>
/// <returns>The serialized object</returns>
internal virtual string SerializeStorage()
{
return string.Empty;
}
/// <inheritdoc />
protected Node(string name, string description) : base(name, description)
{
}
/// <summary>
/// Deserializes the <see cref="Storage" /> object and sets it
/// </summary>
/// <param name="serialized">The serialized object</param>
internal virtual void DeserializeStorage(string serialized)
{
}
/// <summary>
/// Gets or sets the storage object of this node, this is saved across sessions
/// </summary>
public TStorage? Storage
{
get => _storage;
set => SetAndNotify(ref _storage, value);
}
#endregion
}
internal override string SerializeStorage()
{
return CoreJson.SerializeObject(Storage, true);
}
/// <summary>
/// Represents a kind of node inside a <see cref="NodeScript" /> containing storage value of type
/// <typeparamref name="TStorage" />.
/// </summary>
/// <typeparam name="TStorage">The type of value the node stores</typeparam>
public abstract class Node<TStorage> : Node
{
private TStorage? _storage;
internal override void DeserializeStorage(string serialized)
/// <inheritdoc />
protected Node()
{
}
/// <inheritdoc />
protected Node(string name, string description) : base(name, description)
{
}
/// <summary>
/// Gets or sets the storage object of this node, this is saved across sessions
/// </summary>
public TStorage? Storage
{
get => _storage;
set
{
Storage = CoreJson.DeserializeObject<TStorage>(serialized) ?? default(TStorage);
if (SetAndNotify(ref _storage, value))
StorageModified?.Invoke(this, EventArgs.Empty);
}
}
/// <summary>
/// Represents a kind of node inside a <see cref="NodeScript" /> containing storage value of type
/// <typeparamref name="TStorage" /> and a view model of type <typeparamref name="TViewModel" />.
/// Occurs whenever the storage of this node was modified.
/// </summary>
/// <typeparam name="TStorage">The type of value the node stores</typeparam>
/// <typeparam name="TViewModel">The type of view model the node uses</typeparam>
public abstract class Node<TStorage, TViewModel> : Node<TStorage> where TViewModel : ICustomNodeViewModel
public event EventHandler? StorageModified;
internal override string SerializeStorage()
{
/// <inheritdoc />
protected Node()
{
}
return CoreJson.SerializeObject(Storage, true);
}
/// <inheritdoc />
protected Node(string name, string description) : base(name, description)
{
}
internal override void DeserializeStorage(string serialized)
{
Storage = CoreJson.DeserializeObject<TStorage>(serialized) ?? default(TStorage);
}
}
[Inject]
internal IKernel Kernel { get; set; } = null!;
/// <summary>
/// Represents a kind of node inside a <see cref="NodeScript" /> containing storage value of type
/// <typeparamref name="TStorage" /> and a view model of type <typeparamref name="TViewModel" />.
/// </summary>
/// <typeparam name="TStorage">The type of value the node stores</typeparam>
/// <typeparam name="TViewModel">The type of view model the node uses</typeparam>
public abstract class Node<TStorage, TViewModel> : Node<TStorage> where TViewModel : ICustomNodeViewModel
{
/// <inheritdoc />
protected Node()
{
}
/// <summary>
/// Called when a view model is required
/// </summary>
public virtual TViewModel GetViewModel()
{
return Kernel.Get<TViewModel>(new ConstructorArgument("node", this));
}
/// <inheritdoc />
protected Node(string name, string description) : base(name, description)
{
}
/// <inheritdoc />
public override ICustomNodeViewModel GetCustomViewModel()
{
return GetViewModel();
}
[Inject]
internal IKernel Kernel { get; set; } = null!;
/// <summary>
/// Called when a view model is required
/// </summary>
/// <param name="nodeScript"></param>
public virtual TViewModel GetViewModel(NodeScript nodeScript)
{
return Kernel.Get<TViewModel>(new ConstructorArgument("node", this), new ConstructorArgument("script", nodeScript));
}
/// <param name="nodeScript"></param>
/// <inheritdoc />
public override ICustomNodeViewModel? GetCustomViewModel(NodeScript nodeScript)
{
return GetViewModel(nodeScript);
}
}

View File

@ -0,0 +1,72 @@
using System;
using Artemis.Core;
namespace Artemis.UI.Shared.Services.NodeEditor.Commands;
/// <summary>
/// Represents a node editor command that can be used to update the storage value of a node.
/// </summary>
/// <typeparam name="TStorage">The type of value the node stores</typeparam>
public class UpdateStorage<TStorage> : INodeEditorCommand, IDisposable
{
private readonly Node<TStorage> _node;
private readonly TStorage? _originalValue;
private readonly TStorage? _value;
private readonly string _valueDescription;
private bool _updatedValue;
/// <summary>
/// Creates a new instance of the <see cref="UpdateStorage{T}" /> class.
/// </summary>
/// <param name="node">The node to update.</param>
/// <param name="value">The new value of the node.</param>
/// <param name="valueDescription">The description of the value that was updated.</param>
public UpdateStorage(Node<TStorage> node, TStorage? value, string valueDescription = "value")
{
_node = node;
_value = value;
_valueDescription = valueDescription;
_originalValue = _node.Storage;
}
#region Implementation of INodeEditorCommand
/// <inheritdoc />
public string DisplayName => $"Update node {_valueDescription}";
/// <inheritdoc />
public void Execute()
{
_updatedValue = true;
_node.Storage = _value;
}
/// <inheritdoc />
public void Undo()
{
_updatedValue = false;
_node.Storage = _originalValue;
}
#endregion
#region IDisposable
/// <inheritdoc />
public void Dispose()
{
if (_updatedValue)
{
if (_originalValue is IDisposable disposable)
disposable.Dispose();
}
else
{
if (_value is IDisposable disposable)
disposable.Dispose();
}
}
#endregion
}

View File

@ -4,46 +4,54 @@ using System.Reactive.Disposables;
using Artemis.Core;
using ReactiveUI;
namespace Artemis.UI.Shared.VisualScripting
namespace Artemis.UI.Shared.VisualScripting;
/// <summary>
/// Represents a custom view model for a node
/// </summary>
public abstract class CustomNodeViewModel : ActivatableViewModelBase, ICustomNodeViewModel
{
public abstract class CustomNodeViewModel : ActivatableViewModelBase, ICustomNodeViewModel
/// <summary>
/// Creates a new instance of the <see cref="CustomNodeViewModel" /> class.
/// </summary>
/// <param name="node">The node the view model is for.</param>
/// <param name="script">The script the node is contained in.</param>
protected CustomNodeViewModel(INode node, INodeScript script)
{
protected CustomNodeViewModel(INode node)
Node = node;
Script = script;
this.WhenActivated(d =>
{
Node = node;
this.WhenActivated(d =>
{
Node.PropertyChanged += NodeOnPropertyChanged;
Disposable.Create(() => Node.PropertyChanged -= NodeOnPropertyChanged).DisposeWith(d);
});
}
public INode Node { get; }
#region Events
/// <inheritdoc />
public event EventHandler NodeModified;
/// <summary>
/// Invokes the <see cref="NodeModified"/> event
/// </summary>
protected virtual void OnNodeModified()
{
NodeModified?.Invoke(this, EventArgs.Empty);
}
#endregion
#region Event handlers
private void NodeOnPropertyChanged(object? sender, PropertyChangedEventArgs e)
{
if (e.PropertyName == "Storage")
OnNodeModified();
}
#endregion
Node.PropertyChanged += NodeOnPropertyChanged;
Disposable.Create(() => Node.PropertyChanged -= NodeOnPropertyChanged).DisposeWith(d);
});
}
/// <summary>
/// Gets the node the view model is for.
/// </summary>
public INode Node { get; }
/// <summary>
/// Gets script the node is contained in.
/// </summary>
public INodeScript Script { get; }
/// <summary>
/// Invokes the <see cref="NodeModified" /> event
/// </summary>
protected virtual void OnNodeModified()
{
NodeModified?.Invoke(this, EventArgs.Empty);
}
private void NodeOnPropertyChanged(object? sender, PropertyChangedEventArgs e)
{
if (e.PropertyName == "Storage")
OnNodeModified();
}
/// <inheritdoc />
public event EventHandler? NodeModified;
}

View File

@ -8,6 +8,7 @@ using Artemis.Core.Events;
using Artemis.UI.Ninject.Factories;
using Artemis.UI.Screens.VisualScripting.Pins;
using Artemis.UI.Shared;
using Artemis.UI.Shared.Services.Interfaces;
using Artemis.UI.Shared.Services.NodeEditor;
using Artemis.UI.Shared.Services.NodeEditor.Commands;
using Avalonia;
@ -21,15 +22,17 @@ namespace Artemis.UI.Screens.VisualScripting;
public class NodeScriptViewModel : ActivatableViewModelBase
{
private readonly INodeEditorService _nodeEditorService;
private readonly INotificationService _notificationService;
private readonly SourceList<NodeViewModel> _nodeViewModels;
private readonly INodeVmFactory _nodeVmFactory;
private DragCableViewModel? _dragViewModel;
private List<NodeViewModel>? _initialNodeSelection;
public NodeScriptViewModel(NodeScript nodeScript, INodeVmFactory nodeVmFactory, INodeEditorService nodeEditorService)
public NodeScriptViewModel(NodeScript nodeScript, INodeVmFactory nodeVmFactory, INodeEditorService nodeEditorService, INotificationService notificationService)
{
_nodeVmFactory = nodeVmFactory;
_nodeEditorService = nodeEditorService;
_notificationService = notificationService;
_nodeViewModels = new SourceList<NodeViewModel>();
NodeScript = nodeScript;
@ -44,6 +47,13 @@ public class NodeScriptViewModel : ActivatableViewModelBase
Observable.FromEventPattern<SingleValueEventArgs<INode>>(x => NodeScript.NodeRemoved += x, x => NodeScript.NodeRemoved -= x)
.Subscribe(e => HandleNodeRemoved(e.EventArgs))
.DisposeWith(d);
nodeEditorService.GetHistory(NodeScript).Undo
.Subscribe(c => _notificationService.CreateNotification().WithMessage(c != null ? $"Undid {c.DisplayName}" : "Nothing to undo").Show())
.DisposeWith(d);
nodeEditorService.GetHistory(NodeScript).Redo
.Subscribe(c => _notificationService.CreateNotification().WithMessage(c != null ? $"Redid {c.DisplayName}" : "Nothing to redo").Show())
.DisposeWith(d);
});
// Create VMs for all nodes

View File

@ -113,7 +113,7 @@ public class NodeViewModel : ActivatableViewModelBase
})).DisposeWith(d);
if (Node is Node coreNode)
CustomNodeViewModel = coreNode.GetCustomViewModel();
CustomNodeViewModel = coreNode.GetCustomViewModel(nodeScriptViewModel.NodeScript);
});
}

View File

@ -1,10 +1,11 @@
using Artemis.UI.Shared.VisualScripting;
using Artemis.Core;
using Artemis.UI.Shared.VisualScripting;
namespace Artemis.VisualScripting.Nodes.Color.CustomViewModels;
public class StaticSKColorValueNodeCustomViewModel : CustomNodeViewModel
{
public StaticSKColorValueNodeCustomViewModel(StaticSKColorValueNode node) : base(node)
public StaticSKColorValueNodeCustomViewModel(StaticSKColorValueNode node, INodeScript script) : base(node, script)
{
}
}

View File

@ -10,7 +10,7 @@ public class EnumEqualsNodeCustomViewModel : CustomNodeViewModel
{
private readonly EnumEqualsNode _node;
public EnumEqualsNodeCustomViewModel(EnumEqualsNode node) : base(node)
public EnumEqualsNodeCustomViewModel(EnumEqualsNode node, INodeScript script) : base(node, script)
{
_node = node;
}

View File

@ -11,7 +11,7 @@ public class LayerPropertyNodeCustomViewModel : CustomNodeViewModel
private RenderProfileElement _selectedProfileElement;
public LayerPropertyNodeCustomViewModel(LayerPropertyNode node) : base(node)
public LayerPropertyNodeCustomViewModel(LayerPropertyNode node, INodeScript script) : base(node, script)
{
_node = node;
}

View File

@ -5,14 +5,14 @@ namespace Artemis.VisualScripting.Nodes.CustomViewModels;
public class StaticNumericValueNodeCustomViewModel : CustomNodeViewModel
{
public StaticNumericValueNodeCustomViewModel(INode node) : base(node)
public StaticNumericValueNodeCustomViewModel(INode node, INodeScript script) : base(node, script)
{
}
}
public class StaticStringValueNodeCustomViewModel : CustomNodeViewModel
{
public StaticStringValueNodeCustomViewModel(INode node) : base(node)
public StaticStringValueNodeCustomViewModel(INode node, INodeScript script) : base(node, script)
{
}
}

View File

@ -1,9 +1,11 @@
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Reactive.Disposables;
using Artemis.Core;
using Artemis.Core.Modules;
using Artemis.Core.Services;
using Artemis.Storage.Entities.Profile;
using Artemis.UI.Shared.Services.NodeEditor;
using Artemis.UI.Shared.Services.NodeEditor.Commands;
using Artemis.UI.Shared.VisualScripting;
using ReactiveUI;
@ -12,29 +14,40 @@ namespace Artemis.VisualScripting.Nodes.DataModel.CustomViewModels;
public class DataModelEventNodeCustomViewModel : CustomNodeViewModel
{
private readonly DataModelEventNode _node;
private readonly INodeEditorService _nodeEditorService;
private bool _updating;
private DataModelPath? _dataModelPath;
private ObservableCollection<Module>? _modules;
public DataModelEventNodeCustomViewModel(DataModelEventNode node, ISettingsService settingsService) : base(node)
public DataModelEventNodeCustomViewModel(DataModelEventNode node, INodeScript script, ISettingsService settingsService, INodeEditorService nodeEditorService) : base(node, script)
{
_node = node;
_nodeEditorService = nodeEditorService;
ShowFullPaths = settingsService.GetSetting("ProfileEditor.ShowFullPaths", true);
ShowDataModelValues = settingsService.GetSetting("ProfileEditor.ShowDataModelValues", false);
Modules = new ObservableCollection<Module>();
this.WhenActivated(d =>
{
if (Modules != null)
return;
// Set up extra modules
if (_node.Script?.Context is Profile scriptProfile && scriptProfile.Configuration.Module != null)
Modules = new ObservableCollection<Module> {scriptProfile.Configuration.Module};
else if (_node.Script?.Context is ProfileConfiguration profileConfiguration && profileConfiguration.Module != null)
Modules = new ObservableCollection<Module> {profileConfiguration.Module};
Modules = new ObservableCollection<Module>();
if (_node.Script.Context is Profile scriptProfile && scriptProfile.Configuration.Module != null)
Modules.Add(scriptProfile.Configuration.Module);
else if (_node.Script.Context is ProfileConfiguration profileConfiguration && profileConfiguration.Module != null)
Modules.Add(profileConfiguration.Module);
// Subscribe to node changes
_node.WhenAnyValue(n => n.Storage).Subscribe(UpdateDataModelPath).DisposeWith(d);
UpdateDataModelPath(_node.Storage);
_node.PropertyChanged += NodeOnPropertyChanged;
Disposable.Create(() => _node.PropertyChanged -= NodeOnPropertyChanged).DisposeWith(d);
Disposable.Create(() =>
{
_dataModelPath?.Dispose();
_dataModelPath = null;
}).DisposeWith(d);
});
this.WhenAnyValue(vm => vm.DataModelPath).Subscribe(ApplyDataModelPath);
}
public PluginSetting<bool> ShowFullPaths { get; }
@ -47,25 +60,48 @@ public class DataModelEventNodeCustomViewModel : CustomNodeViewModel
set => RaiseAndSetIfChanged(ref _modules, value);
}
public DataModelPath DataModelPath
public DataModelPath? DataModelPath
{
get => _node.DataModelPath;
set
get => _dataModelPath;
set => RaiseAndSetIfChanged(ref _dataModelPath, value);
}
private void UpdateDataModelPath(DataModelPathEntity? entity)
{
try
{
if (ReferenceEquals(_node.DataModelPath, value))
if (_updating)
return;
_node.DataModelPath?.Dispose();
_node.DataModelPath = value;
_node.DataModelPath.Save();
_updating = true;
_node.Storage = _node.DataModelPath.Entity;
DataModelPath? old = DataModelPath;
DataModelPath = entity != null ? new DataModelPath(entity) : null;
old?.Dispose();
}
finally
{
_updating = false;
}
}
private void NodeOnPropertyChanged(object? sender, PropertyChangedEventArgs e)
private void ApplyDataModelPath(DataModelPath? path)
{
if (e.PropertyName == nameof(DataModelNode.DataModelPath))
this.RaisePropertyChanged(nameof(DataModelPath));
try
{
if (_updating)
return;
if (path?.Path == _node.Storage?.Path)
return;
_updating = true;
path?.Save();
_nodeEditorService.ExecuteCommand(Script, new UpdateStorage<DataModelPathEntity>(_node, path?.Entity, "event"));
}
finally
{
_updating = false;
}
}
}

View File

@ -4,6 +4,9 @@ using System.Reactive.Disposables;
using Artemis.Core;
using Artemis.Core.Modules;
using Artemis.Core.Services;
using Artemis.Storage.Entities.Profile;
using Artemis.UI.Shared.Services.NodeEditor;
using Artemis.UI.Shared.Services.NodeEditor.Commands;
using Artemis.UI.Shared.VisualScripting;
using ReactiveUI;
@ -12,29 +15,39 @@ namespace Artemis.VisualScripting.Nodes.DataModel.CustomViewModels;
public class DataModelNodeCustomViewModel : CustomNodeViewModel
{
private readonly DataModelNode _node;
private readonly INodeEditorService _nodeEditorService;
private ObservableCollection<Module>? _modules;
private DataModelPath? _dataModelPath;
private bool _updating;
public DataModelNodeCustomViewModel(DataModelNode node, ISettingsService settingsService) : base(node)
public DataModelNodeCustomViewModel(DataModelNode node, INodeScript script, ISettingsService settingsService, INodeEditorService nodeEditorService) : base(node, script)
{
_node = node;
_nodeEditorService = nodeEditorService;
ShowFullPaths = settingsService.GetSetting("ProfileEditor.ShowFullPaths", true);
ShowDataModelValues = settingsService.GetSetting("ProfileEditor.ShowDataModelValues", false);
this.WhenActivated(d =>
{
if (Modules != null)
return;
Modules = new ObservableCollection<Module>();
// Set up extra modules
if (_node.Script?.Context is Profile scriptProfile && scriptProfile.Configuration.Module != null)
Modules.Add(scriptProfile.Configuration.Module);
Modules = new ObservableCollection<Module> {scriptProfile.Configuration.Module};
else if (_node.Script?.Context is ProfileConfiguration profileConfiguration && profileConfiguration.Module != null)
Modules.Add(profileConfiguration.Module);
Modules = new ObservableCollection<Module> {profileConfiguration.Module};
_node.PropertyChanged += NodeOnPropertyChanged;
Disposable.Create(() => _node.PropertyChanged -= NodeOnPropertyChanged).DisposeWith(d);
// Subscribe to node changes
_node.WhenAnyValue(n => n.Storage).Subscribe(UpdateDataModelPath).DisposeWith(d);
UpdateDataModelPath(_node.Storage);
Disposable.Create(() =>
{
_dataModelPath?.Dispose();
_dataModelPath = null;
}).DisposeWith(d);
});
this.WhenAnyValue(vm => vm.DataModelPath).Subscribe(ApplyDataModelPath);
}
public PluginSetting<bool> ShowFullPaths { get; }
@ -48,24 +61,46 @@ public class DataModelNodeCustomViewModel : CustomNodeViewModel
public DataModelPath? DataModelPath
{
get => _node.DataModelPath;
set
get => _dataModelPath;
set => RaiseAndSetIfChanged(ref _dataModelPath, value);
}
private void UpdateDataModelPath(DataModelPathEntity? entity)
{
try
{
if (ReferenceEquals(_node.DataModelPath, value))
if (_updating)
return;
_node.DataModelPath?.Dispose();
_node.DataModelPath = value;
_node.DataModelPath?.Save();
_updating = true;
_node.Storage = _node.DataModelPath?.Entity;
_node.UpdateOutputPin();
DataModelPath? old = DataModelPath;
DataModelPath = entity != null ? new DataModelPath(entity) : null;
old?.Dispose();
}
finally
{
_updating = false;
}
}
private void NodeOnPropertyChanged(object? sender, PropertyChangedEventArgs e)
private void ApplyDataModelPath(DataModelPath? path)
{
if (e.PropertyName == nameof(DataModelNode.DataModelPath))
this.RaisePropertyChanged(nameof(DataModelPath));
try
{
if (_updating)
return;
if (path?.Path == _node.Storage?.Path)
return;
_updating = true;
path?.Save();
_nodeEditorService.ExecuteCommand(Script, new UpdateStorage<DataModelPathEntity>(_node, path?.Entity, "path"));
}
finally
{
_updating = false;
}
}
}

View File

@ -1,10 +1,12 @@
using Artemis.VisualScripting.Nodes.DataModel.CustomViewModels;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Markup.Xaml;
using Avalonia.ReactiveUI;
namespace Artemis.VisualScripting.Nodes.DataModel.CustomViews
{
public partial class DataModelEventNodeCustomView : UserControl
public partial class DataModelEventNodeCustomView : ReactiveUserControl<DataModelEventNodeCustomViewModel>
{
public DataModelEventNodeCustomView()
{

View File

@ -1,10 +1,12 @@
using Artemis.VisualScripting.Nodes.DataModel.CustomViewModels;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Markup.Xaml;
using Avalonia.ReactiveUI;
namespace Artemis.VisualScripting.Nodes.DataModel.CustomViews
{
public partial class DataModelNodeCustomView : UserControl
public partial class DataModelNodeCustomView : ReactiveUserControl<DataModelNodeCustomViewModel>
{
public DataModelNodeCustomView()
{

View File

@ -1,4 +1,5 @@
using Artemis.Core;
using System.ComponentModel;
using Artemis.Core;
using Artemis.Core.Events;
using Artemis.Storage.Entities.Profile;
using Artemis.VisualScripting.Nodes.DataModel.CustomViewModels;
@ -24,6 +25,9 @@ public class DataModelEventNode : Node<DataModelPathEntity, DataModelEventNodeCu
CycleValues.PinAdded += CycleValuesOnPinAdded;
CycleValues.PinRemoved += CycleValuesOnPinRemoved;
CycleValues.Add(CycleValues.CreatePin());
// Monitor storage for changes
StorageModified += (_, _) => UpdateDataModelPath();
}
public INodeScript? Script { get; set; }
@ -31,12 +35,6 @@ public class DataModelEventNode : Node<DataModelPathEntity, DataModelEventNodeCu
public InputPinCollection CycleValues { get; }
public OutputPin Output { get; }
public DataModelPath? DataModelPath
{
get => _dataModelPath;
set => SetAndNotify(ref _dataModelPath, value);
}
public override void Initialize(INodeScript script)
{
Script = script;
@ -44,13 +42,13 @@ public class DataModelEventNode : Node<DataModelPathEntity, DataModelEventNodeCu
if (Storage == null)
return;
DataModelPath = new DataModelPath(Storage);
UpdateDataModelPath();
}
public override void Evaluate()
{
object? outputValue = null;
if (DataModelPath?.GetValue() is IDataModelEvent dataModelEvent)
if (_dataModelPath?.GetValue() is IDataModelEvent dataModelEvent)
{
if (dataModelEvent.LastTrigger > _lastTrigger)
{
@ -111,6 +109,14 @@ public class DataModelEventNode : Node<DataModelPathEntity, DataModelEventNodeCu
}
}
private void UpdateDataModelPath()
{
DataModelPath? old = _dataModelPath;
_dataModelPath = Storage != null ? new DataModelPath(Storage) : null;
old?.Dispose();
}
private void ChangeCurrentType(Type type)
{
CycleValues.ChangeType(type);
@ -140,5 +146,6 @@ public class DataModelEventNode : Node<DataModelPathEntity, DataModelEventNodeCu
/// <inheritdoc />
public void Dispose()
{
_dataModelPath?.Dispose();
}
}

View File

@ -1,4 +1,5 @@
using Artemis.Core;
using System.ComponentModel;
using Artemis.Core;
using Artemis.Storage.Entities.Profile;
using Artemis.VisualScripting.Nodes.DataModel.CustomViewModels;
using Avalonia.Threading;
@ -13,17 +14,12 @@ public class DataModelNode : Node<DataModelPathEntity, DataModelNodeCustomViewMo
public DataModelNode() : base("Data Model", "Outputs a selectable data model value")
{
Output = CreateOutputPin(typeof(object));
StorageModified += (_, _) => UpdateDataModelPath();
}
public INodeScript? Script { get; private set; }
public OutputPin Output { get; }
public DataModelPath? DataModelPath
{
get => _dataModelPath;
set => SetAndNotify(ref _dataModelPath, value);
}
public override void Initialize(INodeScript script)
{
Script = script;
@ -31,18 +27,26 @@ public class DataModelNode : Node<DataModelPathEntity, DataModelNodeCustomViewMo
if (Storage == null)
return;
DataModelPath = new DataModelPath(Storage);
DataModelPath.PathValidated += DataModelPathOnPathValidated;
UpdateDataModelPath();
}
private void UpdateDataModelPath()
{
DataModelPath? old = _dataModelPath;
old?.Dispose();
_dataModelPath = Storage != null ? new DataModelPath(Storage) : null;
if (_dataModelPath != null)
_dataModelPath.PathValidated += DataModelPathOnPathValidated;
UpdateOutputPin();
}
public override void Evaluate()
{
if (DataModelPath == null || !DataModelPath.IsValid)
if (_dataModelPath == null || !_dataModelPath.IsValid)
return;
object? pathValue = DataModelPath.GetValue();
object? pathValue = _dataModelPath.GetValue();
if (pathValue == null)
{
if (!Output.Type.IsValueType)
@ -56,7 +60,7 @@ public class DataModelNode : Node<DataModelPathEntity, DataModelNodeCustomViewMo
public void UpdateOutputPin()
{
Type? type = DataModelPath?.GetPropertyType();
Type? type = _dataModelPath?.GetPropertyType();
if (Numeric.IsTypeCompatible(type))
type = typeof(Numeric);
type ??= typeof(object);
@ -73,6 +77,6 @@ public class DataModelNode : Node<DataModelPathEntity, DataModelNodeCustomViewMo
/// <inheritdoc />
public void Dispose()
{
DataModelPath?.Dispose();
_dataModelPath?.Dispose();
}
}

View File

@ -9,7 +9,7 @@ public class EasingTypeNodeCustomViewModel : CustomNodeViewModel
private readonly EasingTypeNode _node;
private NodeEasingViewModel _selectedEasingViewModel;
public EasingTypeNodeCustomViewModel(EasingTypeNode node) : base(node)
public EasingTypeNodeCustomViewModel(EasingTypeNode node, INodeScript script) : base(node, script)
{
_node = node;
EasingViewModels = new ObservableCollection<NodeEasingViewModel>(Enum.GetValues(typeof(Easings.Functions)).Cast<Easings.Functions>().Select(e => new NodeEasingViewModel(e)));

View File

@ -5,7 +5,7 @@ namespace Artemis.VisualScripting.Nodes.Maths.CustomViewModels;
public class MathExpressionNodeCustomViewModel : CustomNodeViewModel
{
public MathExpressionNodeCustomViewModel(INode node) : base(node)
public MathExpressionNodeCustomViewModel(INode node, INodeScript script) : base(node, script)
{
}
}