/// <summary> /// Computes drawing commands to layout a block of text within /// certain constraints /// </summary> /// <returns></returns> public static TextRendererResult ComputeText(string text, TextRendererOptions options, UIElement target) { var TextStyle = options.TextStyle; var _Scale = options.Scale; var txtScale = TextStyle.Scale * _Scale; var m_LineHeight = TextStyle.MeasureString("W").Y - (2 * txtScale.Y); var spaceWidth = TextStyle.MeasureString(" ").X; var words = text.Split(' ').ToList(); var newWordsArray = TextRenderer.ExtractLineBreaks(words); var m_Lines = new List <UITextEditLine>(); TextRenderer.CalculateLines(m_Lines, newWordsArray, TextStyle, options.MaxWidth, spaceWidth, options.TopLeftIconSpace, m_LineHeight); var topLeft = options.Position; var position = topLeft; var result = new TextRendererResult(); var drawCommands = new List <ITextDrawCmd>(); result.DrawingCommands = drawCommands; var yPosition = topLeft.Y; var numLinesAdded = 0; var realMaxWidth = 0; for (var i = 0; i < m_Lines.Count; i++) { var lineOffset = (i * m_LineHeight < options.TopLeftIconSpace.Y) ? options.TopLeftIconSpace.X : 0; var line = m_Lines[i]; var xPosition = topLeft.X + lineOffset; if (line.LineWidth > realMaxWidth) { realMaxWidth = (int)line.LineWidth; } /** Alignment **/ if (options.Alignment == TextAlignment.Center) { xPosition += (int)Math.Round(((options.MaxWidth - lineOffset) - line.LineWidth) / 2); } var segmentPosition = target.LocalPoint(new Vector2(xPosition, yPosition)); drawCommands.Add(new TextDrawCmd_Text { Selected = false, Text = line.Text, Style = TextStyle, Position = segmentPosition, Scale = txtScale }); numLinesAdded++; yPosition += m_LineHeight; position.Y += m_LineHeight; } result.BoundingBox = new Rectangle((int)topLeft.X, (int)topLeft.Y, (int)options.MaxWidth, (int)(yPosition - m_LineHeight)); result.MaxWidth = realMaxWidth; foreach (var cmd in drawCommands) { cmd.Init(); } return(result); }
private void TextChanged() { BodyText = ((Offscreen && Message != "") ? "[" + Name + "] " : "") + Message; var textW = Math.Max(130, Message.Length/2); BodyTextLabels = TextRenderer.ComputeText(BodyText, new TextRendererOptions { Alignment = TextAlignment.Center, MaxWidth = textW, Position = new Microsoft.Xna.Framework.Vector2(18, 18), Scale = _Scale, TextStyle = BodyTextStyle, WordWrap = true, }, this); BTOffsets = new List<Vector2>(); foreach (var cmd in BodyTextLabels.DrawingCommands) { if (cmd is TextDrawCmd_Text) BTOffsets.Add(((TextDrawCmd_Text)cmd).Position); } DisplayRect.Width = textW + 18 * 2; DisplayRect.Height = BodyTextLabels.BoundingBox.Height + 18 * 2; }
/// <summary> /// Computes drawing commands to layout a block of text within /// certain constraints /// </summary> /// <returns></returns> public static TextRendererResult ComputeText(string text, TextRendererOptions options, UIElement target) { var TextStyle = options.TextStyle; var _Scale = options.Scale; var txtScale = TextStyle.Scale * _Scale; var m_LineHeight = TextStyle.MeasureString("W").Y - (2 * txtScale.Y); var spaceWidth = TextStyle.MeasureString(" ").X; var words = text.Split(' ').ToList(); var newWordsArray = TextRenderer.ExtractLineBreaks(words); var m_Lines = new List<UITextEditLine>(); TextRenderer.CalculateLines(m_Lines, newWordsArray, TextStyle, options.MaxWidth, spaceWidth, options.TopLeftIconSpace, m_LineHeight); var topLeft = options.Position; var position = topLeft; var result = new TextRendererResult(); var drawCommands = new List<ITextDrawCmd>(); result.DrawingCommands = drawCommands; var yPosition = topLeft.Y; var numLinesAdded = 0; var realMaxWidth = 0; for (var i = 0; i < m_Lines.Count; i++) { var lineOffset = (i*m_LineHeight < options.TopLeftIconSpace.Y) ? options.TopLeftIconSpace.X : 0; var line = m_Lines[i]; var xPosition = topLeft.X+lineOffset; if (line.LineWidth > realMaxWidth) realMaxWidth = (int)line.LineWidth; /** Alignment **/ if (options.Alignment == TextAlignment.Center) { xPosition += (int)Math.Round(((options.MaxWidth-lineOffset) - line.LineWidth) / 2); } var segmentPosition = target.LocalPoint(new Vector2(xPosition, yPosition)); drawCommands.Add(new TextDrawCmd_Text { Selected = false, Text = line.Text, Style = TextStyle, Position = segmentPosition, Scale = txtScale }); numLinesAdded++; yPosition += m_LineHeight; position.Y += m_LineHeight; } result.BoundingBox = new Rectangle((int)topLeft.X, (int)topLeft.Y, (int)options.MaxWidth, (int)(yPosition-m_LineHeight)); result.MaxWidth = realMaxWidth; foreach (var cmd in drawCommands) { cmd.Init(); } return result; }
protected override void CalculateMatrix() { base.CalculateMatrix(); if (Type == PrimBoxType.Primitive) { BodyTextLabels = TextRenderer.ComputeText(BodyText, new TextRendererOptions { Alignment = TextAlignment.Center, MaxWidth = 300, Position = new Microsoft.Xna.Framework.Vector2(0, 24), Scale = _Scale, TextStyle = BodyTextStyle, WordWrap = true, }, this); var shift = (Width - 300) / 2; foreach (var cmd in BodyTextLabels.DrawingCommands) { if (cmd is TextDrawCmd_Text) { ((TextDrawCmd_Text)cmd).Position.X += shift; } } } }
public void UpdateDisplay() { Descriptor.Operand = Operand; Style = PGroupStyles.ByType[Descriptor.Group]; TitleText = Descriptor.GetTitle(Master.Scope); var titleWidth = Title.CaptionStyle.MeasureString(TitleText).X; Title.Caption = TitleText; Title.CaptionStyle.Color = Style.Title; BodyText = Descriptor.GetBody(Master.Scope); BodyTextStyle.Color = Style.Body; BodyTextLabels = TextRenderer.ComputeText(BodyText, new TextRendererOptions { Alignment = TextAlignment.Center, MaxWidth = 300, Position = new Microsoft.Xna.Framework.Vector2(0, 24), Scale = _Scale, TextStyle = BodyTextStyle, WordWrap = true, }, this); Width = Math.Max((int)titleWidth, BodyTextLabels.MaxWidth)+10; Height = BodyTextLabels.BoundingBox.Height+20; Title.Size = new Vector2(Width, 24); var shift = (Width - 300) / 2; foreach (var cmd in BodyTextLabels.DrawingCommands) { if (cmd is TextDrawCmd_Text) { ((TextDrawCmd_Text)cmd).Position.X += shift; } } if (Descriptor.Returns == PrimitiveReturnTypes.TrueFalse) { Nodes[0].Visible = true; Nodes[1].Type = NodeType.True; } else { Nodes[0].Visible = false; Nodes[1].Type = NodeType.Done; } HitTest.Region.Width = Width; HitTest.Region.Height = Height; }
private void ComputeText() { m_MessageText = TextRenderer.ComputeText(m_Options.Message, new TextRendererOptions { Alignment = m_Options.Alignment, MaxWidth = m_Options.Width - 64, Position = new Microsoft.Xna.Framework.Vector2(32, 38), Scale = _Scale, TextStyle = m_TextStyle, WordWrap = true, TopLeftIconSpace = IconSpace }, this); m_TextDirty = false; }
/// <summary> /// Computes drawing commands to layout a block of text within /// certain constraints /// </summary> /// <returns></returns> public static TextRendererResult ComputeText(string text, TextRendererOptions options, UIElement target) { var TextStyle = options.TextStyle; var _Scale = options.Scale; var txtScale = TextStyle.Scale * _Scale; var m_LineHeight = TextStyle.MeasureString("W").Y - (2 * txtScale.Y); var spaceWidth = TextStyle.MeasureString(" ").X; text = text.Replace("\r", ""); var bbCommands = new List <BBCodeCommand>(); if (options.BBCode) { var parsed = new BBCodeParser(text); text = parsed.Stripped; bbCommands = parsed.Commands; } var words = text.Split(' ').ToList(); var newWordsArray = TextRenderer.ExtractLineBreaks(words); var m_Lines = new List <UITextEditLine>(); TextRenderer.CalculateLines(m_Lines, newWordsArray, TextStyle, options.MaxWidth, spaceWidth, options.TopLeftIconSpace, m_LineHeight); var topLeft = options.Position; var position = topLeft; var result = new TextRendererResult(); var drawCommands = new List <ITextDrawCmd>(); result.DrawingCommands = drawCommands; var yPosition = topLeft.Y; var numLinesAdded = 0; var realMaxWidth = 0; var bbIndex = 0; var bbColorStack = new Stack <Color>(); var lastColor = TextStyle.Color; var shadowApplied = false; for (var i = 0; i < m_Lines.Count; i++) { var lineOffset = (i * m_LineHeight < options.TopLeftIconSpace.Y) ? options.TopLeftIconSpace.X : 0; var line = m_Lines[i]; var segments = CalculateSegments(line, bbCommands, ref bbIndex); var xPosition = topLeft.X + lineOffset; segments.ForEach(x => x.Size = TextStyle.MeasureString(x.Text)); var thisLineWidth = segments.Sum(x => x.Size.X); if (thisLineWidth > realMaxWidth) { realMaxWidth = (int)thisLineWidth; } /** Alignment **/ if (options.Alignment == TextAlignment.Center) { xPosition += (int)Math.Round(((options.MaxWidth - lineOffset) - thisLineWidth) / 2); } foreach (var segment in segments) { var segmentSize = segment.Size; var segmentPosition = target.LocalPoint(new Vector2(xPosition, yPosition)); if (segment.Text.Length > 0) { drawCommands.Add(new TextDrawCmd_Text { Selected = segment.Selected, Text = segment.Text, Style = TextStyle, Position = segmentPosition, Scale = txtScale }); xPosition += segmentSize.X; } if (segment.StartCommand != null) { var cmd = segment.StartCommand; switch (cmd.Type) { case BBCodeCommandType.color: if (cmd.Close) { //pop a color off our stack if (bbColorStack.Count > 0) { lastColor = bbColorStack.Pop(); drawCommands.Add(new TextDrawCmd_Color(TextStyle, lastColor)); } } else { bbColorStack.Push(lastColor); lastColor = cmd.ParseColor(); drawCommands.Add(new TextDrawCmd_Color(TextStyle, lastColor)); } break; case BBCodeCommandType.s: if (cmd.Close) { drawCommands.Add(new TextDrawCmd_Shadow(TextStyle, false)); shadowApplied = false; } else { drawCommands.Add(new TextDrawCmd_Shadow(TextStyle, true)); shadowApplied = true; } break; case BBCodeCommandType.emoji: if (segment.BBCatchup) { break; } drawCommands.Add(new TextDrawCmd_Emoji(TextStyle, cmd.Parameter, target.LocalPoint(new Vector2(xPosition, yPosition)), _Scale)); break; } } } /* * var segmentPosition = target.LocalPoint(new Vector2(xPosition, yPosition)); * drawCommands.Add(new TextDrawCmd_Text * { * Selected = false, * Text = line.Text, * Style = TextStyle, * Position = segmentPosition, * Scale = txtScale * }); */ numLinesAdded++; yPosition += m_LineHeight; position.Y += m_LineHeight; } if (shadowApplied) { drawCommands.Add(new TextDrawCmd_Shadow(TextStyle, false)); } while (bbColorStack.Count > 0) { lastColor = bbColorStack.Pop(); drawCommands.Add(new TextDrawCmd_Color(TextStyle, lastColor)); } result.BoundingBox = new Rectangle((int)topLeft.X, (int)topLeft.Y, (int)options.MaxWidth, (int)(yPosition - (m_LineHeight + topLeft.Y))); result.MaxWidth = realMaxWidth; foreach (var cmd in drawCommands) { cmd.Init(); } return(result); }