using System; using System.Collections.Generic; using System.Linq; using Unity.CodeEditor.Rendering; using Unity.CodeEditor.Text; using UnityEngine; namespace Unity.CodeEditor.Text { /// /// Represents a line of text that is used for text rendering. /// internal class TextLine : IDisposable { private readonly TextRun[] _textRuns; private int _initialTextLineOffset; internal TextLine( TextRun[] textRuns, int length, GUIContent markupGUIContent, GUIContent plainGuiContent, float height, Rect lineRect, TextParagraphProperties paragraphProperties, int initialTextLineOffset) { _textRuns = textRuns; _initialTextLineOffset = initialTextLineOffset; Length = length; MarkupGUIContent = markupGUIContent; PlainGUIContent = plainGuiContent; TextParagraphProperties = paragraphProperties; Height = Mathf.Round(height + paragraphProperties.Leading); LineRect = lineRect; } /// /// Gets the text runs that are contained within a line. /// /// /// The contained text runs. /// internal IReadOnlyList TextRuns => _textRuns; /// /// Gets the total number of TextSource positions of the current line. /// internal int Length { get; } internal TextParagraphProperties TextParagraphProperties { get; } internal GUIContent MarkupGUIContent { get; } internal GUIContent PlainGUIContent { get; } internal string MarkupText => MarkupGUIContent?.text ?? string.Empty; /// /// Gets the height of a line of text. /// /// /// The text line height. /// internal float Height { get; } internal Rect LineRect { get; } /// /// Gets the width of a line of text, excluding trailing whitespace characters. /// /// /// The text line width, excluding trailing whitespace characters. /// internal float Width => LineRect.width; /// /// Gets the width of a line of text, including trailing whitespace characters. /// /// /// The text line width, including trailing whitespace characters. /// internal float WidthIncludingTrailingWhitespace => LineRect.width; internal int TrailingWhitespaceLength { get; } /// /// Gets the character hit corresponding to the specified distance from the beginning of the line. /// /// A value that represents the distance from the beginning of the line. /// The object at the specified distance from the beginning of the line. internal CharacterHit GetCharacterHitFromDistance(float distance) { int index = TextParagraphProperties.GUIStyle.GetCursorStringIndex(LineRect, PlainGUIContent, new Vector2(distance, 0)); return new CharacterHit(index + _initialTextLineOffset, 0); } /// /// Gets the distance from the beginning of the line to the specified character hit. /// . /// /// The object whose distance you want to query. /// A that represents the distance from the beginning of the line. internal float GetDistanceFromCharacterHit(CharacterHit characterHit) { return TextParagraphProperties.GUIStyle.GetCursorPixelPosition(LineRect, PlainGUIContent, characterHit.FirstCharacterIndex - _initialTextLineOffset).x; } /// /// Gets the next character hit for caret navigation. /// /// The current . /// The next . internal CharacterHit GetNextCaretCharacterHit(CharacterHit characterHit) { throw new NotImplementedException(); } /// /// Gets the previous character hit for caret navigation. /// /// The current . /// The previous . internal CharacterHit GetPreviousCaretCharacterHit(CharacterHit characterHit) { throw new NotImplementedException(); } /// /// Gets the previous character hit after backspacing. /// /// The current . /// The after backspacing. internal CharacterHit GetBackspaceCaretCharacterHit(CharacterHit characterHit) { throw new NotImplementedException(); } /// /// Get an array of bounding rectangles of a range of characters within a text line. /// /// index of first character of specified range /// number of characters of the specified range /// an array of bounding rectangles. internal IReadOnlyList GetTextBounds(int firstTextSourceCharacterIndex, int textLength) { Vector2 iniPoint = TextParagraphProperties.GUIStyle.GetCursorPixelPosition(LineRect, PlainGUIContent, firstTextSourceCharacterIndex - _initialTextLineOffset); Vector2 endPoint = TextParagraphProperties.GUIStyle.GetCursorPixelPosition(LineRect, PlainGUIContent, firstTextSourceCharacterIndex + textLength - _initialTextLineOffset); return new List() { new Rect(iniPoint.x, iniPoint.y, endPoint.x - iniPoint.x, endPoint.y - iniPoint.y) }; } public void Dispose() { } } }