DFLayoutIntegerFieldProperties.cs 1.6 KB

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