diff --git a/cocos/ui/UIEditBox/UIEditBoxImpl-ios.h b/cocos/ui/UIEditBox/UIEditBoxImpl-ios.h index 056e25ae12b4..2ab3adbde9cf 100644 --- a/cocos/ui/UIEditBox/UIEditBoxImpl-ios.h +++ b/cocos/ui/UIEditBox/UIEditBoxImpl-ios.h @@ -33,36 +33,7 @@ #include "extensions/ExtensionMacros.h" #include "UIEditBoxImpl.h" -#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; - --(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; - -@end +@class UIEditBoxImplIOS_objc; NS_CC_BEGIN diff --git a/cocos/ui/UIEditBox/UIEditBoxImpl-ios.mm b/cocos/ui/UIEditBox/UIEditBoxImpl-ios.mm index 6ffab47aef17..0e9612aa58dd 100644 --- a/cocos/ui/UIEditBox/UIEditBoxImpl-ios.mm +++ b/cocos/ui/UIEditBox/UIEditBoxImpl-ios.mm @@ -33,126 +33,187 @@ 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_) +#import +#import + +#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(); 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 { + +- (CGRect)editingRectForBounds:(CGRect)bounds +{ return [self textRectForBounds:bounds]; } + @end +#pragma mark - UIEditBox ios implementation -@implementation UIEditBoxImplIOS_objc -@synthesize textField = textField_; -@synthesize editState = editState_; -@synthesize editBox = editBox_; +@interface UIEditBoxImplIOS_objc : NSObject -- (void)dealloc -{ - [textField_ resignFirstResponder]; - [textField_ removeFromSuperview]; - self.textField = NULL; - [super dealloc]; -} +@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 --(id) initWithFrame: (CGRect) frameRect editBox: (void*) editBox +#pragma mark - UIEditBox iOS implementation + +@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 = nil; + + [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]; -} - --(void) visit -{ + _textField.frame = frame; } --(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]; } +/** + * 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..."); - editState_ = YES; + _editState = YES; auto view = cocos2d::Director::getInstance()->getOpenGLView(); CCEAGLView *eaglview = (CCEAGLView *) view->getEAGLView(); @@ -161,31 +222,34 @@ - (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()); @@ -193,23 +257,24 @@ - (BOOL)textFieldShouldEndEditing:(UITextField *)sender } #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) - { - getEditBoxImplIOS()->onEndEditing(); - } + if (_editBox != nil) + { + getEditBoxImplIOS()->onEndEditing(); + } + return YES; } @@ -220,15 +285,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; @@ -236,29 +301,6 @@ - (BOOL)textField:(UITextField *) textField shouldChangeCharactersInRange:(NSRan 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 @@ -310,9 +352,9 @@ - (void) textChanged _systemControl = [[UIEditBoxImplIOS_objc alloc] initWithFrame:rect editBox:this]; if (!_systemControl) break; - initInactiveLabels(size); + initInactiveLabels(size); setContentSize(size); - + return true; }while (0); @@ -321,16 +363,16 @@ - (void) textChanged 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); @@ -368,13 +410,13 @@ - (void) textChanged 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(); @@ -388,24 +430,24 @@ - (void) textChanged 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) @@ -509,18 +551,18 @@ - (void) textChanged 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) @@ -548,7 +590,7 @@ - (void) textChanged void EditBoxImplIOS::setPlaceHolder(const char* pText) { _systemControl.textField.placeholder = [NSString stringWithUTF8String:pText]; - _labelPlaceHolder->setString(pText); + _labelPlaceHolder->setString(pText); } static CGPoint convertDesignCoordToScreenCoord(const Vec2& designCoord) @@ -573,8 +615,8 @@ static CGPoint convertDesignCoordToScreenCoord(const Vec2& designCoord) void EditBoxImplIOS::setPosition(const Vec2& pos) { - _position = pos; - adjustTextFieldPosition(); + _position = pos; + adjustTextFieldPosition(); } void EditBoxImplIOS::setVisible(bool visible) @@ -601,8 +643,8 @@ static CGPoint convertDesignCoordToScreenCoord(const Vec2& designCoord) 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) @@ -629,20 +671,20 @@ static CGPoint convertDesignCoordToScreenCoord(const Vec2& designCoord) 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]; } @@ -653,18 +695,18 @@ static CGPoint convertDesignCoordToScreenCoord(const Vec2& designCoord) 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()); + } } }