StructureNode.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. using PixiEditor.ChangeableDocument.Changeables.Animations;
  2. using PixiEditor.ChangeableDocument.Changeables.Graph.Interfaces;
  3. using PixiEditor.ChangeableDocument.Changeables.Interfaces;
  4. using PixiEditor.ChangeableDocument.ChangeInfos.Properties;
  5. using PixiEditor.ChangeableDocument.Helpers;
  6. using PixiEditor.ChangeableDocument.Rendering;
  7. using Drawie.Backend.Core;
  8. using Drawie.Backend.Core.ColorsImpl;
  9. using Drawie.Backend.Core.Numerics;
  10. using Drawie.Backend.Core.Surfaces;
  11. using Drawie.Backend.Core.Surfaces.ImageData;
  12. using Drawie.Backend.Core.Surfaces.PaintImpl;
  13. using Drawie.Numerics;
  14. using BlendMode = PixiEditor.ChangeableDocument.Enums.BlendMode;
  15. namespace PixiEditor.ChangeableDocument.Changeables.Graph.Nodes;
  16. public abstract class StructureNode : RenderNode, IReadOnlyStructureNode, IRenderInput
  17. {
  18. public const string DefaultMemberName = "DEFAULT_MEMBER_NAME";
  19. public const string IsVisiblePropertyName = "IsVisible";
  20. public const string OpacityPropertyName = "Opacity";
  21. public const string BlendModePropertyName = "BlendMode";
  22. public const string ClipToPreviousMemberPropertyName = "ClipToPreviousMember";
  23. public const string MaskIsVisiblePropertyName = "MaskIsVisible";
  24. public const string MaskPropertyName = "Mask";
  25. public const string FiltersPropertyName = "Filters";
  26. public const string FilterlessOutputPropertyName = "FilterlessOutput";
  27. public const string RawOutputPropertyName = "RawOutput";
  28. public InputProperty<float> Opacity { get; }
  29. public InputProperty<bool> IsVisible { get; }
  30. public bool ClipToPreviousMember { get; set; }
  31. public InputProperty<BlendMode> BlendMode { get; }
  32. public RenderInputProperty CustomMask { get; }
  33. public InputProperty<bool> MaskIsVisible { get; }
  34. public InputProperty<Filter> Filters { get; }
  35. public RenderInputProperty Background { get; }
  36. public RenderOutputProperty FilterlessOutput { get; }
  37. public RenderOutputProperty RawOutput { get; }
  38. public OutputProperty<VecD> TightSize { get; }
  39. public OutputProperty<VecD> CanvasPosition { get; }
  40. public OutputProperty<VecD> CenterPosition { get; }
  41. public ChunkyImage? EmbeddedMask { get; set; }
  42. protected Texture renderedMask;
  43. protected static readonly Paint replacePaint = new Paint() { BlendMode = Drawie.Backend.Core.Surfaces.BlendMode.Src };
  44. protected static readonly Paint clearPaint = new Paint()
  45. {
  46. BlendMode = Drawie.Backend.Core.Surfaces.BlendMode.Src, Color = Colors.Transparent
  47. };
  48. protected static readonly Paint clipPaint = new Paint()
  49. {
  50. BlendMode = Drawie.Backend.Core.Surfaces.BlendMode.DstIn
  51. };
  52. public virtual ShapeCorners GetTransformationCorners(KeyFrameTime frameTime)
  53. {
  54. return new ShapeCorners(GetTightBounds(frameTime).GetValueOrDefault());
  55. }
  56. public string MemberName
  57. {
  58. get => DisplayName;
  59. set => DisplayName = value;
  60. }
  61. protected Paint maskPaint = new Paint() { BlendMode = Drawie.Backend.Core.Surfaces.BlendMode.DstIn, ColorFilter = Nodes.Filters.MaskFilter };
  62. protected Paint blendPaint = new Paint() { BlendMode = Drawie.Backend.Core.Surfaces.BlendMode.SrcOver };
  63. protected Paint maskPreviewPaint = new Paint()
  64. {
  65. BlendMode = Drawie.Backend.Core.Surfaces.BlendMode.SrcOver,
  66. ColorFilter = ColorFilter.CreateCompose(Nodes.Filters.AlphaGrayscaleFilter, Nodes.Filters.MaskFilter)
  67. };
  68. private int maskCacheHash = 0;
  69. protected StructureNode()
  70. {
  71. Painter filterlessPainter = new Painter(OnFilterlessPaint);
  72. Painter rawPainter = new Painter(OnRawPaint);
  73. Background = CreateRenderInput("Background", "BACKGROUND");
  74. Opacity = CreateInput<float>(OpacityPropertyName, "OPACITY", 1);
  75. IsVisible = CreateInput<bool>(IsVisiblePropertyName, "IS_VISIBLE", true);
  76. BlendMode = CreateInput(BlendModePropertyName, "BLEND_MODE", Enums.BlendMode.Normal);
  77. CustomMask = CreateRenderInput(MaskPropertyName, "MASK");
  78. MaskIsVisible = CreateInput<bool>(MaskIsVisiblePropertyName, "MASK_IS_VISIBLE", true);
  79. Filters = CreateInput<Filter>(FiltersPropertyName, "FILTERS", null);
  80. FilterlessOutput = CreateRenderOutput(FilterlessOutputPropertyName, "WITHOUT_FILTERS",
  81. () => filterlessPainter, () => Background.Value);
  82. RawOutput = CreateRenderOutput(RawOutputPropertyName, "RAW_LAYER_OUTPUT", () => rawPainter);
  83. CanvasPosition = CreateOutput<VecD>("CanvasPosition", "CANVAS_POSITION", VecD.Zero);
  84. CenterPosition = CreateOutput<VecD>("CenterPosition", "CENTER_POSITION", VecD.Zero);
  85. TightSize = CreateOutput<VecD>("Size", "SIZE", VecD.Zero);
  86. MemberName = DefaultMemberName;
  87. }
  88. protected override void OnExecute(RenderContext context)
  89. {
  90. base.OnExecute(context);
  91. if (TightSize.Connections.Count > 0)
  92. {
  93. TightSize.Value = GetTightBounds(context.FrameTime)?.Size ?? VecD.Zero;
  94. }
  95. if (CanvasPosition.Connections.Count > 0)
  96. {
  97. CanvasPosition.Value = GetTightBounds(context.FrameTime)?.TopLeft ?? VecD.Zero;
  98. }
  99. if (CenterPosition.Connections.Count > 0)
  100. {
  101. CenterPosition.Value = GetTightBounds(context.FrameTime)?.Center ?? VecD.Zero;
  102. }
  103. }
  104. protected override void OnPaint(RenderContext context, DrawingSurface renderTarget)
  105. {
  106. if (Output.Connections.Count > 0)
  107. {
  108. RenderForOutput(context, renderTarget, Output);
  109. }
  110. }
  111. private void OnFilterlessPaint(RenderContext context, DrawingSurface renderTarget)
  112. {
  113. RenderForOutput(context, renderTarget, FilterlessOutput);
  114. }
  115. private void OnRawPaint(RenderContext context, DrawingSurface renderTarget)
  116. {
  117. RenderForOutput(context, renderTarget, RawOutput);
  118. }
  119. public abstract VecD GetScenePosition(KeyFrameTime frameTime);
  120. public abstract VecD GetSceneSize(KeyFrameTime frameTime);
  121. public void RenderForOutput(RenderContext context, DrawingSurface renderTarget, RenderOutputProperty output)
  122. {
  123. if(IsDisposed)
  124. {
  125. return;
  126. }
  127. var renderObjectContext = CreateSceneContext(context, renderTarget, output);
  128. int renderSaved = renderTarget.Canvas.Save();
  129. VecD scenePos = GetScenePosition(context.FrameTime);
  130. VecD sceneSize = GetSceneSize(context.FrameTime);
  131. //renderTarget.Canvas.ClipRect(new RectD(scenePos - (sceneSize / 2f), sceneSize));
  132. Render(renderObjectContext);
  133. renderTarget?.Canvas.RestoreToCount(renderSaved);
  134. }
  135. protected SceneObjectRenderContext CreateSceneContext(RenderContext context, DrawingSurface renderTarget,
  136. RenderOutputProperty output)
  137. {
  138. var sceneSize = GetSceneSize(context.FrameTime);
  139. RectD localBounds = new RectD(0, 0, sceneSize.X, sceneSize.Y);
  140. SceneObjectRenderContext renderObjectContext = new SceneObjectRenderContext(output, renderTarget, localBounds,
  141. context.FrameTime, context.ChunkResolution, context.DocumentSize, renderTarget == context.RenderSurface,
  142. context.ProcessingColorSpace,
  143. context.Opacity);
  144. renderObjectContext.FullRerender = context.FullRerender;
  145. return renderObjectContext;
  146. }
  147. public abstract void Render(SceneObjectRenderContext sceneContext);
  148. protected void ApplyMaskIfPresent(DrawingSurface surface, RenderContext context)
  149. {
  150. if (MaskIsVisible.Value)
  151. {
  152. if (CustomMask.Value != null)
  153. {
  154. int layer = surface.Canvas.SaveLayer(maskPaint);
  155. surface.Canvas.Scale((float)context.ChunkResolution.Multiplier());
  156. CustomMask.Value.Paint(context, surface);
  157. surface.Canvas.RestoreToCount(layer);
  158. }
  159. else if (EmbeddedMask != null)
  160. {
  161. if (context.FullRerender)
  162. {
  163. EmbeddedMask.DrawMostUpToDateRegionOn(
  164. new RectI(0, 0, EmbeddedMask.LatestSize.X, EmbeddedMask.LatestSize.Y),
  165. ChunkResolution.Full,
  166. surface, VecI.Zero, maskPaint);
  167. }
  168. else if(renderedMask != null)
  169. {
  170. surface.Canvas.DrawSurface(renderedMask.DrawingSurface, 0, 0, maskPaint);
  171. }
  172. }
  173. }
  174. }
  175. protected override bool CacheChanged(RenderContext context)
  176. {
  177. int cacheHash = EmbeddedMask?.GetCacheHash() ?? 0;
  178. return base.CacheChanged(context) || maskCacheHash != cacheHash;
  179. }
  180. protected override void UpdateCache(RenderContext context)
  181. {
  182. base.UpdateCache(context);
  183. maskCacheHash = EmbeddedMask?.GetCacheHash() ?? 0;
  184. }
  185. public virtual void RenderChunk(VecI chunkPos, ChunkResolution resolution, KeyFrameTime frameTime, ColorSpace processingColorSpace)
  186. {
  187. RenderChunkyImageChunk(chunkPos, resolution, EmbeddedMask, 55, processingColorSpace, ref renderedMask);
  188. }
  189. protected void RenderChunkyImageChunk(VecI chunkPos, ChunkResolution resolution, ChunkyImage img,
  190. int textureId, ColorSpace processingColorSpace,
  191. ref Texture? renderSurface)
  192. {
  193. if (img is null)
  194. {
  195. return;
  196. }
  197. VecI targetSize = img.LatestSize;
  198. renderSurface = RequestTexture(textureId, targetSize, processingColorSpace, false);
  199. int saved = renderSurface.DrawingSurface.Canvas.Save();
  200. if (!img.DrawMostUpToDateChunkOn(
  201. chunkPos,
  202. ChunkResolution.Full,
  203. renderSurface.DrawingSurface,
  204. chunkPos * ChunkResolution.Full.PixelSize(),
  205. replacePaint))
  206. {
  207. var chunkSize = ChunkResolution.Full.PixelSize();
  208. renderSurface.DrawingSurface.Canvas.DrawRect(new RectD(chunkPos * chunkSize, new VecD(chunkSize)),
  209. clearPaint);
  210. }
  211. renderSurface.DrawingSurface.Canvas.RestoreToCount(saved);
  212. }
  213. protected void ApplyRasterClip(DrawingSurface toClip, DrawingSurface clipSource)
  214. {
  215. if (ClipToPreviousMember && Background.Value != null)
  216. {
  217. toClip.Canvas.DrawSurface(clipSource, 0, 0, clipPaint);
  218. }
  219. }
  220. protected bool IsEmptyMask()
  221. {
  222. return EmbeddedMask != null && MaskIsVisible.Value && !EmbeddedMask.LatestOrCommittedChunkExists();
  223. }
  224. protected bool HasOperations()
  225. {
  226. return (MaskIsVisible.Value && (EmbeddedMask != null || CustomMask.Value != null)) || ClipToPreviousMember;
  227. }
  228. protected void DrawClipSource(DrawingSurface drawOnto, IClipSource clipSource, SceneObjectRenderContext context)
  229. {
  230. blendPaint.Color = Colors.White;
  231. clipSource.DrawClipSource(context, drawOnto);
  232. }
  233. public abstract RectD? GetTightBounds(KeyFrameTime frameTime);
  234. public override void SerializeAdditionalData(Dictionary<string, object> additionalData)
  235. {
  236. base.SerializeAdditionalData(additionalData);
  237. if (EmbeddedMask != null)
  238. {
  239. additionalData["embeddedMask"] = EmbeddedMask;
  240. }
  241. if (ClipToPreviousMember)
  242. {
  243. additionalData["clipToPreviousMember"] = ClipToPreviousMember;
  244. }
  245. }
  246. internal override OneOf<None, IChangeInfo, List<IChangeInfo>> DeserializeAdditionalData(IReadOnlyDocument target,
  247. IReadOnlyDictionary<string, object> data)
  248. {
  249. base.DeserializeAdditionalData(target, data);
  250. bool hasMask = data.ContainsKey("embeddedMask");
  251. if (hasMask)
  252. {
  253. ChunkyImage? mask = (ChunkyImage?)data["embeddedMask"];
  254. EmbeddedMask?.Dispose();
  255. EmbeddedMask = mask;
  256. return new List<IChangeInfo> { new StructureMemberMask_ChangeInfo(Id, mask != null) };
  257. }
  258. if (data.ContainsKey("clipToPreviousMember"))
  259. {
  260. ClipToPreviousMember = (bool)data["clipToPreviousMember"];
  261. return new List<IChangeInfo> { new StructureMemberClipToMemberBelow_ChangeInfo(Id, ClipToPreviousMember) };
  262. }
  263. return new None();
  264. }
  265. public override RectD? GetPreviewBounds(int frame, string elementFor = "")
  266. {
  267. if (elementFor == nameof(EmbeddedMask) && EmbeddedMask != null)
  268. {
  269. return new RectD(VecD.Zero, EmbeddedMask.LatestSize);
  270. }
  271. return null;
  272. }
  273. public override bool RenderPreview(DrawingSurface renderOn, RenderContext context,
  274. string elementToRenderName)
  275. {
  276. if (elementToRenderName != nameof(EmbeddedMask))
  277. {
  278. return false;
  279. }
  280. var img = EmbeddedMask;
  281. if (img is null)
  282. {
  283. return false;
  284. }
  285. renderOn.Canvas.DrawSurface(renderedMask.DrawingSurface, VecI.Zero, maskPreviewPaint);
  286. return true;
  287. }
  288. public override void Dispose()
  289. {
  290. Output.Value = null;
  291. base.Dispose();
  292. maskPaint.Dispose();
  293. blendPaint.Dispose();
  294. }
  295. }