public void DrawMarkedMultiLineString(Font font, TextEditor textEditor, ref Rect rect,ref Vector2 offset, Color color, Color selectionColor) { int realIndex; int line = FindLineIndex(textEditor.ToString(), textEditor.MarkerIndex, out realIndex); float markerX = font.MessureSubstring(textEditor.ToString(), realIndex, textEditor.MarkerIndex - realIndex).X; int indexOfFirst = font.BestHeightFit(textEditor.ToString(), offset.Y); int indexOfLast = font.BestHeightFit(textEditor.ToString(), offset.Y + rect.Bottom + font.Size); this.batch.BufferSubString(font, textEditor.ToString(), indexOfFirst, indexOfLast - indexOfFirst, new Vector2(rect.X, rect.Y), color, new Vector2(offset.X, (offset.Y > 0) ? font.Size + 8: 0), Vector2.One); this.batch.BufferFrame(this.pixel, new Rect(rect.X + markerX - offset.X, rect.Y - offset.Y + line * font.LineHeight, 1, font.LineHeight), color); if (textEditor.Selected) { DrawSelection(font, textEditor, ref rect, ref offset, ref selectionColor, markerX); } }
public void DrawMarkedString(Font textureFont, TextEditor text, ref Rect rect, Color color, Color selectionColor, TextAlignment textAlignment) { int length = textureFont.BestWidthFitBackWards(text.ToString(), text.MarkerIndex, rect.W); float markerPos; if (length < text.Length) markerPos = rect.W - 1; else { markerPos = textureFont.MessureSubstring(text.ToString(), Math.Max(0,(text.MarkerIndex - length)), text.MarkerIndex - (text.Length - length)).X; markerPos = MathHelper.Clamp(0, rect.W - 1, markerPos); } Vector2 align = GetAlignment(textureFont, text.ToString(), text.MarkerIndex - length, length, ref rect, textAlignment); if (text.Selected) { float selectionStart = textureFont.MessureSubstring(text.ToString(), 0, text.SelectionIndex - (text.Length - length)).X; this.batch.BufferFrame(this.pixel, new Rect(rect.X + selectionStart, rect.Y, markerPos - selectionStart, rect.H), selectionColor * 0.6f); } this.batch.BufferFrame(this.pixel, new Rect(rect.X + markerPos, rect.Y, 1, rect.H), color); this.batch.BufferSubString(textureFont, text.ToString(), Math.Max(0, text.MarkerIndex - length), length, new Vector2(rect.X, rect.Y), color, align, Vector2.One); }
private void DrawSelection(Font font, TextEditor textEditor, ref Rect rect, ref Vector2 offset, ref Color selectionColor, float markerX) { int min = Math.Min(textEditor.SelectionIndex, textEditor.MarkerIndex); int max = Math.Max(textEditor.SelectionIndex, textEditor.MarkerIndex); string txt = textEditor.ToString(); int lastLine = min; int real; int line = FindLineIndex(textEditor.ToString(), min, out real); bool first = true; float selectionLineWidth; for (int i = min; i < max; i++) { if (txt[i] == '\n') { selectionLineWidth = font.MessureSubstring(textEditor.ToString(), lastLine, i - lastLine).X; if (first) //Anoying special case. First line found. { float selectionX = font.MessureSubstring(textEditor.ToString(), real, min - real).X; this.batch.BufferFrame(this.pixel, new Rect(rect.X - offset.X + selectionX, rect.Y - offset.Y + line * font.LineHeight, selectionLineWidth, font.LineHeight), selectionColor * 0.6f); first = false; } else this.batch.BufferFrame(this.pixel, new Rect(rect.X - offset.X, rect.Y - offset.Y + line * font.LineHeight, selectionLineWidth, font.LineHeight), selectionColor * 0.6f); lastLine = i; line++; } } selectionLineWidth = font.MessureSubstring(textEditor.ToString(), lastLine, max - lastLine).X; if (textEditor.MarkerIndex < textEditor.SelectionIndex) { FindLineIndex(textEditor.ToString(), max, out real); float mi2 = font.MessureSubstring(textEditor.ToString(), real, min - real).X; this.batch.BufferFrame(this.pixel, new Rect(rect.X + mi2 - offset.X, rect.Y - offset.Y + line * font.LineHeight, selectionLineWidth, font.LineHeight), selectionColor * 0.6f); } else { this.batch.BufferFrame(this.pixel, new Rect(rect.X + markerX - selectionLineWidth - offset.X, rect.Y - offset.Y + line * font.LineHeight, selectionLineWidth, font.LineHeight), selectionColor * 0.6f); } }