Exemplo n.º 1
0
        public TextEntityView(IEntity entity, IFontScaler fontScaler, SpriteBatch spriteBatch, FontBank fontBank)
            : base(entity)
        {
            _fontScaler  = fontScaler ?? throw new ArgumentNullException();
            _spriteBatch = spriteBatch ?? throw new ArgumentNullException();
            _fontBank    = fontBank ?? throw new ArgumentNullException();

            _textEntity = this.GetEntityAs <ITextEntity>();
            _font       = _fontBank.GetFont(_textEntity.FontName, _textEntity.FontSize);

            _cachedFont = _font;
        }
Exemplo n.º 2
0
        public void RefreshCachedTexture(GraphicsDevice device, SpriteBatch spriteBatch)
        {
            FontBank.FontDescription renderFont = _font;

            if (_textEntity.Behaviour == TextEntityBehaviour.AutoScale)
            {
                renderFont = _fontScaler.GetScaledFont(_textEntity.Text, _textEntity.Bounds, renderFont);
            }

            switch (_textEntity.Behaviour)
            {
            case TextEntityBehaviour.ClipToBounds:
                _cachedTexture = new RenderTarget2D(device, (int)_textEntity.Bounds.Width, (int)_textEntity.Bounds.Height);
                break;

            case TextEntityBehaviour.AutoScale:
            case TextEntityBehaviour.Default:
            default:
                Vector2 textSize = renderFont.Font.MeasureString(_textEntity.Text);
                _cachedTexture = new RenderTarget2D(device, (int)textSize.X, (int)textSize.Y);
                break;
            }

            var oldTargets = device.GetRenderTargets();

            device.SetRenderTarget(_cachedTexture);

            spriteBatch.Begin();
            spriteBatch.DrawString(renderFont.Font, _textEntity.Text, Vector2.Zero, Color.White);
            spriteBatch.End();

            device.SetRenderTargets(oldTargets);

            _cachedFont = renderFont;
            _cachedTextureScaledSize = (Point)GetScaledSize(_textEntity.Bounds.Size, new SizeF(_cachedTexture.Width, _cachedTexture.Height));
        }