IntCollection: Removed VOID_SaveValue interface and members.
[VOID.git] / VOID_DataValue.cs
blob:a/VOID_DataValue.cs -> blob:b/VOID_DataValue.cs
--- a/VOID_DataValue.cs
+++ b/VOID_DataValue.cs
@@ -26,6 +26,8 @@
 	public interface IVOID_DataValue
 	{
 		void Refresh();
+		string ValueUnitString();
+		void DoGUIHorizontal();
 	}
 
 	public class VOID_DataValue<T> : IVOID_DataValue
@@ -59,13 +61,6 @@
 			}
 		}
 
-		public string ValueUnitString {
-			get {
-				return this.Value.ToString() + this.Units;
-			}
-		}
-
-
 		/*
 		 * Methods
 		 * */
@@ -74,7 +69,6 @@
 			this.Label = Label;
 			this.Units = Units;
 			this.ValueFunc = ValueFunc;
-			this.cache = this.ValueFunc.Invoke ();
 		}
 
 		public void Refresh()
@@ -86,6 +80,10 @@
 		{
 			this.Refresh ();
 			return (T)this.cache;
+		}
+
+		public string ValueUnitString() {
+			return this.Value.ToString() + this.Units;
 		}
 
 		public virtual void DoGUIHorizontal()
@@ -93,7 +91,7 @@
 			GUILayout.BeginHorizontal (GUILayout.ExpandWidth (true));
 			GUILayout.Label (this.Label + ":");
 			GUILayout.FlexibleSpace ();
-			GUILayout.Label (this.ValueUnitString, GUILayout.ExpandWidth (false));
+			GUILayout.Label (this.ValueUnitString(), GUILayout.ExpandWidth (false));
 			GUILayout.EndHorizontal ();
 		}
 
@@ -110,6 +108,7 @@
 
 	internal interface IVOID_NumericValue
 	{
+		double ToDouble();
 		string ToString(string format);
 		string ToSIString(int digits, int MinMagnitude, int MaxMagnitude);
 	}
@@ -118,34 +117,95 @@
 	{
 		public VOID_NumValue(string Label, Func<T> ValueFunc, string Units = "") : base(Label, ValueFunc, Units) {}
 
+		public abstract double ToDouble();
 		public abstract string ToString(string Format);
 		public abstract string ToSIString(int digits = 3, int MinMagnitude = 0, int MaxMagnitude = int.MaxValue);
 
-		public ushort DoGUIHorizontal(ushort digits)
-		{
+		public abstract string ValueUnitString(string format);
+		
+		public virtual string ValueUnitString(int digits) {
+			return Tools.MuMech_ToSI(this.ToDouble(), digits) + this.Units;
+		}
+
+		public virtual string ValueUnitString(int digits, int MinMagnitude, int MaxMagnitude)
+		{
+			return Tools.MuMech_ToSI(this.ToDouble(), digits, MinMagnitude, MaxMagnitude) + this.Units;
+		}
+
+		public virtual void DoGUIHorizontal(string format)
+		{
+			GUILayout.BeginHorizontal (GUILayout.ExpandWidth (true));
+			GUILayout.Label (this.Label + ":");
+			GUILayout.FlexibleSpace ();
+			GUILayout.Label (this.ValueUnitString(format), GUILayout.ExpandWidth (false));
+			GUILayout.EndHorizontal ();
+		}
+
+		public virtual int DoGUIHorizontal(int digits, bool precisionButton = true)
+		{
+			if (precisionButton)
+			{
+				return this.DoGUIHorizontalPrec(digits);
+			}
+
 			GUILayout.BeginHorizontal(GUILayout.ExpandWidth(true));
 			GUILayout.Label(this.Label + ":", GUILayout.ExpandWidth(true));
 			GUILayout.FlexibleSpace();
-			GUILayout.Label(
-				this.ToSIString(digits),
-				GUILayout.ExpandWidth(false)
-			);
-			if (GUILayout.Button ("P")) {
-				digits = (ushort)((digits + 3) % 15);
+			GUILayout.Label(this.ValueUnitString(digits), GUILayout.ExpandWidth(false));
+			GUILayout.EndHorizontal();
+
+			return digits;
+		}
+
+		public virtual int DoGUIHorizontalPrec(int digits)
+		{
+			float magnitude;
+			float magLimit;
+
+			magnitude = (float)Math.Log10(Math.Abs(this.ToDouble()));
+
+			magLimit = Mathf.Max(magnitude, 6f);
+			magLimit = Mathf.Round((float)Math.Ceiling(magLimit / 3f) * 3f);
+
+			GUILayout.BeginHorizontal(GUILayout.ExpandWidth(true));
+			GUILayout.Label(this.Label + "ⁱ:", GUILayout.ExpandWidth(true));
+			GUILayout.FlexibleSpace();
+
+			GUILayout.Label(this.ValueUnitString(3, int.MinValue, (int)magnitude - digits), GUILayout.ExpandWidth(false));
+			GUILayout.EndHorizontal();
+
+			if (Event.current.type == EventType.mouseUp)
+			{
+				Rect lastRect = GUILayoutUtility.GetLastRect();
+				if (lastRect.Contains(Event.current.mousePosition))
+				{
+					if (Event.current.button == 0)
+					{
+						digits = (digits + 3) % (int)magLimit;
+					}
+					else if (Event.current.button == 1)
+					{
+						digits = (digits - 3) % (int)magLimit;
+						if (digits < 0)
+						{
+							digits = (int)magLimit - 3;
+						}
+					}
+				}
 			}
-			GUILayout.EndHorizontal();
 
 			return digits;
 		}
-		public override void DoGUIHorizontal ()
-		{
-			this.DoGUIHorizontal (3);
-		}
 	}
 
 	public class VOID_DoubleValue : VOID_NumValue<double>, IVOID_NumericValue
 	{
 		public VOID_DoubleValue(string Label, Func<double> ValueFunc, string Units) : base(Label, ValueFunc, Units) {}
+
+		public override double ToDouble ()
+		{
+			return this.Value;
+		}
 
 		public override string ToString(string format)
 		{
@@ -157,6 +217,10 @@
 			);
 		}
 
+		public override string ValueUnitString(string format) {
+			return this.Value.ToString(format) + this.Units;
+		}
+
 		public override string ToSIString(int digits = 3, int MinMagnitude = 0, int MaxMagnitude = int.MaxValue)
 		{
 			return string.Format (
@@ -169,6 +233,15 @@
 	public class VOID_FloatValue : VOID_NumValue<float>, IVOID_NumericValue
 	{
 		public VOID_FloatValue(string Label, Func<float> ValueFunc, string Units) : base(Label, ValueFunc, Units) {}
+
+		public override double ToDouble ()
+		{
+			return (double)this.Value;
+		}
+
+		public override string ValueUnitString(string format) {
+			return this.Value.ToString(format) + this.Units;
+		}
 
 		public override string ToString(string format)
 		{
@@ -193,6 +266,15 @@
 	{
 		public VOID_IntValue(string Label, Func<int> ValueFunc, string Units) : base(Label, ValueFunc, Units) {}
 
+		public override double ToDouble ()
+		{
+			return (double)this.Value;
+		}
+
+		public override string ValueUnitString(string format) {
+			return this.Value.ToString(format) + this.Units;
+		}
+
 		public override string ToString(string format)
 		{
 			return string.Format (
@@ -216,7 +298,7 @@
 
 	public class VOID_StrValue : VOID_DataValue<string>
 	{
-		public VOID_StrValue(string Label, Func<string> ValueFunc, string Units = "") : base(Label, ValueFunc, Units) {}
+		public VOID_StrValue(string Label, Func<string> ValueFunc) : base(Label, ValueFunc, "") {}
 	}
 }