summaryrefslogtreecommitdiff
path: root/Plugins/MonoGame.Extended/source/MonoGame.Extended.Gui/Serialization/GuiTextureAtlasJsonConverter.cs
blob: a0617cf8711f4d2c4ccc48e87af719d087fb453d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
using System;
using System.Text.Json;
using Microsoft.Xna.Framework.Content;
using MonoGame.Extended.Serialization;
using MonoGame.Extended.TextureAtlases;

namespace MonoGame.Extended.Gui.Serialization
{
    public class GuiTextureAtlasJsonConverter : ContentManagerJsonConverter<TextureAtlas>
    {
        private readonly IGuiTextureRegionService _textureRegionService;

        public GuiTextureAtlasJsonConverter(ContentManager contentManager, IGuiTextureRegionService textureRegionService)
            : base(contentManager, atlas => atlas.Name)
        {
            _textureRegionService = textureRegionService;
        }

        /// <inheritdoc />
        public override TextureAtlas Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
        {
            var textureAtlas = base.Read(ref reader, typeToConvert, options);
            if (textureAtlas is not null)
            {
                _textureRegionService.TextureAtlases.Add(textureAtlas);
            }

            return textureAtlas;
        }
    }
}