常に行いたい操作とその操作を元に戻すための操作をセットにして履歴を保持することで、 行った操作を元に戻す機能、元に戻した操作をやり直す機能を提供するクラス。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
using System; using System.Collections.Generic; /// <summary> /// 実行、元に戻す(undo)、やり直す(redo)の各動作を定義するインターフェース /// </summary> public interface ICommand { /// <summary> /// 操作を実行するメソッド /// </summary> void Do(); /// <summary> /// 操作を元に戻すメソッド /// </summary> void Undo(); /// <summary> /// 操作をやり直すメソッド /// </summary> void Redo(); } /// <summary> /// 行なった操作の履歴を蓄積することでUndo,Redoの機能を提供するクラス /// </summary> public class UndoRedoManager { private Stack<ICommand> undo = new Stack<ICommand>(); private Stack<ICommand> redo = new Stack<ICommand>(); private bool canUndo = false; private bool canRedo = false; /// <summary> /// 操作を実行し、かつその内容を履歴に追加します。 /// </summary> /// <param name="command">ICommandインターフェースを実装し、行う操作を定義したオブジェクト</param> public void Do(ICommand command) { this.undo.Push(command); this.CanUndo = this.undo.Count > 0; command.Do(); this.redo.Clear(); this.CanRedo = this.redo.Count > 0; } /// <summary> /// 操作を実行し、かつその内容を履歴に追加します。 /// </summary> /// <param name="doMethod">操作を行なうメソッド</param> /// <param name="doParamater">doMethodに必要な引数</param> /// <param name="undoMethod">操作を行なう前の状態に戻すためのメソッド</param> /// <param name="undoParamater">undoMethodに必要な引数</param> public void Do(Delegate doMethod, object[] doParamater, Delegate undoMethod, object[] undoParamater) { Command command = new Command(doMethod, doParamater, undoMethod, undoParamater); this.Do(command); } /// <summary> /// 行なった操作を取り消してひとつ前の状態に戻します。 /// </summary> public void Undo() { if(this.undo.Count >= 1) { ICommand command = this.undo.Pop(); this.CanUndo = this.undo.Count > 0; command.Undo(); this.redo.Push(command); this.CanRedo = this.redo.Count > 0; } } /// <summary> /// 取り消した操作をやり直します。 /// </summary> public void Redo() { if(this.redo.Count >= 1) { ICommand command = this.redo.Pop(); this.CanRedo = this.redo.Count > 0; command.Redo(); this.undo.Push(command); this.CanUndo = this.undo.Count > 0; } } /// <summary> /// Undo出来るかどうかを返します。 /// </summary> public bool CanUndo { private set { if(this.canUndo != value) { this.canUndo = value; if(this.CanUndoChange != null) { this.CanUndoChange(this, EventArgs.Empty); } } } get { return this.canUndo; } } /// <summary> /// Redo出来るかどうかを返します。 /// </summary> public bool CanRedo { private set { if(this.canRedo != value) { this.canRedo = value; if(this.CanRedoChange != null) { this.CanRedoChange(this, EventArgs.Empty); } } } get { return this.canRedo; } } /// <summary> /// Undo出来るかどうかの状態が変化すると発生します。 /// </summary> public event EventHandler CanUndoChange; /// <summary> /// Redo出来るかどうかの状態が変化すると発生します。 /// </summary> public event EventHandler CanRedoChange; private class Command:ICommand { private Delegate doMethod; private Delegate undoMethod; private object[] doParamater; private object[] undoParamater; public Command(Delegate doMethod, object[] doParamater, Delegate undoMethod, object[] undoParamater) { this.doMethod = doMethod; this.doParamater = doParamater; this.undoMethod = undoMethod; this.undoParamater = undoParamater; } #region ICommand メンバ public void Do() { doMethod.DynamicInvoke(doParamater); } public void Undo() { undoMethod.DynamicInvoke(undoParamater); } public void Redo() { doMethod.DynamicInvoke(doParamater); } #endregion } } |
以下のように使用する。
既存のアプリケーションに取り込むのはかなり大変でしょう。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
using System; using System.Windows.Forms; namespace Test { public partial class Form1:Form { UndoRedoManager undoRedoManager = new UndoRedoManager(); public Form1() { InitializeComponent(); undoRedoManager.CanUndoChange += (sender, e) => { this.戻すButton.Enabled = this.undoRedoManager.CanUndo; }; undoRedoManager.CanRedoChange += (sender, e) => { this.やり直すButton.Enabled = this.undoRedoManager.CanRedo; }; } private void 足すButton_Click(object sender, EventArgs e) { //行う操作を定義する object[] doParam = new object[] { this.progressBar1, (int)this.numericUpDown1.Value }; Action<ProgressBar, int> doAction = new Action<ProgressBar, int>((progressBar, value) => { progressBar.Value += value; }); //操作を行うときには必ずその操作を取り消す為の操作も定義する object[] undoParam = new object[] { this.progressBar1, (int)this.numericUpDown1.Value }; Action<ProgressBar, int> undoAction = new Action<ProgressBar, int>((progressBar, value) => { progressBar.Value -= value; }); //両方を渡すことで操作が実行される。 undoRedoManager.Do(doAction, doParam, undoAction, undoParam); } private void 引くButton_Click(object sender, EventArgs e) { object[] doParam = new object[] { this.progressBar1, (int)this.numericUpDown1.Value }; Action<ProgressBar, int> doAction = new Action<ProgressBar, int>((progressBar, value) => { progressBar.Value -= value; }); object[] undoParam = new object[] { this.progressBar1, (int)this.numericUpDown1.Value }; Action<ProgressBar, int> undoAction = new Action<ProgressBar, int>((progressBar, value) => { progressBar.Value += value; }); undoRedoManager.Do(doAction, doParam, undoAction, undoParam); } private void 戻すButton_Click(object sender, EventArgs e) { undoRedoManager.Undo(); } private void やり直すButton_Click(object sender, EventArgs e) { undoRedoManager.Redo(); } } } |
ICommandインターフェースを実装したオブジェクトを作成して渡すことでより安全に実装出来る。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
using System; using System.Windows.Forms; namespace Test { public partial class Form1:Form { UndoRedoManager undoRedoManager = new UndoRedoManager(); public Form1() { InitializeComponent(); undoRedoManager.CanUndoChange += (sender, e) => { this.戻すButton.Enabled = this.undoRedoManager.CanUndo; }; undoRedoManager.CanRedoChange += (sender, e) => { this.やり直すButton.Enabled = this.undoRedoManager.CanRedo; }; } private class AddCommand:ICommand { private ProgressBar progressBer; private int value; public AddCommand(ProgressBar progressBer, int value) { this.progressBer = progressBer; this.value = value; } #region ICommand メンバ public void Do() { this.progressBer.Value += value; } public void Redo() { this.Do(); } public void Undo() { this.progressBer.Value -= value; } #endregion } private void 足すButton_Click(object sender, EventArgs e) { AddCommand command = new AddCommand(this.progressBar1, (int)this.numericUpDown1.Value); undoRedoManager.Do(command); } private void 引くButton_Click(object sender, EventArgs e) { AddCommand command = new AddCommand(this.progressBar1, -(int)this.numericUpDown1.Value); undoRedoManager.Do(command); } private void 戻すButton_Click(object sender, EventArgs e) { undoRedoManager.Undo(); } private void やり直すButton_Click(object sender, EventArgs e) { undoRedoManager.Redo(); } } } |