StructureNode.cs 13 KB

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