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

Nodes - Added to float node

Nodes - Updated almost all custom VMs to stay in sync across multiple instances
This commit is contained in:
Robert 2021-08-22 00:56:23 +02:00
parent ed33d8a148
commit 7933bf7ee9
8 changed files with 217 additions and 65 deletions

View File

@ -1,9 +1,23 @@
using Newtonsoft.Json; namespace Artemis.Core
namespace Artemis.Core
{ {
/// <summary>
/// Represents a custom view model for a <see cref="INode" />
/// </summary>
public interface ICustomNodeViewModel public interface ICustomNodeViewModel
{ {
/// <summary>
/// Gets the node the view models belongs to
/// </summary>
public INode Node { get; } public INode Node { get; }
/// <summary>
/// Called whenever the custom view model is activated
/// </summary>
void OnActivate();
/// <summary>
/// Called whenever the custom view model is closed
/// </summary>
void OnDeactivate();
} }
} }

View File

@ -50,6 +50,15 @@ namespace Artemis.VisualScripting.Editor.Controls
#endregion #endregion
#region Constructors
public VisualScriptNodePresenter()
{
Unloaded += OnUnloaded;
}
#endregion
#region Methods #region Methods
protected override Size MeasureOverride(Size constraint) protected override Size MeasureOverride(Size constraint)
@ -144,8 +153,13 @@ namespace Artemis.VisualScripting.Editor.Controls
private void GetCustomViewModel() private void GetCustomViewModel()
{ {
CustomViewModel?.OnDeactivate();
if (Node?.Node is CustomViewModelNode customViewModelNode) if (Node?.Node is CustomViewModelNode customViewModelNode)
{
CustomViewModel = customViewModelNode.GetCustomViewModel(); CustomViewModel = customViewModelNode.GetCustomViewModel();
CustomViewModel.OnActivate();
}
else else
CustomViewModel = null; CustomViewModel = null;
} }
@ -156,6 +170,12 @@ namespace Artemis.VisualScripting.Editor.Controls
presenter.GetCustomViewModel(); presenter.GetCustomViewModel();
} }
private void OnUnloaded(object sender, RoutedEventArgs e)
{
CustomViewModel?.OnDeactivate();
CustomViewModel = null;
}
#endregion #endregion
} }
} }

View File

@ -60,10 +60,21 @@ namespace Artemis.VisualScripting.Nodes
public override void Evaluate() public override void Evaluate()
{ {
if (!int.TryParse(Input.Value?.ToString(), out int value)) Integer.Value = Input.Value switch
{
int input => input,
double input => (int) input,
float input => (int) input,
_ => TryParse(Input.Value)
};
}
private int TryParse(object input)
{
if (!int.TryParse(input?.ToString(), out int value))
value = 0; value = 0;
Integer.Value = value; return value;
} }
#endregion #endregion
@ -95,10 +106,67 @@ namespace Artemis.VisualScripting.Nodes
public override void Evaluate() public override void Evaluate()
{ {
if (!double.TryParse(Input.Value?.ToString(), out double value)) Double.Value = Input.Value switch
value = 0; {
int input => input,
double input => input,
float input => input,
_ => TryParse(Input.Value)
};
}
Double.Value = value; private double TryParse(object input)
{
if (!double.TryParse(input?.ToString(), out double value))
value = 0.0;
return value;
}
#endregion
}
[Node("To Float", "Converts the input to a float.")]
public class ConvertToFloatNode : Node
{
#region Properties & Fields
public InputPin<object> Input { get; }
public OutputPin<float> Float { get; }
#endregion
#region Constructors
public ConvertToFloatNode()
: base("To Float", "Converts the input to a float.")
{
Input = CreateInputPin<object>();
Float = CreateOutputPin<float>();
}
#endregion
#region Methods
public override void Evaluate()
{
Float.Value = Input.Value switch
{
int input => input,
double input => (float) input,
float input => input,
_ => TryParse(Input.Value)
};
}
private float TryParse(object input)
{
if (!float.TryParse(input?.ToString(), out float value))
value = 0.0f;
return value;
} }
#endregion #endregion

View File

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

View File

@ -1,4 +1,5 @@
using Artemis.Core; using System.ComponentModel;
using Artemis.Core;
using Artemis.Core.Modules; using Artemis.Core.Modules;
using Stylet; using Stylet;
@ -26,7 +27,6 @@ namespace Artemis.VisualScripting.Nodes.CustomViewModels
set set
{ {
_node.DataModelPath = value; _node.DataModelPath = value;
OnPropertyChanged(nameof(DataModelPath));
if (_node.DataModelPath != null) if (_node.DataModelPath != null)
{ {
@ -42,7 +42,7 @@ namespace Artemis.VisualScripting.Nodes.CustomViewModels
} }
} }
public void Initialize() public override void OnActivate()
{ {
if (Modules != null) if (Modules != null)
return; return;
@ -52,17 +52,19 @@ namespace Artemis.VisualScripting.Nodes.CustomViewModels
Modules.Add(scriptProfile.Configuration.Module); Modules.Add(scriptProfile.Configuration.Module);
else if (_node.Script.Context is ProfileConfiguration profileConfiguration && profileConfiguration.Module != null) else if (_node.Script.Context is ProfileConfiguration profileConfiguration && profileConfiguration.Module != null)
Modules.Add(profileConfiguration.Module); Modules.Add(profileConfiguration.Module);
_node.PropertyChanged += NodeOnPropertyChanged;
} }
#region Overrides of CustomNodeViewModel public override void OnDeactivate()
/// <inheritdoc />
protected override void OnDisplay()
{ {
Initialize(); _node.PropertyChanged -= NodeOnPropertyChanged;
base.OnDisplay();
} }
#endregion private void NodeOnPropertyChanged(object sender, PropertyChangedEventArgs e)
{
if (e.PropertyName == nameof(DataModelNode.DataModelPath))
OnPropertyChanged(nameof(DataModelPath));
}
} }
} }

View File

@ -66,15 +66,15 @@ namespace Artemis.VisualScripting.Nodes.CustomViewModels
NotifyOfPropertyChange(nameof(SelectedLayerProperty)); NotifyOfPropertyChange(nameof(SelectedLayerProperty));
} }
#region Overrides of CustomNodeViewModel public override void OnActivate()
/// <inheritdoc />
protected override void OnDisplay()
{ {
GetProfileElements(); GetProfileElements();
GetLayerProperties(); GetLayerProperties();
} }
#endregion public override void OnDeactivate()
{
base.OnDeactivate();
}
} }
} }

View File

@ -1,4 +1,4 @@
using Artemis.Core; using System.ComponentModel;
namespace Artemis.VisualScripting.Nodes.CustomViewModels namespace Artemis.VisualScripting.Nodes.CustomViewModels
{ {
@ -13,12 +13,24 @@ namespace Artemis.VisualScripting.Nodes.CustomViewModels
public double Input public double Input
{ {
get => (double) _node.Storage; get => (double) (_node.Storage ?? 0.0);
set set => _node.Storage = value;
{ }
_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)); OnPropertyChanged(nameof(Input));
}
} }
} }
@ -33,12 +45,24 @@ namespace Artemis.VisualScripting.Nodes.CustomViewModels
public float Input public float Input
{ {
get => (float)_node.Storage; get => (float) (_node.Storage ?? 0f);
set set => _node.Storage = value;
{ }
_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)); OnPropertyChanged(nameof(Input));
}
} }
} }
@ -53,17 +77,24 @@ namespace Artemis.VisualScripting.Nodes.CustomViewModels
public int Input public int Input
{ {
get get => _node.Storage is long longInput ? (int) longInput : (int) (_node.Storage ?? 0);
{ set => _node.Storage = value;
if (_node.Storage is long longInput) }
return (int)longInput;
return (int)_node.Storage; public override void OnActivate()
} {
set _node.PropertyChanged += NodeOnPropertyChanged;
{ }
_node.Storage = value;
public override void OnDeactivate()
{
_node.PropertyChanged -= NodeOnPropertyChanged;
}
private void NodeOnPropertyChanged(object? sender, PropertyChangedEventArgs e)
{
if (e.PropertyName == nameof(Node.Storage))
OnPropertyChanged(nameof(Input)); OnPropertyChanged(nameof(Input));
}
} }
} }
@ -78,12 +109,24 @@ namespace Artemis.VisualScripting.Nodes.CustomViewModels
public string Input public string Input
{ {
get => (string)_node.Storage; get => (string) _node.Storage;
set set => _node.Storage = value;
{ }
_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)); OnPropertyChanged(nameof(Input));
}
} }
} }
} }

View File

@ -9,13 +9,20 @@ namespace Artemis.VisualScripting.Nodes
[Node("Data Model-Value", "Outputs a selectable data model value.")] [Node("Data Model-Value", "Outputs a selectable data model value.")]
public class DataModelNode : Node<DataModelNodeCustomViewModel> public class DataModelNode : Node<DataModelNodeCustomViewModel>
{ {
private DataModelPath _dataModelPath;
public DataModelNode() : base("Data Model", "Outputs a selectable data model value") public DataModelNode() : base("Data Model", "Outputs a selectable data model value")
{ {
} }
public INodeScript Script { get; private set; } public INodeScript Script { get; private set; }
public OutputPin Output { get; private set; } public OutputPin Output { get; private set; }
public DataModelPath DataModelPath { get; set; }
public DataModelPath DataModelPath
{
get => _dataModelPath;
set => SetAndNotify(ref _dataModelPath , value);
}
public override void Initialize(INodeScript script) public override void Initialize(INodeScript script)
{ {
@ -36,7 +43,10 @@ namespace Artemis.VisualScripting.Nodes
public void UpdateOutputPin() public void UpdateOutputPin()
{ {
if (Pins.Contains(Output)) if (Output != null && Output.Type == DataModelPath?.GetPropertyType())
return;
if (Output != null && Pins.Contains(Output))
{ {
RemovePin(Output); RemovePin(Output);
Output = null; Output = null;