|
1 | 1 | namespace DevWinUI; |
| 2 | + |
2 | 3 | public partial class AmbLight : XamlLight |
3 | 4 | { |
4 | | - private static readonly string Id = typeof(AmbLight).FullName!; |
| 5 | + private static readonly string Id = typeof(AmbLight).FullName; |
5 | 6 |
|
6 | | - protected override void OnConnected(UIElement newElement) |
| 7 | + public Color Color |
| 8 | + { |
| 9 | + get => (Color)GetValue(ColorProperty); |
| 10 | + set => SetValue(ColorProperty, value); |
| 11 | + } |
| 12 | + public static readonly DependencyProperty ColorProperty = DependencyProperty.Register( |
| 13 | + nameof(Color), typeof(Color), typeof(AmbLight), new PropertyMetadata(Colors.White, (s, e) => |
| 14 | + { |
| 15 | + var self = (AmbLight)s; |
| 16 | + var newColor = (Color)e.NewValue; |
| 17 | + |
| 18 | + if (self.CompositionLight is AmbientLight ambientLight) |
| 19 | + { |
| 20 | + ambientLight.Color = newColor; |
| 21 | + } |
| 22 | + })); |
| 23 | + |
| 24 | + public double Intensity |
7 | 25 | { |
8 | | - Compositor compositor = CompositionTarget.GetCompositorForCurrentThread(); |
| 26 | + get => (double)GetValue(IntensityProperty); |
| 27 | + set => SetValue(IntensityProperty, value); |
| 28 | + } |
| 29 | + public static readonly DependencyProperty IntensityProperty = DependencyProperty.Register( |
| 30 | + nameof(Intensity), typeof(double), typeof(AmbLight), new PropertyMetadata(1.0d, (s, e) => |
| 31 | + { |
| 32 | + var self = (AmbLight)s; |
| 33 | + var newIntensity = (float)e.NewValue; |
9 | 34 |
|
10 | | - // Create AmbientLight and set its properties |
11 | | - AmbientLight ambientLight = compositor.CreateAmbientLight(); |
12 | | - ambientLight.Color = Colors.White; |
| 35 | + if (self.CompositionLight is AmbientLight ambientLight) |
| 36 | + { |
| 37 | + ambientLight.Intensity = newIntensity; |
| 38 | + } |
| 39 | + })); |
13 | 40 |
|
14 | | - // Associate CompositionLight with XamlLight |
| 41 | + protected override void OnConnected(UIElement newElement) |
| 42 | + { |
| 43 | + var compositor = CompositionTarget.GetCompositorForCurrentThread(); |
| 44 | + |
| 45 | + var ambientLight = CreateAmbientLight(); |
15 | 46 | CompositionLight = ambientLight; |
16 | 47 |
|
17 | | - // Add UIElement to the Light's Targets |
18 | 48 | AddTargetElement(GetId(), newElement); |
| 49 | + |
| 50 | + AmbientLight CreateAmbientLight() |
| 51 | + { |
| 52 | + var light = compositor.CreateAmbientLight(); |
| 53 | + |
| 54 | + light.Color = Color; |
| 55 | + light.Intensity = Intensity.ToFloat(); |
| 56 | + |
| 57 | + return light; |
| 58 | + } |
19 | 59 | } |
20 | 60 |
|
21 | 61 | protected override void OnDisconnected(UIElement oldElement) |
22 | 62 | { |
23 | | - // Dispose Light when it is removed from the tree |
24 | 63 | RemoveTargetElement(GetId(), oldElement); |
25 | 64 | CompositionLight.Dispose(); |
26 | 65 | } |
27 | 66 |
|
28 | | - protected override string GetId() |
29 | | - { |
30 | | - return Id; |
31 | | - } |
| 67 | + protected override string GetId() => Id; |
32 | 68 | } |
33 | 69 |
|
34 | 70 |
|
0 commit comments