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 "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
|
||||||
|
|
||||||
|
|
|
@ -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];
|
|
||||||
}
|
|
||||||
|
|
||||||
-(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();
|
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
|
||||||
|
|
||||||
|
|
||||||
|
@ -310,9 +352,9 @@ bool EditBoxImplIOS::initWithSize(const Size& size)
|
||||||
_systemControl = [[UIEditBoxImplIOS_objc alloc] initWithFrame:rect editBox:this];
|
_systemControl = [[UIEditBoxImplIOS_objc alloc] initWithFrame:rect editBox:this];
|
||||||
if (!_systemControl) break;
|
if (!_systemControl) break;
|
||||||
|
|
||||||
initInactiveLabels(size);
|
initInactiveLabels(size);
|
||||||
setContentSize(size);
|
setContentSize(size);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}while (0);
|
}while (0);
|
||||||
|
|
||||||
|
@ -321,16 +363,16 @@ bool EditBoxImplIOS::initWithSize(const Size& size)
|
||||||
|
|
||||||
void EditBoxImplIOS::initInactiveLabels(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->setAnchorPoint(Vec2(0, 0.5f));
|
||||||
_label->setColor(Color3B::WHITE);
|
_label->setColor(Color3B::WHITE);
|
||||||
_label->setVisible(false);
|
_label->setVisible(false);
|
||||||
_editBox->addChild(_label, kLabelZOrder);
|
_editBox->addChild(_label, kLabelZOrder);
|
||||||
|
|
||||||
_labelPlaceHolder = Label::create();
|
_labelPlaceHolder = Label::create();
|
||||||
// align the text vertically center
|
// align the text vertically center
|
||||||
_labelPlaceHolder->setAnchorPoint(Vec2(0, 0.5f));
|
_labelPlaceHolder->setAnchorPoint(Vec2(0, 0.5f));
|
||||||
_labelPlaceHolder->setColor(Color3B::GRAY);
|
_labelPlaceHolder->setColor(Color3B::GRAY);
|
||||||
_editBox->addChild(_labelPlaceHolder, kLabelZOrder);
|
_editBox->addChild(_labelPlaceHolder, kLabelZOrder);
|
||||||
|
@ -368,13 +410,13 @@ void EditBoxImplIOS::setInactiveText(const char* pText)
|
||||||
void EditBoxImplIOS::setFont(const char* pFontName, int fontSize)
|
void EditBoxImplIOS::setFont(const char* pFontName, int fontSize)
|
||||||
{
|
{
|
||||||
bool isValidFontName = true;
|
bool isValidFontName = true;
|
||||||
if(pFontName == NULL || strlen(pFontName) == 0) {
|
if(pFontName == NULL || strlen(pFontName) == 0) {
|
||||||
isValidFontName = false;
|
isValidFontName = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
CCEAGLView *eaglview = static_cast<CCEAGLView *>(cocos2d::Director::getInstance()->getOpenGLView()->getEAGLView());
|
CCEAGLView *eaglview = static_cast<CCEAGLView *>(cocos2d::Director::getInstance()->getOpenGLView()->getEAGLView());
|
||||||
float retinaFactor = eaglview.contentScaleFactor;
|
float retinaFactor = eaglview.contentScaleFactor;
|
||||||
NSString * fntName = [NSString stringWithUTF8String:pFontName];
|
NSString * fntName = [NSString stringWithUTF8String:pFontName];
|
||||||
|
|
||||||
auto glview = cocos2d::Director::getInstance()->getOpenGLView();
|
auto glview = cocos2d::Director::getInstance()->getOpenGLView();
|
||||||
|
|
||||||
|
@ -388,24 +430,24 @@ void EditBoxImplIOS::setFont(const char* pFontName, int fontSize)
|
||||||
textFont = [UIFont systemFontOfSize:fontSize * scaleFactor / retinaFactor];
|
textFont = [UIFont systemFontOfSize:fontSize * scaleFactor / retinaFactor];
|
||||||
}
|
}
|
||||||
|
|
||||||
if(textFont != nil) {
|
if(textFont != nil) {
|
||||||
[_systemControl.textField setFont:textFont];
|
[_systemControl.textField setFont:textFont];
|
||||||
}
|
}
|
||||||
|
|
||||||
_label->setSystemFontName(pFontName);
|
_label->setSystemFontName(pFontName);
|
||||||
_label->setSystemFontSize(fontSize);
|
_label->setSystemFontSize(fontSize);
|
||||||
}
|
}
|
||||||
|
|
||||||
void EditBoxImplIOS::setFontColor(const Color4B& color)
|
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];
|
_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)
|
void EditBoxImplIOS::setPlaceholderFont(const char* pFontName, int fontSize)
|
||||||
{
|
{
|
||||||
_labelPlaceHolder->setSystemFontName(pFontName);
|
_labelPlaceHolder->setSystemFontName(pFontName);
|
||||||
_labelPlaceHolder->setSystemFontSize(fontSize);
|
_labelPlaceHolder->setSystemFontSize(fontSize);
|
||||||
}
|
}
|
||||||
|
|
||||||
void EditBoxImplIOS::setPlaceholderFontColor(const Color4B &color)
|
void EditBoxImplIOS::setPlaceholderFontColor(const Color4B &color)
|
||||||
|
@ -509,18 +551,18 @@ void EditBoxImplIOS::refreshInactiveText()
|
||||||
const char* text = getText();
|
const char* text = getText();
|
||||||
if(_systemControl.textField.hidden == YES)
|
if(_systemControl.textField.hidden == YES)
|
||||||
{
|
{
|
||||||
setInactiveText(text);
|
setInactiveText(text);
|
||||||
if(strlen(text) == 0)
|
if(strlen(text) == 0)
|
||||||
{
|
{
|
||||||
_label->setVisible(false);
|
_label->setVisible(false);
|
||||||
_labelPlaceHolder->setVisible(true);
|
_labelPlaceHolder->setVisible(true);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
_label->setVisible(true);
|
_label->setVisible(true);
|
||||||
_labelPlaceHolder->setVisible(false);
|
_labelPlaceHolder->setVisible(false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void EditBoxImplIOS::setText(const char* text)
|
void EditBoxImplIOS::setText(const char* text)
|
||||||
|
@ -548,7 +590,7 @@ const char* EditBoxImplIOS::getText(void)
|
||||||
void EditBoxImplIOS::setPlaceHolder(const char* pText)
|
void EditBoxImplIOS::setPlaceHolder(const char* pText)
|
||||||
{
|
{
|
||||||
_systemControl.textField.placeholder = [NSString stringWithUTF8String:pText];
|
_systemControl.textField.placeholder = [NSString stringWithUTF8String:pText];
|
||||||
_labelPlaceHolder->setString(pText);
|
_labelPlaceHolder->setString(pText);
|
||||||
}
|
}
|
||||||
|
|
||||||
static CGPoint convertDesignCoordToScreenCoord(const Vec2& designCoord)
|
static CGPoint convertDesignCoordToScreenCoord(const Vec2& designCoord)
|
||||||
|
@ -573,8 +615,8 @@ static CGPoint convertDesignCoordToScreenCoord(const Vec2& designCoord)
|
||||||
|
|
||||||
void EditBoxImplIOS::setPosition(const Vec2& pos)
|
void EditBoxImplIOS::setPosition(const Vec2& pos)
|
||||||
{
|
{
|
||||||
_position = pos;
|
_position = pos;
|
||||||
adjustTextFieldPosition();
|
adjustTextFieldPosition();
|
||||||
}
|
}
|
||||||
|
|
||||||
void EditBoxImplIOS::setVisible(bool visible)
|
void EditBoxImplIOS::setVisible(bool visible)
|
||||||
|
@ -601,8 +643,8 @@ void EditBoxImplIOS::setContentSize(const Size& size)
|
||||||
void EditBoxImplIOS::setAnchorPoint(const Vec2& anchorPoint)
|
void EditBoxImplIOS::setAnchorPoint(const Vec2& anchorPoint)
|
||||||
{
|
{
|
||||||
CCLOG("[Edit text] anchor point = (%f, %f)", anchorPoint.x, anchorPoint.y);
|
CCLOG("[Edit text] anchor point = (%f, %f)", anchorPoint.x, anchorPoint.y);
|
||||||
_anchorPoint = anchorPoint;
|
_anchorPoint = anchorPoint;
|
||||||
setPosition(_position);
|
setPosition(_position);
|
||||||
}
|
}
|
||||||
|
|
||||||
void EditBoxImplIOS::visit(void)
|
void EditBoxImplIOS::visit(void)
|
||||||
|
@ -629,20 +671,20 @@ void EditBoxImplIOS::updatePosition(float dt)
|
||||||
|
|
||||||
void EditBoxImplIOS::adjustTextFieldPosition()
|
void EditBoxImplIOS::adjustTextFieldPosition()
|
||||||
{
|
{
|
||||||
Size contentSize = _editBox->getContentSize();
|
Size contentSize = _editBox->getContentSize();
|
||||||
Rect rect = Rect(0, 0, contentSize.width, contentSize.height);
|
Rect rect = Rect(0, 0, contentSize.width, contentSize.height);
|
||||||
rect = RectApplyAffineTransform(rect, _editBox->nodeToWorldTransform());
|
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)];
|
[_systemControl setPosition:convertDesignCoordToScreenCoord(designCoord)];
|
||||||
}
|
}
|
||||||
|
|
||||||
void EditBoxImplIOS::openKeyboard()
|
void EditBoxImplIOS::openKeyboard()
|
||||||
{
|
{
|
||||||
_label->setVisible(false);
|
_label->setVisible(false);
|
||||||
_labelPlaceHolder->setVisible(false);
|
_labelPlaceHolder->setVisible(false);
|
||||||
|
|
||||||
_systemControl.textField.hidden = NO;
|
_systemControl.textField.hidden = NO;
|
||||||
[_systemControl openKeyboard];
|
[_systemControl openKeyboard];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -653,18 +695,18 @@ void EditBoxImplIOS::closeKeyboard()
|
||||||
|
|
||||||
void EditBoxImplIOS::onEndEditing()
|
void EditBoxImplIOS::onEndEditing()
|
||||||
{
|
{
|
||||||
_systemControl.textField.hidden = YES;
|
_systemControl.textField.hidden = YES;
|
||||||
if(strlen(getText()) == 0)
|
if(strlen(getText()) == 0)
|
||||||
{
|
{
|
||||||
_label->setVisible(false);
|
_label->setVisible(false);
|
||||||
_labelPlaceHolder->setVisible(true);
|
_labelPlaceHolder->setVisible(true);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
_label->setVisible(true);
|
_label->setVisible(true);
|
||||||
_labelPlaceHolder->setVisible(false);
|
_labelPlaceHolder->setVisible(false);
|
||||||
setInactiveText(getText());
|
setInactiveText(getText());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue