Undoables

/* * Made by Lonami, the 30/11/2014 at 17:00 * (C) LonamiWebs * * -> HOW TO USE: * You can initialize any UndoableType by asiggning it it's value. For example: * UndoableInt ui = 7; * UndoableFloat uf = 38.42f; * UndoableString us = "potato"; * * You can also initialize by creating a new UndoableType and using (or not) var keyword: * var ui = new UndoableInt(7); * var uf = new UndoableFloat(38.42f); * var us = new UndoableString("potato"); * * You can NOT set a new value the same way before. You have to use the SetValue() method: * ui.SetValue(14); * uf.SetValue(24.12f); * us.SetValue("tomato"); * * If you do NOT use SetValue method, you will NOT be able to use Undo and Redo methods. * You can specify how many times you wish to Undo, or Redo: * ui.Undo(3); // will undo 3 times * uf.Redo(); // will redo once * us.Redo(5); // will redo 5 times * * Both Undo and Redo methods return a boolean value: if Undo (or Redo) was successful, returns true. * Otherwise, it will return false. * * To retrieve the value of the Undoable, you may use Undoable.Value or Undoable implicitly: * int value_a = ui.Value; * int value_b = ui; * * Both value_a and value_b will be the same. */ using System; using System.Collections.Generic; namespace Undoables { public class UndoableInt { readonly List<int> StoredValues = new List<int>(); int Step = 1; public bool Undo(int times = 1) { int bStep = Step; for (int j = times; j > 0; j--) { Step++; int i = StoredValues.Count - Step; if (i < 0) i = StoredValues.Count - --Step; Value = StoredValues[i]; } return bStep != Step; } public bool Redo(int times = 1) { int bStep = Step; for (int j = times; j > 0; j--) { Step--; int i = StoredValues.Count - Step; if (i >= StoredValues.Count) i = StoredValues.Count - ++Step; Value = StoredValues[i]; } return bStep != Step; } public void SetValue(int value) { StoredValues.Add(value); Value = value; Step = 1; } public UndoableInt(int initialValue) { SetValue(initialValue); } public static implicit operator UndoableInt(int initialValue) { return new UndoableInt(initialValue); } public static implicit operator int(UndoableInt undoableInt) { return undoableInt.Value; } public int Value { get; private set; } } public class UndoableString { readonly List<string> StoredValues = new List<string>(); int Step = 1; public bool Undo(int times = 1) { int bStep = Step; for (int j = times; j > 0; j--) { Step++; int i = StoredValues.Count - Step; if (i < 0) i = StoredValues.Count - --Step; Value = StoredValues[i]; } return bStep != Step; } public bool Redo(int times = 1) { int bStep = Step; for (int j = times; j > 0; j--) { Step--; int i = StoredValues.Count - Step; if (i >= StoredValues.Count) i = StoredValues.Count - ++Step; Value = StoredValues[i]; } return bStep != Step; } public void SetValue(string value) { StoredValues.Add(value); Value = value; Step = 1; } public UndoableString(string initialValue) { SetValue(initialValue); } public static implicit operator UndoableString(string initialValue) { return new UndoableString(initialValue); } public static implicit operator string(UndoableString undoableString) { return undoableString.Value; } public string Value { get; private set; } } public class UndoableFloat { readonly List<float> StoredValues = new List<float>(); int Step = 1; public bool Undo(int times = 1) { int bStep = Step; for (int j = times; j > 0; j--) { Step++; int i = StoredValues.Count - Step; if (i < 0) i = StoredValues.Count - --Step; Value = StoredValues[i]; } return bStep != Step; } public bool Redo(int times = 1) { int bStep = Step; for (int j = times; j > 0; j--) { Step--; int i = StoredValues.Count - Step; if (i >= StoredValues.Count) i = StoredValues.Count - ++Step; Value = StoredValues[i]; } return bStep != Step; } public void SetValue(float value) { StoredValues.Add(value); Value = value; Step = 1; } public UndoableFloat(float initialValue) { SetValue(initialValue); } public static implicit operator UndoableFloat(float initialValue) { return new UndoableFloat(initialValue); } public static implicit operator float(UndoableFloat undoableFloat) { return undoableFloat.Value; } public float Value { get; private set; } } public class UndoableObject { readonly List<object> StoredValues = new List<object>(); int Step = 1; public bool Undo(int times = 1) { int bStep = Step; for (int j = times; j > 0; j--) { Step++; int i = StoredValues.Count - Step; if (i < 0) i = StoredValues.Count - --Step; Value = StoredValues[i]; } return bStep != Step; } public bool Redo(int times = 1) { int bStep = Step; for (int j = times; j > 0; j--) { Step--; int i = StoredValues.Count - Step; if (i >= StoredValues.Count) i = StoredValues.Count - ++Step; Value = StoredValues[i]; } return bStep != Step; } public void SetValue(object value) { StoredValues.Add(value); Value = value; Step = 1; } public UndoableObject(object initialValue) { SetValue(initialValue); } public static implicit operator UndoableObject(object initialValue) { return new UndoableObject(initialValue); } public static implicit operator object(UndoableObject undoableObject) { return undoableObject.Value; } public object Value { get; private set; } } }
A class that implements UndoableInts, UndoableStrings, UndoableFloats and UndoableObjects to your code, so you can easily use undoableInt.Undo();, or even undoableString.Redo();, for example

1 Response

Really cool snippet! I'm not sure what practical things I'd use it for, but it's an interesting concept.

Write a comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.