DFLayoutDoubleFieldProperties.cs 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. using System;
  2. namespace InABox.Core
  3. {
  4. public class DFLayoutDoubleFieldProperties : DFLayoutFieldProperties<double, double?>
  5. {
  6. [EditorSequence(-995)]
  7. [DoubleEditor]
  8. public override double Default { get; set; }
  9. public DFLayoutDoubleFieldProperties()
  10. {
  11. Format = "F2";
  12. }
  13. public string Format { get; set; }
  14. public override string FormatValue(double? value)
  15. {
  16. return string.Format("{0" + (string.IsNullOrWhiteSpace(Format) ? "" : ":" + Format) + "}", value);
  17. }
  18. public override double GetValue(double? value)
  19. {
  20. return value ?? Default;
  21. }
  22. public override void SerializeValue(DFSaveStorageEntry entry, double? value)
  23. {
  24. if(value != null)
  25. {
  26. entry.SetValue(value);
  27. }
  28. }
  29. public override double? DeserializeValue(DFLoadStorageEntry entry)
  30. {
  31. var value = entry.GetValue();
  32. if (value is double d)
  33. return d;
  34. if (value != null && value.GetType().IsNumeric())
  35. {
  36. try
  37. {
  38. return Convert.ToDouble(value);
  39. }
  40. catch (Exception)
  41. {
  42. return null;
  43. }
  44. }
  45. if (double.TryParse(value as string, out var result))
  46. return result;
  47. return null;
  48. }
  49. protected override void LoadProperties()
  50. {
  51. base.LoadProperties();
  52. Format = GetProperty("Format", Format);
  53. }
  54. protected override void SaveProperties()
  55. {
  56. base.SaveProperties();
  57. SetProperty("Format", Format);
  58. }
  59. }
  60. }