| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288 |
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Media;
- namespace ClashDotNetFramework.Models.Controls
- {
- /// <summary>
- /// A control that displays text, with a drop shadow.
- /// </summary>
- public class DropShadowTextBlock : Control
- {
- /// <summary>
- /// The drop shadow color property.
- /// </summary>
- public static readonly DependencyProperty DropShadowColorProperty = DependencyProperty.Register("DropShadowColor", typeof(Color), typeof(DropShadowTextBlock), new PropertyMetadata(DropShadowColorChanged));
- /// <summary>
- /// The drop shadow opacity property.
- /// </summary>
- public static readonly DependencyProperty DropShadowOpacityProperty = DependencyProperty.Register("DropShadowOpacity", typeof(double), typeof(DropShadowTextBlock), new PropertyMetadata(DropShadowOpacityChanged));
- /// <summary>
- /// The text property.
- /// </summary>
- public static readonly DependencyProperty TextProperty = DependencyProperty.Register("Text", typeof(string), typeof(DropShadowTextBlock), null);
- /// <summary>
- /// The text decorations property.
- /// </summary>
- public static readonly DependencyProperty TextDecorationsProperty = DependencyProperty.Register("TextDecorations", typeof(TextDecorationCollection), typeof(DropShadowTextBlock), null);
- /// <summary>
- /// The text wrapping property.
- /// </summary>
- public static readonly DependencyProperty TextWrappingProperty = DependencyProperty.Register("TextWrapping", typeof(TextWrapping), typeof(DropShadowTextBlock), null);
- /// <summary>
- /// The drop shadow distance property.
- /// </summary>
- public static readonly DependencyProperty DropShadowDistanceProperty = DependencyProperty.Register("DropShadowDistance", typeof(double), typeof(DropShadowTextBlock), new PropertyMetadata(DropShadowDistanceChanged));
- /// <summary>
- /// The drop shadow angle property.
- /// </summary>
- public static readonly DependencyProperty DropShadowAngleProperty = DependencyProperty.Register("DropShadowAngle", typeof(double), typeof(DropShadowTextBlock), new PropertyMetadata(DropShadowAngleChanged));
- /// <summary>
- /// Stores the drop shadow brush.
- /// </summary>
- private SolidColorBrush _dropShadowBrush;
- /// <summary>
- /// Stores the drop shadow translate transform.
- /// </summary>
- private TranslateTransform _dropShadowTranslate;
- /// <summary>
- /// DropShadowTextBlock constructor.
- /// </summary>
- public DropShadowTextBlock()
- {
- DefaultStyleKey = typeof(DropShadowTextBlock);
- }
- /// <summary>
- /// Gets or sets the drop shadow color.
- /// </summary>
- [Category("Appearance"), Description("The drop shadow color.")]
- public Color DropShadowColor
- {
- get
- {
- return (Color)GetValue(DropShadowColorProperty);
- }
- set
- {
- SetValue(DropShadowColorProperty, value);
- }
- }
- /// <summary>
- /// Gets or sets the drop shadow opacity.
- /// </summary>
- [Category("Appearance"), Description("The drop shadow opacity.")]
- public double DropShadowOpacity
- {
- get
- {
- return (double)GetValue(DropShadowOpacityProperty);
- }
- set
- {
- SetValue(DropShadowOpacityProperty, value);
- }
- }
- /// <summary>
- /// Gets or sets the link text.
- /// </summary>
- [Category("Common Properties"), Description("The text content.")]
- public string Text
- {
- get
- {
- return (string)GetValue(TextProperty);
- }
- set
- {
- SetValue(TextProperty, value);
- }
- }
- /// <summary>
- /// Gets or sets the text decorations.
- /// </summary>
- [Category("Common Properties"), Description("The text decorations.")]
- public TextDecorationCollection TextDecorations
- {
- get
- {
- return (TextDecorationCollection)GetValue(TextDecorationsProperty);
- }
- set
- {
- SetValue(TextDecorationsProperty, value);
- }
- }
- /// <summary>
- /// Gets or sets the text wrapping.
- /// </summary>
- [Category("Common Properties"), Description("Whether the text wraps.")]
- public TextWrapping TextWrapping
- {
- get
- {
- return (TextWrapping)GetValue(TextWrappingProperty);
- }
- set
- {
- SetValue(TextWrappingProperty, value);
- }
- }
- /// <summary>
- /// Gets or sets the drop shadow distance.
- /// </summary>
- [Category("Appearance"), Description("The drop shadow distance.")]
- public double DropShadowDistance
- {
- get
- {
- return (double)GetValue(DropShadowDistanceProperty);
- }
- set
- {
- SetValue(DropShadowDistanceProperty, value);
- }
- }
- /// <summary>
- /// Gets or sets the drop shadow angle.
- /// </summary>
- [Category("Appearance"), Description("The drop shadow angle.")]
- public double DropShadowAngle
- {
- get
- {
- return (double)GetValue(DropShadowAngleProperty);
- }
- set
- {
- SetValue(DropShadowAngleProperty, value);
- }
- }
- /// <summary>
- /// Gets the UI elements out of the template.
- /// </summary>
- public override void OnApplyTemplate()
- {
- base.OnApplyTemplate();
- _dropShadowTranslate = GetTemplateChild("PART_DropShadowTranslate") as TranslateTransform;
- _dropShadowBrush = GetTemplateChild("PART_DropShadowBrush") as SolidColorBrush;
- UpdateDropShadowPosition();
- UpdateDropShadowBrush();
- }
- /// <summary>
- /// Converts degrees into radians.
- /// </summary>
- /// <param name="degrees">The degree value.</param>
- /// <returns>The degrees as radians.</returns>
- private static double DegreesToRadians(double degrees)
- {
- return degrees * (Math.PI / 180);
- }
- /// <summary>
- /// Gets a point offset by a distance and angle (in degrees).
- /// </summary>
- /// <param name="angle">The angle in degrees.</param>
- /// <param name="distance">The distance.</param>
- /// <returns>The offset point.</returns>
- private static Point GetOffset(double angle, double distance)
- {
- double x = Math.Cos(DegreesToRadians(angle)) * distance;
- double y = Math.Tan(DegreesToRadians(angle)) * x;
- return new Point(x, y);
- }
- /// <summary>
- /// Updates the drop shadow.
- /// </summary>
- internal void UpdateDropShadowPosition()
- {
- if (_dropShadowTranslate != null)
- {
- Point offset = GetOffset(DropShadowAngle, DropShadowDistance);
- _dropShadowTranslate.X = offset.X;
- _dropShadowTranslate.Y = offset.Y;
- }
- }
- /// <summary>
- /// Updates the drop shadow brush.
- /// </summary>
- internal void UpdateDropShadowBrush()
- {
- if (_dropShadowBrush != null)
- {
- _dropShadowBrush.Color = DropShadowColor;
- _dropShadowBrush.Opacity = DropShadowOpacity;
- }
- }
- /// <summary>
- /// Updates the drop shadow.
- /// </summary>
- /// <param name="dependencyObject">The drop shadow text block.</param>
- /// <param name="eventArgs">Dependency Property Changed Event Args</param>
- private static void DropShadowDistanceChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs eventArgs)
- {
- var dropShadowTextBlock = (DropShadowTextBlock)dependencyObject;
- dropShadowTextBlock.UpdateDropShadowPosition();
- }
- /// <summary>
- /// Updates the drop shadow.
- /// </summary>
- /// <param name="dependencyObject">The drop shadow text block.</param>
- /// <param name="eventArgs">Dependency Property Changed Event Args</param>
- private static void DropShadowAngleChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs eventArgs)
- {
- var dropShadowTextBlock = (DropShadowTextBlock)dependencyObject;
- dropShadowTextBlock.UpdateDropShadowPosition();
- }
- /// <summary>
- /// Updates the drop shadow.
- /// </summary>
- /// <param name="dependencyObject">The drop shadow text block.</param>
- /// <param name="eventArgs">Dependency Property Changed Event Args</param>
- private static void DropShadowColorChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs eventArgs)
- {
- var dropShadowTextBlock = (DropShadowTextBlock)dependencyObject;
- dropShadowTextBlock.UpdateDropShadowBrush();
- }
- /// <summary>
- /// Updates the drop shadow.
- /// </summary>
- /// <param name="dependencyObject">The drop shadow text block.</param>
- /// <param name="eventArgs">Dependency Property Changed Event Args</param>
- private static void DropShadowOpacityChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs eventArgs)
- {
- var dropShadowTextBlock = (DropShadowTextBlock)dependencyObject;
- dropShadowTextBlock.UpdateDropShadowBrush();
- }
- }
- }
|