DFLayoutIntegerFieldProperties.cs 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. [EditorSequence(-995)]
  9. [IntegerEditor]
  10. public override int Default { get; set; }
  11. public DFLayoutIntegerFieldProperties()
  12. {
  13. Format = "";
  14. }
  15. public string Format { get; set; }
  16. public override string FormatValue(int? value)
  17. {
  18. return string.Format("{0" + (string.IsNullOrWhiteSpace(Format) ? "" : ":" + Format) + "}", value);
  19. }
  20. public override int GetValue(int? value)
  21. {
  22. return value ?? Default;
  23. }
  24. public override void SerializeValue(DFSaveStorageEntry entry, int? value)
  25. {
  26. if (value.HasValue)
  27. {
  28. entry.SetValue(value);
  29. }
  30. }
  31. public override int? DeserializeValue(DFLoadStorageEntry entry)
  32. {
  33. var value = entry.GetValue();
  34. if (value is null)
  35. return null;
  36. if (value.GetType().IsNumeric())
  37. {
  38. return Convert.ToInt32(value);
  39. }
  40. if (int.TryParse(value as string, out var result))
  41. return result;
  42. return null;
  43. }
  44. protected override void LoadProperties()
  45. {
  46. base.LoadProperties();
  47. Format = GetProperty("Format", Format);
  48. }
  49. protected override void SaveProperties()
  50. {
  51. base.SaveProperties();
  52. SetProperty("Format", Format);
  53. }
  54. }
  55. }