DFLayoutTextStyle.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. [DoubleEditor(ToolTip = "Font size in points. Set to 0 for the default size.")]
  36. public double FontSize { get; set; } = 0;
  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; } = DFLayoutAlignment.Start;
  49. [EditorSequence(7)]
  50. [Caption("Vertical Alignment", IncludePath = false)]
  51. [EnumLookupEditor(typeof(DFLayoutAlignment))]
  52. public DFLayoutAlignment VerticalTextAlignment { get; set; } = DFLayoutAlignment.Middle;
  53. [EditorSequence(8)]
  54. [Caption("Allow Wrap?", IncludePath = false)]
  55. [CheckBoxEditor]
  56. public bool TextWrapping { get; set; } = true;
  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. public void LoadProperties(IDFLayoutTextControl control)
  66. {
  67. IsItalic = control.GetStyleProperty(nameof(IsItalic), false);
  68. IsBold = control.GetStyleProperty(nameof(IsBold), false);
  69. Underline = control.GetStyleProperty(nameof(Underline), UnderlineType.None);
  70. Foreground = control.GetStyleProperty(nameof(Foreground), "");
  71. Background = control.GetStyleProperty(nameof(Background), "");
  72. FontSize = control.GetStyleProperty(nameof(FontSize), 0.0);
  73. TextWrapping = control.GetStyleProperty(nameof(TextWrapping), true);
  74. HorizontalTextAlignment = control.GetStyleProperty(nameof(HorizontalTextAlignment), DFLayoutAlignment.Start);
  75. VerticalTextAlignment = control.GetStyleProperty(nameof(VerticalTextAlignment), DFLayoutAlignment.Middle);
  76. }
  77. public void SaveProperties(IDFLayoutTextControl control)
  78. {
  79. control.SetStyleProperty(nameof(IsItalic), IsItalic);
  80. control.SetStyleProperty(nameof(IsBold), IsBold);
  81. control.SetStyleProperty(nameof(Underline), Underline);
  82. control.SetStyleProperty(nameof(Foreground), Foreground);
  83. control.SetStyleProperty(nameof(Background), Background);
  84. control.SetStyleProperty(nameof(FontSize), FontSize);
  85. control.SetStyleProperty(nameof(TextWrapping), TextWrapping);
  86. control.SetStyleProperty(nameof(HorizontalTextAlignment), HorizontalTextAlignment);
  87. control.SetStyleProperty(nameof(VerticalTextAlignment), VerticalTextAlignment);
  88. }
  89. public Color GetForegroundColour() => ColourFromString(Foreground);
  90. public Color GetBackgroundColour() => ColourFromString(Background);
  91. public string SetForegroundColour(Color colour) => Foreground = ColourToString(colour);
  92. public string SetBackgroundColour(Color colour) => Background = ColourToString(colour);
  93. public static Color ColourFromString(string colour) => string.IsNullOrWhiteSpace(colour)
  94. ? Color.Empty
  95. : DFLayoutUtils.ConvertObjectToColour(colour) ?? Color.Empty;
  96. public static string ColourToString(Color colour) => colour == Color.Empty
  97. ? ""
  98. : colour.IsKnownColor ? colour.ToKnownColor().ToString() : $"#{colour.ToArgb():X4}";
  99. }
  100. }