Skip to content

Commit

Permalink
Fixed undo&redo bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
EX3exp committed Dec 16, 2024
1 parent 2b94e7c commit 7bc94ae
Show file tree
Hide file tree
Showing 14 changed files with 143 additions and 19 deletions.
25 changes: 24 additions & 1 deletion Mirivoice/Commands/AddLineBoxCommand.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Mirivoice.ViewModels;
using Avalonia.Controls;
using Mirivoice.ViewModels;
using Mirivoice.Views;

namespace Mirivoice.Commands
Expand All @@ -7,13 +8,30 @@ public class AddLineBoxCommand : ICommand
{
private readonly MainViewModel v;
private int LineBoxIndexLastAdded;
private bool _canUndo = true;
public bool CanUndo
{
get => _canUndo;
set
{
_canUndo = value;
}
}
public AddLineBoxCommand(MainViewModel mainViewModel)
{
v = mainViewModel;
}

UserControl lastCurrentEdit;
SingleLineEditorView lastSingleLineEditor;
public void Execute(bool isRedoing)
{
if (isRedoing)
{
v.CurrentEdit = lastCurrentEdit;
v.CurrentSingleLineEditor = lastSingleLineEditor;

}
var lineBox = new LineBoxView(v);
int LineNoToBeAdded = v.LineBoxCollection.Count + 1;

Expand All @@ -23,6 +41,8 @@ public void Execute(bool isRedoing)
v.LineBoxCollection.Add(lineBox);
LineBoxIndexLastAdded = v.LineBoxCollection.Count - 1;
lineBox.ScrollToEnd();
lastCurrentEdit = v.CurrentEdit;
lastSingleLineEditor = v.CurrentSingleLineEditor;
}

public void UnExecute()
Expand All @@ -31,6 +51,9 @@ public void UnExecute()
return;

v.LineBoxCollection.RemoveAt(LineBoxIndexLastAdded);

v.CurrentEdit = null;
v.CurrentSingleLineEditor = null;
LineBoxIndexLastAdded -= 1;

}
Expand Down
11 changes: 9 additions & 2 deletions Mirivoice/Commands/AddLineBoxesCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,15 @@ public class AddLineBoxesCommand : ICommand
private string script = string.Empty;
int DefaultVoicerOriginal;
int DefaultVoicerMetaOriginal;


private bool _canUndo = true;
public bool CanUndo
{
get => _canUndo;
set
{
_canUndo = value;
}
}
public AddLineBoxesCommand(MainViewModel mainViewModel)
{
v = mainViewModel;
Expand Down
12 changes: 9 additions & 3 deletions Mirivoice/Commands/DelLineBoxCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,16 @@ public class DelLineBoxCommand : ICommand
private ItemsControl control;
private ObservableCollection<MResult> lastResults;



private SingleLineEditorView lastEditor;

private bool _canUndo = true;
public bool CanUndo
{
get => _canUndo;
set
{
_canUndo = value;
}
}
public DelLineBoxCommand(MainViewModel mainViewModel, LineBoxView l)
{
this.l = l;
Expand Down
9 changes: 9 additions & 0 deletions Mirivoice/Commands/DuplicateLineBoxCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,15 @@ public class DuplicateLineBoxCommand : ICommand
{
private LineBoxView l;
private int LineBoxIndexLastAdded;
private bool _canUndo = true;
public bool CanUndo
{
get => _canUndo;
set
{
_canUndo = value;
}
}
public DuplicateLineBoxCommand(LineBoxView l)
{
this.l = l;
Expand Down
1 change: 1 addition & 0 deletions Mirivoice/Commands/ICommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ namespace Mirivoice.Commands
public interface ICommand
{
public void Execute(bool isRedoing);
public bool CanUndo { get; set; }

public void UnExecute();
}
Expand Down
10 changes: 9 additions & 1 deletion Mirivoice/Commands/LockLineBoxCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,15 @@ public class LockLineBoxCommand : ICommand
{
// Still implementing
LineBoxView l;

private bool _canUndo = true;
public bool CanUndo
{
get => _canUndo;
set
{
_canUndo = value;
}
}
public LockLineBoxCommand(LineBoxView lineBoxView)
{
l = lineBoxView;
Expand Down
10 changes: 9 additions & 1 deletion Mirivoice/Commands/MCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,15 @@ namespace Mirivoice.Commands
public class MCommand : ICommand
{
private readonly MReceiver _receiver;

private bool _canUndo = true;
public bool CanUndo
{
get => _canUndo;
set
{
_canUndo = value;
}
}
public MCommand (MReceiver receiver)
{
_receiver = receiver;
Expand Down
10 changes: 9 additions & 1 deletion Mirivoice/Commands/MementoCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,15 @@ public class MementoCommand<T> : ICommand

public T changedValue;
private bool redoBlocked;

private bool _canUndo = true;
public bool CanUndo
{
get => _canUndo;
set
{
_canUndo = value;
}
}
public MementoCommand(MOriginator<T> originator)
{
_originator = originator;
Expand Down
19 changes: 17 additions & 2 deletions Mirivoice/Commands/SetProsodyCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,32 +10,47 @@ public class SetProsodyCommand : ICommand

int undoMem;
int redoMem;
private bool _canUndo = true ;
public bool CanUndo
{
get => _canUndo;
set
{
_canUndo = value;
}
}

bool isFirstExec = true;


public void Execute(bool isRedoing)
{


if (isRedoing)
{
Log.Debug($"Redo: {redoMem}");
v.DonotExecCommand = true;
v.Prosody = redoMem;
v.DonotExecCommand = false;

}
else
{


if (!isFirstExec)
{
undoMem = v.Prosody;
}

isFirstExec = false;

Log.Debug($"Execute: undo: {undoMem}, redo: {redoMem}");
}
}

public void UnExecute()
{

redoMem = v.Prosody;
v.DonotExecCommand = true;
v.Prosody = undoMem;
Expand Down
10 changes: 9 additions & 1 deletion Mirivoice/Commands/SwapLineBoxCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,15 @@ public class SwapLineBoxCommand: ICommand
private int i1;
private int i2;


private bool _canUndo = true;
public bool CanUndo
{
get => _canUndo;
set
{
_canUndo = value;
}
}
public SwapLineBoxCommand(MainViewModel mainViewModel)
{
v = mainViewModel;
Expand Down
10 changes: 9 additions & 1 deletion Mirivoice/Commands/TextBoxEditCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,15 @@ public class TextBoxEditCommand : ICommand

Memento<string> undoMem = new Memento<string>();
Memento<string> redoMem = new Memento<string>();

private bool _canUndo = true;
public bool CanUndo
{
get => _canUndo;
set
{
_canUndo = value;
}
}

bool isFirstExec = true;
public void Execute(bool isRedoing)
Expand Down
10 changes: 9 additions & 1 deletion Mirivoice/Commands/VITS2SetExpCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,15 @@ public class VITS2SetExpCommand : ICommand
{
int undoMem;
int redoMem;

private bool _canUndo = true;
public bool CanUndo
{
get => _canUndo;
set
{
_canUndo = value;
}
}
readonly ExpressionEditViewModelVITS2 viewModel;
readonly ExpVITS2 mode;
public VITS2SetExpCommand(ExpressionEditViewModelVITS2 viewModel, ExpVITS2 mode)
Expand Down
15 changes: 10 additions & 5 deletions Mirivoice/Mirivoice.Core/Format/MResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -112,19 +112,23 @@ public int Prosody
{
return;
}

setProsodyCommand = new SetProsodyCommand(this);
this.RaiseAndSetIfChanged(ref _prosody, value);
if (IsFirstExec)
{

setProsodyCommand.CanUndo = false;
IsFirstExec = false;
}

setProsodyCommand = new SetProsodyCommand(this);
this.RaiseAndSetIfChanged(ref _prosody, value);
if (!DonotExecCommand)
{
MainManager.Instance.cmd.ExecuteCommand(setProsodyCommand);
}




OnPropertyChanged(nameof(Prosody));

if (l is not null)
Expand All @@ -138,6 +142,7 @@ public int Prosody
public bool IsEditable { get; set; } = false;

private readonly LineBoxView l;

public MResult(string word, string phoneme, bool isEditable, ProsodyType prosodyType, LineBoxView l=null): base(phoneme, false)

Check warning on line 146 in Mirivoice/Mirivoice.Core/Format/MResult.cs

View workflow job for this annotation

GitHub Actions / build (win-x64)

Cannot convert null literal to non-nullable reference type.

Check warning on line 146 in Mirivoice/Mirivoice.Core/Format/MResult.cs

View workflow job for this annotation

GitHub Actions / build (linux-x64)

Cannot convert null literal to non-nullable reference type.

Check warning on line 146 in Mirivoice/Mirivoice.Core/Format/MResult.cs

View workflow job for this annotation

GitHub Actions / build (osx-x64)

Cannot convert null literal to non-nullable reference type.
{
this.Word = word;
Expand Down
10 changes: 10 additions & 0 deletions Mirivoice/Mirivoice.Core/Managers/CommandManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,19 @@ public void ExecuteCommand(ICommand command)
public void Undo()
{
//Log.Debug("======== Undo ========");


if (_undoStack.Count > 0)
{
var command = _undoStack.Pop();
while (!command.CanUndo)
{
command = _undoStack.Pop(); // skip this undo and jump to previous one
if (_undoStack.Count == 0)
{
break;
}
}
command.UnExecute();
_redoStack.Push(command);
IsNeedSave = true;
Expand Down

0 comments on commit 7bc94ae

Please sign in to comment.