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