Skip to content

Commit 63b5509

Browse files
committed
Merge pull request #503 from apportable/apportable_textfield_fixes
Made CCTextField apportable/android friendly - Former-commit-id: 4e8e0c4
2 parents 35d75ea + 8c60176 commit 63b5509

File tree

1 file changed

+48
-6
lines changed

1 file changed

+48
-6
lines changed

cocos2d-ui/CCTextField.m

Lines changed: 48 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,13 @@
2424

2525
#import "CCTextField.h"
2626
#import "CCControlSubclass.h"
27+
#import "CCDirector_Private.h"
2728

28-
@implementation CCTextField
29+
@implementation CCTextField {
30+
#if defined(APPORTABLE)
31+
BOOL _textFieldIsEditing;
32+
#endif
33+
}
2934

3035
+ (id) textFieldWithSpriteFrame:(CCSpriteFrame *)frame
3136
{
@@ -189,6 +194,10 @@ - (void) layout
189194
#ifdef __CC_PLATFORM_IOS
190195
- (void)textFieldDidBeginEditing:(UITextField *)textField
191196
{
197+
#if defined(APPORTABLE)
198+
_textFieldIsEditing = YES;
199+
#endif
200+
192201
if (_keyboardIsShown)
193202
{
194203
[self focusOnTextField];
@@ -197,6 +206,9 @@ - (void)textFieldDidBeginEditing:(UITextField *)textField
197206

198207
- (void)textFieldDidEndEditing:(UITextField *)textField
199208
{
209+
#if defined(APPORTABLE)
210+
_textFieldIsEditing = NO;
211+
#endif
200212
[self endFocusingOnTextField];
201213
}
202214

@@ -256,7 +268,13 @@ - (void)keyboardWasShown:(NSNotification*)notification
256268

257269
_keyboardHeight = kbSize.height;
258270

259-
if (_textField.isEditing)
271+
BOOL focusOnTextField = _textField.isEditing;
272+
273+
#if defined(APPORTABLE)
274+
focusOnTextField = _textFieldIsEditing;
275+
#endif
276+
277+
if (focusOnTextField)
260278
{
261279
[self focusOnTextField];
262280
}
@@ -275,11 +293,18 @@ - (void) keyboardWillBeHidden:(NSNotification*) notification
275293

276294
- (void) focusOnTextField
277295
{
296+
#if defined(APPORTABLE)
297+
// Ensure that all textfields have actually been positioned before checkings textField.frame property,
298+
// it's possible for the apportable keyboard notification to be fired before the mainloop has had a chance to kick of a scheduler update
299+
CCDirector *director = [CCDirector sharedDirector];
300+
[director.scheduler update:0.0];
301+
#endif
302+
278303
CGSize windowSize = [[CCDirector sharedDirector] viewSize];
279304

280305
// Find the location of the textField
281306
float fieldCenterY = _textField.frame.origin.y - (_textField.frame.size.height/2);
282-
307+
283308
// Upper third part of the screen
284309
float upperThirdHeight = windowSize.height / 3;
285310

@@ -290,8 +315,16 @@ - (void) focusOnTextField
290315
// Calculate offset
291316
float dstYLocation = windowSize.height / 4;
292317
float offset = -(fieldCenterY - dstYLocation);
318+
293319
if (offset < -_keyboardHeight) offset = -_keyboardHeight;
294320

321+
#if defined(APPORTABLE)
322+
// Apportable does not support changing the openglview position, so we will just change the current scenes position instead
323+
CCScene *runningScene = [[CCDirector sharedDirector] runningScene];
324+
CGPoint newPosition = runningScene.position;
325+
newPosition.y = (offset * -1);
326+
runningScene.position = newPosition;
327+
#else
295328
// Calcualte target frame
296329
UIView* view = [[CCDirector sharedDirector] view];
297330
CGRect frame = view.frame;
@@ -303,16 +336,23 @@ - (void) focusOnTextField
303336
[UIView setAnimationDuration: 0.2f];
304337

305338
view.frame = frame;
306-
307339
[UIView commitAnimations];
340+
#endif
308341
}
309342
}
310343

311344
- (void) endFocusingOnTextField
312345
{
313-
UIView* view = [[CCDirector sharedDirector] view];
314-
315346
// Slide the main view back down
347+
348+
#if defined(APPORTABLE)
349+
// Apportable does not support changing the openglview position, so we will just change the current scenes position instead
350+
CCScene *runningScene = [[CCDirector sharedDirector] runningScene];
351+
CGPoint newPosition = CGPointZero;
352+
newPosition.y = 0.0f;
353+
runningScene.position = newPosition;
354+
#else
355+
UIView* view = [[CCDirector sharedDirector] view];
316356
[UIView beginAnimations: @"textFieldAnim" context: nil];
317357
[UIView setAnimationBeginsFromCurrentState: YES];
318358
[UIView setAnimationDuration: 0.2f];
@@ -322,6 +362,8 @@ - (void) endFocusingOnTextField
322362
view.frame = frame;
323363

324364
[UIView commitAnimations];
365+
#endif
366+
325367
}
326368

327369
#endif

0 commit comments

Comments
 (0)