VOID_DataValue: Some extra methods to make floating point ToString methods accessible.
[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
@@ -67,7 +69,6 @@
 			this.Label = Label;
 			this.Units = Units;
 			this.ValueFunc = ValueFunc;
-			this.cache = this.ValueFunc.Invoke ();
 		}
 
 		public void Refresh()
@@ -107,6 +108,7 @@
 
 	internal interface IVOID_NumericValue
 	{
+		double ToDouble();
 		string ToString(string format);
 		string ToSIString(int digits, int MinMagnitude, int MaxMagnitude);
 	}
@@ -115,11 +117,20 @@
 	{
 		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 abstract string ValueUnitString(string format);
-		public abstract string ValueUnitString(ushort digits);
+		
+		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)
 		{
@@ -130,16 +141,58 @@
 			GUILayout.EndHorizontal ();
 		}
 
-		public virtual ushort DoGUIHorizontal(ushort digits)
-		{
+		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.ValueUnitString(digits), GUILayout.ExpandWidth(false));
-			if (GUILayout.Button ("P")) {
-				digits = (ushort)((digits + 3) % 15);
+			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;
 		}
@@ -148,6 +201,11 @@
 	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)
 		{
@@ -163,10 +221,6 @@
 			return this.Value.ToString(format) + this.Units;
 		}
 
-		public override string ValueUnitString(ushort digits) {
-			return Tools.MuMech_ToSI(this.Value, digits) + this.Units;
-		}
-
 		public override string ToSIString(int digits = 3, int MinMagnitude = 0, int MaxMagnitude = int.MaxValue)
 		{
 			return string.Format (
@@ -179,13 +233,14 @@
 	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 ValueUnitString(ushort digits) {
-			return Tools.MuMech_ToSI((double)this.Value, digits) + this.Units;
 		}
 
 		public override string ToString(string format)
@@ -210,13 +265,14 @@
 	public class VOID_IntValue : VOID_NumValue<int>, IVOID_NumericValue
 	{
 		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 ValueUnitString(ushort digits) {
-			return Tools.MuMech_ToSI((double)this.Value, digits) + this.Units;
 		}
 
 		public override string ToString(string format)