4 using System.Diagnostics;
6 using SiliconStudio.Core;
7 using SiliconStudio.Core.Mathematics;
8 using SiliconStudio.Paradox.Games;
10 namespace SiliconStudio.
Paradox.UI.Controls
15 [DebuggerDisplay(
"ScrollingText - Name={Name}")]
31 protected readonly
static PropertyKey<uint> DesiredCharacterNumberPropertyKey =
new PropertyKey<uint>(
"DesiredCharacterNumberKey", typeof(
ScrollingText), DefaultValueMetadata.Static((uint)10), ObjectInvalidationMetadata.New<uint>(InvalidateCharacterNumber));
33 private static void InvalidateCharacterNumber(
object propertyOwner,
PropertyKey<uint> propertyKey, uint propertyOldValue)
36 element.InvalidateMeasure();
39 private string textToDisplay =
"";
41 private float elementWidth;
46 private int nextLetterIndex;
48 private bool textHasBeenAppended;
53 public float ScrollingOffset {
get;
private set; }
58 public float AccumulatedWidth {
get;
private set; }
62 ResetDisplayingText();
71 public float ScrollingSpeed
73 get {
return DependencyProperties.Get(ScrollingSpeedPropertyKey); }
74 set { DependencyProperties.Set(ScrollingSpeedPropertyKey, value); }
80 public uint DesiredCharacterNumber
82 get {
return DependencyProperties.Get(DesiredCharacterNumberPropertyKey); }
83 set { DependencyProperties.Set(DesiredCharacterNumberPropertyKey, value); }
89 public bool RepeatText
91 get {
return DependencyProperties.Get(RepeatTextPropertyKey); }
92 set { DependencyProperties.Set(RepeatTextPropertyKey, value); }
95 private static void ValidateScrollingSpeedCallback(ref
float value)
98 throw new ArgumentOutOfRangeException(
"value");
101 private static void RepeatTextInvalidationCallback(
object propertyOwner,
PropertyKey<bool> propertyKey,
bool propertyOldValue)
103 var scrollingText = (ScrollingText)propertyOwner;
104 scrollingText.ResetDisplayingText();
113 if (text == null)
throw new ArgumentNullException(
"text");
115 textHasBeenAppended =
true;
129 if (!textHasBeenAppended)
130 ResetDisplayingText();
132 textHasBeenAppended =
false;
135 private void ResetDisplayingText()
139 ScrollingOffset = IsArrangeValid? ActualWidth: float.PositiveInfinity;
140 AccumulatedWidth = 0;
143 public override string TextToDisplay
145 get {
return textToDisplay; }
152 private float CalculateTextToDisplayWidth()
154 return CalculateTextSize(TextToDisplay).X;
161 UpdateAndAdjustDisplayText(time);
164 private void UpdateAndAdjustDisplayText(
GameTime time = null)
166 if (
string.IsNullOrEmpty(Text))
169 var elapsedSeconds = time != null ? (float)time.Elapsed.TotalSeconds : 0f;
172 var nextOffsetShift = elapsedSeconds * ScrollingSpeed - ScrollingOffset;
175 var sizeNextTextToDisplay = nextOffsetShift + elementWidth;
178 var textToDisplayWidth = CalculateTextToDisplayWidth();
179 while (textToDisplayWidth < sizeNextTextToDisplay && nextLetterIndex < Text.Length)
181 textToDisplay += Text[nextLetterIndex++];
183 var addedCharacterWidth = CalculateTextToDisplayWidth() - textToDisplayWidth;
184 AccumulatedWidth += addedCharacterWidth;
186 if (RepeatText && nextLetterIndex >= Text.Length)
189 textToDisplayWidth += addedCharacterWidth;
193 if (CalculateTextSize(textToDisplay).X < nextOffsetShift)
197 var fontSize =
new Vector2(TextSize, TextSize);
198 while (textToDisplay.Length > 1 &&
Font.MeasureString(textToDisplay, ref fontSize, 1).X < nextOffsetShift)
200 nextOffsetShift -= Font.MeasureString(textToDisplay, ref fontSize, 1).X;
201 textToDisplay = textToDisplay.Substring(1);
205 ScrollingOffset = -nextOffsetShift;
210 return MeasureSize();
215 elementWidth = finalSizeWithoutMargins.X;
217 ScrollingOffset = Math.Min(elementWidth, ScrollingOffset);
219 UpdateAndAdjustDisplayText();
221 return base.ArrangeOverride(finalSizeWithoutMargins);
233 return new Vector3(
Font.MeasureString(
new string(
'A', (
int)DesiredCharacterNumber)), 0);
Provides a base class for all the User Interface elements in Paradox applications.
SiliconStudio.Paradox.Games.Mathematics.Vector2 Vector2
void ClearText()
Clear the currently scrolling text.
Represents a three dimensional mathematical vector.
Vector3 MeasureSize()
Measure the size of the ScrollingText element.
override void Update(GameTime time)
Method called by IUIElementUpdate.Update. This method can be overridden by inherited classes to perfo...
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...
Current timing used for variable-step (real time) or fixed-step (game time) games.
override void OnTextChanged()
Method triggered when the Text changes. Can be overridden in inherited class to changed the default b...
void AppendText(string text)
Append the provided text to the end of the current TextBlock.Text without restarting the display to t...
A text viewer that scrolls automatically the text from right to left.
override Vector3 MeasureOverride(Vector3 availableSizeWithoutMargins)
When overridden in a derived class, measures the size in layout required for possible child elements ...
SiliconStudio.Core.Mathematics.Vector3 Vector3
Provides a lightweight control for displaying small amounts of text.
A class that represents a tag propety.