Merge pull request #12848 from Mazyod/v3

Improving iOS EditBox Implementation
This commit is contained in:
子龙山人 2015-07-18 23:21:27 +08:00
commit b6aa3960d2
2 changed files with 190 additions and 177 deletions

View File

@ -33,36 +33,7 @@
#include "extensions/ExtensionMacros.h"
#include "UIEditBoxImpl.h"
#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;
@end
@class UIEditBoxImplIOS_objc;
NS_CC_BEGIN

View File

@ -33,126 +33,187 @@
#include "2d/CCLabel.h"
#import "platform/ios/CCEAGLView-ios.h"
#define getEditBoxImplIOS() ((cocos2d::ui::EditBoxImplIOS*)editBox_)
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#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
@interface UIEditBoxImplIOS_objc : NSObject <UITextFieldDelegate>
@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
#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 = 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];
}
-(void) setContentSize:(CGSize) size
{
CGRect frame = [textField_ frame];
frame.size = size;
[textField_ setFrame:frame];
}
-(void) visit
{
_textField.frame = frame;
}
-(void) openKeyboard
- (void)setContentSize:(CGSize)size
{
CGRect frame = _textField.frame;
frame.size = size;
_textField.frame = frame;
}
- (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 @@ 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 +257,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)
{
getEditBoxImplIOS()->onEndEditing();
}
if (_editBox != nil)
{
getEditBoxImplIOS()->onEndEditing();
}
return YES;
}
@ -220,15 +285,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;
@ -236,29 +301,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
@ -310,9 +352,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);
@ -321,16 +363,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);
@ -368,13 +410,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<CCEAGLView *>(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 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)
@ -509,18 +551,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)
@ -548,7 +590,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)
@ -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 @@ 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)
@ -629,20 +671,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];
}
@ -653,18 +695,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());
}
}
}