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

Nodes - Added static color node

This commit is contained in:
Robert 2022-03-28 23:33:36 +02:00
parent 28534cb57a
commit 69b881b044
12 changed files with 203 additions and 67 deletions

View File

@ -4,7 +4,7 @@ using Avalonia.Data.Converters;
using FluentAvalonia.UI.Media;
using SkiaSharp;
namespace Artemis.UI.Converters;
namespace Artemis.UI.Shared.Converters;
/// <summary>
/// Converts <see cref="SKColor" /> into <see cref="Color2" />.

View File

@ -33,6 +33,7 @@
<StyleInclude Source="/Styles/Border.axaml" />
<StyleInclude Source="/Styles/Button.axaml" />
<StyleInclude Source="/Styles/Condensed.axaml" />
<StyleInclude Source="/Styles/ColorPickerButton.axaml" />
<StyleInclude Source="/Styles/TextBlock.axaml" />
<StyleInclude Source="/Styles/Sidebar.axaml" />
<StyleInclude Source="/Styles/InfoBar.axaml" />

View File

@ -0,0 +1,51 @@
<Styles xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:controls="clr-namespace:FluentAvalonia.UI.Controls;assembly=FluentAvalonia">
<Design.PreviewWith>
<Border Padding="20">
<Grid Height="24" ColumnDefinitions="*">
<TextBox Classes="condensed"
Text="#FFFF0000"
Padding="2 2 30 2">
</TextBox>
<controls:ColorPickerButton Classes="contained-color-picker-button"
ShowAcceptDismissButtons="False"/>
</Grid>
</Border>
</Design.PreviewWith>
<!-- Add Styles Here -->
<Style Selector="controls|ColorPickerButton.contained-color-picker-button">
<Setter Property="Padding" Value="0" />
<Setter Property="Margin" Value="4 0" />
<Setter Property="Background" Value="Transparent" />
<Setter Property="MinHeight" Value="00" />
<Setter Property="MinWidth" Value="0" />
<Setter Property="VerticalAlignment" Value="Center" />
<Setter Property="HorizontalAlignment" Value="Right" />
<Setter Property="Cursor" Value="Hand" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<controls:Button Name="MainButton"
Padding="0"
BorderThickness="0"
CornerRadius="{TemplateBinding CornerRadius}">
<Border BorderBrush="{DynamicResource ColorPickerButtonOutline}"
BorderThickness="1"
HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
MinWidth="18" MinHeight="18"
Background="{TemplateBinding Color, Converter={StaticResource ColorBrushConv}}"
CornerRadius="{TemplateBinding CornerRadius}">
<controls:ColorPicker Name="ColorPicker" IsVisible="False" />
</Border>
</controls:Button>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style Selector="controls|ColorPickerButton.contained-color-picker-button /template/ Viewbox">
<Setter Property="IsVisible" Value="False"></Setter>
</Style>
</Styles>

View File

@ -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">
<UserControl.Styles>
<Style Selector="controls|ColorPickerButton.contained-color-picker-button">
<Setter Property="Padding" Value="0" />
<Setter Property="Margin" Value="4 0" />
<Setter Property="Background" Value="Transparent" />
<Setter Property="MinHeight" Value="00" />
<Setter Property="MinWidth" Value="0" />
<Setter Property="VerticalAlignment" Value="Center" />
<Setter Property="HorizontalAlignment" Value="Right" />
<Setter Property="Cursor" Value="Hand" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<controls:Button Name="MainButton"
Padding="0"
BorderThickness="0"
CornerRadius="{TemplateBinding CornerRadius}">
<Border BorderBrush="{DynamicResource ColorPickerButtonOutline}"
BorderThickness="1"
HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
MinWidth="18" MinHeight="18"
Background="{TemplateBinding Color, Converter={StaticResource ColorBrushConv}}"
CornerRadius="{TemplateBinding CornerRadius}">
<controls:ColorPicker Name="ColorPicker" IsVisible="False" />
</Border>
</controls:Button>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style Selector="controls|ColorPickerButton.contained-color-picker-button /template/ Viewbox">
<Setter Property="IsVisible" Value="False"></Setter>
</Style>
</UserControl.Styles>
<UserControl.Resources>
<shared:SKColorToStringConverter x:Key="SKColorToStringConverter" />
<converters:SKColorToColor2Converter x:Key="SKColorToColor2Converter" />
<shared:SKColorToColor2Converter x:Key="SKColorToColor2Converter" />
</UserControl.Resources>
<Grid Height="24" ColumnDefinitions="*">
<TextBox Classes="condensed"
@ -59,5 +23,4 @@
FlyoutOpened="ColorPickerButton_OnFlyoutOpened"
FlyoutClosed="ColorPickerButton_OnFlyoutClosed" />
</Grid>
</UserControl>

View File

@ -57,19 +57,20 @@
VerticalAlignment="Center"
HorizontalAlignment="Stretch" />
<Border Margin="5 0 0 0"
VerticalAlignment="Center"
VerticalAlignment="Bottom"
HorizontalAlignment="Right"
BorderThickness="1"
MinWidth="18"
MinHeight="18"
Background="{DynamicResource CheckerboardBrush}"
BorderBrush="{DynamicResource ColorPickerButtonOutline}"
CornerRadius="2">
<Ellipse Stroke="{DynamicResource NormalBorderBrush}">
<Ellipse.Fill>
<SolidColorBrush Color="{Binding Converter={StaticResource SKColorToColorConverter}}" />
</Ellipse.Fill>
</Ellipse>
CornerRadius="4"
ClipToBounds="True">
<Border CornerRadius="4">
<Border.Background>
<SolidColorBrush Color="{Binding Converter={StaticResource SKColorToColorConverter}}" />
</Border.Background>
</Border>
</Border>
</StackPanel>
</DataTemplate>

View File

@ -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<SKColor>(_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<SKColor>(_node, updatedValue));
}
}

View File

@ -0,0 +1,30 @@
<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:converters="clr-namespace:Artemis.UI.Shared.Converters;assembly=Artemis.UI.Shared"
xmlns:controls="clr-namespace:FluentAvalonia.UI.Controls;assembly=FluentAvalonia"
xmlns:customViewModels="clr-namespace:Artemis.VisualScripting.Nodes.Color.CustomViewModels"
xmlns:behaviors="clr-namespace:Artemis.UI.Shared.Behaviors;assembly=Artemis.UI.Shared"
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
x:Class="Artemis.VisualScripting.Nodes.Color.CustomViews.StaticSKColorValueNodeCustomView"
x:DataType="customViewModels:StaticSKColorValueNodeCustomViewModel">
<UserControl.Resources>
<converters:SKColorToStringConverter x:Key="SKColorToStringConverter" />
<converters:SKColorToColor2Converter x:Key="SKColorToColor2Converter" />
</UserControl.Resources>
<Grid Height="24" ColumnDefinitions="*" Width="110">
<TextBox Classes="condensed"
Watermark="#00000000"
Padding="2 2 30 2">
<Interaction.Behaviors>
<behaviors:LostFocusTextBoxBindingBehavior Text="{CompiledBinding CurrentValue, Converter={StaticResource SKColorToStringConverter}}"/>
</Interaction.Behaviors>
</TextBox>
<controls:ColorPickerButton Classes="contained-color-picker-button"
Color="{CompiledBinding CurrentValue, Converter={StaticResource SKColorToColor2Converter}}"
ShowAcceptDismissButtons="False"
FlyoutOpened="ColorPickerButton_OnFlyoutOpened"
FlyoutClosed="ColorPickerButton_OnFlyoutClosed" />
</Grid>
</UserControl>

View File

@ -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<StaticSKColorValueNodeCustomViewModel>
{
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();
}
}

View File

@ -1,15 +0,0 @@
<UserControl x:Class="Artemis.VisualScripting.Nodes.Color.CustomViews.StaticSKColorValueNodeCustomView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:shared="clr-namespace:Artemis.UI.Shared;assembly=Artemis.UI.Shared"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800">
<UserControl.Resources>
<shared:SKColorToColorConverter x:Key="SKColorToColorConverter" />
</UserControl.Resources>
<shared:ColorPicker VerticalAlignment="Center"
HorizontalAlignment="Stretch"
Color="{Binding Node.Storage, Converter={StaticResource SKColorToColorConverter}}" />
</UserControl>

View File

@ -13,6 +13,7 @@ public class StaticSKColorValueNode : Node<SKColor, StaticSKColorValueNodeCustom
: base("Color", "Outputs a configurable color value.")
{
Output = CreateOutputPin<SKColor>();
Storage = new SKColor(255, 0, 0);
}
#endregion

View File

@ -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<SingleValueEventArgs<IPin>>(x => _node.Values.PinAdded += x, x => _node.Values.PinAdded -= x)
.Subscribe(_ =>
{
string? old = InputValue;
InputValue = null;
InputValue = old;
})
.DisposeWith(d);
Observable.FromEventPattern<SingleValueEventArgs<IPin>>(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<string>(_node, InputValue));
}
}

View File

@ -43,8 +43,15 @@ public class MathExpressionNode : Node<string, MathExpressionNodeCustomViewModel
public override void Evaluate()
{
if (Storage != null)
Output.Value = new Numeric(_evaluator.CalcNumber(Storage, _variables));
try
{
if (Storage != null)
Output.Value = new Numeric(_evaluator.CalcNumber(Storage, _variables));
}
catch
{
Output.Value = new Numeric(0);
}
}
private void SetPinNames()