DropShadowTextBlock.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using System.Windows;
  8. using System.Windows.Controls;
  9. using System.Windows.Media;
  10. namespace ClashDotNetFramework.Models.Controls
  11. {
  12. /// <summary>
  13. /// A control that displays text, with a drop shadow.
  14. /// </summary>
  15. public class DropShadowTextBlock : Control
  16. {
  17. /// <summary>
  18. /// The drop shadow color property.
  19. /// </summary>
  20. public static readonly DependencyProperty DropShadowColorProperty = DependencyProperty.Register("DropShadowColor", typeof(Color), typeof(DropShadowTextBlock), new PropertyMetadata(DropShadowColorChanged));
  21. /// <summary>
  22. /// The drop shadow opacity property.
  23. /// </summary>
  24. public static readonly DependencyProperty DropShadowOpacityProperty = DependencyProperty.Register("DropShadowOpacity", typeof(double), typeof(DropShadowTextBlock), new PropertyMetadata(DropShadowOpacityChanged));
  25. /// <summary>
  26. /// The text property.
  27. /// </summary>
  28. public static readonly DependencyProperty TextProperty = DependencyProperty.Register("Text", typeof(string), typeof(DropShadowTextBlock), null);
  29. /// <summary>
  30. /// The text decorations property.
  31. /// </summary>
  32. public static readonly DependencyProperty TextDecorationsProperty = DependencyProperty.Register("TextDecorations", typeof(TextDecorationCollection), typeof(DropShadowTextBlock), null);
  33. /// <summary>
  34. /// The text wrapping property.
  35. /// </summary>
  36. public static readonly DependencyProperty TextWrappingProperty = DependencyProperty.Register("TextWrapping", typeof(TextWrapping), typeof(DropShadowTextBlock), null);
  37. /// <summary>
  38. /// The drop shadow distance property.
  39. /// </summary>
  40. public static readonly DependencyProperty DropShadowDistanceProperty = DependencyProperty.Register("DropShadowDistance", typeof(double), typeof(DropShadowTextBlock), new PropertyMetadata(DropShadowDistanceChanged));
  41. /// <summary>
  42. /// The drop shadow angle property.
  43. /// </summary>
  44. public static readonly DependencyProperty DropShadowAngleProperty = DependencyProperty.Register("DropShadowAngle", typeof(double), typeof(DropShadowTextBlock), new PropertyMetadata(DropShadowAngleChanged));
  45. /// <summary>
  46. /// Stores the drop shadow brush.
  47. /// </summary>
  48. private SolidColorBrush _dropShadowBrush;
  49. /// <summary>
  50. /// Stores the drop shadow translate transform.
  51. /// </summary>
  52. private TranslateTransform _dropShadowTranslate;
  53. /// <summary>
  54. /// DropShadowTextBlock constructor.
  55. /// </summary>
  56. public DropShadowTextBlock()
  57. {
  58. DefaultStyleKey = typeof(DropShadowTextBlock);
  59. }
  60. /// <summary>
  61. /// Gets or sets the drop shadow color.
  62. /// </summary>
  63. [Category("Appearance"), Description("The drop shadow color.")]
  64. public Color DropShadowColor
  65. {
  66. get
  67. {
  68. return (Color)GetValue(DropShadowColorProperty);
  69. }
  70. set
  71. {
  72. SetValue(DropShadowColorProperty, value);
  73. }
  74. }
  75. /// <summary>
  76. /// Gets or sets the drop shadow opacity.
  77. /// </summary>
  78. [Category("Appearance"), Description("The drop shadow opacity.")]
  79. public double DropShadowOpacity
  80. {
  81. get
  82. {
  83. return (double)GetValue(DropShadowOpacityProperty);
  84. }
  85. set
  86. {
  87. SetValue(DropShadowOpacityProperty, value);
  88. }
  89. }
  90. /// <summary>
  91. /// Gets or sets the link text.
  92. /// </summary>
  93. [Category("Common Properties"), Description("The text content.")]
  94. public string Text
  95. {
  96. get
  97. {
  98. return (string)GetValue(TextProperty);
  99. }
  100. set
  101. {
  102. SetValue(TextProperty, value);
  103. }
  104. }
  105. /// <summary>
  106. /// Gets or sets the text decorations.
  107. /// </summary>
  108. [Category("Common Properties"), Description("The text decorations.")]
  109. public TextDecorationCollection TextDecorations
  110. {
  111. get
  112. {
  113. return (TextDecorationCollection)GetValue(TextDecorationsProperty);
  114. }
  115. set
  116. {
  117. SetValue(TextDecorationsProperty, value);
  118. }
  119. }
  120. /// <summary>
  121. /// Gets or sets the text wrapping.
  122. /// </summary>
  123. [Category("Common Properties"), Description("Whether the text wraps.")]
  124. public TextWrapping TextWrapping
  125. {
  126. get
  127. {
  128. return (TextWrapping)GetValue(TextWrappingProperty);
  129. }
  130. set
  131. {
  132. SetValue(TextWrappingProperty, value);
  133. }
  134. }
  135. /// <summary>
  136. /// Gets or sets the drop shadow distance.
  137. /// </summary>
  138. [Category("Appearance"), Description("The drop shadow distance.")]
  139. public double DropShadowDistance
  140. {
  141. get
  142. {
  143. return (double)GetValue(DropShadowDistanceProperty);
  144. }
  145. set
  146. {
  147. SetValue(DropShadowDistanceProperty, value);
  148. }
  149. }
  150. /// <summary>
  151. /// Gets or sets the drop shadow angle.
  152. /// </summary>
  153. [Category("Appearance"), Description("The drop shadow angle.")]
  154. public double DropShadowAngle
  155. {
  156. get
  157. {
  158. return (double)GetValue(DropShadowAngleProperty);
  159. }
  160. set
  161. {
  162. SetValue(DropShadowAngleProperty, value);
  163. }
  164. }
  165. /// <summary>
  166. /// Gets the UI elements out of the template.
  167. /// </summary>
  168. public override void OnApplyTemplate()
  169. {
  170. base.OnApplyTemplate();
  171. _dropShadowTranslate = GetTemplateChild("PART_DropShadowTranslate") as TranslateTransform;
  172. _dropShadowBrush = GetTemplateChild("PART_DropShadowBrush") as SolidColorBrush;
  173. UpdateDropShadowPosition();
  174. UpdateDropShadowBrush();
  175. }
  176. /// <summary>
  177. /// Converts degrees into radians.
  178. /// </summary>
  179. /// <param name="degrees">The degree value.</param>
  180. /// <returns>The degrees as radians.</returns>
  181. private static double DegreesToRadians(double degrees)
  182. {
  183. return degrees * (Math.PI / 180);
  184. }
  185. /// <summary>
  186. /// Gets a point offset by a distance and angle (in degrees).
  187. /// </summary>
  188. /// <param name="angle">The angle in degrees.</param>
  189. /// <param name="distance">The distance.</param>
  190. /// <returns>The offset point.</returns>
  191. private static Point GetOffset(double angle, double distance)
  192. {
  193. double x = Math.Cos(DegreesToRadians(angle)) * distance;
  194. double y = Math.Tan(DegreesToRadians(angle)) * x;
  195. return new Point(x, y);
  196. }
  197. /// <summary>
  198. /// Updates the drop shadow.
  199. /// </summary>
  200. internal void UpdateDropShadowPosition()
  201. {
  202. if (_dropShadowTranslate != null)
  203. {
  204. Point offset = GetOffset(DropShadowAngle, DropShadowDistance);
  205. _dropShadowTranslate.X = offset.X;
  206. _dropShadowTranslate.Y = offset.Y;
  207. }
  208. }
  209. /// <summary>
  210. /// Updates the drop shadow brush.
  211. /// </summary>
  212. internal void UpdateDropShadowBrush()
  213. {
  214. if (_dropShadowBrush != null)
  215. {
  216. _dropShadowBrush.Color = DropShadowColor;
  217. _dropShadowBrush.Opacity = DropShadowOpacity;
  218. }
  219. }
  220. /// <summary>
  221. /// Updates the drop shadow.
  222. /// </summary>
  223. /// <param name="dependencyObject">The drop shadow text block.</param>
  224. /// <param name="eventArgs">Dependency Property Changed Event Args</param>
  225. private static void DropShadowDistanceChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs eventArgs)
  226. {
  227. var dropShadowTextBlock = (DropShadowTextBlock)dependencyObject;
  228. dropShadowTextBlock.UpdateDropShadowPosition();
  229. }
  230. /// <summary>
  231. /// Updates the drop shadow.
  232. /// </summary>
  233. /// <param name="dependencyObject">The drop shadow text block.</param>
  234. /// <param name="eventArgs">Dependency Property Changed Event Args</param>
  235. private static void DropShadowAngleChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs eventArgs)
  236. {
  237. var dropShadowTextBlock = (DropShadowTextBlock)dependencyObject;
  238. dropShadowTextBlock.UpdateDropShadowPosition();
  239. }
  240. /// <summary>
  241. /// Updates the drop shadow.
  242. /// </summary>
  243. /// <param name="dependencyObject">The drop shadow text block.</param>
  244. /// <param name="eventArgs">Dependency Property Changed Event Args</param>
  245. private static void DropShadowColorChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs eventArgs)
  246. {
  247. var dropShadowTextBlock = (DropShadowTextBlock)dependencyObject;
  248. dropShadowTextBlock.UpdateDropShadowBrush();
  249. }
  250. /// <summary>
  251. /// Updates the drop shadow.
  252. /// </summary>
  253. /// <param name="dependencyObject">The drop shadow text block.</param>
  254. /// <param name="eventArgs">Dependency Property Changed Event Args</param>
  255. private static void DropShadowOpacityChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs eventArgs)
  256. {
  257. var dropShadowTextBlock = (DropShadowTextBlock)dependencyObject;
  258. dropShadowTextBlock.UpdateDropShadowBrush();
  259. }
  260. }
  261. }