124 lines
3.2 KiB
C#
124 lines
3.2 KiB
C#
///
|
|
/// Copyright (c) 2013-2015 Sensus Metering Systems
|
|
///
|
|
namespace TBF.Boxes
|
|
{
|
|
public class FloatBox : IBox
|
|
{
|
|
/// Box value
|
|
private float val;
|
|
public float Val
|
|
{
|
|
get { return (valid ? val : 0); }
|
|
set { val = value; valid = true; }
|
|
}
|
|
|
|
/// Box name
|
|
string name;
|
|
public string Name
|
|
{
|
|
get { return name; }
|
|
set { name = value; }
|
|
}
|
|
|
|
/// Validity flag: true = the box value is valid
|
|
private bool valid = false;
|
|
public bool Valid
|
|
{
|
|
get { return valid; }
|
|
}
|
|
|
|
/// <summary> Parameterless constructor </summary>
|
|
public FloatBox()
|
|
{
|
|
}
|
|
|
|
/// <summary> Constructor that initializes the value </summary>
|
|
public FloatBox(float val)
|
|
{
|
|
this.Val = val;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Clear the box value and the valid flag
|
|
/// </summary>
|
|
public void Clear()
|
|
{
|
|
val = 0;
|
|
valid = false;
|
|
}
|
|
|
|
///
|
|
/// Specifies a conversion to a string.
|
|
/// In the most complex case the conversion is:
|
|
/// string.Format(FormatEx, val.ToString(Format))
|
|
/// If the value is invalid, the conversion result is 'FormatInvalid'.
|
|
///
|
|
public string Format = null; /// Used as an argument of ToString.) function
|
|
public string FormatEx = "{0}"; /// Used as the first argument of string.Format(...), can be modified to contain units, etc.
|
|
public string FormatInvalid = "---";
|
|
public float Factor = 1.0f; /// Factor is used when converting to/from string by ToString(), UpdateParam() and ValidateParam()
|
|
public float LimitLo = float.MinValue;
|
|
public float LimitHi = float.MaxValue;
|
|
|
|
/// <summary>
|
|
/// Converts the box value to a string representation
|
|
/// </summary>
|
|
public override string ToString()
|
|
{
|
|
if (!valid)
|
|
{
|
|
return FormatInvalid;
|
|
}
|
|
else if (Format == null)
|
|
{
|
|
return string.Format(FormatEx, Val * Factor);
|
|
}
|
|
else
|
|
{
|
|
return string.Format(FormatEx, (Val * Factor).ToString(Format));
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Validate the string representation of the box value
|
|
/// </summary>
|
|
/// <param name="strVal">String representation of the parameter</param>
|
|
/// <returns>true = string is OK, false = string is wrong (see 'message')</returns>
|
|
public bool ValidateParam(string strVal)
|
|
{
|
|
float dummy;
|
|
return Utils.TryParseSFloat(strVal, out dummy) &&
|
|
((dummy / Factor) >= LimitLo) &&
|
|
((dummy / Factor) <= LimitHi);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Update the parameter from a string
|
|
/// </summary>
|
|
/// <param name="strVal">String representation of the box value</param>
|
|
public void UpdateParam(string strVal)
|
|
{
|
|
try
|
|
{
|
|
Val = Utils.ParseSFloat(strVal) / Factor;
|
|
}
|
|
catch { };
|
|
}
|
|
|
|
|
|
public FloatBox Clone()
|
|
{
|
|
FloatBox newFloatBox = new FloatBox();
|
|
newFloatBox.name = name;
|
|
newFloatBox.val = val;
|
|
newFloatBox.valid = valid;
|
|
newFloatBox.Format = Format;
|
|
newFloatBox.FormatInvalid = FormatInvalid;
|
|
newFloatBox.FormatEx = FormatEx;
|
|
newFloatBox.Factor = Factor;
|
|
return newFloatBox;
|
|
}
|
|
}
|
|
}
|