Undo system made
This commit is contained in:
@@ -0,0 +1,18 @@
|
||||
{
|
||||
"name": "Features.History",
|
||||
"rootNamespace": "Darkmatter.Features.History",
|
||||
"references": [
|
||||
"GUID:6a0a834eb41764f12ba55c3fb04a40cb",
|
||||
"GUID:b0214a6008ed146ff8f122a6a9c2f6cc",
|
||||
"GUID:c1c03c0e5b2f4412b9f2be1c20d6a9b1"
|
||||
],
|
||||
"includePlatforms": [],
|
||||
"excludePlatforms": [],
|
||||
"allowUnsafeCode": false,
|
||||
"overrideReferences": false,
|
||||
"precompiledReferences": [],
|
||||
"autoReferenced": true,
|
||||
"defineConstraints": [],
|
||||
"versionDefines": [],
|
||||
"noEngineReferences": false
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 3aa6224adf551496497bf0c866e704b5
|
||||
AssemblyDefinitionImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
8
Assets/Darkmatter/Code/Features/History/Installers.meta
Normal file
8
Assets/Darkmatter/Code/Features/History/Installers.meta
Normal file
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 9cc86805d75dc4f3781d17d7c65ac1c8
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,15 @@
|
||||
using Darkmatter.Core.Contracts.Features.History;
|
||||
using Darkmatter.Libs.Installers;
|
||||
using UnityEngine;
|
||||
using VContainer;
|
||||
|
||||
namespace Darkmatter.Features.History
|
||||
{
|
||||
public class HistoryServiceModule : MonoBehaviour,IServiceModule
|
||||
{
|
||||
public void Register(IContainerBuilder builder)
|
||||
{
|
||||
builder.Register<IUndoStack, UndoStack>(Lifetime.Singleton);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 45c42e41a28d34b01a364a3c2631ba73
|
||||
8
Assets/Darkmatter/Code/Features/History/Stack.meta
Normal file
8
Assets/Darkmatter/Code/Features/History/Stack.meta
Normal file
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 352fce035805a47c4aec94d870adea6d
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
47
Assets/Darkmatter/Code/Features/History/Stack/UndoStack.cs
Normal file
47
Assets/Darkmatter/Code/Features/History/Stack/UndoStack.cs
Normal file
@@ -0,0 +1,47 @@
|
||||
using System.Collections.Generic;
|
||||
using Darkmatter.Core.Contracts.Features.History;
|
||||
|
||||
namespace Darkmatter.Features.History
|
||||
{
|
||||
public sealed class UndoStack : IUndoStack
|
||||
{
|
||||
private const int Capacity = 20;
|
||||
|
||||
private readonly LinkedList<ICommand> _undo = new();
|
||||
private readonly Stack<ICommand> _redo = new();
|
||||
|
||||
public bool CanUndo => _undo.Count > 0;
|
||||
public bool CanRedo => _redo.Count > 0;
|
||||
|
||||
public void Push(ICommand cmd)
|
||||
{
|
||||
cmd.Execute();
|
||||
_undo.AddLast(cmd);
|
||||
if (_undo.Count > Capacity) _undo.RemoveFirst();
|
||||
_redo.Clear();
|
||||
}
|
||||
|
||||
public void Undo()
|
||||
{
|
||||
if (!CanUndo) return;
|
||||
var cmd = _undo.Last!.Value;
|
||||
_undo.RemoveLast();
|
||||
cmd.Undo();
|
||||
_redo.Push(cmd);
|
||||
}
|
||||
|
||||
public void Redo()
|
||||
{
|
||||
if (!CanRedo) return;
|
||||
var cmd = _redo.Pop();
|
||||
cmd.Execute();
|
||||
_undo.AddLast(cmd);
|
||||
}
|
||||
|
||||
public void Clear()
|
||||
{
|
||||
_undo.Clear();
|
||||
_redo.Clear();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 1a9ed946cf48e4bd582992ca4ca662a3
|
||||
Reference in New Issue
Block a user