Skip to content

Commit

Permalink
First commit for improving iOS EditBox
Browse files Browse the repository at this point in the history
+ Moved internal stuff to the implementation file
+ Added pragma and organized methods
+ cleanup synthesize, instance variables, and overall code style for ObjC
  • Loading branch information
Mazyod committed Jul 14, 2015
1 parent 49b47f5 commit 6f8bc04
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 87 deletions.
31 changes: 9 additions & 22 deletions cocos/ui/UIEditBox/UIEditBoxImpl-ios.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,31 +36,18 @@
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>

@interface UICustomUITextField : UITextField
{
}

@end


@interface UIEditBoxImplIOS_objc : NSObject <UITextFieldDelegate>
{
UICustomUITextField* textField_;
void* editBox_;
BOOL editState_;
}

@property(nonatomic, retain) UITextField* textField;
@property(nonatomic, readonly, getter = isEditState) BOOL editState;
@property(nonatomic, assign) void* editBox;

-(id) initWithFrame: (CGRect) frameRect editBox: (void*) editBox;
-(void) doAnimationWhenKeyboardMoveWithDuration:(float)duration distance:(float)distance;
-(void) setPosition:(CGPoint) pos;
-(void) setContentSize:(CGSize) size;
-(void) visit;
-(void) openKeyboard;
-(void) closeKeyboard;
@property (nonatomic, readonly, getter = isEditState) BOOL editState;

- (instancetype)initWithFrame:(CGRect)frameRect editBox:(void *)editBox;
- (void)doAnimationWhenKeyboardMoveWithDuration:(float)duration distance:(float)distance;
- (void)setPosition:(CGPoint) pos;
- (void)setContentSize:(CGSize) size;
- (void)visit;
- (void)openKeyboard;
- (void)closeKeyboard;

@end

Expand Down
163 changes: 98 additions & 65 deletions cocos/ui/UIEditBox/UIEditBoxImpl-ios.mm
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,19 @@ of this software and associated documentation files (the "Software"), to deal
#include "2d/CCLabel.h"
#import "platform/ios/CCEAGLView-ios.h"

#define getEditBoxImplIOS() ((cocos2d::ui::EditBoxImplIOS*)editBox_)
#define getEditBoxImplIOS() ((cocos2d::ui::EditBoxImplIOS *)_editBox)

static const int CC_EDIT_BOX_PADDING = 5;

#pragma mark - Internal Classes

/** TODO: Missing doc - Why is this subclass necessary?
*/
@interface UICustomUITextField : UITextField
@end

@implementation UICustomUITextField

- (CGRect)textRectForBounds:(CGRect)bounds
{
auto glview = cocos2d::Director::getInstance()->getOpenGLView();
Expand All @@ -46,113 +54,134 @@ - (CGRect)textRectForBounds:(CGRect)bounds
return CGRectMake(bounds.origin.x + padding, bounds.origin.y + padding,
bounds.size.width - padding*2, bounds.size.height - padding*2);
}
- (CGRect)editingRectForBounds:(CGRect)bounds {

- (CGRect)editingRectForBounds:(CGRect)bounds
{
return [self textRectForBounds:bounds];
}

@end

#pragma mark - UIEditBox private declerations

@implementation UIEditBoxImplIOS_objc
@interface UIEditBoxImplIOS_objc ()

@synthesize textField = textField_;
@synthesize editState = editState_;
@synthesize editBox = editBox_;
@property (nonatomic, retain) UITextField *textField;
@property (nonatomic, assign) void *editBox;

- (void)dealloc
{
[textField_ resignFirstResponder];
[textField_ removeFromSuperview];
self.textField = NULL;
[super dealloc];
}
@end

#pragma mark - UIEditBox iOS implementation

-(id) initWithFrame: (CGRect) frameRect editBox: (void*) editBox
@implementation UIEditBoxImplIOS_objc

#pragma mark - Init & Dealloc

- (instancetype)initWithFrame:(CGRect)frameRect editBox:(void *)editBox
{
self = [super init];

if (self)
{
editState_ = NO;
self.textField = [[[UICustomUITextField alloc] initWithFrame: frameRect] autorelease];

[textField_ setTextColor:[UIColor whiteColor]];
//TODO: need to delete hard code here.
textField_.font = [UIFont systemFontOfSize:frameRect.size.height*2/3];
textField_.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
textField_.backgroundColor = [UIColor clearColor];
textField_.borderStyle = UITextBorderStyleNone;
textField_.delegate = self;
textField_.hidden = true;
textField_.returnKeyType = UIReturnKeyDefault;
[textField_ addTarget:self action:@selector(textChanged) forControlEvents:UIControlEventEditingChanged];
_editState = NO;
UITextField *textField = [[[UICustomUITextField alloc] initWithFrame: frameRect] autorelease];

textField.textColor = [UIColor whiteColor];
// TODO: need to delete hard code here.
textField.font = [UIFont systemFontOfSize:frameRect.size.height*2/3];
textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
textField.backgroundColor = [UIColor clearColor];
textField.borderStyle = UITextBorderStyleNone;
textField.delegate = self;
textField.hidden = true;
textField.returnKeyType = UIReturnKeyDefault;

[textField addTarget:self action:@selector(textChanged) forControlEvents:UIControlEventEditingChanged];

self.textField = textField;
self.editBox = editBox;
}

return self;
}

-(void) doAnimationWhenKeyboardMoveWithDuration:(float)duration distance:(float)distance
- (void)dealloc
{
[_textField resignFirstResponder];
[_textField removeFromSuperview];

self.textField = NULL;

[super dealloc];
}

#pragma mark - Public methods

- (void)doAnimationWhenKeyboardMoveWithDuration:(float)duration distance:(float)distance
{
auto view = cocos2d::Director::getInstance()->getOpenGLView();
CCEAGLView *eaglview = (CCEAGLView *) view->getEAGLView();
CCEAGLView *eaglview = (CCEAGLView *)view->getEAGLView();

[eaglview doAnimationWhenKeyboardMoveWithDuration:duration distance:distance];
}

-(void) setPosition:(CGPoint) pos
- (void)setPosition:(CGPoint)pos
{
CGRect frame = [textField_ frame];
// TODO: Handle anchor point?
CGRect frame = _textField.frame;
frame.origin = pos;
[textField_ setFrame:frame];

_textField.frame = frame;
}

-(void) setContentSize:(CGSize) size
- (void)setContentSize:(CGSize)size
{
CGRect frame = [textField_ frame];
CGRect frame = _textField.frame;
frame.size = size;
[textField_ setFrame:frame];

_textField.frame = frame;
}

-(void) visit
- (void)visit
{

}

-(void) openKeyboard
- (void)openKeyboard
{
auto view = cocos2d::Director::getInstance()->getOpenGLView();
CCEAGLView *eaglview = (CCEAGLView *) view->getEAGLView();
CCEAGLView *eaglview = (CCEAGLView *)view->getEAGLView();

[eaglview addSubview:textField_];
[textField_ becomeFirstResponder];
[eaglview addSubview:_textField];
[_textField becomeFirstResponder];
}

-(void) closeKeyboard
- (void)closeKeyboard
{
[textField_ resignFirstResponder];
[textField_ removeFromSuperview];
[_textField resignFirstResponder];
[_textField removeFromSuperview];
}

- (BOOL)textFieldShouldReturn:(UITextField *)sender
{
if (sender == textField_) {
if (sender == _textField) {
[sender resignFirstResponder];
}
return NO;
}

-(void)animationSelector
- (void)animationSelector
{
auto view = cocos2d::Director::getInstance()->getOpenGLView();
CCEAGLView *eaglview = (CCEAGLView *) view->getEAGLView();
CCEAGLView *eaglview = (CCEAGLView *)view->getEAGLView();

[eaglview doAnimationWhenAnotherEditBeClicked];
}

- (BOOL)textFieldShouldBeginEditing:(UITextField *)sender // return NO to disallow editing.
{
CCLOG("textFieldShouldBeginEditing...");
editState_ = YES;
_editState = YES;

auto view = cocos2d::Director::getInstance()->getOpenGLView();
CCEAGLView *eaglview = (CCEAGLView *) view->getEAGLView();
Expand All @@ -161,55 +190,59 @@ - (BOOL)textFieldShouldBeginEditing:(UITextField *)sender // return NO to
{
[self performSelector:@selector(animationSelector) withObject:nil afterDelay:0.0f];
}
cocos2d::ui::EditBoxDelegate* pDelegate = getEditBoxImplIOS()->getDelegate();

cocos2d::ui::EditBoxDelegate *pDelegate = getEditBoxImplIOS()->getDelegate();

if (pDelegate != NULL)
{
pDelegate->editBoxEditingDidBegin(getEditBoxImplIOS()->getEditBox());
}

#if CC_ENABLE_SCRIPT_BINDING
cocos2d::ui::EditBox* pEditBox= getEditBoxImplIOS()->getEditBox();
cocos2d::ui::EditBox *pEditBox= getEditBoxImplIOS()->getEditBox();
if (NULL != pEditBox && 0 != pEditBox->getScriptEditBoxHandler())
{
cocos2d::CommonScriptData data(pEditBox->getScriptEditBoxHandler(), "began",pEditBox);
cocos2d::ScriptEvent event(cocos2d::kCommonEvent,(void*)&data);
cocos2d::CommonScriptData data(pEditBox->getScriptEditBoxHandler(), "began", pEditBox);
cocos2d::ScriptEvent event(cocos2d::kCommonEvent, (void *)&data);
cocos2d::ScriptEngineManager::getInstance()->getScriptEngine()->sendEvent(&event);
}
#endif

return YES;
}

- (BOOL)textFieldShouldEndEditing:(UITextField *)sender
{
CCLOG("textFieldShouldEndEditing...");
editState_ = NO;
_editState = NO;
getEditBoxImplIOS()->refreshInactiveText();

cocos2d::ui::EditBoxDelegate* pDelegate = getEditBoxImplIOS()->getDelegate();
cocos2d::ui::EditBoxDelegate *pDelegate = getEditBoxImplIOS()->getDelegate();
if (pDelegate != NULL)
{
pDelegate->editBoxEditingDidEnd(getEditBoxImplIOS()->getEditBox());
pDelegate->editBoxReturn(getEditBoxImplIOS()->getEditBox());
}

#if CC_ENABLE_SCRIPT_BINDING
cocos2d::ui::EditBox* pEditBox= getEditBoxImplIOS()->getEditBox();
if (NULL != pEditBox && 0 != pEditBox->getScriptEditBoxHandler())
cocos2d::ui::EditBox *pEditBox= getEditBoxImplIOS()->getEditBox();
if (pEditBox != nullptr && 0 != pEditBox->getScriptEditBoxHandler())
{
cocos2d::CommonScriptData data(pEditBox->getScriptEditBoxHandler(), "ended",pEditBox);
cocos2d::ScriptEvent event(cocos2d::kCommonEvent,(void*)&data);
cocos2d::CommonScriptData data(pEditBox->getScriptEditBoxHandler(), "ended", pEditBox);
cocos2d::ScriptEvent event(cocos2d::kCommonEvent, (void *)&data);
cocos2d::ScriptEngineManager::getInstance()->getScriptEngine()->sendEvent(&event);
memset(data.eventName, 0, sizeof(data.eventName));
strncpy(data.eventName, "return", sizeof(data.eventName));
event.data = (void*)&data;
event.data = (void *)&data;
cocos2d::ScriptEngineManager::getInstance()->getScriptEngine()->sendEvent(&event);
}
#endif

if(editBox_ != nil)
if (_editBox != nil)
{
getEditBoxImplIOS()->onEndEditing();
}

return YES;
}

Expand All @@ -220,15 +253,15 @@ - (BOOL)textFieldShouldEndEditing:(UITextField *)sender
* @param string The replacement string.
* @return YES if the specified text range should be replaced; otherwise, NO to keep the old text.
*/
- (BOOL)textField:(UITextField *) textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if (getEditBoxImplIOS()->getMaxLength() < 0)
{
return YES;
}

NSUInteger oldLength = [textField.text length];
NSUInteger replacementLength = [string length];
NSUInteger oldLength = textField.text.length;
NSUInteger replacementLength = string.length;
NSUInteger rangeLength = range.length;

NSUInteger newLength = oldLength - rangeLength + replacementLength;
Expand All @@ -239,10 +272,10 @@ - (BOOL)textField:(UITextField *) textField shouldChangeCharactersInRange:(NSRan
/**
* Called each time when the text field's text has changed.
*/
- (void) textChanged
- (void)textChanged
{
// NSLog(@"text is %@", self.textField.text);
cocos2d::ui::EditBoxDelegate* pDelegate = getEditBoxImplIOS()->getDelegate();
cocos2d::ui::EditBoxDelegate *pDelegate = getEditBoxImplIOS()->getDelegate();
if (pDelegate != NULL)
{
pDelegate->editBoxTextChanged(getEditBoxImplIOS()->getEditBox(), getEditBoxImplIOS()->getText());
Expand All @@ -253,7 +286,7 @@ - (void) textChanged
if (NULL != pEditBox && 0 != pEditBox->getScriptEditBoxHandler())
{
cocos2d::CommonScriptData data(pEditBox->getScriptEditBoxHandler(), "changed",pEditBox);
cocos2d::ScriptEvent event(cocos2d::kCommonEvent,(void*)&data);
cocos2d::ScriptEvent event(cocos2d::kCommonEvent, (void *)&data);
cocos2d::ScriptEngineManager::getInstance()->getScriptEngine()->sendEvent(&event);
}
#endif
Expand Down

0 comments on commit 6f8bc04

Please sign in to comment.