Beispiel #1
0
            protected override Task <ResultStatus> DoCommandOverride(ICommandContext commandContext)
            {
                // try to import the font from the original bitmap or ttf file
                Graphics.SpriteFont scalableFont;
                try
                {
                    scalableFont = SignedDistanceFieldFontCompiler.Compile(FontDataFactory, Parameters);
                }
                catch (FontNotFoundException ex)
                {
                    commandContext.Logger.Error($"Font [{ex.FontName}] was not found on this machine.", ex);
                    return(Task.FromResult(ResultStatus.Failed));
                }

                // check that the font data is valid
                if (scalableFont == null || scalableFont.Textures.Count == 0)
                {
                    return(Task.FromResult(ResultStatus.Failed));
                }

                // save the data into the database
                var assetManager = new ContentManager(MicrothreadLocalDatabases.ProviderService);

                assetManager.Save(Url, scalableFont);

                // dispose textures allocated by the StaticFontCompiler
                foreach (var texture in scalableFont.Textures)
                {
                    texture.Dispose();
                }

                return(Task.FromResult(ResultStatus.Successful));
            }
Beispiel #2
0
        /// <summary>
        /// Initializes a new instance of the <see cref="UIEditorController"/> class.
        /// </summary>
        /// <param name="asset">The asset associated with this instance.</param>
        /// <param name="editor">The editor associated with this instance.</param>
        protected UIEditorController([NotNull] AssetViewModel asset, [NotNull] UIEditorBaseViewModel editor)
            : base(asset, editor, CreateEditorGame)
        {
            // move this in a service shared by all editor controllers
            defaultFontLazy = new Lazy <Graphics.SpriteFont>(() =>
            {
                var fontItem           = SignedDistanceFieldSpriteFontFactory.Create();
                fontItem.FontType.Size = 16.0f;
                return(SignedDistanceFieldFontCompiler.Compile(Game.Services.GetService <IFontFactory>(), fontItem));
            });

            var resolutionNode = Editor.NodeContainer.GetNode(((UIAssetBase)Asset.Asset).Design)[nameof(UIAssetBase.UIDesign.Resolution)];

            resolutionNode.ValueChanged += ResolutionChanged;
            resolution = (Vector3)resolutionNode.Retrieve();
        }
Beispiel #3
0
        /// <summary>
        /// Generate a precompiled sprite font from the current sprite font asset.
        /// </summary>
        /// <param name="asset">The sprite font asset</param>
        /// <param name="sourceAsset">The source sprite font asset item</param>
        /// <param name="texturePath">The path of the source texture</param>
        /// <param name="srgb">Indicate if the generated texture should be srgb</param>
        /// <returns>The precompiled sprite font asset</returns>
        public static PrecompiledSpriteFontAsset GeneratePrecompiledSDFSpriteFont(this SpriteFontAsset asset, AssetItem sourceAsset, string texturePath)
        {
            // TODO create PrecompiledSDFSpriteFontAsset
            var scalableFont = (SignedDistanceFieldSpriteFont)SignedDistanceFieldFontCompiler.Compile(FontDataFactory, asset);

            var referenceToSourceFont = new AssetReference(sourceAsset.Id, sourceAsset.Location);
            var glyphs   = new List <Glyph>(scalableFont.CharacterToGlyph.Values);
            var textures = scalableFont.Textures;

            var imageType       = ImageFileType.Png;
            var textureFileName = new UFile(texturePath).GetFullPathWithoutExtension() + imageType.ToFileExtension();

            if (textures != null && textures.Count > 0)
            {
                // save the texture   TODO support for multi-texture
                using (var stream = File.OpenWrite(textureFileName))
                    scalableFont.Textures[0].GetSerializationData().Image.Save(stream, imageType);
            }

            var precompiledAsset = new PrecompiledSpriteFontAsset
            {
                Glyphs             = glyphs,
                Size               = asset.FontType.Size,
                Style              = asset.FontSource.Style,
                OriginalFont       = referenceToSourceFont,
                FontDataFile       = textureFileName,
                BaseOffset         = scalableFont.BaseOffsetY,
                DefaultLineSpacing = scalableFont.DefaultLineSpacing,
                ExtraSpacing       = scalableFont.ExtraSpacing,
                ExtraLineSpacing   = scalableFont.ExtraLineSpacing,
                DefaultCharacter   = asset.DefaultCharacter,
                FontName           = asset.FontSource.GetFontName(),
                IsPremultiplied    = asset.FontType.IsPremultiplied,
                IsSrgb             = false,
            };

            return(precompiledAsset);
        }