4 using System.Diagnostics;
7 using SiliconStudio.Core;
8 using SiliconStudio.Core.Mathematics;
9 using SiliconStudio.Paradox.Graphics;
12 namespace SiliconStudio.
Paradox.UI.Controls
17 [DebuggerDisplay(
"TextBlock - Name={Name}")]
30 private static void InvalidateFont(
object propertyOwner,
PropertyKey propertyKey,
object propertyOldValue)
33 element.InvalidateMeasure();
38 private bool wrapText;
40 private string wrappedText;
42 private float? textSize;
44 private bool synchronousCharacterGeneration;
66 public virtual string TextToDisplay
68 get {
return WrapText? wrappedText: Text; }
76 get {
return DependencyProperties.Get(FontPropertyKey); }
77 set { DependencyProperties.Set(FontPropertyKey, value); }
96 public Color TextColor
98 get {
return DependencyProperties.Get(TextColorPropertyKey); }
99 set { DependencyProperties.Set(TextColorPropertyKey, value); }
105 public float TextSize
109 if (textSize.HasValue)
110 return textSize.Value;
119 textSize = Math.Max(0, Math.Min(float.MaxValue, value));
131 get {
return wrapText; }
134 if(wrapText == value)
148 public bool SynchronousCharacterGeneration
150 get {
return synchronousCharacterGeneration; }
153 if(synchronousCharacterGeneration == value)
156 synchronousCharacterGeneration = value;
158 if (IsMeasureValid && synchronousCharacterGeneration)
171 public bool SnapText {
get; set; }
179 return CalculateTextSize(TextToDisplay);
189 if (textToMeasure == null)
192 return CalculateTextSize(
new SpriteFont.StringProxy(textToMeasure));
195 private Vector2 CalculateTextSize(StringBuilder textToMeasure)
197 return CalculateTextSize(
new SpriteFont.StringProxy(textToMeasure));
205 var sizeRatio = RealSizeVirtualResolutionRatio;
206 var measureFontSize = TextSize * sizeRatio;
207 var realSize = Font.MeasureString(ref textToMeasure, ref measureFontSize);
210 if(SynchronousCharacterGeneration)
211 Font.PreGenerateGlyphs(ref textToMeasure, ref measureFontSize);
216 realSize.X /= sizeRatio.X;
217 realSize.Y /= sizeRatio.Y;
226 UpdateWrappedText(availableSizeWithoutMargins);
228 return new Vector3(CalculateTextSize(), 0);
234 UpdateWrappedText(finalSizeWithoutMargins);
236 return base.ArrangeOverride(finalSizeWithoutMargins);
239 private void UpdateWrappedText(
Vector3 availableSpace)
241 var availableWidth = availableSpace.X;
242 var currentLine =
new StringBuilder(text.Length);
243 var currentText =
new StringBuilder(2 * text.Length);
245 var indexOfNewLine = 0;
248 float lineCurrentSize;
249 var indexNextCharacter = 0;
250 var indexOfLastSpace = -1;
254 lineCurrentSize = CalculateTextSize(currentLine).X;
256 if (lineCurrentSize > availableWidth || indexOfNewLine + indexNextCharacter >= text.Length)
259 var currentCharacter = text[indexOfNewLine + indexNextCharacter];
261 if (currentCharacter ==
'\n')
263 indexOfNewLine += indexNextCharacter + 1;
267 currentLine.Append(currentCharacter);
269 if (
char.IsWhiteSpace(currentCharacter))
270 indexOfLastSpace = indexNextCharacter;
272 ++indexNextCharacter;
276 if (lineCurrentSize <= availableWidth)
279 currentText.Append(currentLine);
284 if (indexOfLastSpace < 0)
287 currentLine.Remove(currentLine.Length - 1, 1);
288 indexOfNewLine += indexNextCharacter - 1;
293 if(indexNextCharacter > indexOfLastSpace)
294 currentLine.Remove(indexOfLastSpace, indexNextCharacter - indexOfLastSpace);
295 indexOfNewLine += indexOfLastSpace + 1;
301 currentLine.Append(
'\n');
302 currentText.Append(currentLine);
308 wrappedText = currentText.ToString();
Provides a base class for all the User Interface elements in Paradox applications.
Represents a two dimensional mathematical vector.
TextAlignment
Specify the available text alignment when rendering text.
virtual void OnTextChanged()
Method triggered when the Text changes. Can be overridden in inherited class to changed the default b...
Represents a three dimensional mathematical vector.
SharpDX.DirectWrite.Font Font
override Vector3 ArrangeOverride(Vector3 finalSizeWithoutMargins)
When overridden in a derived class, positions possible child elements and determines a size for a UIE...
SpriteFont to use with SpriteBatch. See SpriteFont to learn how to use it.
Represents a 32-bit color (4 bytes) in the form of RGBA (in byte order: R, G, B, A).
Vector2 CalculateTextSize(string textToMeasure)
Calculate and returns the size of the provided textToMeasure "/> in virtual pixels size...
Vector2 CalculateTextSize()
Calculate and returns the size of the Text in virtual pixels size.
SiliconStudio.Core.Mathematics.Vector3 Vector3
Provides a lightweight control for displaying small amounts of text.
override Vector3 MeasureOverride(Vector3 availableSizeWithoutMargins)
When overridden in a derived class, measures the size in layout required for possible child elements ...
A class that represents a tag propety.
TextBlock()
Create a new instance of TextBlock.