forked from microsoft/BuildCast
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBufferingVisualManager.cs
203 lines (173 loc) · 7.22 KB
/
BufferingVisualManager.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
// ******************************************************************
// Copyright (c) Microsoft. All rights reserved.
// This code is licensed under the MIT License (MIT).
// THE CODE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH
// THE CODE OR THE USE OR OTHER DEALINGS IN THE CODE.
// ******************************************************************
using System;
using System.Numerics;
using System.Threading.Tasks;
using Windows.UI;
using Windows.UI.Composition;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
namespace BuildCast.Helpers
{
public class BufferingVisualManager
{
private Compositor _compositor;
private PointLight _pointLight;
private AmbientLight _ambientLight;
private TextBlock _bufferingMessage;
private Panel _textHolder;
private FrameworkElement _lightingTarget;
public bool IsBuffering { get; private set; }
public async Task StartBuffering(Panel textHolder, FrameworkElement lightingTarget, bool lightingdisabled)
{
if (!this.IsBuffering)
{
this.IsBuffering = true;
this._textHolder = textHolder;
if (!lightingdisabled)
{
this._lightingTarget = lightingTarget;
this.ConfigureLighting(this._lightingTarget);
}
this._bufferingMessage = await this.ConfigureText(this._textHolder);
}
}
public void StopBuffering(bool lightingdisabled)
{
if (this.IsBuffering)
{
CompositionScopedBatch batch = null;
if (!lightingdisabled)
{
batch = this.RemoveLighting();
}
else
{
batch = this._compositor.CreateScopedBatch(CompositionBatchTypes.Animation);
}
this.RemoveText(batch, this._bufferingMessage);
if (batch != null)
{
batch.Completed += (o, e) =>
{
this.IsBuffering = false;
};
batch.End();
}
}
}
public void SetCompositor(Compositor compositor)
{
this._compositor = compositor;
}
private async Task<TextBlock> ConfigureText(Panel target)
{
TextBlock tb = new TextBlock();
tb.FontSize = 30;
tb.Text = "Loading..";
tb.HorizontalAlignment = HorizontalAlignment.Center;
tb.VerticalAlignment = VerticalAlignment.Center;
tb.SetValue(Grid.RowProperty, 2);
target.Children.Add(tb);
var visual = tb.GetVisual();
var batch = visual.ApplyImplicitAnimation(TimeSpan.FromSeconds(2));
Func<Task> delega = null;
delega = async () =>
{
if (batch.IsEnded)
{
visual.Opacity = 1.0f;
}
else
{
await Window.Current.Dispatcher.RunIdleAsync((s) => { delega(); });
}
};
await Window.Current.Dispatcher.RunIdleAsync((s) => { delega(); });
return tb;
}
private void RemoveText(CompositionScopedBatch batch, TextBlock textElement)
{
if (textElement != null)
{
var visual = textElement.GetVisual();
visual.Opacity = 0;
}
if (batch != null)
{
batch.Completed += (o, e) =>
{
this._textHolder.Children.Remove(this._bufferingMessage);
this._bufferingMessage = null;
};
}
}
private void ConfigureLighting(FrameworkElement element)
{
if (this._compositor != null)
{
// get interop visual for element
var text = element.GetVisual();
this._ambientLight = this._compositor.CreateAmbientLight();
this._ambientLight.Color = Colors.White;
this._ambientLight.Targets.Add(text);
this._pointLight = this._compositor.CreatePointLight();
this._pointLight.Color = Colors.White;
this._pointLight.CoordinateSpace = text;
this._pointLight.Targets.Add(text);
// starts out to the left; vertically centered; light's z-offset is related to fontsize
this._pointLight.Offset = new Vector3(0, (float)element.ActualHeight / 2, 480);
// simple offset.X animation that runs forever
var animation = this._compositor.CreateScalarKeyFrameAnimation();
animation.InsertKeyFrame(1, (float)element.ActualWidth);
animation.Duration = TimeSpan.FromMilliseconds(800);
animation.IterationBehavior = AnimationIterationBehavior.Forever;
animation.Direction = AnimationDirection.Alternate;
var animation2 = this._compositor.CreateColorKeyFrameAnimation();
animation2.Duration = TimeSpan.FromSeconds(2);
animation2.InsertKeyFrame(1, Color.FromArgb(0xFF, 0x50, 0x50, 0x50));
this._ambientLight.StartAnimation(nameof(AmbientLight.Color), animation2);
this._pointLight.StartAnimation("Offset.X", animation);
}
}
private CompositionScopedBatch RemoveLighting()
{
CompositionScopedBatch returnBatch = null;
if (this._compositor != null)
{
returnBatch = this._compositor.CreateScopedBatch(CompositionBatchTypes.Animation);
var animation2 = this._compositor.CreateColorKeyFrameAnimation();
animation2.Duration = TimeSpan.FromSeconds(2);
animation2.InsertKeyFrame(1, Colors.White);
this._ambientLight.StartAnimation("Color", animation2);
var animation = this._compositor.CreateScalarKeyFrameAnimation();
animation.Duration = TimeSpan.FromSeconds(2);
animation.InsertKeyFrame(1, 40.0f);
this._pointLight.StartAnimation("LinearAttenuation", animation);
returnBatch.Completed += (o, e) =>
{
if (this._pointLight != null)
{
this._pointLight.Dispose();
this._pointLight = null;
}
if (this._ambientLight != null)
{
this._ambientLight.Dispose();
this._ambientLight = null;
}
};
}
return returnBatch;
}
}
}