DFLayoutDoubleFieldProperties.cs 1.8 KB

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