DFLayoutTextStyle.cs 5.1 KB

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