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