PreviewWindow.xaml.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. using PixiEditor.Models.DataHolders;
  2. using PixiEditor.Models.ImageManipulation;
  3. using PixiEditor.ViewModels;
  4. using System.Linq;
  5. using System.Windows;
  6. using System.Windows.Controls;
  7. using System.Windows.Input;
  8. using System.Windows.Media;
  9. namespace PixiEditor.Views.UserControls
  10. {
  11. /// <summary>
  12. /// Interaction logic for PreviewWindow.xaml
  13. /// </summary>
  14. public partial class PreviewWindow : UserControl
  15. {
  16. public static readonly DependencyProperty DocumentProperty =
  17. DependencyProperty.Register(nameof(Document), typeof(Document), typeof(PreviewWindow));
  18. public Document Document
  19. {
  20. get => (Document)GetValue(DocumentProperty);
  21. set => SetValue(DocumentProperty, value);
  22. }
  23. public static readonly DependencyProperty ColorCursorPositionProperty =
  24. DependencyProperty.Register(nameof(ColorCursorPosition), typeof(Thickness), typeof(PreviewWindow));
  25. public Thickness ColorCursorPosition
  26. {
  27. get => (Thickness)GetValue(ColorCursorPositionProperty);
  28. private set => SetValue(ColorCursorPositionProperty, value);
  29. }
  30. public static readonly DependencyProperty ColorCursorColorProperty =
  31. DependencyProperty.Register(nameof(ColorCursorColor), typeof(Color), typeof(PreviewWindow));
  32. public Color ColorCursorColor
  33. {
  34. get => (Color)GetValue(ColorCursorColorProperty);
  35. set => SetValue(ColorCursorColorProperty, value);
  36. }
  37. public static readonly DependencyProperty PrimaryColorProperty =
  38. DependencyProperty.Register(nameof(PrimaryColor), typeof(Color), typeof(PreviewWindow));
  39. public Color PrimaryColor
  40. {
  41. get => (Color)GetValue(PrimaryColorProperty);
  42. set => SetValue(PrimaryColorProperty, value);
  43. }
  44. public static readonly DependencyProperty OptionsOpenProperty =
  45. DependencyProperty.Register(nameof(OptionsOpen), typeof(bool), typeof(PreviewWindow));
  46. public bool OptionsOpen
  47. {
  48. get => (bool)GetValue(OptionsOpenProperty);
  49. set => SetValue(OptionsOpenProperty, value);
  50. }
  51. public PreviewWindow()
  52. {
  53. InitializeComponent();
  54. imageGrid.MouseMove += ImageGrid_MouseMove;
  55. imageGrid.MouseRightButtonDown += ImageGrid_MouseRightButtonDown;
  56. imageGrid.MouseEnter += ImageGrid_MouseEnter;
  57. imageGrid.MouseLeave += ImageGrid_MouseLeave;
  58. }
  59. private void ImageGrid_MouseLeave(object sender, MouseEventArgs e)
  60. {
  61. if (ViewModelMain.Current != null)
  62. {
  63. ViewModelMain.Current.OverrideActionDisplay = false;
  64. }
  65. }
  66. private void ImageGrid_MouseEnter(object sender, MouseEventArgs e)
  67. {
  68. if (ViewModelMain.Current != null)
  69. {
  70. ViewModelMain.Current.ActionDisplay = "Right-click to pick color, Shift-right-click to copy color to clipboard";
  71. ViewModelMain.Current.OverrideActionDisplay = true;
  72. }
  73. }
  74. private void ImageGrid_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
  75. {
  76. if (Keyboard.IsKeyDown(Key.LeftShift))
  77. {
  78. CopyColorToClipboard();
  79. }
  80. else
  81. {
  82. CopyColorToPrimary();
  83. }
  84. }
  85. private void CopyColorToPrimary()
  86. {
  87. PrimaryColor = ColorCursorColor;
  88. }
  89. private void CopyColorToClipboard()
  90. {
  91. if (ColorCursorColor.A == 255)
  92. {
  93. Clipboard.SetText(string.Format("#{0:X2}{1:X2}{2:X2}", ColorCursorColor.R, ColorCursorColor.G, ColorCursorColor.B));
  94. }
  95. else
  96. {
  97. Clipboard.SetText(ColorCursorColor.ToString());
  98. }
  99. }
  100. private void ImageGrid_MouseMove(object sender, MouseEventArgs e)
  101. {
  102. if (Document == null)
  103. {
  104. return;
  105. }
  106. Point mousePos = e.GetPosition(imageGrid);
  107. int x = (int)mousePos.X;
  108. int y = (int)mousePos.Y;
  109. Thickness newPos = new Thickness(x, y, 0, 0);
  110. if (ColorCursorPosition == newPos)
  111. {
  112. return;
  113. }
  114. ColorCursorPosition = newPos;
  115. ColorCursorColor = BitmapUtils.GetColorAtPointCombined(x, y, Document.Layers.ToArray());
  116. }
  117. }
  118. }