mirror of https://github.com/axmolengine/axmol.git
Merge pull request #12848 from Mazyod/v3
Improving iOS EditBox Implementation
This commit is contained in:
commit
b6aa3960d2
|
@ -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
|
||||
|
||||
|
|
|
@ -33,64 +33,103 @@
|
|||
#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]];
|
||||
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];
|
||||
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)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();
|
||||
|
@ -101,21 +140,19 @@ static const int CC_EDIT_BOX_PADDING = 5;
|
|||
|
||||
- (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
|
||||
{
|
||||
CGRect frame = [textField_ frame];
|
||||
CGRect frame = _textField.frame;
|
||||
frame.size = size;
|
||||
[textField_ setFrame:frame];
|
||||
}
|
||||
|
||||
-(void) visit
|
||||
{
|
||||
|
||||
_textField.frame = frame;
|
||||
}
|
||||
|
||||
- (void)openKeyboard
|
||||
|
@ -123,19 +160,19 @@ static const int CC_EDIT_BOX_PADDING = 5;
|
|||
auto view = cocos2d::Director::getInstance()->getOpenGLView();
|
||||
CCEAGLView *eaglview = (CCEAGLView *)view->getEAGLView();
|
||||
|
||||
[eaglview addSubview:textField_];
|
||||
[textField_ becomeFirstResponder];
|
||||
[eaglview addSubview:_textField];
|
||||
[_textField becomeFirstResponder];
|
||||
}
|
||||
|
||||
- (void)closeKeyboard
|
||||
{
|
||||
[textField_ resignFirstResponder];
|
||||
[textField_ removeFromSuperview];
|
||||
[_textField resignFirstResponder];
|
||||
[_textField removeFromSuperview];
|
||||
}
|
||||
|
||||
- (BOOL)textFieldShouldReturn:(UITextField *)sender
|
||||
{
|
||||
if (sender == textField_) {
|
||||
if (sender == _textField) {
|
||||
[sender resignFirstResponder];
|
||||
}
|
||||
return NO;
|
||||
|
@ -149,10 +186,34 @@ 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...");
|
||||
editState_ = YES;
|
||||
_editState = YES;
|
||||
|
||||
auto view = cocos2d::Director::getInstance()->getOpenGLView();
|
||||
CCEAGLView *eaglview = (CCEAGLView *) view->getEAGLView();
|
||||
|
@ -161,7 +222,9 @@ static const int CC_EDIT_BOX_PADDING = 5;
|
|||
{
|
||||
[self performSelector:@selector(animationSelector) withObject:nil afterDelay:0.0f];
|
||||
}
|
||||
|
||||
cocos2d::ui::EditBoxDelegate *pDelegate = getEditBoxImplIOS()->getDelegate();
|
||||
|
||||
if (pDelegate != NULL)
|
||||
{
|
||||
pDelegate->editBoxEditingDidBegin(getEditBoxImplIOS()->getEditBox());
|
||||
|
@ -176,13 +239,14 @@ static const int CC_EDIT_BOX_PADDING = 5;
|
|||
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();
|
||||
|
@ -194,7 +258,7 @@ 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())
|
||||
if (pEditBox != nullptr && 0 != pEditBox->getScriptEditBoxHandler())
|
||||
{
|
||||
cocos2d::CommonScriptData data(pEditBox->getScriptEditBoxHandler(), "ended", pEditBox);
|
||||
cocos2d::ScriptEvent event(cocos2d::kCommonEvent, (void *)&data);
|
||||
|
@ -206,10 +270,11 @@ static const int CC_EDIT_BOX_PADDING = 5;
|
|||
}
|
||||
#endif
|
||||
|
||||
if(editBox_ != nil)
|
||||
if (_editBox != nil)
|
||||
{
|
||||
getEditBoxImplIOS()->onEndEditing();
|
||||
}
|
||||
|
||||
return YES;
|
||||
}
|
||||
|
||||
|
@ -227,8 +292,8 @@ static const int CC_EDIT_BOX_PADDING = 5;
|
|||
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
|
||||
|
||||
|
||||
|
|
Loading…
Reference in New Issue