Nodes - Provide scripts with a context

Nodes - Inform nodes about the script they're being initialized for
Nodes - Added float nodes matching the existing other number types
Core - Add API for retrieving data binding values via the interface
This commit is contained in:
Robert committed 2021-08-21 12:15:01 +02:00
1 parent 3ed3cd1b1e
commit 836e979991
26 files changed
+305 -122

No files matched your search

@@ -19,7 +19,7 @@ namespace Artemis.Core
Entity = new DataBindingEntity();
ApplyRegistration(dataBindingRegistration);
Script = new NodeScript<TProperty>(GetScriptName(), "The value to put into the data binding");
Script = new NodeScript<TProperty>(GetScriptName(), "The value to put into the data binding", LayerProperty.ProfileElement.Profile);
Save();
}
@@ -28,7 +28,7 @@ namespace Artemis.Core
{
LayerProperty = layerProperty;
Entity = entity;
Script = new NodeScript<TProperty>(GetScriptName(), "The value to put into the data binding");
Script = new NodeScript<TProperty>(GetScriptName(), "The value to put into the data binding", LayerProperty.ProfileElement.Profile);
// Load will add children so be initialized before that
Load();
@@ -245,8 +245,8 @@ namespace Artemis.Core
Script.Dispose();
Script = Entity.NodeScript != null
? new NodeScript<TProperty>(GetScriptName(), "The value to put into the data binding", Entity.NodeScript)
: new NodeScript<TProperty>(GetScriptName(), "The value to put into the data binding");
? new NodeScript<TProperty>(GetScriptName(), "The value to put into the data binding", Entity.NodeScript, LayerProperty.ProfileElement.Profile)
: new NodeScript<TProperty>(GetScriptName(), "The value to put into the data binding", LayerProperty.ProfileElement.Profile);
}
/// <inheritdoc />
@@ -40,6 +40,9 @@ namespace Artemis.Core
/// <inheritdoc />
public string DisplayName { get; }
/// <inheritdoc />
public Type ValueType => typeof(TProperty);
/// <summary>
/// Gets the data binding created using this registration
/// </summary>
@@ -74,5 +77,11 @@ namespace Artemis.Core
// The related entity is left behind, just in case the data binding is added back later
LayerProperty.DisableDataBinding(DataBinding);
}
/// <inheritdoc />
public object? GetValue()
{
return Getter();
}
}
}
@@ -1,4 +1,6 @@
namespace Artemis.Core
using System;
namespace Artemis.Core
{
/// <summary>
/// Represents a data binding registration
@@ -10,6 +12,11 @@
/// </summary>
string DisplayName { get; }
/// <summary>
/// Gets the type of the value this data binding registration points to
/// </summary>
Type ValueType { get; }
/// <summary>
/// Returns the data binding applied using this registration
/// </summary>
@@ -25,5 +32,11 @@
/// If present, removes the current data binding
/// </summary>
void ClearDataBinding();
/// <summary>
/// Gets the value of the data binding
/// </summary>
/// <returns>A value matching the type of <see cref="ValueType" /></returns>
object? GetValue();
}
}
@@ -23,7 +23,7 @@ namespace Artemis.Core
internal RenderProfileElement(Profile profile) : base(profile)
{
_typeDisplayName = this is Layer ? "layer" : "folder";
_displayCondition = new NodeScript<bool>($"Activate {_typeDisplayName}", $"Whether or not this {_typeDisplayName} should be active");
_displayCondition = new NodeScript<bool>($"Activate {_typeDisplayName}", $"Whether or not this {_typeDisplayName} should be active", Profile);
Timeline = new Timeline();
ExpandedPropertyGroups = new List<string>();
@@ -71,8 +71,8 @@ namespace Artemis.Core
internal void LoadRenderElement()
{
DisplayCondition = RenderElementEntity.NodeScript != null
? new NodeScript<bool>($"Activate {_typeDisplayName}", $"Whether or not this {_typeDisplayName} should be active", RenderElementEntity.NodeScript)
: new NodeScript<bool>($"Activate {_typeDisplayName}", $"Whether or not this {_typeDisplayName} should be active");
? new NodeScript<bool>($"Activate {_typeDisplayName}", $"Whether or not this {_typeDisplayName} should be active", RenderElementEntity.NodeScript, Profile)
: new NodeScript<bool>($"Activate {_typeDisplayName}", $"Whether or not this {_typeDisplayName} should be active", Profile);
Timeline = RenderElementEntity.Timeline != null
? new Timeline(RenderElementEntity.Timeline)
@@ -28,7 +28,7 @@ namespace Artemis.Core
Entity = new ProfileConfigurationEntity();
Icon = new ProfileConfigurationIcon(Entity) {MaterialIcon = icon};
ActivationCondition = new NodeScript<bool>("Activate profile", "Whether or not the profile should be active");
ActivationCondition = new NodeScript<bool>("Activate profile", "Whether or not the profile should be active", this);
}
internal ProfileConfiguration(ProfileCategory category, ProfileConfigurationEntity entity)
@@ -39,7 +39,7 @@ namespace Artemis.Core
Entity = entity;
Icon = new ProfileConfigurationIcon(Entity);
ActivationCondition = new NodeScript<bool>("Activate profile", "Whether or not the profile should be active");
ActivationCondition = new NodeScript<bool>("Activate profile", "Whether or not the profile should be active", this);
Load();
}
@@ -242,8 +242,8 @@ namespace Artemis.Core
ActivationCondition.Dispose();
ActivationCondition = Entity.ActivationCondition != null
? new NodeScript<bool>("Activate profile", "Whether or not the profile should be active", Entity.ActivationCondition)
: new NodeScript<bool>("Activate profile", "Whether or not the profile should be active");
? new NodeScript<bool>("Activate profile", "Whether or not the profile should be active", Entity.ActivationCondition, this)
: new NodeScript<bool>("Activate profile", "Whether or not the profile should be active", this);
EnableHotkey = Entity.EnableHotkey != null ? new ProfileConfigurationHotkey(Entity.EnableHotkey) : null;
DisableHotkey = Entity.DisableHotkey != null ? new ProfileConfigurationHotkey(Entity.DisableHotkey) : null;
+3 -3
View File
@@ -53,7 +53,7 @@ namespace Artemis.Core.Services
string description = nodeAttribute?.Description ?? string.Empty;
string category = nodeAttribute?.Category ?? string.Empty;
NodeData nodeData = new(plugin, nodeType, name, description, category, e => CreateNode(e, nodeType));
NodeData nodeData = new(plugin, nodeType, name, description, category, (s, e) => CreateNode(s, e, nodeType));
return NodeTypeStore.Add(nodeData);
}
@@ -65,7 +65,7 @@ namespace Artemis.Core.Services
return NodeTypeStore.AddColor(type, color, plugin);
}
private INode CreateNode(NodeEntity? entity, Type nodeType)
private INode CreateNode(INodeScript script, NodeEntity? entity, Type nodeType)
{
INode node = _kernel.Get(nodeType) as INode ?? throw new InvalidOperationException($"Node {nodeType} is not an INode");
@@ -79,7 +79,7 @@ namespace Artemis.Core.Services
if (node is CustomViewModelNode customViewModelNode)
customViewModelNode.BaseCustomViewModel = _kernel.Get(customViewModelNode.CustomViewModelType, new ConstructorArgument("node", node));
node.Initialize();
node.Initialize(script);
return node;
}
@@ -2,14 +2,8 @@
namespace Artemis.Core
{
public class CustomNodeViewModel : CorePropertyChanged
public interface ICustomNodeViewModel
{
[JsonIgnore]
public INode Node { get; }
public CustomNodeViewModel(INode node)
{
Node = node;
}
}
}
@@ -19,7 +19,7 @@ namespace Artemis.Core
event EventHandler Resetting;
void Initialize();
void Initialize(INodeScript script);
void Evaluate();
void Reset();
}
@@ -14,6 +14,8 @@ namespace Artemis.Core
Type ResultType { get; }
object? Context { get; set; }
void Run();
void AddNode(INode node);
void RemoveNode(INode node);
+6 -3
View File
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
namespace Artemis.Core
{
@@ -107,7 +108,7 @@ namespace Artemis.Core
OnPropertyChanged(nameof(Pins));
return pin;
}
protected bool RemovePin(Pin pin)
{
bool isRemoved = _pins.Remove(pin);
@@ -136,7 +137,9 @@ namespace Artemis.Core
return pin;
}
public virtual void Initialize() { }
public virtual void Initialize(INodeScript script)
{
}
public abstract void Evaluate();
@@ -148,7 +151,7 @@ namespace Artemis.Core
#endregion
}
public abstract class Node<T> : CustomViewModelNode where T : CustomNodeViewModel
public abstract class Node<T> : CustomViewModelNode where T : ICustomNodeViewModel
{
protected Node()
{
+3 -3
View File
@@ -14,13 +14,13 @@ namespace Artemis.Core
public string Description { get; }
public string Category { get; }
private Func<NodeEntity?, INode> _create;
private Func<INodeScript, NodeEntity?, INode> _create;
#endregion
#region Constructors
internal NodeData(Plugin plugin, Type type, string name, string description, string category, Func<NodeEntity?, INode>? create)
internal NodeData(Plugin plugin, Type type, string name, string description, string category, Func<INodeScript, NodeEntity?, INode>? create)
{
this.Plugin = plugin;
this.Type = type;
@@ -34,7 +34,7 @@ namespace Artemis.Core
#region Methods
public INode CreateNode(NodeEntity? entity) => _create(entity);
public INode CreateNode(INodeScript script, NodeEntity? entity) => _create(script, entity);
#endregion
}
+11 -7
View File
@@ -24,25 +24,29 @@ namespace Artemis.Core
protected INode ExitNode { get; set; }
public abstract Type ResultType { get; }
public object? Context { get; set; }
#endregion
#region Constructors
public NodeScript(string name, string description)
public NodeScript(string name, string description, object? context = null)
{
this.Name = name;
this.Description = description;
this.Context = context;
this.Entity = new NodeScriptEntity();
NodeTypeStore.NodeTypeAdded += NodeTypeStoreOnNodeTypeChanged;
NodeTypeStore.NodeTypeRemoved += NodeTypeStoreOnNodeTypeChanged;
}
internal NodeScript(string name, string description, NodeScriptEntity entity)
internal NodeScript(string name, string description, NodeScriptEntity entity, object? context = null)
{
this.Name = name;
this.Description = description;
this.Entity = entity;
this.Context = context;
NodeTypeStore.NodeTypeAdded += NodeTypeStoreOnNodeTypeChanged;
NodeTypeStore.NodeTypeRemoved += NodeTypeStoreOnNodeTypeChanged;
@@ -108,7 +112,7 @@ namespace Artemis.Core
return null;
// Create the node
node = nodeTypeRegistration.NodeData.CreateNode(nodeEntity);
node = nodeTypeRegistration.NodeData.CreateNode(this, nodeEntity);
}
else
{
@@ -272,8 +276,8 @@ namespace Artemis.Core
#region Constructors
internal NodeScript(string name, string description, NodeScriptEntity entity)
: base(name, description, entity)
internal NodeScript(string name, string description, NodeScriptEntity entity, object? context = null)
: base(name, description, entity, context)
{
ExitNode = new ExitNode<T>(name, description);
AddNode(ExitNode);
@@ -281,8 +285,8 @@ namespace Artemis.Core
Load();
}
public NodeScript(string name, string description)
: base(name, description)
public NodeScript(string name, string description, object? context = null)
: base(name, description, context)
{
ExitNode = new ExitNode<T>(name, description);
AddNode(ExitNode);