DFLayoutTextStyle.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. using System.Drawing;
  2. using System.Text.Json.Serialization;
  3. namespace InABox.Core
  4. {
  5. public enum UnderlineType
  6. {
  7. None,
  8. Single,
  9. Double
  10. }
  11. public interface IDFLayoutTextControl
  12. {
  13. T GetStyleProperty<T>(string property, T defaultValue);
  14. void SetStyleProperty(string property, object? value);
  15. }
  16. public class DFLayoutTextStyle : EnclosedEntity
  17. {
  18. [EditorSequence(0)]
  19. [Caption("Italic", IncludePath = false)]
  20. [CheckBoxEditor]
  21. public bool IsItalic { get; set; } = false;
  22. [EditorSequence(1)]
  23. [CheckBoxEditor]
  24. [Caption("Bold", IncludePath = false)]
  25. public bool IsBold { get; set; } = false;
  26. [EditorSequence(2)]
  27. [EnumLookupEditor(typeof(UnderlineType))]
  28. [Caption("Underline", IncludePath = false)]
  29. public UnderlineType Underline { get; set; } = UnderlineType.None;
  30. [EditorSequence(3)]
  31. [Caption("Font Size", IncludePath = false)]
  32. [Comment("Font size in points.")]
  33. [DoubleEditor(ToolTip = "Font size in points. Set to 0 for the default size.")]
  34. public double FontSize { get; set; } = 0;
  35. [EditorSequence(4)]
  36. [Caption("Text Colour", IncludePath = false)]
  37. [ColorEditor]
  38. public string Foreground { get; set; } = "";
  39. [EditorSequence(5)]
  40. [Caption("Background Colour", IncludePath = false)]
  41. [ColorEditor]
  42. public string Background { get; set; } = "";
  43. [EditorSequence(6)]
  44. [Caption("Horz Alignment", IncludePath = false)]
  45. [EnumLookupEditor(typeof(DFLayoutAlignment))]
  46. public DFLayoutAlignment HorizontalTextAlignment { get; set; } = DFLayoutAlignment.Start;
  47. [EditorSequence(7)]
  48. [Caption("Vertical Alignment", IncludePath = false)]
  49. [EnumLookupEditor(typeof(DFLayoutAlignment))]
  50. public DFLayoutAlignment VerticalTextAlignment { get; set; } = DFLayoutAlignment.Middle;
  51. [EditorSequence(8)]
  52. [Caption("Allow Wrap?", IncludePath = false)]
  53. [CheckBoxEditor]
  54. public bool TextWrapping { get; set; } = true;
  55. [JsonIgnore]
  56. [NullEditor]
  57. [DoNotSerialize]
  58. public Color ForegroundColour { get => GetForegroundColour(); set => SetForegroundColour(value); }
  59. [JsonIgnore]
  60. [NullEditor]
  61. [DoNotSerialize]
  62. public Color BackgroundColour { get => GetBackgroundColour(); set => SetBackgroundColour(value); }
  63. public void LoadProperties(IDFLayoutTextControl control)
  64. {
  65. IsItalic = control.GetStyleProperty(nameof(IsItalic), false);
  66. IsBold = control.GetStyleProperty(nameof(IsBold), false);
  67. Underline = control.GetStyleProperty(nameof(Underline), UnderlineType.None);
  68. Foreground = control.GetStyleProperty(nameof(Foreground), "");
  69. Background = control.GetStyleProperty(nameof(Background), "");
  70. FontSize = control.GetStyleProperty(nameof(FontSize), 0.0);
  71. TextWrapping = control.GetStyleProperty(nameof(TextWrapping), true);
  72. HorizontalTextAlignment = control.GetStyleProperty(nameof(HorizontalTextAlignment), DFLayoutAlignment.Start);
  73. VerticalTextAlignment = control.GetStyleProperty(nameof(VerticalTextAlignment), DFLayoutAlignment.Middle);
  74. }
  75. public void SaveProperties(IDFLayoutTextControl control)
  76. {
  77. control.SetStyleProperty(nameof(IsItalic), IsItalic);
  78. control.SetStyleProperty(nameof(IsBold), IsBold);
  79. control.SetStyleProperty(nameof(Underline), Underline);
  80. control.SetStyleProperty(nameof(Foreground), Foreground);
  81. control.SetStyleProperty(nameof(Background), Background);
  82. control.SetStyleProperty(nameof(FontSize), FontSize);
  83. control.SetStyleProperty(nameof(TextWrapping), TextWrapping);
  84. control.SetStyleProperty(nameof(HorizontalTextAlignment), HorizontalTextAlignment);
  85. control.SetStyleProperty(nameof(VerticalTextAlignment), VerticalTextAlignment);
  86. }
  87. public Color GetForegroundColour() => ColourFromString(Foreground);
  88. public Color GetBackgroundColour() => ColourFromString(Background);
  89. public string SetForegroundColour(Color colour) => Foreground = ColourToString(colour);
  90. public string SetBackgroundColour(Color colour) => Background = ColourToString(colour);
  91. public static Color ColourFromString(string colour) => string.IsNullOrWhiteSpace(colour)
  92. ? Color.Empty
  93. : DFLayoutUtils.ConvertObjectToColour(colour) ?? Color.Empty;
  94. public static string ColourToString(Color colour) => colour == Color.Empty
  95. ? ""
  96. : colour.IsKnownColor ? colour.ToKnownColor().ToString() : $"#{colour.ToArgb():X4}";
  97. }
  98. }