From 5d4f0d45d53cd533143727e7ed8a9a7bc8333fcf Mon Sep 17 00:00:00 2001 From: Raspberry-Monster Date: Sat, 15 Apr 2023 19:07:01 +0800 Subject: [PATCH 01/21] =?UTF-8?q?[TimeMachine]=20=E5=BC=95=E5=85=A5TimeMac?= =?UTF-8?q?hine=EF=BC=88=E5=A4=9A=E6=AD=A5=E6=92=A4=E5=9B=9E=E5=B7=A5?= =?UTF-8?q?=E5=85=B7=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Ink Canvas/Helpers/TimeMachine.cs | 112 +++++++++++++++++ Ink Canvas/Ink Canvas.csproj | 1 + Ink Canvas/MainWindow.xaml | 6 +- Ink Canvas/MainWindow.xaml.cs | 193 +++++++++++++++++++----------- 4 files changed, 241 insertions(+), 71 deletions(-) create mode 100644 Ink Canvas/Helpers/TimeMachine.cs diff --git a/Ink Canvas/Helpers/TimeMachine.cs b/Ink Canvas/Helpers/TimeMachine.cs new file mode 100644 index 0000000..4823ad5 --- /dev/null +++ b/Ink Canvas/Helpers/TimeMachine.cs @@ -0,0 +1,112 @@ +using System.Collections.Generic; +using System.Linq; +using System.Windows.Documents; +using System.Windows.Forms.VisualStyles; +using System.Windows.Ink; + +namespace Ink_Canvas.Helpers +{ + public class TimeMachine + { + private readonly List _currentStrokeHistory = new List(); + + private int _currentIndex = -1; + + public delegate void OnUndoStateChange(bool status); + + public event OnUndoStateChange OnUndoStateChanged; + + public delegate void OnRedoStateChange(bool status); + + public event OnRedoStateChange OnRedoStateChanged; + + public void CommitStrokeUserInputHistory(StrokeCollection stroke) + { + _currentStrokeHistory.Add(new TimeMachineHistory(stroke, TimeMachineHistoryType.UserInput, false)); + _currentIndex = _currentStrokeHistory.Count - 1; + OnUndoStateChanged?.Invoke(true); + OnRedoStateChanged?.Invoke(false); + } + + public void CommitStrokeShapeHistory(StrokeCollection strokeToBeReplaced, StrokeCollection generatedStroke) + { + _currentStrokeHistory.Add(new TimeMachineHistory(generatedStroke, + TimeMachineHistoryType.ShapeRecognition, + false, + strokeToBeReplaced)); + _currentIndex = _currentStrokeHistory.Count - 1; + OnUndoStateChanged?.Invoke(true); + OnRedoStateChanged?.Invoke(false); + } + + public void CommitStrokeEraseHistory(StrokeCollection stroke) + { + _currentStrokeHistory.Add(new TimeMachineHistory(stroke, TimeMachineHistoryType.UserInput, false)); + _currentIndex = _currentStrokeHistory.Count - 1; + OnUndoStateChanged?.Invoke(true); + OnRedoStateChanged?.Invoke(false); + } + + public void ClearStrokeHistory() + { + _currentStrokeHistory.Clear(); + _currentIndex = -1; + OnUndoStateChanged?.Invoke(true); + OnRedoStateChanged?.Invoke(false); + } + public TimeMachineHistory Undo() + { + var item = _currentStrokeHistory[_currentIndex]; + item.IsReversed = !item.IsReversed; + _currentIndex--; + OnUndoStateChanged?.Invoke(_currentIndex > -1); + OnRedoStateChanged?.Invoke(_currentStrokeHistory.Count - _currentIndex - 1 > 0); + return item; + } + + public TimeMachineHistory Redo() + { + var item = _currentStrokeHistory[++_currentIndex]; + item.IsReversed = !item.IsReversed; + OnUndoStateChanged?.Invoke(_currentIndex > -1); + if (_currentIndex != -1) OnRedoStateChanged?.Invoke(_currentStrokeHistory.Count - _currentIndex - 1 > 0); + return item; + } + public List ExportTimeMachineHistory() + { + throw new System.Exception(); + } + public bool ImportTimeMachineHistory() + { + throw new System.Exception(); + } + } + + public class TimeMachineHistory + { + public TimeMachineHistoryType CommitType; + public bool IsReversed; + public StrokeCollection CurrentStroke; + public StrokeCollection ShapeRecognitionReplacedStroke; + public TimeMachineHistory(StrokeCollection currentStroke, TimeMachineHistoryType commitType, bool isReversed) + { + CommitType = commitType; + CurrentStroke = currentStroke; + IsReversed = isReversed; + ShapeRecognitionReplacedStroke = null; + } + public TimeMachineHistory(StrokeCollection currentStroke, TimeMachineHistoryType commitType, bool isReversed , StrokeCollection shapeRecognitionReplacedStroke) + { + CommitType = commitType; + CurrentStroke = currentStroke; + IsReversed = isReversed; + ShapeRecognitionReplacedStroke = shapeRecognitionReplacedStroke; + } + } + + public enum TimeMachineHistoryType + { + UserInput, + ShapeRecognition + } +} \ No newline at end of file diff --git a/Ink Canvas/Ink Canvas.csproj b/Ink Canvas/Ink Canvas.csproj index f7cb2c8..9cd2800 100644 --- a/Ink Canvas/Ink Canvas.csproj +++ b/Ink Canvas/Ink Canvas.csproj @@ -129,6 +129,7 @@ + NamesInputWindow.xaml diff --git a/Ink Canvas/MainWindow.xaml b/Ink Canvas/MainWindow.xaml index 51537d5..03a4178 100644 --- a/Ink Canvas/MainWindow.xaml +++ b/Ink Canvas/MainWindow.xaml @@ -820,7 +820,7 @@ Margin="0,10,0,0" Width="{Binding ElementName=StackPanelMain, Path=ActualWidth}" Click="BtnUndo_Click" Foreground="{Binding ElementName=BtnExit, Path=Foreground}" Background="{Binding ElementName=BtnExit, Path=Background}" - IsEnabled="False" IsEnabledChanged="Btn_IsEnabledChanged"> + IsEnabled="False" Visibility="Collapsed" IsEnabledChanged="Btn_IsEnabledChanged"> @@ -1101,7 +1101,7 @@ - + - + diff --git a/Ink Canvas/MainWindow.xaml.cs b/Ink Canvas/MainWindow.xaml.cs index acf9ef1..c08084e 100644 --- a/Ink Canvas/MainWindow.xaml.cs +++ b/Ink Canvas/MainWindow.xaml.cs @@ -79,8 +79,12 @@ namespace Ink_Canvas if (File.Exists("debug.ini")) Label.Visibility = Visibility.Visible; InitTimers(); + timeMachine.OnRedoStateChanged += TimeMachine_OnRedoStateChanged; + timeMachine.OnUndoStateChanged += TimeMachine_OnUndoStateChanged; } + + #endregion #region Timer @@ -312,6 +316,25 @@ namespace Ink_Canvas #endregion Hotkeys + #region TimeMachine + + public TimeMachine timeMachine= new TimeMachine(); + private void TimeMachine_OnUndoStateChanged(bool status) + { + var result = status ? Visibility.Visible : Visibility.Collapsed; + BtnUndo.Visibility = result; + BtnUndo.IsEnabled = status; + } + + private void TimeMachine_OnRedoStateChanged(bool status) + { + var result = status ? Visibility.Visible : Visibility.Collapsed; + BtnRedo.Visibility = result; + BtnRedo.IsEnabled = status; + } + + #endregion + #region Definations and Loading public static Settings Settings = new Settings(); @@ -916,12 +939,7 @@ namespace Ink_Canvas whiteboardIndex = 0; } strokeCollections[whiteboardIndex] = inkCanvas.Strokes.Clone(); - - BtnUndo.IsEnabled = true; - BtnUndo.Visibility = Visibility.Visible; - - BtnRedo.IsEnabled = false; - BtnRedo.Visibility = Visibility.Collapsed; + } inkCanvas.Strokes.Clear(); @@ -1037,6 +1055,7 @@ namespace Ink_Canvas ThemeManager.Current.ApplicationTheme = ApplicationTheme.Light; } } + StackPanelPPTButtons.Visibility = Visibility.Visible; break; case 1: //黑板或白板模式 @@ -1060,16 +1079,11 @@ namespace Ink_Canvas SymbolIconBtnColorBlackContent.Foreground = Brushes.White; ThemeManager.Current.ApplicationTheme = ApplicationTheme.Light; } + StackPanelPPTButtons.Visibility = Visibility.Collapsed; break; } } - - BtnUndo.IsEnabled = false; - BtnUndo.Visibility = Visibility.Visible; - - BtnRedo.IsEnabled = false; - BtnRedo.Visibility = Visibility.Collapsed; } private void BtnSwitchTheme_Click(object sender, RoutedEventArgs e) @@ -1784,12 +1798,6 @@ namespace Ink_Canvas whiteboardIndex = 0; } strokeCollections[whiteboardIndex] = lastTouchDownStrokeCollection; - - BtnUndo.IsEnabled = true; - BtnUndo.Visibility = Visibility.Visible; - - BtnRedo.IsEnabled = false; - BtnRedo.Visibility = Visibility.Collapsed; } } } @@ -2208,12 +2216,6 @@ namespace Ink_Canvas // GridBackgroundCover.Visibility = Visibility.Hidden; //} - BtnRedo.IsEnabled = false; - BtnRedo.Visibility = Visibility.Collapsed; - - BtnUndo.IsEnabled = false; - BtnUndo.Visibility = Visibility.Visible; - inkCanvas.Strokes.Clear(); BorderFloatingBarMainControls.Visibility = Visibility.Visible; @@ -2349,12 +2351,7 @@ namespace Ink_Canvas //{ // SaveStrokes(); //} - - BtnRedo.IsEnabled = false; - BtnRedo.Visibility = Visibility.Collapsed; - - BtnUndo.IsEnabled = false; - BtnUndo.Visibility = Visibility.Visible; + inkCanvas.Strokes.Clear(); @@ -2388,11 +2385,6 @@ namespace Ink_Canvas if (inkCanvas.Strokes.Count > Settings.Automation.MinimumAutomationStrokeNumber && Settings.Automation.IsAutoSaveScreenShotInPowerPoint && !_isPptClickingBtnTurned) SaveScreenShot(true, Wn.Presentation.Name + "/" + Wn.View.CurrentShowPosition); _isPptClickingBtnTurned = false; - BtnRedo.IsEnabled = false; - BtnRedo.Visibility = Visibility.Collapsed; - - BtnUndo.IsEnabled = false; - BtnUndo.Visibility = Visibility.Visible; inkCanvas.Strokes.Clear(); @@ -3057,40 +3049,98 @@ namespace Ink_Canvas private void BtnUndo_Click(object sender, RoutedEventArgs e) { - int whiteboardIndex = CurrentWhiteboardIndex; - if (currentMode == 0) + var item = timeMachine.Undo(); + if (item.CommitType == TimeMachineHistoryType.UserInput) { - whiteboardIndex = 0; + if (!item.IsReversed) + { + foreach (var strokes in item.CurrentStroke) + { + inkCanvas.Strokes.Add(strokes); + } + } + else + { + foreach (var strokes in item.CurrentStroke) + { + inkCanvas.Strokes.Remove(strokes); + } + } + } + else if (item.CommitType == TimeMachineHistoryType.ShapeRecognition) + { + if (item.IsReversed) + { + + foreach (var strokes in item.CurrentStroke) + { + inkCanvas.Strokes.Remove(strokes); + } + foreach (var strokes in item.ShapeRecognitionReplacedStroke) + { + inkCanvas.Strokes.Add(strokes); + } + } + else + { + foreach (var strokes in item.CurrentStroke) + { + inkCanvas.Strokes.Add(strokes); + } + foreach (var strokes in item.ShapeRecognitionReplacedStroke) + { + inkCanvas.Strokes.Remove(strokes); + } + } } - - StrokeCollection strokes = inkCanvas.Strokes.Clone(); - inkCanvas.Strokes = strokeCollections[whiteboardIndex].Clone(); - strokeCollections[whiteboardIndex] = strokes; - - BtnRedo.IsEnabled = true; - BtnRedo.Visibility = Visibility.Visible; - - BtnUndo.IsEnabled = false; - BtnUndo.Visibility = Visibility.Collapsed; } private void BtnRedo_Click(object sender, RoutedEventArgs e) { - int whiteboardIndex = CurrentWhiteboardIndex; - if (currentMode == 0) + var item = timeMachine.Redo(); + if (item.CommitType == TimeMachineHistoryType.UserInput) { - whiteboardIndex = 0; + if (!item.IsReversed) + { + foreach (var strokes in item.CurrentStroke) + { + inkCanvas.Strokes.Add(strokes); + } + } + else + { + foreach (var strokes in item.CurrentStroke) + { + inkCanvas.Strokes.Remove(strokes); + } + } } + else if (item.CommitType == TimeMachineHistoryType.ShapeRecognition) + { + if (item.IsReversed) + { - StrokeCollection strokes = inkCanvas.Strokes.Clone(); - inkCanvas.Strokes = strokeCollections[whiteboardIndex].Clone(); - strokeCollections[whiteboardIndex] = strokes; - - BtnUndo.IsEnabled = true; - BtnUndo.Visibility = Visibility.Visible; - - BtnRedo.IsEnabled = false; - BtnRedo.Visibility = Visibility.Collapsed; + foreach (var strokes in item.CurrentStroke) + { + inkCanvas.Strokes.Remove(strokes); + } + foreach (var strokes in item.ShapeRecognitionReplacedStroke) + { + inkCanvas.Strokes.Add(strokes); + } + } + else + { + foreach (var strokes in item.CurrentStroke) + { + inkCanvas.Strokes.Add(strokes); + } + foreach (var strokes in item.ShapeRecognitionReplacedStroke) + { + inkCanvas.Strokes.Remove(strokes); + } + } + } } private void Btn_IsEnabledChanged(object sender, DependencyPropertyChangedEventArgs e) @@ -5058,6 +5108,7 @@ namespace Ink_Canvas try { inkCanvas.Opacity = 1; + timeMachine.CommitStrokeUserInputHistory(new StrokeCollection{e.Stroke}); if (Settings.InkToShape.IsInkToShapeEnabled) { try @@ -5138,6 +5189,7 @@ namespace Ink_Canvas }; circles.Add(new Circle(result.Centroid, shape.Width / 2.0, stroke)); SetNewBackupOfStroke(); + timeMachine.CommitStrokeShapeHistory(result.InkDrawingNode.Strokes,new StrokeCollection{stroke}); inkCanvas.Strokes.Add(stroke); inkCanvas.Strokes.Remove(result.InkDrawingNode.Strokes); newStrokes = new StrokeCollection(); @@ -5221,8 +5273,17 @@ namespace Ink_Canvas { DrawingAttributes = inkCanvas.DefaultDrawingAttributes.Clone() }; + var _generateDashedLineEllipseStrokeCollection = + GenerateDashedLineEllipseStrokeCollection(iniP, endP, true, false); + + var resultStrokes = new StrokeCollection + { + _stroke, + _generateDashedLineEllipseStrokeCollection + }; inkCanvas.Strokes.Add(_stroke.Clone()); - inkCanvas.Strokes.Add(GenerateDashedLineEllipseStrokeCollection(iniP, endP, true, false)); + inkCanvas.Strokes.Add(_generateDashedLineEllipseStrokeCollection); + timeMachine.CommitStrokeShapeHistory(result.InkDrawingNode.Strokes,resultStrokes); return; } } @@ -5270,6 +5331,7 @@ namespace Ink_Canvas SetNewBackupOfStroke(); inkCanvas.Strokes.Add(stroke); inkCanvas.Strokes.Remove(result.InkDrawingNode.Strokes); + timeMachine.CommitStrokeShapeHistory(result.InkDrawingNode.Strokes,new StrokeCollection{stroke}); newStrokes = new StrokeCollection(); } } @@ -5338,6 +5400,7 @@ namespace Ink_Canvas SetNewBackupOfStroke(); inkCanvas.Strokes.Add(stroke); inkCanvas.Strokes.Remove(result.InkDrawingNode.Strokes); + timeMachine.CommitStrokeShapeHistory(result.InkDrawingNode.Strokes,new StrokeCollection{stroke}); newStrokes = new StrokeCollection(); } } @@ -5521,12 +5584,6 @@ namespace Ink_Canvas whiteboardIndex = 0; } strokeCollections[whiteboardIndex] = lastTouchDownStrokeCollection; - - BtnUndo.IsEnabled = true; - BtnUndo.Visibility = Visibility.Visible; - - BtnRedo.IsEnabled = false; - BtnRedo.Visibility = Visibility.Collapsed; } public double GetDistance(Point point1, Point point2) @@ -6321,7 +6378,7 @@ namespace Ink_Canvas { BtnPPTSlideShowEnd_Click(BtnPPTSlideShowEnd, null); } - + #endregion #region Save & Open From 28a957eed8dff539689dd74420d1553e540d8181 Mon Sep 17 00:00:00 2001 From: kengwang Date: Sun, 16 Apr 2023 00:08:52 +0800 Subject: [PATCH 02/21] =?UTF-8?q?[feat]=20=E6=96=B0=E5=A2=9E=E6=A9=A1?= =?UTF-8?q?=E7=9A=AE=E5=92=8C=E6=B8=85=E5=B1=8F=E7=9A=84=E6=92=A4=E9=94=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit [WIP] 仍存在不完美的情况 --- Ink Canvas/Helpers/TimeMachine.cs | 11 ++++- Ink Canvas/MainWindow.xaml | 7 +++- Ink Canvas/MainWindow.xaml.cs | 70 ++++++++++++++++++++++++------- 3 files changed, 70 insertions(+), 18 deletions(-) diff --git a/Ink Canvas/Helpers/TimeMachine.cs b/Ink Canvas/Helpers/TimeMachine.cs index 4823ad5..39aee00 100644 --- a/Ink Canvas/Helpers/TimeMachine.cs +++ b/Ink Canvas/Helpers/TimeMachine.cs @@ -41,7 +41,13 @@ namespace Ink_Canvas.Helpers public void CommitStrokeEraseHistory(StrokeCollection stroke) { - _currentStrokeHistory.Add(new TimeMachineHistory(stroke, TimeMachineHistoryType.UserInput, false)); + var col = new StrokeCollection(); + foreach (var stroke1 in stroke) + { + col.Add(stroke1); + } + + _currentStrokeHistory.Add(new TimeMachineHistory(col, TimeMachineHistoryType.Clear, true)); _currentIndex = _currentStrokeHistory.Count - 1; OnUndoStateChanged?.Invoke(true); OnRedoStateChanged?.Invoke(false); @@ -107,6 +113,7 @@ namespace Ink_Canvas.Helpers public enum TimeMachineHistoryType { UserInput, - ShapeRecognition + ShapeRecognition, + Clear } } \ No newline at end of file diff --git a/Ink Canvas/MainWindow.xaml b/Ink Canvas/MainWindow.xaml index 03a4178..b227b22 100644 --- a/Ink Canvas/MainWindow.xaml +++ b/Ink Canvas/MainWindow.xaml @@ -97,10 +97,13 @@ ManipulationDelta="Main_Grid_ManipulationDelta" ManipulationCompleted="Main_Grid_ManipulationCompleted" ManipulationInertiaStarting="inkCanvas_ManipulationInertiaStarting" - IsManipulationEnabled="True" EditingModeChanged="inkCanvas_EditingModeChanged" + IsManipulationEnabled="True" + EditingModeChanged="inkCanvas_EditingModeChanged" PreviewTouchDown="inkCanvas_PreviewTouchDown" PreviewTouchUp="inkCanvas_PreviewTouchUp" - MouseDown="inkCanvas_MouseDown" MouseMove="inkCanvas_MouseMove" MouseUp="inkCanvas_MouseUp" + MouseDown="inkCanvas_MouseDown" + MouseMove="inkCanvas_MouseMove" + MouseUp="inkCanvas_MouseUp" ManipulationStarting="inkCanvas_ManipulationStarting" SelectionChanged="inkCanvas_SelectionChanged" StrokeCollected="inkCanvas_StrokeCollected"> diff --git a/Ink Canvas/MainWindow.xaml.cs b/Ink Canvas/MainWindow.xaml.cs index c08084e..127539b 100644 --- a/Ink Canvas/MainWindow.xaml.cs +++ b/Ink Canvas/MainWindow.xaml.cs @@ -190,6 +190,10 @@ namespace Ink_Canvas } catch { } } + + private bool isPreviousErasing = false; + private StrokeCollection previousStrokes = new StrokeCollection(); + private void inkCanvas_EditingModeChanged(object sender, RoutedEventArgs e) { var inkCanvas1 = sender as InkCanvas; @@ -210,6 +214,21 @@ namespace Ink_Canvas inkCanvas1.ForceCursor = false; } + if (inkCanvas1.EditingMode == InkCanvasEditingMode.EraseByPoint || + inkCanvas1.EditingMode == InkCanvasEditingMode.EraseByStroke) + { + isPreviousErasing = true; + previousStrokes.Clear(); + foreach (var inkCanvas1Stroke in inkCanvas1.Strokes) + { + previousStrokes.Add(inkCanvas1Stroke); + } + } + else if (isPreviousErasing) + { + timeMachine.CommitStrokeEraseHistory(previousStrokes); + isPreviousErasing = false; + } if (inkCanvas1.EditingMode == InkCanvasEditingMode.Ink) forcePointEraser = !forcePointEraser; } @@ -942,7 +961,7 @@ namespace Ink_Canvas } - inkCanvas.Strokes.Clear(); + ClearStrokes(); inkCanvas.Children.Clear(); CancelSingleFingerDragMode(); @@ -992,7 +1011,7 @@ namespace Ink_Canvas GridBackgroundCover.Visibility = Visibility.Collapsed; SaveStrokes(true); - inkCanvas.Strokes.Clear(); + ClearStrokes(); RestoreStrokes(); if (BtnSwitchTheme.Content.ToString() == "浅色") @@ -1029,7 +1048,7 @@ namespace Ink_Canvas GridBackgroundCover.Visibility = Visibility.Collapsed; SaveStrokes(); - inkCanvas.Strokes.Clear(); + ClearStrokes(); RestoreStrokes(true); if (BtnSwitchTheme.Content.ToString() == "浅色") @@ -1063,7 +1082,7 @@ namespace Ink_Canvas GridBackgroundCover.Visibility = Visibility.Visible; SaveStrokes(true); - inkCanvas.Strokes.Clear(); + ClearStrokes(); RestoreStrokes(); BtnSwitch.Content = "屏幕"; @@ -2195,7 +2214,7 @@ namespace Ink_Canvas GridBackgroundCover.Visibility = Visibility.Collapsed; //SaveStrokes(); - inkCanvas.Strokes.Clear(); + ClearStrokes(); if (BtnSwitchTheme.Content.ToString() == "浅色") { @@ -2216,7 +2235,7 @@ namespace Ink_Canvas // GridBackgroundCover.Visibility = Visibility.Hidden; //} - inkCanvas.Strokes.Clear(); + ClearStrokes(); BorderFloatingBarMainControls.Visibility = Visibility.Visible; BorderPenColorRed_MouseUp(BorderPenColorRed, null); @@ -2334,7 +2353,7 @@ namespace Ink_Canvas GridBackgroundCover.Visibility = Visibility.Collapsed; //SaveStrokes(); - inkCanvas.Strokes.Clear(); + ClearStrokes(); //RestoreStrokes(true); if (BtnSwitchTheme.Content.ToString() == "浅色") @@ -2353,7 +2372,7 @@ namespace Ink_Canvas //} - inkCanvas.Strokes.Clear(); + ClearStrokes(); if (Main_Grid.Background != Brushes.Transparent) { @@ -2386,7 +2405,7 @@ namespace Ink_Canvas SaveScreenShot(true, Wn.Presentation.Name + "/" + Wn.View.CurrentShowPosition); _isPptClickingBtnTurned = false; - inkCanvas.Strokes.Clear(); + ClearStrokes(); try { @@ -3056,7 +3075,8 @@ namespace Ink_Canvas { foreach (var strokes in item.CurrentStroke) { - inkCanvas.Strokes.Add(strokes); + if (!inkCanvas.Strokes.Contains(strokes)) + inkCanvas.Strokes.Add(strokes); } } else @@ -3093,6 +3113,14 @@ namespace Ink_Canvas } } } + else if(item.CommitType == TimeMachineHistoryType.Clear) + { + inkCanvas.Strokes.Clear(); + foreach (var stroke in item.CurrentStroke) + { + inkCanvas.Strokes.Add(stroke); + } + } } private void BtnRedo_Click(object sender, RoutedEventArgs e) @@ -3141,6 +3169,14 @@ namespace Ink_Canvas } } } + else if(item.CommitType == TimeMachineHistoryType.Clear) + { + inkCanvas.Strokes.Clear(); + foreach (var stroke in item.CurrentStroke) + { + inkCanvas.Strokes.Add(stroke); + } + } } private void Btn_IsEnabledChanged(object sender, DependencyPropertyChangedEventArgs e) @@ -4948,6 +4984,12 @@ namespace Ink_Canvas } } + private void ClearStrokes() + { + timeMachine.CommitStrokeEraseHistory(inkCanvas.Strokes); + inkCanvas.Strokes.Clear(); + } + private void RestoreStrokes(bool isBackupMain = false) { try @@ -4978,7 +5020,7 @@ namespace Ink_Canvas SaveStrokes(); - inkCanvas.Strokes.Clear(); + ClearStrokes(); CurrentWhiteboardIndex--; RestoreStrokes(); @@ -4998,7 +5040,7 @@ namespace Ink_Canvas } - inkCanvas.Strokes.Clear(); + ClearStrokes(); CurrentWhiteboardIndex++; RestoreStrokes(); @@ -5012,7 +5054,7 @@ namespace Ink_Canvas if (Settings.Automation.IsAutoSaveStrokesAtClear && inkCanvas.Strokes.Count > Settings.Automation.MinimumAutomationStrokeNumber) SaveScreenShot(true); SaveStrokes(); - inkCanvas.Strokes.Clear(); + ClearStrokes(); WhiteboardTotalCount++; CurrentWhiteboardIndex++; @@ -5034,7 +5076,7 @@ namespace Ink_Canvas private void BtnWhiteBoardDelete_Click(object sender, RoutedEventArgs e) { - inkCanvas.Strokes.Clear(); + ClearStrokes(); if (CurrentWhiteboardIndex != WhiteboardTotalCount) { From 27b75ca99b716caa989676000a69abdc5313d317 Mon Sep 17 00:00:00 2001 From: kengwang Date: Sun, 16 Apr 2023 00:14:35 +0800 Subject: [PATCH 03/21] =?UTF-8?q?[fix]=20=E4=BF=AE=E5=A4=8D=20Redo=20?= =?UTF-8?q?=E6=97=B6=E7=9A=84=E6=B8=85=E5=B1=8F=E6=97=A0=E6=95=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Ink Canvas/MainWindow.xaml.cs | 4 ---- 1 file changed, 4 deletions(-) diff --git a/Ink Canvas/MainWindow.xaml.cs b/Ink Canvas/MainWindow.xaml.cs index 127539b..9f88ef7 100644 --- a/Ink Canvas/MainWindow.xaml.cs +++ b/Ink Canvas/MainWindow.xaml.cs @@ -3172,10 +3172,6 @@ namespace Ink_Canvas else if(item.CommitType == TimeMachineHistoryType.Clear) { inkCanvas.Strokes.Clear(); - foreach (var stroke in item.CurrentStroke) - { - inkCanvas.Strokes.Add(stroke); - } } } From 349eaf636ad79b6cfea50a969c4810e1728f6e2d Mon Sep 17 00:00:00 2001 From: kengwang Date: Sun, 16 Apr 2023 12:51:14 +0800 Subject: [PATCH 04/21] =?UTF-8?q?[fix]=20=E4=BF=AE=E5=A4=8D=E4=BA=86?= =?UTF-8?q?=E5=8F=AF=E8=83=BD=E5=AF=BC=E8=87=B4=E5=B4=A9=E6=BA=83=E7=9A=84?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Ink Canvas/MainWindow.xaml.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Ink Canvas/MainWindow.xaml.cs b/Ink Canvas/MainWindow.xaml.cs index 9f88ef7..fa7e0f6 100644 --- a/Ink Canvas/MainWindow.xaml.cs +++ b/Ink Canvas/MainWindow.xaml.cs @@ -3105,7 +3105,8 @@ namespace Ink_Canvas { foreach (var strokes in item.CurrentStroke) { - inkCanvas.Strokes.Add(strokes); + if (!inkCanvas.Strokes.Contains(strokes)) + inkCanvas.Strokes.Add(strokes); } foreach (var strokes in item.ShapeRecognitionReplacedStroke) { From d4cb88aab3045a065bc3a81a0b33105a5ae7bede Mon Sep 17 00:00:00 2001 From: kengwang Date: Sun, 16 Apr 2023 12:57:29 +0800 Subject: [PATCH 05/21] =?UTF-8?q?[chore]=20=E6=94=AF=E6=8C=81=E6=92=A4?= =?UTF-8?q?=E9=94=80=E5=88=A0=E9=99=A4=E9=80=89=E4=B8=AD=E5=A2=A8=E8=BF=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Ink Canvas/MainWindow.xaml.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/Ink Canvas/MainWindow.xaml.cs b/Ink Canvas/MainWindow.xaml.cs index fa7e0f6..a91872a 100644 --- a/Ink Canvas/MainWindow.xaml.cs +++ b/Ink Canvas/MainWindow.xaml.cs @@ -6061,6 +6061,7 @@ namespace Ink_Canvas if (sender != lastBorderMouseDownObject) return; if (inkCanvas.GetSelectedStrokes().Count > 0) { + timeMachine.CommitStrokeEraseHistory(inkCanvas.Strokes); inkCanvas.Strokes.Remove(inkCanvas.GetSelectedStrokes()); GridInkCanvasSelectionCover.Visibility = Visibility.Collapsed; } From 500fd2835981fb99b53dbf02154bc11e66bec921 Mon Sep 17 00:00:00 2001 From: Raspberry-Monster Date: Sun, 16 Apr 2023 15:27:07 +0800 Subject: [PATCH 06/21] =?UTF-8?q?[WIP]=20=E6=B7=BB=E5=8A=A0=E5=AF=B9?= =?UTF-8?q?=E5=88=87=E6=8D=A2=E9=A1=B5=E9=9D=A2=E4=BF=9D=E5=AD=98=E8=AE=B0?= =?UTF-8?q?=E5=BD=95=E7=9A=84=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Ink Canvas/Helpers/TimeMachine.cs | 32 ++++++-- Ink Canvas/MainWindow.xaml.cs | 132 +++++++++++++++++++----------- 2 files changed, 111 insertions(+), 53 deletions(-) diff --git a/Ink Canvas/Helpers/TimeMachine.cs b/Ink Canvas/Helpers/TimeMachine.cs index 39aee00..22e004d 100644 --- a/Ink Canvas/Helpers/TimeMachine.cs +++ b/Ink Canvas/Helpers/TimeMachine.cs @@ -1,7 +1,5 @@ using System.Collections.Generic; using System.Linq; -using System.Windows.Documents; -using System.Windows.Forms.VisualStyles; using System.Windows.Ink; namespace Ink_Canvas.Helpers @@ -22,6 +20,10 @@ namespace Ink_Canvas.Helpers public void CommitStrokeUserInputHistory(StrokeCollection stroke) { + if (_currentIndex + 1 < _currentStrokeHistory.Count) + { + _currentStrokeHistory.RemoveRange(_currentIndex +1 , (_currentStrokeHistory.Count - 1) - _currentIndex); + } _currentStrokeHistory.Add(new TimeMachineHistory(stroke, TimeMachineHistoryType.UserInput, false)); _currentIndex = _currentStrokeHistory.Count - 1; OnUndoStateChanged?.Invoke(true); @@ -30,6 +32,10 @@ namespace Ink_Canvas.Helpers public void CommitStrokeShapeHistory(StrokeCollection strokeToBeReplaced, StrokeCollection generatedStroke) { + if (_currentIndex + 1 < _currentStrokeHistory.Count) + { + _currentStrokeHistory.RemoveRange(_currentIndex +1 , (_currentStrokeHistory.Count - 1) - _currentIndex); + } _currentStrokeHistory.Add(new TimeMachineHistory(generatedStroke, TimeMachineHistoryType.ShapeRecognition, false, @@ -41,6 +47,10 @@ namespace Ink_Canvas.Helpers public void CommitStrokeEraseHistory(StrokeCollection stroke) { + if (_currentIndex + 1 < _currentStrokeHistory.Count) + { + _currentStrokeHistory.RemoveRange(_currentIndex +1 , (_currentStrokeHistory.Count - 1) - _currentIndex); + } var col = new StrokeCollection(); foreach (var stroke1 in stroke) { @@ -78,13 +88,23 @@ namespace Ink_Canvas.Helpers if (_currentIndex != -1) OnRedoStateChanged?.Invoke(_currentStrokeHistory.Count - _currentIndex - 1 > 0); return item; } - public List ExportTimeMachineHistory() + public TimeMachineHistory[] ExportTimeMachineHistory() { - throw new System.Exception(); + if (_currentIndex + 1 < _currentStrokeHistory.Count) + { + _currentStrokeHistory.RemoveRange(_currentIndex +1 , (_currentStrokeHistory.Count - 1) - _currentIndex); + } + return _currentStrokeHistory.ToArray(); } - public bool ImportTimeMachineHistory() + + public bool ImportTimeMachineHistory(TimeMachineHistory[] sourceHistory) { - throw new System.Exception(); + _currentStrokeHistory.Clear(); + _currentStrokeHistory.AddRange(sourceHistory); + _currentIndex = _currentStrokeHistory.Count - 1; + OnUndoStateChanged?.Invoke(_currentIndex > -1); + OnRedoStateChanged?.Invoke(_currentStrokeHistory.Count - _currentIndex - 1 > 0); + return true; } } diff --git a/Ink Canvas/MainWindow.xaml.cs b/Ink Canvas/MainWindow.xaml.cs index 9f88ef7..ffa1e69 100644 --- a/Ink Canvas/MainWindow.xaml.cs +++ b/Ink Canvas/MainWindow.xaml.cs @@ -14,6 +14,7 @@ using System.IO; using System.Linq; using System.Net; using System.Reflection; +using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using System.Threading; using System.Threading.Tasks; @@ -961,7 +962,7 @@ namespace Ink_Canvas } - ClearStrokes(); + ClearStrokes(false); inkCanvas.Children.Clear(); CancelSingleFingerDragMode(); @@ -1011,7 +1012,7 @@ namespace Ink_Canvas GridBackgroundCover.Visibility = Visibility.Collapsed; SaveStrokes(true); - ClearStrokes(); + ClearStrokes(true); RestoreStrokes(); if (BtnSwitchTheme.Content.ToString() == "浅色") @@ -1048,7 +1049,7 @@ namespace Ink_Canvas GridBackgroundCover.Visibility = Visibility.Collapsed; SaveStrokes(); - ClearStrokes(); + ClearStrokes(true); RestoreStrokes(true); if (BtnSwitchTheme.Content.ToString() == "浅色") @@ -1082,7 +1083,7 @@ namespace Ink_Canvas GridBackgroundCover.Visibility = Visibility.Visible; SaveStrokes(true); - ClearStrokes(); + ClearStrokes(true); RestoreStrokes(); BtnSwitch.Content = "屏幕"; @@ -2214,7 +2215,7 @@ namespace Ink_Canvas GridBackgroundCover.Visibility = Visibility.Collapsed; //SaveStrokes(); - ClearStrokes(); + ClearStrokes(true); if (BtnSwitchTheme.Content.ToString() == "浅色") { @@ -2235,7 +2236,7 @@ namespace Ink_Canvas // GridBackgroundCover.Visibility = Visibility.Hidden; //} - ClearStrokes(); + ClearStrokes(true); BorderFloatingBarMainControls.Visibility = Visibility.Visible; BorderPenColorRed_MouseUp(BorderPenColorRed, null); @@ -2353,7 +2354,7 @@ namespace Ink_Canvas GridBackgroundCover.Visibility = Visibility.Collapsed; //SaveStrokes(); - ClearStrokes(); + ClearStrokes(true); //RestoreStrokes(true); if (BtnSwitchTheme.Content.ToString() == "浅色") @@ -2372,7 +2373,7 @@ namespace Ink_Canvas //} - ClearStrokes(); + ClearStrokes(true); if (Main_Grid.Background != Brushes.Transparent) { @@ -2405,7 +2406,7 @@ namespace Ink_Canvas SaveScreenShot(true, Wn.Presentation.Name + "/" + Wn.View.CurrentShowPosition); _isPptClickingBtnTurned = false; - ClearStrokes(); + ClearStrokes(true); try { @@ -3083,7 +3084,8 @@ namespace Ink_Canvas { foreach (var strokes in item.CurrentStroke) { - inkCanvas.Strokes.Remove(strokes); + if (inkCanvas.Strokes.Contains(strokes)) + inkCanvas.Strokes.Remove(strokes); } } } @@ -3094,22 +3096,26 @@ namespace Ink_Canvas foreach (var strokes in item.CurrentStroke) { - inkCanvas.Strokes.Remove(strokes); + if (inkCanvas.Strokes.Contains(strokes)) + inkCanvas.Strokes.Remove(strokes); } foreach (var strokes in item.ShapeRecognitionReplacedStroke) { - inkCanvas.Strokes.Add(strokes); + if (!inkCanvas.Strokes.Contains(strokes)) + inkCanvas.Strokes.Add(strokes); } } else { foreach (var strokes in item.CurrentStroke) { - inkCanvas.Strokes.Add(strokes); + if (!inkCanvas.Strokes.Contains(strokes)) + inkCanvas.Strokes.Add(strokes); } foreach (var strokes in item.ShapeRecognitionReplacedStroke) { - inkCanvas.Strokes.Remove(strokes); + if (inkCanvas.Strokes.Contains(strokes)) + inkCanvas.Strokes.Remove(strokes); } } } @@ -3132,14 +3138,16 @@ namespace Ink_Canvas { foreach (var strokes in item.CurrentStroke) { - inkCanvas.Strokes.Add(strokes); + if (!inkCanvas.Strokes.Contains(strokes)) + inkCanvas.Strokes.Add(strokes); } } else { foreach (var strokes in item.CurrentStroke) { - inkCanvas.Strokes.Remove(strokes); + if (inkCanvas.Strokes.Contains(strokes)) + inkCanvas.Strokes.Remove(strokes); } } } @@ -3150,22 +3158,26 @@ namespace Ink_Canvas foreach (var strokes in item.CurrentStroke) { - inkCanvas.Strokes.Remove(strokes); + if (inkCanvas.Strokes.Contains(strokes)) + inkCanvas.Strokes.Remove(strokes); } foreach (var strokes in item.ShapeRecognitionReplacedStroke) { - inkCanvas.Strokes.Add(strokes); + if (!inkCanvas.Strokes.Contains(strokes)) + inkCanvas.Strokes.Add(strokes); } } else { foreach (var strokes in item.CurrentStroke) { - inkCanvas.Strokes.Add(strokes); + if (!inkCanvas.Strokes.Contains(strokes)) + inkCanvas.Strokes.Add(strokes); } foreach (var strokes in item.ShapeRecognitionReplacedStroke) { - inkCanvas.Strokes.Remove(strokes); + if (inkCanvas.Strokes.Contains(strokes)) + inkCanvas.Strokes.Remove(strokes); } } } @@ -4963,26 +4975,27 @@ namespace Ink_Canvas int CurrentWhiteboardIndex = 1; int WhiteboardTotalCount = 1; - MemoryStream[] WhiteboardStrokesStreams = new MemoryStream[101]; //最多99页,0用来存储非白板时的墨迹以便还原 + TimeMachineHistory[][] TimeMachineHistories = new TimeMachineHistory[101][]; //最多99页,0用来存储非白板时的墨迹以便还原 private void SaveStrokes(bool isBackupMain = false) { - MemoryStream ms = new MemoryStream(); - inkCanvas.Strokes.Save(ms); - ms.Position = 0; if (isBackupMain) { - WhiteboardStrokesStreams[0] = ms; + var timeMachineHistory = timeMachine.ExportTimeMachineHistory(); + timeMachine.ClearStrokeHistory(); + TimeMachineHistories[0] = timeMachineHistory; } else { - WhiteboardStrokesStreams[CurrentWhiteboardIndex] = ms; + var timeMachineHistory = timeMachine.ExportTimeMachineHistory(); + timeMachine.ClearStrokeHistory(); + TimeMachineHistories[CurrentWhiteboardIndex] = timeMachineHistory; } } - private void ClearStrokes() + private void ClearStrokes(bool IsErasedByCode) { - timeMachine.CommitStrokeEraseHistory(inkCanvas.Strokes); + if (!IsErasedByCode) timeMachine.CommitStrokeEraseHistory(inkCanvas.Strokes); inkCanvas.Strokes.Clear(); } @@ -4990,20 +5003,53 @@ namespace Ink_Canvas { try { - if (WhiteboardStrokesStreams[CurrentWhiteboardIndex] == null) return; //防止白板打开后不居中 + if (TimeMachineHistories[CurrentWhiteboardIndex] == null) return; //防止白板打开后不居中 if (isBackupMain) { - if (WhiteboardStrokesStreams[0].Length > 0) + foreach (var item in TimeMachineHistories[0]) { - inkCanvas.Strokes = new System.Windows.Ink.StrokeCollection(WhiteboardStrokesStreams[0]); + if (!item.IsReversed) + { + switch (item.CommitType) + { + case TimeMachineHistoryType.UserInput: + inkCanvas.Strokes.Add(item.CurrentStroke); + break; + case TimeMachineHistoryType.ShapeRecognition: + inkCanvas.Strokes.Remove(item.ShapeRecognitionReplacedStroke); + inkCanvas.Strokes.Add(item.CurrentStroke); + break; + case TimeMachineHistoryType.Clear: + inkCanvas.Strokes.Clear(); + break; + } + } + } + timeMachine.ImportTimeMachineHistory(TimeMachineHistories[0]); } else { - if (WhiteboardStrokesStreams[CurrentWhiteboardIndex].Length > 0) + foreach (var item in TimeMachineHistories[CurrentWhiteboardIndex]) { - inkCanvas.Strokes = new System.Windows.Ink.StrokeCollection(WhiteboardStrokesStreams[CurrentWhiteboardIndex]); + if (!item.IsReversed) + { + switch (item.CommitType) + { + case TimeMachineHistoryType.UserInput: + inkCanvas.Strokes.Add(item.CurrentStroke); + break; + case TimeMachineHistoryType.ShapeRecognition: + inkCanvas.Strokes.Remove(item.ShapeRecognitionReplacedStroke); + inkCanvas.Strokes.Add(item.CurrentStroke); + break; + case TimeMachineHistoryType.Clear: + inkCanvas.Strokes.Clear(); + break; + } + } } + timeMachine.ImportTimeMachineHistory(TimeMachineHistories[CurrentWhiteboardIndex]); } AdjustStrokeColor(); } @@ -5016,7 +5062,7 @@ namespace Ink_Canvas SaveStrokes(); - ClearStrokes(); + ClearStrokes(true); CurrentWhiteboardIndex--; RestoreStrokes(); @@ -5036,7 +5082,7 @@ namespace Ink_Canvas } - ClearStrokes(); + ClearStrokes(true); CurrentWhiteboardIndex++; RestoreStrokes(); @@ -5050,7 +5096,7 @@ namespace Ink_Canvas if (Settings.Automation.IsAutoSaveStrokesAtClear && inkCanvas.Strokes.Count > Settings.Automation.MinimumAutomationStrokeNumber) SaveScreenShot(true); SaveStrokes(); - ClearStrokes(); + ClearStrokes(true); WhiteboardTotalCount++; CurrentWhiteboardIndex++; @@ -5059,12 +5105,10 @@ namespace Ink_Canvas { for (int i = WhiteboardTotalCount; i > CurrentWhiteboardIndex; i--) { - WhiteboardStrokesStreams[i] = WhiteboardStrokesStreams[i - 1]; + TimeMachineHistories[i] = TimeMachineHistories[i - 1]; } } - WhiteboardStrokesStreams[CurrentWhiteboardIndex] = new MemoryStream(); - UpdateIndexInfoDisplay(); if (WhiteboardTotalCount >= 99) BtnWhiteBoardAdd.IsEnabled = false; @@ -5072,13 +5116,13 @@ namespace Ink_Canvas private void BtnWhiteBoardDelete_Click(object sender, RoutedEventArgs e) { - ClearStrokes(); + ClearStrokes(true); if (CurrentWhiteboardIndex != WhiteboardTotalCount) { for (int i = CurrentWhiteboardIndex; i <= WhiteboardTotalCount; i++) { - WhiteboardStrokesStreams[i] = WhiteboardStrokesStreams[i + 1]; + TimeMachineHistories[i] = TimeMachineHistories[i + 1]; } } else @@ -5097,12 +5141,6 @@ namespace Ink_Canvas private void UpdateIndexInfoDisplay() { - BtnUndo.IsEnabled = false; - BtnUndo.Visibility = Visibility.Visible; - - BtnRedo.IsEnabled = false; - BtnRedo.Visibility = Visibility.Collapsed; - TextBlockWhiteBoardIndexInfo.Text = string.Format("{0} / {1}", CurrentWhiteboardIndex, WhiteboardTotalCount); if (CurrentWhiteboardIndex == 1) From c0b33140ab05377d0d47ba3aa147b2af158e299d Mon Sep 17 00:00:00 2001 From: Raspberry-Monster Date: Wed, 19 Apr 2023 22:34:49 +0800 Subject: [PATCH 07/21] =?UTF-8?q?[Typo/TimeMachine]=20=E4=BF=AE=E6=94=B9?= =?UTF-8?q?=E9=83=A8=E5=88=86=E5=8F=98=E9=87=8F=E5=91=BD=E5=90=8D=EF=BC=8C?= =?UTF-8?q?=E6=94=AF=E6=8C=81=E6=92=A4=E5=9B=9E=E6=93=A6=E9=99=A4=E8=A1=8C?= =?UTF-8?q?=E4=B8=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Ink Canvas/Helpers/TimeMachine.cs | 28 ++++----- Ink Canvas/MainWindow.xaml | 3 +- Ink Canvas/MainWindow.xaml.cs | 94 ++++++++++++++++++------------- 3 files changed, 69 insertions(+), 56 deletions(-) diff --git a/Ink Canvas/Helpers/TimeMachine.cs b/Ink Canvas/Helpers/TimeMachine.cs index 22e004d..5bd726d 100644 --- a/Ink Canvas/Helpers/TimeMachine.cs +++ b/Ink Canvas/Helpers/TimeMachine.cs @@ -51,13 +51,7 @@ namespace Ink_Canvas.Helpers { _currentStrokeHistory.RemoveRange(_currentIndex +1 , (_currentStrokeHistory.Count - 1) - _currentIndex); } - var col = new StrokeCollection(); - foreach (var stroke1 in stroke) - { - col.Add(stroke1); - } - - _currentStrokeHistory.Add(new TimeMachineHistory(col, TimeMachineHistoryType.Clear, true)); + _currentStrokeHistory.Add(new TimeMachineHistory(stroke, TimeMachineHistoryType.Clear, true)); _currentIndex = _currentStrokeHistory.Count - 1; OnUndoStateChanged?.Invoke(true); OnRedoStateChanged?.Invoke(false); @@ -73,7 +67,7 @@ namespace Ink_Canvas.Helpers public TimeMachineHistory Undo() { var item = _currentStrokeHistory[_currentIndex]; - item.IsReversed = !item.IsReversed; + item.StrokeHasBeenCleared = !item.StrokeHasBeenCleared; _currentIndex--; OnUndoStateChanged?.Invoke(_currentIndex > -1); OnRedoStateChanged?.Invoke(_currentStrokeHistory.Count - _currentIndex - 1 > 0); @@ -83,7 +77,7 @@ namespace Ink_Canvas.Helpers public TimeMachineHistory Redo() { var item = _currentStrokeHistory[++_currentIndex]; - item.IsReversed = !item.IsReversed; + item.StrokeHasBeenCleared = !item.StrokeHasBeenCleared; OnUndoStateChanged?.Invoke(_currentIndex > -1); if (_currentIndex != -1) OnRedoStateChanged?.Invoke(_currentStrokeHistory.Count - _currentIndex - 1 > 0); return item; @@ -111,22 +105,22 @@ namespace Ink_Canvas.Helpers public class TimeMachineHistory { public TimeMachineHistoryType CommitType; - public bool IsReversed; + public bool StrokeHasBeenCleared; public StrokeCollection CurrentStroke; - public StrokeCollection ShapeRecognitionReplacedStroke; - public TimeMachineHistory(StrokeCollection currentStroke, TimeMachineHistoryType commitType, bool isReversed) + public StrokeCollection ReplacedStroke; + public TimeMachineHistory(StrokeCollection currentStroke, TimeMachineHistoryType commitType, bool strokeHasBeenCleared) { CommitType = commitType; CurrentStroke = currentStroke; - IsReversed = isReversed; - ShapeRecognitionReplacedStroke = null; + StrokeHasBeenCleared = strokeHasBeenCleared; + ReplacedStroke = null; } - public TimeMachineHistory(StrokeCollection currentStroke, TimeMachineHistoryType commitType, bool isReversed , StrokeCollection shapeRecognitionReplacedStroke) + public TimeMachineHistory(StrokeCollection currentStroke, TimeMachineHistoryType commitType, bool strokeHasBeenCleared , StrokeCollection replacedStroke) { CommitType = commitType; CurrentStroke = currentStroke; - IsReversed = isReversed; - ShapeRecognitionReplacedStroke = shapeRecognitionReplacedStroke; + StrokeHasBeenCleared = strokeHasBeenCleared; + ReplacedStroke = replacedStroke; } } diff --git a/Ink Canvas/MainWindow.xaml b/Ink Canvas/MainWindow.xaml index b227b22..16dc481 100644 --- a/Ink Canvas/MainWindow.xaml +++ b/Ink Canvas/MainWindow.xaml @@ -106,7 +106,8 @@ MouseUp="inkCanvas_MouseUp" ManipulationStarting="inkCanvas_ManipulationStarting" SelectionChanged="inkCanvas_SelectionChanged" - StrokeCollected="inkCanvas_StrokeCollected"> + StrokeCollected="inkCanvas_StrokeCollected" + StrokeErasing="InkCanvas_OnStrokeErasing"> + @@ -1133,18 +1134,14 @@ - - - - - - + + From 2f617e2d83aa8735297201541fc31bbd6842caff Mon Sep 17 00:00:00 2001 From: WXRIW Date: Mon, 8 May 2023 19:58:14 +0800 Subject: [PATCH 21/21] Code cleanup --- Ink Canvas/Helpers/TimeMachine.cs | 3 ++ Ink Canvas/MainWindow.xaml.cs | 57 ++++++++++++++++--------------- Ink Canvas/Settings.cs | 8 ++--- 3 files changed, 37 insertions(+), 31 deletions(-) diff --git a/Ink Canvas/Helpers/TimeMachine.cs b/Ink Canvas/Helpers/TimeMachine.cs index 4a53d0b..3031c1f 100644 --- a/Ink Canvas/Helpers/TimeMachine.cs +++ b/Ink Canvas/Helpers/TimeMachine.cs @@ -41,6 +41,7 @@ namespace Ink_Canvas.Helpers _currentIndex = _currentStrokeHistory.Count - 1; NotifyUndoRedoState(); } + public void CommitStrokeRotateHistory(StrokeCollection strokeToBeReplaced, StrokeCollection generatedStroke) { if (_currentIndex + 1 < _currentStrokeHistory.Count) @@ -72,6 +73,7 @@ namespace Ink_Canvas.Helpers _currentIndex = -1; NotifyUndoRedoState(); } + public TimeMachineHistory Undo() { var item = _currentStrokeHistory[_currentIndex]; @@ -89,6 +91,7 @@ namespace Ink_Canvas.Helpers NotifyUndoRedoState(); return item; } + public TimeMachineHistory[] ExportTimeMachineHistory() { if (_currentIndex + 1 < _currentStrokeHistory.Count) diff --git a/Ink Canvas/MainWindow.xaml.cs b/Ink Canvas/MainWindow.xaml.cs index e1e4112..fa80f7c 100644 --- a/Ink Canvas/MainWindow.xaml.cs +++ b/Ink Canvas/MainWindow.xaml.cs @@ -326,12 +326,14 @@ namespace Ink_Canvas ClearingCanvas, Rotate } + private CommitReason _currentCommitType = CommitReason.UserInput; private bool IsEraseByPoint => inkCanvas.EditingMode == InkCanvasEditingMode.EraseByPoint; private StrokeCollection ReplacedStroke; private StrokeCollection AddedStroke; private StrokeCollection CuboidStrokeCollection; private TimeMachine timeMachine = new TimeMachine(); + private void TimeMachine_OnUndoStateChanged(bool status) { var result = status ? Visibility.Visible : Visibility.Collapsed; @@ -345,6 +347,7 @@ namespace Ink_Canvas BtnRedo.Visibility = result; BtnRedo.IsEnabled = status; } + private void StrokesOnStrokesChanged(object sender, StrokeCollectionChangedEventArgs e) { if (_currentCommitType == CommitReason.CodeInput || _currentCommitType == CommitReason.ShapeDrawing) return; @@ -856,7 +859,7 @@ namespace Ink_Canvas { ToggleSwitchAutoSaveStrokesInPowerPoint.IsOn = false; } - + if (Settings.PowerPointSettings.IsNotifyPreviousPage) { ToggleSwitchNotifyPreviousPage.IsOn = true; @@ -865,7 +868,7 @@ namespace Ink_Canvas { ToggleSwitchNotifyPreviousPage.IsOn = false; } - + if (Settings.PowerPointSettings.IsNotifyHiddenPage) { ToggleSwitchNotifyHiddenPage.IsOn = true; @@ -874,7 +877,7 @@ namespace Ink_Canvas { ToggleSwitchNotifyHiddenPage.IsOn = false; } - + if (Settings.PowerPointSettings.IsNoClearStrokeOnSelectWhenInPowerPoint) { ToggleSwitchNoStrokeClearInPowerPoint.IsOn = true; @@ -883,7 +886,7 @@ namespace Ink_Canvas { ToggleSwitchNoStrokeClearInPowerPoint.IsOn = false; } - + if (Settings.PowerPointSettings.IsShowStrokeOnSelectInPowerPoint) { ToggleSwitchShowStrokeOnSelectInPowerPoint.IsOn = true; @@ -920,7 +923,7 @@ namespace Ink_Canvas { BtnSwitchTheme.Content = "深色"; } - BtnSwitchTheme_Click(null , null); + BtnSwitchTheme_Click(null, null); if (Settings.PowerPointSettings.IsAutoSaveScreenShotInPowerPoint) { ToggleSwitchAutoSaveScreenShotInPowerPoint.IsOn = true; @@ -937,7 +940,7 @@ namespace Ink_Canvas if (Settings.Advanced != null) { - TouchMultiplierSlider.Value=Settings.Advanced.TouchMultiplier; + TouchMultiplierSlider.Value = Settings.Advanced.TouchMultiplier; if (Settings.Advanced.IsLogEnabled) { ToggleSwitchIsLogEnabled.IsOn = true; @@ -1368,8 +1371,8 @@ namespace Ink_Canvas } else { - - + + // Auto-clear Strokes // 很烦, 要重新来, 要等待截图完成再清理笔记 if (BtnPPTSlideShowEnd.Visibility != Visibility.Visible) @@ -1410,8 +1413,8 @@ namespace Ink_Canvas BtnClear_Click(BtnClear, null); } } - - + + if (Settings.PowerPointSettings.IsShowStrokeOnSelectInPowerPoint) { inkCanvas.Visibility = Visibility.Visible; @@ -1430,10 +1433,10 @@ namespace Ink_Canvas } - + Main_Grid.Background = Brushes.Transparent; - + GridBackgroundCoverHolder.Visibility = Visibility.Collapsed; if (currentMode != 0) { @@ -2175,7 +2178,7 @@ namespace Ink_Canvas { if (int.TryParse(File.ReadAllText(folderPath + "/position"), out var page)) { - if (page <= 0 ) return; + if (page <= 0) return; new YesOrNoNotificationWindow($"上次播放到了第 {page} 页, 是否立即跳转", () => { if (pptApplication.SlideShowWindows.Count >= 1) @@ -3001,29 +3004,29 @@ namespace Ink_Canvas Settings.PowerPointSettings.IsAutoSaveStrokesInPowerPoint = ToggleSwitchAutoSaveStrokesInPowerPoint.IsOn; SaveSettingsToFile(); } - + private void ToggleSwitchNotifyPreviousPage_Toggled(object sender, RoutedEventArgs e) { if (!isLoaded) return; Settings.PowerPointSettings.IsNotifyPreviousPage = ToggleSwitchNotifyPreviousPage.IsOn; SaveSettingsToFile(); - } - + } + private void ToggleSwitchNotifyHiddenPage_Toggled(object sender, RoutedEventArgs e) { if (!isLoaded) return; Settings.PowerPointSettings.IsNotifyHiddenPage = ToggleSwitchNotifyHiddenPage.IsOn; SaveSettingsToFile(); } - + private void ToggleSwitchNoStrokeClearInPowerPoint_Toggled(object sender, RoutedEventArgs e) { if (!isLoaded) return; Settings.PowerPointSettings.IsNoClearStrokeOnSelectWhenInPowerPoint = ToggleSwitchNoStrokeClearInPowerPoint.IsOn; SaveSettingsToFile(); } - - + + private void ToggleSwitchShowStrokeOnSelectInPowerPoint_Toggled(object sender, RoutedEventArgs e) { if (!isLoaded) return; @@ -3166,7 +3169,7 @@ namespace Ink_Canvas #endregion #region Advanced - + private void ToggleSwitchIsSpecialScreen_OnToggled(object sender, RoutedEventArgs e) { if (!isLoaded) return; @@ -3174,7 +3177,7 @@ namespace Ink_Canvas TouchMultiplierSlider.Visibility = ToggleSwitchIsSpecialScreen.IsOn ? Visibility.Visible : Visibility.Collapsed; SaveSettingsToFile(); } - + private void TouchMultiplierSlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs e) { if (!isLoaded) return; @@ -3185,7 +3188,7 @@ namespace Ink_Canvas private void ToggleSwitchEraserBindTouchMultiplier_Toggled(object sender, RoutedEventArgs e) { if (!isLoaded) return; - Settings.Advanced.EraserBindTouchMultiplier= ToggleSwitchEraserBindTouchMultiplier.IsOn; + Settings.Advanced.EraserBindTouchMultiplier = ToggleSwitchEraserBindTouchMultiplier.IsOn; SaveSettingsToFile(); } @@ -3389,7 +3392,7 @@ namespace Ink_Canvas private void BtnRedo_Click(object sender, RoutedEventArgs e) { - if(inkCanvas.GetSelectedStrokes().Count != 0) + if (inkCanvas.GetSelectedStrokes().Count != 0) { GridInkCanvasSelectionCover.Visibility = Visibility.Collapsed; inkCanvas.Select(new StrokeCollection()); @@ -3782,7 +3785,7 @@ namespace Ink_Canvas else { inkCanvas.EditingMode = InkCanvasEditingMode.Select; - + } } @@ -5294,7 +5297,7 @@ namespace Ink_Canvas MouseTouchMove(e.GetPosition(inkCanvas)); } } - + private void inkCanvas_MouseUp(object sender, MouseButtonEventArgs e) { if (drawingShapeMode == 5) @@ -5401,7 +5404,7 @@ namespace Ink_Canvas var timeMachineHistory = timeMachine.ExportTimeMachineHistory(); TimeMachineHistories[0] = timeMachineHistory; timeMachine.ClearStrokeHistory(); - + } else { @@ -5518,7 +5521,7 @@ namespace Ink_Canvas } } } - } + } } _currentCommitType = CommitReason.UserInput; } diff --git a/Ink Canvas/Settings.cs b/Ink Canvas/Settings.cs index d523649..fede77b 100644 --- a/Ink Canvas/Settings.cs +++ b/Ink Canvas/Settings.cs @@ -21,7 +21,7 @@ namespace Ink_Canvas [JsonProperty("startup")] public Startup Startup { get; set; } = new Startup(); } - + public class Canvas { [JsonProperty("inkWidth")] @@ -48,7 +48,7 @@ namespace Ink_Canvas public bool IsEnableTwoFingerRotation { get; set; } = false; [JsonProperty("isEnableTwoFingerRotationOnSelection")] public bool IsEnableTwoFingerRotationOnSelection { get; set; } = false; - + } public class Startup @@ -101,7 +101,7 @@ namespace Ink_Canvas [JsonProperty("isEnableFingerGestureSlideShowControl")] public bool IsEnableFingerGestureSlideShowControl { get; set; } = true; } - + public class Automation { [JsonProperty("isAutoKillPptService")] @@ -112,7 +112,7 @@ namespace Ink_Canvas public bool IsAutoSaveStrokesAtScreenshot { get; set; } = false; [JsonProperty("isAutoSaveStrokesAtClear")] public bool IsAutoSaveStrokesAtClear { get; set; } = false; - + [JsonProperty("isAutoClearWhenExitingWritingMode")] public bool IsAutoClearWhenExitingWritingMode { get; set; } = false;