ZoomPanel.xaml.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. using java.security;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.ComponentModel;
  5. using System.Linq;
  6. using System.Runtime.CompilerServices;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows;
  10. using System.Windows.Controls;
  11. using System.Windows.Data;
  12. using System.Windows.Documents;
  13. using System.Windows.Input;
  14. using System.Windows.Markup;
  15. using System.Windows.Media;
  16. using System.Windows.Media.Imaging;
  17. using System.Windows.Navigation;
  18. using System.Windows.Shapes;
  19. namespace InABox.WPF;
  20. /// <summary>
  21. /// Interaction logic for ZoomPanel.xaml
  22. /// </summary>
  23. public partial class ZoomPanel : ContentControl, INotifyPropertyChanged
  24. {
  25. public delegate void AreaChangedHandler(ZoomPanel panel);
  26. public event AreaChangedHandler? AreaChanged;
  27. private bool _showNavigationButtons = false;
  28. public bool ShowNavigationButtons
  29. {
  30. get => _showNavigationButtons;
  31. set
  32. {
  33. _showNavigationButtons = value;
  34. OnPropertyChanged();
  35. }
  36. }
  37. public double _minScale = 0.5;
  38. public double MinScale
  39. {
  40. get => _minScale;
  41. set
  42. {
  43. _minScale = value;
  44. if(_scale < _minScale)
  45. {
  46. Scale = _minScale;
  47. }
  48. }
  49. }
  50. public double _scale = 1;
  51. public double Scale
  52. {
  53. get => _scale;
  54. set
  55. {
  56. _scale = value;
  57. OnPropertyChanged();
  58. AreaChanged?.Invoke(this);
  59. }
  60. }
  61. private ScrollViewer Scroll => (ScrollViewer)GetTemplateChild("PART_Scroll");
  62. private Border ContentBorder => (Border)GetTemplateChild("PART_ContentBorder");
  63. public double ScrollX
  64. {
  65. get => Scroll.HorizontalOffset;
  66. set
  67. {
  68. Scroll.ScrollToHorizontalOffset(value);
  69. AreaChanged?.Invoke(this);
  70. }
  71. }
  72. public double ScrollY
  73. {
  74. get => Scroll.VerticalOffset;
  75. set
  76. {
  77. Scroll.ScrollToVerticalOffset(value);
  78. AreaChanged?.Invoke(this);
  79. }
  80. }
  81. public double WheelSpeed { get; set; } = 0.002;
  82. public double PanAmount { get; set; } = 50;
  83. static ZoomPanel()
  84. {
  85. DefaultStyleKeyProperty.OverrideMetadata(typeof(ZoomPanel), new FrameworkPropertyMetadata(typeof(ZoomPanel)));
  86. }
  87. public override void OnApplyTemplate()
  88. {
  89. base.OnApplyTemplate();
  90. ((Button)GetTemplateChild("PART_ZoomInButton")).Click += ZoomInButton_Click;
  91. ((Button)GetTemplateChild("PART_ZoomOutButton")).Click += ZoomOutButton_Click;
  92. ((Button)GetTemplateChild("PART_LeftButton")).Click += LeftButton_Click;
  93. ((Button)GetTemplateChild("PART_RightButton")).Click += RightButton_Click;
  94. ((Button)GetTemplateChild("PART_UpButton")).Click += UpButton_Click;
  95. ((Button)GetTemplateChild("PART_DownButton")).Click += DownButton_Click;
  96. ((Button)GetTemplateChild("PART_DownButton")).Click += DownButton_Click;
  97. ((Border)GetTemplateChild("PART_ContentBorder")).MouseWheel += ItemsControl_MouseWheel;
  98. }
  99. public event PropertyChangedEventHandler? PropertyChanged;
  100. protected void OnPropertyChanged([CallerMemberName] string propertyName = "")
  101. {
  102. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
  103. }
  104. private void ZoomInButton_Click(object sender, RoutedEventArgs e)
  105. {
  106. Scale *= 1.1;
  107. }
  108. private void ZoomOutButton_Click(object sender, RoutedEventArgs e)
  109. {
  110. Scale /= 1.1;
  111. }
  112. private void Pan(double x, double y)
  113. {
  114. ScrollX += x;
  115. ScrollY += y;
  116. //Origin = new(Origin.X - x, Origin.Y - y);
  117. }
  118. private void DownButton_Click(object sender, RoutedEventArgs e)
  119. {
  120. Pan(0, PanAmount);
  121. }
  122. private void UpButton_Click(object sender, RoutedEventArgs e)
  123. {
  124. Pan(0, -PanAmount);
  125. }
  126. private void RightButton_Click(object sender, RoutedEventArgs e)
  127. {
  128. Pan(PanAmount, 0);
  129. }
  130. private void LeftButton_Click(object sender, RoutedEventArgs e)
  131. {
  132. Pan(-PanAmount, 0);
  133. }
  134. private void ItemsControl_MouseWheel(object sender, MouseWheelEventArgs e)
  135. {
  136. if ((Keyboard.Modifiers & ModifierKeys.Control) == ModifierKeys.Control)
  137. {
  138. var pos = e.GetPosition(ContentBorder);
  139. var contentMPos = new Point(pos.X / Scale, pos.Y / Scale);
  140. if (e.Delta > 0)
  141. {
  142. Scale *= 1 + e.Delta * WheelSpeed;
  143. }
  144. else
  145. {
  146. Scale /= 1 + (-e.Delta) * WheelSpeed;
  147. }
  148. Scale = Math.Max(MinScale, Scale);
  149. var scaledPos = new Point(contentMPos.X * Scale, contentMPos.Y * Scale);
  150. var offset = scaledPos - pos;
  151. Pan(offset.X, offset.Y);
  152. e.Handled = true;
  153. }
  154. else if ((Keyboard.Modifiers & ModifierKeys.Shift) == ModifierKeys.Shift)
  155. {
  156. Pan(-e.Delta, 0);
  157. e.Handled = true;
  158. }
  159. }
  160. }