public static TextureRenderInfo BuildSingleTexture(Gwen.Renderer.Tao renderer, string url)
        {
            if (!ResProtocol.IsSingleTexture(url))
            {
                return(null);
            }

            AssetDesc desc = Scene.Instance.GetAssetDesc(url);

            if (desc == null)
            {
                return(null);
            }

            string fullpath = Path.Combine(GState.AssetRoot, SysUtil.ToWindowsPath(desc.Path));

            if (!File.Exists(fullpath))
            {
                return(null);
            }

            Gwen.Texture t;
            t = new Gwen.Texture(renderer);
            if (t == null)
            {
                return(null);
            }

            t.Load(fullpath);
            if (t.Failed)
            {
                return(null);
            }

            TextureRenderInfo tri = new TextureRenderInfo();

            tri.u1      = 0;
            tri.v1      = 0;
            tri.u2      = 1;
            tri.v2      = 1;
            tri.texture = t;
            return(tri);
        }
        public TextureRenderInfo GetTextureRenderInfo(Gwen.Renderer.Tao renderer, string url)
        {
            TextureRenderInfo tri;

            if (m_lut.TryGetValue(url, out tri))
            {
                return(tri);
            }

            // 如果是单张贴图,直接处理后返回
            if (ResProtocol.IsSingleTexture(url))
            {
                tri        = GwenTextureUtil.BuildSingleTexture(renderer, url);
                m_lut[url] = tri;
                return(tri);
            }

            string atlasName;
            string tileName;

            if (!ResProtocol.ParseURL(url, out atlasName, out tileName))
            {
                return(null);
            }

            string filePath = ResourceManager.Instance.GetAtlasFilePath(atlasName);
            Atlas  atlas    = AtlasManager.Instance.GetAtlas(renderer, filePath);

            if (atlas == null)
            {
                return(null);
            }

            ImageResource res = ResourceManager.Instance.GetResource(url);

            if (res == null)
            {
                return(null);
            }

            Texture9GridBorderInfo uv9Grid = null;
            Rectangle border9Grid          = atlas.GetTileBorderInfo(tileName);

            if (border9Grid != ucore.Const.ZERO_RECT)
            {
                uv9Grid        = new Texture9GridBorderInfo();
                uv9Grid.border = border9Grid;
                uv9Grid.size   = res.Size;
                if (border9Grid.Left != 0 && border9Grid.Right != 0)
                {
                    uv9Grid.uLeft  = ((float)res.Position.X + (float)border9Grid.Left) / (float)atlas.Texture.Width;
                    uv9Grid.uRight = ((float)res.Position.X + (float)border9Grid.Right) / (float)atlas.Texture.Width;
                }

                if (border9Grid.Top != 0 && border9Grid.Bottom != 0)
                {
                    uv9Grid.vTop    = ((float)res.Position.Y + (float)border9Grid.Top) / (float)atlas.Texture.Height;
                    uv9Grid.vBottom = ((float)res.Position.Y + (float)border9Grid.Bottom) / (float)atlas.Texture.Height;
                }
            }

            tri         = new TextureRenderInfo();
            tri.u1      = (float)res.Position.X / (float)atlas.Texture.Width;
            tri.v1      = (float)res.Position.Y / (float)atlas.Texture.Height;
            tri.u2      = ((float)res.Position.X + (float)res.Size.Width) / (float)atlas.Texture.Width;
            tri.v2      = ((float)res.Position.Y + (float)res.Size.Height) / (float)atlas.Texture.Height;
            tri.texture = atlas.Texture;
            tri.uv9Grid = uv9Grid;
            m_lut[url]  = tri;
            return(tri);
        }
Exemple #3
0
        private void DrawImage(GwenRenderContext grc, Rectangle rect, string url)
        {
            TextureRenderInfo tri = GwenTextureProvider.Instance.GetTextureRenderInfo(grc.Renderer, url);

            if (tri == null) // 找不到贴图的话,正常的处理应该用一个显眼的错误图案,这里暂时先忽略,待补充
            {
                return;
            }

            // 处理非九宫格图像渲染
            if (tri.uv9Grid == null)
            {
                grc.Renderer.DrawTexturedRect(tri.texture, rect, tri.u1, tri.v1, tri.u2, tri.v2);
                return;
            }

            // 九宫格渲染
            bool hori = tri.uv9Grid.IsHoriStreched();
            bool vert = tri.uv9Grid.IsVertStreched();

            if (hori && vert)
            {
                // ========== 完全九宫格 ===========

                // 先画四个角
                Rectangle upperLeft = rect;
                upperLeft.Width  = tri.uv9Grid.GetLeft(rect.Width);
                upperLeft.Height = tri.uv9Grid.GetTop(rect.Height);
                grc.Renderer.DrawTexturedRect(tri.texture, upperLeft, tri.u1, tri.v1, tri.uv9Grid.uLeft, tri.uv9Grid.vTop);
                Rectangle upperRight = rect;
                upperRight.Width  = tri.uv9Grid.GetRight(rect.Width);
                upperRight.Height = tri.uv9Grid.GetTop(rect.Height);
                upperRight.X      = rect.Right - upperRight.Width;
                grc.Renderer.DrawTexturedRect(tri.texture, upperRight, tri.uv9Grid.uRight, tri.v1, tri.u2, tri.uv9Grid.vTop);

                Rectangle lowerLeft = rect;
                lowerLeft.Width  = tri.uv9Grid.GetLeft(rect.Width);
                lowerLeft.Height = tri.uv9Grid.GetBottom(rect.Height);
                lowerLeft.Y      = rect.Bottom - lowerLeft.Height;
                grc.Renderer.DrawTexturedRect(tri.texture, lowerLeft, tri.u1, tri.uv9Grid.vBottom, tri.uv9Grid.uLeft, tri.v2);
                Rectangle lowerRight = rect;
                lowerRight.Width  = tri.uv9Grid.GetRight(rect.Width);
                lowerRight.Height = tri.uv9Grid.GetBottom(rect.Height);
                lowerRight.X      = rect.Right - lowerRight.Width;
                lowerRight.Y      = rect.Bottom - lowerRight.Height;
                grc.Renderer.DrawTexturedRect(tri.texture, lowerRight, tri.uv9Grid.uRight, tri.uv9Grid.vBottom, tri.u2, tri.v2);

                // 再画四个方向上的拉伸部分
                if (tri.uv9Grid.HasHoriStrechArea(rect.Width))
                {
                    grc.Renderer.DrawTexturedRect(tri.texture,
                                                  new Rectangle(rect.X + upperLeft.Width, rect.Y, rect.Width - upperLeft.Width - upperRight.Width, tri.uv9Grid.GetTop(rect.Height)),
                                                  tri.uv9Grid.uLeft, tri.v1, tri.uv9Grid.uRight, tri.uv9Grid.vTop);
                    grc.Renderer.DrawTexturedRect(tri.texture,
                                                  new Rectangle(rect.X + upperLeft.Width, rect.Bottom - lowerRight.Height, rect.Width - upperLeft.Width - upperRight.Width, lowerRight.Height),
                                                  tri.uv9Grid.uLeft, tri.uv9Grid.vBottom, tri.uv9Grid.uRight, tri.v2);
                }

                if (tri.uv9Grid.HasVertStrechArea(rect.Height))
                {
                    grc.Renderer.DrawTexturedRect(tri.texture,
                                                  new Rectangle(rect.X, rect.Y + upperLeft.Height, upperLeft.Width, rect.Height - upperLeft.Height - lowerLeft.Height),
                                                  tri.u1, tri.uv9Grid.vTop, tri.uv9Grid.uLeft, tri.uv9Grid.vBottom);
                    grc.Renderer.DrawTexturedRect(tri.texture,
                                                  new Rectangle(rect.Right - lowerRight.Width, rect.Y + upperLeft.Height, lowerRight.Width, rect.Height - upperRight.Height - lowerRight.Height),
                                                  tri.uv9Grid.uRight, tri.uv9Grid.vTop, tri.u2, tri.uv9Grid.vBottom);
                }

                // 最后画中央区域拉伸部分
                if (tri.uv9Grid.HasHoriStrechArea(rect.Width) && tri.uv9Grid.HasVertStrechArea(rect.Height))
                {
                    grc.Renderer.DrawTexturedRect(tri.texture,
                                                  new Rectangle(rect.X + upperLeft.Width,
                                                                rect.Y + upperLeft.Height,
                                                                rect.Width - upperLeft.Width - upperRight.Width,
                                                                rect.Height - upperRight.Height - lowerRight.Height),
                                                  tri.uv9Grid.uLeft, tri.uv9Grid.vTop, tri.uv9Grid.uRight, tri.uv9Grid.vBottom);
                }
            }
            else if (hori)
            {
                // ========== 横向九宫格 ===========

                // 先画左右两端
                Rectangle leftPart = rect;
                leftPart.Width = tri.uv9Grid.GetLeft(rect.Width);
                grc.Renderer.DrawTexturedRect(tri.texture, leftPart, tri.u1, tri.v1, tri.uv9Grid.uLeft, tri.v2);
                Rectangle rightPart = rect;
                rightPart.Width = tri.uv9Grid.GetRight(rect.Width);
                rightPart.X     = rect.Right - rightPart.Width;
                grc.Renderer.DrawTexturedRect(tri.texture, rightPart, tri.uv9Grid.uRight, tri.v1, tri.u2, tri.v2);

                // 再画中间拉伸部分
                if (tri.uv9Grid.HasHoriStrechArea(rect.Width))
                {
                    Rectangle middlePart = rect;
                    middlePart.Width = rect.Width - leftPart.Width - rightPart.Width;
                    middlePart.X     = rect.X + leftPart.Width;
                    grc.Renderer.DrawTexturedRect(tri.texture, middlePart, tri.uv9Grid.uLeft, tri.v1, tri.uv9Grid.uRight, tri.v2);
                }
            }
            else if (vert)
            {
                // ========== 纵向九宫格 ===========

                // 先画上下两端
                Rectangle topPart = rect;
                topPart.Height = tri.uv9Grid.GetTop(rect.Height);
                grc.Renderer.DrawTexturedRect(tri.texture, topPart, tri.u1, tri.v1, tri.u2, tri.uv9Grid.vTop);
                Rectangle bottomPart = rect;
                bottomPart.Height = tri.uv9Grid.GetBottom(rect.Height);
                bottomPart.Y      = rect.Bottom - bottomPart.Height;
                grc.Renderer.DrawTexturedRect(tri.texture, bottomPart, tri.u1, tri.uv9Grid.vBottom, tri.u2, tri.v2);

                // 再画中间拉伸部分
                if (tri.uv9Grid.HasVertStrechArea(rect.Height))
                {
                    Rectangle middlePart = rect;
                    middlePart.Height = rect.Height - topPart.Height - bottomPart.Height;
                    middlePart.Y      = rect.Y + topPart.Height;
                    grc.Renderer.DrawTexturedRect(tri.texture, middlePart, tri.u1, tri.uv9Grid.vTop, tri.u2, tri.uv9Grid.vBottom);
                }
            }
        }