From 29a4fdfaebd333ac94ae60606b075370dce28957 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Fri, 1 Sep 2017 13:08:27 +0200 Subject: [PATCH] Added new render transform extensions --- .../CompositionExtensions.cs | 78 ++++++++++++++++++- 1 file changed, 77 insertions(+), 1 deletion(-) diff --git a/UICompositionAnimations/CompositionExtensions.cs b/UICompositionAnimations/CompositionExtensions.cs index e506cda..8079aee 100644 --- a/UICompositionAnimations/CompositionExtensions.cs +++ b/UICompositionAnimations/CompositionExtensions.cs @@ -1625,6 +1625,22 @@ public static void SetVisualOffset(this UIElement element, TranslationAxis axis, visual.Offset = endOffset; } + /// + /// Sets the offset property of the visual object for a given object + /// + /// The target element + /// The x offset + /// The y offset + public static void SetVisualOffset(this UIElement element, float x, float y) + { + // Get the element visual and stop the animation + Visual visual = element.GetVisual(); + + // Set the desired offset + Vector3 offset = new Vector3(x, y, visual.Offset.Z); + visual.Offset = offset; + } + /// /// Sets the offset value of a given object /// @@ -1643,12 +1659,71 @@ public static Task SetVisualOffsetAsync(this UIElement element, TranslationAxis }); } + /// + /// Sets the offset value of a given object + /// + /// The to edit + /// The x offset + /// The y offset + public static Task SetVisualOffsetAsync(this UIElement element, float x, float y) + { + Visual visual = element.GetVisual(); + return Task.Run(() => + { + Vector3 offset = new Vector3(x, y, visual.Offset.Z); + visual.Offset = offset; + }); + } + /// /// Returns the visual offset for a target /// /// The input element public static Vector3 GetVisualOffset(this UIElement element) => element.GetVisual().Offset; + /// + /// Sets the translation property of the visual object for a given object + /// + /// The target element + /// The translation axis to edit + /// The final translation value to set for that axis + public static void SetVisualTranslation(this UIElement element, TranslationAxis axis, float translation) + { + // Get the element visual and stop the animation + Visual visual = element.GetVisual(); + + // Set the desired translation + /* [1.0, 0.0, 0.0, 0.0, + 0.0, 1.0, 0.0, 0.0, + 0.0, 0.0, 1.0, 0.0, + translation.X, translation.Y, translation.Z, 1.0] */ + if (axis == TranslationAxis.X) + { + float y = visual.TransformMatrix.M42; + visual.TransformMatrix = Matrix4x4.CreateTranslation(translation, y, 0); + } + else + { + float x = visual.TransformMatrix.M41; + visual.TransformMatrix = Matrix4x4.CreateTranslation(x, translation, 0); + } + } + + /// + /// Sets the translation property of the visual object for a given object + /// + /// The target element + /// The x translation + /// The y translation + public static void SetVisualTranslation(this UIElement element, float x, float y) + { + // Get the element visual and stop the animation + Visual visual = element.GetVisual(); + + // Set the desired offset + visual.TransformMatrix = Matrix4x4.CreateTranslation(x, y, 0); + } + /// /// Resets the scale, offset and opacity properties for a framework element /// @@ -1689,4 +1764,5 @@ public static async Task ResetCompositionVisualPropertiesAsync(this FrameworkEle #endregion } -} \ No newline at end of file +} + \ No newline at end of file