From afe9142d7d9bc88425ca47d90ace2ad03df5dd5a Mon Sep 17 00:00:00 2001 From: Raspberry-Monster Date: Sat, 25 May 2024 17:40:59 +0800 Subject: [PATCH] =?UTF-8?q?[BugFix]=20=E4=BF=AE=E5=A4=8DDrawingAttributes?= =?UTF-8?q?=20=E6=97=A0=E6=B3=95=E6=AD=A3=E7=A1=AE=E4=BF=9D=E5=AD=98?= =?UTF-8?q?=E7=9A=84Bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Ink Canvas/Helpers/TimeMachine.cs | 43 ++++-- Ink Canvas/MainWindow.xaml.cs | 217 +++++++++++++++++++++++------- 2 files changed, 202 insertions(+), 58 deletions(-) diff --git a/Ink Canvas/Helpers/TimeMachine.cs b/Ink Canvas/Helpers/TimeMachine.cs index cbc601f4..84e92967 100644 --- a/Ink Canvas/Helpers/TimeMachine.cs +++ b/Ink Canvas/Helpers/TimeMachine.cs @@ -1,7 +1,7 @@ -using System.Collections.Generic; -using System.Windows; +using System; +using System.Collections.Generic; using System.Windows.Ink; -using System.Windows.Media; +using System.Windows.Input; namespace Ink_Canvas.Helpers { @@ -44,20 +44,30 @@ public void CommitStrokeShapeHistory(StrokeCollection strokeToBeReplaced, Stroke NotifyUndoRedoState(); } - public void CommitStrokeManipulationHistory(StrokeCollection manipulatedStrokes, Matrix matrix) + public void CommitStrokeManipulationHistory(Dictionary> stylusPointDictionary) { if (_currentIndex + 1 < _currentStrokeHistory.Count) { _currentStrokeHistory.RemoveRange(_currentIndex + 1, (_currentStrokeHistory.Count - 1) - _currentIndex); } _currentStrokeHistory.Add( - new TimeMachineHistory(manipulatedStrokes, - TimeMachineHistoryType.Manipulation, - matrix)); + new TimeMachineHistory(stylusPointDictionary, + TimeMachineHistoryType.Manipulation)); + _currentIndex = _currentStrokeHistory.Count - 1; + NotifyUndoRedoState(); + } + public void CommitStrokeDrawingAttributesHistory(Dictionary> drawingAttributes) + { + if (_currentIndex + 1 < _currentStrokeHistory.Count) + { + _currentStrokeHistory.RemoveRange(_currentIndex + 1, (_currentStrokeHistory.Count - 1) - _currentIndex); + } + _currentStrokeHistory.Add( + new TimeMachineHistory(drawingAttributes, + TimeMachineHistoryType.DrawingAttributes)); _currentIndex = _currentStrokeHistory.Count - 1; NotifyUndoRedoState(); } - public void CommitStrokeEraseHistory(StrokeCollection stroke, StrokeCollection sourceStroke = null) { if (_currentIndex + 1 < _currentStrokeHistory.Count) @@ -124,7 +134,9 @@ public class TimeMachineHistory public bool StrokeHasBeenCleared; public StrokeCollection CurrentStroke; public StrokeCollection ReplacedStroke; - public Matrix ManipulationHistory; + //这里说一下 Tuple的 Value1 是初始值 ; Value 2 是改变值 + public Dictionary> StylusPointDictionary; + public Dictionary> DrawingAttributes; public TimeMachineHistory(StrokeCollection currentStroke, TimeMachineHistoryType commitType, bool strokeHasBeenCleared) { CommitType = commitType; @@ -139,11 +151,15 @@ public TimeMachineHistory(StrokeCollection currentStroke, TimeMachineHistoryType StrokeHasBeenCleared = strokeHasBeenCleared; ReplacedStroke = replacedStroke; } - public TimeMachineHistory(StrokeCollection currentStroke, TimeMachineHistoryType commitType, Matrix matrix) + public TimeMachineHistory(Dictionary> stylusPointDictionary, TimeMachineHistoryType commitType) { CommitType=commitType; - CurrentStroke = currentStroke; - ManipulationHistory = matrix; + StylusPointDictionary = stylusPointDictionary; + } + public TimeMachineHistory(Dictionary> drawingAttributes, TimeMachineHistoryType commitType) + { + CommitType = commitType; + DrawingAttributes = drawingAttributes; } } @@ -152,6 +168,7 @@ public enum TimeMachineHistoryType UserInput, ShapeRecognition, Clear, - Manipulation + Manipulation, + DrawingAttributes } } \ No newline at end of file diff --git a/Ink Canvas/MainWindow.xaml.cs b/Ink Canvas/MainWindow.xaml.cs index cd115667..8b48ff20 100644 --- a/Ink Canvas/MainWindow.xaml.cs +++ b/Ink Canvas/MainWindow.xaml.cs @@ -349,6 +349,9 @@ private enum CommitReason private StrokeCollection ReplacedStroke; private StrokeCollection AddedStroke; private StrokeCollection CuboidStrokeCollection; + private Dictionary> StrokeManipulationHistory; + private Dictionary StrokeInitialHistory = new Dictionary(); + private Dictionary> DrawingAttributesHistory = new Dictionary>(); private TimeMachine timeMachine = new TimeMachine(); private void ApplyHistoryToCanvas(TimeMachineHistory item) @@ -405,12 +408,49 @@ private void ApplyHistoryToCanvas(TimeMachineHistory item) } else if (item.CommitType == TimeMachineHistoryType.Manipulation) { - item.ManipulationHistory.Invert(); - foreach (var strokes in item.CurrentStroke) + if (!item.StrokeHasBeenCleared) + { + foreach (var currentStroke in item.StylusPointDictionary) { - strokes.Transform(item.ManipulationHistory, false); + if (inkCanvas.Strokes.Contains(currentStroke.Key)) + { + currentStroke.Key.StylusPoints = currentStroke.Value.Item2; + } } - + } + else + { + foreach (var currentStroke in item.StylusPointDictionary) + { + if (inkCanvas.Strokes.Contains(currentStroke.Key)) + { + currentStroke.Key.StylusPoints = currentStroke.Value.Item1; + } + } + } + } + else if (item.CommitType == TimeMachineHistoryType.DrawingAttributes) + { + if (!item.StrokeHasBeenCleared) + { + foreach (var currentStroke in item.DrawingAttributes) + { + if (inkCanvas.Strokes.Contains(currentStroke.Key)) + { + currentStroke.Key.DrawingAttributes = currentStroke.Value.Item2; + } + } + } + else + { + foreach (var currentStroke in item.DrawingAttributes) + { + if (inkCanvas.Strokes.Contains(currentStroke.Key)) + { + currentStroke.Key.DrawingAttributes = currentStroke.Value.Item1; + } + } + } } else if (item.CommitType == TimeMachineHistoryType.Clear) { @@ -470,6 +510,20 @@ private void TimeMachine_OnRedoStateChanged(bool status) private void StrokesOnStrokesChanged(object sender, StrokeCollectionChangedEventArgs e) { + foreach (var stroke in e?.Removed) + { + stroke.StylusPointsChanged -= Stroke_StylusPointsChanged; + stroke.StylusPointsReplaced -= Stroke_StylusPointsReplaced; + stroke.DrawingAttributesChanged -= Stroke_DrawingAttributesChanged; + StrokeInitialHistory.Remove(stroke); + } + foreach (var stroke in e?.Added) + { + stroke.StylusPointsChanged += Stroke_StylusPointsChanged; + stroke.StylusPointsReplaced += Stroke_StylusPointsReplaced; + stroke.DrawingAttributesChanged += Stroke_DrawingAttributesChanged; + StrokeInitialHistory[stroke] = stroke.StylusPoints.Clone(); + } if (_currentCommitType == CommitReason.CodeInput || _currentCommitType == CommitReason.ShapeDrawing) return; if ((e.Added.Count != 0 || e.Removed.Count != 0) && IsEraseByPoint) { @@ -509,6 +563,68 @@ private void StrokesOnStrokesChanged(object sender, StrokeCollectionChangedEvent } } + private void Stroke_DrawingAttributesChanged(object sender, PropertyDataChangedEventArgs e) + { + var key = sender as Stroke; + var currentValue = key.DrawingAttributes.Clone(); + var previousValue = key.DrawingAttributes.Clone(); + if(e.PropertyGuid == DrawingAttributeIds.Color) + { + previousValue.Color = (Color)e.PreviousValue; + } + if (e.PropertyGuid == DrawingAttributeIds.IsHighlighter) + { + previousValue.IsHighlighter = (bool)e.PreviousValue; + } + if (e.PropertyGuid == DrawingAttributeIds.StylusHeight) + { + previousValue.Height = (int)e.PreviousValue; + } + if (e.PropertyGuid == DrawingAttributeIds.StylusWidth) + { + previousValue.Width = (int)e.PreviousValue; + } + if (e.PropertyGuid == DrawingAttributeIds.StylusTip) + { + previousValue.StylusTip = (StylusTip)e.PreviousValue; + } + if (e.PropertyGuid == DrawingAttributeIds.StylusTipTransform) + { + previousValue.StylusTipTransform = (Matrix)e.PreviousValue; + } + if (e.PropertyGuid == DrawingAttributeIds.DrawingFlags) + { + previousValue.IgnorePressure = (bool)e.PreviousValue; + } + DrawingAttributesHistory[key] = new Tuple(previousValue, currentValue); + } + + private void Stroke_StylusPointsReplaced(object sender, StylusPointsReplacedEventArgs e) + { + StrokeInitialHistory[sender as Stroke] = e.NewStylusPoints.Clone(); + } + + private void Stroke_StylusPointsChanged(object sender, EventArgs e) + { + var selectedStrokes = inkCanvas.GetSelectedStrokes(); + var count = selectedStrokes.Count; + if (count == 0) count = inkCanvas.Strokes.Count; + if(StrokeManipulationHistory == null) + { + StrokeManipulationHistory = new Dictionary>(); + } + StrokeManipulationHistory[sender as Stroke] = + new Tuple(StrokeInitialHistory[sender as Stroke], (sender as Stroke).StylusPoints.Clone()); + if ((StrokeManipulationHistory.Count == count || sender == null) && dec.Count == 0 ) + { + timeMachine.CommitStrokeManipulationHistory(StrokeManipulationHistory); + foreach (var item in StrokeManipulationHistory) + { + StrokeInitialHistory[item.Key] = item.Value.Item2; + } + StrokeManipulationHistory = null; + } + } #endregion #region Definations and Loading @@ -1622,6 +1738,11 @@ private void ColorSwitchCheck() catch { } } } + if (DrawingAttributesHistory.Count > 0) + { + timeMachine.CommitStrokeDrawingAttributesHistory(DrawingAttributesHistory); + DrawingAttributesHistory = new Dictionary>(); + } else { inkCanvas.IsManipulationEnabled = true; @@ -2156,15 +2277,6 @@ private void Main_Grid_ManipulationDelta(object sender, ManipulationDeltaEventAr catch { } } } - if(lastTempManiputlaionMatrix == null) - { - lastTempManiputlaionMatrix = m; - lastTempStrokeCollection = strokes; - } - else - { - lastTempManiputlaionMatrix?.Append(m); - } } else { @@ -2188,15 +2300,6 @@ private void Main_Grid_ManipulationDelta(object sender, ManipulationDeltaEventAr circle.Centroid = new Point((circle.Stroke.StylusPoints[0].X + circle.Stroke.StylusPoints[circle.Stroke.StylusPoints.Count / 2].X) / 2, (circle.Stroke.StylusPoints[0].Y + circle.Stroke.StylusPoints[circle.Stroke.StylusPoints.Count / 2].Y) / 2); } - if (lastTempManiputlaionMatrix == null) - { - lastTempManiputlaionMatrix = m; - lastTempStrokeCollection = inkCanvas.Strokes; - } - else - { - lastTempManiputlaionMatrix?.Append(m); - } } } } @@ -3592,6 +3695,12 @@ private void ChangeStrokeThickness(double multipler) stroke.DrawingAttributes.Height = newHeight; } } + if (DrawingAttributesHistory.Count > 0) + { + + timeMachine.CommitStrokeDrawingAttributesHistory(DrawingAttributesHistory); + DrawingAttributesHistory = new Dictionary>(); + } } private void GridPenWidthRestore_MouseUp(object sender, MouseButtonEventArgs e) @@ -3626,8 +3735,16 @@ private void ImageFlipHorizontal_MouseUp(object sender, MouseButtonEventArgs e) { stroke.Transform(m, false); } - timeMachine.CommitStrokeManipulationHistory(targetStrokes, m); - + if (DrawingAttributesHistory.Count > 0) + { + var collecion = new StrokeCollection(); + foreach (var item in DrawingAttributesHistory) + { + collecion.Add(item.Key); + } + timeMachine.CommitStrokeDrawingAttributesHistory(DrawingAttributesHistory); + DrawingAttributesHistory = new Dictionary>(); + } //updateBorderStrokeSelectionControlLocation(); } @@ -3652,7 +3769,11 @@ private void ImageFlipVertical_MouseUp(object sender, MouseButtonEventArgs e) { stroke.Transform(m, false); } - timeMachine.CommitStrokeManipulationHistory(targetStrokes, m); + if (DrawingAttributesHistory.Count > 0) + { + timeMachine.CommitStrokeDrawingAttributesHistory(DrawingAttributesHistory); + DrawingAttributesHistory = new Dictionary>(); + } } private void ImageRotate45_MouseUp(object sender, MouseButtonEventArgs e) @@ -3676,7 +3797,11 @@ private void ImageRotate45_MouseUp(object sender, MouseButtonEventArgs e) { stroke.Transform(m, false); } - timeMachine.CommitStrokeManipulationHistory(targetStrokes, m); + if (DrawingAttributesHistory.Count > 0) + { + timeMachine.CommitStrokeDrawingAttributesHistory(DrawingAttributesHistory); + DrawingAttributesHistory = new Dictionary>(); + } } private void ImageRotate90_MouseUp(object sender, MouseButtonEventArgs e) @@ -3700,7 +3825,16 @@ private void ImageRotate90_MouseUp(object sender, MouseButtonEventArgs e) { stroke.Transform(m, false); } - timeMachine.CommitStrokeManipulationHistory(targetStrokes, m); + if (DrawingAttributesHistory.Count > 0) + { + var collecion = new StrokeCollection(); + foreach (var item in DrawingAttributesHistory) + { + collecion.Add(item.Key); + } + timeMachine.CommitStrokeDrawingAttributesHistory(DrawingAttributesHistory); + DrawingAttributesHistory = new Dictionary>(); + } } #endregion @@ -3771,7 +3905,7 @@ private void inkCanvas_SelectionChanged(object sender, EventArgs e) GridInkCanvasSelectionCover.Visibility = Visibility.Visible; BorderStrokeSelectionClone.Background = Brushes.Transparent; isStrokeSelectionCloneOn = false; - updateBorderStrokeSelectionControlLocation(); + updateBorderStrokeSelectionControlLocation(); } } @@ -3840,15 +3974,6 @@ private void GridInkCanvasSelectionCover_ManipulationDelta(object sender, Manipu } catch { } } - if (lastTempManiputlaionMatrix == null) - { - lastTempManiputlaionMatrix = m; - lastTempStrokeCollection = strokes; - } - else - { - lastTempManiputlaionMatrix?.Append(m); - } updateBorderStrokeSelectionControlLocation(); } } @@ -5109,7 +5234,6 @@ private void Main_Grid_TouchUp(object sender, TouchEventArgs e) isWaitUntilNextTouchDown = false; } } - Matrix? lastTempManiputlaionMatrix = null; Stroke lastTempStroke = null; StrokeCollection lastTempStrokeCollection = new StrokeCollection(); bool isWaitUntilNextTouchDown = false; @@ -5404,15 +5528,17 @@ private void inkCanvas_MouseUp(object sender, MouseButtonEventArgs e) timeMachine.CommitStrokeUserInputHistory(collection); } } - if(lastTempManiputlaionMatrix != null) - { - timeMachine.CommitStrokeManipulationHistory(lastTempStrokeCollection, lastTempManiputlaionMatrix.Value); - lastTempStrokeCollection = null; - lastTempManiputlaionMatrix = null; - } lastTempStroke = null; lastTempStrokeCollection = null; - lastTempManiputlaionMatrix = null; + if(StrokeManipulationHistory?.Count > 0) + { + timeMachine.CommitStrokeManipulationHistory(StrokeManipulationHistory); + foreach (var item in StrokeManipulationHistory) + { + StrokeInitialHistory[item.Key] = item.Value.Item2; + } + StrokeManipulationHistory = null; + } } private bool NeedUpdateIniP() @@ -6981,6 +7107,7 @@ private void SymbolIconTools_MouseUp(object sender, MouseButtonEventArgs e) } } + #region Drag bool isDragDropInEffect = false;