DFLayoutIntegerFieldProperties.cs 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. using Newtonsoft.Json.Linq;
  2. using Newtonsoft.Json.Schema;
  3. using System;
  4. namespace InABox.Core
  5. {
  6. public class DFLayoutIntegerFieldProperties : DFLayoutFieldProperties<int, int?>
  7. {
  8. public DFLayoutIntegerFieldProperties()
  9. {
  10. Format = "";
  11. }
  12. public string Format { get; set; }
  13. public override string FormatValue(int? value)
  14. {
  15. return string.Format("{0" + (string.IsNullOrWhiteSpace(Format) ? "" : ":" + Format) + "}", value);
  16. }
  17. public override int GetValue(int? value)
  18. {
  19. return value ?? Default;
  20. }
  21. public override void SerializeValue(DFSaveStorageEntry entry, int? value)
  22. {
  23. if (value.HasValue)
  24. {
  25. entry.SetValue(value);
  26. }
  27. }
  28. public override int? DeserializeValue(DFLoadStorageEntry entry)
  29. {
  30. var value = entry.GetValue();
  31. if (value is null)
  32. return null;
  33. if (value.GetType().IsNumeric())
  34. {
  35. return Convert.ToInt32(value);
  36. }
  37. if (int.TryParse(value as string, out var result))
  38. return result;
  39. return null;
  40. }
  41. protected override void LoadProperties()
  42. {
  43. base.LoadProperties();
  44. Format = GetProperty("Format", Format);
  45. }
  46. protected override void SaveProperties()
  47. {
  48. base.SaveProperties();
  49. SetProperty("Format", Format);
  50. }
  51. }
  52. }