/// <summary> /// Draw the entity. /// </summary> /// <param name="spriteBatch">Sprite batch to draw on.</param> /// <param name="phase">The phase we are currently drawing.</param> override protected void DrawEntity(SpriteBatch spriteBatch, DrawPhase phase) { // update processed text if needed if (_processedText == null) { UpdateDestinationRects(); } // draw background color if (BackgroundColor.A > 0) { // get background color Color backColor = UserInterface.Active.DrawUtils.FixColorOpacity(BackgroundColor); // get destination rect to draw it var rect = BackgroundColorUseBoxSize ? _destRect : _actualDestRect; // fix height for box background and scaling if (BackgroundColorUseBoxSize) { rect.Height = (int)(rect.Height / GlobalScale); } // add padding var padding = new Point( (int)(BackgroundColorPadding.X * GlobalScale), (int)(BackgroundColorPadding.Y * GlobalScale)); rect.Location -= padding; rect.Size += padding + padding; // draw background color spriteBatch.Draw(Resources.WhiteTexture, rect, backColor); } // draw outilnes //int outlineWidth = OutlineWidth; //if (outlineWidth > 0) //{ // DrawTextOutline(spriteBatch, outlineWidth); //} // get fill color Color fillCol = UserInterface.Active.DrawUtils.FixColorOpacity(FillColor); // draw text itself //spriteBatch.DrawString(_currFont, _processedText, _position, fillCol, // 0, _fontOrigin, _actualScale, SpriteEffects.None, 0.5f); CustomUitls.DrawParagraph(spriteBatch, UserInterface.unifont, _processedText, _position - _fontOrigin, fillCol, _destRect.Width, (int)UserInterface.fontSize.Y); // call base draw function base.DrawEntity(spriteBatch, phase); }
/// <summary> /// Calculate the paragraph actual destination rect with word-wrap and other factors taken into consideration. /// </summary> public void CalcTextActualRectWithWrap() { // update font properties UpdateFontPropertiesIfNeeded(); // calc actual scale float actualScale = Scale * BaseSize * GlobalScale; if (actualScale != _actualScale) { _actualScale = actualScale; MarkAsDirty(); } // get text and add things like line-breaks to wrap words etc. string newProcessedText = Text; if (WrapWords) { newProcessedText = WrapText(UserInterface.unifont, newProcessedText, _destRect.Width, _actualScale); } // if processed text changed if (newProcessedText != _processedText) { _processedText = newProcessedText; MarkAsDirty(); } // due to the mechanism of calculating destination rect etc based on parent and anchor, // to set text alignment all we need to do is keep the size the actual text size. // so we just update _size every frame and the text alignemtn (left, right, center..) fix itself by the destination rect. _fontOrigin = Vector2.Zero; _position = new Vector2(_destRect.X, _destRect.Y); Vector2 size = CustomUitls.MeasureParagraph(_processedText); // set position and origin based on anchor. // note: no top-left here because thats the default set above. bool alreadyCentered = false; switch (_anchor) { case Anchor.Center: _fontOrigin = size / 2; _position += new Vector2(_destRect.Width / 2, _destRect.Height / 2); alreadyCentered = true; break; case Anchor.AutoCenter: case Anchor.TopCenter: _fontOrigin = new Vector2(size.X / 2, 0); _position += new Vector2(_destRect.Width / 2, 0f); alreadyCentered = true; break; case Anchor.TopRight: _fontOrigin = new Vector2(size.X, 0); _position += new Vector2(_destRect.Width, 0f); break; case Anchor.BottomCenter: _fontOrigin = new Vector2(size.X / 2, size.Y); _position += new Vector2(_destRect.Width / 2, _destRect.Height); alreadyCentered = true; break; case Anchor.BottomRight: _fontOrigin = new Vector2(size.X, size.Y); _position += new Vector2(_destRect.Width, _destRect.Height); break; case Anchor.BottomLeft: _fontOrigin = new Vector2(0f, size.Y); _position += new Vector2(0f, _destRect.Height); break; case Anchor.CenterLeft: _fontOrigin = new Vector2(0f, size.Y / 2); _position += new Vector2(0f, _destRect.Height / 2); break; case Anchor.CenterRight: _fontOrigin = new Vector2(size.X, size.Y / 2); _position += new Vector2(_destRect.Width, _destRect.Height / 2); break; } // force center align if (AlignToCenter && !alreadyCentered) { _fontOrigin.X = size.X / 2; _position.X = _destRect.X + _destRect.Width / 2; } // set actual height _actualDestRect.X = (int)_position.X - (int)(_fontOrigin.X * _actualScale); _actualDestRect.Y = (int)_position.Y - (int)(_fontOrigin.Y * _actualScale); _actualDestRect.Width = (int)((size.X) * _actualScale); _actualDestRect.Height = (int)((size.Y) * _actualScale); // apply min size if (MinSize != null) { Point minInPixels = CalcActualSizeInPixels(MinSize.Value); _actualDestRect.Width = System.Math.Max(minInPixels.X, _actualDestRect.Width); _actualDestRect.Height = System.Math.Max(minInPixels.Y, _actualDestRect.Height); } // apply max size if (MaxSize != null) { Point maxInPixels = CalcActualSizeInPixels(MaxSize.Value); _actualDestRect.Width = System.Math.Min(maxInPixels.X, _actualDestRect.Width); _actualDestRect.Height = System.Math.Min(maxInPixels.Y, _actualDestRect.Height); } }