4 using SiliconStudio.Core.Mathematics;
5 using SiliconStudio.Paradox.Graphics;
6 using SiliconStudio.Paradox.UI.Controls;
10 namespace SiliconStudio.
Paradox.UI.Renderers
15 internal class DefaultEditTextRenderer : ElementRenderer
22 public override void RenderColor(UIElement element, UIRenderingContext context)
24 base.RenderColor(element, context);
28 if (editText.Font == null)
32 var fontScale = element.RealSizeVirtualResolutionRatio;
33 var color = editText.RenderOpacity * Color.White;
34 var image = editText.ActiveImage;
35 if(!editText.IsSelectionActive)
36 image = editText.MouseOverState == MouseOverState.MouseOverElement? editText.MouseOverImage : editText.InactiveImage;
38 if (image != null && image.Texture != null)
40 Batch.DrawImage(image.Texture, image.TextureAlpha, ref editText.WorldMatrixInternal, ref image.RegionInternal, ref editText.RenderSizeInternal, ref image.BordersInternal, ref color, context.DepthBias,
image.Orientation);
44 var textRegionSize =
new Vector2(editText.ActualWidth - editText.Padding.Left - editText.Padding.Right,
45 editText.ActualHeight - editText.Padding.Top - editText.Padding.Bottom);
48 var selectionColor = editText.RenderOpacity * editText.SelectionColor;
49 var caretColor = editText.RenderOpacity * editText.CaretColor;
51 var offsetTextStart = 0f;
52 var offsetAlignment = 0f;
53 var selectionSize = 0f;
56 if(editText.IsSelectionActive)
58 var fontSize = fontScale * editText.TextSize;
59 offsetTextStart = font.MeasureString(editText.TextToDisplay, ref fontSize, editText.SelectionStart).X;
60 selectionSize = font.MeasureString(editText.TextToDisplay, ref fontSize, editText.SelectionStart + editText.SelectionLength).X - offsetTextStart;
63 offsetTextStart /= fontScale.X;
64 selectionSize /= fontScale.X;
67 offsetAlignment = -textRegionSize.X / 2f;
70 var textWidth = font.MeasureString(editText.TextToDisplay, ref fontSize).X;
72 textWidth /= fontScale.X;
74 offsetAlignment = editText.TextAlignment == TextAlignment.Center ? -textWidth / 2 : -textRegionSize.X / 2f + (textRegionSize.X - textWidth);
77 var selectionWorldMatrix = element.WorldMatrixInternal;
78 selectionWorldMatrix.M41 += offsetTextStart + selectionSize / 2 + offsetAlignment;
79 var selectionScaleVector =
new Vector3(selectionSize, editText.LineCount * editText.Font.GetTotalLineSpacing(editText.TextSize), 0);
80 Batch.DrawRectangle(ref selectionWorldMatrix, ref selectionScaleVector, ref selectionColor, context.DepthBias + 1);
84 var drawCommand =
new SpriteFont.InternalUIDrawCommand
86 Color = editText.RenderOpacity * editText.TextColor,
87 DepthBias = context.DepthBias + 2,
88 FontScale = fontScale,
89 FontSize = editText.TextSize,
91 SnapText = editText.SnapText,
92 WorldMatrix = editText.WorldMatrixInternal,
98 Batch.DrawString(font, editText.TextToDisplay, ref drawCommand);
101 if (editText.IsCaretVisible)
103 var sizeCaret = editText.CaretWidth / fontScale.X;
104 var caretWorldMatrix = element.WorldMatrixInternal;
105 caretWorldMatrix.M41 += offsetTextStart + offsetAlignment + (editText.CaretPosition > editText.SelectionStart? selectionSize: 0);
106 var caretScaleVector =
new Vector3(sizeCaret, editText.LineCount * editText.Font.GetTotalLineSpacing(editText.TextSize), 0);
107 Batch.DrawRectangle(ref caretWorldMatrix, ref caretScaleVector, ref caretColor, context.DepthBias + 3);
SiliconStudio.Paradox.Games.Mathematics.Vector2 Vector2
SiliconStudio.Core.IServiceRegistry IServiceRegistry
TextAlignment
Specify the available text alignment when rendering text.
SiliconStudio.Core.Mathematics.Vector3 Vector3
Represents a three dimensional mathematical vector.
SharpDX.DirectWrite.Font Font
SiliconStudio.Core.IServiceRegistry IServiceRegistry
Represents a 32-bit color (4 bytes) in the form of RGBA (in byte order: R, G, B, A).
Android.Widget.Orientation Orientation
Represent an edit text where the user can enter text.