Skip to content

Commit 1d92a03

Browse files
committed
Set image default scale
1 parent de41977 commit 1d92a03

File tree

3 files changed

+31
-15
lines changed

3 files changed

+31
-15
lines changed

src/GenshinWoodmen/Views/UIElement/ImageViewWindow.xaml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@
44
xmlns:local="clr-namespace:GenshinWoodmen.Views"
55
xmlns:mica="clr-namespace:MicaWPF.Controls;assembly=MicaWPF"
66
WindowStartupLocation="CenterScreen"
7-
Icon="../../Resources/favicon.ico"
8-
RenderOptions.BitmapScalingMode="Fant"
9-
WindowState="Maximized" Cursor="Hand">
7+
TitleBarType="WinUI"
8+
Icon="../../Resources/favicon.ico"
9+
RenderOptions.BitmapScalingMode="Fant"
10+
WindowState="Maximized" Cursor="Hand">
1011
<Grid>
11-
<local:PanAndZoomCanvas>
12+
<local:PanAndZoomCanvas x:Name="imageCanvas" MinDeterminant="0.005" MaxDeterminant="4">
1213
<Image x:Name="imageControl" Stretch="Uniform">
1314
<Image.RenderTransform>
1415
<TransformGroup>

src/GenshinWoodmen/Views/UIElement/ImageViewWindow.xaml.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using GenshinWoodmen.Core;
22
using MicaWPF.Controls;
33
using System;
4+
using System.Windows.Forms;
45
using System.Windows.Media.Imaging;
56

67
namespace GenshinWoodmen.Views
@@ -10,6 +11,11 @@ public partial class ImageViewWindow : MicaWindow
1011
public ImageViewWindow()
1112
{
1213
InitializeComponent();
14+
15+
imageControl.Loaded += (_, _) =>
16+
{
17+
imageCanvas.SetTransform(new(0.8d, 0d, 0d, 0.8d, (Width - imageControl.Source.Width * 0.8d) / 2d - 5, -SystemInformation.CaptionHeight - 8));
18+
};
1319
}
1420

1521
public void Load(string uriString)

src/GenshinWoodmen/Views/UIElement/PanAndZoomCanvas.cs

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,12 @@ namespace GenshinWoodmen.Views
1010
/// </summary>
1111
public class PanAndZoomCanvas : Canvas
1212
{
13-
public MatrixTransform? Transform { get; protected set; } = null!;
14-
public float Zoomfactor { get; set; } = 1.25f;
13+
public MatrixTransform Transform { get; set; } = new();
14+
public float? MinDeterminant { get; set; } = null!;
15+
public float? MaxDeterminant { get; set; } = null!;
16+
public float Zoomfactor { get; set; } = 1.333f;
1517
protected Point MousePosition = new();
18+
protected bool DownFlow = false;
1619

1720
public PanAndZoomCanvas()
1821
{
@@ -24,21 +27,21 @@ public PanAndZoomCanvas()
2427

2528
private void OnMouseDown(object sender, MouseButtonEventArgs e)
2629
{
27-
if (e.ChangedButton == MouseButton.Left)
30+
if (e.ChangedButton == MouseButton.Left && e.ClickCount == 1)
2831
{
29-
Transform ??= new();
3032
MousePosition = Transform.Inverse.Transform(e.GetPosition(this));
33+
DownFlow = true;
3134
}
3235
}
3336

3437
private void OnMouseUp(object sender, MouseButtonEventArgs e)
3538
{
39+
DownFlow = false;
3640
}
3741

3842
private void OnMouseMove(object sender, MouseEventArgs e)
3943
{
40-
if (Transform == null) return;
41-
if (e.LeftButton == MouseButtonState.Pressed)
44+
if (e.LeftButton == MouseButtonState.Pressed && DownFlow)
4245
{
4346
Point mousePosition = Transform.Inverse.Transform(e.GetPosition(this));
4447
Vector delta = Point.Subtract(mousePosition, MousePosition);
@@ -62,23 +65,29 @@ private void OnMouseWheel(object sender, MouseWheelEventArgs e)
6265

6366
Point mousePostion = e.GetPosition(this);
6467

65-
Matrix scaleMatrix = (Transform ??= new()).Matrix;
68+
Matrix scaleMatrix = Transform.Matrix;
6669
scaleMatrix.ScaleAt(scaleFactor, scaleFactor, mousePostion.X, mousePostion.Y);
70+
if (scaleMatrix.Determinant <= MinDeterminant || scaleMatrix.Determinant >= MaxDeterminant) return;
6771
Transform.Matrix = scaleMatrix;
6872

73+
SetTransform(Transform);
74+
}
75+
76+
public void SetTransform(MatrixTransform transform)
77+
{
6978
foreach (UIElement child in Children)
7079
{
7180
double x = Canvas.GetLeft(child);
7281
double y = Canvas.GetTop(child);
7382

74-
double sx = x * scaleFactor;
75-
double sy = y * scaleFactor;
83+
double sx = x ;
84+
double sy = y;
7685

7786
Canvas.SetLeft(child, sx);
7887
Canvas.SetTop(child, sy);
7988

80-
child.RenderTransform = Transform;
89+
child.RenderTransform = Transform = transform;
8190
}
8291
}
8392
}
84-
}
93+
}

0 commit comments

Comments
 (0)