Commit f014efd8 authored by DailyShana's avatar DailyShana

rewrite undo

parent e72065c5
...@@ -20,7 +20,7 @@ public void SetPath(string gamepath) ...@@ -20,7 +20,7 @@ public void SetPath(string gamepath)
luapath = MyPath.Combine(gamepath, "script"); luapath = MyPath.Combine(gamepath, "script");
ydkpath = MyPath.Combine(gamepath, "deck"); ydkpath = MyPath.Combine(gamepath, "deck");
replaypath = MyPath.Combine(gamepath, "replay"); replaypath = MyPath.Combine(gamepath, "replay");
} }
/// <summary>游戏目录</summary> /// <summary>游戏目录</summary>
public string gamepath; public string gamepath;
/// <summary>大图目录</summary> /// <summary>大图目录</summary>
...@@ -36,71 +36,63 @@ public void SetPath(string gamepath) ...@@ -36,71 +36,63 @@ public void SetPath(string gamepath)
/// <summary>录像目录</summary> /// <summary>录像目录</summary>
public string replaypath; public string replaypath;
public string GetImage(long id, bool bak = false) public string GetImage(long id)
{ {
return GetImage(id.ToString(), bak); return GetImage(id.ToString());
} }
public string GetImageThum(long id, bool bak = false) public string GetImageThum(long id)
{ {
return GetImageThum(id.ToString(), bak); return GetImageThum(id.ToString());
} }
public string GetImageField(long id, bool bak = false) public string GetImageField(long id)
{ {
return GetImageField(id.ToString(), bak);//场地图 return GetImageField(id.ToString());//场地图
} }
public string GetScript(long id, bool bak = false) public string GetScript(long id)
{ {
return GetScript(id.ToString(), bak); return GetScript(id.ToString());
} }
public string GetYdk(string name) public string GetYdk(string name)
{ {
return MyPath.Combine(ydkpath, name + ".ydk"); return MyPath.Combine(ydkpath, name + ".ydk");
} }
//字符串id //字符串id
public string GetImage(string id, bool bak = false) public string GetImage(string id)
{ {
if (bak)
return MyPath.Combine(picpath, id + ".jpg.bak");
return MyPath.Combine(picpath, id + ".jpg"); return MyPath.Combine(picpath, id + ".jpg");
} }
public string GetImageThum(string id, bool bak = false) public string GetImageThum(string id)
{ {
if (bak)
return MyPath.Combine(picpath2, id + ".jpg.bak");
return MyPath.Combine(picpath2, id + ".jpg"); return MyPath.Combine(picpath2, id + ".jpg");
} }
public string GetImageField(string id, bool bak = false) public string GetImageField(string id)
{ {
if (bak)
return MyPath.Combine(fieldpath, id + ".png.bak");
return MyPath.Combine(fieldpath, id+ ".png");//场地图 return MyPath.Combine(fieldpath, id+ ".png");//场地图
} }
public string GetScript(string id, bool bak = false) public string GetScript(string id)
{ {
if (bak)
return MyPath.Combine(luapath, "c" + id + ".lua.bak");
return MyPath.Combine(luapath, "c" + id + ".lua"); return MyPath.Combine(luapath, "c" + id + ".lua");
} }
public string[] GetCardfiles(long id, bool bak = false) public string[] GetCardfiles(long id)
{ {
string[] files = new string[]{ string[] files = new string[]{
GetImage(id, bak),//大图 GetImage(id),//大图
GetImageThum(id, bak),//小图 GetImageThum(id),//小图
GetImageField(id, bak),//场地图 GetImageField(id),//场地图
GetScript(id, bak) GetScript(id)
}; };
return files; return files;
} }
public string[] GetCardfiles(string id, bool bak = false) public string[] GetCardfiles(string id)
{ {
string[] files = new string[]{ string[] files = new string[]{
GetImage(id, bak),//大图 GetImage(id),//大图
GetImageThum(id, bak),//小图 GetImageThum(id),//小图
GetImageField(id, bak),//场地图 GetImageField(id),//场地图
GetScript(id, bak) GetScript(id)
}; };
return files; return files;
} }
} }
} }
This diff is collapsed.
using System;
using System.Collections.Generic;
namespace DataEditorX.Core
{
public delegate void StatusBool(bool val);
public interface ICommand : ICloneable
{
bool Excute(params object[] args);
}
public interface IBackableCommand : ICommand
{
void Undo();
}
public interface ICommandManager
{
void ExcuteCommand(ICommand command, params object[] args);
void Undo();
void ReverseUndo();//反撤销
event StatusBool UndoStateChanged;
}
public class CommandManager : ICommandManager
{
private Stack<ICommand> undoStack = new Stack<ICommand>();
private Stack<ICommand> reverseStack = new Stack<ICommand>();
public event StatusBool UndoStateChanged;
public CommandManager()
{
UndoStateChanged += new StatusBool(CommandManager_UndoStateChanged);
UndoStateChanged += new StatusBool(CommandManager_ReverseUndoStateChanged);
}
private void CommandManager_UndoStateChanged(bool val)
{
}
private void CommandManager_ReverseUndoStateChanged(bool val)
{
}
#region ICommandManager 成员
public void ExcuteCommand(ICommand command, params object[] args)
{
if(!command.Excute(args)) return;
reverseStack.Clear();
if (command is IBackableCommand)
{
undoStack.Push((ICommand)command.Clone());
}
else
{
undoStack.Clear();
}
UndoStateChanged(undoStack.Count > 0);
}
public void Undo()
{
IBackableCommand command = (IBackableCommand)undoStack.Pop();
if (command == null)
{
return;
}
command.Undo();
reverseStack.Push((ICommand)command.Clone());
UndoStateChanged(undoStack.Count > 0);
//UndoStateChanged(reverseStack.Count > 0);
}
public void ReverseUndo()
{
IBackableCommand command = (IBackableCommand)reverseStack.Pop();
if (command == null)
{
return;
}
command.Excute();
undoStack.Push((ICommand)command.Clone());
UndoStateChanged(undoStack.Count > 0);
}
#endregion
}
}
...@@ -3,10 +3,9 @@ ...@@ -3,10 +3,9 @@
using System.IO; using System.IO;
using System.Collections.Generic; using System.Collections.Generic;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using Microsoft.VisualBasic; using Microsoft.VisualBasic.FileIO;
using System.Configuration; using System.Configuration;
using DataEditorX.Config; using DataEditorX.Config;
using System.Windows.Forms;
using DataEditorX.Core.Info; using DataEditorX.Core.Info;
...@@ -205,59 +204,20 @@ public static string[] ReadImage(string path) ...@@ -205,59 +204,20 @@ public static string[] ReadImage(string path)
#region 删除资源 #region 删除资源
//删除资源 //删除资源
public enum DeleteOption public static void CardDelete(long id, YgoPath ygopath)
{
BACKUP,
RESTORE,
CLEAN,
NONE,
}
public static void CardDelete(long id, YgoPath ygopath, DeleteOption option)
{ {
string[] files = ygopath.GetCardfiles(id); string[] files = ygopath.GetCardfiles(id);
string[] bakfiles = ygopath.GetCardfiles(id, true); for (int i = 0; i < files.Length; i++)
switch (option)
{ {
case DeleteOption.BACKUP: if (FileSystem.FileExists(files[i]))
for (int i = 0; i < files.Length; i++) FileSystem.DeleteFile(files[i], UIOption.OnlyErrorDialogs, RecycleOption.SendToRecycleBin);
{
if (File.Exists(bakfiles[i]))
File.Delete(bakfiles[i]);
if (File.Exists(files[i]))
File.Move(files[i], files[i] + ".bak");
}
break;
case DeleteOption.RESTORE:
for (int i = 0; i < bakfiles.Length; i++)
{
if (File.Exists(files[i]))
File.Delete(files[i]);
if (File.Exists(bakfiles[i]))
File.Move(bakfiles[i], bakfiles[i].Replace("bak", ""));
}
break;
case DeleteOption.CLEAN:
for (int i = 0; i < bakfiles.Length; i++)
{
if (File.Exists(bakfiles[i]))
File.Delete(bakfiles[i]);
}
break;
case DeleteOption.NONE:
for (int i = 0; i < files.Length; i++)
{
if (File.Exists(files[i]))
File.Delete(files[i]);
}
break;
} }
} }
#endregion #endregion
#region 资源改名 #region 资源改名
//资源改名 //资源改名
public static void CardRename(long newid, long oldid, YgoPath ygopath, bool delold) public static void CardRename(long newid, long oldid, YgoPath ygopath)
{ {
string[] newfiles = ygopath.GetCardfiles(newid); string[] newfiles = ygopath.GetCardfiles(newid);
string[] oldfiles = ygopath.GetCardfiles(oldid); string[] oldfiles = ygopath.GetCardfiles(oldid);
...@@ -266,13 +226,32 @@ public static void CardRename(long newid, long oldid, YgoPath ygopath, bool delo ...@@ -266,13 +226,32 @@ public static void CardRename(long newid, long oldid, YgoPath ygopath, bool delo
{ {
if (File.Exists(oldfiles[i])) if (File.Exists(oldfiles[i]))
{ {
if (delold) try {
File.Move(oldfiles[i], newfiles[i]); File.Move(oldfiles[i], newfiles[i]);
else }
File.Copy(oldfiles[i], newfiles[i], false); catch { }
} }
} }
} }
#endregion #endregion
#region 复制资源
public static void CardCopy(long newid, long oldid, YgoPath ygopath)
{
string[] newfiles = ygopath.GetCardfiles(newid);
string[] oldfiles = ygopath.GetCardfiles(oldid);
for (int i = 0; i < oldfiles.Length; i++)
{
if (File.Exists(oldfiles[i]))
{
try {
File.Copy(oldfiles[i], newfiles[i], false);
}
catch { }
}
}
}
#endregion
} }
} }
...@@ -55,6 +55,8 @@ public partial class DataEditForm : DockContent, IDataForm ...@@ -55,6 +55,8 @@ public partial class DataEditForm : DockContent, IDataForm
//setcode正在输入 //setcode正在输入
bool[] setcodeIsedit = new bool[5]; bool[] setcodeIsedit = new bool[5];
CommandManager cmdManager = new CommandManager();
Image m_cover; Image m_cover;
MSEConfig msecfg; MSEConfig msecfg;
...@@ -90,6 +92,13 @@ void Initialize(string datapath) ...@@ -90,6 +92,13 @@ void Initialize(string datapath)
InitializeComponent(); InitializeComponent();
title = this.Text; title = this.Text;
nowCdbFile = ""; nowCdbFile = "";
cmdManager.UndoStateChanged += delegate (bool val)
{
if (val)
btn_undo.Enabled = true;
else
btn_undo.Enabled = false;
};
} }
#endregion #endregion
...@@ -147,18 +156,6 @@ void DataEditFormLoad(object sender, EventArgs e) ...@@ -147,18 +156,6 @@ void DataEditFormLoad(object sender, EventArgs e)
//窗体关闭 //窗体关闭
void DataEditFormFormClosing(object sender, FormClosingEventArgs e) void DataEditFormFormClosing(object sender, FormClosingEventArgs e)
{ {
//清理备份文件
List<long> delids = new List<long>();
foreach (CardEdit.FileDeleted deleted in cardedit.undoDeleted)
{
if (deleted != null && deleted.deleted)
delids.AddRange(deleted.ids);
}
if (delids.Count != 0)
{
foreach (long id in delids)
YGOUtil.CardDelete(id, GetPath(), YGOUtil.DeleteOption.CLEAN);
}
//当前有任务执行,是否结束 //当前有任务执行,是否结束
if (tasker != null && tasker.IsRuning()) if (tasker != null && tasker.IsRuning())
{ {
...@@ -566,7 +563,7 @@ void Lv_cardlistKeyDown(object sender, KeyEventArgs e) ...@@ -566,7 +563,7 @@ void Lv_cardlistKeyDown(object sender, KeyEventArgs e)
switch (e.KeyCode) switch (e.KeyCode)
{ {
case Keys.Delete: case Keys.Delete:
cardedit.DelCards(menuitem_operacardsfile.Checked); cmdManager.ExcuteCommand(cardedit.delCard, menuitem_operacardsfile.Checked);
break; break;
case Keys.Right: case Keys.Right:
Btn_PageDownClick(null, null); Btn_PageDownClick(null, null);
...@@ -730,18 +727,14 @@ void Btn_resetClick(object sender, EventArgs e) ...@@ -730,18 +727,14 @@ void Btn_resetClick(object sender, EventArgs e)
//添加 //添加
void Btn_addClick(object sender, EventArgs e) void Btn_addClick(object sender, EventArgs e)
{ {
if(cardedit != null) if (cardedit != null)
cardedit.AddCard(); cmdManager.ExcuteCommand(cardedit.addCard);
if (cardedit.undoSQL.Count != 0)
btn_undo.Enabled = true;
} }
//修改 //修改
void Btn_modClick(object sender, EventArgs e) void Btn_modClick(object sender, EventArgs e)
{ {
if (cardedit != null) if (cardedit != null)
cardedit.ModCard(menuitem_operacardsfile.Checked); cmdManager.ExcuteCommand(cardedit.modCard, menuitem_operacardsfile.Checked);
if (cardedit.undoSQL.Count != 0)
btn_undo.Enabled = true;
} }
//打开脚本 //打开脚本
void Btn_luaClick(object sender, EventArgs e) void Btn_luaClick(object sender, EventArgs e)
...@@ -753,16 +746,16 @@ void Btn_luaClick(object sender, EventArgs e) ...@@ -753,16 +746,16 @@ void Btn_luaClick(object sender, EventArgs e)
void Btn_delClick(object sender, EventArgs e) void Btn_delClick(object sender, EventArgs e)
{ {
if (cardedit != null) if (cardedit != null)
cardedit.DelCards(menuitem_operacardsfile.Checked); cmdManager.ExcuteCommand(cardedit.delCard, menuitem_operacardsfile.Checked);
if (cardedit.undoSQL.Count != 0)
btn_undo.Enabled = true;
} }
//撤销
void Btn_undoClick(object sender, EventArgs e) void Btn_undoClick(object sender, EventArgs e)
{ {
if (cardedit != null) if (cardedit != null)
cardedit.Undo(); {
if (cardedit.undoSQL.Count == 0) cmdManager.Undo();
btn_undo.Enabled = false; Search(true);
}
} }
//导入卡图 //导入卡图
void Btn_imgClick(object sender, EventArgs e) void Btn_imgClick(object sender, EventArgs e)
...@@ -1105,45 +1098,8 @@ void Menuitem_copyselecttoClick(object sender, EventArgs e) ...@@ -1105,45 +1098,8 @@ void Menuitem_copyselecttoClick(object sender, EventArgs e)
//保存卡片到当前数据库 //保存卡片到当前数据库
public void SaveCards(Card[] cards) public void SaveCards(Card[] cards)
{ {
if (!CheckOpen()) cmdManager.ExcuteCommand(cardedit.copyCard, cards);
return;
if (cards == null || cards.Length == 0)
return;
bool replace = false;
Card[] oldcards = DataBase.Read(nowCdbFile, true, "");
if (oldcards != null && oldcards.Length != 0)
{
int i = 0;
foreach (Card oc in oldcards)
{
foreach (Card c in cards)
{
if (c.id == oc.id)
{
i += 1;
if (i == 1)
{
replace = MyMsg.Question(LMSG.IfReplaceExistingCard);
break;
}
}
}
if (i > 0)
break;
}
}
cardedit.undoSQL.Add("");
cardedit.undoModified.Add(new CardEdit.FileModified());
cardedit.undoDeleted.Add(new CardEdit.FileDeleted());
DataBase.CopyDB(nowCdbFile, !replace, cards);
CardEdit.DBcopied copied = new CardEdit.DBcopied();
copied.copied = true;
copied.NewCards = cards;
copied.replace = replace;
copied.OldCards = oldcards;
cardedit.undoCopied.Add(copied);
Search(srcCard, true); Search(srcCard, true);
btn_undo.Enabled = true;
} }
//卡片另存为 //卡片另存为
void CopyTo(Card[] cards) void CopyTo(Card[] cards)
......
...@@ -87,6 +87,7 @@ ...@@ -87,6 +87,7 @@
</Compile> </Compile>
<Compile Include="Common\MyPath.cs" /> <Compile Include="Common\MyPath.cs" />
<Compile Include="Controls\History.cs" /> <Compile Include="Controls\History.cs" />
<Compile Include="Core\CommandManager.cs" />
<Compile Include="Core\IDataForm.cs" /> <Compile Include="Core\IDataForm.cs" />
<Compile Include="Controls\IEditForm.cs" /> <Compile Include="Controls\IEditForm.cs" />
<Compile Include="Controls\IMainForm.cs" /> <Compile Include="Controls\IMainForm.cs" />
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment