Files
ShrlAlgoToolkit/Melskin/Controls/Decorations/LightedSurface.xaml.cs
2026-02-17 22:17:13 +08:00

188 lines
8.1 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using System.Windows.Input;
using Melskin.Effects;
namespace Melskin.Controls.Decorations;
/// <summary>
/// LightedSurface 控件继承自 ContentControl用于创建具有光照效果的表面。该控件支持鼠标移动时的动态光照反应
/// 并允许通过设置不同的属性来自定义光照的颜色、强度和大小等特性。
/// </summary>
/// <remarks>
/// 此控件适用于需要在界面上添加视觉吸引力或强调区域的应用场景,比如按钮高亮、列表项选择或其他交互元素。
/// 通过调整光照颜色LightColorBrush、光照强度LightIntensity以及光照大小LightSize等属性
/// 可以实现丰富的视觉效果。此外CornerRadius 属性允许设置控件圆角,从而使得光照效果更加柔和自然。
/// </remarks>
public class LightedSurface : ContentControl
{
static LightedSurface()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(LightedSurface),
new FrameworkPropertyMetadata(typeof(LightedSurface)));
}
private readonly LightedSurfaceEffect effect = new();
/// <inheritdoc />
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
InitEffect();
}
private void InitEffect()
{
var contentBorder = GetTemplateChild("PART_lightBorder") as System.Windows.Controls.Border;
effect.UiSize = new Point(ActualWidth, ActualHeight);
effect.LightSize = LightSize;
effect.LightColor = LightColorBrush.Color;
effect.Intensity = LightIntensity;
contentBorder!.Effect = effect;
}
/// <inheritdoc />
protected override void OnMouseMove(MouseEventArgs e)
{
base.OnMouseMove(e);
effect.MousePosition = e.GetPosition(this);
}
/// <inheritdoc />
protected override void OnRenderSizeChanged(SizeChangedInfo sizeInfo)
{
base.OnRenderSizeChanged(sizeInfo);
effect.UiSize = new Point(sizeInfo.NewSize.Width, sizeInfo.NewSize.Height);
}
/// <summary>
/// 获取或设置光照的颜色。此属性决定了 LightedSurface 控件上光照效果的颜色。
/// </summary>
/// <remarks>
/// 通过修改此属性,可以改变控件表面光照的颜色,从而实现不同的视觉效果。默认情况下,光照颜色为白色。
/// 若要自定义光照颜色,请设置一个 SolidColorBrush 对象作为此属性的值。
/// </remarks>
public SolidColorBrush LightColorBrush
{
get => (SolidColorBrush)GetValue(LightColorBrushProperty);
set => SetValue(LightColorBrushProperty, value);
}
/// <summary>
/// 定义了 LightedSurface 控件的光照颜色属性。此依赖属性允许用户设置控件的光照颜色,支持使用任何 SolidColorBrush 对象。
/// 当此属性值发生变化时,会触发 LightColorChangedCallback 方法,从而更新控件内部使用的光照效果的颜色。
/// </summary>
public static readonly DependencyProperty LightColorBrushProperty =
DependencyProperty.Register(nameof(LightColorBrush),
typeof(SolidColorBrush), typeof(LightedSurface),
new PropertyMetadata(Brushes.White, LightColorChangedCallback));
/// <summary>
/// 当 LightedSurface 控件的光照颜色属性值发生变化时调用此回调方法。
/// 方法会更新与控件关联的效果中的光照颜色。
/// </summary>
/// <param name="d">触发属性更改的依赖对象,此处应为 LightedSurface 的一个实例。</param>
/// <param name="e">包含有关属性更改的信息,包括新旧值。</param>
private static void LightColorChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (d is not LightedSurface lightedSurface)
{
return;
}
lightedSurface.effect.LightColor = ((SolidColorBrush)e.NewValue).Color;
}
/// <summary>
/// 获取或设置光照的强度。此属性决定了 LightedSurface 控件上光照效果的亮度。
/// </summary>
/// <remarks>
/// 通过修改此属性,可以改变控件表面光照的亮度,从而实现不同的视觉效果。光照强度的有效范围是 0 到 1其中 0 表示无光照1 表示最大亮度。默认情况下,光照强度为 0。
/// </remarks>
public double LightIntensity
{
get => (double)GetValue(LightIntensityProperty);
set => SetValue(LightIntensityProperty, value);
}
/// <summary>
///
/// </summary>
public static readonly DependencyProperty LightIntensityProperty =
DependencyProperty.Register(nameof(LightIntensity),
typeof(double), typeof(LightedSurface),
new PropertyMetadata(0.0, LightIntensityChangedCallback));
private static void LightIntensityChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (d is not LightedSurface lightedSurface)
{
return;
}
lightedSurface.effect.Intensity = (double)e.NewValue;
}
/// <summary>
/// 获取或设置光源的大小。此属性决定了 LightedSurface 控件上光照效果的尺寸。
/// </summary>
/// <remarks>
/// 通过修改此属性,可以调整控件表面光照的大小,从而实现不同的视觉效果。默认情况下,光源大小为 40.0。
/// 若要自定义光源大小,请设置一个 double 类型的值作为此属性的值。
/// </remarks>
public double LightSize
{
get => (double)GetValue(LightSizeProperty);
set => SetValue(LightSizeProperty, value);
}
/// <summary>
/// 获取或设置光照的大小。此属性决定了 LightedSurface 控件上光照效果的尺寸。
/// </summary>
/// <remarks>
/// 通过修改此属性,可以改变控件表面光照的大小,从而实现不同的视觉效果。默认情况下,光照大小为 40.0。
/// 若要自定义光照大小,请设置一个 double 类型的值作为此属性的值。
/// 当此属性值发生变化时,会触发 LightSizeChangedCallback 方法,从而更新控件内部使用的光照效果的大小。
/// </remarks>
public static readonly DependencyProperty LightSizeProperty =
DependencyProperty.Register(nameof(LightSize),
typeof(double), typeof(LightedSurface),
new PropertyMetadata(40.0, LightSizeChangedCallback));
private static void LightSizeChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (d is not LightedSurface lightedSurface)
{
return;
}
lightedSurface.effect.LightSize = (double)e.NewValue;
}
/// <summary>
/// 获取或设置控件的圆角半径。此属性决定了 LightedSurface 控件四个角的圆角程度。
/// </summary>
/// <remarks>
/// 通过修改此属性可以改变控件边角的圆滑程度从而实现不同的视觉效果。默认情况下圆角半径为4。
/// 若要自定义圆角半径,请设置一个 CornerRadius 对象作为此属性的值。
/// </remarks>
public CornerRadius CornerRadius
{
get => (CornerRadius)GetValue(CornerRadiusProperty);
set => SetValue(CornerRadiusProperty, value);
}
/// <summary>
/// 定义了 LightedSurface 控件的圆角属性。此依赖属性允许用户设置控件四个角的半径,从而改变控件外观的圆润程度。
/// 通过修改此属性,可以使得光照效果更加柔和自然,适用于需要平滑边缘的应用场景。
/// </summary>
/// <remarks>
/// 默认情况下CornerRadius 的值为 4。用户可以通过设置不同的 CornerRadius 值来自定义控件的圆角大小。
/// 例如,设置更大的 CornerRadius 可以使控件看起来更圆润,而较小的值则会使控件的边缘更加锐利。
/// </remarks>
public static readonly DependencyProperty CornerRadiusProperty =
DependencyProperty.Register(nameof(CornerRadius),
typeof(CornerRadius), typeof(LightedSurface),
new PropertyMetadata(new CornerRadius(4)));
}