using System.Collections.Generic; namespace CoreUnit.Control.UserControl.Canvas { class EditCommandBase { public virtual bool DoUndo(IModel data) { return false; } public virtual bool DoRedo(IModel data) { return false; } } class EditCommandAdd : EditCommandBase { List m_objects = null; IDrawObject m_object; ICanvasLayer m_layer; public EditCommandAdd(ICanvasLayer layer, IDrawObject obj) { m_object = obj; m_layer = layer; } public EditCommandAdd(ICanvasLayer layer, List objects) { m_objects = new List(objects); m_layer = layer; } public override bool DoUndo(IModel data) { if (m_object != null) { data.DeleteObjects(new List() { m_object }); ///////////////////////////////////////////////// //객체 삭제 후 이름을 재 명명(hochul.kim) for (int i = 0; i < m_layer.Objects.Count; i++) { IDrawObject obj = m_layer.Objects[i]; obj.No = i; } ///////////////////////////////////////////////// } if (m_objects != null) { data.DeleteObjects(m_objects); ///////////////////////////////////////////////// //객체 삭제 후 이름을 재 명명(hochul.kim) for (int i = 0; i < m_layer.Objects.Count; i++) { IDrawObject obj = m_layer.Objects[i]; obj.No = i; } ///////////////////////////////////////////////// } return true; } public override bool DoRedo(IModel data) { if (m_object != null) { data.AddObject(m_layer, m_object); ///////////////////////////////////////////////// //객체 삭제 후 이름을 재 명명(hochul.kim) for (int i = 0; i < m_layer.Objects.Count; i++) { IDrawObject obj = m_layer.Objects[i]; obj.No = i; } ///////////////////////////////////////////////// } if (m_objects != null) { foreach (IDrawObject obj in m_objects) { data.AddObject(m_layer, obj); } ///////////////////////////////////////////////// //객체 삭제 후 이름을 재 명명(hochul.kim) for (int i = 0; i < m_layer.Objects.Count; i++) { IDrawObject obj = m_layer.Objects[i]; obj.No = i; } ///////////////////////////////////////////////// } return true; } } class EditCommandRemove : EditCommandBase { List m_objects = null; ICanvasLayer m_layer; public EditCommandRemove(ICanvasLayer layer, List objects) { m_objects = new List(objects); m_layer = layer; } public override bool DoUndo(IModel data) { if (m_objects != null) { foreach (IDrawObject obj in m_objects) { m_layer.Objects.Insert(obj.No, obj); } ///////////////////////////////////////////////// //객체 삭제 후 이름을 재 명명(hochul.kim) for (int i = 0; i < m_layer.Objects.Count; i++) { IDrawObject obj = m_layer.Objects[i]; obj.No = i; } } ///////////////////////////////////////////////// return true; } public override bool DoRedo(IModel data) { if (m_objects != null) { data.DeleteObjects(m_objects); ///////////////////////////////////////////////// //객체 삭제 후 이름을 재 명명(hochul.kim) for (int i = 0; i < m_layer.Objects.Count; i++) { IDrawObject obj = m_layer.Objects[i]; obj.No = i; } ///////////////////////////////////////////////// } return true; } } class EditCommandMove : EditCommandBase { List m_objects = new List(); UnitPoint m_offset; public EditCommandMove(UnitPoint offset, IEnumerable objects) { m_objects = new List(objects); m_offset = offset; } public override bool DoUndo(IModel data) { foreach (IDrawObject obj in m_objects) { UnitPoint offset = new UnitPoint(-m_offset.X, -m_offset.Y); obj.Move(offset); } return true; } public override bool DoRedo(IModel data) { foreach (IDrawObject obj in m_objects) obj.Move(m_offset); return true; } } class EditCommandRotate : EditCommandBase { List m_objects = new List(); float m_angle; public EditCommandRotate(float angle, IEnumerable objects) { m_objects = new List(objects); m_angle = angle; } public override bool DoUndo(IModel data) { foreach (IDrawObject obj in m_objects) { obj.Rotate(-1 * m_angle, new UnitPoint(obj.MiddlePoint.X, obj.MiddlePoint.Y)); } return true; } public override bool DoRedo(IModel data) { foreach (IDrawObject obj in m_objects) obj.Rotate(m_angle, new UnitPoint(obj.MiddlePoint.X, obj.MiddlePoint.Y)); return true; } } class EditCommandMakeLength : EditCommandBase { List m_objects = new List(); float m_distance; public EditCommandMakeLength(float distance, IEnumerable objects) { m_objects = new List(objects); m_distance = distance; } public override bool DoUndo(IModel data) { foreach (IDrawObject obj in m_objects) { obj.MakeLength(-1 * m_distance); } return true; } public override bool DoRedo(IModel data) { foreach (IDrawObject obj in m_objects) obj.MakeLength(m_distance); return true; } } class EditCommandNodeMove : EditCommandBase { List m_objects = new List(); public EditCommandNodeMove(IEnumerable objects) { m_objects = new List(objects); } public override bool DoUndo(IModel data) { foreach (INodePoint obj in m_objects) obj.Undo(); return true; } public override bool DoRedo(IModel data) { foreach (INodePoint obj in m_objects) obj.Redo(); return true; } } class EditCommandEditTool : EditCommandBase { IEditTool m_tool; public EditCommandEditTool(IEditTool tool) { m_tool = tool; } public override bool DoUndo(IModel data) { m_tool.Undo(); return true; } public override bool DoRedo(IModel data) { m_tool.Redo(); return true; } } class UndoRedoBuffer { List m_undoBuffer = new List(); List m_redoBuffer = new List(); bool m_canCapture = true; bool m_dirty = false; public UndoRedoBuffer() { } public void Clear() { m_undoBuffer.Clear(); m_redoBuffer.Clear(); } public bool Dirty { get { return m_dirty; } set { m_dirty = value; } } public bool CanCapture { get { return m_canCapture; } } public bool CanUndo { get { return m_undoBuffer.Count > 0; } } public bool CanRedo { get { return m_redoBuffer.Count > 0; } } public void AddCommand(EditCommandBase command) { if (m_canCapture && command != null) { m_undoBuffer.Add(command); m_redoBuffer.Clear(); Dirty = true; } } public bool DoUndo(IModel data) { if (m_undoBuffer.Count == 0) return false; m_canCapture = false; EditCommandBase command = m_undoBuffer[m_undoBuffer.Count - 1]; bool result = command.DoUndo(data); m_undoBuffer.RemoveAt(m_undoBuffer.Count - 1); m_redoBuffer.Add(command); m_canCapture = true; Dirty = true; return result; } public bool DoRedo(IModel data) { if (m_redoBuffer.Count == 0) return false; m_canCapture = false; EditCommandBase command = m_redoBuffer[m_redoBuffer.Count - 1]; bool result = command.DoRedo(data); m_redoBuffer.RemoveAt(m_redoBuffer.Count - 1); m_undoBuffer.Add(command); m_canCapture = true; Dirty = true; return result; } } }