public int ReadFrom(byte[] Buffer, int StartIndex = 0)
        {
            int cursor = StartIndex;

            Width   = BitConverter.ToUInt32(Buffer, cursor);
            cursor += TypeSizes.INT;

            Height  = BitConverter.ToUInt32(Buffer, cursor);
            cursor += TypeSizes.INT;

            XOffset = BitConverter.ToInt32(Buffer, cursor);
            cursor += TypeSizes.INT;

            YOffset = BitConverter.ToInt32(Buffer, cursor);
            cursor += TypeSizes.INT;

            byte HotSpotCount = Buffer[cursor];

            cursor++;

            HotSpots.Clear();
            HotSpots.Capacity = HotSpotCount;
            for (int i = 0; i < HotSpotCount; i++)
            {
                BgfBitmapHotspot hotspot = new BgfBitmapHotspot(Buffer, cursor);
                cursor += hotspot.ByteLength;

                HotSpots.Add(hotspot);
            }

            isCompressed = BitConverter.ToBoolean(Buffer, cursor);
            cursor++;

            CompressedLength = BitConverter.ToInt32(Buffer, cursor);
            cursor          += TypeSizes.INT;

            if (IsCompressed)
            {
                PixelData = new byte[CompressedLength];
                Array.Copy(Buffer, cursor, PixelData, 0, CompressedLength);
                cursor += CompressedLength;
            }
            else
            {
                PixelData = new byte[UncompressedLength];
                Array.Copy(Buffer, cursor, PixelData, 0, UncompressedLength);
                cursor += UncompressedLength;
            }
            return(ByteLength);
        }
Exemple #2
0
        /// <summary>
        /// Loads this BgfFile instance from XML/BMP
        /// </summary>
        /// <param name="Filename"></param>
        public void LoadXml(string Filename)
        {
            string path = Path.GetDirectoryName(Filename);

            // save raw filename without path or extensions
            Filename = Path.GetFileNameWithoutExtension(Filename);

            // init XML reader
            XmlReader reader = XmlReader.Create(Filename);

            // rootnode
            reader.ReadToFollowing("bgf");
            uint version = Convert.ToUInt32(reader["version"]);
            if (version >= BgfFile.VERSION9)
            {
                ShrinkFactor = Convert.ToUInt32(reader["shrink"]);

                // skip maxindices, we use dynamic getter
                // MaxIndices = Convert.ToUInt32(reader["maxindices"]);

                // frames
                reader.ReadToFollowing("frames");
                int FrameCount = Convert.ToInt32(reader["count"]);
                Frames.Capacity = FrameCount;
                for (int i = 0; i < FrameCount; i++)
                {
                    reader.ReadToFollowing("frame");
                    uint width = Convert.ToUInt32(reader["width"]);
                    uint height = Convert.ToUInt32(reader["height"]);
                    int xoffset = Convert.ToInt32(reader["xoffset"]);
                    int yoffset = Convert.ToInt32(reader["yoffset"]);
                    string file = reader["file"];

                    // hotspots
                    reader.ReadToFollowing("hotspots");
                    byte hotspotcount = Convert.ToByte(reader["count"]);
                    List<BgfBitmapHotspot> hotspots = new List<BgfBitmapHotspot>(hotspotcount);
                    for (int j = 0; j < hotspotcount; j++)
                    {
                        reader.ReadToFollowing("hotspot");
                        sbyte index = Convert.ToSByte(reader["index"]);
                        int x = Convert.ToInt32(reader["x"]);
                        int y = Convert.ToInt32(reader["y"]);

                        BgfBitmapHotspot hotspot = new BgfBitmapHotspot(index, x, y);
                        hotspots.Add(hotspot);
                    }

                    // load bitmap from file
                    Bitmap bmp = (Bitmap)Image.FromFile(path + "/" + file);

                    byte[] pixelData = BgfBitmap.BitmapToPixelData(bmp);
                    BgfBitmap bgfBitmap = new BgfBitmap(
                        (uint)i + 1,
                        version,
                        width,
                        height,
                        xoffset,
                        yoffset,
                        hotspots,
                        false,
                        0,
                        pixelData);

                    // cleanp temporary bitmap
                    bmp.Dispose();

                    Frames.Add(bgfBitmap);
                }

                // framesets
                reader.ReadToFollowing("framesets");
                int FrameSetCount = Convert.ToInt32(reader["count"]);
                FrameSets.Capacity = FrameSetCount;
                for (int i = 0; i < FrameSetCount; i++)
                {
                    reader.ReadToFollowing("frameset");
                    string[] indices = reader["indices"].Split(' ');
                    List<int> intIndices = new List<int>();
                    foreach (string index in indices)
                        intIndices.Add(Convert.ToInt32(index));

                    BgfFrameSet bgfFrameSet = new BgfFrameSet((uint)i + 1, intIndices);

                    FrameSets.Add(bgfFrameSet);
                }
            }
            else
                throw new Exception("Wrong format version: " + version + " (expected " + BgfFile.VERSION9 + ").");
        }
Exemple #3
0
        /// <summary>
        /// Loads this BgfFile instance from XML/BMP
        /// </summary>
        /// <param name="Filename"></param>
        public void LoadXml(string Filename)
        {
            string path = Path.GetDirectoryName(Filename);

            // save raw filename without path or extensions
            Filename = Path.GetFileNameWithoutExtension(Filename);

            // init XML reader
            XmlReader reader = XmlReader.Create(Filename);

            // rootnode
            reader.ReadToFollowing("bgf");
            uint version = Convert.ToUInt32(reader["version"]);

            if (version >= BgfFile.VERSION9)
            {
                ShrinkFactor = Convert.ToUInt32(reader["shrink"]);

                // skip maxindices, we use dynamic getter
                // MaxIndices = Convert.ToUInt32(reader["maxindices"]);

                // frames
                reader.ReadToFollowing("frames");
                int FrameCount = Convert.ToInt32(reader["count"]);
                Frames.Capacity = FrameCount;
                for (int i = 0; i < FrameCount; i++)
                {
                    reader.ReadToFollowing("frame");
                    uint   width   = Convert.ToUInt32(reader["width"]);
                    uint   height  = Convert.ToUInt32(reader["height"]);
                    int    xoffset = Convert.ToInt32(reader["xoffset"]);
                    int    yoffset = Convert.ToInt32(reader["yoffset"]);
                    string file    = reader["file"];

                    // hotspots
                    reader.ReadToFollowing("hotspots");
                    byte hotspotcount = Convert.ToByte(reader["count"]);
                    List <BgfBitmapHotspot> hotspots = new List <BgfBitmapHotspot>(hotspotcount);
                    for (int j = 0; j < hotspotcount; j++)
                    {
                        reader.ReadToFollowing("hotspot");
                        sbyte index = Convert.ToSByte(reader["index"]);
                        int   x     = Convert.ToInt32(reader["x"]);
                        int   y     = Convert.ToInt32(reader["y"]);

                        BgfBitmapHotspot hotspot = new BgfBitmapHotspot(index, x, y);
                        hotspots.Add(hotspot);
                    }

                    // load bitmap from file
                    Bitmap bmp = (Bitmap)Image.FromFile(path + "/" + file);

                    byte[]    pixelData = BgfBitmap.BitmapToPixelData(bmp);
                    BgfBitmap bgfBitmap = new BgfBitmap(
                        (uint)i + 1,
                        version,
                        width,
                        height,
                        xoffset,
                        yoffset,
                        hotspots,
                        false,
                        0,
                        pixelData);

                    // cleanp temporary bitmap
                    bmp.Dispose();

                    Frames.Add(bgfBitmap);
                }

                // framesets
                reader.ReadToFollowing("framesets");
                int FrameSetCount = Convert.ToInt32(reader["count"]);
                FrameSets.Capacity = FrameSetCount;
                for (int i = 0; i < FrameSetCount; i++)
                {
                    reader.ReadToFollowing("frameset");
                    string[]   indices    = reader["indices"].Split(' ');
                    List <int> intIndices = new List <int>();
                    foreach (string index in indices)
                    {
                        intIndices.Add(Convert.ToInt32(index));
                    }

                    BgfFrameSet bgfFrameSet = new BgfFrameSet((uint)i + 1, intIndices);

                    FrameSets.Add(bgfFrameSet);
                }
            }
            else
            {
                throw new Exception("Wrong format version: " + version + " (expected " + BgfFile.VERSION9 + ").");
            }
        }
        /// <summary>
        /// Updates the ViewerParent and ViewerHotspot property.
        /// </summary>
        /// <param name="Root">The object this suboverlay belongs to</param>
        /// <param name="SubOverlays">The current suboverlays of the object</param>
        public void UpdateHotspots(ObjectBase Root, IList<SubOverlay> SubOverlays)
        {
            FrontParent = null;
            FrontHotspot = null;
            ViewerParent = null;
            ViewerHotspot = null;

            // try find hotspot on active mainoverlay frame
            if (Root.ViewerFrame != null)           
                ViewerHotspot = Root.ViewerFrame.FindHotspot(hotSpot);
           
            // if not found
            if (ViewerHotspot == null)
            {
                // try find on suboverlays
                foreach (SubOverlay subOv in SubOverlays)
                {
                    if (subOv.ViewerFrame != null)
                    {
                        ViewerHotspot = subOv.ViewerFrame.FindHotspot(hotSpot);
                        if (ViewerHotspot != null)
                        {
                            ViewerParent = subOv;
                            break;
                        }
                    }
                }
            }
        }
        /// <summary>
        /// Import a storyboard given a full path to the storyboard bmp. The XML file for
        /// the storyboard must have a matching filename with the XML extension, and be in
        /// the same directory.
        /// </summary>
        /// <param name="Filename"></param>
        /// <returns></returns>
        public bool ImportStoryboard(string Filename)
        {
            string path = Path.GetDirectoryName(Filename);
            // raw filename without path or extensions
            string xmlFilename = Path.GetFileNameWithoutExtension(Filename);

            // index for reading bmp data, incremented as we split the giant bmp
            uint readoffset = 0;

            // open bmp into one giant bitmap
            Bitmap bmp         = (Bitmap)Image.FromFile(Filename);
            uint   totalWidth  = (uint)bmp.Width;
            uint   totalHeight = (uint)bmp.Height;

            byte[] pixelData = BgfBitmap.BitmapToPixelData(bmp);

            // cleanp temporary bitmap
            bmp.Dispose();

            // init XML reader
            XmlReader reader = XmlReader.Create(Path.Combine(path, xmlFilename) + FileExtensions.XML);

            // read in xml file
            // rootnode
            reader.ReadToFollowing("bgf");
            uint version = Convert.ToUInt32(reader["version"]);

            if (version >= BgfFile.VERSION9)
            {
                ShrinkFactor = Convert.ToUInt32(reader["shrink"]);

                // skip maxindices, we use dynamic getter
                // MaxIndices = Convert.ToUInt32(reader["maxindices"]);

                // frames
                reader.ReadToFollowing("frames");
                int  FrameCount       = Convert.ToInt32(reader["count"]);
                uint savedTotalHeight = Convert.ToUInt32(reader["totalheight"]);
                uint savedTotalWidth  = Convert.ToUInt32(reader["totalwidth"]);
                // Saved dimensions must match those loaded for the storyboard bmp.
                if (savedTotalWidth != totalWidth || savedTotalHeight != totalHeight)
                {
                    return(false);
                }

                Frames.Capacity = FrameCount;
                for (int i = 0; i < FrameCount; i++)
                {
                    reader.ReadToFollowing("frame");
                    uint width   = Convert.ToUInt32(reader["width"]);
                    uint height  = Convert.ToUInt32(reader["height"]);
                    int  xoffset = Convert.ToInt32(reader["xoffset"]);
                    int  yoffset = Convert.ToInt32(reader["yoffset"]);

                    // hotspots
                    reader.ReadToFollowing("hotspots");
                    byte hotspotcount = Convert.ToByte(reader["count"]);
                    List <BgfBitmapHotspot> hotspots = new List <BgfBitmapHotspot>(hotspotcount);
                    for (int j = 0; j < hotspotcount; j++)
                    {
                        reader.ReadToFollowing("hotspot");
                        sbyte index = Convert.ToSByte(reader["index"]);
                        int   x     = Convert.ToInt32(reader["x"]);
                        int   y     = Convert.ToInt32(reader["y"]);

                        BgfBitmapHotspot hotspot = new BgfBitmapHotspot(index, x, y);
                        hotspots.Add(hotspot);
                    }

                    // get pixels for this frame from the single bmp
                    byte[] pixels      = new byte[height * width];
                    uint   writeoffset = 0;
                    for (int j = 0; j < height; ++j)
                    {
                        Wrapper.CopyMem(pixelData, (int)readoffset, pixels, (int)writeoffset, width);
                        readoffset  += totalWidth;
                        writeoffset += width;
                    }

                    BgfBitmap bgfBitmap = new BgfBitmap(
                        (uint)i + 1,
                        version,
                        width,
                        height,
                        xoffset,
                        yoffset,
                        hotspots,
                        false,
                        0,
                        pixels);

                    Frames.Add(bgfBitmap);
                }

                // framesets
                reader.ReadToFollowing("framesets");
                int FrameSetCount = Convert.ToInt32(reader["count"]);
                FrameSets.Capacity = FrameSetCount;
                for (int i = 0; i < FrameSetCount; i++)
                {
                    reader.ReadToFollowing("frameset");
                    string[]   indices    = reader["indices"].Split(' ');
                    List <int> intIndices = new List <int>();
                    foreach (string index in indices)
                    {
                        intIndices.Add(Convert.ToInt32(index));
                    }

                    BgfFrameSet bgfFrameSet = new BgfFrameSet((uint)i + 1, intIndices);

                    FrameSets.Add(bgfFrameSet);
                }
            }
            else
            {
                throw new Exception("Wrong format version: " + version + " (expected " + BgfFile.VERSION9 + ").");
            }

            return(true);
        }
        public int ReadFrom(byte[] Buffer, int StartIndex = 0)
        {
            int cursor = StartIndex;

            Width = BitConverter.ToUInt32(Buffer, cursor);
            cursor += TypeSizes.INT;

            Height = BitConverter.ToUInt32(Buffer, cursor);
            cursor += TypeSizes.INT;

            XOffset = BitConverter.ToInt32(Buffer, cursor);
            cursor += TypeSizes.INT;

            YOffset = BitConverter.ToInt32(Buffer, cursor);
            cursor += TypeSizes.INT;

            byte HotSpotCount = Buffer[cursor];
            cursor++;

            HotSpots.Clear();
            HotSpots.Capacity = HotSpotCount;
            for (int i = 0; i < HotSpotCount; i++)
            {
                BgfBitmapHotspot hotspot = new BgfBitmapHotspot(Buffer, cursor);
                cursor += hotspot.ByteLength;

                HotSpots.Add(hotspot);
            }

            isCompressed = BitConverter.ToBoolean(Buffer, cursor);
            cursor++;

            CompressedLength = BitConverter.ToInt32(Buffer, cursor);
            cursor += TypeSizes.INT;

            if (IsCompressed)
            {
                PixelData = new byte[CompressedLength];
                Array.Copy(Buffer, cursor, PixelData, 0, CompressedLength);
                cursor += CompressedLength;
            }
            else
            {
                PixelData = new byte[UncompressedLength];
                Array.Copy(Buffer, cursor, PixelData, 0, UncompressedLength);
                cursor += UncompressedLength;
            }
            return ByteLength;
        }