Nodes - Reorganized code

This commit is contained in:
Robert committed 2022-03-30 20:10:43 +02:00
1 parent 69b881b044
commit 7c83d5345f
73 files changed
+1055 -738

No files matched your search

@@ -0,0 +1,34 @@
using Artemis.Core;
namespace Artemis.VisualScripting.Nodes.Operators;
[Node("And", "Checks if all inputs are true.", "Operators", InputType = typeof(bool), OutputType = typeof(bool))]
public class AndNode : Node
{
#region Constructors
public AndNode()
: base("And", "Checks if all inputs are true.")
{
Input = CreateInputPinCollection<bool>();
Result = CreateOutputPin<bool>();
}
#endregion
#region Methods
public override void Evaluate()
{
Result.Value = Input.Values.All(v => v);
}
#endregion
#region Properties & Fields
public InputPinCollection<bool> Input { get; set; }
public OutputPin<bool> Result { get; }
#endregion
}
@@ -0,0 +1,36 @@
using Artemis.Core;
using Artemis.Core.Events;
using Artemis.VisualScripting.Nodes.Operators.Screens;
namespace Artemis.VisualScripting.Nodes.Operators;
[Node("Enum Equals", "Determines the equality between an input and a selected enum value", "Operators", InputType = typeof(Enum), OutputType = typeof(bool))]
public class EnumEqualsNode : Node<Enum, EnumEqualsNodeCustomViewModel>
{
public EnumEqualsNode() : base("Enum Equals", "Determines the equality between an input and a selected enum value")
{
InputPin = CreateInputPin<Enum>();
OutputPin = CreateOutputPin<bool>();
InputPin.PinConnected += InputPinOnPinConnected;
}
private void InputPinOnPinConnected(object? sender, SingleValueEventArgs<IPin> e)
{
if (Storage?.GetType() != InputPin.ConnectedTo.First().Type)
Storage = Enum.GetValues(InputPin.ConnectedTo.First().Type).Cast<Enum>().FirstOrDefault();
}
public InputPin<Enum> InputPin { get; }
public OutputPin<bool> OutputPin { get; }
#region Overrides of Node
/// <inheritdoc />
public override void Evaluate()
{
OutputPin.Value = InputPin.Value != null && InputPin.Value.Equals(Storage);
}
#endregion
}
@@ -0,0 +1,44 @@
using Artemis.Core;
namespace Artemis.VisualScripting.Nodes.Operators;
[Node("Equals", "Checks if the two inputs are equals.", "Operators", InputType = typeof(bool), OutputType = typeof(bool))]
public class EqualsNode : Node
{
#region Constructors
public EqualsNode()
: base("Equals", "Checks if the two inputs are equals.")
{
Input1 = CreateInputPin<object>();
Input2 = CreateInputPin<object>();
Result = CreateOutputPin<bool>();
}
#endregion
#region Methods
public override void Evaluate()
{
try
{
Result.Value = Equals(Input1.Value, Input2.Value);
}
catch
{
Result.Value = false;
}
}
#endregion
#region Properties & Fields
public InputPin<object> Input1 { get; }
public InputPin<object> Input2 { get; }
public OutputPin<bool> Result { get; }
#endregion
}
@@ -0,0 +1,57 @@
using System.Collections;
using Artemis.Core;
namespace Artemis.VisualScripting.Nodes.Operators;
[Node("Greater than", "Checks if the first input is greater than the second.", "Operators", InputType = typeof(object), OutputType = typeof(bool))]
public class GreaterThanNode : Node
{
#region Constructors
public GreaterThanNode()
: base("Greater than", "Checks if the first input is greater than the second.")
{
Input1 = CreateInputPin<object>();
Input2 = CreateInputPin<object>();
Result = CreateOutputPin<bool>();
}
#endregion
#region Methods
public override void Evaluate()
{
if (Input1.Value is Numeric numeric1 && Input2.Value is Numeric numeric2)
{
Result.Value = numeric1 > numeric2;
return;
}
if (Input2.Value != null && Input1.Value != null && Input1.Value.IsNumber() && Input2.Value.IsNumber())
{
Result.Value = Convert.ToSingle(Input1.Value) > Convert.ToSingle(Input2.Value);
return;
}
try
{
Result.Value = Comparer.DefaultInvariant.Compare(Input1.Value, Input2.Value) == 1;
}
catch
{
Result.Value = false;
}
}
#endregion
#region Properties & Fields
public InputPin<object> Input1 { get; }
public InputPin<object> Input2 { get; }
public OutputPin<bool> Result { get; }
#endregion
}
@@ -0,0 +1,57 @@
using System.Collections;
using Artemis.Core;
namespace Artemis.VisualScripting.Nodes.Operators;
[Node("Less than", "Checks if the first input is less than the second.", "Operators", InputType = typeof(object), OutputType = typeof(bool))]
public class LessThanNode : Node
{
#region Constructors
public LessThanNode()
: base("Less than", "Checks if the first input is less than the second.")
{
Input1 = CreateInputPin<object>();
Input2 = CreateInputPin<object>();
Result = CreateOutputPin<bool>();
}
#endregion
#region Methods
public override void Evaluate()
{
if (Input1.Value is Numeric numeric1 && Input2.Value is Numeric numeric2)
{
Result.Value = numeric1 < numeric2;
return;
}
if (Input2.Value != null && Input1.Value != null && Input1.Value.IsNumber() && Input2.Value.IsNumber())
{
Result.Value = Convert.ToSingle(Input1.Value) < Convert.ToSingle(Input2.Value);
return;
}
try
{
Result.Value = Comparer.DefaultInvariant.Compare(Input1.Value, Input2.Value) == -1;
}
catch
{
Result.Value = false;
}
}
#endregion
#region Properties & Fields
public InputPin<object> Input1 { get; }
public InputPin<object> Input2 { get; }
public OutputPin<bool> Result { get; }
#endregion
}
@@ -0,0 +1,34 @@
using Artemis.Core;
namespace Artemis.VisualScripting.Nodes.Operators;
[Node("Negate", "Negates the boolean.", "Operators", InputType = typeof(bool), OutputType = typeof(bool))]
public class NegateNode : Node
{
#region Constructors
public NegateNode()
: base("Negate", "Negates the boolean.")
{
Input = CreateInputPin<bool>();
Output = CreateOutputPin<bool>();
}
#endregion
#region Methods
public override void Evaluate()
{
Output.Value = !Input.Value;
}
#endregion
#region Properties & Fields
public InputPin<bool> Input { get; }
public OutputPin<bool> Output { get; }
#endregion
}
@@ -0,0 +1,34 @@
using Artemis.Core;
namespace Artemis.VisualScripting.Nodes.Operators;
[Node("Or", "Checks if any inputs are true.", "Operators", InputType = typeof(bool), OutputType = typeof(bool))]
public class OrNode : Node
{
#region Constructors
public OrNode()
: base("Or", "Checks if any inputs are true.")
{
Input = CreateInputPinCollection<bool>();
Result = CreateOutputPin<bool>();
}
#endregion
#region Methods
public override void Evaluate()
{
Result.Value = Input.Values.Any(v => v);
}
#endregion
#region Properties & Fields
public InputPinCollection<bool> Input { get; set; }
public OutputPin<bool> Result { get; }
#endregion
}
@@ -0,0 +1,15 @@
<UserControl xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:screens="clr-namespace:Artemis.VisualScripting.Nodes.Operators.Screens"
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
x:Class="Artemis.VisualScripting.Nodes.Operators.Screens.EnumEqualsNodeCustomView"
x:DataType="screens:EnumEqualsNodeCustomViewModel">
<ComboBox IsEnabled="{CompiledBinding EnumValues.Count}"
Items="{CompiledBinding EnumValues}"
SelectedItem="{CompiledBinding CurrentValue}"
PlaceholderText="Connect a pin..."
Classes="condensed"
VerticalAlignment="Center" />
</UserControl>
@@ -0,0 +1,18 @@
using Avalonia.Markup.Xaml;
using Avalonia.ReactiveUI;
namespace Artemis.VisualScripting.Nodes.Operators.Screens
{
public partial class EnumEqualsNodeCustomView : ReactiveUserControl<EnumEqualsNodeCustomViewModel>
{
public EnumEqualsNodeCustomView()
{
InitializeComponent();
}
private void InitializeComponent()
{
AvaloniaXamlLoader.Load(this);
}
}
}
@@ -0,0 +1,59 @@
using System.Collections.ObjectModel;
using System.Reactive.Linq;
using Artemis.Core;
using Artemis.Core.Events;
using Artemis.UI.Shared.Services.NodeEditor;
using Artemis.UI.Shared.Services.NodeEditor.Commands;
using Artemis.UI.Shared.VisualScripting;
using Avalonia.Controls.Mixins;
using DynamicData;
using ReactiveUI;
namespace Artemis.VisualScripting.Nodes.Operators.Screens;
public class EnumEqualsNodeCustomViewModel : CustomNodeViewModel
{
private readonly EnumEqualsNode _node;
private readonly INodeEditorService _nodeEditorService;
public EnumEqualsNodeCustomViewModel(EnumEqualsNode node, INodeScript script, INodeEditorService nodeEditorService) : base(node, script)
{
_node = node;
_nodeEditorService = nodeEditorService;
NodeModified += (_, _) => this.RaisePropertyChanged(nameof(CurrentValue));
this.WhenActivated(d =>
{
if (_node.InputPin.ConnectedTo.Any())
{
EnumValues.Clear();
EnumValues.AddRange(Enum.GetValues(_node.InputPin.ConnectedTo.First().Type).Cast<Enum>());
this.RaisePropertyChanged(nameof(CurrentValue));
}
Observable.FromEventPattern<SingleValueEventArgs<IPin>>(x => _node.InputPin.PinConnected += x, x => _node.InputPin.PinConnected -= x)
.Subscribe(p =>
{
EnumValues.AddRange(Enum.GetValues(p.EventArgs.Value.Type).Cast<Enum>());
this.RaisePropertyChanged(nameof(CurrentValue));
})
.DisposeWith(d);
Observable.FromEventPattern<SingleValueEventArgs<IPin>>(x => _node.InputPin.PinDisconnected += x, x => _node.InputPin.PinDisconnected -= x)
.Subscribe(_ => EnumValues.Clear())
.DisposeWith(d);
});
}
public ObservableCollection<Enum> EnumValues { get; } = new();
public Enum? CurrentValue
{
get => _node.Storage;
set
{
if (value != null && !Equals(_node.Storage, value))
_nodeEditorService.ExecuteCommand(Script, new UpdateStorage<Enum>(_node, value));
}
}
}
@@ -0,0 +1,34 @@
using Artemis.Core;
namespace Artemis.VisualScripting.Nodes.Operators;
[Node("Exclusive Or", "Checks if one of the inputs is true.", "Operators", InputType = typeof(bool), OutputType = typeof(bool))]
public class XorNode : Node
{
#region Constructors
public XorNode()
: base("Exclusive Or", "Checks if one of the inputs is true.")
{
Input = CreateInputPinCollection<bool>();
Result = CreateOutputPin<bool>();
}
#endregion
#region Methods
public override void Evaluate()
{
Result.Value = Input.Values.Count(v => v) == 1;
}
#endregion
#region Properties & Fields
public InputPinCollection<bool> Input { get; set; }
public OutputPin<bool> Result { get; }
#endregion
}