3 #if SILICONSTUDIO_PLATFORM_IOS
7 using MonoTouch.Foundation;
9 using SiliconStudio.Paradox.Games;
10 using SiliconStudio.Paradox.UI.Events;
12 namespace SiliconStudio.
Paradox.UI.Controls
14 public partial class EditText
16 private UITextField attachedTextField;
18 private static UIButton doneButton;
19 private static UITextField textField;
20 private static EditText currentActiveEditText;
21 private static UIView barView;
22 private static UIView overlayView;
23 private static GameContext gameContext;
25 private static void InitializeStaticImpl()
27 doneButton = UIButton.FromType(UIButtonType.RoundedRect);
28 doneButton.SetTitle(NSBundle.MainBundle.LocalizedString(
"UIDoneButton", null), UIControlState.Normal);
29 doneButton.SetTitleColor(UIColor.Black, UIControlState.Normal);
30 doneButton.TouchDown += DoneButtonOnTouchDown;
32 textField =
new UITextField
34 KeyboardType = UIKeyboardType.Default,
35 BorderStyle = UITextBorderStyle.RoundedRect,
37 textField.EditingDidEnd += TextFieldOnEditingDidEnd;
38 textField.EditingDidBegin += TextFieldOnEditingDidBegin;
40 barView =
new UIView {
Hidden =
true };
41 barView.AddSubview(textField);
42 barView.AddSubview(doneButton);
43 barView.BackgroundColor = UIColor.Gray;
45 overlayView =
new UIView {
Hidden =
true };
46 overlayView.AddSubview(barView);
47 overlayView.BackgroundColor =
new UIColor(0,0,0,0.4f);
50 private static void TextFieldOnEditingDidBegin(
object sender,
EventArgs eventArgs)
52 overlayView.Hidden =
false;
53 barView.Hidden =
false;
55 if (currentActiveEditText != null)
60 currentActiveEditText.game.SlowDownDrawCalls =
true;
64 private void InitializeImpl()
66 if (gameContext == null)
68 gameContext = game.Context;
69 gameContext.GameView.AddSubview(overlayView);
71 NSNotificationCenter.DefaultCenter.AddObserver(UIDevice.OrientationDidChangeNotification, OnScreenRotated);
73 UpdateOverlayAndEditBarLayout();
77 private void OnScreenRotated(NSNotification nsNotification)
79 if (gameContext == null)
82 UpdateOverlayAndEditBarLayout();
85 private static void UpdateOverlayAndEditBarLayout()
87 const int spaceX = 10;
89 const int buttonWidth = 60;
90 const int buttonHeight = 35;
91 const int barHeight = buttonHeight + 2*spaceY;
93 var viewFrame = gameContext.GameView.Frame;
95 barView.Frame =
new RectangleF(0, 0, viewFrame.Width, barHeight);
96 overlayView.Frame =
new RectangleF(viewFrame.X, viewFrame.Y, 2 * viewFrame.Width, viewFrame.Height);
97 textField.Frame =
new RectangleF(spaceX, spaceY, viewFrame.Width - buttonWidth - 3*spaceX, buttonHeight);
98 doneButton.Frame =
new RectangleF(viewFrame.Width - buttonWidth - spaceX, spaceY, buttonWidth, buttonHeight);
101 private static void TextFieldOnEditingDidEnd(
object sender,
EventArgs eventArgs)
103 currentActiveEditText.IsSelectionActive =
false;
104 barView.Hidden =
true;
105 overlayView.Hidden =
true;
106 FocusedElement = null;
108 if (currentActiveEditText != null)
111 currentActiveEditText.game.SlowDownDrawCalls =
false;
115 private static void DoneButtonOnTouchDown(
object sender,
EventArgs eventArgs)
117 currentActiveEditText.IsSelectionActive =
false;
120 private void TextFieldOnValueChanged(
object sender,
EventArgs eventArgs)
122 if (attachedTextField == null)
126 if (text == attachedTextField.Text)
129 text = attachedTextField.Text;
130 UpdateTextToDisplay();
136 private int GetLineCountImpl()
141 private void OnMaxLinesChangedImpl()
145 private void OnMinLinesChangedImpl()
149 private void ActivateEditTextImpl()
151 currentActiveEditText =
this;
152 attachedTextField = textField;
154 UpdateInputTypeImpl();
155 attachedTextField.Text = text;
156 attachedTextField.EditingChanged += TextFieldOnValueChanged;
157 attachedTextField.ShouldChangeCharacters += ShouldChangeCharacters;
158 attachedTextField.BecomeFirstResponder();
161 private bool ShouldChangeCharacters(UITextField theTextField, NSRange range,
string replacementString)
164 var predicate = CharacterFilterPredicate;
165 foreach (var character
in replacementString)
167 if (predicate != null && !predicate(character))
171 var replacementSize = replacementString.Length - range.Length;
172 return replacementSize < 0 || theTextField.Text.Length + replacementSize <= MaxLength;
175 private void DeactivateEditTextImpl()
177 attachedTextField.EditingChanged -= TextFieldOnValueChanged;
178 attachedTextField.ShouldChangeCharacters -= ShouldChangeCharacters;
179 attachedTextField.SecureTextEntry =
false;
180 attachedTextField.ResignFirstResponder();
181 attachedTextField = null;
182 currentActiveEditText = null;
185 private void OnTouchMoveImpl(TouchEventArgs args)
189 private void OnTouchDownImpl(TouchEventArgs args)
193 private void UpdateInputTypeImpl()
195 if (attachedTextField == null)
198 attachedTextField.SecureTextEntry = ShouldHideText;
201 private void UpdateSelectionToEditImpl()
203 if (attachedTextField == null)
206 attachedTextField.SelectedTextRange = attachedTextField.GetTextRange(
207 attachedTextField.GetPosition(attachedTextField.BeginningOfDocument, selectionStart),
208 attachedTextField.GetPosition(attachedTextField.BeginningOfDocument, selectionStop));
211 private void UpdateSelectionFromEditImpl()
213 if (attachedTextField == null)
216 selectionStart = attachedTextField.GetOffsetFromPosition(attachedTextField.BeginningOfDocument, attachedTextField.SelectedTextRange.Start);
217 selectionStop = attachedTextField.GetOffsetFromPosition(attachedTextField.BeginningOfDocument, attachedTextField.SelectedTextRange.End);
220 private void UpdateTextToEditImpl()
222 if (attachedTextField == null)
226 if (Text != attachedTextField.Text)
227 attachedTextField.Text = text;
SiliconStudio.Core.Mathematics.RectangleF RectangleF