public void CommitTest() { Counter counter = new Counter(); UndoManager undoMgr = new UndoManager(); undoMgr.BeginTransaction("From 1 to 10"); for (int i = 0; i < 10; i++) { counter.Increase(); undoMgr.AddAction(new AddCountAction(counter)); } undoMgr.Commit(); Assert.IsTrue(counter.Count == 10); undoMgr.Undo(); Assert.IsTrue(counter.Count == 0); undoMgr.Redo(); Assert.IsTrue(counter.Count == 10); undoMgr.Undo(); Assert.IsTrue(counter.Count == 0); undoMgr.Redo(); Assert.IsTrue(counter.Count == 10); undoMgr.BeginTransaction("From 10 to 15"); for (int i = 0; i < 5; i++) { counter.Increase(); undoMgr.AddAction(new AddCountAction(counter)); } undoMgr.Commit(); Assert.IsTrue(counter.Count == 15); }
/// <summary> /// add uploadAction /// </summary> /// <param name="item"></param> public static void AddAction(IUndoAction item) { if (_current != null) { _current.AddAction(item); } }
/// <summary> /// Update frame layer control /// </summary> /// <param name="propertyName">property name</param> protected virtual void OnPropertyChanged(string propertyName, object newValue, object oldValue) { _isChanged = true; if (IsEdit) { return; } UndoManager undoMgr = UndoManager.Current; if (undoMgr != null) { if (undoMgr.BeginTransaction("Changed the " + propertyName + "Property")) { undoMgr.AddAction(new SimpleUndoAction { UndoHandler = () => this.SetProperty(propertyName, oldValue), RedoHandler = () => this.SetProperty(propertyName, newValue), }); undoMgr.Commit(); } } if (_PropertyChanged != null) { _PropertyChanged(this, new PropertyChangedEventArgs(propertyName, newValue, oldValue)); } }
public void Test1() { Counter counter = new Counter(); UndoManager undoMgr = new UndoManager(); counter.Increase(); undoMgr.AddAction(new AddCountAction(counter)); counter.Increase(); undoMgr.AddAction(new AddCountAction(counter)); counter.Increase(); undoMgr.AddAction(new AddCountAction(counter)); Assert.IsTrue(counter.Count == 3); Assert.IsTrue(undoMgr.UndoStackCount == counter.Count); undoMgr.Undo(); Assert.IsTrue(counter.Count == 2); Assert.IsTrue(undoMgr.UndoStackCount == 2); Assert.IsTrue(undoMgr.RedoStackCount == 1); undoMgr.Undo(); Assert.IsTrue(counter.Count == 1); Assert.IsTrue(undoMgr.UndoStackCount == 1); Assert.IsTrue(undoMgr.RedoStackCount == 2); undoMgr.Undo(); Assert.IsTrue(counter.Count == 0); Assert.IsTrue(undoMgr.UndoStackCount == 0); Assert.IsTrue(undoMgr.RedoStackCount == 3); undoMgr.Redo(); Assert.IsTrue(counter.Count == 1); Assert.IsTrue(undoMgr.UndoStackCount == 1); Assert.IsTrue(undoMgr.RedoStackCount == 2); undoMgr.Redo(); Assert.IsTrue(counter.Count == 2); Assert.IsTrue(undoMgr.UndoStackCount == 2); Assert.IsTrue(undoMgr.RedoStackCount == 1); undoMgr.Redo(); Assert.IsTrue(counter.Count == 3); Assert.IsTrue(undoMgr.UndoStackCount == 3); Assert.IsTrue(undoMgr.RedoStackCount == 0); }
public void RollbackTest() { Counter counter = new Counter(); UndoManager undoMgr = new UndoManager(); undoMgr.BeginTransaction("From 1 to 10"); for (int i = 0; i < 10; i++) { counter.Increase(); undoMgr.AddAction(new AddCountAction(counter)); } undoMgr.Rollback(); Assert.IsTrue(counter.Count == 0); Assert.IsTrue(undoMgr.UndoStackCount == 0); }
private void Update() { if (EventSystem.current.IsPointerOverGameObject()) { return; } var currentTool = GetCurrentTool(); if (currentTool == Tool.Select) { if (Input.GetKeyDown(KeyCode.Mouse0)) { RaycastHit hit; var ray = camera.ScreenPointToRay(Input.mousePosition); if (Physics.Raycast(ray, out hit)) { int x = Mathf.FloorToInt(hit.point.x + hit.normal.x / -2); int y = Mathf.FloorToInt(hit.point.y + hit.normal.y / -2); int z = Mathf.FloorToInt(hit.point.z + hit.normal.z / -2); if (ModelManager.GetCube(x, y, z) && !CubeSelectionController.DoesCubeExist(x, y, z) && TransformController.GetSelectedTranformComponents() == 0) { int color = ModelManager.GetCubeColor(x, y, z); //CubeSelectionController.ResetCubePositions(); CubeSelectionController.AddCube(x, y, z, color); ModelManager.RemoveCube(x, y, z); TransformController.UpdateVisibleTools(); } } else { var controllerPosition = CubeSelectionController.GetPosition(); foreach (var cube in CubeSelectionController.GetAllCubes()) { ModelManager.AddCube(cube.x + (int)controllerPosition.x, cube.y + (int)controllerPosition.y, cube.z + (int)controllerPosition.z, cube.color); } CubeSelectionController.Clear(); CubeSelectionController.ResetPosition(); TransformController.UpdateVisibleTools(); } } } else if (currentTool == Tool.Create) { if (Input.GetKey(KeyCode.Mouse0) && Input.GetKey(KeyCode.LeftShift) && Time.time - lastRapidTool > 0.15f) { lastRapidTool = Time.time; RaycastHit hit; var ray = camera.ScreenPointToRay(Input.mousePosition); if (Physics.Raycast(ray, out hit)) { int x = Mathf.FloorToInt(hit.point.x + hit.normal.x / 2); int y = Mathf.FloorToInt(hit.point.y + hit.normal.y / 2); int z = Mathf.FloorToInt(hit.point.z + hit.normal.z / 2); ModelManager.AddCube(x, y, z); UndoManager.AddAction(new CreateBlockAction(x, y, z, ColorpickerController.GetClosestColor().GetAsIndex())); } } else if (Input.GetKeyDown(KeyCode.Mouse0)) { RaycastHit hit; var ray = camera.ScreenPointToRay(Input.mousePosition); if (Physics.Raycast(ray, out hit)) { int x = Mathf.FloorToInt(hit.point.x + hit.normal.x / 2); int y = Mathf.FloorToInt(hit.point.y + hit.normal.y / 2); int z = Mathf.FloorToInt(hit.point.z + hit.normal.z / 2); ModelManager.AddCube(x, y, z); UndoManager.AddAction(new CreateBlockAction(x, y, z, ColorpickerController.GetClosestColor().GetAsIndex())); } else { var point = ray.GetPoint(5); int x = Mathf.FloorToInt(point.x); int y = Mathf.FloorToInt(point.y); int z = Mathf.FloorToInt(point.z); ModelManager.AddCube(x, y, z); UndoManager.AddAction(new CreateBlockAction(x, y, z, ColorpickerController.GetClosestColor().GetAsIndex())); } } } else if (currentTool == Tool.Destroy) { if (Input.GetKey(KeyCode.Mouse0) && Input.GetKey(KeyCode.LeftShift) && Time.time - lastRapidTool > 0.15f) { lastRapidTool = Time.time; RaycastHit hit; var ray = camera.ScreenPointToRay(Input.mousePosition); if (Physics.Raycast(ray, out hit)) { int x = Mathf.FloorToInt(hit.point.x + hit.normal.x / -2); int y = Mathf.FloorToInt(hit.point.y + hit.normal.y / -2); int z = Mathf.FloorToInt(hit.point.z + hit.normal.z / -2); int color = ModelManager.GetCubeColor(x, y, z); ModelManager.RemoveCube(x, y, z); UndoManager.AddAction(new RemoveBlockAction(x, y, z, color)); } } else if (Input.GetKeyDown(KeyCode.Mouse0)) { RaycastHit hit; var ray = camera.ScreenPointToRay(Input.mousePosition); if (Physics.Raycast(ray, out hit)) { int x = Mathf.FloorToInt(hit.point.x + hit.normal.x / -2); int y = Mathf.FloorToInt(hit.point.y + hit.normal.y / -2); int z = Mathf.FloorToInt(hit.point.z + hit.normal.z / -2); int color = ModelManager.GetCubeColor(x, y, z); ModelManager.RemoveCube(x, y, z); UndoManager.AddAction(new RemoveBlockAction(x, y, z, color)); } } } else if (currentTool == Tool.Paint) { if (Input.GetKey(KeyCode.Mouse0)) { RaycastHit hit; var ray = camera.ScreenPointToRay(Input.mousePosition); if (Physics.Raycast(ray, out hit)) { int x = Mathf.FloorToInt(hit.point.x + hit.normal.x / -2); int y = Mathf.FloorToInt(hit.point.y + hit.normal.y / -2); int z = Mathf.FloorToInt(hit.point.z + hit.normal.z / -2); int color = ModelManager.GetCubeColor(x, y, z); int colorpickerValue = ColorpickerController.GetClosestColor().GetAsIndex(); if (color != colorpickerValue) { UndoManager.AddAction(new PaintBlockAction(x, y, z, color, ColorpickerController.GetClosestColor().GetAsIndex())); ModelManager.AddCube(x, y, z); } } } } else if (currentTool == Tool.Colorpicker) { if (Input.GetKeyDown(KeyCode.Mouse0)) { RaycastHit hit; var ray = camera.ScreenPointToRay(Input.mousePosition); if (Physics.Raycast(ray, out hit)) { int x = Mathf.FloorToInt(hit.point.x + hit.normal.x / -2); int y = Mathf.FloorToInt(hit.point.y + hit.normal.y / -2); int z = Mathf.FloorToInt(hit.point.z + hit.normal.z / -2); int color = ModelManager.GetCubeColor(x, y, z); int paletteWidth = ColorPaletteManager.GetPaletteWidth(); ColorpickerController.SetColor(color % paletteWidth, Mathf.FloorToInt(color / paletteWidth)); } } } if (Input.GetKeyDown(KeyCode.Mouse1)) { CursorController.AddUser("mainLook"); isLooking = true; } if (Input.GetKeyUp(KeyCode.Mouse1)) { CursorController.RemoveUser("mainLook"); isLooking = false; } if (Input.GetKeyDown(KeyCode.LeftShift)) { movementSpeed = 0.35f; } if (Input.GetKeyUp(KeyCode.LeftShift)) { movementSpeed = 0.175f; } }
private void bind_Parse(object sender, ConvertEventArgs e) { try { // Get the existing value from the object. Binding binding = (Binding)sender; string boundProperty = binding.BindingMemberInfo.BindingField; object datasource = binding.DataSource; Type controlType = datasource.GetType(); PropertyInfo pi = controlType.GetProperty(boundProperty); object oldValue = pi.GetValue(datasource, null); // If this is the first modification to this FieldControl, store a copy of // its original underlying IField value in the Tag property. if (binding.Control.Parent.Parent is FieldControl) { if (binding.Control.Parent.Parent.Tag == null) { if (((FieldControl)binding.Control.Parent.Parent).BaseValue != null) { binding.Control.Parent.Parent.Tag = ShallowClone(((FieldControl)binding.Control.Parent.Parent).BaseValue); } } } bool readOnly = false; if (binding.Control.Parent.Parent is FieldControl) { readOnly = (binding.Control.Parent.Parent as FieldControl).ReadOnly; } // Convert to the desired type. string[] parts = e.DesiredType.ToString().Split('.'); if (parts[0] != "System") { throw new Exception("Cannot convert type '" + e.DesiredType + "' - it is not a standard System type."); } Type t = Type.GetType("System.Convert"); MethodInfo mi = t.GetMethod("To" + parts[1], new Type[1] { typeof(string) }); object newValue = mi.Invoke(null, new object[1] { e.Value.ToString() }); if (readOnly) { e.Value = oldValue; // No changes for j00! } else { // Notify the FieldControl that it is Dirty. if (binding.Control.Parent.Parent is FieldControl) { ((FieldControl)binding.Control.Parent.Parent).Dirty = true; } // Add this action to the UndoManager. IField field = binding.DataSource as IField; if (field != null) { undoManager.AddAction(new UndoableFieldEditAction(field, boundProperty, oldValue, newValue)); } } } catch (Exception ex) { Trace.WriteLine("Could not set undo state for object: " + ex.Message); } }
public void Increase() { count++; undoManager.AddAction(new AddCountAction(this)); }