JD2022-TU1/main/tools/legacy/UA_AtlasEditor/MainForm_undo.cs

134 lines
3.7 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace UA_AtlasEditor
{
public partial class MainForm
{
public class UndoData
{
public bool _needSave;
public List<AtlasShape> _shapes;
public UndoData(Atlas atlas, bool needSave )
{
_needSave = needSave;
_shapes = atlas.DuplicateShapeList();
}
}
int _undoCursor = 0;
int _undoMarkSave = 0;
bool _needSave = false;
readonly List<UndoData> _Done = new List<UndoData>();
const int c_maxUndo = 100;
public void undoClear()
{
_Done.Clear();
_undoMarkSave = 0;
_undoCursor = 0;
_needSave = false;
}
public void undoSave()
{
_needSave = false;
_undoMarkSave = _undoCursor;
}
public bool undoNeedSave()
{
return _needSave || (_undoMarkSave != _undoCursor);
}
public void undoPush(bool needSave, bool refresh = true)
{
while (_undoCursor != _Done.Count)
{
if (_undoMarkSave == _Done.Count)
{
_undoMarkSave--;
if (_Done[_Done.Count - 1]._needSave)
_needSave = true;
}
_Done.RemoveAt(_Done.Count - 1);
}
if (_undoMarkSave == _undoCursor && !needSave)
_undoMarkSave++;
_undoCursor++;
UndoData undo = new UndoData(_atlas, needSave );
_Done.Add(undo);
while (_Done.Count >= c_maxUndo)
{
_undoCursor--;
if (_undoMarkSave > 0)
_undoMarkSave--;
else
_needSave = _Done[0]._needSave;
_Done.RemoveAt(0);
}
if (!refresh) return;
atlasSetTitle();
}
public bool canUndo()
{
return _undoCursor > 0;
}
public bool canRedo()
{
return _undoCursor < _Done.Count;
}
public void undoRecallLast()
{
UndoData undo = _Done[_undoCursor - 1];
_atlas.ReplaceShapeList(undo._shapes);
}
public void doUndo(bool refresh = true)
{
if (!canUndo()) return;
UndoData save = new UndoData(_atlas, false);
UndoData undo = _Done[_undoCursor - 1];
_atlas.ReplaceShapeList(undo._shapes);
undo._shapes = save._shapes;
if (_undoMarkSave == _undoCursor && !_Done[_undoCursor - 1]._needSave)
_undoMarkSave--;
_undoCursor--;
if (!refresh) return;
atlasSetTitle();
MainFor_TreatResult(c_result_invalidate | c_result_updatelist | c_result_updatelistselection | c_result_updatelistselecteditems);
infoUpdate();
}
public void doRedo(bool refresh = true)
{
if (!canRedo()) return;
UndoData save = new UndoData(_atlas, false);
UndoData undo = _Done[_undoCursor];
_atlas.ReplaceShapeList(undo._shapes);
undo._shapes = save._shapes;
if (_undoMarkSave == _undoCursor && !_Done[_undoCursor]._needSave)
_undoMarkSave++;
_undoCursor++;
if (!refresh) return;
atlasSetTitle();
MainFor_TreatResult(c_result_invalidate | c_result_updatelist | c_result_updatelistselection | c_result_updatelistselecteditems);
}
}
}