DFLayoutTextStyle.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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; } = false;
  25. [EditorSequence(1)]
  26. [CheckBoxEditor]
  27. [Caption("Bold", IncludePath = false)]
  28. public bool IsBold { get; set; } = false;
  29. [EditorSequence(2)]
  30. [EnumLookupEditor(typeof(UnderlineType))]
  31. [Caption("Underline", IncludePath = false)]
  32. public UnderlineType Underline { get; set; } = UnderlineType.None;
  33. [EditorSequence(3)]
  34. [Caption("Font Size", IncludePath = false)]
  35. [Comment("Font size in points.")]
  36. [DoubleEditor(ToolTip = "Font size in points. Set to 0 for the default size.")]
  37. public double FontSize { get; set; } = 0;
  38. [EditorSequence(4)]
  39. [Caption("Text Colour", IncludePath = false)]
  40. [ColorEditor]
  41. public string Foreground { get; set; } = "";
  42. [EditorSequence(5)]
  43. [Caption("Background Colour", IncludePath = false)]
  44. [ColorEditor]
  45. public string Background { get; set; } = "";
  46. [EditorSequence(6)]
  47. [Caption("Horz Alignment", IncludePath = false)]
  48. [EnumLookupEditor(typeof(DFLayoutAlignment))]
  49. public DFLayoutAlignment HorizontalTextAlignment { get; set; } = DFLayoutAlignment.Start;
  50. [EditorSequence(7)]
  51. [Caption("Vertical Alignment", IncludePath = false)]
  52. [EnumLookupEditor(typeof(DFLayoutAlignment))]
  53. public DFLayoutAlignment VerticalTextAlignment { get; set; } = DFLayoutAlignment.Middle;
  54. [EditorSequence(8)]
  55. [Caption("Allow Wrap?", IncludePath = false)]
  56. [CheckBoxEditor]
  57. public bool TextWrapping { get; set; } = true;
  58. [JsonIgnore]
  59. [NullEditor]
  60. [DoNotSerialize]
  61. public Color ForegroundColour { get => GetForegroundColour(); set => SetForegroundColour(value); }
  62. [JsonIgnore]
  63. [NullEditor]
  64. [DoNotSerialize]
  65. public Color BackgroundColour { get => GetBackgroundColour(); set => SetBackgroundColour(value); }
  66. public void LoadProperties(IDFLayoutTextControl control)
  67. {
  68. IsItalic = control.GetStyleProperty(nameof(IsItalic), false);
  69. IsBold = control.GetStyleProperty(nameof(IsBold), false);
  70. Underline = control.GetStyleProperty(nameof(Underline), UnderlineType.None);
  71. Foreground = control.GetStyleProperty(nameof(Foreground), "");
  72. Background = control.GetStyleProperty(nameof(Background), "");
  73. FontSize = control.GetStyleProperty(nameof(FontSize), 0.0);
  74. TextWrapping = control.GetStyleProperty(nameof(TextWrapping), true);
  75. HorizontalTextAlignment = control.GetStyleProperty(nameof(HorizontalTextAlignment), DFLayoutAlignment.Start);
  76. VerticalTextAlignment = control.GetStyleProperty(nameof(VerticalTextAlignment), DFLayoutAlignment.Middle);
  77. }
  78. public void SaveProperties(IDFLayoutTextControl control)
  79. {
  80. control.SetStyleProperty(nameof(IsItalic), IsItalic);
  81. control.SetStyleProperty(nameof(IsBold), IsBold);
  82. control.SetStyleProperty(nameof(Underline), Underline);
  83. control.SetStyleProperty(nameof(Foreground), Foreground);
  84. control.SetStyleProperty(nameof(Background), Background);
  85. control.SetStyleProperty(nameof(FontSize), FontSize);
  86. control.SetStyleProperty(nameof(TextWrapping), TextWrapping);
  87. control.SetStyleProperty(nameof(HorizontalTextAlignment), HorizontalTextAlignment);
  88. control.SetStyleProperty(nameof(VerticalTextAlignment), VerticalTextAlignment);
  89. }
  90. public Color GetForegroundColour() => ColourFromString(Foreground);
  91. public Color GetBackgroundColour() => ColourFromString(Background);
  92. public string SetForegroundColour(Color colour) => Foreground = ColourToString(colour);
  93. public string SetBackgroundColour(Color colour) => Background = ColourToString(colour);
  94. public static Color ColourFromString(string colour) => string.IsNullOrWhiteSpace(colour)
  95. ? Color.Empty
  96. : DFLayoutUtils.ConvertObjectToColour(colour) ?? Color.Empty;
  97. public static string ColourToString(Color colour) => colour == Color.Empty
  98. ? ""
  99. : colour.IsKnownColor ? colour.ToKnownColor().ToString() : $"#{colour.ToArgb():X4}";
  100. }
  101. }