ModifyImageLeftNode.cs 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. using PixiEditor.ChangeableDocument.Changeables.Graph.Context;
  2. using PixiEditor.ChangeableDocument.Changeables.Graph.Interfaces;
  3. using PixiEditor.ChangeableDocument.Rendering;
  4. using Drawie.Backend.Core;
  5. using Drawie.Backend.Core.ColorsImpl;
  6. using Drawie.Backend.Core.Shaders.Generation;
  7. using Drawie.Backend.Core.Shaders.Generation.Expressions;
  8. using Drawie.Backend.Core.Surfaces;
  9. using Drawie.Numerics;
  10. namespace PixiEditor.ChangeableDocument.Changeables.Graph.Nodes;
  11. [NodeInfo("ModifyImageLeft")]
  12. [PairNode(typeof(ModifyImageRightNode), "ModifyImageZone", true)]
  13. public class ModifyImageLeftNode : Node, IPairNode
  14. {
  15. public InputProperty<Texture?> Image { get; }
  16. public FuncOutputProperty<Float2> Coordinate { get; }
  17. public FuncOutputProperty<Half4> Color { get; }
  18. public InputProperty<ColorSampleMode> SampleMode { get; }
  19. public InputProperty<bool> NormalizeCoordinates { get; }
  20. public Guid OtherNode { get; set; }
  21. public ModifyImageLeftNode()
  22. {
  23. Image = CreateInput<Texture?>("Surface", "IMAGE", null);
  24. Coordinate = CreateFuncOutput("Coordinate", "UV", ctx => ctx.OriginalPosition ?? new Float2(""));
  25. Color = CreateFuncOutput("Color", "COLOR", GetColor);
  26. SampleMode = CreateInput("SampleMode", "COLOR_SAMPLE_MODE", ColorSampleMode.ColorManaged);
  27. NormalizeCoordinates = CreateInput("NormalizeCoordinates", "NORMALIZE_COORDINATES", true);
  28. }
  29. private Half4 GetColor(FuncContext context)
  30. {
  31. context.ThrowOnMissingContext();
  32. if (Image.Value == null)
  33. {
  34. return new Half4("") { ConstantValue = Colors.Transparent };
  35. }
  36. return context.SampleSurface(Image.Value.DrawingSurface, context.SamplePosition, SampleMode.Value,
  37. NormalizeCoordinates.Value);
  38. }
  39. protected override void OnExecute(RenderContext context)
  40. {
  41. RenderPreviews(context);
  42. }
  43. public override Node CreateCopy() => new ModifyImageLeftNode();
  44. private void RenderPreviews(RenderContext context)
  45. {
  46. var previews = context.GetPreviewTexturesForNode(Id);
  47. if (previews is null) return;
  48. foreach (var request in previews)
  49. {
  50. var texture = request.Texture;
  51. if (texture is null) continue;
  52. int saved = texture.DrawingSurface.Canvas.Save();
  53. VecI size = Image.Value?.Size ?? context.RenderOutputSize;
  54. VecD scaling = PreviewUtility.CalculateUniformScaling(size, texture.Size);
  55. VecD offset = PreviewUtility.CalculateCenteringOffset(size, texture.Size, scaling);
  56. texture.DrawingSurface.Canvas.Translate((float)offset.X, (float)offset.Y);
  57. texture.DrawingSurface.Canvas.Scale((float)scaling.X, (float)scaling.Y);
  58. var previewCtx =
  59. PreviewUtility.CreatePreviewContext(context, scaling, context.RenderOutputSize, texture.Size);
  60. texture.DrawingSurface.Canvas.Clear();
  61. RenderPreview(texture.DrawingSurface);
  62. texture.DrawingSurface.Canvas.RestoreToCount(saved);
  63. }
  64. }
  65. public bool RenderPreview(DrawingSurface renderOn)
  66. {
  67. if (Image.Value is null)
  68. {
  69. return false;
  70. }
  71. renderOn.Canvas.DrawSurface(Image.Value.DrawingSurface, 0, 0);
  72. return true;
  73. }
  74. }