Skip to content

Commit

Permalink
[add] ComboBoxMatrixTransformCenterPoint
Browse files Browse the repository at this point in the history
  • Loading branch information
ChangSakura committed Jul 28, 2024
1 parent c0ef0b7 commit d9a1f97
Show file tree
Hide file tree
Showing 11 changed files with 82 additions and 43 deletions.
6 changes: 6 additions & 0 deletions Ink Canvas/Helpers/InkCanvasElementHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,11 @@ public static Rect GetAllElementsBounds(InkCanvas inkCanvas)
}
return totalBounds;
}

public static Point GetAllElementsBoundsCenterPoint(InkCanvas inkCanvas)
{
Rect bounds = GetAllElementsBounds(inkCanvas);
return new Point(bounds.Left + bounds.Width / 2, bounds.Top + bounds.Height / 2);
}
}
}
4 changes: 1 addition & 3 deletions Ink Canvas/Helpers/InkCanvasImageHelper.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace Ink_Canvas.Helpers
{
Expand All @@ -13,7 +11,7 @@ public static bool IsNotCanvasElementSelected(InkCanvas inkCanvas)
if (inkCanvas.GetSelectedStrokes().Count > 0) return false;
foreach (UIElement element in inkCanvas.GetSelectedElements())
{
if (element is Image selectedImage)
if (element is Image)
{
return false;
}
Expand Down
13 changes: 13 additions & 0 deletions Ink Canvas/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -2682,6 +2682,19 @@
<TextBlock FontSize="26" FontWeight="Bold" Foreground="{DynamicResource SettingsPageForeground}" Text="手势选项" />
</GroupBox.Header>
<ikw:SimpleStackPanel Spacing="5">
<ikw:SimpleStackPanel Orientation="Horizontal">
<TextBlock VerticalAlignment="Center" FontSize="14" Foreground="{DynamicResource SettingsPageForeground}" Text="缩放/旋转变换中心点:" />
<ComboBox
Name="ComboBoxMatrixTransformCenterPoint"
Width="180"
Margin="5,0,0,0"
SelectedIndex="0"
SelectionChanged="ComboBoxMatrixTransformCenterPoint_SelectionChanged">
<ComboBoxItem Content="画布中心点(默认)" />
<ComboBoxItem Content="选择内容中心点" />
<ComboBoxItem Content="手势操作中心点" />
</ComboBox>
</ikw:SimpleStackPanel>
<ikw:SimpleStackPanel Orientation="Horizontal">
<ikw:SimpleStackPanel Width="50">
<ui:ToggleSwitch
Expand Down
1 change: 0 additions & 1 deletion Ink Canvas/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
using System.Windows;
using System.Windows.Controls;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using File = System.IO.File;
using MessageBox = System.Windows.MessageBox;
Expand Down
3 changes: 0 additions & 3 deletions Ink Canvas/MainWindow_cs/MW_ImageControls.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media.Imaging;
using Microsoft.Win32;
using System.Windows.Ink;
using System.Windows.Media;

namespace Ink_Canvas
{
Expand Down
27 changes: 27 additions & 0 deletions Ink Canvas/MainWindow_cs/MW_MatrixTransform.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using Ink_Canvas.Helpers;
using System.Windows;

namespace Ink_Canvas
{
public partial class MainWindow : Window
{
private Point GetMatrixTransformCenterPoint(Point gestureOperationCenterPoint, FrameworkElement fe)
{
Point canvasCenterPoint = new Point(fe.ActualWidth / 2, fe.ActualHeight / 2);
if (!isLoaded) return canvasCenterPoint;
if (Settings.Gesture.MatrixTransformCenterPoint == MatrixTransformCenterPointOptions.CanvasCenterPoint)
{
return canvasCenterPoint;
}
else if (Settings.Gesture.MatrixTransformCenterPoint == MatrixTransformCenterPointOptions.GestureOperationCenterPoint)
{
return gestureOperationCenterPoint;
}
else if (Settings.Gesture.MatrixTransformCenterPoint == MatrixTransformCenterPointOptions.SelectedElementsCenterPoint)
{
return InkCanvasElementHelper.GetAllElementsBoundsCenterPoint(inkCanvas);
}
return canvasCenterPoint;
}
}
}
38 changes: 12 additions & 26 deletions Ink Canvas/MainWindow_cs/MW_SelectionGestures.cs
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,7 @@ private void ChangeSelectedStrokeThickness(double multipler)
private void MatrixTransform(int type)
{
Matrix m = new Matrix();
Point center = new Point(inkCanvas.GetSelectionBounds().Left + inkCanvas.GetSelectionBounds().Width / 2,
inkCanvas.GetSelectionBounds().Top + inkCanvas.GetSelectionBounds().Height / 2);
Point center = InkCanvasElementHelper.GetAllElementsBoundsCenterPoint(inkCanvas);

switch (type)
{
Expand All @@ -181,7 +180,7 @@ private void MatrixTransform(int type)
List<Image> selectedImages = InkCanvasImageHelper.GetSelectedImages(inkCanvas);
foreach (Image image in selectedImages)
{
ApplyImageMatrixTransform(image, m, center);
ApplyImageMatrixTransform(image, m);
}

if (DrawingAttributesHistory.Count > 0)
Expand All @@ -195,23 +194,17 @@ private void MatrixTransform(int type)
}
}

private void ApplyImageMatrixTransform(Image image, Matrix matrix, Point center, double transX = 0, double transY = 0)
{
TransformGroup transformGroup = GetOrCreateTransformGroup(image);
TransformGroup centeredTransformGroup = new TransformGroup();
centeredTransformGroup.Children.Add(new MatrixTransform(matrix));
transformGroup.Children.Add(centeredTransformGroup);
}

private TransformGroup GetOrCreateTransformGroup(Image image)
private void ApplyImageMatrixTransform(Image image, Matrix matrix)
{
TransformGroup transformGroup = image.RenderTransform as TransformGroup;
if (transformGroup == null)
{
transformGroup = new TransformGroup();
image.RenderTransform = transformGroup;
}
return transformGroup;
TransformGroup centeredTransformGroup = new TransformGroup();
centeredTransformGroup.Children.Add(new MatrixTransform(matrix));
transformGroup.Children.Add(centeredTransformGroup);
}

private void BtnFlipHorizontal_Click(object sender, RoutedEventArgs e)
Expand Down Expand Up @@ -293,9 +286,10 @@ private void GridInkCanvasSelectionCover_MouseMove(object sender, MouseEventArgs
Point mousePoint = e.GetPosition(inkCanvas);
Vector trans = new Vector(mousePoint.X - lastMousePoint.X, mousePoint.Y - lastMousePoint.Y);
lastMousePoint = mousePoint;
Rect bounds = inkCanvas.GetSelectionBounds();
Point center = new Point(bounds.Left + bounds.Width / 2, bounds.Top + bounds.Height / 2);
Point center = InkCanvasElementHelper.GetAllElementsBoundsCenterPoint(inkCanvas);
Matrix m = new Matrix();
// add Translate
m.Translate(trans.X, trans.Y);
// handle images
List<Image> images = new List<Image>();
if (ImagesSelectionClone.Count != 0)
Expand All @@ -308,10 +302,8 @@ private void GridInkCanvasSelectionCover_MouseMove(object sender, MouseEventArgs
}
foreach (Image image in images)
{
ApplyImageMatrixTransform(image, m, center, trans.X, trans.Y);
ApplyImageMatrixTransform(image, m);
}
// add Translate
m.Translate(trans.X, trans.Y);
// handle strokes
StrokeCollection strokes = inkCanvas.GetSelectedStrokes();
if (StrokesSelectionClone.Count != 0)
Expand Down Expand Up @@ -497,13 +489,7 @@ private void GridInkCanvasSelectionCover_ManipulationDelta(object sender, Manipu
Vector trans = md.Translation;
double rotate = md.Rotation;
Vector scale = md.Scale;
Rect bounds = InkCanvasElementHelper.GetAllElementsBounds(inkCanvas);
Point center = new Point(bounds.Left + bounds.Width / 2, bounds.Top + bounds.Height / 2);
/*
Rect bounds = inkCanvas.GetSelectionBounds();
Point center = new Point(bounds.Left + bounds.Width / 2, bounds.Top + bounds.Height / 2);
*/
//Point center = e.ManipulationOrigin;
Point center = GetMatrixTransformCenterPoint(e.ManipulationOrigin, e.Source as FrameworkElement);
Matrix m = new Matrix();
// add Scale
m.ScaleAt(scale.X, scale.Y, center.X, center.Y);
Expand Down Expand Up @@ -531,7 +517,7 @@ private void GridInkCanvasSelectionCover_ManipulationDelta(object sender, Manipu
// handle images
foreach (Image image in images)
{
ApplyImageMatrixTransform(image, m, center, trans.X, trans.Y);
ApplyImageMatrixTransform(image, m);
}
// handle strokes
foreach (Stroke stroke in strokes)
Expand Down
8 changes: 8 additions & 0 deletions Ink Canvas/MainWindow_cs/MW_Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,13 @@ private void ToggleSwitchAutoSaveScreenShotInPowerPoint_Toggled(object sender, R

#region Gesture

private void ComboBoxMatrixTransformCenterPoint_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (!isLoaded) return;
Settings.Gesture.MatrixTransformCenterPoint = (MatrixTransformCenterPointOptions)ComboBoxMatrixTransformCenterPoint.SelectedIndex;
SaveSettingsToFile();
}

private void ToggleSwitchEnableFingerGestureSlideShowControl_Toggled(object sender, RoutedEventArgs e)
{
if (!isLoaded) return;
Expand Down Expand Up @@ -695,6 +702,7 @@ public static void SetSettingsToRecommendation()
Settings.Canvas.UsingWhiteboard = false;
Settings.Canvas.HyperbolaAsymptoteOption = 0;

Settings.Gesture.MatrixTransformCenterPoint = MatrixTransformCenterPointOptions.CanvasCenterPoint;
Settings.Gesture.AutoSwitchTwoFingerGesture = true;
Settings.Gesture.IsEnableTwoFingerTranslate = true;
Settings.Gesture.IsEnableTwoFingerZoom = false;
Expand Down
1 change: 1 addition & 0 deletions Ink Canvas/MainWindow_cs/MW_SettingsToLoad.cs
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,7 @@ private void LoadSettings(bool isStartup = false)
// Gesture
if (Settings.Gesture != null)
{
ComboBoxMatrixTransformCenterPoint.SelectedIndex = (int)Settings.Gesture.MatrixTransformCenterPoint;
if (Settings.Gesture.IsEnableMultiTouchMode)
{
ToggleSwitchEnableMultiTouchMode.IsOn = true;
Expand Down
14 changes: 4 additions & 10 deletions Ink Canvas/MainWindow_cs/MW_TouchEvents.cs
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ public double GetTouchBoundWidth(TouchEventArgs e)
//记录触摸设备ID
private List<int> dec = new List<int>();
//中心点
System.Windows.Point centerPoint;
Point centerPoint;
InkCanvasEditingMode lastInkCanvasEditingMode = InkCanvasEditingMode.Ink;
bool isSingleFingerDragMode = false;

Expand Down Expand Up @@ -376,13 +376,7 @@ private void Main_Grid_ManipulationDelta(object sender, ManipulationDeltaEventAr
{
double rotate = md.Rotation;
Vector scale = md.Scale;
FrameworkElement fe = e.Source as FrameworkElement;
Point center = new Point(fe.ActualWidth / 2, fe.ActualHeight / 2);
/*
Rect bounds = InkCanvasElementHelper.GetAllElementsBounds(inkCanvas);
Point center = new Point(bounds.Left + bounds.Width / 2, bounds.Top + bounds.Height / 2);
*/
//Point center = e.ManipulationOrigin;
Point center = GetMatrixTransformCenterPoint(e.ManipulationOrigin, e.Source as FrameworkElement);
if (Settings.Gesture.IsEnableTwoFingerZoom)
m.ScaleAt(scale.X, scale.Y, center.X, center.Y);
if (Settings.Gesture.IsEnableTwoFingerRotation)
Expand All @@ -395,11 +389,11 @@ private void Main_Grid_ManipulationDelta(object sender, ManipulationDeltaEventAr
{
if (Settings.Gesture.IsEnableTwoFingerTranslate)
{
ApplyImageMatrixTransform(image, m, center, trans.X, trans.Y);
ApplyImageMatrixTransform(image, m);
}
else
{
ApplyImageMatrixTransform(image, m, center);
ApplyImageMatrixTransform(image, m);
}
}
}
Expand Down
10 changes: 10 additions & 0 deletions Ink Canvas/Resources/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,16 @@ public class Gesture
public bool IsEnableTwoFingerRotation { get; set; } = false;
[JsonProperty("isEnableTwoFingerRotationOnSelection")]
public bool IsEnableTwoFingerRotationOnSelection { get; set; } = false;

[JsonProperty("matrixTransformCenterPoint")]
public MatrixTransformCenterPointOptions MatrixTransformCenterPoint { get; set; } = MatrixTransformCenterPointOptions.CanvasCenterPoint;
}

public enum MatrixTransformCenterPointOptions
{
CanvasCenterPoint,
SelectedElementsCenterPoint,
GestureOperationCenterPoint
}

public class Startup
Expand Down

0 comments on commit d9a1f97

Please sign in to comment.