2012-08-15 14:33:56 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2010-2012 cocos2d-x.org
|
|
|
|
Copyright (c) 2012 James Chen
|
|
|
|
|
|
|
|
http://www.cocos2d-x.org
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
THE SOFTWARE.
|
|
|
|
****************************************************************************/
|
|
|
|
#include "CCEditBoxImplIOS.h"
|
2013-01-05 11:10:24 +08:00
|
|
|
|
|
|
|
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
|
|
|
|
|
2013-03-14 18:14:16 +08:00
|
|
|
#define kLabelZOrder 9999
|
|
|
|
|
2012-08-15 14:33:56 +08:00
|
|
|
#include "CCEditBox.h"
|
2013-01-05 11:10:24 +08:00
|
|
|
#import "EAGLView.h"
|
2013-01-05 10:56:57 +08:00
|
|
|
|
|
|
|
#define getEditBoxImplIOS() ((cocos2d::extension::CCEditBoxImplIOS*)editBox_)
|
|
|
|
|
2013-03-12 17:23:12 +08:00
|
|
|
static const int CC_EDIT_BOX_PADDING = 5;
|
|
|
|
|
2013-01-05 10:56:57 +08:00
|
|
|
@implementation CustomUITextField
|
|
|
|
- (CGRect)textRectForBounds:(CGRect)bounds {
|
2013-03-18 23:14:20 +08:00
|
|
|
float padding = CC_EDIT_BOX_PADDING * cocos2d::CCEGLView::sharedOpenGLView()->getScaleX() / [[EAGLView sharedEGLView] contentScaleFactor ];
|
|
|
|
return CGRectMake(bounds.origin.x + padding, bounds.origin.y + padding,
|
|
|
|
bounds.size.width - padding*2, bounds.size.height - padding*2);
|
2013-01-05 10:56:57 +08:00
|
|
|
}
|
|
|
|
- (CGRect)editingRectForBounds:(CGRect)bounds {
|
|
|
|
return [self textRectForBounds:bounds];
|
|
|
|
}
|
|
|
|
@end
|
|
|
|
|
|
|
|
|
|
|
|
@implementation EditBoxImplIOS
|
|
|
|
|
|
|
|
@synthesize textField = textField_;
|
|
|
|
@synthesize editState = editState_;
|
|
|
|
@synthesize editBox = editBox_;
|
|
|
|
|
|
|
|
- (void)dealloc
|
|
|
|
{
|
|
|
|
[textField_ resignFirstResponder];
|
|
|
|
[textField_ removeFromSuperview];
|
|
|
|
self.textField = NULL;
|
|
|
|
[super dealloc];
|
|
|
|
}
|
|
|
|
|
|
|
|
-(id) initWithFrame: (CGRect) frameRect editBox: (void*) editBox
|
|
|
|
{
|
|
|
|
self = [super init];
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
if (self == nil) break;
|
|
|
|
editState_ = NO;
|
|
|
|
self.textField = [[[CustomUITextField alloc] initWithFrame: frameRect] autorelease];
|
|
|
|
if (!textField_) break;
|
|
|
|
[textField_ setTextColor:[UIColor whiteColor]];
|
|
|
|
textField_.font = [UIFont systemFontOfSize:frameRect.size.height*2/3]; //TODO need to delete hard code here.
|
2013-02-20 21:41:08 +08:00
|
|
|
textField_.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
|
2013-01-05 10:56:57 +08:00
|
|
|
textField_.backgroundColor = [UIColor clearColor];
|
|
|
|
textField_.borderStyle = UITextBorderStyleNone;
|
|
|
|
textField_.delegate = self;
|
|
|
|
textField_.returnKeyType = UIReturnKeyDefault;
|
|
|
|
[textField_ addTarget:self action:@selector(textChanged) forControlEvents:UIControlEventEditingChanged];
|
|
|
|
self.editBox = editBox;
|
|
|
|
|
2013-05-24 18:10:04 +08:00
|
|
|
|
2013-01-05 10:56:57 +08:00
|
|
|
|
|
|
|
return self;
|
|
|
|
}while(0);
|
|
|
|
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
-(void) doAnimationWhenKeyboardMoveWithDuration:(float)duration distance:(float)distance
|
|
|
|
{
|
|
|
|
id eglView = [EAGLView sharedEGLView];
|
|
|
|
[eglView doAnimationWhenKeyboardMoveWithDuration:duration distance:distance];
|
|
|
|
}
|
|
|
|
|
|
|
|
-(void) setPosition:(CGPoint) pos
|
|
|
|
{
|
|
|
|
CGRect frame = [textField_ frame];
|
|
|
|
frame.origin = pos;
|
|
|
|
[textField_ setFrame:frame];
|
|
|
|
}
|
|
|
|
|
|
|
|
-(void) setContentSize:(CGSize) size
|
|
|
|
{
|
2013-04-02 21:34:33 +08:00
|
|
|
CGRect frame = [textField_ frame];
|
|
|
|
frame.size = size;
|
|
|
|
[textField_ setFrame:frame];
|
2013-01-05 10:56:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
-(void) visit
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
-(void) openKeyboard
|
|
|
|
{
|
2013-05-24 18:10:04 +08:00
|
|
|
[[EAGLView sharedEGLView] addSubview:textField_];
|
2013-01-05 10:56:57 +08:00
|
|
|
[textField_ becomeFirstResponder];
|
|
|
|
}
|
|
|
|
|
|
|
|
-(void) closeKeyboard
|
|
|
|
{
|
|
|
|
[textField_ resignFirstResponder];
|
2013-05-24 18:10:04 +08:00
|
|
|
[textField_ removeFromSuperview];
|
2013-01-05 10:56:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL)textFieldShouldReturn:(UITextField *)sender
|
|
|
|
{
|
|
|
|
if (sender == textField_) {
|
|
|
|
[sender resignFirstResponder];
|
|
|
|
}
|
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
|
|
|
|
-(void)animationSelector
|
|
|
|
{
|
|
|
|
id eglView = [EAGLView sharedEGLView];
|
|
|
|
[eglView doAnimationWhenAnotherEditBeClicked];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL)textFieldShouldBeginEditing:(UITextField *)sender // return NO to disallow editing.
|
|
|
|
{
|
|
|
|
CCLOG("textFieldShouldBeginEditing...");
|
|
|
|
editState_ = YES;
|
|
|
|
id eglView = [EAGLView sharedEGLView];
|
|
|
|
if ([eglView isKeyboardShown])
|
|
|
|
{
|
|
|
|
[self performSelector:@selector(animationSelector) withObject:nil afterDelay:0.0f];
|
|
|
|
}
|
|
|
|
cocos2d::extension::CCEditBoxDelegate* pDelegate = getEditBoxImplIOS()->getDelegate();
|
|
|
|
if (pDelegate != NULL)
|
|
|
|
{
|
|
|
|
pDelegate->editBoxEditingDidBegin(getEditBoxImplIOS()->getCCEditBox());
|
|
|
|
}
|
2013-05-24 11:42:27 +08:00
|
|
|
|
|
|
|
cocos2d::extension::CCEditBox* pEditBox= getEditBoxImplIOS()->getCCEditBox();
|
|
|
|
if (NULL != pEditBox && 0 != pEditBox->getScriptEditBoxHandler())
|
|
|
|
{
|
|
|
|
cocos2d::CCScriptEngineProtocol* pEngine = cocos2d::CCScriptEngineManager::sharedManager()->getScriptEngine();
|
|
|
|
pEngine->executeEvent(pEditBox->getScriptEditBoxHandler(), "began",pEditBox);
|
|
|
|
}
|
2013-01-05 10:56:57 +08:00
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL)textFieldShouldEndEditing:(UITextField *)sender
|
|
|
|
{
|
|
|
|
CCLOG("textFieldShouldEndEditing...");
|
|
|
|
editState_ = NO;
|
2013-03-14 18:14:16 +08:00
|
|
|
getEditBoxImplIOS()->setText(getEditBoxImplIOS()->getText());
|
2013-01-05 10:56:57 +08:00
|
|
|
|
|
|
|
cocos2d::extension::CCEditBoxDelegate* pDelegate = getEditBoxImplIOS()->getDelegate();
|
|
|
|
if (pDelegate != NULL)
|
|
|
|
{
|
|
|
|
pDelegate->editBoxEditingDidEnd(getEditBoxImplIOS()->getCCEditBox());
|
|
|
|
pDelegate->editBoxReturn(getEditBoxImplIOS()->getCCEditBox());
|
|
|
|
}
|
2013-05-24 11:42:27 +08:00
|
|
|
|
|
|
|
cocos2d::extension::CCEditBox* pEditBox= getEditBoxImplIOS()->getCCEditBox();
|
|
|
|
if (NULL != pEditBox && 0 != pEditBox->getScriptEditBoxHandler())
|
|
|
|
{
|
|
|
|
cocos2d::CCScriptEngineProtocol* pEngine = cocos2d::CCScriptEngineManager::sharedManager()->getScriptEngine();
|
|
|
|
pEngine->executeEvent(pEditBox->getScriptEditBoxHandler(), "ended",pEditBox);
|
|
|
|
pEngine->executeEvent(pEditBox->getScriptEditBoxHandler(), "return",pEditBox);
|
|
|
|
}
|
2013-03-12 17:23:12 +08:00
|
|
|
|
|
|
|
if(editBox_ != nil)
|
|
|
|
{
|
|
|
|
getEditBoxImplIOS()->onEndEditing();
|
|
|
|
}
|
2013-01-05 10:56:57 +08:00
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delegate method called before the text has been changed.
|
|
|
|
* @param textField The text field containing the text.
|
|
|
|
* @param range The range of characters to be replaced.
|
|
|
|
* @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
|
|
|
|
{
|
|
|
|
if (getEditBoxImplIOS()->getMaxLength() < 0)
|
|
|
|
{
|
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
|
|
|
|
NSUInteger oldLength = [textField.text length];
|
|
|
|
NSUInteger replacementLength = [string length];
|
|
|
|
NSUInteger rangeLength = range.length;
|
|
|
|
|
|
|
|
NSUInteger newLength = oldLength - rangeLength + replacementLength;
|
|
|
|
|
|
|
|
return newLength <= getEditBoxImplIOS()->getMaxLength();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Called each time when the text field's text has changed.
|
|
|
|
*/
|
|
|
|
- (void) textChanged
|
|
|
|
{
|
|
|
|
// NSLog(@"text is %@", self.textField.text);
|
|
|
|
cocos2d::extension::CCEditBoxDelegate* pDelegate = getEditBoxImplIOS()->getDelegate();
|
|
|
|
if (pDelegate != NULL)
|
|
|
|
{
|
|
|
|
pDelegate->editBoxTextChanged(getEditBoxImplIOS()->getCCEditBox(), getEditBoxImplIOS()->getText());
|
|
|
|
}
|
2013-05-24 11:42:27 +08:00
|
|
|
|
|
|
|
cocos2d::extension::CCEditBox* pEditBox= getEditBoxImplIOS()->getCCEditBox();
|
|
|
|
if (NULL != pEditBox && 0 != pEditBox->getScriptEditBoxHandler())
|
|
|
|
{
|
|
|
|
cocos2d::CCScriptEngineProtocol* pEngine = cocos2d::CCScriptEngineManager::sharedManager()->getScriptEngine();
|
|
|
|
pEngine->executeEvent(pEditBox->getScriptEditBoxHandler(), "changed",pEditBox);
|
|
|
|
}
|
|
|
|
|
2013-01-05 10:56:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
2012-08-15 14:33:56 +08:00
|
|
|
|
|
|
|
NS_CC_EXT_BEGIN
|
|
|
|
|
2012-08-17 14:29:07 +08:00
|
|
|
CCEditBoxImpl* __createSystemEditBox(CCEditBox* pEditBox)
|
|
|
|
{
|
|
|
|
return new CCEditBoxImplIOS(pEditBox);
|
|
|
|
}
|
|
|
|
|
2012-08-15 14:33:56 +08:00
|
|
|
CCEditBoxImplIOS::CCEditBoxImplIOS(CCEditBox* pEditText)
|
|
|
|
: CCEditBoxImpl(pEditText)
|
2013-03-12 17:23:12 +08:00
|
|
|
, m_pLabel(NULL)
|
|
|
|
, m_pLabelPlaceHolder(NULL)
|
2013-01-05 10:56:57 +08:00
|
|
|
, m_systemControl(NULL)
|
2013-02-20 21:41:08 +08:00
|
|
|
, m_obAnchorPoint(ccp(0.5f, 0.5f))
|
2012-08-15 14:33:56 +08:00
|
|
|
, m_nMaxTextLength(-1)
|
|
|
|
{
|
2012-10-15 13:01:36 +08:00
|
|
|
m_bInRetinaMode = [[EAGLView sharedEGLView] contentScaleFactor] == 2.0f ? true : false;
|
2012-08-15 14:33:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
CCEditBoxImplIOS::~CCEditBoxImplIOS()
|
|
|
|
{
|
2013-01-05 10:56:57 +08:00
|
|
|
[m_systemControl release];
|
2012-08-15 14:33:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CCEditBoxImplIOS::doAnimationWhenKeyboardMove(float duration, float distance)
|
|
|
|
{
|
2013-01-05 10:56:57 +08:00
|
|
|
if ([m_systemControl isEditState] || distance < 0.0f)
|
2012-08-15 14:33:56 +08:00
|
|
|
{
|
2013-01-05 10:56:57 +08:00
|
|
|
[m_systemControl doAnimationWhenKeyboardMoveWithDuration:duration distance:distance];
|
2012-08-15 14:33:56 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CCEditBoxImplIOS::initWithSize(const CCSize& size)
|
|
|
|
{
|
|
|
|
do
|
|
|
|
{
|
2012-08-21 14:58:31 +08:00
|
|
|
CCEGLViewProtocol* eglView = CCEGLView::sharedOpenGLView();
|
2012-08-15 14:33:56 +08:00
|
|
|
|
2012-10-15 13:01:36 +08:00
|
|
|
CGRect rect = CGRectMake(0, 0, size.width * eglView->getScaleX(),size.height * eglView->getScaleY());
|
|
|
|
|
|
|
|
if (m_bInRetinaMode)
|
2012-08-15 14:33:56 +08:00
|
|
|
{
|
2012-10-15 13:01:36 +08:00
|
|
|
rect.size.width /= 2.0f;
|
|
|
|
rect.size.height /= 2.0f;
|
2012-08-15 14:33:56 +08:00
|
|
|
}
|
2012-10-15 13:01:36 +08:00
|
|
|
|
2013-01-05 10:56:57 +08:00
|
|
|
m_systemControl = [[EditBoxImplIOS alloc] initWithFrame:rect editBox:this];
|
|
|
|
if (!m_systemControl) break;
|
2012-08-15 14:33:56 +08:00
|
|
|
|
2013-03-12 17:23:12 +08:00
|
|
|
initInactiveLabels(size);
|
2013-04-02 21:34:33 +08:00
|
|
|
setContentSize(size);
|
2013-03-12 17:23:12 +08:00
|
|
|
|
2012-08-15 14:33:56 +08:00
|
|
|
return true;
|
|
|
|
}while (0);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-03-12 17:23:12 +08:00
|
|
|
void CCEditBoxImplIOS::initInactiveLabels(const CCSize& size)
|
|
|
|
{
|
|
|
|
const char* pDefaultFontName = [[m_systemControl.textField.font fontName] UTF8String];
|
|
|
|
|
2013-03-18 23:14:20 +08:00
|
|
|
m_pLabel = CCLabelTTF::create("", "", 0.0f);
|
2013-03-12 17:23:12 +08:00
|
|
|
m_pLabel->setAnchorPoint(ccp(0, 0.5f));
|
2013-03-14 14:29:22 +08:00
|
|
|
m_pLabel->setColor(ccWHITE);
|
2013-03-12 17:23:12 +08:00
|
|
|
m_pLabel->setVisible(false);
|
2013-03-14 18:14:16 +08:00
|
|
|
m_pEditBox->addChild(m_pLabel, kLabelZOrder);
|
2013-03-12 17:23:12 +08:00
|
|
|
|
2013-03-18 23:14:20 +08:00
|
|
|
m_pLabelPlaceHolder = CCLabelTTF::create("", "", 0.0f);
|
2013-03-12 17:23:12 +08:00
|
|
|
// align the text vertically center
|
|
|
|
m_pLabelPlaceHolder->setAnchorPoint(ccp(0, 0.5f));
|
|
|
|
m_pLabelPlaceHolder->setColor(ccGRAY);
|
2013-03-14 18:14:16 +08:00
|
|
|
m_pEditBox->addChild(m_pLabelPlaceHolder, kLabelZOrder);
|
2013-03-18 23:14:20 +08:00
|
|
|
|
|
|
|
setFont(pDefaultFontName, size.height*2/3);
|
|
|
|
setPlaceholderFont(pDefaultFontName, size.height*2/3);
|
2013-03-12 17:23:12 +08:00
|
|
|
}
|
|
|
|
|
2013-04-02 21:34:33 +08:00
|
|
|
void CCEditBoxImplIOS::placeInactiveLabels() {
|
|
|
|
m_pLabel->setPosition(ccp(CC_EDIT_BOX_PADDING, m_tContentSize.height / 2.0f));
|
|
|
|
m_pLabelPlaceHolder->setPosition(ccp(CC_EDIT_BOX_PADDING, m_tContentSize.height / 2.0f));
|
|
|
|
}
|
|
|
|
|
2013-03-12 17:23:12 +08:00
|
|
|
void CCEditBoxImplIOS::setInactiveText(const char* pText)
|
|
|
|
{
|
|
|
|
if(m_systemControl.textField.secureTextEntry == YES)
|
|
|
|
{
|
|
|
|
std::string passwordString;
|
|
|
|
for(int i = 0; i < strlen(pText); ++i)
|
|
|
|
passwordString.append("\u25CF");
|
|
|
|
m_pLabel->setString(passwordString.c_str());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
m_pLabel->setString(getText());
|
|
|
|
|
|
|
|
// Clip the text width to fit to the text box
|
|
|
|
float fMaxWidth = m_pEditBox->getContentSize().width - CC_EDIT_BOX_PADDING * 2;
|
|
|
|
CCRect clippingRect = m_pLabel->getTextureRect();
|
|
|
|
if(clippingRect.size.width > fMaxWidth) {
|
|
|
|
clippingRect.size.width = fMaxWidth;
|
|
|
|
m_pLabel->setTextureRect(clippingRect);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-20 21:41:08 +08:00
|
|
|
void CCEditBoxImplIOS::setFont(const char* pFontName, int fontSize)
|
|
|
|
{
|
2013-03-18 23:14:20 +08:00
|
|
|
bool isValidFontName = true;
|
|
|
|
if(pFontName == NULL || strlen(pFontName) == 0) {
|
|
|
|
isValidFontName = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
float retinaFactor = m_bInRetinaMode ? 2.0f : 1.0f;
|
2013-02-20 21:41:08 +08:00
|
|
|
NSString * fntName = [NSString stringWithUTF8String:pFontName];
|
2013-03-18 23:14:20 +08:00
|
|
|
float scaleFactor = CCEGLView::sharedOpenGLView()->getScaleX();
|
|
|
|
UIFont *textFont = nil;
|
|
|
|
if (isValidFontName) {
|
|
|
|
textFont = [UIFont fontWithName:fntName size:fontSize * scaleFactor / retinaFactor];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isValidFontName || textFont == nil){
|
|
|
|
textFont = [UIFont systemFontOfSize:fontSize * scaleFactor / retinaFactor];
|
|
|
|
}
|
|
|
|
|
|
|
|
if(textFont != nil) {
|
2013-02-20 21:41:08 +08:00
|
|
|
[m_systemControl.textField setFont:textFont];
|
2013-03-18 23:14:20 +08:00
|
|
|
}
|
2013-03-12 17:23:12 +08:00
|
|
|
|
|
|
|
m_pLabel->setFontName(pFontName);
|
|
|
|
m_pLabel->setFontSize(fontSize);
|
|
|
|
m_pLabelPlaceHolder->setFontName(pFontName);
|
|
|
|
m_pLabelPlaceHolder->setFontSize(fontSize);
|
2013-02-20 21:41:08 +08:00
|
|
|
}
|
|
|
|
|
2012-08-15 14:33:56 +08:00
|
|
|
void CCEditBoxImplIOS::setFontColor(const ccColor3B& color)
|
|
|
|
{
|
2013-01-05 10:56:57 +08:00
|
|
|
m_systemControl.textField.textColor = [UIColor colorWithRed:color.r / 255.0f green:color.g / 255.0f blue:color.b / 255.0f alpha:1.0f];
|
2013-03-12 17:23:12 +08:00
|
|
|
m_pLabel->setColor(color);
|
2012-08-15 14:33:56 +08:00
|
|
|
}
|
|
|
|
|
2013-02-25 14:05:38 +08:00
|
|
|
void CCEditBoxImplIOS::setPlaceholderFont(const char* pFontName, int fontSize)
|
|
|
|
{
|
|
|
|
// TODO need to be implemented.
|
|
|
|
}
|
|
|
|
|
2012-08-15 14:33:56 +08:00
|
|
|
void CCEditBoxImplIOS::setPlaceholderFontColor(const ccColor3B& color)
|
|
|
|
{
|
2013-03-12 17:23:12 +08:00
|
|
|
m_pLabelPlaceHolder->setColor(color);
|
2012-08-15 14:33:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CCEditBoxImplIOS::setInputMode(EditBoxInputMode inputMode)
|
|
|
|
{
|
|
|
|
switch (inputMode)
|
|
|
|
{
|
|
|
|
case kEditBoxInputModeEmailAddr:
|
2013-01-05 10:56:57 +08:00
|
|
|
m_systemControl.textField.keyboardType = UIKeyboardTypeEmailAddress;
|
2012-08-15 14:33:56 +08:00
|
|
|
break;
|
|
|
|
case kEditBoxInputModeNumeric:
|
2013-01-05 10:56:57 +08:00
|
|
|
m_systemControl.textField.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
|
2012-08-15 14:33:56 +08:00
|
|
|
break;
|
|
|
|
case kEditBoxInputModePhoneNumber:
|
2013-01-05 10:56:57 +08:00
|
|
|
m_systemControl.textField.keyboardType = UIKeyboardTypePhonePad;
|
2012-08-15 14:33:56 +08:00
|
|
|
break;
|
|
|
|
case kEditBoxInputModeUrl:
|
2013-01-05 10:56:57 +08:00
|
|
|
m_systemControl.textField.keyboardType = UIKeyboardTypeURL;
|
2012-08-15 14:33:56 +08:00
|
|
|
break;
|
|
|
|
case kEditBoxInputModeDecimal:
|
2013-01-05 10:56:57 +08:00
|
|
|
m_systemControl.textField.keyboardType = UIKeyboardTypeDecimalPad;
|
2012-08-15 14:33:56 +08:00
|
|
|
break;
|
|
|
|
case kEditBoxInputModeSingleLine:
|
2013-01-05 10:56:57 +08:00
|
|
|
m_systemControl.textField.keyboardType = UIKeyboardTypeDefault;
|
2012-08-15 14:33:56 +08:00
|
|
|
break;
|
|
|
|
default:
|
2013-01-05 10:56:57 +08:00
|
|
|
m_systemControl.textField.keyboardType = UIKeyboardTypeDefault;
|
2012-08-15 14:33:56 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCEditBoxImplIOS::setMaxLength(int maxLength)
|
|
|
|
{
|
|
|
|
m_nMaxTextLength = maxLength;
|
|
|
|
}
|
|
|
|
|
|
|
|
int CCEditBoxImplIOS::getMaxLength()
|
|
|
|
{
|
|
|
|
return m_nMaxTextLength;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCEditBoxImplIOS::setInputFlag(EditBoxInputFlag inputFlag)
|
|
|
|
{
|
|
|
|
switch (inputFlag)
|
|
|
|
{
|
|
|
|
case kEditBoxInputFlagPassword:
|
2013-01-05 10:56:57 +08:00
|
|
|
m_systemControl.textField.secureTextEntry = YES;
|
2012-08-15 14:33:56 +08:00
|
|
|
break;
|
|
|
|
case kEditBoxInputFlagInitialCapsWord:
|
2013-01-05 10:56:57 +08:00
|
|
|
m_systemControl.textField.autocapitalizationType = UITextAutocapitalizationTypeWords;
|
2012-08-15 14:33:56 +08:00
|
|
|
break;
|
|
|
|
case kEditBoxInputFlagInitialCapsSentence:
|
2013-01-05 10:56:57 +08:00
|
|
|
m_systemControl.textField.autocapitalizationType = UITextAutocapitalizationTypeSentences;
|
2012-08-15 14:33:56 +08:00
|
|
|
break;
|
|
|
|
case kEditBoxInputFlagInitialCapsAllCharacters:
|
2013-01-05 10:56:57 +08:00
|
|
|
m_systemControl.textField.autocapitalizationType = UITextAutocapitalizationTypeAllCharacters;
|
2012-08-15 14:33:56 +08:00
|
|
|
break;
|
2013-03-20 03:00:35 +08:00
|
|
|
case kEditBoxInputFlagSensitive:
|
|
|
|
m_systemControl.textField.autocorrectionType = UITextAutocorrectionTypeNo;
|
|
|
|
break;
|
2012-08-15 14:33:56 +08:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCEditBoxImplIOS::setReturnType(KeyboardReturnType returnType)
|
|
|
|
{
|
|
|
|
switch (returnType) {
|
|
|
|
case kKeyboardReturnTypeDefault:
|
2013-01-05 10:56:57 +08:00
|
|
|
m_systemControl.textField.returnKeyType = UIReturnKeyDefault;
|
2012-08-15 14:33:56 +08:00
|
|
|
break;
|
|
|
|
case kKeyboardReturnTypeDone:
|
2013-01-05 10:56:57 +08:00
|
|
|
m_systemControl.textField.returnKeyType = UIReturnKeyDone;
|
2012-08-15 14:33:56 +08:00
|
|
|
break;
|
|
|
|
case kKeyboardReturnTypeSend:
|
2013-01-05 10:56:57 +08:00
|
|
|
m_systemControl.textField.returnKeyType = UIReturnKeySend;
|
2012-08-15 14:33:56 +08:00
|
|
|
break;
|
|
|
|
case kKeyboardReturnTypeSearch:
|
2013-01-05 10:56:57 +08:00
|
|
|
m_systemControl.textField.returnKeyType = UIReturnKeySearch;
|
2012-08-15 14:33:56 +08:00
|
|
|
break;
|
|
|
|
case kKeyboardReturnTypeGo:
|
2013-01-05 10:56:57 +08:00
|
|
|
m_systemControl.textField.returnKeyType = UIReturnKeyGo;
|
2012-08-15 14:33:56 +08:00
|
|
|
break;
|
|
|
|
default:
|
2013-01-05 10:56:57 +08:00
|
|
|
m_systemControl.textField.returnKeyType = UIReturnKeyDefault;
|
2012-08-15 14:33:56 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-17 14:29:07 +08:00
|
|
|
bool CCEditBoxImplIOS::isEditing()
|
|
|
|
{
|
2013-01-05 10:56:57 +08:00
|
|
|
return [m_systemControl isEditState] ? true : false;
|
2012-08-17 14:29:07 +08:00
|
|
|
}
|
|
|
|
|
2012-08-15 14:33:56 +08:00
|
|
|
void CCEditBoxImplIOS::setText(const char* pText)
|
|
|
|
{
|
2013-01-05 10:56:57 +08:00
|
|
|
m_systemControl.textField.text = [NSString stringWithUTF8String:pText];
|
2013-03-12 17:23:12 +08:00
|
|
|
if(m_systemControl.textField.hidden == YES) {
|
|
|
|
setInactiveText(pText);
|
|
|
|
if(strlen(pText) == 0)
|
|
|
|
{
|
|
|
|
m_pLabel->setVisible(false);
|
|
|
|
m_pLabelPlaceHolder->setVisible(true);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_pLabel->setVisible(true);
|
|
|
|
m_pLabelPlaceHolder->setVisible(false);
|
|
|
|
}
|
|
|
|
}
|
2012-08-15 14:33:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const char* CCEditBoxImplIOS::getText(void)
|
|
|
|
{
|
2013-01-05 10:56:57 +08:00
|
|
|
return [m_systemControl.textField.text UTF8String];
|
2012-08-15 14:33:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CCEditBoxImplIOS::setPlaceHolder(const char* pText)
|
|
|
|
{
|
2013-01-05 10:56:57 +08:00
|
|
|
m_systemControl.textField.placeholder = [NSString stringWithUTF8String:pText];
|
2013-03-12 17:23:12 +08:00
|
|
|
m_pLabelPlaceHolder->setString(pText);
|
2012-08-15 14:33:56 +08:00
|
|
|
}
|
|
|
|
|
2012-10-15 13:01:36 +08:00
|
|
|
static CGPoint convertDesignCoordToScreenCoord(const CCPoint& designCoord, bool bInRetinaMode)
|
2012-08-15 14:33:56 +08:00
|
|
|
{
|
2012-08-21 14:58:31 +08:00
|
|
|
CCEGLViewProtocol* eglView = CCEGLView::sharedOpenGLView();
|
2012-10-15 13:01:36 +08:00
|
|
|
float viewH = (float)[[EAGLView sharedEGLView] getHeight];
|
|
|
|
|
|
|
|
CCPoint visiblePos = ccp(designCoord.x * eglView->getScaleX(), designCoord.y * eglView->getScaleY());
|
2012-08-21 14:58:31 +08:00
|
|
|
CCPoint screenGLPos = ccpAdd(visiblePos, eglView->getViewPortRect().origin);
|
2012-10-15 13:01:36 +08:00
|
|
|
|
2012-08-15 14:33:56 +08:00
|
|
|
CGPoint screenPos = CGPointMake(screenGLPos.x, viewH - screenGLPos.y);
|
2012-10-15 13:01:36 +08:00
|
|
|
|
|
|
|
if (bInRetinaMode)
|
|
|
|
{
|
|
|
|
screenPos.x = screenPos.x / 2.0f;
|
|
|
|
screenPos.y = screenPos.y / 2.0f;
|
|
|
|
}
|
|
|
|
CCLOG("[EditBox] pos x = %f, y = %f", screenGLPos.x, screenGLPos.y);
|
2012-08-15 14:33:56 +08:00
|
|
|
return screenPos;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCEditBoxImplIOS::setPosition(const CCPoint& pos)
|
|
|
|
{
|
2013-03-12 17:23:12 +08:00
|
|
|
m_obPosition = pos;
|
|
|
|
adjustTextFieldPosition();
|
2012-08-15 14:33:56 +08:00
|
|
|
}
|
|
|
|
|
2013-03-03 18:58:51 +08:00
|
|
|
void CCEditBoxImplIOS::setVisible(bool visible)
|
|
|
|
{
|
2013-03-12 17:53:43 +08:00
|
|
|
// m_systemControl.textField.hidden = !visible;
|
2013-03-03 18:58:51 +08:00
|
|
|
}
|
|
|
|
|
2012-08-15 14:33:56 +08:00
|
|
|
void CCEditBoxImplIOS::setContentSize(const CCSize& size)
|
|
|
|
{
|
|
|
|
m_tContentSize = size;
|
2012-08-17 14:29:07 +08:00
|
|
|
CCLOG("[Edit text] content size = (%f, %f)", size.width, size.height);
|
2013-04-02 21:34:33 +08:00
|
|
|
placeInactiveLabels();
|
|
|
|
CCEGLViewProtocol* eglView = CCEGLView::sharedOpenGLView();
|
|
|
|
CGSize controlSize = CGSizeMake(size.width * eglView->getScaleX(),size.height * eglView->getScaleY());
|
|
|
|
|
|
|
|
if (m_bInRetinaMode)
|
|
|
|
{
|
|
|
|
controlSize.width /= 2.0f;
|
|
|
|
controlSize.height /= 2.0f;
|
|
|
|
}
|
|
|
|
[m_systemControl setContentSize:controlSize];
|
2012-08-15 14:33:56 +08:00
|
|
|
}
|
|
|
|
|
2013-02-20 21:41:08 +08:00
|
|
|
void CCEditBoxImplIOS::setAnchorPoint(const CCPoint& anchorPoint)
|
|
|
|
{
|
|
|
|
CCLOG("[Edit text] anchor point = (%f, %f)", anchorPoint.x, anchorPoint.y);
|
|
|
|
m_obAnchorPoint = anchorPoint;
|
|
|
|
setPosition(m_obPosition);
|
|
|
|
}
|
|
|
|
|
2012-08-15 14:33:56 +08:00
|
|
|
void CCEditBoxImplIOS::visit(void)
|
|
|
|
{
|
2013-03-12 17:23:12 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCEditBoxImplIOS::onEnter(void)
|
|
|
|
{
|
|
|
|
adjustTextFieldPosition();
|
2013-03-14 18:14:16 +08:00
|
|
|
const char* pText = getText();
|
|
|
|
if (pText) {
|
|
|
|
setInactiveText(pText);
|
|
|
|
}
|
2013-03-12 17:23:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CCEditBoxImplIOS::adjustTextFieldPosition()
|
|
|
|
{
|
|
|
|
CCSize contentSize = m_pEditBox->getContentSize();
|
|
|
|
CCRect rect = CCRectMake(0, 0, contentSize.width, contentSize.height);
|
|
|
|
rect = CCRectApplyAffineTransform(rect, m_pEditBox->nodeToWorldTransform());
|
|
|
|
|
|
|
|
CCPoint designCoord = ccp(rect.origin.x, rect.origin.y + rect.size.height);
|
|
|
|
[m_systemControl setPosition:convertDesignCoordToScreenCoord(designCoord, m_bInRetinaMode)];
|
2012-08-15 14:33:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CCEditBoxImplIOS::openKeyboard()
|
|
|
|
{
|
2013-03-12 17:23:12 +08:00
|
|
|
m_pLabel->setVisible(false);
|
|
|
|
m_pLabelPlaceHolder->setVisible(false);
|
|
|
|
|
|
|
|
m_systemControl.textField.hidden = NO;
|
2013-01-05 10:56:57 +08:00
|
|
|
[m_systemControl openKeyboard];
|
2012-08-15 14:33:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CCEditBoxImplIOS::closeKeyboard()
|
|
|
|
{
|
2013-01-05 10:56:57 +08:00
|
|
|
[m_systemControl closeKeyboard];
|
2012-08-15 14:33:56 +08:00
|
|
|
}
|
|
|
|
|
2013-03-12 17:23:12 +08:00
|
|
|
void CCEditBoxImplIOS::onEndEditing()
|
|
|
|
{
|
|
|
|
m_systemControl.textField.hidden = YES;
|
|
|
|
if(strlen(getText()) == 0)
|
|
|
|
{
|
|
|
|
m_pLabel->setVisible(false);
|
|
|
|
m_pLabelPlaceHolder->setVisible(true);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_pLabel->setVisible(true);
|
|
|
|
m_pLabelPlaceHolder->setVisible(false);
|
|
|
|
setInactiveText(getText());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-15 14:33:56 +08:00
|
|
|
NS_CC_EXT_END
|
|
|
|
|
2013-01-05 11:10:24 +08:00
|
|
|
#endif /* #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS) */
|
|
|
|
|
|
|
|
|