コード例 #1
0
        protected override Texture CreateTexture(Size size, GraphicsLoadContext loadContext)
        {
            return(MappedImageUtility.CreateTexture(loadContext, size, spriteBatch =>
            {
                var requiresFlip = !loadContext.GraphicsDevice.IsUvOriginTopLeft;

                var leftWidth = _left.Coords.Width;
                var rightWidth = _right.Coords.Width;

                var leftRect = new Rectangle(0, 0, leftWidth, size.Height);

                spriteBatch.DrawImage(
                    _left.Texture.Value,
                    _left.Coords,
                    leftRect.ToRectangleF(),
                    ColorRgbaF.White,
                    requiresFlip);

                var middleRect = new Rectangle(leftRect.Right, 0, size.Width - leftWidth - rightWidth, size.Height);
                spriteBatch.DrawImage(
                    _middle.Texture.Value,
                    _middle.Coords,
                    middleRect.ToRectangleF(),
                    ColorRgbaF.White,
                    requiresFlip);

                var rightRect = new Rectangle(middleRect.Right, 0, rightWidth, size.Height);
                spriteBatch.DrawImage(
                    _right.Texture.Value,
                    _right.Coords,
                    rightRect.ToRectangleF(),
                    ColorRgbaF.White,
                    requiresFlip);
            }));
        }
コード例 #2
0
        public Image CreateStretchableImage(
            WndDrawData wndDrawData,
            int leftIndex,
            int middleIndex,
            int rightIndex)
        {
            var leftImage   = wndDrawData.Items[leftIndex].Image;
            var middleImage = wndDrawData.Items[middleIndex].Image;
            var rightImage  = wndDrawData.Items[rightIndex].Image;

            var leftMappedImageTexture   = GetMappedImage(leftImage);
            var middleMappedImageTexture = GetMappedImage(middleImage);
            var rightMappedImageTexture  = GetMappedImage(rightImage);

            if (leftMappedImageTexture != null &&
                middleMappedImageTexture != null &&
                rightMappedImageTexture != null)
            {
                var naturalSize = new Size(
                    leftMappedImageTexture.SourceRect.Width + middleMappedImageTexture.SourceRect.Width + rightMappedImageTexture.SourceRect.Width,
                    leftMappedImageTexture.SourceRect.Height);

                return(new Image(naturalSize, size =>
                {
                    var cacheKey = new WndImageKey
                    {
                        DestinationSize = size,
                        LeftImage = leftImage,
                        MiddleImage = middleImage,
                        RightImage = rightImage
                    };

                    if (!_cache.TryGetValue(cacheKey, out var result))
                    {
                        result = CreateTexture(
                            cacheKey,
                            spriteBatch =>
                        {
                            var leftWidth = leftMappedImageTexture.SourceRect.Width;
                            var rightWidth = rightMappedImageTexture.SourceRect.Width;
                            var leftRect = new Rectangle(0, 0, leftWidth, cacheKey.DestinationSize.Height);
                            spriteBatch.DrawImage(
                                leftMappedImageTexture.Texture,
                                leftMappedImageTexture.SourceRect,
                                leftRect.ToRectangleF(),
                                ColorRgbaF.White);
                            var middleRect = new Rectangle(leftRect.Right, 0, cacheKey.DestinationSize.Width - leftWidth - rightWidth, cacheKey.DestinationSize.Height);
                            spriteBatch.DrawImage(
                                middleMappedImageTexture.Texture,
                                middleMappedImageTexture.SourceRect,
                                middleRect.ToRectangleF(),
                                ColorRgbaF.White);
                            var rightRect = new Rectangle(middleRect.Right, 0, rightWidth, cacheKey.DestinationSize.Height);
                            spriteBatch.DrawImage(
                                rightMappedImageTexture.Texture,
                                rightMappedImageTexture.SourceRect,
                                rightRect.ToRectangleF(),
                                ColorRgbaF.White);
                        });

                        _cache.Add(cacheKey, result);
                    }

                    return result;
                }));
            }
            else
            {
                return(null);
            }
        }
コード例 #3
0
ファイル: WndImageLoader.cs プロジェクト: hcll123456/OpenSAGE
        public Image CreateStretchableImage(
            WndDrawData wndDrawData,
            int leftIndex,
            int middleIndex,
            int rightIndex)
        {
            var leftImage   = wndDrawData.Items[leftIndex].Image;
            var middleImage = wndDrawData.Items[middleIndex].Image;
            var rightImage  = wndDrawData.Items[rightIndex].Image;

            var leftMappedImageTexture   = GetMappedImage(leftImage);
            var middleMappedImageTexture = GetMappedImage(middleImage);
            var rightMappedImageTexture  = GetMappedImage(rightImage);

            if (leftMappedImageTexture != null &&
                middleMappedImageTexture != null &&
                rightMappedImageTexture != null)
            {
                var naturalSize = new Size(
                    leftMappedImageTexture.Coords.Width + middleMappedImageTexture.Coords.Width + rightMappedImageTexture.Coords.Width,
                    leftMappedImageTexture.Coords.Height);

                bool requiresFlip = !_contentManager.GraphicsDevice.IsUvOriginTopLeft;

                return(new Image("WndImage", naturalSize, size =>
                {
                    var cacheKey = new WndImageKey
                    {
                        DestinationSize = size,
                        LeftImage = leftImage,
                        MiddleImage = middleImage,
                        RightImage = rightImage
                    };

                    if (!_cache.TryGetValue(cacheKey, out var result))
                    {
                        result = CreateTexture(
                            cacheKey,
                            spriteBatch =>
                        {
                            var leftWidth = leftMappedImageTexture.Coords.Width;
                            var rightWidth = rightMappedImageTexture.Coords.Width;
                            var leftRect = new Rectangle(0, 0, leftWidth, cacheKey.DestinationSize.Height);
                            spriteBatch.DrawImage(
                                leftMappedImageTexture.Texture.Value,
                                leftMappedImageTexture.Coords,
                                leftRect.ToRectangleF(),
                                ColorRgbaF.White,
                                requiresFlip);
                            var middleRect = new Rectangle(leftRect.Right, 0, cacheKey.DestinationSize.Width - leftWidth - rightWidth, cacheKey.DestinationSize.Height);
                            spriteBatch.DrawImage(
                                middleMappedImageTexture.Texture.Value,
                                middleMappedImageTexture.Coords,
                                middleRect.ToRectangleF(),
                                ColorRgbaF.White,
                                requiresFlip);
                            var rightRect = new Rectangle(middleRect.Right, 0, rightWidth, cacheKey.DestinationSize.Height);
                            spriteBatch.DrawImage(
                                rightMappedImageTexture.Texture.Value,
                                rightMappedImageTexture.Coords,
                                rightRect.ToRectangleF(),
                                ColorRgbaF.White,
                                requiresFlip);
                        });

                        _cache.Add(cacheKey, result);
                    }

                    return result;
                }));
            }
            else
            {
                return(null);
            }
        }