write shit

This commit is contained in:
2021-07-11 20:55:29 +08:00
parent b290828f58
commit 8c641db61c
4 changed files with 101 additions and 4 deletions

View File

@ -60,6 +60,7 @@
<Compile Include="Core\ClipboardUtil.cs" /> <Compile Include="Core\ClipboardUtil.cs" />
<Compile Include="Core\FileOperation.cs" /> <Compile Include="Core\FileOperation.cs" />
<Compile Include="Core\KeyboardState.cs" /> <Compile Include="Core\KeyboardState.cs" />
<Compile Include="Core\LimitedStack.cs" />
<Compile Include="UI\AddItem.xaml.cs"> <Compile Include="UI\AddItem.xaml.cs">
<DependentUpon>AddItem.xaml</DependentUpon> <DependentUpon>AddItem.xaml</DependentUpon>
</Compile> </Compile>

View File

@ -325,14 +325,26 @@ namespace BallanceTASEditor.Core.FileOperation {
long counter = 0; long counter = 0;
if (isInsertBefore) { if (isInsertBefore) {
foreach (var item in data.IterateFull()) { foreach (var item in data.IterateFull()) {
if (node == null) {
// insert from tail instead
mMem.AddLast(item.Value);
} else {
mMem.AddBefore(node, item.Value); mMem.AddBefore(node, item.Value);
}
if (counter == gottenPointerPos) if (counter == gottenPointerPos)
mPointer = node.Previous; mPointer = node.Previous;
counter++; counter++;
} }
} else { } else {
foreach (var item in data.IterateFullReversed()) { foreach (var item in data.IterateFullReversed()) {
if (node == null) {
// insert from head instead
mMem.AddFirst(item.Value);
} else {
mMem.AddAfter(node, item.Value); mMem.AddAfter(node, item.Value);
}
if (counter == gottenPointerPos) if (counter == gottenPointerPos)
mPointer = node.Next; mPointer = node.Next;
counter++; counter++;

39
Core/LimitedStack.cs Normal file
View File

@ -0,0 +1,39 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace BallanceTASEditor.Core {
public class LimitedStack<T> {
private static readonly int STACK_LENGTH = 20;
public LimitedStack() {
_stack = new LinkedList<T>();
}
private LinkedList<T> _stack;
public void Push(T data) {
_stack.AddLast(data);
if (_stack.Count > STACK_LENGTH) {
_stack.RemoveFirst();
}
}
public T Pop() {
if (_stack.Last == null) return default(T);
var data = _stack.Last.Value;
_stack.RemoveLast();
return data;
}
public void Clear() {
_stack.Clear();
}
public bool IsEmpty() {
return _stack.Count == 0;
}
}
}

View File

@ -1,4 +1,5 @@
using BallanceTASEditor.Core.TASStruct; using BallanceTASEditor.Core.FileOperation;
using BallanceTASEditor.Core.TASStruct;
using BallanceTASEditor.UI; using BallanceTASEditor.UI;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
@ -18,6 +19,9 @@ namespace BallanceTASEditor.Core {
fs.Dispose(); fs.Dispose();
mPointer = mMem.First; mPointer = mMem.First;
mPointerIndex = 0; mPointerIndex = 0;
mRedoStack = new LimitedStack<RevocableOperation>();
mUndoStack = new LimitedStack<RevocableOperation>();
} }
public string mFilename { get; private set; } public string mFilename { get; private set; }
@ -26,6 +30,9 @@ namespace BallanceTASEditor.Core {
LinkedListNode<FrameData> mPointer; LinkedListNode<FrameData> mPointer;
long mPointerIndex; long mPointerIndex;
LimitedStack<RevocableOperation> mRedoStack;
LimitedStack<RevocableOperation> mUndoStack;
public bool IsEmpty() { public bool IsEmpty() {
return (mPointer == null); return (mPointer == null);
} }
@ -67,6 +74,11 @@ namespace BallanceTASEditor.Core {
// if isSet is null, mean flip state // if isSet is null, mean flip state
public void Set(SelectionRange field, SelectionRange absoluteRange, bool? isSet) { public void Set(SelectionRange field, SelectionRange absoluteRange, bool? isSet) {
var oper = new SetOperation(field, absoluteRange, isSet);
oper.Do(ref mMem, ref mPointer, ref mPointerIndex);
mUndoStack.Push(oper);
mRedoStack.Clear();
/*
if (mPointer == null) return; if (mPointer == null) return;
uint offset = 0; uint offset = 0;
@ -78,9 +90,15 @@ namespace BallanceTASEditor.Core {
else if (isSet == true) item.Value.SetKeyStates(offset); else if (isSet == true) item.Value.SetKeyStates(offset);
else if (isSet == false) item.Value.UnsetKeyStates(offset); else if (isSet == false) item.Value.UnsetKeyStates(offset);
} }
*/
} }
public void Remove(SelectionRange absoluteRange) { public void Remove(SelectionRange absoluteRange) {
var oper = new RemoveOperation(absoluteRange);
oper.Do(ref mMem, ref mPointer, ref mPointerIndex);
mUndoStack.Push(oper);
mRedoStack.Clear();
/*
if (mPointer == null) return; if (mPointer == null) return;
// remove // remove
@ -105,9 +123,15 @@ namespace BallanceTASEditor.Core {
mPointerIndex = newIndex; mPointerIndex = newIndex;
} }
} }
*/
} }
public void Add(long absolutePos, long count, float deltaTime, bool isAddBefore) { public void Add(long absolutePos, long count, float deltaTime, bool isAddBefore) {
var oper = new AddOperation(absolutePos, count, deltaTime, isAddBefore);
oper.Do(ref mMem, ref mPointer, ref mPointerIndex);
mUndoStack.Push(oper);
mRedoStack.Clear();
/*
if (count <= 0) return; if (count <= 0) return;
if (mPointer == null) { if (mPointer == null) {
@ -132,9 +156,15 @@ namespace BallanceTASEditor.Core {
} }
} }
} }
*/
} }
public void Insert(long absolutePos, LinkedList<FrameData> data, bool isInsertBefore) { public void Insert(long absolutePos, LinkedList<FrameData> data, bool isInsertBefore, bool isOverwritten) {
var oper = new InsertOperation(absolutePos, data, isInsertBefore, isOverwritten);
oper.Do(ref mMem, ref mPointer, ref mPointerIndex);
mUndoStack.Push(oper);
mRedoStack.Clear();
/*
if (data.Count == 0) return; if (data.Count == 0) return;
// the same process route with add function // the same process route with add function
@ -156,6 +186,21 @@ namespace BallanceTASEditor.Core {
} }
} }
} }
*/
}
public void Redo() {
if (mRedoStack.IsEmpty()) return;
var oper = mRedoStack.Pop();
oper.Do(ref mMem, ref mPointer, ref mPointerIndex);
mUndoStack.Push(oper);
}
public void Undo() {
if (mUndoStack.IsEmpty()) return;
var oper = mUndoStack.Pop();
oper.Undo(ref mMem, ref mPointer, ref mPointerIndex);
mRedoStack.Push(oper);
} }
public void Copy(SelectionRange absoluteRange, LinkedList<FrameData> data) { public void Copy(SelectionRange absoluteRange, LinkedList<FrameData> data) {