Browse Source

Hide Y value on math node if not needed

CPKreuz 1 year ago
parent
commit
b7a30eeb07

+ 5 - 0
src/PixiEditor.ChangeableDocument/Enums/MathNodeMode.cs

@@ -10,3 +10,8 @@ public enum MathNodeMode
     Cos,
     Tan,
 }
+
+public static class MathNodeModeExtensions
+{
+    public static bool UsesYValue(this MathNodeMode mode) => !(mode is >= MathNodeMode.Sin and <= MathNodeMode.Tan);
+}

+ 7 - 0
src/PixiEditor/Models/Events/NodePropertyValueChanged.cs

@@ -0,0 +1,7 @@
+using PixiEditor.Models.Handlers;
+
+namespace PixiEditor.Models.Events;
+
+public delegate void NodePropertyValueChanged(INodePropertyHandler property, NodePropertyValueChangedArgs args);
+
+public record NodePropertyValueChangedArgs(object OldValue, object NewValue);

+ 3 - 0
src/PixiEditor/Models/Handlers/INodePropertyHandler.cs

@@ -1,4 +1,5 @@
 using System.Collections.ObjectModel;
+using PixiEditor.Models.Events;
 
 namespace PixiEditor.Models.Handlers;
 
@@ -10,5 +11,7 @@ public interface INodePropertyHandler
     public bool IsInput { get; }
     public INodePropertyHandler? ConnectedOutput { get; set; }
     public ObservableCollection<INodePropertyHandler> ConnectedInputs { get; }
+
+    public event NodePropertyValueChanged ValueChanged;
     public INodeHandler Node { get; set; }
 }

+ 1 - 1
src/PixiEditor/Styles/Templates/NodePropertyViewTemplate.axaml

@@ -6,7 +6,7 @@
         <Setter Property="ClipToBounds" Value="False" />
         <Setter Property="Template">
             <ControlTemplate>
-                <Grid Margin="-5, 2" ColumnDefinitions="15, *, 15" MinHeight="18">
+                <Grid Margin="-5, 2" ColumnDefinitions="15, *, 15" MinHeight="18" IsVisible="{Binding DataContext.IsVisible, RelativeSource={RelativeSource TemplatedParent}}">
                     <properties:NodeSocket Name="PART_InputSocket"
                                            ClipToBounds="False"
                                            Node="{Binding DataContext.Node, RelativeSource={RelativeSource TemplatedParent}}"

+ 26 - 1
src/PixiEditor/ViewModels/Document/Nodes/MathNodeViewModel.cs

@@ -1,8 +1,33 @@
 using PixiEditor.ChangeableDocument.Changeables.Graph.Nodes;
+using PixiEditor.ChangeableDocument.Enums;
 using PixiEditor.Extensions.Common.Localization;
+using PixiEditor.Models.Events;
+using PixiEditor.Models.Handlers;
 using PixiEditor.ViewModels.Nodes;
+using PixiEditor.ViewModels.Nodes.Properties;
 
 namespace PixiEditor.ViewModels.Document.Nodes;
 
 [NodeViewModel("MATH_NODE", "NUMBERS", "\ue90e")]
-internal class MathNodeViewModel : NodeViewModel<MathNode>;
+internal class MathNodeViewModel : NodeViewModel<MathNode>
+{
+    private GenericEnumPropertyViewModel Mode { get; set; }
+    
+    private NodePropertyViewModel Y { get; set; }
+    
+    public override void OnInitialized()
+    {
+        Mode = FindInputProperty("Mode") as GenericEnumPropertyViewModel;
+        Y = FindInputProperty("Y");
+        
+        Mode.ValueChanged += ModeChanged;
+    }
+
+    private void ModeChanged(INodePropertyHandler property, NodePropertyValueChangedArgs args)
+    {
+        if (Mode.Value is not MathNodeMode mode)
+            return;
+        
+        Y.IsVisible = mode.UsesYValue();
+    }
+}

+ 22 - 2
src/PixiEditor/ViewModels/Nodes/NodePropertyViewModel.cs

@@ -4,6 +4,7 @@ using Avalonia.Media;
 using Avalonia.Styling;
 using PixiEditor.DrawingApi.Core.Shaders.Generation;
 using PixiEditor.Models.DocumentModels;
+using PixiEditor.Models.Events;
 using PixiEditor.Models.Handlers;
 using PixiEditor.ViewModels.Nodes.Properties;
 
@@ -12,6 +13,7 @@ namespace PixiEditor.ViewModels.Nodes;
 internal abstract class NodePropertyViewModel : ViewModelBase, INodePropertyHandler
 {
     private string propertyName;
+    private bool isVisible = true;
     private string displayName;
     private object? _value;
     private INodeHandler node;
@@ -33,9 +35,12 @@ internal abstract class NodePropertyViewModel : ViewModelBase, INodePropertyHand
         get => _value;
         set
         {
+            var oldValue = _value;
+            
             if (SetProperty(ref _value, value))
             {
                 ViewModelMain.Current.NodeGraphManager.UpdatePropertyValue((node, PropertyName, value));
+                ValueChanged?.Invoke(this, new NodePropertyValueChangedArgs(oldValue, value));
             }
         }
     }
@@ -58,6 +63,12 @@ internal abstract class NodePropertyViewModel : ViewModelBase, INodePropertyHand
         set => SetProperty(ref isFunc, value);
     }
 
+    public bool IsVisible
+    {
+        get => isVisible;
+        set => SetProperty(ref isVisible, value);
+    }
+
     public INodePropertyHandler? ConnectedOutput
     {
         get => connectedOutput;
@@ -161,9 +172,18 @@ internal abstract class NodePropertyViewModel : ViewModelBase, INodePropertyHand
         
         return (NodePropertyViewModel)Activator.CreateInstance(viewModelType, node, type);
     }
-
-    public void InternalSetValue(object? value) => SetProperty(ref _value, value, nameof(Value));
     
+    public event NodePropertyValueChanged? ValueChanged;
+
+    public void InternalSetValue(object? value)
+    {
+        var oldValue = _value;
+        if (SetProperty(ref _value, value, nameof(Value)))
+        {
+            ValueChanged?.Invoke(this, new NodePropertyValueChangedArgs(oldValue, value));
+        }
+    }
+
     private static bool IsShaderType(Type type)
     {
         return type.IsAssignableTo(typeof(ShaderExpressionVariable));

+ 10 - 0
src/PixiEditor/ViewModels/Nodes/NodeViewModel.cs

@@ -335,10 +335,20 @@ internal abstract class NodeViewModel : ObservableObject, INodeHandler
         return Inputs.FirstOrDefault(x => x.PropertyName == propName) as NodePropertyViewModel;
     }
     
+    public NodePropertyViewModel<T> FindInputProperty<T>(string propName)
+    {
+        return Inputs.FirstOrDefault(x => x.PropertyName == propName) as NodePropertyViewModel<T>;
+    }
+
     public NodePropertyViewModel FindOutputProperty(string propName)
     {
         return Outputs.FirstOrDefault(x => x.PropertyName == propName) as NodePropertyViewModel;
     }
+
+    public NodePropertyViewModel<T> FindOutputProperty<T>(string propName)
+    {
+        return Outputs.FirstOrDefault(x => x.PropertyName == propName) as NodePropertyViewModel<T>;
+    }
 }
 
 internal abstract class NodeViewModel<T> : NodeViewModel where T : Node { }