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 "extensions/ExtensionMacros.h"
#include "UIEditBoxImpl.h" #include "UIEditBoxImpl.h"
#import <Foundation/Foundation.h> @class UIEditBoxImplIOS_objc;
#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
NS_CC_BEGIN NS_CC_BEGIN

View File

@ -33,126 +33,187 @@
#include "2d/CCLabel.h" #include "2d/CCLabel.h"
#import "platform/ios/CCEAGLView-ios.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; 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 @implementation UICustomUITextField
- (CGRect)textRectForBounds:(CGRect)bounds - (CGRect)textRectForBounds:(CGRect)bounds
{ {
auto glview = cocos2d::Director::getInstance()->getOpenGLView(); auto glview = cocos2d::Director::getInstance()->getOpenGLView();
float padding = CC_EDIT_BOX_PADDING * glview->getScaleX() / glview->getContentScaleFactor(); float padding = CC_EDIT_BOX_PADDING * glview->getScaleX() / glview->getContentScaleFactor();
return CGRectMake(bounds.origin.x + padding, bounds.origin.y + padding, return CGRectInset(bounds, padding, padding);
bounds.size.width - padding*2, bounds.size.height - padding*2);
} }
- (CGRect)editingRectForBounds:(CGRect)bounds {
- (CGRect)editingRectForBounds:(CGRect)bounds
{
return [self textRectForBounds:bounds]; return [self textRectForBounds:bounds];
} }
@end @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 @implementation UIEditBoxImplIOS_objc
@synthesize textField = textField_; #pragma mark - Init & Dealloc
@synthesize editState = editState_;
@synthesize editBox = editBox_;
- (void)dealloc - (instancetype)initWithFrame:(CGRect)frameRect editBox:(void *)editBox
{
[textField_ resignFirstResponder];
[textField_ removeFromSuperview];
self.textField = NULL;
[super dealloc];
}
-(id) initWithFrame: (CGRect) frameRect editBox: (void*) editBox
{ {
self = [super init]; self = [super init];
if (self) if (self)
{ {
editState_ = NO; _editState = NO;
self.textField = [[[UICustomUITextField alloc] initWithFrame: frameRect] autorelease]; UITextField *textField = [[[UICustomUITextField alloc] initWithFrame: frameRect] autorelease];
[textField_ setTextColor:[UIColor whiteColor]]; textField.textColor = [UIColor whiteColor];
//TODO: need to delete hard code here. // TODO: need to delete hard code here.
textField_.font = [UIFont systemFontOfSize:frameRect.size.height*2/3]; textField.font = [UIFont systemFontOfSize:frameRect.size.height*2/3];
textField_.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter; textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
textField_.backgroundColor = [UIColor clearColor]; textField.backgroundColor = [UIColor clearColor];
textField_.borderStyle = UITextBorderStyleNone; textField.borderStyle = UITextBorderStyleNone;
textField_.delegate = self; textField.delegate = self;
textField_.hidden = true; textField.hidden = true;
textField_.returnKeyType = UIReturnKeyDefault; textField.returnKeyType = UIReturnKeyDefault;
[textField_ addTarget:self action:@selector(textChanged) forControlEvents:UIControlEventEditingChanged];
[textField addTarget:self action:@selector(textChanged) forControlEvents:UIControlEventEditingChanged];
self.textField = textField;
self.editBox = editBox; self.editBox = editBox;
} }
return self; 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(); auto view = cocos2d::Director::getInstance()->getOpenGLView();
CCEAGLView *eaglview = (CCEAGLView *) view->getEAGLView(); CCEAGLView *eaglview = (CCEAGLView *)view->getEAGLView();
[eaglview doAnimationWhenKeyboardMoveWithDuration:duration distance:distance]; [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; 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; frame.size = size;
[textField_ setFrame:frame];
_textField.frame = frame;
} }
-(void) visit - (void)openKeyboard
{
}
-(void) openKeyboard
{ {
auto view = cocos2d::Director::getInstance()->getOpenGLView(); auto view = cocos2d::Director::getInstance()->getOpenGLView();
CCEAGLView *eaglview = (CCEAGLView *) view->getEAGLView(); CCEAGLView *eaglview = (CCEAGLView *)view->getEAGLView();
[eaglview addSubview:textField_]; [eaglview addSubview:_textField];
[textField_ becomeFirstResponder]; [_textField becomeFirstResponder];
} }
-(void) closeKeyboard - (void)closeKeyboard
{ {
[textField_ resignFirstResponder]; [_textField resignFirstResponder];
[textField_ removeFromSuperview]; [_textField removeFromSuperview];
} }
- (BOOL)textFieldShouldReturn:(UITextField *)sender - (BOOL)textFieldShouldReturn:(UITextField *)sender
{ {
if (sender == textField_) { if (sender == _textField) {
[sender resignFirstResponder]; [sender resignFirstResponder];
} }
return NO; return NO;
} }
-(void)animationSelector - (void)animationSelector
{ {
auto view = cocos2d::Director::getInstance()->getOpenGLView(); auto view = cocos2d::Director::getInstance()->getOpenGLView();
CCEAGLView *eaglview = (CCEAGLView *) view->getEAGLView(); CCEAGLView *eaglview = (CCEAGLView *)view->getEAGLView();
[eaglview doAnimationWhenAnotherEditBeClicked]; [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. - (BOOL)textFieldShouldBeginEditing:(UITextField *)sender // return NO to disallow editing.
{ {
CCLOG("textFieldShouldBeginEditing..."); CCLOG("textFieldShouldBeginEditing...");
editState_ = YES; _editState = YES;
auto view = cocos2d::Director::getInstance()->getOpenGLView(); auto view = cocos2d::Director::getInstance()->getOpenGLView();
CCEAGLView *eaglview = (CCEAGLView *) view->getEAGLView(); 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]; [self performSelector:@selector(animationSelector) withObject:nil afterDelay:0.0f];
} }
cocos2d::ui::EditBoxDelegate* pDelegate = getEditBoxImplIOS()->getDelegate();
cocos2d::ui::EditBoxDelegate *pDelegate = getEditBoxImplIOS()->getDelegate();
if (pDelegate != NULL) if (pDelegate != NULL)
{ {
pDelegate->editBoxEditingDidBegin(getEditBoxImplIOS()->getEditBox()); pDelegate->editBoxEditingDidBegin(getEditBoxImplIOS()->getEditBox());
} }
#if CC_ENABLE_SCRIPT_BINDING #if CC_ENABLE_SCRIPT_BINDING
cocos2d::ui::EditBox* pEditBox= getEditBoxImplIOS()->getEditBox(); cocos2d::ui::EditBox *pEditBox= getEditBoxImplIOS()->getEditBox();
if (NULL != pEditBox && 0 != pEditBox->getScriptEditBoxHandler()) if (NULL != pEditBox && 0 != pEditBox->getScriptEditBoxHandler())
{ {
cocos2d::CommonScriptData data(pEditBox->getScriptEditBoxHandler(), "began",pEditBox); cocos2d::CommonScriptData data(pEditBox->getScriptEditBoxHandler(), "began", pEditBox);
cocos2d::ScriptEvent event(cocos2d::kCommonEvent,(void*)&data); cocos2d::ScriptEvent event(cocos2d::kCommonEvent, (void *)&data);
cocos2d::ScriptEngineManager::getInstance()->getScriptEngine()->sendEvent(&event); cocos2d::ScriptEngineManager::getInstance()->getScriptEngine()->sendEvent(&event);
} }
#endif #endif
return YES; return YES;
} }
- (BOOL)textFieldShouldEndEditing:(UITextField *)sender - (BOOL)textFieldShouldEndEditing:(UITextField *)sender
{ {
CCLOG("textFieldShouldEndEditing..."); CCLOG("textFieldShouldEndEditing...");
editState_ = NO; _editState = NO;
getEditBoxImplIOS()->refreshInactiveText(); getEditBoxImplIOS()->refreshInactiveText();
cocos2d::ui::EditBoxDelegate* pDelegate = getEditBoxImplIOS()->getDelegate(); cocos2d::ui::EditBoxDelegate *pDelegate = getEditBoxImplIOS()->getDelegate();
if (pDelegate != NULL) if (pDelegate != NULL)
{ {
pDelegate->editBoxEditingDidEnd(getEditBoxImplIOS()->getEditBox()); pDelegate->editBoxEditingDidEnd(getEditBoxImplIOS()->getEditBox());
@ -193,23 +257,24 @@ static const int CC_EDIT_BOX_PADDING = 5;
} }
#if CC_ENABLE_SCRIPT_BINDING #if CC_ENABLE_SCRIPT_BINDING
cocos2d::ui::EditBox* pEditBox= getEditBoxImplIOS()->getEditBox(); cocos2d::ui::EditBox *pEditBox= getEditBoxImplIOS()->getEditBox();
if (NULL != pEditBox && 0 != pEditBox->getScriptEditBoxHandler()) if (pEditBox != nullptr && 0 != pEditBox->getScriptEditBoxHandler())
{ {
cocos2d::CommonScriptData data(pEditBox->getScriptEditBoxHandler(), "ended",pEditBox); cocos2d::CommonScriptData data(pEditBox->getScriptEditBoxHandler(), "ended", pEditBox);
cocos2d::ScriptEvent event(cocos2d::kCommonEvent,(void*)&data); cocos2d::ScriptEvent event(cocos2d::kCommonEvent, (void *)&data);
cocos2d::ScriptEngineManager::getInstance()->getScriptEngine()->sendEvent(&event); cocos2d::ScriptEngineManager::getInstance()->getScriptEngine()->sendEvent(&event);
memset(data.eventName, 0, sizeof(data.eventName)); memset(data.eventName, 0, sizeof(data.eventName));
strncpy(data.eventName, "return", sizeof(data.eventName)); strncpy(data.eventName, "return", sizeof(data.eventName));
event.data = (void*)&data; event.data = (void *)&data;
cocos2d::ScriptEngineManager::getInstance()->getScriptEngine()->sendEvent(&event); cocos2d::ScriptEngineManager::getInstance()->getScriptEngine()->sendEvent(&event);
} }
#endif #endif
if(editBox_ != nil) if (_editBox != nil)
{ {
getEditBoxImplIOS()->onEndEditing(); getEditBoxImplIOS()->onEndEditing();
} }
return YES; return YES;
} }
@ -220,15 +285,15 @@ static const int CC_EDIT_BOX_PADDING = 5;
* @param string The replacement string. * @param string The replacement string.
* @return YES if the specified text range should be replaced; otherwise, NO to keep the old text. * @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) if (getEditBoxImplIOS()->getMaxLength() < 0)
{ {
return YES; return YES;
} }
NSUInteger oldLength = [textField.text length]; NSUInteger oldLength = textField.text.length;
NSUInteger replacementLength = [string length]; NSUInteger replacementLength = string.length;
NSUInteger rangeLength = range.length; NSUInteger rangeLength = range.length;
NSUInteger newLength = oldLength - rangeLength + replacementLength; NSUInteger newLength = oldLength - rangeLength + replacementLength;
@ -236,29 +301,6 @@ static const int CC_EDIT_BOX_PADDING = 5;
return newLength <= getEditBoxImplIOS()->getMaxLength(); 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 @end