Exemple #1
0
        public static void Export(PortraitSheet sheet, string baseDirectory)
        {
            foreach (int emoteIndex in sheet.emoteMap.Keys)
            {
                string emotion = GraphicsManager.Emotions[emoteIndex].Name;

                int ii = sheet.emoteMap[emoteIndex].Position;
                {
                    Texture2D tex = new Texture2D(device, GraphicsManager.PortraitSize, GraphicsManager.PortraitSize);
                    BaseSheet.Blit(sheet.baseTexture, tex, ii % sheet.TotalX * GraphicsManager.PortraitSize, ii / sheet.TotalX * GraphicsManager.PortraitSize, GraphicsManager.PortraitSize, GraphicsManager.PortraitSize, 0, 0);
                    using (Stream stream = new FileStream(baseDirectory + emotion + ".png", FileMode.Create, FileAccess.Write, FileShare.None))
                        ExportTex(stream, tex);
                    tex.Dispose();
                }

                if (sheet.emoteMap[emoteIndex].HasReverse)
                {
                    ii++;
                    Texture2D tex2 = new Texture2D(device, GraphicsManager.PortraitSize, GraphicsManager.PortraitSize);
                    BaseSheet.Blit(sheet.baseTexture, tex2, ii % sheet.TotalX * GraphicsManager.PortraitSize, ii / sheet.TotalX * GraphicsManager.PortraitSize, GraphicsManager.PortraitSize, GraphicsManager.PortraitSize, 0, 0);
                    using (Stream stream = new FileStream(baseDirectory + emotion + "^.png", FileMode.Create, FileAccess.Write, FileShare.None))
                        ExportTex(stream, tex2);
                    tex2.Dispose();
                }
            }
        }
        //frompath (import) has two varieties: they will take
        // - take a folder containing all elements, each with a char number
        // - take a simple png
        //fromstream (load) will take the png and all the rectangles, and the char maps from stream
        //save will save as .font


        public static new FontSheet Import(string path)
        {
            //get all extra stat from an XML
            if (File.Exists(path + "FontData.xml"))
            {
                XmlDocument doc = new XmlDocument();
                doc.Load(path + "FontData.xml");
                int spaceWidth = Convert.ToInt32(doc.SelectSingleNode("FontData/SpaceWidth").InnerText);
                int charHeight = Convert.ToInt32(doc.SelectSingleNode("FontData/CharHeight").InnerText);
                int charSpace  = Convert.ToInt32(doc.SelectSingleNode("FontData/CharSpace").InnerText);
                int lineSpace  = Convert.ToInt32(doc.SelectSingleNode("FontData/LineSpace").InnerText);

                HashSet <int> colorlessGlyphs = new HashSet <int>();
                XmlNode       glyphNodes      = doc.SelectSingleNode("FontData/Colorless");
                foreach (XmlNode glyphNode in glyphNodes.SelectNodes("Glyph"))
                {
                    int glyphIdx = Convert.ToInt32(glyphNode.InnerText, 16);
                    colorlessGlyphs.Add(glyphIdx);
                }

                string[]         pngs   = Directory.GetFiles(path, "*.png", SearchOption.TopDirectoryOnly);
                List <ImageInfo> sheets = new List <ImageInfo>();
                foreach (string dir in pngs)
                {
                    int       png      = Convert.ToInt32(Path.GetFileNameWithoutExtension(dir), 16);
                    Texture2D newSheet = null;
                    using (FileStream fileStream = new FileStream(dir, FileMode.Open, FileAccess.Read, FileShare.Read))
                        newSheet = ImportTex(fileStream);

                    sheets.Add(new ImageInfo(png, newSheet));
                }
                if (sheets.Count == 0)
                {
                    return(null);
                }

                Canvas canvas = new Canvas();
                canvas.SetCanvasDimensions(Canvas.INFINITE_SIZE, Canvas.INFINITE_SIZE);
                OptimalMapper mapper = new OptimalMapper(canvas, 0.975f, 10);
                Atlas         atlas  = mapper.Mapping(sheets);

                Rectangle[] rects = new Rectangle[sheets.Count];
                Dictionary <int, GlyphData> chars = new Dictionary <int, GlyphData>();
                Texture2D tex = new Texture2D(device, atlas.Width, atlas.Height);
                for (int ii = 0; ii < atlas.MappedImages.Count; ii++)
                {
                    MappedImageInfo info = atlas.MappedImages[ii];
                    BaseSheet.Blit(info.ImageInfo.Texture, tex, 0, 0, info.ImageInfo.Width, info.ImageInfo.Height, info.X, info.Y);
                    rects[ii] = new Rectangle(info.X, info.Y, info.ImageInfo.Width, info.ImageInfo.Height);
                    chars[info.ImageInfo.ID] = new GlyphData(ii, colorlessGlyphs.Contains(info.ImageInfo.ID));
                    info.ImageInfo.Texture.Dispose();
                }

                return(new FontSheet(tex, rects, spaceWidth, charHeight, charSpace, lineSpace, chars));
            }
            else
            {
                throw new Exception("Error finding XML file in " + path + ".");
            }
        }
Exemple #3
0
        //this can inherit tilesheet
        //frompath (import) will take a folder containing all elements
        //fromstream (load) will take the png, the tile height/width, and the emotion maps
        //save will save as .portrait


        public static new PortraitSheet Import(string baseDirectory)
        {
            //load all available tilesets
            //get all frames
            Dictionary <int, PortraitData> animData = new Dictionary <int, PortraitData>();
            List <Texture2D> sheets = new List <Texture2D>();

            for (int ii = 0; ii < GraphicsManager.Emotions.Count; ii++)
            {
                string emotion = GraphicsManager.Emotions[ii].Name;
                if (File.Exists(baseDirectory + emotion + ".png"))
                {
                    bool hasReverse = File.Exists(baseDirectory + emotion + "^.png");
                    {
                        Texture2D newSheet = null;
                        using (FileStream fileStream = new FileStream(baseDirectory + emotion + ".png", FileMode.Open, FileAccess.Read, FileShare.Read))
                            newSheet = ImportTex(fileStream);

                        animData.Add(ii, new PortraitData(sheets.Count, hasReverse));
                        if (newSheet.Width != GraphicsManager.PortraitSize || newSheet.Height != GraphicsManager.PortraitSize)
                        {
                            throw new InvalidOperationException(baseDirectory + emotion + ".png has incorrect dimensions for portrait.");
                        }
                        sheets.Add(newSheet);
                    }

                    if (hasReverse)
                    {
                        Texture2D newSheet = null;
                        using (FileStream fileStream = new FileStream(baseDirectory + emotion + "^.png", FileMode.Open, FileAccess.Read, FileShare.Read))
                            newSheet = ImportTex(fileStream);

                        if (newSheet.Width != GraphicsManager.PortraitSize || newSheet.Height != GraphicsManager.PortraitSize)
                        {
                            throw new InvalidOperationException(baseDirectory + emotion + ".png has incorrect dimensions for portrait.");
                        }
                        sheets.Add(newSheet);
                    }
                }
            }
            if (sheets.Count == 0)
            {
                return(null);
            }

            int fullWidth = (int)Math.Ceiling(Math.Sqrt(sheets.Count));

            Texture2D tex = new Texture2D(device, GraphicsManager.PortraitSize * fullWidth, GraphicsManager.PortraitSize * fullWidth);

            for (int ii = 0; ii < sheets.Count; ii++)
            {
                BaseSheet.Blit(sheets[ii], tex, 0, 0, sheets[ii].Width, sheets[ii].Height, ii % fullWidth * GraphicsManager.PortraitSize, ii / fullWidth * GraphicsManager.PortraitSize);
                sheets[ii].Dispose();
            }
            return(new PortraitSheet(tex, animData));
        }
Exemple #4
0
        //frompath (import) will take a folder containing all elements
        //fromstream (load) will take the png, and the rectangles (head.body/tail are precalculated)
        //save will save as .beam

        public static new BeamSheet Import(string path)
        {
            if (File.Exists(path + "BeamData.xml"))
            {
                XmlDocument doc = new XmlDocument();
                doc.Load(path + "BeamData.xml");
                int totalFrames = Convert.ToInt32(doc.SelectSingleNode("BeamData/TotalFrames").InnerText);

                List <Texture2D> sheets = new List <Texture2D>();
                Rectangle[]      rects  = new Rectangle[3 * totalFrames];
                int maxWidth            = 0;
                int maxHeight           = 0;

                for (int ii = 0; ii < 3; ii++)
                {
                    using (FileStream fileStream = new FileStream(path + ((BeamFrame)ii).ToString() + ".png", FileMode.Open, FileAccess.Read, FileShare.Read))
                    {
                        Texture2D newSheet = Texture2D.FromStream(device, fileStream);

                        for (int jj = 0; jj < totalFrames; jj++)
                        {
                            rects[ii * totalFrames + jj] = new Rectangle(newSheet.Width / totalFrames * jj, maxHeight, newSheet.Width / totalFrames, newSheet.Height);
                        }

                        maxWidth   = Math.Max(maxWidth, newSheet.Width);
                        maxHeight += newSheet.Height;
                        sheets.Add(newSheet);
                    }
                }

                Texture2D tex = new Texture2D(device, maxWidth, maxHeight);

                int curHeight = 0;
                for (int ii = 0; ii < sheets.Count; ii++)
                {
                    BaseSheet.Blit(sheets[ii], tex, 0, 0, sheets[ii].Width, sheets[ii].Height, 0, curHeight);
                    curHeight += sheets[ii].Height;
                    sheets[ii].Dispose();
                }

                return(new BeamSheet(tex, rects, totalFrames));
            }
            else
            {
                throw new Exception("Error finding XML file in " + path + ".");
            }
        }
        //frompath (import) will take a folder containing all elements
        //fromstream (load) will take the png and all the rectangles from stream
        //save will save as .atlas

        public static new SpriteSheet Import(string path)
        {
            string[]         pngs   = Directory.GetFiles(path, "*.png", SearchOption.TopDirectoryOnly);
            List <ImageInfo> sheets = new List <ImageInfo>();
            int index = 0;

            foreach (string dir in pngs)
            {
                Texture2D newSheet = null;
                using (FileStream fileStream = new FileStream(dir, FileMode.Open, FileAccess.Read, FileShare.Read))
                    newSheet = ImportTex(fileStream);

                sheets.Add(new ImageInfo(index, newSheet));
                index++;
            }
            if (sheets.Count == 0)
            {
                return(null);
            }

            Canvas canvas = new Canvas();

            canvas.SetCanvasDimensions(Canvas.INFINITE_SIZE, Canvas.INFINITE_SIZE);
            OptimalMapper mapper = new OptimalMapper(canvas);
            Atlas         atlas  = mapper.Mapping(sheets);

            Rectangle[] rects = new Rectangle[sheets.Count];
            Texture2D   tex   = new Texture2D(device, atlas.Width, atlas.Height);

            for (int ii = 0; ii < atlas.MappedImages.Count; ii++)
            {
                MappedImageInfo info = atlas.MappedImages[ii];
                BaseSheet.Blit(info.ImageInfo.Texture, tex, 0, 0, info.ImageInfo.Width, info.ImageInfo.Height, info.X, info.Y);
                rects[info.ImageInfo.ID] = new Rectangle(info.X, info.Y, info.ImageInfo.Width, info.ImageInfo.Height);
                info.ImageInfo.Texture.Dispose();
            }

            return(new SpriteSheet(tex, rects));
        }
 public static void Blit(BaseSheet source, Texture2D dest, int srcPx, int srcPy, int srcW, int srcH, int destX, int destY)
 {
     BaseSheet.Blit(source.baseTexture, dest, srcPx, srcPy, srcW, srcH, destX, destY);
 }