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
// //
// VOID_DataValue.cs // VOID_DataValue.cs
// //
// Author: // Author:
// toadicus <> // toadicus <>
// //
// Copyright (c) 2013 toadicus // Copyright (c) 2013 toadicus
// //
// This program is free software: you can redistribute it and/or modify // This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by // it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or // the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version. // (at your option) any later version.
// //
// This program is distributed in the hope that it will be useful, // This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of // but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details. // GNU General Public License for more details.
// //
// You should have received a copy of the GNU General Public License // You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>. // along with this program. If not, see <http://www.gnu.org/licenses/>.
using System; using System;
using UnityEngine; using UnityEngine;
   
namespace VOID namespace VOID
{ {
public interface IVOID_DataValue public interface IVOID_DataValue
{ {
void Refresh(); void Refresh();
string ValueUnitString(); string ValueUnitString();
void DoGUIHorizontal(); void DoGUIHorizontal();
} }
   
public class VOID_DataValue<T> : IVOID_DataValue public class VOID_DataValue<T> : IVOID_DataValue
{ {
/* /*
* Static Members * Static Members
* */ * */
public static implicit operator T(VOID_DataValue<T> v) public static implicit operator T(VOID_DataValue<T> v)
{ {
return (T)v.Value; return (T)v.Value;
} }
   
/* /*
* Instance Members * Instance Members
* */ * */
/* /*
* Fields * Fields
* */ * */
protected T cache; protected T cache;
protected Func<T> ValueFunc; protected Func<T> ValueFunc;
   
/* /*
* Properties * Properties
* */ * */
public string Label { get; protected set; } public string Label { get; protected set; }
public string Units { get; protected set; } public string Units { get; protected set; }
   
public T Value { public T Value {
get { get {
return (T)this.cache; return (T)this.cache;
} }
} }
   
/* /*
* Methods * Methods
* */ * */
public VOID_DataValue(string Label, Func<T> ValueFunc, string Units = "") public VOID_DataValue(string Label, Func<T> ValueFunc, string Units = "")
{ {
this.Label = Label; this.Label = Label;
this.Units = Units; this.Units = Units;
this.ValueFunc = ValueFunc; this.ValueFunc = ValueFunc;
} }
   
public void Refresh() public void Refresh()
{ {
this.cache = this.ValueFunc.Invoke (); this.cache = this.ValueFunc.Invoke ();
} }
   
public T GetFreshValue() public T GetFreshValue()
{ {
this.Refresh (); this.Refresh ();
return (T)this.cache; return (T)this.cache;
} }
   
public string ValueUnitString() { public string ValueUnitString() {
return this.Value.ToString() + this.Units; return this.Value.ToString() + this.Units;
} }
   
public virtual void DoGUIHorizontal() public virtual void DoGUIHorizontal()
{ {
GUILayout.BeginHorizontal (GUILayout.ExpandWidth (true)); GUILayout.BeginHorizontal (GUILayout.ExpandWidth (true));
GUILayout.Label (this.Label + ":"); GUILayout.Label (this.Label + ":");
GUILayout.FlexibleSpace (); GUILayout.FlexibleSpace ();
GUILayout.Label (this.ValueUnitString(), GUILayout.ExpandWidth (false)); GUILayout.Label (this.ValueUnitString(), GUILayout.ExpandWidth (false));
GUILayout.EndHorizontal (); GUILayout.EndHorizontal ();
} }
   
public override string ToString() public override string ToString()
{ {
return string.Format ( return string.Format (
"{0}: {1}{2}", "{0}: {1}{2}",
this.Label, this.Label,
this.Value.ToString (), this.Value.ToString (),
this.Units this.Units
); );
} }
} }
   
internal interface IVOID_NumericValue internal interface IVOID_NumericValue
{ {
  double ToDouble();
string ToString(string format); string ToString(string format);
string ToSIString(int digits, int MinMagnitude, int MaxMagnitude); string ToSIString(int digits, int MinMagnitude, int MaxMagnitude);
} }
   
public abstract class VOID_NumValue<T> : VOID_DataValue<T>, IVOID_NumericValue public abstract class VOID_NumValue<T> : VOID_DataValue<T>, IVOID_NumericValue
{ {
public VOID_NumValue(string Label, Func<T> ValueFunc, string Units = "") : base(Label, ValueFunc, Units) {} 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 ToString(string Format);
public abstract string ToSIString(int digits = 3, int MinMagnitude = 0, int MaxMagnitude = int.MaxValue); public abstract string ToSIString(int digits = 3, int MinMagnitude = 0, int MaxMagnitude = int.MaxValue);
   
public abstract string ValueUnitString(string format); public abstract string ValueUnitString(string format);
public abstract string ValueUnitString(ushort digits); public abstract string ValueUnitString(ushort digits);
   
public virtual void DoGUIHorizontal(string format) public virtual void DoGUIHorizontal(string format)
{ {
GUILayout.BeginHorizontal (GUILayout.ExpandWidth (true)); GUILayout.BeginHorizontal (GUILayout.ExpandWidth (true));
GUILayout.Label (this.Label + ":"); GUILayout.Label (this.Label + ":");
GUILayout.FlexibleSpace (); GUILayout.FlexibleSpace ();
GUILayout.Label (this.ValueUnitString(format), GUILayout.ExpandWidth (false)); GUILayout.Label (this.ValueUnitString(format), GUILayout.ExpandWidth (false));
GUILayout.EndHorizontal (); GUILayout.EndHorizontal ();
} }
   
public virtual ushort DoGUIHorizontal(ushort digits, bool precisionButton = true) public virtual ushort DoGUIHorizontal(ushort digits, bool precisionButton = true)
{ {
GUILayout.BeginHorizontal(GUILayout.ExpandWidth(true)); GUILayout.BeginHorizontal(GUILayout.ExpandWidth(true));
GUILayout.Label(this.Label + ":", GUILayout.ExpandWidth(true)); GUILayout.Label(this.Label + ":", GUILayout.ExpandWidth(true));
GUILayout.FlexibleSpace(); GUILayout.FlexibleSpace();
GUILayout.Label(this.ValueUnitString(digits), GUILayout.ExpandWidth(false)); GUILayout.Label(this.ValueUnitString(digits), GUILayout.ExpandWidth(false));
if (precisionButton) if (precisionButton)
{ {
if (GUILayout.Button ("P")) if (GUILayout.Button (digits.ToString()))
{ {
digits = (ushort)((digits + 3) % 15); 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);
} }
} }
GUILayout.EndHorizontal(); GUILayout.EndHorizontal();
   
return digits; return digits;
} }
} }
   
public class VOID_DoubleValue : VOID_NumValue<double>, IVOID_NumericValue public class VOID_DoubleValue : VOID_NumValue<double>, IVOID_NumericValue
{ {
public VOID_DoubleValue(string Label, Func<double> ValueFunc, string Units) : base(Label, ValueFunc, Units) {} 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) public override string ToString(string format)
{ {
return string.Format ( return string.Format (
"{0}: {1}{2}", "{0}: {1}{2}",
this.Label, this.Label,
this.Value.ToString (format), this.Value.ToString (format),
this.Units this.Units
); );
} }
   
public override string ValueUnitString(string format) { public override string ValueUnitString(string format) {
return this.Value.ToString(format) + this.Units; return this.Value.ToString(format) + this.Units;
} }
   
public override string ValueUnitString(ushort digits) { public override string ValueUnitString(ushort digits) {
return Tools.MuMech_ToSI(this.Value, digits) + this.Units; 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) public override string ToSIString(int digits = 3, int MinMagnitude = 0, int MaxMagnitude = int.MaxValue)
{ {
return string.Format ( return string.Format (
"{0}{1}", "{0}{1}",
Tools.MuMech_ToSI (this.Value, digits, MinMagnitude, MaxMagnitude), Tools.MuMech_ToSI (this.Value, digits, MinMagnitude, MaxMagnitude),
this.Units this.Units
); );
} }
} }
public class VOID_FloatValue : VOID_NumValue<float>, IVOID_NumericValue public class VOID_FloatValue : VOID_NumValue<float>, IVOID_NumericValue
{ {
public VOID_FloatValue(string Label, Func<float> ValueFunc, string Units) : base(Label, ValueFunc, Units) {} 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) { public override string ValueUnitString(string format) {
return this.Value.ToString(format) + this.Units; return this.Value.ToString(format) + this.Units;
} }
   
public override string ValueUnitString(ushort digits) { public override string ValueUnitString(ushort digits) {
return Tools.MuMech_ToSI((double)this.Value, digits) + this.Units; 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) public override string ToString(string format)
{ {
return string.Format ( return string.Format (
"{0}: {1}{2}", "{0}: {1}{2}",
this.Label, this.Label,
this.Value.ToString (format), this.Value.ToString (format),
this.Units this.Units
); );
} }
   
public override string ToSIString(int digits = 3, int MinMagnitude = 0, int MaxMagnitude = int.MaxValue) public override string ToSIString(int digits = 3, int MinMagnitude = 0, int MaxMagnitude = int.MaxValue)
{ {
return string.Format ( return string.Format (
"{0}{1}", "{0}{1}",
Tools.MuMech_ToSI ((double)this.Value, digits, MinMagnitude, MaxMagnitude), Tools.MuMech_ToSI ((double)this.Value, digits, MinMagnitude, MaxMagnitude),
this.Units this.Units
); );
} }
} }
public class VOID_IntValue : VOID_NumValue<int>, IVOID_NumericValue public class VOID_IntValue : VOID_NumValue<int>, IVOID_NumericValue
{ {
public VOID_IntValue(string Label, Func<int> ValueFunc, string Units) : base(Label, ValueFunc, Units) {} 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) { public override string ValueUnitString(string format) {
return this.Value.ToString(format) + this.Units; return this.Value.ToString(format) + this.Units;
} }
   
public override string ValueUnitString(ushort digits) { public override string ValueUnitString(ushort digits) {
return Tools.MuMech_ToSI((double)this.Value, digits) + this.Units; 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) public override string ToString(string format)
{ {
return string.Format ( return string.Format (
"{0}: {1}{2}", "{0}: {1}{2}",
this.Label, this.Label,
this.Value.ToString (format), this.Value.ToString (format),
this.Units this.Units
); );
} }
   
public override string ToSIString(int digits = 3, int MinMagnitude = 0, int MaxMagnitude = int.MaxValue) public override string ToSIString(int digits = 3, int MinMagnitude = 0, int MaxMagnitude = int.MaxValue)
{ {
return string.Format ( return string.Format (
"{0}{1}", "{0}{1}",
Tools.MuMech_ToSI ((double)this.Value, digits, MinMagnitude, MaxMagnitude), Tools.MuMech_ToSI ((double)this.Value, digits, MinMagnitude, MaxMagnitude),
this.Units this.Units
); );
} }
} }
   
   
public class VOID_StrValue : VOID_DataValue<string> public class VOID_StrValue : VOID_DataValue<string>
{ {
public VOID_StrValue(string Label, Func<string> ValueFunc) : base(Label, ValueFunc, "") {} public VOID_StrValue(string Label, Func<string> ValueFunc) : base(Label, ValueFunc, "") {}
} }
} }