Esempio n. 1
0
        /// <summary>
        /// Loads a SpriteAtlas created with the Sprite Atlas Packer tool
        /// </summary>
        public SpriteAtlas LoadSpriteAtlas(string name, bool premultiplyAlpha = false)
        {
            if (LoadedAssets.TryGetValue(name, out var asset))
            {
                if (asset is SpriteAtlas spriteAtlas)
                {
                    return(spriteAtlas);
                }
            }

            var atlasData = SpriteAtlasLoader.ParseSpriteAtlasData($"Content/{name}.atlas");
            var atlas     = atlasData.AsSpriteAtlas(ReadAsset <Texture2D>(name, null));

            LoadedAssets.Add(name, atlas);
            DisposableAssets.Add(atlas);

            return(atlas);
        }
        void LoadTextureAndAtlasFiles()
        {
            if (_texturePtr != IntPtr.Zero)
            {
                Core.GetGlobalManager <ImGuiManager>().UnbindTexture(_texturePtr);
            }

            _spriteAtlasData.Clear();
            _nonEditableAnimations.Clear();
            _atlasAllowsAnimationEditing = true;
            _hasSlicedContent            = false;

            var _atlasTexture = Texture2D.FromStream(Core.GraphicsDevice, File.OpenRead(_sourceImageFile));

            _textureSize            = new Num.Vector2(_atlasTexture.Width, _atlasTexture.Height);
            _textureAspectRatio     = _textureSize.X / _textureSize.Y;
            _texturePtr             = Core.GetGlobalManager <ImGuiManager>().BindTexture(_atlasTexture);
            _textureLoadedThisFrame = true;

            if (File.Exists(_sourceAtlasFile))
            {
                _hasSlicedContent = true;
                _spriteAtlasData  = SpriteAtlasLoader.ParseSpriteAtlasData(_sourceAtlasFile, true);

                // ensure animations are contiguous, since that is all we support.
                // First check that all frames are in order in the animations
                for (var j = 0; j < _spriteAtlasData.AnimationFrames.Count; j++)
                {
                    var animation = _spriteAtlasData.AnimationFrames[j];
                    for (var i = 0; i < animation.Count; i++)
                    {
                        if (i == 0)
                        {
                            continue;
                        }

                        if (animation[i] != animation[i - 1] + 1)
                        {
                            _atlasAllowsAnimationEditing = false;
                            _nonEditableAnimations.Add(j);
                        }
                    }
                }

                // Next check that all frames are in order, ghetto style. We check that all the y-values of the rects
                // always increase or stay the same. Not perfect by any means, but we dont know if this is padded in some
                // odd way or contains sprites of odd sizes so this is a quick and dirty solution.
                var lastRectY = -1;
                for (var i = 0; i < _spriteAtlasData.SourceRects.Count; i++)
                {
                    if (i == 0 || lastRectY <= _spriteAtlasData.SourceRects[i].Y)
                    {
                        lastRectY = _spriteAtlasData.SourceRects[i].Y;
                        continue;
                    }

                    _atlasAllowsAnimationEditing = false;
                    return;
                }
            }
        }