forked from stride3d/stride
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEditText.iOS.cs
More file actions
263 lines (213 loc) · 9.43 KB
/
EditText.iOS.cs
File metadata and controls
263 lines (213 loc) · 9.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
// Copyright (c) .NET Foundation and Contributors (https://dotnetfoundation.org/ & https://stride3d.net) and Silicon Studio Corp. (https://www.siliconstudio.co.jp)
// Distributed under the MIT license. See the LICENSE.md file in the project root for more information.
#if STRIDE_PLATFORM_IOS
using System;
using System.Diagnostics;
using System.Drawing;
using Foundation;
using UIKit;
using Stride.Core;
using Stride.Core.Annotations;
using Stride.Games;
using Stride.Input;
using Stride.UI.Events;
namespace Stride.UI.Controls
{
public partial class EditText
{
private UITextField attachedTextField;
private static UIButton doneButton;
private static UITextField textField;
private static EditText currentActiveEditText;
private static UIView barView;
private static UIView overlayView;
private static GameContextiOS gameContext;
private static void InitializeStaticImpl()
{
doneButton = UIButton.FromType(UIButtonType.RoundedRect);
doneButton.SetTitle(NSBundle.MainBundle.GetLocalizedString("UIDoneButton", null), UIControlState.Normal);
doneButton.SetTitleColor(UIColor.Black, UIControlState.Normal);
doneButton.TouchDown += DoneButtonOnTouchDown;
textField = new UITextField
{
KeyboardType = UIKeyboardType.Default,
BorderStyle = UITextBorderStyle.RoundedRect,
};
textField.EditingDidEnd += TextFieldOnEditingDidEnd;
textField.EditingDidBegin += TextFieldOnEditingDidBegin;
barView = new UIView { Hidden = true };
barView.AddSubview(textField);
barView.AddSubview(doneButton);
barView.BackgroundColor = UIColor.Gray;
overlayView = new UIView { Hidden = true };
overlayView.AddSubview(barView);
overlayView.BackgroundColor = new UIColor(0,0,0,0.4f);
}
[NotNull]
private GameBase GetGame()
{
if (UIElementServices.Services == null)
throw new InvalidOperationException("services");
return (GameBase)UIElementServices.Services.GetSafeServiceAs<IGame>();
}
private static void TextFieldOnEditingDidBegin(object sender, EventArgs eventArgs)
{
overlayView.Hidden = false;
barView.Hidden = false;
if (currentActiveEditText != null)
{
// TODO: Check if this is still needed; currently disabled since SlowDownDrawCalls was removed
// we need to skip some draw calls here to let the time to iOS to draw its own keyboard animations... (Thank you iOS)
// If we don't do this when changing the type of keyboard (split / docked / undocked), the keyboard freeze for about 5/10 seconds before updating.
// Note: Setting UIView.EnableAnimation to false does not solve the problem. Only animation when the keyboard appear/disappear are skipped.
//currentActiveEditText.GetGame().SlowDownDrawCalls = true;
}
}
private void InitializeImpl()
{
// nothing to do here
}
private void EnsureGameContext()
{
if (gameContext == null)
{
var game = GetGame();
Debug.Assert(game.Context is GameContextiOS, "There is only one possible descendant of GameContext for iOS.");
gameContext = (GameContextiOS)game.Context;
throw new NotImplementedException();
//gameContext.Control.GameView.AddSubview(overlayView);
NSNotificationCenter.DefaultCenter.AddObserver(UIDevice.OrientationDidChangeNotification, OnScreenRotated);
UpdateOverlayAndEditBarLayout();
}
}
private void OnScreenRotated(NSNotification nsNotification)
{
if (gameContext == null)
return;
UpdateOverlayAndEditBarLayout();
}
private static void UpdateOverlayAndEditBarLayout()
{
const int spaceX = 10;
const int spaceY = 5;
const int buttonWidth = 60;
const int buttonHeight = 35;
const int barHeight = buttonHeight + 2*spaceY;
throw new NotImplementedException();
/*var viewFrame = gameContext.Control.GameView.Frame;
barView.Frame = new RectangleF(0, 0, (int)viewFrame.Width, barHeight);
overlayView.Frame = new RectangleF((int)viewFrame.X, (int)viewFrame.Y, 2 * (int)viewFrame.Width, (int)viewFrame.Height); // if we don't over-set width background can be seen during rotation...
textField.Frame = new RectangleF(spaceX, spaceY, (int)viewFrame.Width - buttonWidth - 3 * spaceX, buttonHeight);
doneButton.Frame = new RectangleF((int)viewFrame.Width - buttonWidth - spaceX, spaceY, buttonWidth, buttonHeight);*/
}
private static void TextFieldOnEditingDidEnd(object sender, EventArgs eventArgs)
{
currentActiveEditText.IsSelectionActive = false;
barView.Hidden = true;
overlayView.Hidden = true;
if (currentActiveEditText != null)
{
// TODO: Check if this is still needed; currently disabled since SlowDownDrawCalls was removed
// Editing finished, we can now draw back to normal frame rate.
//currentActiveEditText.GetGame().SlowDownDrawCalls = false;
}
}
private static void DoneButtonOnTouchDown(object sender, EventArgs eventArgs)
{
currentActiveEditText.IsSelectionActive = false;
}
private void TextFieldOnValueChanged(object sender, EventArgs eventArgs)
{
if (attachedTextField == null)
return;
// early exit if text did not changed
if (text == attachedTextField.Text)
return;
text = attachedTextField.Text;
UpdateTextToDisplay();
RaiseEvent(new RoutedEventArgs(TextChangedEvent));
InvalidateMeasure();
}
private int GetLineCountImpl()
{
return 1;
}
private void OnMaxLinesChangedImpl()
{
}
private void OnMinLinesChangedImpl()
{
}
private void ActivateEditTextImpl(InputManager inputManager)
{
EnsureGameContext();
currentActiveEditText = this;
attachedTextField = textField;
UpdateInputTypeImpl();
attachedTextField.Text = text;
attachedTextField.EditingChanged += TextFieldOnValueChanged;
attachedTextField.ShouldChangeCharacters += ShouldChangeCharacters;
attachedTextField.BecomeFirstResponder();
}
private bool ShouldChangeCharacters(UITextField theTextField, NSRange range, string replacementString)
{
// check that new characters are correct.
var predicate = CharacterFilterPredicate;
foreach (var character in replacementString)
{
if (predicate != null && !predicate(character))
return false;
}
var replacementSize = replacementString.Length - range.Length;
return replacementSize < 0 || theTextField.Text.Length + replacementSize <= MaxLength;
}
private void DeactivateEditTextImpl(InputManager inputManager)
{
attachedTextField.EditingChanged -= TextFieldOnValueChanged;
attachedTextField.ShouldChangeCharacters -= ShouldChangeCharacters;
attachedTextField.SecureTextEntry = false;
attachedTextField.ResignFirstResponder();
attachedTextField = null;
currentActiveEditText = null;
}
private void OnTouchMoveImpl(TouchEventArgs args)
{
}
private void OnTouchDownImpl(TouchEventArgs args)
{
}
private void OnTouchUpImpl(TouchEventArgs args)
{
}
private void UpdateInputTypeImpl()
{
if (attachedTextField == null)
return;
attachedTextField.SecureTextEntry = ShouldHideText;
}
private void UpdateSelectionToEditImpl()
{
if (attachedTextField == null)
return;
attachedTextField.SelectedTextRange = attachedTextField.GetTextRange(
attachedTextField.GetPosition(attachedTextField.BeginningOfDocument, selectionStart),
attachedTextField.GetPosition(attachedTextField.BeginningOfDocument, selectionStop));
}
private void UpdateSelectionFromEditImpl()
{
if (attachedTextField == null)
return;
selectionStart = (int)attachedTextField.GetOffsetFromPosition(attachedTextField.BeginningOfDocument, attachedTextField.SelectedTextRange.Start);
selectionStop = (int)attachedTextField.GetOffsetFromPosition(attachedTextField.BeginningOfDocument, attachedTextField.SelectedTextRange.End);
}
private void UpdateTextToEditImpl()
{
if (attachedTextField == null)
return;
// update the iOS text edit only the text changed to avoid re-triggering events.
if (Text != attachedTextField.Text)
attachedTextField.Text = text;
}
}
}
#endif