mirror of
https://github.com/Artemis-RGB/Artemis
synced 2026-11-12 11:56:30 +00:00
Nodes - Added math expression node
Node - Fix visual script cable present event unregister
This commit is contained in:
1 parent
ecdca26cd3
commit
e356d59daa
14 files changed
+290
-22
No files matched your search
+37
@@ -0,0 +1,37 @@
|
||||
using System.ComponentModel;
|
||||
using Artemis.VisualScripting.Nodes.CustomViewModels;
|
||||
|
||||
namespace Artemis.VisualScripting.Nodes.Maths.CustomViewModels
|
||||
{
|
||||
public class MathExpressionNodeCustomViewModel : CustomNodeViewModel
|
||||
{
|
||||
private readonly MathExpressionNode _node;
|
||||
|
||||
public MathExpressionNodeCustomViewModel(MathExpressionNode node) : base(node)
|
||||
{
|
||||
_node = node;
|
||||
}
|
||||
|
||||
public string Input
|
||||
{
|
||||
get => (string)_node.Storage;
|
||||
set => _node.Storage = value;
|
||||
}
|
||||
|
||||
public override void OnActivate()
|
||||
{
|
||||
_node.PropertyChanged += NodeOnPropertyChanged;
|
||||
}
|
||||
|
||||
public override void OnDeactivate()
|
||||
{
|
||||
_node.PropertyChanged -= NodeOnPropertyChanged;
|
||||
}
|
||||
|
||||
private void NodeOnPropertyChanged(object sender, PropertyChangedEventArgs e)
|
||||
{
|
||||
if (e.PropertyName == nameof(Node.Storage))
|
||||
OnPropertyChanged(nameof(Input));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
<UserControl x:Class="Artemis.VisualScripting.Nodes.Maths.CustomViews.MathExpressionNodeCustomView"
|
||||
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.Maths.CustomViews"
|
||||
mc:Ignorable="d"
|
||||
d:DesignHeight="450" d:DesignWidth="800">
|
||||
<TextBox VerticalAlignment="Bottom"
|
||||
HorizontalAlignment="Stretch"
|
||||
Text="{Binding Input}" />
|
||||
</UserControl>
|
||||
@@ -0,0 +1,112 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using Artemis.Core;
|
||||
using Artemis.VisualScripting.Nodes.Maths.CustomViewModels;
|
||||
using NoStringEvaluating.Contract;
|
||||
using NoStringEvaluating.Contract.Variables;
|
||||
using NoStringEvaluating.Models.Values;
|
||||
|
||||
namespace Artemis.VisualScripting.Nodes.Maths
|
||||
{
|
||||
[Node("Math Expression", "Outputs the result of a math expression.", "Math", OutputType = typeof(int))]
|
||||
public class MathExpressionNode : Node<MathExpressionNodeCustomViewModel>
|
||||
{
|
||||
private readonly INoStringEvaluator _evaluator;
|
||||
private readonly PinsVariablesContainer _variables;
|
||||
|
||||
#region Constructors
|
||||
public MathExpressionNode(INoStringEvaluator evaluator)
|
||||
: base("Math Expression", "Outputs the result of a math expression.")
|
||||
{
|
||||
_evaluator = evaluator;
|
||||
Output = CreateOutputPin<float>();
|
||||
Values = CreateInputPinCollection<float>("Values", 2);
|
||||
Values.PinAdded += (_, _) => SetPinNames();
|
||||
Values.PinRemoved += (_, _) => SetPinNames();
|
||||
_variables = new PinsVariablesContainer(Values);
|
||||
|
||||
SetPinNames();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Properties & Fields
|
||||
|
||||
public OutputPin<float> Output { get; }
|
||||
public InputPinCollection<float> Values { get; }
|
||||
|
||||
#endregion
|
||||
|
||||
#region Methods
|
||||
|
||||
public override void Evaluate()
|
||||
{
|
||||
if (Storage is string formula)
|
||||
Output.Value = (float) _evaluator.CalcNumber(formula, _variables);
|
||||
}
|
||||
|
||||
private void SetPinNames()
|
||||
{
|
||||
int index = 1;
|
||||
foreach (IPin value in Values)
|
||||
{
|
||||
value.Name = ExcelColumnFromNumber(index).ToLower();
|
||||
index++;
|
||||
}
|
||||
}
|
||||
|
||||
public static string ExcelColumnFromNumber(int column)
|
||||
{
|
||||
string columnString = "";
|
||||
decimal columnNumber = column;
|
||||
while (columnNumber > 0)
|
||||
{
|
||||
decimal currentLetterNumber = (columnNumber - 1) % 26;
|
||||
char currentLetter = (char) (currentLetterNumber + 65);
|
||||
columnString = currentLetter + columnString;
|
||||
columnNumber = (columnNumber - (currentLetterNumber + 1)) / 26;
|
||||
}
|
||||
|
||||
return columnString;
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
|
||||
public class PinsVariablesContainer : IVariablesContainer
|
||||
{
|
||||
private readonly InputPinCollection<float> _values;
|
||||
|
||||
public PinsVariablesContainer(InputPinCollection<float> values)
|
||||
{
|
||||
_values = values;
|
||||
}
|
||||
|
||||
#region Implementation of IVariablesContainer
|
||||
|
||||
/// <inheritdoc />
|
||||
public IVariable AddOrUpdate(string name, double value)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public EvaluatorValue GetValue(string name)
|
||||
{
|
||||
IPin pin = _values.FirstOrDefault(v => v.Name == name);
|
||||
return pin == null ? new EvaluatorValue(0) : new EvaluatorValue((double) pin.PinValue);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public bool TryGetValue(string name, out EvaluatorValue value)
|
||||
{
|
||||
IPin pin = _values.FirstOrDefault(v => v.Name == name);
|
||||
double unboxed = (float) pin.PinValue;
|
||||
value = pin == null ? new EvaluatorValue(0) : new EvaluatorValue(unboxed);
|
||||
|
||||
return pin != null;
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user