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

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 2021-08-21 12:15:01 +02:00
parent 3ed3cd1b1e
commit 836e979991
26 changed files with 305 additions and 122 deletions

View File

@ -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 />

View File

@ -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();
}
}
}

View File

@ -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();
}
}

View File

@ -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)

View File

@ -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;

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;
}

View File

@ -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;
}
}
}

View File

@ -19,7 +19,7 @@ namespace Artemis.Core
event EventHandler Resetting;
void Initialize();
void Initialize(INodeScript script);
void Evaluate();
void Reset();
}

View File

@ -14,6 +14,8 @@ namespace Artemis.Core
Type ResultType { get; }
object? Context { get; set; }
void Run();
void AddNode(INode node);
void RemoveNode(INode node);

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()
{

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
}

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);

View File

@ -116,8 +116,6 @@ namespace Artemis.UI.Screens.ProfileEditor.DisplayConditions
if (RenderProfileElement == null)
return;
RenderProfileElement.DisplayCondition ??= new NodeScript<bool>("End Result", "");
_windowManager.ShowDialog(_nodeVmFactory.NodeScriptWindowViewModel(RenderProfileElement.DisplayCondition));
_profileEditorService.SaveSelectedProfileElement();
}

View File

@ -50,6 +50,9 @@
<XamlRuntime>$(DefaultXamlRuntime)</XamlRuntime>
<SubType>Designer</SubType>
</Page>
<Page Update="Nodes\CustomViews\StaticFloatValueNodeCustomView.xaml">
<XamlRuntime>$(DefaultXamlRuntime)</XamlRuntime>
</Page>
<Page Update="ResourceDictionaries\DataModelConditions.xaml">
<XamlRuntime>$(DefaultXamlRuntime)</XamlRuntime>
</Page>

View File

@ -383,7 +383,8 @@ namespace Artemis.VisualScripting.Editor.Controls
if (_creationBoxParent.ContextMenu != null)
_creationBoxParent.ContextMenu.IsOpen = false;
INode node = nodeData.CreateNode(null);
INode node = nodeData.CreateNode(Script, null);
node.Initialize(Script);
Script.AddNode(node);
InitializeNode(node, _lastRightClickLocation);

View File

@ -0,0 +1,34 @@
using System.Windows;
using Artemis.Core;
using Stylet;
namespace Artemis.VisualScripting.Nodes.CustomViewModels
{
public abstract class CustomNodeViewModel : PropertyChangedBase, IViewAware, ICustomNodeViewModel
{
protected CustomNodeViewModel(INode node)
{
Node = node;
}
public INode Node { get; }
#region Implementation of IViewAware
/// <inheritdoc />
public void AttachView(UIElement view)
{
View = view;
OnDisplay();
}
protected virtual void OnDisplay()
{
}
/// <inheritdoc />
public UIElement View { get; private set; }
#endregion
}
}

View File

@ -1,16 +1,25 @@
using Artemis.Core;
using Artemis.Core.Modules;
using Stylet;
namespace Artemis.VisualScripting.Nodes.CustomViewModels
{
public class DataModelNodeCustomViewModel : CustomNodeViewModel
{
private readonly DataModelNode _node;
private BindableCollection<Module> _modules;
public DataModelNodeCustomViewModel(DataModelNode node) : base(node)
{
_node = node;
}
public BindableCollection<Module> Modules
{
get => _modules;
set => SetAndNotify(ref _modules, value);
}
public DataModelPath DataModelPath
{
get => _node.DataModelPath;
@ -32,5 +41,28 @@ namespace Artemis.VisualScripting.Nodes.CustomViewModels
_node.UpdateOutputPin();
}
}
public void Initialize()
{
if (Modules != null)
return;
Modules = new BindableCollection<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);
}
#region Overrides of CustomNodeViewModel
/// <inheritdoc />
protected override void OnDisplay()
{
Initialize();
base.OnDisplay();
}
#endregion
}
}

View File

@ -1,24 +0,0 @@
using Artemis.Core;
namespace Artemis.VisualScripting.Nodes.CustomViewModels
{
public class StaticDoubleValueNodeCustomViewModel : CustomNodeViewModel
{
private readonly StaticDoubleValueNode _node;
public StaticDoubleValueNodeCustomViewModel(StaticDoubleValueNode node) : base(node)
{
_node = node;
}
public double Input
{
get => (double) _node.Storage;
set
{
_node.Storage = value;
OnPropertyChanged(nameof(Input));
}
}
}
}

View File

@ -1,29 +0,0 @@
using Artemis.Core;
namespace Artemis.VisualScripting.Nodes.CustomViewModels
{
public class StaticIntegerValueNodeCustomViewModel : CustomNodeViewModel
{
private readonly StaticIntegerValueNode _node;
public StaticIntegerValueNodeCustomViewModel(StaticIntegerValueNode node) : base(node)
{
_node = node;
}
public int Input
{
get
{
if (_node.Storage is long longInput)
return (int) longInput;
return (int) _node.Storage;
}
set
{
_node.Storage = value;
OnPropertyChanged(nameof(Input));
}
}
}
}

View File

@ -1,24 +0,0 @@
using Artemis.Core;
namespace Artemis.VisualScripting.Nodes.CustomViewModels
{
public class StaticStringValueNodeCustomViewModel : CustomNodeViewModel
{
private readonly StaticStringValueNode _node;
public StaticStringValueNodeCustomViewModel(StaticStringValueNode node) : base(node)
{
_node = node;
}
public string Input
{
get => (string) _node.Storage;
set
{
_node.Storage = value;
OnPropertyChanged(nameof(Input));
}
}
}
}

View File

@ -0,0 +1,89 @@
using Artemis.Core;
namespace Artemis.VisualScripting.Nodes.CustomViewModels
{
public class StaticDoubleValueNodeCustomViewModel : CustomNodeViewModel
{
private readonly StaticDoubleValueNode _node;
public StaticDoubleValueNodeCustomViewModel(StaticDoubleValueNode node) : base(node)
{
_node = node;
}
public double Input
{
get => (double) _node.Storage;
set
{
_node.Storage = value;
OnPropertyChanged(nameof(Input));
}
}
}
public class StaticFloatValueNodeCustomViewModel : CustomNodeViewModel
{
private readonly StaticFloatValueNode _node;
public StaticFloatValueNodeCustomViewModel(StaticFloatValueNode node) : base(node)
{
_node = node;
}
public float Input
{
get => (float)_node.Storage;
set
{
_node.Storage = value;
OnPropertyChanged(nameof(Input));
}
}
}
public class StaticIntegerValueNodeCustomViewModel : CustomNodeViewModel
{
private readonly StaticIntegerValueNode _node;
public StaticIntegerValueNodeCustomViewModel(StaticIntegerValueNode node) : base(node)
{
_node = node;
}
public int Input
{
get
{
if (_node.Storage is long longInput)
return (int)longInput;
return (int)_node.Storage;
}
set
{
_node.Storage = value;
OnPropertyChanged(nameof(Input));
}
}
}
public class StaticStringValueNodeCustomViewModel : CustomNodeViewModel
{
private readonly StaticStringValueNode _node;
public StaticStringValueNodeCustomViewModel(StaticStringValueNode node) : base(node)
{
_node = node;
}
public string Input
{
get => (string)_node.Storage;
set
{
_node.Storage = value;
OnPropertyChanged(nameof(Input));
}
}
}
}

View File

@ -8,5 +8,5 @@
xmlns:controls="clr-namespace:Artemis.UI.Shared.Controls;assembly=Artemis.UI.Shared"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800">
<controls:DataModelPicker DataModelPath="{Binding DataModelPath}" ButtonBrush="#434343"/>
<controls:DataModelPicker DataModelPath="{Binding DataModelPath}" Modules="{Binding Modules}" ButtonBrush="#434343"/>
</UserControl>

View File

@ -0,0 +1,12 @@
<UserControl x:Class="Artemis.VisualScripting.Nodes.CustomViews.StaticFloatValueNodeCustomView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:Artemis.VisualScripting.Nodes.CustomViewModels"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800">
<TextBox VerticalAlignment="Center"
HorizontalAlignment="Stretch"
Text="{Binding Input}" />
</UserControl>

View File

@ -13,18 +13,21 @@ namespace Artemis.VisualScripting.Nodes
{
}
public INodeScript Script { get; private set; }
public OutputPin Output { get; private set; }
public DataModelPath DataModelPath { get; set; }
public override void Initialize()
public override void Initialize(INodeScript script)
{
if (Storage is not DataModelPathEntity pathEntity)
Script = script;
if (Storage is not DataModelPathEntity pathEntity)
return;
DataModelPath = new DataModelPath(null, pathEntity);
UpdateOutputPin();
}
public override void Evaluate()
{
if (DataModelPath.IsValid && Output != null)

View File

@ -29,7 +29,7 @@ namespace Artemis.VisualScripting.Nodes
Output.Value = Storage as int? ?? 0;
}
public override void Initialize() => Storage ??= 0;
public override void Initialize(INodeScript script) => Storage ??= 0;
#endregion
}
@ -60,7 +60,38 @@ namespace Artemis.VisualScripting.Nodes
Output.Value = Storage as double? ?? 0.0;
}
public override void Initialize() => Storage ??= 0.0;
public override void Initialize(INodeScript script) => Storage ??= 0.0;
#endregion
}
[Node("Float-Value", "Outputs a configurable float value.")]
public class StaticFloatValueNode : Node<StaticFloatValueNodeCustomViewModel>
{
#region Properties & Fields
public OutputPin<float> Output { get; }
#endregion
#region Constructors
public StaticFloatValueNode()
: base("Float", "Outputs a configurable float value.")
{
Output = CreateOutputPin<float>();
}
#endregion
#region Methods
public override void Evaluate()
{
Output.Value = Storage as float? ?? 0.0f;
}
public override void Initialize(INodeScript script) => Storage ??= 0.0f;
#endregion
}

View File

@ -35,6 +35,38 @@ namespace Artemis.VisualScripting.Nodes
#endregion
}
[Node("Sum (Float)", "Sums the connected float values.")]
public class SumFloatsNode : Node
{
#region Properties & Fields
public InputPinCollection<float> Values { get; }
public OutputPin<float> Sum { get; }
#endregion
#region Constructors
public SumFloatsNode()
: base("Sum", "Sums the connected float values.")
{
Values = CreateInputPinCollection<float>("Values", 2);
Sum = CreateOutputPin<float>("Sum");
}
#endregion
#region Methods
public override void Evaluate()
{
Sum.Value = Values.Values.Sum();
}
#endregion
}
[Node("Sum (Double)", "Sums the connected double values.")]
public class SumDoublesNode : Node
{