From 66fd39205c8952bdbfb45cca3af046533cca2975 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Kosson?= Date: Mon, 18 Sep 2023 06:43:55 +0200 Subject: [PATCH] #2 - Added zoom in/out shortcut and menu item --- Canvas.cs | 16 ++++++++++++---- MainForm.Designer.cs | 44 ++++++++++++++++++++++++++++++++------------ MainForm.cs | 4 ++++ 3 files changed, 48 insertions(+), 16 deletions(-) diff --git a/Canvas.cs b/Canvas.cs index 5c426fd..0b95d6b 100644 --- a/Canvas.cs +++ b/Canvas.cs @@ -432,11 +432,19 @@ private Vector2 Snap(Vector2 point, params Vector2[] hints) protected override void OnMouseWheel(MouseEventArgs e) { base.OnMouseWheel(e); + Zoom(e.Delta > 0 ? 1.1f : 1 / 1.1f, e.X, e.Y); + } - viewMatrix.Translate(-e.X, -e.Y, MatrixOrder.Append); - if (e.Delta > 0) viewMatrix.Scale(1.1f, 1.1f, MatrixOrder.Append); - else viewMatrix.Scale(1 / 1.1f, 1 / 1.1f, MatrixOrder.Append); - viewMatrix.Translate(e.X, e.Y, MatrixOrder.Append); + public void Zoom(float scale) + { + Zoom(scale, Width / 2, Height / 2); + } + + public void Zoom(float scale, int x, int y) + { + viewMatrix.Translate(-x, -y, MatrixOrder.Append); + viewMatrix.Scale(scale, scale, MatrixOrder.Append); + viewMatrix.Translate(x, y, MatrixOrder.Append); matrixUpdated = true; diff --git a/MainForm.Designer.cs b/MainForm.Designer.cs index c6db682..0d33da1 100644 --- a/MainForm.Designer.cs +++ b/MainForm.Designer.cs @@ -56,6 +56,8 @@ private void InitializeComponent() toolStripSeparator4 = new System.Windows.Forms.ToolStripSeparator(); selectAllToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); toolStripSeparator5 = new System.Windows.Forms.ToolStripSeparator(); + commentUncommentLinesToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + toolStripSeparator9 = new System.Windows.Forms.ToolStripSeparator(); convertToAbsoluteToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); convertToRelativeToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); toolStripSeparator6 = new System.Windows.Forms.ToolStripSeparator(); @@ -88,8 +90,8 @@ private void InitializeComponent() backgroundImageToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); textShapeToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); aboutToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); - commentUncommentLinesToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); - toolStripSeparator9 = new System.Windows.Forms.ToolStripSeparator(); + zoomInToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + zoomOutToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); ((System.ComponentModel.ISupportInitialize)splitContainer).BeginInit(); splitContainer.Panel1.SuspendLayout(); splitContainer.Panel2.SuspendLayout(); @@ -320,6 +322,19 @@ private void InitializeComponent() toolStripSeparator5.Name = "toolStripSeparator5"; toolStripSeparator5.Size = new System.Drawing.Size(241, 6); // + // commentUncommentLinesToolStripMenuItem + // + commentUncommentLinesToolStripMenuItem.Name = "commentUncommentLinesToolStripMenuItem"; + commentUncommentLinesToolStripMenuItem.ShortcutKeyDisplayString = "/"; + commentUncommentLinesToolStripMenuItem.Size = new System.Drawing.Size(244, 22); + commentUncommentLinesToolStripMenuItem.Text = "Comment / uncomment lines"; + commentUncommentLinesToolStripMenuItem.Click += commentUncommentLinesToolStripMenuItem_Click; + // + // toolStripSeparator9 + // + toolStripSeparator9.Name = "toolStripSeparator9"; + toolStripSeparator9.Size = new System.Drawing.Size(241, 6); + // // convertToAbsoluteToolStripMenuItem // convertToAbsoluteToolStripMenuItem.Name = "convertToAbsoluteToolStripMenuItem"; @@ -390,7 +405,7 @@ private void InitializeComponent() // // viewToolStripMenuItem // - viewToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] { showGridToolStripMenuItem, snapToGridToolStripMenuItem, snapToItemsToolStripMenuItem, snapToAxesToolStripMenuItem, showCoordinatesToolStripMenuItem, showPerformanceStatsToolStripMenuItem, panzoomToFitToolStripMenuItem, panViewToSelectionToolStripMenuItem }); + viewToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] { showGridToolStripMenuItem, snapToGridToolStripMenuItem, snapToItemsToolStripMenuItem, snapToAxesToolStripMenuItem, showCoordinatesToolStripMenuItem, showPerformanceStatsToolStripMenuItem, panzoomToFitToolStripMenuItem, panViewToSelectionToolStripMenuItem, zoomInToolStripMenuItem, zoomOutToolStripMenuItem }); viewToolStripMenuItem.Name = "viewToolStripMenuItem"; viewToolStripMenuItem.Size = new System.Drawing.Size(44, 20); viewToolStripMenuItem.Text = "View"; @@ -564,18 +579,21 @@ private void InitializeComponent() aboutToolStripMenuItem.Text = "About"; aboutToolStripMenuItem.Click += aboutToolStripMenuItem_Click; // - // commentUncommentLinesToolStripMenuItem + // zoomInToolStripMenuItem // - commentUncommentLinesToolStripMenuItem.Name = "commentUncommentLinesToolStripMenuItem"; - commentUncommentLinesToolStripMenuItem.ShortcutKeyDisplayString = "/"; - commentUncommentLinesToolStripMenuItem.Size = new System.Drawing.Size(244, 22); - commentUncommentLinesToolStripMenuItem.Text = "Comment / uncomment lines"; - commentUncommentLinesToolStripMenuItem.Click += commentUncommentLinesToolStripMenuItem_Click; + zoomInToolStripMenuItem.Name = "zoomInToolStripMenuItem"; + zoomInToolStripMenuItem.ShortcutKeyDisplayString = "="; + zoomInToolStripMenuItem.Size = new System.Drawing.Size(233, 22); + zoomInToolStripMenuItem.Text = "Zoom in"; + zoomInToolStripMenuItem.Click += zoomInToolStripMenuItem_Click; // - // toolStripSeparator9 + // zoomOutToolStripMenuItem // - toolStripSeparator9.Name = "toolStripSeparator9"; - toolStripSeparator9.Size = new System.Drawing.Size(241, 6); + zoomOutToolStripMenuItem.Name = "zoomOutToolStripMenuItem"; + zoomOutToolStripMenuItem.ShortcutKeyDisplayString = "-"; + zoomOutToolStripMenuItem.Size = new System.Drawing.Size(233, 22); + zoomOutToolStripMenuItem.Text = "Zoom out"; + zoomOutToolStripMenuItem.Click += zoomOutToolStripMenuItem_Click; // // MainForm // @@ -663,5 +681,7 @@ private void InitializeComponent() private System.Windows.Forms.ToolStripMenuItem scaleToolStripMenuItem; private System.Windows.Forms.ToolStripMenuItem commentUncommentLinesToolStripMenuItem; private System.Windows.Forms.ToolStripSeparator toolStripSeparator9; + private System.Windows.Forms.ToolStripMenuItem zoomInToolStripMenuItem; + private System.Windows.Forms.ToolStripMenuItem zoomOutToolStripMenuItem; } } \ No newline at end of file diff --git a/MainForm.cs b/MainForm.cs index d10fc77..eed7fbe 100644 --- a/MainForm.cs +++ b/MainForm.cs @@ -336,6 +336,8 @@ protected override void OnKeyDown(KeyEventArgs e) else if (e.KeyCode == Keys.OemSemicolon && e.Modifiers == Keys.None) AddComment(false); else if (e.KeyCode == Keys.OemSemicolon && e.Modifiers == Keys.Shift) AddComment(true); else if (e.KeyCode == Keys.OemQuestion && e.Modifiers == Keys.None) ToggleComment(); + else if (e.KeyCode == Keys.Oemplus && e.Modifiers == Keys.None) canvas.Zoom(1.1f); + else if (e.KeyCode == Keys.OemMinus && e.Modifiers == Keys.None) canvas.Zoom(1 / 1.1f); else { e.Handled = false; } if (e.Handled && e.Alt) { e.SuppressKeyPress = true; } @@ -382,5 +384,7 @@ protected override void OnKeyDown(KeyEventArgs e) private void convertToOutlineToolStripMenuItem_Click(object sender, EventArgs e) => ConvertToOutline(); private void rotateToolStripMenuItem_Click(object sender, EventArgs e) => canvas.StartMouseRotate(); private void commentUncommentLinesToolStripMenuItem_Click(object sender, EventArgs e) => ToggleComment(); + private void zoomInToolStripMenuItem_Click(object sender, EventArgs e) => canvas.Zoom(1.5f); + private void zoomOutToolStripMenuItem_Click(object sender, EventArgs e) => canvas.Zoom(1 / 1.5f); } } \ No newline at end of file