| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272 |
- using System.Collections.ObjectModel;
- using Avalonia;
- using Avalonia.Media;
- using Drawie.Backend.Core.Shaders.Generation;
- using PixiEditor.Models.Events;
- using PixiEditor.Models.Handlers;
- using PixiEditor.ViewModels.Nodes.Properties;
- 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;
- private bool isInput;
- private bool isFunc;
- private IBrush socketBrush;
- private string errors = string.Empty;
- private bool mergeChanges = false;
- private object computedValue;
- private ObservableCollection<INodePropertyHandler> connectedInputs = new();
- private INodePropertyHandler? connectedOutput;
- public event NodePropertyValueChanged? ValueChanged;
- public event EventHandler? ConnectedOutputChanged;
- public string DisplayName
- {
- get => displayName;
- set => SetProperty(ref displayName, value);
- }
- public object? Value
- {
- get => _value;
- set
- {
- var oldValue = _value;
- if (MergeChanges)
- {
- ViewModelMain.Current.NodeGraphManager.BeginUpdatePropertyValue((node, PropertyName, value));
- }
- else
- {
- ViewModelMain.Current.NodeGraphManager.UpdatePropertyValue((node, PropertyName, value));
- }
- if (SetProperty(ref _value, value))
- {
- ValueChanged?.Invoke(this, new NodePropertyValueChangedArgs(oldValue, value));
- }
- }
- }
- public bool MergeChanges
- {
- get => mergeChanges;
- set
- {
- if (SetProperty(ref mergeChanges, value))
- {
- if (!value)
- {
- ViewModelMain.Current.NodeGraphManager.EndUpdatePropertyValue();
- }
- }
- }
- }
- public object ComputedValue
- {
- get
- {
- return computedValue;
- }
- set
- {
- SetProperty(ref computedValue, value);
- }
- }
- public bool IsInput
- {
- get => isInput;
- set
- {
- if (SetProperty(ref isInput, value))
- {
- OnPropertyChanged(nameof(ShowInputField));
- }
- }
- }
- public bool IsFunc
- {
- get => isFunc;
- set => SetProperty(ref isFunc, value);
- }
- public bool IsVisible
- {
- get => isVisible;
- set => SetProperty(ref isVisible, value);
- }
- public INodePropertyHandler? ConnectedOutput
- {
- get => connectedOutput;
- set
- {
- if (SetProperty(ref connectedOutput, value))
- {
- OnPropertyChanged(nameof(ShowInputField));
- ConnectedOutputChanged?.Invoke(this, EventArgs.Empty);
- }
- }
- }
- public bool ShowInputField
- {
- get => IsInput && ConnectedOutput == null;
- }
- public ObservableCollection<INodePropertyHandler> ConnectedInputs
- {
- get => connectedInputs;
- set => SetProperty(ref connectedInputs, value);
- }
- public INodeHandler Node
- {
- get => node;
- set => SetProperty(ref node, value);
- }
- public string PropertyName
- {
- get => propertyName;
- set => SetProperty(ref propertyName, value);
- }
- public IBrush SocketBrush
- {
- get => socketBrush;
- set => SetProperty(ref socketBrush, value);
- }
- public Type PropertyType { get; }
- public string? Errors
- {
- get => errors;
- set => SetProperty(ref errors, value);
- }
- public NodePropertyViewModel(INodeHandler node, Type propertyType)
- {
- Node = node;
- PropertyType = propertyType;
- var targetType = propertyType;
- if (propertyType.IsAssignableTo(typeof(Delegate)))
- {
- targetType = propertyType.GetMethod("Invoke").ReturnType;
- }
- if (Application.Current.Styles.TryGetResource($"{targetType.Name}SocketBrush", App.Current.ActualThemeVariant,
- out object brush))
- {
- if (brush is IBrush brushValue)
- {
- SocketBrush = brushValue;
- }
- }
- if (SocketBrush == null)
- {
- if (Application.Current.Styles.TryGetResource($"DefaultSocketBrush", App.Current.ActualThemeVariant,
- out object defaultBrush))
- {
- if (defaultBrush is IBrush defaultBrushValue)
- {
- SocketBrush = defaultBrushValue;
- }
- }
- }
- }
- public static NodePropertyViewModel? CreateFromType(Type type, INodeHandler node)
- {
- Type propertyType = type;
- if (type.IsAssignableTo(typeof(Delegate)))
- {
- propertyType = type.GetMethod("Invoke").ReturnType;
- }
- if (IsShaderType(propertyType))
- {
- propertyType = type.GetMethod("Invoke").ReturnType.BaseType.GenericTypeArguments[0];
- }
- string typeName = propertyType.Name;
- string name = $"{typeName}PropertyViewModel";
- Type viewModelType = Type.GetType($"PixiEditor.ViewModels.Nodes.Properties.{name}");
- if (viewModelType == null)
- {
- if (propertyType.IsEnum)
- {
- return new GenericEnumPropertyViewModel(node, type, propertyType);
- }
- return new GenericPropertyViewModel(node, type);
- }
- return (NodePropertyViewModel)Activator.CreateInstance(viewModelType, node, type);
- }
- public void UpdateComputedValue()
- {
- ViewModelMain.Current.NodeGraphManager.GetComputedPropertyValue(this);
- }
- public void InternalSetComputedValue(object value)
- {
- computedValue = value;
- OnPropertyChanged(nameof(ComputedValue));
- }
- 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));
- }
- }
- internal abstract class NodePropertyViewModel<T> : NodePropertyViewModel
- {
- public new T Value
- {
- get
- {
- if (base.Value == null)
- return default;
- if (base.Value is T value)
- return value;
- return default;
- }
- set => base.Value = value;
- }
- public NodePropertyViewModel(NodeViewModel node, Type valueType) : base(node, valueType)
- {
- }
- }
|