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
{
/// <summary>
/// Gets the node the view models belongs to
/// </summary>
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
#region Constructors
public VisualScriptNodePresenter()
{
Unloaded += OnUnloaded;
}
#endregion
#region Methods
protected override Size MeasureOverride(Size constraint)
@ -144,8 +153,13 @@ namespace Artemis.VisualScripting.Editor.Controls
private void GetCustomViewModel()
{
CustomViewModel?.OnDeactivate();
if (Node?.Node is CustomViewModelNode customViewModelNode)
{
CustomViewModel = customViewModelNode.GetCustomViewModel();
CustomViewModel.OnActivate();
}
else
CustomViewModel = null;
}
@ -156,6 +170,12 @@ namespace Artemis.VisualScripting.Editor.Controls
presenter.GetCustomViewModel();
}
private void OnUnloaded(object sender, RoutedEventArgs e)
{
CustomViewModel?.OnDeactivate();
CustomViewModel = null;
}
#endregion
}
}

View File

@ -60,10 +60,21 @@ namespace Artemis.VisualScripting.Nodes
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;
Integer.Value = value;
return value;
}
#endregion
@ -95,12 +106,69 @@ namespace Artemis.VisualScripting.Nodes
public override void Evaluate()
{
if (!double.TryParse(Input.Value?.ToString(), out double value))
value = 0;
Double.Value = Input.Value switch
{
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
}
}

View File

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

View File

@ -1,4 +1,5 @@
using Artemis.Core;
using System.ComponentModel;
using Artemis.Core;
using Artemis.Core.Modules;
using Stylet;
@ -26,7 +27,6 @@ namespace Artemis.VisualScripting.Nodes.CustomViewModels
set
{
_node.DataModelPath = value;
OnPropertyChanged(nameof(DataModelPath));
if (_node.DataModelPath != null)
{
@ -41,8 +41,8 @@ namespace Artemis.VisualScripting.Nodes.CustomViewModels
_node.UpdateOutputPin();
}
}
public void Initialize()
public override void OnActivate()
{
if (Modules != null)
return;
@ -52,17 +52,19 @@ namespace Artemis.VisualScripting.Nodes.CustomViewModels
Modules.Add(scriptProfile.Configuration.Module);
else if (_node.Script.Context is ProfileConfiguration profileConfiguration && profileConfiguration.Module != null)
Modules.Add(profileConfiguration.Module);
_node.PropertyChanged += NodeOnPropertyChanged;
}
#region Overrides of CustomNodeViewModel
/// <inheritdoc />
protected override void OnDisplay()
public override void OnDeactivate()
{
Initialize();
base.OnDisplay();
_node.PropertyChanged -= NodeOnPropertyChanged;
}
#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));
}
#region Overrides of CustomNodeViewModel
/// <inheritdoc />
protected override void OnDisplay()
public override void OnActivate()
{
GetProfileElements();
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
{
@ -13,12 +13,24 @@ namespace Artemis.VisualScripting.Nodes.CustomViewModels
public double Input
{
get => (double) _node.Storage;
set
{
_node.Storage = value;
get => (double) (_node.Storage ?? 0.0);
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));
}
}
}
@ -33,12 +45,24 @@ namespace Artemis.VisualScripting.Nodes.CustomViewModels
public float Input
{
get => (float)_node.Storage;
set
{
_node.Storage = value;
get => (float) (_node.Storage ?? 0f);
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));
}
}
}
@ -53,17 +77,24 @@ namespace Artemis.VisualScripting.Nodes.CustomViewModels
public int Input
{
get
{
if (_node.Storage is long longInput)
return (int)longInput;
return (int)_node.Storage;
}
set
{
_node.Storage = value;
get => _node.Storage is long longInput ? (int) longInput : (int) (_node.Storage ?? 0);
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));
}
}
}
@ -78,12 +109,24 @@ namespace Artemis.VisualScripting.Nodes.CustomViewModels
public string Input
{
get => (string)_node.Storage;
set
{
_node.Storage = value;
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));
}
}
}
}

View File

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