|
| 1 | +namespace DevWinUI; |
| 2 | + |
| 3 | +[TemplatePart(Name = nameof(PART_ImageGrid), Type = typeof(Grid))] |
| 4 | +public partial class BlendedImage : Control |
| 5 | +{ |
| 6 | + private const string PART_ImageGrid = "PART_ImageGrid"; |
| 7 | + private Grid grid; |
| 8 | + |
| 9 | + private Compositor _compositor; |
| 10 | + private ICompositionGenerator _generator; |
| 11 | + private CanvasGeometry _leftGeometry; |
| 12 | + private CanvasGeometry _rightGeometry; |
| 13 | + private float _topOffset; |
| 14 | + private IGaussianMaskSurface _leftGaussianSurface; |
| 15 | + private IGaussianMaskSurface _rightGaussianSurface; |
| 16 | + private float _currentWidth; |
| 17 | + private float _currentHeight; |
| 18 | + |
| 19 | + public double BlurRadius |
| 20 | + { |
| 21 | + get { return (double)GetValue(BlurRadiusProperty); } |
| 22 | + set { SetValue(BlurRadiusProperty, value); } |
| 23 | + } |
| 24 | + |
| 25 | + public static readonly DependencyProperty BlurRadiusProperty = |
| 26 | + DependencyProperty.Register(nameof(BlurRadius), typeof(double), typeof(BlendedImage), new PropertyMetadata(100.0d, OnPropertyChanged)); |
| 27 | + |
| 28 | + public double SplitValue |
| 29 | + { |
| 30 | + get { return (double)GetValue(SplitValueProperty); } |
| 31 | + set { SetValue(SplitValueProperty, value); } |
| 32 | + } |
| 33 | + |
| 34 | + public static readonly DependencyProperty SplitValueProperty = |
| 35 | + DependencyProperty.Register(nameof(SplitValue), typeof(double), typeof(BlendedImage), new PropertyMetadata(50.0d, OnPropertyChanged)); |
| 36 | + |
| 37 | + private static void OnPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) |
| 38 | + { |
| 39 | + var ctl = (BlendedImage)d; |
| 40 | + if (ctl != null) |
| 41 | + { |
| 42 | + ctl.Redraw(); |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + public object PrimaryImageSource |
| 47 | + { |
| 48 | + get { return (object)GetValue(PrimaryImageSourceProperty); } |
| 49 | + set { SetValue(PrimaryImageSourceProperty, value); } |
| 50 | + } |
| 51 | + |
| 52 | + public static readonly DependencyProperty PrimaryImageSourceProperty = |
| 53 | + DependencyProperty.Register(nameof(PrimaryImageSource), typeof(object), typeof(BlendedImage), new PropertyMetadata(null)); |
| 54 | + |
| 55 | + public object SecondaryImageSource |
| 56 | + { |
| 57 | + get { return (object)GetValue(SecondaryImageSourceProperty); } |
| 58 | + set { SetValue(SecondaryImageSourceProperty, value); } |
| 59 | + } |
| 60 | + |
| 61 | + public static readonly DependencyProperty SecondaryImageSourceProperty = |
| 62 | + DependencyProperty.Register(nameof(SecondaryImageSource), typeof(object), typeof(BlendedImage), new PropertyMetadata(null)); |
| 63 | + |
| 64 | + public BlendedImage() |
| 65 | + { |
| 66 | + DefaultStyleKey = typeof(BlendedImage); |
| 67 | + } |
| 68 | + |
| 69 | + protected override void OnApplyTemplate() |
| 70 | + { |
| 71 | + base.OnApplyTemplate(); |
| 72 | + |
| 73 | + grid = GetTemplateChild(PART_ImageGrid) as Grid; |
| 74 | + |
| 75 | + _compositor = CompositionTarget.GetCompositorForCurrentThread(); |
| 76 | + _generator = _compositor.CreateCompositionGenerator(); |
| 77 | + |
| 78 | + SizeChanged -= OnSizeChanged; |
| 79 | + SizeChanged += OnSizeChanged; |
| 80 | + } |
| 81 | + |
| 82 | + private void OnSizeChanged(object sender, SizeChangedEventArgs e) |
| 83 | + { |
| 84 | + if (ActualWidth <= 0 || ActualHeight <= 0) |
| 85 | + return; |
| 86 | + |
| 87 | + _currentWidth = (float)ActualWidth; |
| 88 | + _currentHeight = (float)ActualHeight; |
| 89 | + |
| 90 | + RebuildSurfaces(); |
| 91 | + } |
| 92 | + private async void RebuildSurfaces() |
| 93 | + { |
| 94 | + if (grid == null || _generator == null) |
| 95 | + return; |
| 96 | + |
| 97 | + var size = new Vector2(_currentWidth, _currentHeight); |
| 98 | + _topOffset = -_currentHeight / 2f; |
| 99 | + |
| 100 | + // Create visuals again |
| 101 | + var leftImageVisual = _compositor.CreateSpriteVisual(); |
| 102 | + leftImageVisual.Size = size; |
| 103 | + |
| 104 | + var leftImageSurface = |
| 105 | + await _generator.CreateImageSurfaceAsync( |
| 106 | + GeneralHelper.GetUriFromObjectSource(PrimaryImageSource), |
| 107 | + size.ToSize(), |
| 108 | + ImageSurfaceOptions.Default |
| 109 | + ); |
| 110 | + |
| 111 | + _leftGeometry = CanvasGeometry.CreateRectangle( |
| 112 | + _generator.Device, |
| 113 | + new Rect(0f, 0f, 2f * (_currentWidth * (float)SplitValue) / 100f, 2f * _currentHeight) |
| 114 | + ); |
| 115 | + |
| 116 | + var leftOffset = -(_currentWidth * (float)SplitValue) / 100f; |
| 117 | + |
| 118 | + var leftMaskedBrush = _compositor.CreateMaskBrush(); |
| 119 | + leftMaskedBrush.Source = _compositor.CreateSurfaceBrush(leftImageSurface); |
| 120 | + _leftGaussianSurface = _generator.CreateGaussianMaskSurface(size.ToSize(), _leftGeometry, new Vector2(leftOffset, _topOffset), (float)BlurRadius); |
| 121 | + leftMaskedBrush.Mask = _compositor.CreateSurfaceBrush(_leftGaussianSurface); |
| 122 | + leftImageVisual.Brush = leftMaskedBrush; |
| 123 | + |
| 124 | + // Right side |
| 125 | + var rightImageVisual = _compositor.CreateSpriteVisual(); |
| 126 | + rightImageVisual.Size = size; |
| 127 | + |
| 128 | + var rightImageSurface = |
| 129 | + await _generator.CreateImageSurfaceAsync( |
| 130 | + GeneralHelper.GetUriFromObjectSource(SecondaryImageSource), |
| 131 | + size.ToSize(), |
| 132 | + ImageSurfaceOptions.Default |
| 133 | + ); |
| 134 | + |
| 135 | + _rightGeometry = CanvasGeometry.CreateRectangle( |
| 136 | + _generator.Device, |
| 137 | + new Rect(0, 0, 2f * (_currentWidth * (100f - (float)SplitValue)) / 100f, 2f * _currentHeight) |
| 138 | + ); |
| 139 | + |
| 140 | + var rightMaskedBrush = _compositor.CreateMaskBrush(); |
| 141 | + rightMaskedBrush.Source = _compositor.CreateSurfaceBrush(rightImageSurface); |
| 142 | + _rightGaussianSurface = _generator.CreateGaussianMaskSurface(size.ToSize(), _rightGeometry, new Vector2(-leftOffset, _topOffset), (float)BlurRadius); |
| 143 | + rightMaskedBrush.Mask = _compositor.CreateSurfaceBrush(_rightGaussianSurface); |
| 144 | + rightImageVisual.Brush = rightMaskedBrush; |
| 145 | + |
| 146 | + // Replace child visual |
| 147 | + var container = _compositor.CreateContainerVisual(); |
| 148 | + container.Size = size; |
| 149 | + container.Children.InsertAtTop(leftImageVisual); |
| 150 | + container.Children.InsertAtTop(rightImageVisual); |
| 151 | + |
| 152 | + ElementCompositionPreview.SetElementChildVisual(grid, container); |
| 153 | + } |
| 154 | + |
| 155 | + private void Redraw() |
| 156 | + { |
| 157 | + if (_leftGeometry == null || _rightGeometry == null) |
| 158 | + { |
| 159 | + return; |
| 160 | + } |
| 161 | + |
| 162 | + var leftOffset = -(_currentWidth * (float)SplitValue) / 100f; |
| 163 | + _leftGeometry = CanvasGeometry.CreateRectangle(_generator.Device, new Rect(0f, 0f, 2f * (_currentWidth * (float)SplitValue) / 100f, 2f * _currentHeight)); |
| 164 | + _leftGaussianSurface.Redraw(_leftGeometry, new Vector2(leftOffset, _topOffset), (float)BlurRadius); |
| 165 | + _rightGeometry = CanvasGeometry.CreateRectangle(_generator.Device, new Rect(0, 0, 2f * (_currentWidth * (100f - (float)SplitValue)) / 100, 2f * _currentHeight)); |
| 166 | + _rightGaussianSurface.Redraw(_rightGeometry, new Vector2(-leftOffset, _topOffset), (float)BlurRadius); |
| 167 | + } |
| 168 | +} |
| 169 | + |
0 commit comments