Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
DefaultEditTextRenderer.cs
Go to the documentation of this file.
1 // Copyright (c) 2014 Silicon Studio Corp. (http://siliconstudio.co.jp)
2 // This file is distributed under GPL v3. See LICENSE.md for details.
3 
4 using SiliconStudio.Core.Mathematics;
5 using SiliconStudio.Paradox.Graphics;
6 using SiliconStudio.Paradox.UI.Controls;
9 
10 namespace SiliconStudio.Paradox.UI.Renderers
11 {
12  /// <summary>
13  /// The default renderer for <see cref="EditText"/>.
14  /// </summary>
15  internal class DefaultEditTextRenderer : ElementRenderer
16  {
17  public DefaultEditTextRenderer(IServiceRegistry services)
18  : base(services)
19  {
20  }
21 
22  public override void RenderColor(UIElement element, UIRenderingContext context)
23  {
24  base.RenderColor(element, context);
25 
26  var editText = (EditText)element;
27 
28  if (editText.Font == null)
29  return;
30 
31  // determine the image to draw in background of the edit text
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;
37 
38  if (image != null && image.Texture != null)
39  {
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);
41  }
42 
43  // calculate the size of the text region by removing padding
44  var textRegionSize = new Vector2(editText.ActualWidth - editText.Padding.Left - editText.Padding.Right,
45  editText.ActualHeight - editText.Padding.Top - editText.Padding.Bottom);
46 
47  var font = editText.Font;
48  var selectionColor = editText.RenderOpacity * editText.SelectionColor;
49  var caretColor = editText.RenderOpacity * editText.CaretColor;
50 
51  var offsetTextStart = 0f;
52  var offsetAlignment = 0f;
53  var selectionSize = 0f;
54 
55  // Draw the selection
56  if(editText.IsSelectionActive)
57  {
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;
61  if (font.IsDynamic)
62  {
63  offsetTextStart /= fontScale.X;
64  selectionSize /= fontScale.X;
65  }
66 
67  offsetAlignment = -textRegionSize.X / 2f;
68  if (editText.TextAlignment != TextAlignment.Left)
69  {
70  var textWidth = font.MeasureString(editText.TextToDisplay, ref fontSize).X;
71  if (font.IsDynamic)
72  textWidth /= fontScale.X;
73 
74  offsetAlignment = editText.TextAlignment == TextAlignment.Center ? -textWidth / 2 : -textRegionSize.X / 2f + (textRegionSize.X - textWidth);
75  }
76 
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);
81  }
82 
83  // create the text draw command
84  var drawCommand = new SpriteFont.InternalUIDrawCommand
85  {
86  Color = editText.RenderOpacity * editText.TextColor,
87  DepthBias = context.DepthBias + 2,
88  FontScale = fontScale,
89  FontSize = editText.TextSize,
90  Batch = Batch,
91  SnapText = editText.SnapText,
92  WorldMatrix = editText.WorldMatrixInternal,
93  Alignment = editText.TextAlignment,
94  Size = textRegionSize
95  };
96 
97  // Draw the text
98  Batch.DrawString(font, editText.TextToDisplay, ref drawCommand);
99 
100  // Draw the cursor
101  if (editText.IsCaretVisible)
102  {
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);
108  }
109  }
110  }
111 }
SiliconStudio.Paradox.Games.Mathematics.Vector2 Vector2
SiliconStudio.Core.IServiceRegistry IServiceRegistry
Definition: ModelRenderer.cs:9
TextAlignment
Specify the available text alignment when rendering text.
Definition: TextAlignment.cs:8
SiliconStudio.Core.Mathematics.Vector3 Vector3
Represents a three dimensional mathematical vector.
Definition: Vector3.cs:42
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).
Definition: Color.cs:16
Android.Widget.Orientation Orientation
Definition: Section.cs:9
Represent an edit text where the user can enter text.
Definition: EditText.cs:20