Skip to content

Commit a1aff13

Browse files
committed
Add Color and Intensity in AmbLight
1 parent 60bc554 commit a1aff13

File tree

2 files changed

+21799
-12482
lines changed

2 files changed

+21799
-12482
lines changed
Lines changed: 49 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,70 @@
11
namespace DevWinUI;
2+
23
public partial class AmbLight : XamlLight
34
{
4-
private static readonly string Id = typeof(AmbLight).FullName!;
5+
private static readonly string Id = typeof(AmbLight).FullName;
56

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
725
{
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;
934

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+
}));
1340

14-
// Associate CompositionLight with XamlLight
41+
protected override void OnConnected(UIElement newElement)
42+
{
43+
var compositor = CompositionTarget.GetCompositorForCurrentThread();
44+
45+
var ambientLight = CreateAmbientLight();
1546
CompositionLight = ambientLight;
1647

17-
// Add UIElement to the Light's Targets
1848
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+
}
1959
}
2060

2161
protected override void OnDisconnected(UIElement oldElement)
2262
{
23-
// Dispose Light when it is removed from the tree
2463
RemoveTargetElement(GetId(), oldElement);
2564
CompositionLight.Dispose();
2665
}
2766

28-
protected override string GetId()
29-
{
30-
return Id;
31-
}
67+
protected override string GetId() => Id;
3268
}
3369

3470

0 commit comments

Comments
 (0)