Skip to content

Commit

Permalink
Merge pull request #7 from Sergio0694/debug_merge
Browse files Browse the repository at this point in the history
Translation animations, updated lighting effects
  • Loading branch information
Sergio0694 authored Oct 13, 2017
2 parents 1347bce + a0d01f2 commit 51395d0
Show file tree
Hide file tree
Showing 10 changed files with 114 additions and 43 deletions.
29 changes: 19 additions & 10 deletions UICompositionAnimations/Brushes/LightingBrush.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,8 @@ private static void OnSpecularAmountChanged(DependencyObject d, DependencyProper
// The factory to create the brush
CompositionEffectFactory _Factory;

// The invert effect for the opacity mask
InvertEffect _InverseEffect;

// The luminance effect to generate the opacity mask
LuminanceToAlphaEffect _LuminanceEffect;
// The color transformation effect to convert luminance to opacity
ColorMatrixEffect _ColorMatrixEffect;

/// <inheritdoc cref="XamlCompositionBrushBase"/>
protected override void OnConnected()
Expand All @@ -99,9 +96,22 @@ protected override void OnConnected()
DiffuseAmount = (float)DiffuseAmount,
AmbientAmount = 0
};
_LuminanceEffect = new LuminanceToAlphaEffect { Source = sceneLightingEffect }; // Map the bright areas of the light to an opacity mask
_InverseEffect = new InvertEffect { Source = _LuminanceEffect }; // Invert the colors to make the brighter areas white
_Factory = Window.Current.Compositor.CreateEffectFactory(_InverseEffect, new[] { "Light.DiffuseAmount", "Light.SpecularShine", "Light.SpecularAmount" });

// Setup the color matrix effect to map the luminosity
_ColorMatrixEffect = new ColorMatrixEffect
{
Source = sceneLightingEffect,
ColorMatrix = new Matrix5x4
{
M11 = 0, M21 = 0, M31 = 0, M41 = 0, M51 = 1,
M12 = 0, M22 = 0, M32 = 0, M42 = 0, M52 = 1,
M13 = 0, M23 = 0, M33 = 0, M43 = 0, M53 = 1,
M14 = 0.2125f, M24 = 0.7154f, M34 = 0.0721f, M44 = 0, M54 = 0
}
};

// Initialize the factory
_Factory = Window.Current.Compositor.CreateEffectFactory(_ColorMatrixEffect, new[] { "Light.DiffuseAmount", "Light.SpecularShine", "Light.SpecularAmount" });

// Create and store the brush
_Brush = _Factory.CreateBrush();
Expand All @@ -117,8 +127,7 @@ protected override void OnDisconnected()
{
_Brush?.Dispose();
_Factory?.Dispose();
_LuminanceEffect?.Dispose();
_InverseEffect?.Dispose();
_ColorMatrixEffect?.Dispose();
CompositionBrush = null;
}
base.OnDisconnected();
Expand Down
17 changes: 17 additions & 0 deletions UICompositionAnimations/Composition/CompositionManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,23 @@ public static Vector3KeyFrameAnimation CreateVector3KeyFrameAnimation([NotNull]
return ani;
}

/// <summary>
/// Creates a <see cref="CompositionAnimation"/> instance with the given parameters to on a target element, using an expression animation
/// </summary>
/// <param name="compositor">The current <see cref="Compositor"/> instance used to create the animation</param>
/// <param name="from">The optional starting value for the animation</param>
/// <param name="to">The final value for the animation</param>
/// <param name="duration">The animation duration</param>
/// <param name="delay">The optional initial delay for the animation</param>
/// <param name="ease">The optional easing function for the animation</param>
[PublicAPI]
[Pure, NotNull]
public static CompositionAnimation CreateMatrix4x4KeyFrameAnimation([NotNull] this Compositor compositor,
Matrix4x4? from, Matrix4x4 to, TimeSpan duration, TimeSpan? delay, [CanBeNull] CompositionEasingFunction ease = null)
{
throw new NotImplementedException(); // TODO
}

#endregion
}
}
78 changes: 52 additions & 26 deletions UICompositionAnimations/CompositionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -949,20 +949,31 @@ public static Task SetCompositionScaleImplicitAnimationAsync([NotNull] this Fram
// Manages the scale animation
private static async Task<float> ManageCompositionSlideAnimationAsync([NotNull] Visual visual,
TranslationAxis axis, float? startXY, float endXY,
int ms, int? msDelay, [NotNull] CompositionEasingFunction easingFunction)
int ms, int? msDelay, [NotNull] CompositionEasingFunction easingFunction, SlideAnimationType type)
{
// Get the default values
visual.StopAnimation("Offset");
visual.StopAnimation(type == SlideAnimationType.Offset ? "Offset" : "MatrixTransform.Translation");

// Get the easing function, the duration and delay
TimeSpan duration = TimeSpan.FromMilliseconds(ms);
TimeSpan? delay;
if (msDelay.HasValue) delay = TimeSpan.FromMilliseconds(msDelay.Value);
else delay = null;

// Calculate the initial and final offset values
Vector3 initialOffset = visual.Offset;
Vector3 endOffset = visual.Offset;
// Setup the animation start and end positions
Vector3 initialOffset, endOffset;
if (type == SlideAnimationType.Offset)
{
initialOffset = visual.Offset;
endOffset = visual.Offset;
}
else
{
initialOffset = visual.TransformMatrix.Translation;
endOffset = visual.TransformMatrix.Translation;
}

// Create the animation
if (axis == TranslationAxis.X)
{
if (startXY.HasValue) initialOffset.X = startXY.Value;
Expand All @@ -973,18 +984,16 @@ private static async Task<float> ManageCompositionSlideAnimationAsync([NotNull]
if (startXY.HasValue) initialOffset.Y = startXY.Value;
endOffset.Y = endXY;
}

// Scale animation
Vector3KeyFrameAnimation offsetAnimation = visual.Compositor.CreateVector3KeyFrameAnimation(initialOffset, endOffset, duration, delay, easingFunction);
Vector3KeyFrameAnimation animation = visual.Compositor.CreateVector3KeyFrameAnimation(initialOffset, endOffset, duration, delay, easingFunction);

// Get the batch and start the animations
CompositionScopedBatch batch = visual.Compositor.CreateScopedBatch(CompositionBatchTypes.Animation);
TaskCompletionSource<object> tcs = new TaskCompletionSource<object>();
batch.Completed += (s, e) => tcs.SetResult(null);
visual.StartAnimation("Offset", offsetAnimation);
visual.StartAnimation(type == SlideAnimationType.Offset ? "Offset" : "MatrixTransform.Translation", animation);
batch.End();
await tcs.Task;
return initialOffset.X;
return axis == TranslationAxis.X ? initialOffset.X : initialOffset.Y;
}

/// <summary>
Expand All @@ -999,9 +1008,10 @@ private static async Task<float> ManageCompositionSlideAnimationAsync([NotNull]
/// <param name="easingFunction">The easing function to use with the new animations</param>
/// <param name="reverse">If true, the animation will be played in reverse mode when it finishes for the first time</param>
/// <param name="callback">An <see cref="Action"/> to execute when the new animations end</param>
/// <param name="type">The type of animation to apply</param>
public static async void StartCompositionSlideAnimation([NotNull] this UIElement element,
TranslationAxis axis, float? startOffset, float endOffset,
int ms, int? msDelay, EasingFunctionNames easingFunction, bool reverse = false, Action callback = null)
int ms, int? msDelay, EasingFunctionNames easingFunction, bool reverse = false, Action callback = null, SlideAnimationType type = SlideAnimationType.Offset)
{
await element.StartCompositionSlideAnimationAsync(axis, startOffset, endOffset, ms, msDelay, easingFunction, reverse);
callback?.Invoke();
Expand All @@ -1022,9 +1032,10 @@ public static async void StartCompositionSlideAnimation([NotNull] this UIElement
/// <param name="y2">The Y coordinate of the second control point of the cubic beizer easing function</param>
/// <param name="reverse">If true, the animation will be played in reverse mode when it finishes for the first time</param>
/// <param name="callback">An <see cref="Action"/> to execute when the new animations end</param>
/// <param name="type">The type of animation to apply</param>
public static async void StartCompositionSlideAnimation([NotNull] this UIElement element,
TranslationAxis axis, float? startOffset, float endOffset,
int ms, int? msDelay, float x1, float y1, float x2, float y2, bool reverse = false, Action callback = null)
int ms, int? msDelay, float x1, float y1, float x2, float y2, bool reverse = false, Action callback = null, SlideAnimationType type = SlideAnimationType.Offset)
{
await element.StartCompositionSlideAnimationAsync(axis, startOffset, endOffset, ms, msDelay, x1, y1, x2, y2, reverse);
callback?.Invoke();
Expand All @@ -1041,14 +1052,15 @@ public static async void StartCompositionSlideAnimation([NotNull] this UIElement
/// <param name="msDelay">The delay before the animation starts, in milliseconds. If null, there will be no delay</param>
/// <param name="easingFunction">The easing function to use with the new animations</param>
/// <param name="reverse">If true, the animation will be played in reverse mode when it finishes for the first time</param>
/// <param name="type">The type of animation to apply</param>
public static async Task StartCompositionSlideAnimationAsync([NotNull] this UIElement element,
TranslationAxis axis, float? startOffset, float endOffset,
int ms, int? msDelay, EasingFunctionNames easingFunction, bool reverse = false)
int ms, int? msDelay, EasingFunctionNames easingFunction, bool reverse = false, SlideAnimationType type = SlideAnimationType.Offset)
{
Visual visual = element.GetVisual();
CompositionEasingFunction ease = visual.GetEasingFunction(easingFunction);
startOffset = await ManageCompositionSlideAnimationAsync(visual, axis, startOffset, endOffset, ms, msDelay, ease);
if (reverse) await ManageCompositionSlideAnimationAsync(visual, axis, endOffset, startOffset.Value, ms, msDelay, ease);
startOffset = await ManageCompositionSlideAnimationAsync(visual, axis, startOffset, endOffset, ms, msDelay, ease, type);
if (reverse) await ManageCompositionSlideAnimationAsync(visual, axis, endOffset, startOffset.Value, ms, msDelay, ease, type);
}

/// <summary>
Expand All @@ -1065,20 +1077,21 @@ public static async Task StartCompositionSlideAnimationAsync([NotNull] this UIEl
/// <param name="x2">The X coordinate of the second control point of the cubic beizer easing function</param>
/// <param name="y2">The Y coordinate of the second control point of the cubic beizer easing function</param>
/// <param name="reverse">If true, the animation will be played in reverse mode when it finishes for the first time</param>
/// <param name="type">The type of animation to apply</param>
public static async Task StartCompositionSlideAnimationAsync([NotNull] this UIElement element,
TranslationAxis axis, float? startOffset, float endOffset,
int ms, int? msDelay, float x1, float y1, float x2, float y2, bool reverse = false)
int ms, int? msDelay, float x1, float y1, float x2, float y2, bool reverse = false, SlideAnimationType type = SlideAnimationType.Offset)
{
Visual visual = element.GetVisual();
CompositionEasingFunction ease = visual.GetEasingFunction(x1, y1, x2, y2);
startOffset = await ManageCompositionSlideAnimationAsync(visual, axis, startOffset, endOffset, ms, msDelay, ease);
if (reverse) await ManageCompositionSlideAnimationAsync(visual, axis, endOffset, startOffset.Value, ms, msDelay, ease);
startOffset = await ManageCompositionSlideAnimationAsync(visual, axis, startOffset, endOffset, ms, msDelay, ease, type);
if (reverse) await ManageCompositionSlideAnimationAsync(visual, axis, endOffset, startOffset.Value, ms, msDelay, ease, type);
}

// Sets an implicit slide animation on the target element
private static void SetCompositionSlideImplicitAnimation([NotNull] UIElement element, [NotNull] Visual visual, ImplicitAnimationType type,
TranslationAxis axis, float start, float end,
int ms, int? msDelay, [NotNull] CompositionEasingFunction easingFunction)
int ms, int? msDelay, [NotNull] CompositionEasingFunction easingFunction, SlideAnimationType animationType)
{
// Get the easing function, the duration and delay
TimeSpan duration = TimeSpan.FromMilliseconds(ms);
Expand All @@ -1087,8 +1100,19 @@ private static void SetCompositionSlideImplicitAnimation([NotNull] UIElement ele
else delay = null;

// Calculate the initial and final offset values
Vector3 initialOffset = visual.Offset;
Vector3 endOffset = visual.Offset;
Vector3 initialOffset, endOffset;
if (animationType == SlideAnimationType.Offset)
{
initialOffset = visual.Offset;
endOffset = visual.Offset;
}
else
{
initialOffset = visual.TransformMatrix.Translation;
endOffset = visual.TransformMatrix.Translation;
}

// Setup the target values
if (axis == TranslationAxis.X)
{
initialOffset.X = start;
Expand All @@ -1103,7 +1127,7 @@ private static void SetCompositionSlideImplicitAnimation([NotNull] UIElement ele
// Get the animations
CompositionAnimationGroup group = visual.Compositor.CreateAnimationGroup();
Vector3KeyFrameAnimation offsetAnimation = visual.Compositor.CreateVector3KeyFrameAnimation(initialOffset, endOffset, duration, delay, easingFunction);
offsetAnimation.Target = "Offset";
offsetAnimation.Target = animationType == SlideAnimationType.Offset ? "Offset" : "MatrixTransform.Translation";
group.Add(offsetAnimation);

// Set the implicit animation
Expand All @@ -1122,13 +1146,14 @@ private static void SetCompositionSlideImplicitAnimation([NotNull] UIElement ele
/// <param name="ms">The duration of the scale animation, in milliseconds</param>
/// <param name="msDelay">The delay before the animation starts, in milliseconds. If null, there will be no delay</param>
/// <param name="easingFunction">The easing function to use with the new animations</param>
/// <param name="animationType">The type of animation to apply</param>
public static void SetCompositionSlideImplicitAnimation([NotNull] this UIElement element, ImplicitAnimationType type,
TranslationAxis axis, float start, float end,
int ms, int? msDelay, EasingFunctionNames easingFunction)
int ms, int? msDelay, EasingFunctionNames easingFunction, SlideAnimationType animationType = SlideAnimationType.Offset)
{
Visual visual = element.GetVisual();
CompositionEasingFunction ease = visual.GetEasingFunction(easingFunction);
SetCompositionSlideImplicitAnimation(element, visual, type, axis, start, end, ms, msDelay, ease);
SetCompositionSlideImplicitAnimation(element, visual, type, axis, start, end, ms, msDelay, ease, animationType);
}

/// <summary>
Expand All @@ -1145,13 +1170,14 @@ public static void SetCompositionSlideImplicitAnimation([NotNull] this UIElement
/// <param name="y1">The Y coordinate of the first control point of the cubic beizer easing function</param>
/// <param name="x2">The X coordinate of the second control point of the cubic beizer easing function</param>
/// <param name="y2">The Y coordinate of the second control point of the cubic beizer easing function</param>
/// <param name="animationType">The type of animation to apply</param>
public static void SetCompositionSlideImplicitAnimation([NotNull] this UIElement element, ImplicitAnimationType type,
TranslationAxis axis, float start, float end,
int ms, int? msDelay, float x1, float y1, float x2, float y2)
int ms, int? msDelay, float x1, float y1, float x2, float y2, SlideAnimationType animationType = SlideAnimationType.Offset)
{
Visual visual = element.GetVisual();
CompositionEasingFunction ease = visual.GetEasingFunction(x1, y1, x2, y2);
SetCompositionSlideImplicitAnimation(element, visual, type, axis, start, end, ms, msDelay, ease);
SetCompositionSlideImplicitAnimation(element, visual, type, axis, start, end, ms, msDelay, ease, animationType);
}

#endregion
Expand Down
18 changes: 18 additions & 0 deletions UICompositionAnimations/Enums/SlideAnimationType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
namespace UICompositionAnimations.Enums
{
/// <summary>
/// Indicates the type of slide animation to apply to a <see cref="Windows.UI.Composition.Visual"/> element
/// </summary>
public enum SlideAnimationType
{
/// <summary>
/// The element will be animated using its offset property
/// </summary>
Offset,

/// <summary>
/// The element will be animated through its render transform property
/// </summary>
RenderTransform
}
}
2 changes: 1 addition & 1 deletion UICompositionAnimations/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("2.11.1.0")]
[assembly: AssemblyVersion("2.12.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: ComVisible(false)]
1 change: 1 addition & 0 deletions UICompositionAnimations/UICompositionAnimations.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@
<Compile Include="Enums\BitmapCacheMode.cs" />
<Compile Include="Enums\EasingFunctionNames.cs" />
<Compile Include="Enums\ImplicitAnimationType.cs" />
<Compile Include="Enums\SlideAnimationType.cs" />
<Compile Include="Helpers\ApiInformaitonHelper.cs" />
<Compile Include="Helpers\ColorConverter.cs" />
<Compile Include="Helpers\DispatcherHelper.cs" />
Expand Down
Loading

0 comments on commit 51395d0

Please sign in to comment.