From 6f8bc0493430299fb0d76e6b435b778c99b42e43 Mon Sep 17 00:00:00 2001 From: Mazyad Alabduljaleel Date: Tue, 14 Jul 2015 16:18:03 +0300 Subject: [PATCH 1/5] First commit for improving iOS EditBox + Moved internal stuff to the implementation file + Added pragma and organized methods + cleanup synthesize, instance variables, and overall code style for ObjC --- cocos/ui/UIEditBox/UIEditBoxImpl-ios.h | 29 ++--- cocos/ui/UIEditBox/UIEditBoxImpl-ios.mm | 161 ++++++++++++++---------- 2 files changed, 105 insertions(+), 85 deletions(-) diff --git a/cocos/ui/UIEditBox/UIEditBoxImpl-ios.h b/cocos/ui/UIEditBox/UIEditBoxImpl-ios.h index 056e25ae12..ae07d9f55f 100644 --- a/cocos/ui/UIEditBox/UIEditBoxImpl-ios.h +++ b/cocos/ui/UIEditBox/UIEditBoxImpl-ios.h @@ -36,31 +36,18 @@ #import #import -@interface UICustomUITextField : UITextField -{ -} - -@end - @interface UIEditBoxImplIOS_objc : NSObject -{ - UICustomUITextField* textField_; - void* editBox_; - BOOL editState_; -} -@property(nonatomic, retain) UITextField* textField; -@property(nonatomic, readonly, getter = isEditState) BOOL editState; -@property(nonatomic, assign) void* editBox; +@property (nonatomic, readonly, getter = isEditState) BOOL editState; --(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; +- (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 diff --git a/cocos/ui/UIEditBox/UIEditBoxImpl-ios.mm b/cocos/ui/UIEditBox/UIEditBoxImpl-ios.mm index 6ffab47aef..35f41df2d3 100644 --- a/cocos/ui/UIEditBox/UIEditBoxImpl-ios.mm +++ b/cocos/ui/UIEditBox/UIEditBoxImpl-ios.mm @@ -33,11 +33,19 @@ #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(); @@ -46,105 +54,126 @@ static const int CC_EDIT_BOX_PADDING = 5; 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 + +@interface UIEditBoxImplIOS_objc () + +@property (nonatomic, retain) UITextField *textField; +@property (nonatomic, assign) void *editBox; + +@end + +#pragma mark - UIEditBox iOS implementation @implementation UIEditBoxImplIOS_objc -@synthesize textField = textField_; -@synthesize editState = editState_; -@synthesize editBox = editBox_; +#pragma mark - Init & Dealloc -- (void)dealloc -{ - [textField_ resignFirstResponder]; - [textField_ removeFromSuperview]; - self.textField = NULL; - [super dealloc]; -} - --(id) initWithFrame: (CGRect) frameRect editBox: (void*) editBox +- (instancetype)initWithFrame:(CGRect)frameRect editBox:(void *)editBox { self = [super init]; if (self) { - editState_ = NO; - self.textField = [[[UICustomUITextField alloc] initWithFrame: frameRect] autorelease]; + _editState = NO; + UITextField *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]; + 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]; } @@ -152,7 +181,7 @@ static const int CC_EDIT_BOX_PADDING = 5; - (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(); @@ -161,31 +190,34 @@ static const int CC_EDIT_BOX_PADDING = 5; { [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()); @@ -193,23 +225,24 @@ static const int CC_EDIT_BOX_PADDING = 5; } #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; } @@ -220,15 +253,15 @@ static const int CC_EDIT_BOX_PADDING = 5; * @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; @@ -239,10 +272,10 @@ static const int CC_EDIT_BOX_PADDING = 5; /** * 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()); @@ -253,7 +286,7 @@ static const int CC_EDIT_BOX_PADDING = 5; 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 From 008a5ef89c321ef5023a51a85a54202505ed1f62 Mon Sep 17 00:00:00 2001 From: Mazyad Alabduljaleel Date: Tue, 14 Jul 2015 16:34:11 +0300 Subject: [PATCH 2/5] replaced tabs with whitespace --- cocos/ui/UIEditBox/UIEditBoxImpl-ios.mm | 114 ++++++++++++------------ 1 file changed, 57 insertions(+), 57 deletions(-) diff --git a/cocos/ui/UIEditBox/UIEditBoxImpl-ios.mm b/cocos/ui/UIEditBox/UIEditBoxImpl-ios.mm index 35f41df2d3..72c034331d 100644 --- a/cocos/ui/UIEditBox/UIEditBoxImpl-ios.mm +++ b/cocos/ui/UIEditBox/UIEditBoxImpl-ios.mm @@ -89,12 +89,12 @@ static const int CC_EDIT_BOX_PADDING = 5; textField.textColor = [UIColor whiteColor]; // TODO: need to delete hard code here. textField.font = [UIFont systemFontOfSize:frameRect.size.height*2/3]; - textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter; + textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter; textField.backgroundColor = [UIColor clearColor]; textField.borderStyle = UITextBorderStyleNone; textField.delegate = self; textField.hidden = true; - textField.returnKeyType = UIReturnKeyDefault; + textField.returnKeyType = UIReturnKeyDefault; [textField addTarget:self action:@selector(textChanged) forControlEvents:UIControlEventEditingChanged]; @@ -238,10 +238,10 @@ static const int CC_EDIT_BOX_PADDING = 5; } #endif - if (_editBox != nil) - { - getEditBoxImplIOS()->onEndEditing(); - } + if (_editBox != nil) + { + getEditBoxImplIOS()->onEndEditing(); + } return YES; } @@ -343,9 +343,9 @@ bool EditBoxImplIOS::initWithSize(const Size& size) _systemControl = [[UIEditBoxImplIOS_objc alloc] initWithFrame:rect editBox:this]; if (!_systemControl) break; - initInactiveLabels(size); + initInactiveLabels(size); setContentSize(size); - + return true; }while (0); @@ -354,16 +354,16 @@ bool EditBoxImplIOS::initWithSize(const Size& size) void EditBoxImplIOS::initInactiveLabels(const Size& size) { - const char* pDefaultFontName = [[_systemControl.textField.font fontName] UTF8String]; + const char* pDefaultFontName = [[_systemControl.textField.font fontName] UTF8String]; - _label = Label::create(); + _label = Label::create(); _label->setAnchorPoint(Vec2(0, 0.5f)); _label->setColor(Color3B::WHITE); _label->setVisible(false); _editBox->addChild(_label, kLabelZOrder); - + _labelPlaceHolder = Label::create(); - // align the text vertically center + // align the text vertically center _labelPlaceHolder->setAnchorPoint(Vec2(0, 0.5f)); _labelPlaceHolder->setColor(Color3B::GRAY); _editBox->addChild(_labelPlaceHolder, kLabelZOrder); @@ -401,13 +401,13 @@ void EditBoxImplIOS::setInactiveText(const char* pText) void EditBoxImplIOS::setFont(const char* pFontName, int fontSize) { bool isValidFontName = true; - if(pFontName == NULL || strlen(pFontName) == 0) { + if(pFontName == NULL || strlen(pFontName) == 0) { isValidFontName = false; } CCEAGLView *eaglview = static_cast(cocos2d::Director::getInstance()->getOpenGLView()->getEAGLView()); float retinaFactor = eaglview.contentScaleFactor; - NSString * fntName = [NSString stringWithUTF8String:pFontName]; + NSString * fntName = [NSString stringWithUTF8String:pFontName]; auto glview = cocos2d::Director::getInstance()->getOpenGLView(); @@ -421,24 +421,24 @@ void EditBoxImplIOS::setFont(const char* pFontName, int fontSize) textFont = [UIFont systemFontOfSize:fontSize * scaleFactor / retinaFactor]; } - if(textFont != nil) { - [_systemControl.textField setFont:textFont]; + if(textFont != nil) { + [_systemControl.textField setFont:textFont]; } - _label->setSystemFontName(pFontName); - _label->setSystemFontSize(fontSize); + _label->setSystemFontName(pFontName); + _label->setSystemFontSize(fontSize); } void EditBoxImplIOS::setFontColor(const Color4B& color) { _systemControl.textField.textColor = [UIColor colorWithRed:color.r / 255.0f green:color.g / 255.0f blue:color.b / 255.0f alpha:color.a / 255.f]; - _label->setTextColor(color); + _label->setTextColor(color); } void EditBoxImplIOS::setPlaceholderFont(const char* pFontName, int fontSize) { - _labelPlaceHolder->setSystemFontName(pFontName); - _labelPlaceHolder->setSystemFontSize(fontSize); + _labelPlaceHolder->setSystemFontName(pFontName); + _labelPlaceHolder->setSystemFontSize(fontSize); } void EditBoxImplIOS::setPlaceholderFontColor(const Color4B &color) @@ -542,18 +542,18 @@ void EditBoxImplIOS::refreshInactiveText() const char* text = getText(); if(_systemControl.textField.hidden == YES) { - setInactiveText(text); - if(strlen(text) == 0) - { - _label->setVisible(false); - _labelPlaceHolder->setVisible(true); - } - else - { - _label->setVisible(true); - _labelPlaceHolder->setVisible(false); - } - } + setInactiveText(text); + if(strlen(text) == 0) + { + _label->setVisible(false); + _labelPlaceHolder->setVisible(true); + } + else + { + _label->setVisible(true); + _labelPlaceHolder->setVisible(false); + } + } } void EditBoxImplIOS::setText(const char* text) @@ -581,7 +581,7 @@ const char* EditBoxImplIOS::getText(void) void EditBoxImplIOS::setPlaceHolder(const char* pText) { _systemControl.textField.placeholder = [NSString stringWithUTF8String:pText]; - _labelPlaceHolder->setString(pText); + _labelPlaceHolder->setString(pText); } static CGPoint convertDesignCoordToScreenCoord(const Vec2& designCoord) @@ -606,8 +606,8 @@ static CGPoint convertDesignCoordToScreenCoord(const Vec2& designCoord) void EditBoxImplIOS::setPosition(const Vec2& pos) { - _position = pos; - adjustTextFieldPosition(); + _position = pos; + adjustTextFieldPosition(); } void EditBoxImplIOS::setVisible(bool visible) @@ -634,8 +634,8 @@ void EditBoxImplIOS::setContentSize(const Size& size) void EditBoxImplIOS::setAnchorPoint(const Vec2& anchorPoint) { CCLOG("[Edit text] anchor point = (%f, %f)", anchorPoint.x, anchorPoint.y); - _anchorPoint = anchorPoint; - setPosition(_position); + _anchorPoint = anchorPoint; + setPosition(_position); } void EditBoxImplIOS::visit(void) @@ -662,20 +662,20 @@ void EditBoxImplIOS::updatePosition(float dt) void EditBoxImplIOS::adjustTextFieldPosition() { - Size contentSize = _editBox->getContentSize(); - Rect rect = Rect(0, 0, contentSize.width, contentSize.height); + Size contentSize = _editBox->getContentSize(); + Rect rect = Rect(0, 0, contentSize.width, contentSize.height); rect = RectApplyAffineTransform(rect, _editBox->nodeToWorldTransform()); - - Vec2 designCoord = Vec2(rect.origin.x, rect.origin.y + rect.size.height); + + Vec2 designCoord = Vec2(rect.origin.x, rect.origin.y + rect.size.height); [_systemControl setPosition:convertDesignCoordToScreenCoord(designCoord)]; } void EditBoxImplIOS::openKeyboard() { - _label->setVisible(false); - _labelPlaceHolder->setVisible(false); + _label->setVisible(false); + _labelPlaceHolder->setVisible(false); - _systemControl.textField.hidden = NO; + _systemControl.textField.hidden = NO; [_systemControl openKeyboard]; } @@ -686,18 +686,18 @@ void EditBoxImplIOS::closeKeyboard() void EditBoxImplIOS::onEndEditing() { - _systemControl.textField.hidden = YES; - if(strlen(getText()) == 0) - { - _label->setVisible(false); - _labelPlaceHolder->setVisible(true); - } - else - { - _label->setVisible(true); - _labelPlaceHolder->setVisible(false); - setInactiveText(getText()); - } + _systemControl.textField.hidden = YES; + if(strlen(getText()) == 0) + { + _label->setVisible(false); + _labelPlaceHolder->setVisible(true); + } + else + { + _label->setVisible(true); + _labelPlaceHolder->setVisible(false); + setInactiveText(getText()); + } } } From 7861bfd403617d4573f7be9eec2a13ad5e9a85e9 Mon Sep 17 00:00:00 2001 From: Mazyad Alabduljaleel Date: Fri, 17 Jul 2015 16:25:45 +0300 Subject: [PATCH 3/5] Final touch ups: - group textfield delegate methods - remove unused code --- cocos/ui/UIEditBox/UIEditBoxImpl-ios.h | 7 ++-- cocos/ui/UIEditBox/UIEditBoxImpl-ios.mm | 54 ++++++++++++------------- 2 files changed, 29 insertions(+), 32 deletions(-) diff --git a/cocos/ui/UIEditBox/UIEditBoxImpl-ios.h b/cocos/ui/UIEditBox/UIEditBoxImpl-ios.h index ae07d9f55f..b7d6d8d9e9 100644 --- a/cocos/ui/UIEditBox/UIEditBoxImpl-ios.h +++ b/cocos/ui/UIEditBox/UIEditBoxImpl-ios.h @@ -43,9 +43,10 @@ - (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)setPosition:(CGPoint)pos; +- (void)setContentSize:(CGSize)size; + - (void)openKeyboard; - (void)closeKeyboard; diff --git a/cocos/ui/UIEditBox/UIEditBoxImpl-ios.mm b/cocos/ui/UIEditBox/UIEditBoxImpl-ios.mm index 72c034331d..d38236ff24 100644 --- a/cocos/ui/UIEditBox/UIEditBoxImpl-ios.mm +++ b/cocos/ui/UIEditBox/UIEditBoxImpl-ios.mm @@ -110,7 +110,7 @@ static const int CC_EDIT_BOX_PADDING = 5; [_textField resignFirstResponder]; [_textField removeFromSuperview]; - self.textField = NULL; + self.textField = nil; [super dealloc]; } @@ -142,11 +142,6 @@ static const int CC_EDIT_BOX_PADDING = 5; _textField.frame = frame; } -- (void)visit -{ - -} - - (void)openKeyboard { auto view = cocos2d::Director::getInstance()->getOpenGLView(); @@ -178,6 +173,30 @@ static const int CC_EDIT_BOX_PADDING = 5; [eaglview doAnimationWhenAnotherEditBeClicked]; } +/** + * Called each time when the text field's text has changed. + */ +- (void)textChanged +{ + cocos2d::ui::EditBoxDelegate *pDelegate = getEditBoxImplIOS()->getDelegate(); + if (pDelegate != NULL) + { + pDelegate->editBoxTextChanged(getEditBoxImplIOS()->getEditBox(), getEditBoxImplIOS()->getText()); + } + +#if CC_ENABLE_SCRIPT_BINDING + cocos2d::ui::EditBox* pEditBox= getEditBoxImplIOS()->getEditBox(); + if (NULL != pEditBox && 0 != pEditBox->getScriptEditBoxHandler()) + { + cocos2d::CommonScriptData data(pEditBox->getScriptEditBoxHandler(), "changed", pEditBox); + cocos2d::ScriptEvent event(cocos2d::kCommonEvent, (void *)&data); + cocos2d::ScriptEngineManager::getInstance()->getScriptEngine()->sendEvent(&event); + } +#endif +} + +#pragma mark - UITextField delegate methods + - (BOOL)textFieldShouldBeginEditing:(UITextField *)sender // return NO to disallow editing. { CCLOG("textFieldShouldBeginEditing..."); @@ -269,29 +288,6 @@ static const int CC_EDIT_BOX_PADDING = 5; return newLength <= getEditBoxImplIOS()->getMaxLength(); } -/** - * Called each time when the text field's text has changed. - */ -- (void)textChanged -{ - // NSLog(@"text is %@", self.textField.text); - cocos2d::ui::EditBoxDelegate *pDelegate = getEditBoxImplIOS()->getDelegate(); - if (pDelegate != NULL) - { - pDelegate->editBoxTextChanged(getEditBoxImplIOS()->getEditBox(), getEditBoxImplIOS()->getText()); - } - -#if CC_ENABLE_SCRIPT_BINDING - cocos2d::ui::EditBox* pEditBox= getEditBoxImplIOS()->getEditBox(); - if (NULL != pEditBox && 0 != pEditBox->getScriptEditBoxHandler()) - { - cocos2d::CommonScriptData data(pEditBox->getScriptEditBoxHandler(), "changed",pEditBox); - cocos2d::ScriptEvent event(cocos2d::kCommonEvent, (void *)&data); - cocos2d::ScriptEngineManager::getInstance()->getScriptEngine()->sendEvent(&event); - } -#endif -} - @end From 54f52dac801e17b861d48614c5e7924a9ba1c13c Mon Sep 17 00:00:00 2001 From: Mazyad Alabduljaleel Date: Fri, 17 Jul 2015 17:37:15 +0300 Subject: [PATCH 4/5] reduce header exposure this is important, so we know that this class is only used in one place --- cocos/ui/UIEditBox/UIEditBoxImpl-ios.h | 19 +------------------ cocos/ui/UIEditBox/UIEditBoxImpl-ios.mm | 18 ++++++++++++++++-- 2 files changed, 17 insertions(+), 20 deletions(-) diff --git a/cocos/ui/UIEditBox/UIEditBoxImpl-ios.h b/cocos/ui/UIEditBox/UIEditBoxImpl-ios.h index b7d6d8d9e9..2ab3adbde9 100644 --- a/cocos/ui/UIEditBox/UIEditBoxImpl-ios.h +++ b/cocos/ui/UIEditBox/UIEditBoxImpl-ios.h @@ -33,24 +33,7 @@ #include "extensions/ExtensionMacros.h" #include "UIEditBoxImpl.h" -#import -#import - - -@interface UIEditBoxImplIOS_objc : NSObject - -@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)openKeyboard; -- (void)closeKeyboard; - -@end +@class UIEditBoxImplIOS_objc; NS_CC_BEGIN diff --git a/cocos/ui/UIEditBox/UIEditBoxImpl-ios.mm b/cocos/ui/UIEditBox/UIEditBoxImpl-ios.mm index d38236ff24..a0e0c113bb 100644 --- a/cocos/ui/UIEditBox/UIEditBoxImpl-ios.mm +++ b/cocos/ui/UIEditBox/UIEditBoxImpl-ios.mm @@ -33,6 +33,9 @@ #include "2d/CCLabel.h" #import "platform/ios/CCEAGLView-ios.h" +#import +#import + #define getEditBoxImplIOS() ((cocos2d::ui::EditBoxImplIOS *)_editBox) static const int CC_EDIT_BOX_PADDING = 5; @@ -62,12 +65,23 @@ static const int CC_EDIT_BOX_PADDING = 5; @end -#pragma mark - UIEditBox private declerations +#pragma mark - UIEditBox ios implementation -@interface UIEditBoxImplIOS_objc () + +@interface UIEditBoxImplIOS_objc : NSObject @property (nonatomic, retain) UITextField *textField; @property (nonatomic, assign) void *editBox; +@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)openKeyboard; +- (void)closeKeyboard; @end From 2bb26e31d0626c6aee4d84adcbb30e4245abd13c Mon Sep 17 00:00:00 2001 From: Mazyad Alabduljaleel Date: Fri, 17 Jul 2015 17:38:16 +0300 Subject: [PATCH 5/5] simplify CGRect math --- cocos/ui/UIEditBox/UIEditBoxImpl-ios.mm | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/cocos/ui/UIEditBox/UIEditBoxImpl-ios.mm b/cocos/ui/UIEditBox/UIEditBoxImpl-ios.mm index a0e0c113bb..0e9612aa58 100644 --- a/cocos/ui/UIEditBox/UIEditBoxImpl-ios.mm +++ b/cocos/ui/UIEditBox/UIEditBoxImpl-ios.mm @@ -54,8 +54,7 @@ static const int CC_EDIT_BOX_PADDING = 5; auto glview = cocos2d::Director::getInstance()->getOpenGLView(); float padding = CC_EDIT_BOX_PADDING * glview->getScaleX() / glview->getContentScaleFactor(); - return CGRectMake(bounds.origin.x + padding, bounds.origin.y + padding, - bounds.size.width - padding*2, bounds.size.height - padding*2); + return CGRectInset(bounds, padding, padding); } - (CGRect)editingRectForBounds:(CGRect)bounds