3 #if SILICONSTUDIO_PLATFORM_WINDOWS_DESKTOP
6 using SiliconStudio.Core.Mathematics;
7 using SiliconStudio.Paradox.Input;
10 namespace SiliconStudio.
Paradox.UI.Controls
12 public partial class EditText
14 private static void InitializeStaticImpl()
18 private void InitializeImpl()
22 private int GetLineCountImpl()
27 return text.Split(
'\n').Length;
30 private void OnMaxLinesChangedImpl()
34 private void OnMinLinesChangedImpl()
38 private void ActivateEditTextImpl()
42 private void DeactivateEditTextImpl()
44 FocusedElement = null;
47 private void OnTouchMoveImpl(TouchEventArgs args)
49 var currentPosition = FindNearestCharacterIndex(
new Vector2(args.WorldPosition.X - WorldMatrix.M41, args.WorldPosition.Y - WorldMatrix.M42));
53 if (currentPosition < selectionStop)
54 Select(currentPosition, selectionStop - currentPosition,
true);
56 Select(selectionStop, currentPosition - selectionStop);
60 if (currentPosition < SelectionStart)
61 Select(currentPosition, selectionStart - currentPosition,
true);
63 Select(selectionStart, currentPosition - selectionStart);
67 private void OnTouchDownImpl(TouchEventArgs args)
70 CaretPosition = FindNearestCharacterIndex(
new Vector2(args.WorldPosition.X - WorldMatrix.M41, args.WorldPosition.Y - WorldMatrix.M42));
78 protected virtual int FindNearestCharacterIndex(
Vector2 position)
83 var textRegionSize = (ActualWidth - Padding.Left - Padding.Right);
84 var fontScale = RealSizeVirtualResolutionRatio;
85 var fontSize = fontScale * TextSize;
88 var alignmentOffset = -textRegionSize / 2f;
91 var textWidth = Font.MeasureString(TextToDisplay, ref fontSize).X;
93 textWidth /= fontScale.X;
95 alignmentOffset =
TextAlignment == TextAlignment.Center ? -textWidth / 2 : -textRegionSize / 2f + (textRegionSize - textWidth);
97 var touchInText = position.X - alignmentOffset;
100 var characterIndex = 1;
101 var previousCharacterOffset = 0f;
102 var currentCharacterOffset = Font.MeasureString(TextToDisplay, ref fontSize, characterIndex).X;
103 while (currentCharacterOffset < touchInText && characterIndex < textToDisplay.Length)
106 previousCharacterOffset = currentCharacterOffset;
107 currentCharacterOffset = Font.MeasureString(TextToDisplay, ref fontSize, characterIndex).X;
109 currentCharacterOffset /= fontScale.X;
117 if (currentCharacterOffset < touchInText)
119 return textToDisplay.Length;
122 const float Alpha = 0.66f;
123 var previousElementRatio = Math.Abs(touchInText - previousCharacterOffset) / Alpha;
124 var currentElementRation = Math.Abs(currentCharacterOffset - touchInText) / (1 - Alpha);
125 return previousElementRatio < currentElementRation ? characterIndex - 1 : characterIndex;
128 internal override void OnKeyPressed(KeyEventArgs args)
130 base.OnKeyPressed(args);
132 InterpretKey(args.Key, args.Input);
135 private void InterpretKey(
Keys key, InputManagerBase input)
138 if (SelectionLength > 0 && (key ==
Keys.Delete || key ==
Keys.Back))
145 if (key ==
Keys.Back)
147 selectionStart = Math.Max(0, selectionStart - 1);
153 if (key ==
Keys.Delete)
161 if (key ==
Keys.Left && (input.IsKeyDown(
Keys.LeftShift) || input.IsKeyDown(Keys.RightShift)))
163 if (caretAtStart || selectionStart == selectionStop)
164 Select(selectionStart - 1, SelectionLength + 1,
true);
166 Select(selectionStart, SelectionLength - 1);
172 if (key ==
Keys.Right && (input.IsKeyDown(
Keys.LeftShift) || input.IsKeyDown(Keys.RightShift)))
174 if (caretAtStart && selectionStart != selectionStop)
175 Select(selectionStart + 1, SelectionLength - 1,
true);
177 Select(selectionStart, SelectionLength + 1);
183 if (key ==
Keys.Left)
185 CaretPosition = CaretPosition - 1;
190 if (key ==
Keys.Right)
192 CaretPosition = CaretPosition + 1;
197 if (key ==
Keys.Enter || key ==
Keys.Escape)
199 IsSelectionActive =
false;
204 var character =
'\0';
205 if (TryConvertKeyToCharacter(key, input.IsKeyDown(
Keys.LeftShift) || input.IsKeyDown(Keys.RightShift), ref character))
206 SelectedText =
new string(character, 1);
209 private bool TryConvertKeyToCharacter(
Keys key,
bool isMajuscule, ref Char character)
222 case Keys.HangulMode:
227 case Keys.ImeConvert:
228 case Keys.ImeNonConvert:
230 case Keys.ImeModeChange:
242 case Keys.PrintScreen:
279 case Keys.RightShift:
284 case Keys.BrowserBack:
285 case Keys.BrowserForward:
286 case Keys.BrowserRefresh:
287 case Keys.BrowserStop:
288 case Keys.BrowserSearch:
289 case Keys.BrowserFavorites:
290 case Keys.BrowserHome:
291 case Keys.VolumeMute:
292 case Keys.VolumeDown:
294 case Keys.MediaNextTrack:
295 case Keys.MediaPreviousTrack:
297 case Keys.MediaPlayPause:
298 case Keys.LaunchMail:
299 case Keys.SelectMedia:
300 case Keys.LaunchApplication1:
301 case Keys.LaunchApplication2:
324 case Keys.NumPadEnter:
325 case Keys.NumPadDecimal:
361 character = isMajuscule?
'A':
'a';
364 character = isMajuscule ?
'B' :
'b';
367 character = isMajuscule ?
'C' :
'c';
370 character = isMajuscule ?
'D' :
'd';
373 character = isMajuscule ?
'E' :
'e';
376 character = isMajuscule ?
'F' :
'f';
379 character = isMajuscule ?
'G' :
'g';
382 character = isMajuscule ?
'H' :
'h';
385 character = isMajuscule ?
'I' :
'i';
388 character = isMajuscule ?
'J' :
'j';
391 character = isMajuscule ?
'K' :
'k';
394 character = isMajuscule ?
'L' :
'l';
397 character = isMajuscule ?
'M' :
'm';
400 character = isMajuscule ?
'N' :
'n';
403 character = isMajuscule ?
'O' :
'o';
406 character = isMajuscule ?
'P' :
'p';
409 character = isMajuscule ?
'Q' :
'q';
412 character = isMajuscule ?
'R' :
'r';
415 character = isMajuscule ?
'S' :
's';
418 character = isMajuscule ?
'T' :
't';
421 character = isMajuscule ?
'U' :
'u';
424 character = isMajuscule ?
'V' :
'v';
427 character = isMajuscule ?
'W' :
'w';
430 character = isMajuscule ?
'X' :
'x';
433 character = isMajuscule ?
'Y' :
'y';
436 character = isMajuscule ?
'Z' :
'z';
481 throw new ArgumentOutOfRangeException(
"key");
487 private void UpdateTextToEditImpl()
491 private void UpdateInputTypeImpl()
495 private void UpdateSelectionFromEditImpl()
499 private void UpdateSelectionToEditImpl()
SiliconStudio.Paradox.Games.Mathematics.Vector2 Vector2
TextAlignment
Specify the available text alignment when rendering text.
SiliconStudio.Paradox.Input.Keys Keys
SharpDX.DirectWrite.Font Font
Allow data to be stored in the alpha component.