From 23b14f1ed7b64cfc731546bea6a95b85f35d69f4 Mon Sep 17 00:00:00 2001 From: EX3 Date: Fri, 6 Dec 2024 13:07:42 +0900 Subject: [PATCH] Improved Slider undo & redo TODO fix minor bugs about slider undo&redo --- Mirivoice/Commands/SetProsodyCommand.cs | 1 - Mirivoice/Commands/VITS2SetExpCommand.cs | 113 ++++++++++++ Mirivoice/Commands/VITS2SetExpOriginator.cs | 49 ----- .../Mirivoice.Core/Managers/CommandManager.cs | 6 + .../ExpressionEditViewModelVITS2.cs | 122 ++----------- Mirivoice/Views/ExpressionEditViewVITS2.axaml | 12 +- .../Views/ExpressionEditViewVITS2.axaml.cs | 169 +++++++++++++++--- 7 files changed, 276 insertions(+), 196 deletions(-) create mode 100644 Mirivoice/Commands/VITS2SetExpCommand.cs delete mode 100644 Mirivoice/Commands/VITS2SetExpOriginator.cs diff --git a/Mirivoice/Commands/SetProsodyCommand.cs b/Mirivoice/Commands/SetProsodyCommand.cs index 7b392b0..7fe0e40 100644 --- a/Mirivoice/Commands/SetProsodyCommand.cs +++ b/Mirivoice/Commands/SetProsodyCommand.cs @@ -6,7 +6,6 @@ namespace Mirivoice.Commands { public class SetProsodyCommand : ICommand { - // TODO private MResult v; int undoMem; diff --git a/Mirivoice/Commands/VITS2SetExpCommand.cs b/Mirivoice/Commands/VITS2SetExpCommand.cs new file mode 100644 index 0000000..9643e5a --- /dev/null +++ b/Mirivoice/Commands/VITS2SetExpCommand.cs @@ -0,0 +1,113 @@ +using Avalonia; +using Mirivoice.Mirivoice.Core.Format; +using Mirivoice.ViewModels; +using Serilog; + +namespace Mirivoice.Commands +{ + public enum ExpVITS2 + { + Speed = 0, + Noise1 = 1, + Noise2 = 2, + } + + public class VITS2SetExpCommand : ICommand + { + int undoMem; + int redoMem; + + readonly ExpressionEditViewModelVITS2 viewModel; + readonly ExpVITS2 mode; + public VITS2SetExpCommand(ExpressionEditViewModelVITS2 viewModel, ExpVITS2 mode) + { + //Log.Debug("LineEditCommand created"); + this.mode = mode; + this.viewModel = viewModel; + switch (mode) + { + case ExpVITS2.Speed: + undoMem = viewModel.VITS2Speed; + break; + case ExpVITS2.Noise1: + undoMem = viewModel.VITS2Noise1; + break; + case ExpVITS2.Noise2: + undoMem = viewModel.VITS2Noise2; + break; + default: + Log.Error("VITS2 Exp editor -- Invalid mode"); + break; + } + } + + + bool isFirstExec = true; + public void Execute(bool isRedoing) + { + if (isRedoing) + { + switch (mode) + { + case ExpVITS2.Speed: + viewModel.VITS2Speed = redoMem; + break; + case ExpVITS2.Noise1: + viewModel.VITS2Noise1 = redoMem; + break; + case ExpVITS2.Noise2: + viewModel.VITS2Noise2 = redoMem; + break; + default: + Log.Error("VITS2 Exp editor -- Invalid mode"); + break; + } + } + else + { + if (!isFirstExec) + { + switch (mode) + { + case ExpVITS2.Speed: + undoMem = viewModel.VITS2Speed; + break; + case ExpVITS2.Noise1: + undoMem = viewModel.VITS2Noise1; + break; + case ExpVITS2.Noise2: + undoMem = viewModel.VITS2Noise2; + break; + default: + Log.Error("VITS2 Exp editor -- Invalid mode"); + break; + } + } + isFirstExec = false; + } + } + + public void UnExecute() + { + switch (mode) + { + case ExpVITS2.Speed: + redoMem = viewModel.VITS2Speed; + viewModel.VITS2Speed = undoMem; + break; + case ExpVITS2.Noise1: + redoMem = viewModel.VITS2Noise1; + viewModel.VITS2Noise1 = undoMem; + break; + case ExpVITS2.Noise2: + redoMem = viewModel.VITS2Noise2; + viewModel.VITS2Noise2 = undoMem; + break; + default: + Log.Error("VITS2 Exp editor -- Invalid mode"); + break; + } + } + } + +} diff --git a/Mirivoice/Commands/VITS2SetExpOriginator.cs b/Mirivoice/Commands/VITS2SetExpOriginator.cs deleted file mode 100644 index dac78db..0000000 --- a/Mirivoice/Commands/VITS2SetExpOriginator.cs +++ /dev/null @@ -1,49 +0,0 @@ -using Mirivoice.ViewModels; - -namespace Mirivoice.Commands -{ - public enum ExpVITS2 - { - Speed = 0, - Noise1 = 1, - Noise2 = 2, - } - public class VITS2SetExpOriginator : MOriginator - { - private int index; - - private readonly ExpressionEditViewModelVITS2 v; - - ExpVITS2 type; - public VITS2SetExpOriginator(ref int index, ExpressionEditViewModelVITS2 v, ExpVITS2 type) : base(ref index) - { - this.index = index; - this.v = v; - this.type = type; - } - - - public override void UpdateProperties() - { - //Log.Debug("[Updating Properties] -- {obj}", obj); - switch (type) - { - case ExpVITS2.Speed: - v.NotProcessingSpeedCommand = true; // prevent recursion loop - v._vits2Speed = obj; - v.OnPropertyChanged(nameof(v.VITS2Speed)); - break; - case ExpVITS2.Noise1: - v.NotProcessingNoise1Command = true; // prevent recursion loop - v._vits2Noise1 = obj; - v.OnPropertyChanged(nameof(v.VITS2Noise1)); - break; - case ExpVITS2.Noise2: - v.NotProcessingNoise2Command = true; // prevent recursion loop - v._vits2Noise2 = obj; - v.OnPropertyChanged(nameof(v.VITS2Noise2)); - break; - } - } - } -} diff --git a/Mirivoice/Mirivoice.Core/Managers/CommandManager.cs b/Mirivoice/Mirivoice.Core/Managers/CommandManager.cs index 881b890..68c5326 100644 --- a/Mirivoice/Mirivoice.Core/Managers/CommandManager.cs +++ b/Mirivoice/Mirivoice.Core/Managers/CommandManager.cs @@ -4,6 +4,7 @@ using System.Text; using System.IO; using Mirivoice.ViewModels; +using System.Linq; namespace Mirivoice.Mirivoice.Core.Managers { @@ -19,6 +20,11 @@ public class CommandManager public CommandManager() { } + + public bool IsAlreadyExecuted(ICommand target) + { + return _undoStack.Count > 0 && _undoStack.Peek() == target; + } public void SetMainViewModel(MainViewModel v) { this.v = v; diff --git a/Mirivoice/ViewModels/ExpressionEditViewModelVITS2.cs b/Mirivoice/ViewModels/ExpressionEditViewModelVITS2.cs index dbee9d4..76d3e94 100644 --- a/Mirivoice/ViewModels/ExpressionEditViewModelVITS2.cs +++ b/Mirivoice/ViewModels/ExpressionEditViewModelVITS2.cs @@ -7,6 +7,7 @@ using Mirivoice.Mirivoice.Core.Format; using Mirivoice.Views; using ReactiveUI; +using Serilog; using System; namespace Mirivoice.ViewModels @@ -16,17 +17,12 @@ public class ExpressionEditViewModelVITS2: ViewModelBase public StackPanel CurrentExpression { get; set; } private readonly LineBoxView l; - public bool NotProcessingSpeedCommand = false; - public bool NotProcessingNoise1Command = false; - public bool NotProcessingNoise2Command = false; public int lastSpeed; public int lastNoise1; public int lastNoise2; - public bool UndobackupSpeed = false; - public bool UndobackupNoise1 = false; - public bool UndobackupNoise2 = false; + public int MaxSpeed { get; set; } = 100; public int MinSpeed { get; set; } = 0; @@ -37,6 +33,8 @@ public class ExpressionEditViewModelVITS2: ViewModelBase public int MaxNoise2 { get; set; } = 100; public int MinNoise2 { get; set; } = 0; + + public int _vits2Speed; public int VITS2Speed { @@ -47,41 +45,14 @@ public int VITS2Speed set { - lastSpeed = _vits2Speed; - - if (!IsDragging) - { - if (NotProcessingSpeedCommand) - { - if (!UndobackupSpeed) - { - - - SetSpeedCommand.Backup(lastSpeed); - UndobackupSpeed = true; - } - MainManager.Instance.cmd.ExecuteCommand(SetSpeedCommand); - - UndobackupSpeed = false; - } - else - { - NotProcessingSpeedCommand = false; - - } - } - else - { - NotProcessingSpeedCommand = true; - } - - this.RaiseAndSetIfChanged(ref _vits2Speed, value); OnPropertyChanged(nameof(VITS2Speed)); l.Exp.VITS2Speed = ScaleValue(100 - value, 0.5f, 1.5f); l.IsCacheIsVaild = false; + + } } @@ -91,36 +62,12 @@ public int VITS2Noise1 get => _vits2Noise1; set { - lastNoise1 = _vits2Noise1; - - if (!IsDragging) - { - if (NotProcessingNoise1Command) - { - if (!UndobackupNoise1) - { - SetNoise1Command.Backup(lastNoise1); - UndobackupNoise1 = true; - } - MainManager.Instance.cmd.ExecuteCommand(SetNoise1Command); - UndobackupNoise1 = false; - } - else - { - NotProcessingNoise1Command = false; - - } - } - else - { - NotProcessingNoise1Command = true; - } - - this.RaiseAndSetIfChanged(ref _vits2Noise1, value); OnPropertyChanged(nameof(VITS2Noise1)); l.Exp.VITS2Noise1 = ScaleValue(value, -0.3335f, 1.6675f); l.IsCacheIsVaild = false; + + } } @@ -130,36 +77,13 @@ public int VITS2Noise2 get => _vits2Noise2; set { - lastNoise2 = _vits2Noise2; - - if (!IsDragging) - { - if (NotProcessingNoise2Command) - { - if (!UndobackupNoise2) - { - SetNoise2Command.Backup(lastNoise2); - UndobackupNoise2 = true; - } - MainManager.Instance.cmd.ExecuteCommand(SetNoise2Command); - UndobackupNoise2 = false; - } - else - { - NotProcessingNoise2Command = false; - - } - } - else - { - NotProcessingNoise2Command = true; - } - this.RaiseAndSetIfChanged(ref _vits2Noise2, value); OnPropertyChanged(nameof(VITS2Noise2)); l.Exp.VITS2Noise2 = ScaleValue(value, -0.4f, 2f); l.IsCacheIsVaild = false; + + } } public ExpressionEditViewModelVITS2(LineBoxView l) @@ -169,34 +93,10 @@ public ExpressionEditViewModelVITS2(LineBoxView l) _vits2Speed = 100 - GetSliderValue(l.Exp.VITS2Speed, 0.5f, 1.5f); _vits2Noise1 = GetSliderValue(l.Exp.VITS2Noise1, -0.3335f, 1.6675f); _vits2Noise2 = GetSliderValue(l.Exp.VITS2Noise2, -0.4f, 2f); - SetCommands(); - } - - public bool IsDragging = false; - - - - MOriginator SetSpeedOriginator; - public MementoCommand SetSpeedCommand; - MOriginator SetNoise1Originator; - public MementoCommand SetNoise1Command; - MOriginator SetNoise2Originator; - public MementoCommand SetNoise2Command; - void SetCommands() - { - SetSpeedOriginator = new VITS2SetExpOriginator(ref _vits2Speed, this, ExpVITS2.Speed); - SetSpeedCommand = new MementoCommand(SetSpeedOriginator); - NotProcessingSpeedCommand = true; + } - SetNoise1Originator = new VITS2SetExpOriginator(ref _vits2Noise1, this, ExpVITS2.Noise1); - SetNoise1Command = new MementoCommand(SetNoise1Originator); - NotProcessingNoise1Command = true; - SetNoise2Originator = new VITS2SetExpOriginator(ref _vits2Noise2, this, ExpVITS2.Noise2); - SetNoise2Command = new MementoCommand(SetNoise2Originator); - NotProcessingNoise2Command = true; - } public static float ScaleValue(int input, float minValue, float maxValue) { diff --git a/Mirivoice/Views/ExpressionEditViewVITS2.axaml b/Mirivoice/Views/ExpressionEditViewVITS2.axaml index 295a77f..894c14e 100644 --- a/Mirivoice/Views/ExpressionEditViewVITS2.axaml +++ b/Mirivoice/Views/ExpressionEditViewVITS2.axaml @@ -13,22 +13,22 @@ - - + + - - + + - - + + diff --git a/Mirivoice/Views/ExpressionEditViewVITS2.axaml.cs b/Mirivoice/Views/ExpressionEditViewVITS2.axaml.cs index f73c6b3..98bfd51 100644 --- a/Mirivoice/Views/ExpressionEditViewVITS2.axaml.cs +++ b/Mirivoice/Views/ExpressionEditViewVITS2.axaml.cs @@ -13,6 +13,8 @@ using System; using System.ComponentModel; using Serilog; +using System.Windows.Input; +using Mirivoice.Commands; namespace Mirivoice; @@ -25,63 +27,172 @@ public ExpressionEditViewVITS2(LineBoxView l) { this.vMeta = vMeta; InitializeComponent(l); + this.FindControl("SpeedSlider").AddHandler(PointerPressedEvent, OnPointerPressedSpeed, handledEventsToo: true); + this.FindControl("SpeedSlider").AddHandler(PointerReleasedEvent, OnPointerReleasedSpeed, handledEventsToo: true); + this.FindControl("Noise1Slider").AddHandler(PointerPressedEvent, OnPointerPressedNoise1, handledEventsToo: true); + this.FindControl("Noise1Slider").AddHandler(PointerReleasedEvent, OnPointerReleasedNoise1, handledEventsToo: true); - } + this.FindControl("Noise2Slider").AddHandler(PointerPressedEvent, OnPointerPressedNoise2, handledEventsToo: true); + this.FindControl("Noise2Slider").AddHandler(PointerReleasedEvent, OnPointerReleasedNoise2, handledEventsToo: true); - private void OnChangedSpeed(object sender, AvaloniaPropertyChangedEventArgs e) - { - if (viewModel is null) - return; - viewModel.IsDragging = false; - viewModel.OnPropertyChanged(nameof(viewModel.VITS2Speed)); + this.FindControl("NumericSpeed").AddHandler(NumericUpDown.ValueChangedEvent, OnValueChangedSpeed, handledEventsToo: true); + this.FindControl("NumericNoise1").AddHandler(NumericUpDown.ValueChangedEvent, OnValueChangedNoise1, handledEventsToo: true); + this.FindControl("NumericNoise2").AddHandler(NumericUpDown.ValueChangedEvent, OnValueChangedNoise2, handledEventsToo: true); } - private void OnChangedNoise1(object sender, AvaloniaPropertyChangedEventArgs e) + + bool IsDraggingSpeed = false; + bool IsDraggingNoise1 = false; + bool IsDraggingNoise2 = false; + + VITS2SetExpCommand vITS2SetExpCommandSpeed; + VITS2SetExpCommand vITS2SetExpCommandNoise1; + VITS2SetExpCommand vITS2SetExpCommandNoise2; + + private void OnPointerPressedSpeed(object? sender, PointerPressedEventArgs e) { - if (viewModel is null) - return; - viewModel.IsDragging = false; - viewModel.OnPropertyChanged(nameof(viewModel.VITS2Noise1)); + + IsDraggingSpeed = true; + if (e.GetCurrentPoint(this).Properties.IsRightButtonPressed) + { + viewModel.ClrVITS2Speed(); + } + vITS2SetExpCommandSpeed = new VITS2SetExpCommand(viewModel, ExpVITS2.Speed); } - private void OnChangedNoise2(object sender, AvaloniaPropertyChangedEventArgs e) + private void OnPointerPressedNoise1(object? sender, PointerPressedEventArgs e) { - if (viewModel is null) - return; - viewModel.IsDragging = false; - viewModel.OnPropertyChanged(nameof(viewModel.VITS2Noise2)); + + IsDraggingNoise1 = true; + if (e.GetCurrentPoint(this).Properties.IsRightButtonPressed) + { + viewModel.ClrVITS2Noise1(); + } + + vITS2SetExpCommandNoise1 = new VITS2SetExpCommand(viewModel, ExpVITS2.Noise1); } - private void ClearVITS2Speed(object sender, PointerPressedEventArgs e) + private void OnPointerPressedNoise2(object? sender, PointerPressedEventArgs e) { + + IsDraggingNoise2 = true; + if (e.GetCurrentPoint(this).Properties.IsRightButtonPressed) { - viewModel.ClrVITS2Speed(); + viewModel.ClrVITS2Noise2(); } - + + vITS2SetExpCommandNoise2 = new VITS2SetExpCommand(viewModel, ExpVITS2.Noise2); } - private void Tapped(object sender, TappedEventArgs e) + private void OnPointerReleasedSpeed(object? sender, PointerReleasedEventArgs e) { - Log.Debug("Tapped"); - viewModel.IsDragging = true; + + + if (!MainManager.Instance.cmd.IsAlreadyExecuted(vITS2SetExpCommandSpeed)) + MainManager.Instance.cmd.ExecuteCommand(vITS2SetExpCommandSpeed); + + IsDraggingSpeed = false; } - private void ClearVITS2Noise1(object sender, PointerPressedEventArgs e) + private void OnPointerReleasedNoise1(object? sender, PointerReleasedEventArgs e) { - if (e.GetCurrentPoint(this).Properties.IsRightButtonPressed) + if (!MainManager.Instance.cmd.IsAlreadyExecuted(vITS2SetExpCommandNoise1)) + MainManager.Instance.cmd.ExecuteCommand(vITS2SetExpCommandNoise1); + IsDraggingNoise1 = false; + } + + private void OnPointerReleasedNoise2(object? sender, PointerReleasedEventArgs e) + { + if (!MainManager.Instance.cmd.IsAlreadyExecuted(vITS2SetExpCommandNoise2)) + MainManager.Instance.cmd.ExecuteCommand(vITS2SetExpCommandNoise2); + IsDraggingNoise2 = false; + } + + bool numericChangedSpeed = false; + bool numericChangedNoise1 = false; + bool numericChangedNoise2 = false; + + private void OnValueChangedSpeed(object? sender, NumericUpDownValueChangedEventArgs e) + { + + numericChangedSpeed = true; + } + + private void OnValueChangedNoise1(object? sender, NumericUpDownValueChangedEventArgs e) + { + + numericChangedNoise1 = true; + } + + private void OnValueChangedNoise2(object? sender, NumericUpDownValueChangedEventArgs e) + { + + numericChangedNoise2 = true; + } + + private void OnChangedSpeed(object sender, AvaloniaPropertyChangedEventArgs e) + { + if (viewModel is null) + return; + + + if (!IsDraggingSpeed || numericChangedSpeed) { - viewModel.ClrVITS2Noise1(); + + if (!MainManager.Instance.cmd.IsAlreadyExecuted(vITS2SetExpCommandSpeed)) + { + vITS2SetExpCommandSpeed = new VITS2SetExpCommand(viewModel, ExpVITS2.Speed); + MainManager.Instance.cmd.ExecuteCommand(vITS2SetExpCommandSpeed); + } + numericChangedSpeed = false; + } + + viewModel.OnPropertyChanged(nameof(viewModel.VITS2Speed)); } - private void ClearVITS2Noise2(object sender, PointerPressedEventArgs e) + + private void OnChangedNoise1(object sender, AvaloniaPropertyChangedEventArgs e) { - if (e.GetCurrentPoint(this).Properties.IsRightButtonPressed) + if (viewModel is null) + return; + + + if (!IsDraggingNoise1 || numericChangedNoise1) { - viewModel.ClrVITS2Noise2(); + + if (!MainManager.Instance.cmd.IsAlreadyExecuted(vITS2SetExpCommandNoise1)) + { + vITS2SetExpCommandNoise1 = new VITS2SetExpCommand(viewModel, ExpVITS2.Noise1); + MainManager.Instance.cmd.ExecuteCommand(vITS2SetExpCommandNoise1); + } + numericChangedNoise1 = false; + } + viewModel.OnPropertyChanged(nameof(viewModel.VITS2Noise1)); + } + + private void OnChangedNoise2(object sender, AvaloniaPropertyChangedEventArgs e) + { + if (viewModel is null) + return; + + + if (!IsDraggingNoise2 || numericChangedNoise2) + { + + if (!MainManager.Instance.cmd.IsAlreadyExecuted(vITS2SetExpCommandNoise2)) + { + vITS2SetExpCommandNoise2 = new VITS2SetExpCommand(viewModel, ExpVITS2.Noise2); + MainManager.Instance.cmd.ExecuteCommand(vITS2SetExpCommandNoise2); + + } + numericChangedNoise2 = false; } + viewModel.OnPropertyChanged(nameof(viewModel.VITS2Noise2)); } + + private void InitializeComponent(LineBoxView l) { AvaloniaXamlLoader.Load(this);