BrightnessToolExecutor.cs 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. using Avalonia.Input;
  2. using PixiEditor.AvaloniaUI.Models.Handlers;
  3. using PixiEditor.AvaloniaUI.Models.Handlers.Toolbars;
  4. using PixiEditor.AvaloniaUI.Models.Handlers.Tools;
  5. using PixiEditor.AvaloniaUI.Models.Tools;
  6. using PixiEditor.ChangeableDocument.Actions.Generated;
  7. using PixiEditor.DrawingApi.Core.Numerics;
  8. namespace PixiEditor.AvaloniaUI.Models.DocumentModels.UpdateableChangeExecutors;
  9. #nullable enable
  10. internal class BrightnessToolExecutor : UpdateableChangeExecutor
  11. {
  12. private Guid guidValue;
  13. private bool repeat;
  14. private float correctionFactor;
  15. private int toolSize;
  16. public override ExecutionState Start()
  17. {
  18. IStructureMemberHandler? member = document!.SelectedStructureMember;
  19. IBrightnessToolHandler? tool = GetHandler<IBrightnessToolHandler>();
  20. if (tool is null || member is null || tool.Toolbar is not IBasicToolbar toolbar)
  21. return ExecutionState.Error;
  22. if (member is not ILayerHandler layer || layer.ShouldDrawOnMask)
  23. return ExecutionState.Error;
  24. guidValue = member.GuidValue;
  25. repeat = tool.BrightnessMode == BrightnessMode.Repeat;
  26. toolSize = toolbar.ToolSize;
  27. correctionFactor = tool.Darken || tool.UsedWith == MouseButton.Right ? -tool.CorrectionFactor : tool.CorrectionFactor;
  28. ChangeBrightness_Action action = new(guidValue, controller!.LastPixelPosition, correctionFactor, toolSize, repeat);
  29. internals!.ActionAccumulator.AddActions(action);
  30. return ExecutionState.Success;
  31. }
  32. public override void OnPixelPositionChange(VecI pos)
  33. {
  34. ChangeBrightness_Action action = new(guidValue, pos, correctionFactor, toolSize, repeat);
  35. internals!.ActionAccumulator.AddActions(action);
  36. }
  37. public override void OnLeftMouseButtonUp()
  38. {
  39. internals!.ActionAccumulator.AddFinishedActions(new EndChangeBrightness_Action());
  40. onEnded?.Invoke(this);
  41. }
  42. public override void ForceStop()
  43. {
  44. internals!.ActionAccumulator.AddFinishedActions(new EndChangeBrightness_Action());
  45. }
  46. }