public static RequestedTransform GetTransform <RequestedTransform>(this UIElement element, bool create) where RequestedTransform : Transform, new()
        {
            TransformCreationMode mode = TransformCreationMode.None;

            if (create)
            {
                mode = TransformCreationMode.Create | TransformCreationMode.AddToGroup;
            }

            return(element.GetTransform <RequestedTransform>(mode));
        }
        /// <summary>
        /// Returns a render transform of the specified type from the element, creating it if necessary
        /// </summary>
        /// <typeparam name="TRequestedTransform">The type of transform (Rotate, Translate, etc)</typeparam>
        /// <param name="element">The element to check</param>
        /// <param name="mode">The mode to use for creating transforms, if not found</param>
        /// <returns>The specified transform, or null if not found and not created</returns>
        public static TRequestedTransform GetTransform <TRequestedTransform>(this UIElement element, TransformCreationMode mode) where TRequestedTransform : Transform, new()
        {
            Transform           originalTransform  = element.RenderTransform;
            TRequestedTransform requestedTransform = null;
            MatrixTransform     matrixTransform    = null;
            TransformGroup      transformGroup     = null;

            // Current transform is null -- create if necessary and return
            if (originalTransform == null)
            {
                if ((mode & TransformCreationMode.Create) == TransformCreationMode.Create)
                {
                    requestedTransform      = new TRequestedTransform();
                    element.RenderTransform = requestedTransform;
                    return(requestedTransform);
                }

                return(null);
            }

            // Transform is exactly what we want -- return it
            requestedTransform = originalTransform as TRequestedTransform;
            if (requestedTransform != null)
            {
                return(requestedTransform);
            }


            // The existing transform is matrix transform - overwrite if necessary and return
            matrixTransform = originalTransform as MatrixTransform;
            if (matrixTransform != null)
            {
                if (matrixTransform.Matrix.IsIdentity &&
                    (mode & TransformCreationMode.Create) == TransformCreationMode.Create &&
                    (mode & TransformCreationMode.IgnoreIdentityMatrix) == TransformCreationMode.IgnoreIdentityMatrix)
                {
                    requestedTransform      = new TRequestedTransform();
                    element.RenderTransform = requestedTransform;
                    return(requestedTransform);
                }

                return(null);
            }

            // Transform is actually a group -- check for the requested type
            transformGroup = originalTransform as TransformGroup;
            if (transformGroup != null)
            {
                foreach (Transform child in transformGroup.Children)
                {
                    // Child is the right type -- return it
                    if (child is TRequestedTransform)
                    {
                        return(child as TRequestedTransform);
                    }
                }

                // Right type was not found, but we are OK to add it
                if ((mode & TransformCreationMode.AddToGroup) == TransformCreationMode.AddToGroup)
                {
                    requestedTransform = new TRequestedTransform();
                    transformGroup.Children.Add(requestedTransform);
                    return(requestedTransform);
                }

                return(null);
            }

            // Current ransform is not a group and is not what we want;
            // create a new group containing the existing transform and the new one
            if ((mode & TransformCreationMode.CombineIntoGroup) == TransformCreationMode.CombineIntoGroup)
            {
                transformGroup = new TransformGroup();
                transformGroup.Children.Add(originalTransform);
                transformGroup.Children.Add(requestedTransform);
                element.RenderTransform = transformGroup;
                return(requestedTransform);
            }

            Debug.Assert(false, "Shouldn't get here");
            return(null);
        }
Exemple #3
0
        /// <summary>Returns a render transform of the specified type from the element, creating it if necessary. </summary>
        /// <typeparam name="TRequestedTransform">The type of transform (Rotate, Translate, etc)</typeparam>
        /// <param name="element">The element to check</param>
        /// <param name="mode">The mode to use for creating transforms, if not found</param>
        /// <returns>The specified transform, or null if not found and not created</returns>
        public static TRequestedTransform GetTransform <TRequestedTransform>(this UIElement element, TransformCreationMode mode)
            where TRequestedTransform : Transform, new()
        {
            Transform originalTransform = element.RenderTransform;

            if (originalTransform == null)
            {
                if ((mode & TransformCreationMode.Create) == TransformCreationMode.Create)
                {
                    element.RenderTransform = new TRequestedTransform();
                    return((TRequestedTransform)element.RenderTransform);
                }
                return(null);
            }

            var requestedTransform = originalTransform as TRequestedTransform;

            if (requestedTransform != null)
            {
                return(requestedTransform);
            }

            var matrixTransform = originalTransform as MatrixTransform;

            if (matrixTransform != null)
            {
                if (matrixTransform.Matrix.IsIdentity &&
                    (mode & TransformCreationMode.Create) == TransformCreationMode.Create &&
                    (mode & TransformCreationMode.IgnoreIdentityMatrix) == TransformCreationMode.IgnoreIdentityMatrix)
                {
                    requestedTransform      = new TRequestedTransform();
                    element.RenderTransform = requestedTransform;
                    return(requestedTransform);
                }

                return(null);
            }

            var transformGroup = originalTransform as TransformGroup;

            if (transformGroup != null)
            {
                foreach (Transform child in transformGroup.Children)
                {
                    if (child is TRequestedTransform)
                    {
                        return(child as TRequestedTransform);
                    }
                }

                if ((mode & TransformCreationMode.AddToGroup) == TransformCreationMode.AddToGroup)
                {
                    requestedTransform = new TRequestedTransform();
                    transformGroup.Children.Add(requestedTransform);
                    return(requestedTransform);
                }

                return(null);
            }

            if ((mode & TransformCreationMode.CombineIntoGroup) == TransformCreationMode.CombineIntoGroup)
            {
                requestedTransform = new TRequestedTransform();
                transformGroup     = new TransformGroup();
                transformGroup.Children.Add(originalTransform);
                transformGroup.Children.Add(requestedTransform);
                element.RenderTransform = transformGroup;
                return(requestedTransform);
            }

            return(null);
        }