예제 #1
0
    /// <summary>
    /// 拉伸 RectTransfrom
    /// </summary>
    public static void Stretch(StretchType type, RectTransform input)
    {
        switch (type)
        {
        case StretchType.StretchHorizontal:
        {
            input.anchorMin = new Vector2(0, 0.5f);
            input.anchorMax = new Vector2(1, 0.5f);
            input.pivot     = new Vector2(0.5f, 0.5f);
            input.offsetMin = new Vector2(0, input.offsetMin.y);
            input.offsetMax = new Vector2(0, input.offsetMax.y);
        }
            return;

        case StretchType.StretchVertical:
        {
            input.anchorMin = new Vector2(0.5f, 0);
            input.anchorMax = new Vector2(0.5f, 1);
            input.pivot     = new Vector2(0.5f, 0.5f);
            input.offsetMin = new Vector2(input.offsetMin.x, 0);
            input.offsetMax = new Vector2(input.offsetMax.x, 0);
        }
            return;

        case StretchType.StretchAll:
        {
            input.anchorMin = new Vector2(0, 0);
            input.anchorMax = new Vector2(1, 1);
            input.pivot     = new Vector2(0.5f, 0.5f);
            input.offsetMin = Vector2.zero;
            input.offsetMax = Vector2.zero;
        }
            return;
        }
    }
예제 #2
0
    public void SetStretch(bool screen, StretchType offsetType, Vector2 offsetValues)
    {
        screenStretch = screen;
        stretchType   = offsetType;
        stretchValues = offsetValues;
        if (uiStretch == null)
        {
            return;
        }
        uiStretch.container = screen ? null : uiLabel.gameObject;
        switch (offsetType)
        {
        case StretchType.Pixel:
            uiStretch.borderPadding = offsetValues;
            uiStretch.relativeSize  = Vector2.one;
            break;

        case StretchType.Relative:
            uiStretch.relativeSize = offsetValues;
            break;

        default:
            break;
        }
        return;
    }
예제 #3
0
        public void Init(float min = DEFAULT_IDEAL_MIN, float max = DEFAULT_IDEAL_MAX, float math = DEFAULT_MATCH)
        {
            if (!isInit)
            {
                screenSize  = new Vector2(Screen.width, Screen.height);
                screenRatio = screenSize.x / screenSize.y;

                screenModle = screenSize.x > screenSize.y
                                           ? ScreenModle.Landscape
                                           : ScreenModle.Portrait;

                if (screenModle == ScreenModle.Portrait)
                {
                    isiPhoneX = screenRatio.EQ(IPHONEX_MIN / IPHONEX_MAX);
                    idealSize = new Vector2(min, max);
                }
                else
                {
                    isiPhoneX = screenRatio.EQ(IPHONEX_MAX / IPHONEX_MIN);
                    idealSize = new Vector2(max, min);
                }
                idealRatio = idealSize.x / idealSize.y;

                // 计算canvas的大小
                float logWidth    = Mathf.Log(screenSize.x / idealSize.x, 2f);
                float logHeight   = Mathf.Log(screenSize.y / idealSize.y, 2f);
                float scaleFactor = Mathf.Pow(2f, Mathf.Lerp(logWidth, logHeight, math));
                canvasRealSize = screenSize / scaleFactor;
                canvasSize     = canvasRealSize;
                // iphoneX顶部刘海,底部圆角都是无效区域
                if (isiPhoneX)
                {
                    canvasRealSize -= screenModle == ScreenModle.Portrait
                                                  ? new Vector2(0, IPHONEX_INVALID_OFFSET)
                                                  : new Vector2(IPHONEX_INVALID_OFFSET, 0);
                }

                canvasRealRatio = canvasRealSize.x / canvasRealSize.y;

                if (canvasRealRatio.EQ(idealRatio))
                {
                    stretchType = StretchType.None;     // 未拉伸
                }
                if (canvasRealRatio.GT(idealRatio))
                {
                    stretchType = screenModle == ScreenModle.Portrait ? StretchType.Height : StretchType.Width;     // eg:iphoneX
                }
                else
                {
                    stretchType = screenModle == ScreenModle.Portrait ? StretchType.Width : StretchType.Height;     //eg: ipad
                }
                isInit = true;
            }
        }
예제 #4
0
        public static Bitmap Stretch(Bitmap image, int length, StretchType type)
        {
            Bitmap result;

            switch (type)
            {
            case StretchType.ToRight:
                result = new Bitmap(image, length, image.Height);
                return(result);

            default:
                return(image);
            }
        }
예제 #5
0
        private void TestArrangeOverrideInfiniteValues(StretchType stretch)
        {
            var imageSize = new Vector3(100, 50, 0);
            var sprite    = new Sprite {
                Region = new Rectangle(0, 0, (int)imageSize.X, (int)imageSize.Y), Borders = new Vector4(1, 2, 3, 4)
            };
            var image = new ImageElement {
                Source = (SpriteFromTexture)sprite, StretchType = stretch
            };

            image.Arrange(new Vector3(float.PositiveInfinity), false);
            Assert.Equal(imageSize, image.RenderSize);

            image.Arrange(new Vector3(150, float.PositiveInfinity, 10), false);
            Assert.Equal(stretch == StretchType.None ? imageSize : new Vector3(150, 75, 0), image.RenderSize);
        }
예제 #6
0
        public void TestMeasureOverrideInfiniteValues(StretchType stretch)
        {
            var imageSize = new Vector3(100, 50, 0);
            var Sprite    = new Sprite {
                Region = new Rectangle(0, 0, (int)imageSize.X, (int)imageSize.Y), Borders = new Vector4(1, 2, 3, 4)
            };
            var image = new ImageElement {
                Source = Sprite, StretchType = stretch
            };

            image.Measure(new Vector3(float.PositiveInfinity));
            Assert.AreEqual(imageSize, image.DesiredSizeWithMargins);

            image.Measure(new Vector3(150, float.PositiveInfinity, 10));
            Assert.AreEqual(stretch == StretchType.None ? imageSize : new Vector3(150, 75, 0), image.DesiredSizeWithMargins);
        }
        /// <summary>
        /// Stretch shapes in the list
        /// </summary>
        /// <param name="stretchShapes">The shapes to stretch</param>
        /// <param name="stretchAction">The function to use to stretch</param>
        /// <param name="defaultReferenceEdge">The function to return the default reference edge</param>
        /// <param name="stretchType">The type of stretch to perform</param>
        private void Stretch(PowerPoint.ShapeRange stretchShapes, GetAppropriateStretchAction stretchAction,
                             GetDefaultReferenceEdge defaultReferenceEdge, StretchType stretchType)
        {
            if (!ValidateSelection(stretchShapes))
            {
                return;
            }

            var referenceShape = GetReferenceShape(stretchShapes, defaultReferenceEdge, stretchType);
            var referenceEdge  = defaultReferenceEdge(new PPShape(referenceShape));

            for (var i = 1; i <= stretchShapes.Count; i++)
            {
                if (referenceShape.Equals(stretchShapes[i]))
                {
                    continue;
                }
                var stretchShape = new PPShape(stretchShapes[i]);
                var sa           = stretchAction(referenceEdge, stretchShape);
                sa(referenceEdge, stretchShape);
            }
        }
        private PowerPoint.Shape GetReferenceShape(PowerPoint.ShapeRange shapes, GetDefaultReferenceEdge getReferenceEdge,
                                                   StretchType stretchType)
        {
            var refShapeIndex = 1;

            if (ReferenceType == StretchRefType.Outermost)
            {
                var refPpShape = new PPShape(shapes[1]);
                for (var i = 2; i <= shapes.Count; i++)
                {
                    var tempPpShape = new PPShape(shapes[i]);
                    if (((stretchType == StretchType.Left || stretchType == StretchType.Top) &&
                         getReferenceEdge(refPpShape) > getReferenceEdge(tempPpShape)) ||
                        ((stretchType == StretchType.Right || stretchType == StretchType.Bottom) &&
                         getReferenceEdge(refPpShape) < getReferenceEdge(tempPpShape)))
                    {
                        refPpShape    = tempPpShape;
                        refShapeIndex = i;
                    }
                }
            }

            return(shapes[refShapeIndex]);
        }
예제 #9
0
        public void TestArrangeOverrideInfiniteValues(StretchType stretch)
        {
            var imageSize = new Vector3(100, 50, 0);
            var Sprite = new Sprite { Region = new Rectangle(0, 0, (int)imageSize.X, (int)imageSize.Y), Borders = new Vector4(1, 2, 3, 4) };
            var image = new ImageElement { Source = Sprite, StretchType = stretch };

            image.Arrange(new Vector3(float.PositiveInfinity), false);
            Assert.AreEqual(imageSize, image.RenderSize);

            image.Arrange(new Vector3(150, float.PositiveInfinity, 10), false);
            Assert.AreEqual(stretch == StretchType.None ? imageSize : new Vector3(150, 75, 0), image.RenderSize);
        }
예제 #10
0
        /// <summary>
        /// Calculates the actual image size from the size that is available.
        /// </summary>
        /// <param name="sprite"></param>
        /// <param name="availableSizeWithoutMargins"></param>
        /// <param name="stretchType"></param>
        /// <param name="stretchDirection"></param>
        /// <param name="isMeasuring"></param>
        /// <returns></returns>
        public static Vector3 CalculateImageSizeFromAvailable(Sprite sprite, Vector3 availableSizeWithoutMargins, StretchType stretchType, StretchDirection stretchDirection, bool isMeasuring)
        {
            if (sprite == null) // no associated image -> no region needed
            {
                return(Vector3.Zero);
            }

            var idealSize = sprite.SizeInPixels;

            if (idealSize.X <= 0 || idealSize.Y <= 0) // image size null or invalid -> no region needed
            {
                return(Vector3.Zero);
            }

            if (float.IsInfinity(availableSizeWithoutMargins.X) && float.IsInfinity(availableSizeWithoutMargins.Y)) // unconstrained available size -> take the best size for the image: the image size
            {
                return(new Vector3(idealSize, 0));
            }

            // initialize the desired size with maximum available size
            var desiredSize = availableSizeWithoutMargins;

            // compute the desired image ratios
            var desiredScale = new Vector2(desiredSize.X / idealSize.X, desiredSize.Y / idealSize.Y);

            // when the size along a given axis is free take the same ratio as the constrained axis.
            if (float.IsInfinity(desiredScale.X))
            {
                desiredScale.X = desiredScale.Y;
            }
            if (float.IsInfinity(desiredScale.Y))
            {
                desiredScale.Y = desiredScale.X;
            }

            // adjust the scales depending on the type of stretch to apply
            switch (stretchType)
            {
            case StretchType.None:
                desiredScale = Vector2.One;
                break;

            case StretchType.Uniform:
                desiredScale.X = desiredScale.Y = Math.Min(desiredScale.X, desiredScale.Y);
                break;

            case StretchType.UniformToFill:
                desiredScale.X = desiredScale.Y = Math.Max(desiredScale.X, desiredScale.Y);
                break;

            case StretchType.FillOnStretch:
                // if we are only measuring we prefer keeping the image resolution than using all the available space.
                if (isMeasuring)
                {
                    desiredScale.X = desiredScale.Y = Math.Min(desiredScale.X, desiredScale.Y);
                }
                break;

            case StretchType.Fill:
                break;

            default:
                throw new ArgumentOutOfRangeException(nameof(stretchType));
            }

            // adjust the scales depending on the stretch directions
            switch (stretchDirection)
            {
            case StretchDirection.Both:
                break;

            case StretchDirection.DownOnly:
                desiredScale.X = Math.Min(desiredScale.X, 1);
                desiredScale.Y = Math.Min(desiredScale.Y, 1);
                break;

            case StretchDirection.UpOnly:
                desiredScale.X = Math.Max(1, desiredScale.X);
                desiredScale.Y = Math.Max(1, desiredScale.Y);
                break;

            default:
                throw new ArgumentOutOfRangeException(nameof(stretchDirection));
            }

            // update the desired size based on the desired scales
            desiredSize = new Vector3(idealSize.X * desiredScale.X, idealSize.Y * desiredScale.Y, 0f);

            if (!isMeasuring || !sprite.HasBorders)
            {
                return(desiredSize);
            }

            // consider sprite borders
            var borderSum = new Vector2(sprite.BordersInternal.X + sprite.BordersInternal.Z, sprite.BordersInternal.Y + sprite.BordersInternal.W);

            if (sprite.Orientation == ImageOrientation.Rotated90)
            {
                Utilities.Swap(ref borderSum.X, ref borderSum.Y);
            }

            return(new Vector3(Math.Max(desiredSize.X, borderSum.X), Math.Max(desiredSize.Y, borderSum.Y), desiredSize.Z));
        }
        private PowerPoint.Shape GetReferenceShape(PowerPoint.ShapeRange shapes, GetDefaultReferenceEdge getReferenceEdge,
            StretchType stretchType)
        {
            var refShapeIndex = 1;

            if (ReferenceType == StretchRefType.Outermost)
            {
                var refPpShape = new PPShape(shapes[1]);
                for (var i = 2; i <= shapes.Count; i++)
                {
                    var tempPpShape = new PPShape(shapes[i]);
                    if (((stretchType == StretchType.Left || stretchType == StretchType.Top) &&
                        getReferenceEdge(refPpShape) > getReferenceEdge(tempPpShape)) ||
                        ((stretchType == StretchType.Right || stretchType == StretchType.Bottom) &&
                        getReferenceEdge(refPpShape) < getReferenceEdge(tempPpShape)))
                    {
                        refPpShape = tempPpShape;
                        refShapeIndex = i;
                    }
                }
            }

            return shapes[refShapeIndex];

        }
        /// <summary>
        /// Stretch shapes in the list
        /// </summary>
        /// <param name="stretchShapes">The shapes to stretch</param>
        /// <param name="stretchAction">The function to use to stretch</param>
        /// <param name="defaultReferenceEdge">The function to return the default reference edge</param>
        /// <param name="stretchType">The type of stretch to perform</param>
        private void Stretch(PowerPoint.ShapeRange stretchShapes, GetAppropriateStretchAction stretchAction, 
            GetDefaultReferenceEdge defaultReferenceEdge, StretchType stretchType)
        {
            if (!ValidateSelection(stretchShapes))
            {
                return;
            }

            var referenceShape = GetReferenceShape(stretchShapes, defaultReferenceEdge, stretchType);
            var referenceEdge = defaultReferenceEdge(new PPShape(referenceShape));

            for (var i = 1; i <= stretchShapes.Count; i++)
            {
                if (referenceShape.Equals(stretchShapes[i]))
                {
                    continue;
                }
                var stretchShape = new PPShape(stretchShapes[i]);
                var sa = stretchAction(referenceEdge, stretchShape);
                sa(referenceEdge, stretchShape);
            }
        }
예제 #13
0
        /// <summary>
        /// Calculates the actual image size from the size that is available.
        /// </summary>
        /// <param name="sprite"></param>
        /// <param name="availableSizeWithoutMargins"></param>
        /// <param name="stretchType"></param>
        /// <param name="stretchDirection"></param>
        /// <param name="isMeasuring"></param>
        /// <returns></returns>
        public static Vector3 CalculateImageSizeFromAvailable(Sprite sprite, Vector3 availableSizeWithoutMargins, StretchType stretchType, StretchDirection stretchDirection, bool isMeasuring)
        {
            if (sprite == null) // no associated image -> no region needed
                return Vector3.Zero;

            var idealSize = sprite.SizeInPixels;
            if (idealSize.X <= 0 || idealSize.Y <= 0) // image size null or invalid -> no region needed
                return Vector3.Zero;

            if (float.IsInfinity(availableSizeWithoutMargins.X) && float.IsInfinity(availableSizeWithoutMargins.Y)) // unconstrained available size -> take the best size for the image: the image size
                return new Vector3(idealSize, 0);

            // initialize the desired size with maximum available size
            var desiredSize = availableSizeWithoutMargins;

            // compute the desired image ratios
            var desiredScale = new Vector2(desiredSize.X / idealSize.X, desiredSize.Y / idealSize.Y);

            // when the size along a given axis is free take the same ratio as the constrained axis.
            if (float.IsInfinity(desiredScale.X))
                desiredScale.X = desiredScale.Y;
            if (float.IsInfinity(desiredScale.Y))
                desiredScale.Y = desiredScale.X;

            // adjust the scales depending on the type of stretch to apply
            switch (stretchType)
            {
                case StretchType.None:
                    desiredScale = Vector2.One;
                    break;
                case StretchType.Uniform:
                    desiredScale.X = desiredScale.Y = Math.Min(desiredScale.X, desiredScale.Y);
                    break;
                case StretchType.UniformToFill:
                    desiredScale.X = desiredScale.Y = Math.Max(desiredScale.X, desiredScale.Y);
                    break;
                case StretchType.FillOnStretch:
                    // if we are only measuring we prefer keeping the image resolution than using all the available space.
                    if (isMeasuring)
                        desiredScale.X = desiredScale.Y = Math.Min(desiredScale.X, desiredScale.Y);
                    break;
                case StretchType.Fill:
                    break;
                default:
                    throw new ArgumentOutOfRangeException(nameof(stretchType));
            }

            // adjust the scales depending on the stretch directions
            switch (stretchDirection)
            {
                case StretchDirection.Both:
                    break;
                case StretchDirection.DownOnly:
                    desiredScale.X = Math.Min(desiredScale.X, 1);
                    desiredScale.Y = Math.Min(desiredScale.Y, 1);
                    break;
                case StretchDirection.UpOnly:
                    desiredScale.X = Math.Max(1, desiredScale.X);
                    desiredScale.Y = Math.Max(1, desiredScale.Y);
                    break;
                default:
                    throw new ArgumentOutOfRangeException(nameof(stretchDirection));
            }

            // update the desired size based on the desired scales
            desiredSize = new Vector3(idealSize.X * desiredScale.X, idealSize.Y * desiredScale.Y, 0f);
            
            if (!isMeasuring || !sprite.HasBorders)
                return desiredSize;

            // consider sprite borders
            var borderSum = new Vector2(sprite.BordersInternal.X + sprite.BordersInternal.Z, sprite.BordersInternal.Y + sprite.BordersInternal.W);
            if (sprite.Orientation == ImageOrientation.Rotated90)
                Utilities.Swap(ref borderSum.X, ref borderSum.Y);

            return new Vector3(Math.Max(desiredSize.X, borderSum.X), Math.Max(desiredSize.Y, borderSum.Y), desiredSize.Z);
        }
예제 #14
0
        public void TestMeasureOverrideInfiniteValues(StretchType stretch)
        {
            var imageSize = new Vector3(100, 50, 0);
            var sprite = new Sprite { Region = new Rectangle(0, 0, (int)imageSize.X, (int)imageSize.Y), Borders = new Vector4(1, 2, 3, 4) };
            var image = new ImageElement { Source = (SpriteFromTexture)sprite, StretchType = stretch };
            
            image.Measure(new Vector3(float.PositiveInfinity));
            Assert.AreEqual(imageSize, image.DesiredSizeWithMargins);

            image.Measure(new Vector3(150, float.PositiveInfinity, 10));
            Assert.AreEqual(stretch == StretchType.None ? imageSize : new Vector3(150, 75, 0), image.DesiredSizeWithMargins);
        }