Esempio n. 1
0
        /// <summary>
        /// Returns a TypeDictionary containing any metadata defined by the frame
        /// or null if the frame does not define metadata.
        /// </summary>
        public TypeDictionary FrameMetadata(string filename)
        {
            if (!metadata.TryGetValue(filename, out var fileMetadata))
            {
                FrameLoader.GetFrames(fileSystem, filename, loaders, out fileMetadata);
                metadata[filename] = fileMetadata;
            }

            return(fileMetadata);
        }
Esempio n. 2
0
        /// <summary>
        /// Returns the first set of sprites with the given filename.
        /// If getUsedFrames is defined then the indices returned by the function call
        /// are guaranteed to be loaded.  The value of other indices in the returned
        /// array are undefined and should never be accessed.
        /// </summary>
        public Sprite[] this[string filename, Func <int, IEnumerable <int> > getUsedFrames = null]
        {
            get
            {
                var allSprites = sprites.GetOrAdd(filename);
                var sprite     = allSprites.FirstOrDefault();

                ISpriteFrame[] unloaded;
                if (!unloadedFrames.TryGetValue(filename, out unloaded))
                {
                    unloaded = null;
                }

                // This is the first time that the file has been requested
                // Load all of the frames into the unused buffer and initialize
                // the loaded cache (initially empty)
                if (sprite == null)
                {
                    TypeDictionary fileMetadata = null;
                    unloaded = FrameLoader.GetFrames(fileSystem, filename, loaders, out fileMetadata);
                    unloadedFrames[filename] = unloaded;
                    metadata[filename]       = fileMetadata;

                    sprite = new Sprite[unloaded.Length];
                    allSprites.Add(sprite);
                }

                // HACK: The sequency code relies on side-effects from getUsedFrames
                var indices = getUsedFrames != null?getUsedFrames(sprite.Length) :
                                  Enumerable.Range(0, sprite.Length);

                // Load any unused frames into the SheetBuilder
                if (unloaded != null)
                {
                    foreach (var i in indices)
                    {
                        if (unloaded[i] != null)
                        {
                            sprite[i]   = SheetBuilder.Add(unloaded[i]);
                            unloaded[i] = null;
                        }
                    }

                    // All frames have been loaded
                    if (unloaded.All(f => f == null))
                    {
                        unloadedFrames.Remove(filename);
                    }
                }

                return(sprite);
            }
        }
Esempio n. 3
0
 public FrameCache(IReadOnlyFileSystem fileSystem, ISpriteLoader[] loaders)
 {
     frames = new Cache <string, ISpriteFrame[]>(filename => FrameLoader.GetFrames(fileSystem, filename, loaders, out _));
 }
Esempio n. 4
0
        public FrameCache(IReadOnlyFileSystem fileSystem, SpriteLoaderBase[] loaders)
        {
            TypeDictionary metadata;

            frames = new Cache <string, ISpriteFrame[]>(filename => FrameLoader.GetFrames(fileSystem, filename, loaders, out metadata));
        }        // ISpriteFrame[] are give a key and value for this Cache
Esempio n. 5
0
        public FrameCache(IReadOnlyFileSystem fileSystem, ISpriteLoader[] loaders)
        {
            TypeDictionary metadata;

            frames = new Cache <string, ISpriteFrame[]>(filename => FrameLoader.GetFrames(fileSystem, filename, loaders, out metadata));
        }
Esempio n. 6
0
        /// <summary>
        /// Returns the first set of sprites with the given filename.
        /// If getUsedFrames is defined then the indices returned by the function call
        /// are guaranteed to be loaded.  The value of other indices in the returned
        /// array are undefined and should never be accessed.
        /// </summary>
        public Sprite[] this[string filename, Func <int, IEnumerable <int> > getThisIndexesSpritesFromFile = null]
        {
            get
            {
                var allSprites = sprites.GetOrAdd(filename);
                var sprite     = allSprites.FirstOrDefault();

                ISpriteFrame[] newFramesFromFile;
                if (!ParsedFramesStorage.TryGetValue(filename, out newFramesFromFile))
                {
                    newFramesFromFile = null;
                }

                // This is the first time that the file has been requested
                // Load all of the frames into the unused buffer and initialize
                // the loaded cache (initially empty)
                if (sprite == null)
                {
                    TypeDictionary fileMetadata = null;
                    using (var stream = fileSystem.Open(filename))
                    {
                        newFramesFromFile = FrameLoader.GetFrames(stream, filename, loaders, out fileMetadata);                         //загрузит все спрайты из shp,wsa,... файлов в память
                    }
                    ParsedFramesStorage[filename] = newFramesFromFile;
                    metadata[filename]            = fileMetadata;

                    sprite = new Sprite[newFramesFromFile.Length];
                    allSprites.Add(sprite);
                }

                // HACK: The sequency code relies on side-effects from getUsedFrames
                var indices = getThisIndexesSpritesFromFile != null?getThisIndexesSpritesFromFile(sprite.Length) :
                                  Enumerable.Range(0, sprite.Length);

                // Load any unused frames into the SheetBuilder
                if (newFramesFromFile != null)
                {
                    foreach (var i in indices)
                    {
                        if (newFramesFromFile[i] != null)
                        {
                            //sprite[i] = SheetBuilder.Add(framesCandidates[i]);

                            if (filename.Contains("png1"))                             //for Loaders with 4bytes per pixel
                            {
                                using (var stream = fileSystem.Open(filename))
                                {
                                    sprite[i] = SheetBuilder2D.Add(new Png(stream));
                                }
                            }
                            else
                            {
                                sprite[i] = SheetBuilder2D.Add(newFramesFromFile[i]);
                            }
                            newFramesFromFile[i] = null;
                        }
                    }

                    // All frames have been loaded
                    if (newFramesFromFile.All(f => f == null))
                    {
                        ParsedFramesStorage.Remove(filename);
                    }
                }

                return(sprite);
            }
        }