From 69b881b04478824a3c07d76bde1342bbc2331152 Mon Sep 17 00:00:00 2001 From: Robert Date: Mon, 28 Mar 2022 23:33:36 +0200 Subject: [PATCH] Nodes - Added static color node --- .../Converters/SKColorToColor2Converter.cs | 2 +- src/Artemis.UI.Shared/Styles/Artemis.axaml | 1 + .../Styles/ColorPickerButton.axaml | 51 +++++++++++++++++++ .../SKColorPropertyInputView.axaml | 39 +------------- .../Screens/VisualScripting/CableView.axaml | 15 +++--- .../StaticSKColorValueNodeCustomViewModel.cs | 42 ++++++++++++++- .../StaticSKColorValueNodeCustomView.axaml | 30 +++++++++++ .../StaticSKColorValueNodeCustomView.axaml.cs | 29 +++++++++++ .../StaticSKColorValueNodeCustomView.xaml | 15 ------ .../Nodes/Color/StaticSKColorValueNode.cs | 1 + .../MathExpressionNodeCustomViewModel.cs | 34 +++++++++++-- .../Nodes/Maths/ExpressionNode.cs | 11 +++- 12 files changed, 203 insertions(+), 67 deletions(-) rename src/{Artemis.UI => Artemis.UI.Shared}/Converters/SKColorToColor2Converter.cs (95%) create mode 100644 src/Artemis.UI.Shared/Styles/ColorPickerButton.axaml create mode 100644 src/Artemis.VisualScripting/Nodes/Color/CustomViews/StaticSKColorValueNodeCustomView.axaml create mode 100644 src/Artemis.VisualScripting/Nodes/Color/CustomViews/StaticSKColorValueNodeCustomView.axaml.cs delete mode 100644 src/Artemis.VisualScripting/Nodes/Color/CustomViews/StaticSKColorValueNodeCustomView.xaml diff --git a/src/Artemis.UI/Converters/SKColorToColor2Converter.cs b/src/Artemis.UI.Shared/Converters/SKColorToColor2Converter.cs similarity index 95% rename from src/Artemis.UI/Converters/SKColorToColor2Converter.cs rename to src/Artemis.UI.Shared/Converters/SKColorToColor2Converter.cs index bd27466a6..4b86659c4 100644 --- a/src/Artemis.UI/Converters/SKColorToColor2Converter.cs +++ b/src/Artemis.UI.Shared/Converters/SKColorToColor2Converter.cs @@ -4,7 +4,7 @@ using Avalonia.Data.Converters; using FluentAvalonia.UI.Media; using SkiaSharp; -namespace Artemis.UI.Converters; +namespace Artemis.UI.Shared.Converters; /// /// Converts into . diff --git a/src/Artemis.UI.Shared/Styles/Artemis.axaml b/src/Artemis.UI.Shared/Styles/Artemis.axaml index 5e51c1e8e..19d356099 100644 --- a/src/Artemis.UI.Shared/Styles/Artemis.axaml +++ b/src/Artemis.UI.Shared/Styles/Artemis.axaml @@ -33,6 +33,7 @@ + diff --git a/src/Artemis.UI.Shared/Styles/ColorPickerButton.axaml b/src/Artemis.UI.Shared/Styles/ColorPickerButton.axaml new file mode 100644 index 000000000..411d5649e --- /dev/null +++ b/src/Artemis.UI.Shared/Styles/ColorPickerButton.axaml @@ -0,0 +1,51 @@ + + + + + + + + + + + + + + + + diff --git a/src/Artemis.UI/DefaultTypes/PropertyInput/SKColorPropertyInputView.axaml b/src/Artemis.UI/DefaultTypes/PropertyInput/SKColorPropertyInputView.axaml index eadb2ff57..0fc593ab8 100644 --- a/src/Artemis.UI/DefaultTypes/PropertyInput/SKColorPropertyInputView.axaml +++ b/src/Artemis.UI/DefaultTypes/PropertyInput/SKColorPropertyInputView.axaml @@ -3,50 +3,14 @@ xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:controls="clr-namespace:FluentAvalonia.UI.Controls;assembly=FluentAvalonia" - xmlns:converters="clr-namespace:Artemis.UI.Converters" xmlns:propertyInput="clr-namespace:Artemis.UI.DefaultTypes.PropertyInput" xmlns:shared="clr-namespace:Artemis.UI.Shared.Converters;assembly=Artemis.UI.Shared" mc:Ignorable="d" d:DesignWidth="200" d:DesignHeight="450" x:Class="Artemis.UI.DefaultTypes.PropertyInput.SKColorPropertyInputView" x:DataType="propertyInput:SKColorPropertyInputViewModel"> - - - - - - + - \ No newline at end of file diff --git a/src/Artemis.UI/Screens/VisualScripting/CableView.axaml b/src/Artemis.UI/Screens/VisualScripting/CableView.axaml index 28865b75f..b264b08ed 100644 --- a/src/Artemis.UI/Screens/VisualScripting/CableView.axaml +++ b/src/Artemis.UI/Screens/VisualScripting/CableView.axaml @@ -57,19 +57,20 @@ VerticalAlignment="Center" HorizontalAlignment="Stretch" /> - - - - - + CornerRadius="4" + ClipToBounds="True"> + + + + + diff --git a/src/Artemis.VisualScripting/Nodes/Color/CustomViewModels/StaticSKColorValueNodeCustomViewModel.cs b/src/Artemis.VisualScripting/Nodes/Color/CustomViewModels/StaticSKColorValueNodeCustomViewModel.cs index 8e0fc74e3..e6f6b691f 100644 --- a/src/Artemis.VisualScripting/Nodes/Color/CustomViewModels/StaticSKColorValueNodeCustomViewModel.cs +++ b/src/Artemis.VisualScripting/Nodes/Color/CustomViewModels/StaticSKColorValueNodeCustomViewModel.cs @@ -1,11 +1,51 @@ using Artemis.Core; +using Artemis.UI.Shared.Services.NodeEditor; +using Artemis.UI.Shared.Services.NodeEditor.Commands; using Artemis.UI.Shared.VisualScripting; +using ReactiveUI; +using SkiaSharp; namespace Artemis.VisualScripting.Nodes.Color.CustomViewModels; public class StaticSKColorValueNodeCustomViewModel : CustomNodeViewModel { - public StaticSKColorValueNodeCustomViewModel(StaticSKColorValueNode node, INodeScript script) : base(node, script) + private readonly StaticSKColorValueNode _node; + private readonly INodeEditorService _nodeEditorService; + private bool _applyChanges; + private SKColor? _currentValue; + + public StaticSKColorValueNodeCustomViewModel(StaticSKColorValueNode node, INodeScript script, INodeEditorService nodeEditorService) : base(node, script) { + _node = node; + _nodeEditorService = nodeEditorService; + _applyChanges = true; + + NodeModified += (_, _) => CurrentValue = _node.Storage; + CurrentValue = _node.Storage; + } + + public SKColor? CurrentValue + { + get => _currentValue; + set + { + if (_applyChanges && value != _node.Storage) + _nodeEditorService.ExecuteCommand(Script, new UpdateStorage(_node, value ?? SKColor.Empty)); + this.RaiseAndSetIfChanged(ref _currentValue, value); + } + } + + public void PauseUpdating() + { + _applyChanges = false; + } + + public void ResumeUpdating() + { + _applyChanges = true; + + SKColor updatedValue = CurrentValue ?? SKColor.Empty; + if (updatedValue != _node.Storage) + _nodeEditorService.ExecuteCommand(Script, new UpdateStorage(_node, updatedValue)); } } \ No newline at end of file diff --git a/src/Artemis.VisualScripting/Nodes/Color/CustomViews/StaticSKColorValueNodeCustomView.axaml b/src/Artemis.VisualScripting/Nodes/Color/CustomViews/StaticSKColorValueNodeCustomView.axaml new file mode 100644 index 000000000..7232d2070 --- /dev/null +++ b/src/Artemis.VisualScripting/Nodes/Color/CustomViews/StaticSKColorValueNodeCustomView.axaml @@ -0,0 +1,30 @@ + + + + + + + + + + + + + + diff --git a/src/Artemis.VisualScripting/Nodes/Color/CustomViews/StaticSKColorValueNodeCustomView.axaml.cs b/src/Artemis.VisualScripting/Nodes/Color/CustomViews/StaticSKColorValueNodeCustomView.axaml.cs new file mode 100644 index 000000000..b9ca5c908 --- /dev/null +++ b/src/Artemis.VisualScripting/Nodes/Color/CustomViews/StaticSKColorValueNodeCustomView.axaml.cs @@ -0,0 +1,29 @@ +using Artemis.VisualScripting.Nodes.Color.CustomViewModels; +using Avalonia.Markup.Xaml; +using Avalonia.ReactiveUI; +using FluentAvalonia.UI.Controls; + +namespace Artemis.VisualScripting.Nodes.Color.CustomViews; + +public class StaticSKColorValueNodeCustomView : ReactiveUserControl +{ + public StaticSKColorValueNodeCustomView() + { + InitializeComponent(); + } + + private void InitializeComponent() + { + AvaloniaXamlLoader.Load(this); + } + + private void ColorPickerButton_OnFlyoutOpened(ColorPickerButton sender, EventArgs args) + { + ViewModel?.PauseUpdating(); + } + + private void ColorPickerButton_OnFlyoutClosed(ColorPickerButton sender, EventArgs args) + { + ViewModel?.ResumeUpdating(); + } +} \ No newline at end of file diff --git a/src/Artemis.VisualScripting/Nodes/Color/CustomViews/StaticSKColorValueNodeCustomView.xaml b/src/Artemis.VisualScripting/Nodes/Color/CustomViews/StaticSKColorValueNodeCustomView.xaml deleted file mode 100644 index eb02e7984..000000000 --- a/src/Artemis.VisualScripting/Nodes/Color/CustomViews/StaticSKColorValueNodeCustomView.xaml +++ /dev/null @@ -1,15 +0,0 @@ - - - - - - \ No newline at end of file diff --git a/src/Artemis.VisualScripting/Nodes/Color/StaticSKColorValueNode.cs b/src/Artemis.VisualScripting/Nodes/Color/StaticSKColorValueNode.cs index b6f0512f5..44ccafcbf 100644 --- a/src/Artemis.VisualScripting/Nodes/Color/StaticSKColorValueNode.cs +++ b/src/Artemis.VisualScripting/Nodes/Color/StaticSKColorValueNode.cs @@ -13,6 +13,7 @@ public class StaticSKColorValueNode : Node(); + Storage = new SKColor(255, 0, 0); } #endregion diff --git a/src/Artemis.VisualScripting/Nodes/Maths/CustomViewModels/MathExpressionNodeCustomViewModel.cs b/src/Artemis.VisualScripting/Nodes/Maths/CustomViewModels/MathExpressionNodeCustomViewModel.cs index 6453f5f34..b8ec2c498 100644 --- a/src/Artemis.VisualScripting/Nodes/Maths/CustomViewModels/MathExpressionNodeCustomViewModel.cs +++ b/src/Artemis.VisualScripting/Nodes/Maths/CustomViewModels/MathExpressionNodeCustomViewModel.cs @@ -1,9 +1,14 @@ -using Artemis.Core; +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 ReactiveUI; +using ReactiveUI.Validation.Components.Abstractions; using ReactiveUI.Validation.Extensions; +using ReactiveUI.Validation.Helpers; namespace Artemis.VisualScripting.Nodes.Maths.CustomViewModels; @@ -18,8 +23,30 @@ public class MathExpressionNodeCustomViewModel : CustomNodeViewModel _node = node; _nodeEditorService = nodeEditorService; - NodeModified += (_, _) => InputValue = _node.Storage; this.ValidationRule(vm => vm.InputValue, value => _node.IsSyntaxValid(value), value => _node.GetSyntaxErrors(value)); + + this.WhenActivated(d => + { + Observable.FromEventPattern>(x => _node.Values.PinAdded += x, x => _node.Values.PinAdded -= x) + .Subscribe(_ => + { + string? old = InputValue; + InputValue = null; + InputValue = old; + }) + .DisposeWith(d); + Observable.FromEventPattern>(x => _node.Values.PinRemoved += x, x => _node.Values.PinRemoved -= x) + .Subscribe(_ => + { + string? old = InputValue; + InputValue = null; + InputValue = old; + }) + .DisposeWith(d); + }); + + NodeModified += (_, _) => InputValue = _node.Storage; + InputValue = _node.Storage; } public string? InputValue @@ -30,7 +57,8 @@ public class MathExpressionNodeCustomViewModel : CustomNodeViewModel public void UpdateInputValue() { - if (!HasErrors && _node.Storage != InputValue) + // The value could be invalid but that's ok, we still want to save it + if (_node.Storage != InputValue) _nodeEditorService.ExecuteCommand(Script, new UpdateStorage(_node, InputValue)); } } \ No newline at end of file diff --git a/src/Artemis.VisualScripting/Nodes/Maths/ExpressionNode.cs b/src/Artemis.VisualScripting/Nodes/Maths/ExpressionNode.cs index 840760f3d..00aff4b3d 100644 --- a/src/Artemis.VisualScripting/Nodes/Maths/ExpressionNode.cs +++ b/src/Artemis.VisualScripting/Nodes/Maths/ExpressionNode.cs @@ -43,8 +43,15 @@ public class MathExpressionNode : Node