VOID_DataValue: Changed the precision configurable to tweak the units and the decimal position rather than just add more digits after the decimal.
[VOID.git] / VOID_DataValue.cs
blob:a/VOID_DataValue.cs -> blob:b/VOID_DataValue.cs
--- a/VOID_DataValue.cs
+++ b/VOID_DataValue.cs
@@ -122,7 +122,15 @@
 		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)
 		{
@@ -133,22 +141,58 @@
 			GUILayout.EndHorizontal ();
 		}
 
-		public virtual ushort DoGUIHorizontal(ushort digits, bool precisionButton = true)
-		{
+		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 (precisionButton)
+			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)
 			{
-				if (GUILayout.Button (digits.ToString()))
+				Rect lastRect = GUILayoutUtility.GetLastRect();
+				if (lastRect.Contains(Event.current.mousePosition))
 				{
-					float magnitude = Math.Max((float)Math.Log10(Math.Abs(this.ToDouble())), 6f);
-					magnitude = (float)Math.Ceiling (magnitude / 3f) * 3f;
-					digits = (ushort)((digits + 3) % (int)magnitude);
+					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;
 		}
@@ -177,11 +221,6 @@
 			return this.Value.ToString(format) + this.Units;
 		}
 
-		public override string ValueUnitString(ushort digits) {
-			int magnitude = (int)Math.Log10(Math.Abs(this.Value));
-			return Tools.MuMech_ToSI(this.Value, 2, int.MinValue, magnitude - digits) + this.Units;
-		}
-
 		public override string ToSIString(int digits = 3, int MinMagnitude = 0, int MaxMagnitude = int.MaxValue)
 		{
 			return string.Format (
@@ -202,11 +241,6 @@
 
 		public override string ValueUnitString(string format) {
 			return this.Value.ToString(format) + this.Units;
-		}
-
-		public override string ValueUnitString(ushort digits) {
-			int magnitude = Math.Max((int)Math.Log10(Math.Abs(this.Value)), 0);
-			return Tools.MuMech_ToSI(this.Value, 2, int.MinValue, magnitude - digits) + this.Units;
 		}
 
 		public override string ToString(string format)
@@ -241,11 +275,6 @@
 			return this.Value.ToString(format) + this.Units;
 		}
 
-		public override string ValueUnitString(ushort digits) {
-			int magnitude = (int)Math.Log10(Math.Abs(this.Value));
-			return Tools.MuMech_ToSI(this.Value, 2, int.MinValue, magnitude - digits) + this.Units;
-		}
-
 		public override string ToString(string format)
 		{
 			return string.Format (