using System; namespace Otter { /// /// Component used for a value with built in min, max, and wrapping capabilities. Can be useful for making /// menus. /// public class Counter : Component { #region Public Fields /// /// The current value of the Counter. /// public int Value = 0; /// /// Determines if the value should wrap around when exceeding the min or max. /// public bool Wrap = false; /// /// Determines if the value should be clamped by the minimum and maximum value. /// public bool Cap = true; /// /// The minimum value of the Counter. /// public int Min = 0; /// /// The maximum value of the Counter. /// public int Max = 0; /// /// The starting value of the Counter. /// public int InitialValue = 0; /// /// A callback for when the Counter increments. /// public Action OnIncrement; /// /// A callback for when the Counter decrements. /// public Action OnDecrement; /// /// A callback for when the counter reaches the maximum value. /// public Action OnMax; /// /// A callback for when the counter reaches the minimum value. /// public Action OnMin; #endregion #region Public Properties /// /// If the Counter is currently at or exceeding the maximum value. /// public bool AtMax { get { return Value >= Max; } } /// /// If the Counter is currently at or exceeding the minimum value. /// public bool AtMin { get { return Value <= Min; } } /// /// The length of the Counter from Min to Max. /// public int Length { get { return Math.Abs(Max - Min) + 1; } } #endregion #region Constructors /// /// Create a new Counter. /// /// The initial value. /// The minimum value. /// The maximum value. /// If the counter should wrap when it reaches the minimum or maximum values. /// If the counter shouldn't be allowed to exceed the minimum or maximum values. public Counter(int value, int min, int max, bool wrap = false, bool cap = true) { InitialValue = value; Value = value; if (min > max) throw new ArgumentException("Min must be lower than max!"); Min = min; Max = max; Wrap = wrap; Cap = cap; } #endregion #region Public Methods /// /// Reset the Counter back to its initial value. /// public void Reset() { Value = InitialValue; } /// /// Increment the value of the Counter. /// /// How much to increment by. /// The new value. public int Increment(int value = 1) { Value += value; if (Cap) { if (Value > Max) { if (Wrap) { while (Value > Max) { Value -= (Length); } } else Value = Max; } } return Value; } /// /// Decrement the value of the Counter. /// /// How much to decrement by. /// The new value. public int Decrement(int value = 1) { Value -= value; if (Cap) { if (Value < Min) { if (Wrap) { while (Value < Min) { Value += (Length); } } else Value = Min; } } return Value; } /// /// Update the Counter. /// public override void Update() { if (Cap) { if (Value > Max) { if (Wrap) { while (Value > Max) { Value -= (Length); } } else Value = Max; } if (Value < Min) { if (Wrap) { while (Value < Min) { Value += (Length); } } else Value = Min; } } } /// /// Force the value to the maximum value. /// public void GoToMax() { Value = Max; } /// /// Force the value to the minimum value. /// public void GoToMin() { Value = Min; } #endregion #region Operators public static implicit operator float(Counter counter) { return counter.Value; } public static implicit operator int(Counter counter) { return (int)counter.Value; } #endregion } }