NodePropertyViewModel.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. using System.Collections.ObjectModel;
  2. using Avalonia;
  3. using Avalonia.Media;
  4. using Avalonia.Styling;
  5. using PixiEditor.AvaloniaUI.Models.DocumentModels;
  6. using PixiEditor.AvaloniaUI.Models.Handlers;
  7. using PixiEditor.AvaloniaUI.ViewModels.Nodes.Properties;
  8. namespace PixiEditor.AvaloniaUI.ViewModels.Nodes;
  9. internal abstract class NodePropertyViewModel : ViewModelBase, INodePropertyHandler
  10. {
  11. private string propertyName;
  12. private string displayName;
  13. private object value;
  14. private INodeHandler node;
  15. private bool isInput;
  16. private bool isFunc;
  17. private IBrush socketBrush;
  18. private ObservableCollection<INodePropertyHandler> connectedInputs = new();
  19. private INodePropertyHandler? connectedOutput;
  20. public string DisplayName
  21. {
  22. get => displayName;
  23. set => SetProperty(ref displayName, value);
  24. }
  25. public object Value
  26. {
  27. get => value;
  28. set => SetProperty(ref value, value);
  29. }
  30. public bool IsInput
  31. {
  32. get => isInput;
  33. set => SetProperty(ref isInput, value);
  34. }
  35. public bool IsFunc
  36. {
  37. get => isFunc;
  38. set => SetProperty(ref isFunc, value);
  39. }
  40. public INodePropertyHandler? ConnectedOutput
  41. {
  42. get => connectedOutput;
  43. set => SetProperty(ref connectedOutput, value);
  44. }
  45. public ObservableCollection<INodePropertyHandler> ConnectedInputs
  46. {
  47. get => connectedInputs;
  48. set => SetProperty(ref connectedInputs, value);
  49. }
  50. public INodeHandler Node
  51. {
  52. get => node;
  53. set => SetProperty(ref node, value);
  54. }
  55. public string PropertyName
  56. {
  57. get => propertyName;
  58. set => SetProperty(ref propertyName, value);
  59. }
  60. public IBrush SocketBrush
  61. {
  62. get => socketBrush;
  63. set => SetProperty(ref socketBrush, value);
  64. }
  65. public Type PropertyType { get; }
  66. public NodePropertyViewModel(INodeHandler node, Type propertyType)
  67. {
  68. Node = node;
  69. PropertyType = propertyType;
  70. var targetType = propertyType;
  71. if (propertyType.IsAssignableTo(typeof(Delegate)))
  72. {
  73. targetType = propertyType.GetMethod("Invoke").ReturnType;
  74. }
  75. if (Application.Current.Styles.TryGetResource($"{targetType.Name}SocketBrush", App.Current.ActualThemeVariant, out object brush))
  76. {
  77. if (brush is IBrush brushValue)
  78. {
  79. SocketBrush = brushValue;
  80. }
  81. }
  82. if(SocketBrush == null)
  83. {
  84. if(Application.Current.Styles.TryGetResource($"DefaultSocketBrush", App.Current.ActualThemeVariant, out object defaultBrush))
  85. {
  86. if (defaultBrush is IBrush defaultBrushValue)
  87. {
  88. SocketBrush = defaultBrushValue;
  89. }
  90. }
  91. }
  92. }
  93. public static NodePropertyViewModel? CreateFromType(Type type, INodeHandler node)
  94. {
  95. Type propertyType = type;
  96. if (type.IsAssignableTo(typeof(Delegate)))
  97. {
  98. propertyType = type.GetMethod("Invoke").ReturnType;
  99. }
  100. string name = $"{propertyType.Name}PropertyViewModel";
  101. Type viewModelType = Type.GetType($"PixiEditor.AvaloniaUI.ViewModels.Nodes.Properties.{name}");
  102. if (viewModelType == null)
  103. {
  104. return new GenericPropertyViewModel(node, type);
  105. }
  106. return (NodePropertyViewModel)Activator.CreateInstance(viewModelType, node, type);
  107. }
  108. }
  109. internal abstract class NodePropertyViewModel<T> : NodePropertyViewModel
  110. {
  111. private T nodeValue;
  112. public new T Value
  113. {
  114. get => nodeValue;
  115. set => SetProperty(ref nodeValue, value);
  116. }
  117. public NodePropertyViewModel(NodeViewModel node, Type valueType) : base(node, valueType)
  118. {
  119. }
  120. }