DFLayoutTextStyle.cs 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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 class DFLayoutTextStyle : EnclosedEntity
  15. {
  16. [EditorSequence(0)]
  17. [CheckBoxEditor]
  18. public bool IsItalic { get; set; }
  19. [EditorSequence(1)]
  20. [CheckBoxEditor]
  21. public bool IsBold { get; set; }
  22. [EditorSequence(2)]
  23. [EnumLookupEditor(typeof(UnderlineType))]
  24. public UnderlineType Underline { get; set; }
  25. [EditorSequence(3)]
  26. [DoubleEditor(Caption = "Font size in points. Set to 0 for the default size.")]
  27. public double FontSize { get; set; }
  28. [EditorSequence(4)]
  29. [ColorEditor]
  30. public string Foreground { get; set; }
  31. [EditorSequence(5)]
  32. [ColorEditor]
  33. public string Background { get; set; }
  34. [JsonIgnore]
  35. [NullEditor]
  36. public Color ForegroundColour { get => GetForegroundColour(); set => SetForegroundColour(value); }
  37. [JsonIgnore]
  38. [NullEditor]
  39. public Color BackgroundColour { get => GetBackgroundColour(); set => SetBackgroundColour(value); }
  40. protected override void Init()
  41. {
  42. base.Init();
  43. IsItalic = false;
  44. IsBold = false;
  45. Underline = UnderlineType.None;
  46. Foreground = "";
  47. Background = "";
  48. FontSize = 0;
  49. }
  50. public Color GetForegroundColour() => ColourFromString(Foreground);
  51. public Color GetBackgroundColour() => ColourFromString(Background);
  52. public string SetForegroundColour(Color colour) => Foreground = ColourToString(colour);
  53. public string SetBackgroundColour(Color colour) => Background = ColourToString(colour);
  54. public static Color ColourFromString(string colour) => string.IsNullOrWhiteSpace(colour)
  55. ? Color.Empty
  56. : DFLayoutUtils.ConvertObjectToColour(colour) ?? Color.Empty;
  57. public static string ColourToString(Color colour) => colour == Color.Empty
  58. ? ""
  59. : colour.IsKnownColor ? colour.ToString() : $"#{colour.ToArgb():X4}";
  60. }
  61. }