diff --git a/src/Artemis.Core/VisualScripting/CustomNodeViewModel.cs b/src/Artemis.Core/VisualScripting/CustomNodeViewModel.cs index 5975ed806..7efc9956e 100644 --- a/src/Artemis.Core/VisualScripting/CustomNodeViewModel.cs +++ b/src/Artemis.Core/VisualScripting/CustomNodeViewModel.cs @@ -1,9 +1,23 @@ -using Newtonsoft.Json; - -namespace Artemis.Core +namespace Artemis.Core { + /// + /// Represents a custom view model for a + /// public interface ICustomNodeViewModel { + /// + /// Gets the node the view models belongs to + /// public INode Node { get; } + + /// + /// Called whenever the custom view model is activated + /// + void OnActivate(); + + /// + /// Called whenever the custom view model is closed + /// + void OnDeactivate(); } } \ No newline at end of file diff --git a/src/Artemis.VisualScripting/Editor/Controls/VisualScriptNodePresenter.cs b/src/Artemis.VisualScripting/Editor/Controls/VisualScriptNodePresenter.cs index cc88904df..4532b9d8b 100644 --- a/src/Artemis.VisualScripting/Editor/Controls/VisualScriptNodePresenter.cs +++ b/src/Artemis.VisualScripting/Editor/Controls/VisualScriptNodePresenter.cs @@ -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 } } \ No newline at end of file diff --git a/src/Artemis.VisualScripting/Nodes/ConvertNodes.cs b/src/Artemis.VisualScripting/Nodes/ConvertNodes.cs index f9d52f8dd..29215d8d1 100644 --- a/src/Artemis.VisualScripting/Nodes/ConvertNodes.cs +++ b/src/Artemis.VisualScripting/Nodes/ConvertNodes.cs @@ -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 Input { get; } + + public OutputPin Float { get; } + + #endregion + + #region Constructors + + public ConvertToFloatNode() + : base("To Float", "Converts the input to a float.") + { + Input = CreateInputPin(); + Float = CreateOutputPin(); + } + + #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 + } +} \ No newline at end of file diff --git a/src/Artemis.VisualScripting/Nodes/CustomViewModels/CustomNodeViewModel.cs b/src/Artemis.VisualScripting/Nodes/CustomViewModels/CustomNodeViewModel.cs index fca28dbbb..1c7d23809 100644 --- a/src/Artemis.VisualScripting/Nodes/CustomViewModels/CustomNodeViewModel.cs +++ b/src/Artemis.VisualScripting/Nodes/CustomViewModels/CustomNodeViewModel.cs @@ -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 /// - public void AttachView(UIElement view) - { - View = view; - OnDisplay(); - } - - protected virtual void OnDisplay() + public virtual void OnActivate() { } /// - public UIElement View { get; private set; } + public virtual void OnDeactivate() + { + } #endregion } diff --git a/src/Artemis.VisualScripting/Nodes/CustomViewModels/DataModelNodeCustomViewModel.cs b/src/Artemis.VisualScripting/Nodes/CustomViewModels/DataModelNodeCustomViewModel.cs index 126a1815d..dccccb92a 100644 --- a/src/Artemis.VisualScripting/Nodes/CustomViewModels/DataModelNodeCustomViewModel.cs +++ b/src/Artemis.VisualScripting/Nodes/CustomViewModels/DataModelNodeCustomViewModel.cs @@ -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 - - /// - 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)); + } } } \ No newline at end of file diff --git a/src/Artemis.VisualScripting/Nodes/CustomViewModels/LayerPropertyNodeCustomViewModel.cs b/src/Artemis.VisualScripting/Nodes/CustomViewModels/LayerPropertyNodeCustomViewModel.cs index edf84d7e1..05706be9a 100644 --- a/src/Artemis.VisualScripting/Nodes/CustomViewModels/LayerPropertyNodeCustomViewModel.cs +++ b/src/Artemis.VisualScripting/Nodes/CustomViewModels/LayerPropertyNodeCustomViewModel.cs @@ -66,15 +66,15 @@ namespace Artemis.VisualScripting.Nodes.CustomViewModels NotifyOfPropertyChange(nameof(SelectedLayerProperty)); } - #region Overrides of CustomNodeViewModel - - /// - protected override void OnDisplay() + public override void OnActivate() { GetProfileElements(); GetLayerProperties(); } - #endregion + public override void OnDeactivate() + { + base.OnDeactivate(); + } } } \ No newline at end of file diff --git a/src/Artemis.VisualScripting/Nodes/CustomViewModels/StaticValueNodeViewModels.cs b/src/Artemis.VisualScripting/Nodes/CustomViewModels/StaticValueNodeViewModels.cs index 17b09f66e..82087eb87 100644 --- a/src/Artemis.VisualScripting/Nodes/CustomViewModels/StaticValueNodeViewModels.cs +++ b/src/Artemis.VisualScripting/Nodes/CustomViewModels/StaticValueNodeViewModels.cs @@ -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)); - } } } } \ No newline at end of file diff --git a/src/Artemis.VisualScripting/Nodes/DataModelNode.cs b/src/Artemis.VisualScripting/Nodes/DataModelNode.cs index 64b28a169..4f7a825c4 100644 --- a/src/Artemis.VisualScripting/Nodes/DataModelNode.cs +++ b/src/Artemis.VisualScripting/Nodes/DataModelNode.cs @@ -9,13 +9,20 @@ namespace Artemis.VisualScripting.Nodes [Node("Data Model-Value", "Outputs a selectable data model value.")] public class DataModelNode : Node { + 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;