NodePropertyViewModel.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. using System.Collections.ObjectModel;
  2. using Avalonia;
  3. using Avalonia.Media;
  4. using Drawie.Backend.Core.Shaders.Generation;
  5. using PixiEditor.Models.Events;
  6. using PixiEditor.Models.Handlers;
  7. using PixiEditor.ViewModels.Nodes.Properties;
  8. namespace PixiEditor.ViewModels.Nodes;
  9. internal abstract class NodePropertyViewModel : ViewModelBase, INodePropertyHandler
  10. {
  11. private string propertyName;
  12. private bool isVisible = true;
  13. private string displayName;
  14. private object? _value;
  15. private INodeHandler node;
  16. private bool isInput;
  17. private bool isFunc;
  18. private IBrush socketBrush;
  19. private string errors = string.Empty;
  20. private bool mergeChanges = false;
  21. private object computedValue;
  22. private ObservableCollection<INodePropertyHandler> connectedInputs = new();
  23. private INodePropertyHandler? connectedOutput;
  24. public event NodePropertyValueChanged? ValueChanged;
  25. public event EventHandler? ConnectedOutputChanged;
  26. public string DisplayName
  27. {
  28. get => displayName;
  29. set => SetProperty(ref displayName, value);
  30. }
  31. public object? Value
  32. {
  33. get => _value;
  34. set
  35. {
  36. var oldValue = _value;
  37. if (MergeChanges)
  38. {
  39. ViewModelMain.Current.NodeGraphManager.BeginUpdatePropertyValue((node, PropertyName, value));
  40. }
  41. else
  42. {
  43. ViewModelMain.Current.NodeGraphManager.UpdatePropertyValue((node, PropertyName, value));
  44. }
  45. if (SetProperty(ref _value, value))
  46. {
  47. ValueChanged?.Invoke(this, new NodePropertyValueChangedArgs(oldValue, value));
  48. }
  49. }
  50. }
  51. public bool MergeChanges
  52. {
  53. get => mergeChanges;
  54. set
  55. {
  56. if (SetProperty(ref mergeChanges, value))
  57. {
  58. if (!value)
  59. {
  60. ViewModelMain.Current.NodeGraphManager.EndUpdatePropertyValue();
  61. }
  62. }
  63. }
  64. }
  65. public object ComputedValue
  66. {
  67. get
  68. {
  69. return computedValue;
  70. }
  71. set
  72. {
  73. SetProperty(ref computedValue, value);
  74. }
  75. }
  76. public bool IsInput
  77. {
  78. get => isInput;
  79. set
  80. {
  81. if (SetProperty(ref isInput, value))
  82. {
  83. OnPropertyChanged(nameof(ShowInputField));
  84. }
  85. }
  86. }
  87. public bool IsFunc
  88. {
  89. get => isFunc;
  90. set => SetProperty(ref isFunc, value);
  91. }
  92. public bool IsVisible
  93. {
  94. get => isVisible;
  95. set => SetProperty(ref isVisible, value);
  96. }
  97. public INodePropertyHandler? ConnectedOutput
  98. {
  99. get => connectedOutput;
  100. set
  101. {
  102. if (SetProperty(ref connectedOutput, value))
  103. {
  104. OnPropertyChanged(nameof(ShowInputField));
  105. ConnectedOutputChanged?.Invoke(this, EventArgs.Empty);
  106. }
  107. }
  108. }
  109. public bool ShowInputField
  110. {
  111. get => IsInput && ConnectedOutput == null;
  112. }
  113. public ObservableCollection<INodePropertyHandler> ConnectedInputs
  114. {
  115. get => connectedInputs;
  116. set => SetProperty(ref connectedInputs, value);
  117. }
  118. public INodeHandler Node
  119. {
  120. get => node;
  121. set => SetProperty(ref node, value);
  122. }
  123. public string PropertyName
  124. {
  125. get => propertyName;
  126. set => SetProperty(ref propertyName, value);
  127. }
  128. public IBrush SocketBrush
  129. {
  130. get => socketBrush;
  131. set => SetProperty(ref socketBrush, value);
  132. }
  133. public Type PropertyType { get; }
  134. public string? Errors
  135. {
  136. get => errors;
  137. set => SetProperty(ref errors, value);
  138. }
  139. public NodePropertyViewModel(INodeHandler node, Type propertyType)
  140. {
  141. Node = node;
  142. PropertyType = propertyType;
  143. var targetType = propertyType;
  144. if (propertyType.IsAssignableTo(typeof(Delegate)))
  145. {
  146. targetType = propertyType.GetMethod("Invoke").ReturnType;
  147. }
  148. if (Application.Current.Styles.TryGetResource($"{targetType.Name}SocketBrush", App.Current.ActualThemeVariant,
  149. out object brush))
  150. {
  151. if (brush is IBrush brushValue)
  152. {
  153. SocketBrush = brushValue;
  154. }
  155. }
  156. if (SocketBrush == null)
  157. {
  158. if (Application.Current.Styles.TryGetResource($"DefaultSocketBrush", App.Current.ActualThemeVariant,
  159. out object defaultBrush))
  160. {
  161. if (defaultBrush is IBrush defaultBrushValue)
  162. {
  163. SocketBrush = defaultBrushValue;
  164. }
  165. }
  166. }
  167. }
  168. public static NodePropertyViewModel? CreateFromType(Type type, INodeHandler node)
  169. {
  170. Type propertyType = type;
  171. if (type.IsAssignableTo(typeof(Delegate)))
  172. {
  173. propertyType = type.GetMethod("Invoke").ReturnType;
  174. }
  175. if (IsShaderType(propertyType))
  176. {
  177. propertyType = type.GetMethod("Invoke").ReturnType.BaseType.GenericTypeArguments[0];
  178. }
  179. string typeName = propertyType.Name;
  180. string name = $"{typeName}PropertyViewModel";
  181. Type viewModelType = Type.GetType($"PixiEditor.ViewModels.Nodes.Properties.{name}");
  182. if (viewModelType == null)
  183. {
  184. if (propertyType.IsEnum)
  185. {
  186. return new GenericEnumPropertyViewModel(node, type, propertyType);
  187. }
  188. return new GenericPropertyViewModel(node, type);
  189. }
  190. return (NodePropertyViewModel)Activator.CreateInstance(viewModelType, node, type);
  191. }
  192. public void UpdateComputedValue()
  193. {
  194. ViewModelMain.Current.NodeGraphManager.GetComputedPropertyValue(this);
  195. }
  196. public void InternalSetComputedValue(object value)
  197. {
  198. computedValue = value;
  199. OnPropertyChanged(nameof(ComputedValue));
  200. }
  201. public void InternalSetValue(object? value)
  202. {
  203. var oldValue = _value;
  204. if (SetProperty(ref _value, value, nameof(Value)))
  205. {
  206. ValueChanged?.Invoke(this, new NodePropertyValueChangedArgs(oldValue, value));
  207. }
  208. }
  209. private static bool IsShaderType(Type type)
  210. {
  211. return type.IsAssignableTo(typeof(ShaderExpressionVariable));
  212. }
  213. }
  214. internal abstract class NodePropertyViewModel<T> : NodePropertyViewModel
  215. {
  216. public new T Value
  217. {
  218. get
  219. {
  220. if (base.Value == null)
  221. return default;
  222. if (base.Value is T value)
  223. return value;
  224. return default;
  225. }
  226. set => base.Value = value;
  227. }
  228. public NodePropertyViewModel(NodeViewModel node, Type valueType) : base(node, valueType)
  229. {
  230. }
  231. }