Exemplo n.º 1
0
        public static Frame CreateFromNode(Wz_Node frameNode, GraphicsDevice graphicsDevice, GlobalFindNodeFunction findNode)
        {
            if (frameNode == null || frameNode.Value == null)
            {
                return(null);
            }

            while (frameNode.Value is Wz_Uol)
            {
                Wz_Uol  uol     = frameNode.Value as Wz_Uol;
                Wz_Node uolNode = uol.HandleUol(frameNode);
                if (uolNode != null)
                {
                    frameNode = uolNode;
                }
            }
            if (frameNode.Value is Wz_Png)
            {
                var    linkNode = frameNode.GetLinkedSourceNode(findNode);
                Wz_Png png      = linkNode?.GetValue <Wz_Png>() ?? (Wz_Png)frameNode.Value;

                var frame = new Frame(png.ToTexture(graphicsDevice))
                {
                    Png = png,
                };

                foreach (Wz_Node propNode in frameNode.Nodes)
                {
                    switch (propNode.Text)
                    {
                    case "origin":
                        frame.Origin = (propNode.Value as Wz_Vector).ToPoint();
                        break;

                    case "delay":
                        frame.Delay = propNode.GetValue <int>();
                        break;

                    case "z":
                        frame.Z = propNode.GetValue <int>();
                        break;

                    case "a0":
                        frame.A0 = propNode.GetValue <int>();
                        break;

                    case "a1":
                        frame.A1 = propNode.GetValue <int>();
                        break;
                    }
                }

                if (frame.Delay == 0)
                {
                    frame.Delay = 100;//给予默认delay
                }
                return(frame);
            }
            return(null);
        }
Exemplo n.º 2
0
        public static Texture2D ToTexture(this Wz_Png png, GraphicsDevice graphicsDevice)
        {
            var format = GetTextureFormatOfPng(png.Form);

            if (format == SurfaceFormat.Bgra4444)
            {
                //检测是否支持 pre-win8
                if (!graphicsDevice.IsSupportBgra4444())
                {
                    format = SurfaceFormat.Bgra32;
                }
            }
            else if (format == SurfaceFormat.Bgr565)
            {
                //检测是否支持 pre-win8
                if (!graphicsDevice.IsSupportBgr565())
                {
                    format = SurfaceFormat.Bgra32;
                }
            }
            else if (format == SurfaceFormat.Bgra5551)
            {
                //检测是否支持 pre-win8
                if (!graphicsDevice.IsSupportBgra5551())
                {
                    format = SurfaceFormat.Bgra32;
                }
            }

            var t2d = new Texture2D(graphicsDevice, png.Width, png.Height, false, format);

            png.ToTexture(t2d, Point.Zero);
            return(t2d);
        }
        public void Load(AtlasPage page, string path)
        {
            var frameNode = this.TopNode.FindNodeByPath(path);

            if (frameNode == null || frameNode.Value == null)
            {
                return;
            }

            if (frameNode.Value is Wz_Uol)
            {
                Wz_Uol  uol     = frameNode.Value as Wz_Uol;
                Wz_Node uolNode = uol.HandleUol(frameNode);
                if (uolNode != null)
                {
                    frameNode = uolNode;
                }
            }

            if (frameNode.Value is Wz_Png)
            {
                var    linkNode = frameNode.GetLinkedSourceNode(FindNodeFunction);
                Wz_Png png      = linkNode?.GetValue <Wz_Png>() ?? (Wz_Png)frameNode.Value;
                page.rendererObject = png.ToTexture(this.GraphicsDevice);
                page.width          = png.Width;
                page.height         = png.Height;
            }
        }
Exemplo n.º 4
0
        public static Texture2D ToTexture(this Wz_Png png, GraphicsDevice graphicsDevice)
        {
            var       format = GetTextureFormatOfPng(png.Form);
            Texture2D t2d    = null;

            if (format == SurfaceFormat.Bgra4444)
            {
                //检测是否支持
                if (graphicsDevice.IsSupportBgra4444())
                {
                    t2d = MonogameUtils.CreateTexture_BGRA4444(graphicsDevice, png.Width, png.Height);
                }
                else
                {
                    format = SurfaceFormat.Bgra32;
                }
            }
            if (t2d == null)
            {
                t2d = new Texture2D(graphicsDevice, png.Width, png.Height, false, format);
            }

            png.ToTexture(t2d, Point.Zero);
            return(t2d);
        }
Exemplo n.º 5
0
        public static void ToTexture(this Wz_Png png, Texture2D texture, Point origin)
        {
            Rectangle rect = new Rectangle(origin, new Point(png.Width, png.Height));

            //检查大小
            if (rect.X < 0 || rect.Y < 0 || rect.Right > texture.Width || rect.Bottom > texture.Height)
            {
                throw new ArgumentException("Png rectangle is out of bounds.");
            }

            //检查像素格式
            var format = GetTextureFormatOfPng(png.Form);

            if (texture.Format == SurfaceFormat.Bgra32)
            {
                using (var bmp = png.ExtractPng())
                {
                    bmp.ToTexture(texture, origin);
                }
            }
            else if (texture.Format != format)
            {
                throw new ArgumentException($"Texture format({texture.Format}) does not fit the png form({png.Form}).");
            }
            else
            {
                byte[] plainData = png.GetRawData();
                if (plainData == null)
                {
                    throw new Exception("png decoding failed.");
                }

                switch (png.Form)
                {
                case 1:
                case 2:
                case 257:
                case 513:
                case 1026:
                case 2050:
                    texture.SetData(0, 0, rect, plainData, 0, plainData.Length);
                    break;

                case 3:
                    var pixel = Wz_Png.GetPixelDataForm3(plainData, png.Width, png.Height);
                    texture.SetData(0, 0, rect, pixel, 0, pixel.Length);
                    break;

                case 517:
                    pixel = Wz_Png.GetPixelDataForm517(plainData, png.Width, png.Height);
                    texture.SetData(0, 0, rect, pixel, 0, pixel.Length);
                    break;

                default:
                    throw new Exception($"unknown png form ({png.Form}).");
                }
            }
        }
Exemplo n.º 6
0
        public static GifFrame CreateFrameFromNode(Wz_Node frameNode, GlobalFindNodeFunction findNode)
        {
            if (frameNode == null || frameNode.Value == null)
            {
                return(null);
            }

            while (frameNode.Value is Wz_Uol)
            {
                Wz_Uol  uol     = frameNode.Value as Wz_Uol;
                Wz_Node uolNode = uol.HandleUol(frameNode);
                if (uolNode != null)
                {
                    frameNode = uolNode;
                }
            }
            if (frameNode.Value is Wz_Png)
            {
                var    linkNode = frameNode.GetLinkedSourceNode(findNode);
                Wz_Png png      = linkNode?.GetValue <Wz_Png>() ?? (Wz_Png)frameNode.Value;

                var gifFrame = new GifFrame(png.ExtractPng());
                foreach (Wz_Node propNode in frameNode.Nodes)
                {
                    switch (propNode.Text)
                    {
                    case "origin":
                        gifFrame.Origin = (propNode.Value as Wz_Vector);
                        break;

                    case "delay":
                        gifFrame.Delay = propNode.GetValue <int>();
                        break;

                    case "a0":
                        gifFrame.A0 = propNode.GetValue <int>();
                        break;

                    case "a1":
                        gifFrame.A1 = propNode.GetValue <int>();
                        break;
                    }
                }
                if (gifFrame.Delay == 0)
                {
                    gifFrame.Delay = 100;//给予默认delay
                }
                return(gifFrame);
            }
            return(null);
        }
Exemplo n.º 7
0
        public void ShowImage(Wz_Png png)
        {
            //添加到动画控件
            var frame = new Animation.Frame()
            {
                Texture = png.ToTexture(this.GraphicsDevice),
                Png     = png,
                Delay   = 0,
                Origin  = Point.Zero,
            };

            var frameData = new Animation.FrameAnimationData();

            frameData.Frames.Add(frame);

            this.ShowAnimation(frameData);
        }
Exemplo n.º 8
0
        private Texture2D GetTexture(Wz_Png png, string key)
        {
            TextureItem texItem;

            if (!this.loadedTexture.TryGetValue(key, out texItem))
            {
                texItem = new TextureItem()
                {
                    Texture = PngToTexture(this.GraphicsDevice, png)
                };
                this.loadedTexture[key] = texItem;
            }
            else
            {
            }

            if (IsCounting)
            {
                texItem.counter++;
            }
            return(texItem.Texture);
        }
Exemplo n.º 9
0
        public static BitmapOrigin CreateFromNode(Wz_Node node, GlobalFindNodeFunction findNode)
        {
            BitmapOrigin bp = new BitmapOrigin();
            Wz_Uol       uol;

            while ((uol = node.GetValue <Wz_Uol>(null)) != null)
            {
                node = uol.HandleUol(node);
            }

            //获取linkNode
            var    linkNode = node.GetLinkedSourceNode(findNode);
            Wz_Png png      = linkNode?.GetValue <Wz_Png>() ?? (Wz_Png)node.Value;

            bp.Bitmap = png?.ExtractPng();
            Wz_Node   originNode = node.FindNodeByPath("origin");
            Wz_Vector vec        = (originNode == null) ? null : originNode.GetValue <Wz_Vector>();

            bp.Origin = (vec == null) ? new Point() : new Point(vec.X, vec.Y);

            return(bp);
        }
Exemplo n.º 10
0
        public static PngInfo ToPngInfo(this Wz_Png wz_Png)
        {
            string base64;

            using (var bmp = wz_Png.ExtractPng())
            {
                using (var ms = new MemoryStream())
                {
                    bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
                    byte[] data = ms.ToArray();
                    base64 = Convert.ToBase64String(data);
                }
            }
            return(new PngInfo
            {
                Width = wz_Png.Width,
                Height = wz_Png.Height,
                Base64Data = base64,
                DataLength = wz_Png.DataLength,
                Form = wz_Png.Form
            });
        }
Exemplo n.º 11
0
        public RenderFrame CreateFrame(Wz_Node frameNode)
        {
            string key = frameNode.FullPathToFile.Replace('\\', '/');
            Wz_Png png = null;

            string source = frameNode.FindNodeByPath("source").GetValueEx <string>(null);

            if (!string.IsNullOrEmpty(source))
            {
                Wz_Node sourceNode = PluginManager.FindWz(source);
                if (sourceNode != null)
                {
                    png = sourceNode.Value as Wz_Png;
                    key = sourceNode.FullPathToFile.Replace('\\', '/');
                }
            }
            else
            {
                png = frameNode.Value as Wz_Png;
            }

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

            RenderFrame frame = new RenderFrame();
            Wz_Vector   vec   = frameNode.FindNodeByPath("origin").GetValueEx <Wz_Vector>(null);

            frame.Texture = GetTexture(png, key);
            frame.Origin  = (vec == null ? new Vector2() : new Vector2(vec.X, vec.Y));
            frame.Delay   = frameNode.FindNodeByPath("delay").GetValueEx <int>(100);
            frame.Z       = frameNode.FindNodeByPath("z").GetValueEx <int>(0);
            frame.A0      = frameNode.FindNodeByPath("a0").GetValueEx <int>(255);
            frame.A1      = frameNode.FindNodeByPath("a1").GetValueEx <int>(frame.A0);
            return(frame);
        }
Exemplo n.º 12
0
 public static Texture2D PngToTexture(GraphicsDevice device, Wz_Png png)
 {
     return(WzComparerR2.Rendering.WzLibExtension.ToTexture(png, device));
 }
Exemplo n.º 13
0
        public static Texture2D ToTexture(this Wz_Png png, GraphicsDevice graphicsDevice)
        {
            byte[] plainData = png.GetRawData();
            if (plainData == null)
            {
                return(null);
            }

            Texture2D t2d;

            switch (png.Form)
            {
            case 1:
                t2d = null;
                try
                {
                    t2d = MonogameUtils.CreateTexture_BGRA4444(graphicsDevice, png.Width, png.Height);
                    t2d.SetData(plainData);
                }
                catch      //monogame并不支持这个format 用gdi+转
                {
                    if (t2d != null)
                    {
                        t2d.Dispose();
                    }
                    goto default;
                }
                break;

            case 2:
                t2d = new Texture2D(graphicsDevice, png.Width, png.Height, false, SurfaceFormat.Color);
                MonogameUtils.BgraToColor(plainData);
                t2d.SetData(plainData);
                break;

            case 513:
                t2d = new Texture2D(graphicsDevice, png.Width, png.Height, false, SurfaceFormat.Bgr565);
                t2d.SetData(plainData);
                break;

            case 517:
                t2d = new Texture2D(graphicsDevice, png.Width, png.Height, false, SurfaceFormat.Bgr565);
                byte[] texData = new byte[png.Width * png.Height * 2];
                for (int j0 = 0, j1 = png.Height / 16; j0 < j1; j0++)
                {
                    int idxTex = j0 * 16 * png.Width * 2;
                    for (int i0 = 0, i1 = png.Width / 16; i0 < i1; i0++)
                    {
                        int idx = (i0 + j0 * i1) * 2;

                        for (int k = 0; k < 16; k++)
                        {
                            texData[idxTex + i0 * 32 + k * 2]     = plainData[idx];
                            texData[idxTex + i0 * 32 + k * 2 + 1] = plainData[idx + 1];
                        }
                    }
                    for (int k = 1; k < 16; k++)
                    {
                        System.Buffer.BlockCopy(texData, idxTex, texData, idxTex + k * png.Width * 2, png.Width * 2);
                    }
                }
                t2d.SetData(texData);
                break;

            case 1026:
                t2d = new Texture2D(graphicsDevice, png.Width, png.Height, false, SurfaceFormat.Dxt3);
                t2d.SetData(plainData);
                break;

            case 2050:
                t2d = new Texture2D(graphicsDevice, png.Width, png.Height, false, SurfaceFormat.Dxt5);
                t2d.SetData(plainData);
                break;

            default:     //默认从bitmap复制 二次转换
                var bitmap = png.ExtractPng();
                t2d = bitmap.ToTexture(graphicsDevice);
                bitmap.Dispose();
                break;
            }

            return(t2d);
        }
Exemplo n.º 14
0
        /// <summary>
        /// 比较两个节点绑定的值是否相同。
        /// </summary>
        /// <param Name="dataNew">新的值。</param>
        /// <param Name="dataOld">旧的值。</param>
        /// <returns></returns>
        public virtual bool CompareData(object dataNew, object dataOld)
        {
            // skip virtual dir
            {
                if (dataNew is Wz_File fileNew && fileNew.IsSubDir)
                {
                    dataNew = null;
                }
                if (dataOld is Wz_File fileOld && fileOld.IsSubDir)
                {
                    dataNew = null;
                }
            }

            if (dataNew == null && dataOld == null)
            {
                return(true);
            }
            if (dataNew == null ^ dataOld == null)
            {
                return(false);
            }

            Type type = dataNew.GetType();

            if (type != dataOld.GetType())
            {
                return(false);
            }

            if (type.IsClass)
            {
                switch (dataNew)
                {
                case string str:
                    return(str == (string)dataOld);

                case Wz_Image img:
                    Wz_Image imgOld = (Wz_Image)dataOld;
                    return(img.Size == imgOld.Size && img.Checksum == imgOld.Checksum);

                case Wz_File file:
                    Wz_File fileOld = (Wz_File)dataOld;
                    return(file.Type == fileOld.Type);

                case Wz_Png png:
                    Wz_Png pngOld = (Wz_Png)dataOld;
                    switch (this.PngComparison)
                    {
                    case WzPngComparison.SizeOnly:
                        return(png.Width == pngOld.Width && png.Height == pngOld.Height);

                    case WzPngComparison.SizeAndDataLength:
                        return(png.Width == pngOld.Width &&
                               png.Height == pngOld.Height &&
                               png.DataLength == pngOld.DataLength);

                    case WzPngComparison.Pixel:
                        if (!(png.Width == pngOld.Width && png.Height == pngOld.Height && png.Form == pngOld.Form))
                        {
                            return(false);
                        }
                        byte[] pixelNew = png.GetRawData();
                        byte[] pixelOld = pngOld.GetRawData();
                        if (pixelNew == null || pixelOld == null || pixelNew.Length != pixelOld.Length)
                        {
                            return(false);
                        }
                        for (int i = 0, i1 = pixelNew.Length; i < i1; i++)
                        {
                            if (pixelNew[i] != pixelOld[i])
                            {
                                return(false);
                            }
                        }
                        return(true);

                    default:
                        goto case WzPngComparison.SizeAndDataLength;
                    }
                    break;

                case Wz_Vector vector:
                    Wz_Vector vectorOld = (Wz_Vector)dataOld;
                    return(vector.X == vectorOld.X && vector.Y == vectorOld.Y);

                case Wz_Uol uol:
                    return(uol.Uol == ((Wz_Uol)dataOld).Uol);

                case Wz_Sound sound:
                    Wz_Sound soundOld = (Wz_Sound)dataOld;
                    return(sound.Ms == soundOld.Ms && sound.DataLength == soundOld.DataLength);
                }
            }

            return(object.Equals(dataNew, dataOld));
        }