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
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
#define getEditBoxImplIOS() ((cocos2d::extension::EditBoxImplIOS*)editBox_)
|
2013-01-05 10:56:57 +08:00
|
|
|
|
2013-03-12 17:23:12 +08:00
|
|
|
static const int CC_EDIT_BOX_PADDING = 5;
|
|
|
|
|
2013-06-21 08:46:22 +08:00
|
|
|
@implementation CCCustomUITextField
|
2013-01-05 10:56:57 +08:00
|
|
|
- (CGRect)textRectForBounds:(CGRect)bounds {
|
2013-07-12 13:11:21 +08:00
|
|
|
float padding = CC_EDIT_BOX_PADDING * cocos2d::EGLView::getInstance()->getScaleX() / [[CCEAGLView sharedEGLView] contentScaleFactor ];
|
2013-03-18 23:14:20 +08:00
|
|
|
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
|
|
|
|
|
|
|
|
|
2013-06-21 08:46:22 +08:00
|
|
|
@implementation CCEditBoxImplIOS_objc
|
2013-01-05 10:56:57 +08:00
|
|
|
|
|
|
|
@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;
|
2013-06-21 08:46:22 +08:00
|
|
|
self.textField = [[[CCCustomUITextField alloc] initWithFrame: frameRect] autorelease];
|
2013-01-05 10:56:57 +08:00
|
|
|
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;
|
2013-06-27 04:44:49 +08:00
|
|
|
textField_.hidden = true;
|
|
|
|
textField_.returnKeyType = UIReturnKeyDefault;
|
2013-01-05 10:56:57 +08:00
|
|
|
[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
|
|
|
|
{
|
2013-06-21 08:46:22 +08:00
|
|
|
id eglView = [CCEAGLView sharedEGLView];
|
2013-01-05 10:56:57 +08:00
|
|
|
[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-06-21 08:46:22 +08:00
|
|
|
[[CCEAGLView 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
|
|
|
|
{
|
2013-06-21 08:46:22 +08:00
|
|
|
id eglView = [CCEAGLView sharedEGLView];
|
2013-01-05 10:56:57 +08:00
|
|
|
[eglView doAnimationWhenAnotherEditBeClicked];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL)textFieldShouldBeginEditing:(UITextField *)sender // return NO to disallow editing.
|
|
|
|
{
|
|
|
|
CCLOG("textFieldShouldBeginEditing...");
|
|
|
|
editState_ = YES;
|
2013-06-21 08:46:22 +08:00
|
|
|
id eglView = [CCEAGLView sharedEGLView];
|
2013-01-05 10:56:57 +08:00
|
|
|
if ([eglView isKeyboardShown])
|
|
|
|
{
|
|
|
|
[self performSelector:@selector(animationSelector) withObject:nil afterDelay:0.0f];
|
|
|
|
}
|
2013-06-20 14:15:53 +08:00
|
|
|
cocos2d::extension::EditBoxDelegate* pDelegate = getEditBoxImplIOS()->getDelegate();
|
2013-01-05 10:56:57 +08:00
|
|
|
if (pDelegate != NULL)
|
|
|
|
{
|
2013-06-20 14:15:53 +08:00
|
|
|
pDelegate->editBoxEditingDidBegin(getEditBoxImplIOS()->getEditBox());
|
2013-01-05 10:56:57 +08:00
|
|
|
}
|
2013-05-24 11:42:27 +08:00
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
cocos2d::extension::EditBox* pEditBox= getEditBoxImplIOS()->getEditBox();
|
2013-05-24 11:42:27 +08:00
|
|
|
if (NULL != pEditBox && 0 != pEditBox->getScriptEditBoxHandler())
|
2013-07-02 15:23:51 +08:00
|
|
|
{
|
2013-07-04 15:44:42 +08:00
|
|
|
cocos2d::CommonScriptData data(pEditBox->getScriptEditBoxHandler(), "began",pEditBox);
|
2013-07-02 15:23:51 +08:00
|
|
|
cocos2d::ScriptEvent event(cocos2d::kCommonEvent,(void*)&data);
|
2013-07-22 17:24:54 +08:00
|
|
|
cocos2d::ScriptEngineManager::getInstance()->getScriptEngine()->sendEvent(&event);
|
2013-05-24 11:42:27 +08:00
|
|
|
}
|
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
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
cocos2d::extension::EditBoxDelegate* pDelegate = getEditBoxImplIOS()->getDelegate();
|
2013-01-05 10:56:57 +08:00
|
|
|
if (pDelegate != NULL)
|
|
|
|
{
|
2013-06-20 14:15:53 +08:00
|
|
|
pDelegate->editBoxEditingDidEnd(getEditBoxImplIOS()->getEditBox());
|
|
|
|
pDelegate->editBoxReturn(getEditBoxImplIOS()->getEditBox());
|
2013-01-05 10:56:57 +08:00
|
|
|
}
|
2013-05-24 11:42:27 +08:00
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
cocos2d::extension::EditBox* pEditBox= getEditBoxImplIOS()->getEditBox();
|
2013-05-24 11:42:27 +08:00
|
|
|
if (NULL != pEditBox && 0 != pEditBox->getScriptEditBoxHandler())
|
|
|
|
{
|
2013-07-04 15:44:42 +08:00
|
|
|
cocos2d::CommonScriptData data(pEditBox->getScriptEditBoxHandler(), "ended",pEditBox);
|
2013-07-02 15:23:51 +08:00
|
|
|
cocos2d::ScriptEvent event(cocos2d::kCommonEvent,(void*)&data);
|
2013-07-22 17:24:54 +08:00
|
|
|
cocos2d::ScriptEngineManager::getInstance()->getScriptEngine()->sendEvent(&event);
|
2013-07-02 15:23:51 +08:00
|
|
|
memset(data.eventName,0,64*sizeof(char));
|
|
|
|
strncpy(data.eventName,"return",64);
|
|
|
|
event.data = (void*)&data;
|
2013-07-22 17:24:54 +08:00
|
|
|
cocos2d::ScriptEngineManager::getInstance()->getScriptEngine()->sendEvent(&event);
|
2013-05-24 11:42:27 +08:00
|
|
|
}
|
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);
|
2013-06-20 14:15:53 +08:00
|
|
|
cocos2d::extension::EditBoxDelegate* pDelegate = getEditBoxImplIOS()->getDelegate();
|
2013-01-05 10:56:57 +08:00
|
|
|
if (pDelegate != NULL)
|
|
|
|
{
|
2013-06-20 14:15:53 +08:00
|
|
|
pDelegate->editBoxTextChanged(getEditBoxImplIOS()->getEditBox(), getEditBoxImplIOS()->getText());
|
2013-01-05 10:56:57 +08:00
|
|
|
}
|
2013-05-24 11:42:27 +08:00
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
cocos2d::extension::EditBox* pEditBox= getEditBoxImplIOS()->getEditBox();
|
2013-05-24 11:42:27 +08:00
|
|
|
if (NULL != pEditBox && 0 != pEditBox->getScriptEditBoxHandler())
|
|
|
|
{
|
2013-07-04 15:44:42 +08:00
|
|
|
cocos2d::CommonScriptData data(pEditBox->getScriptEditBoxHandler(), "changed",pEditBox);
|
2013-07-02 15:23:51 +08:00
|
|
|
cocos2d::ScriptEvent event(cocos2d::kCommonEvent,(void*)&data);
|
2013-07-22 17:24:54 +08:00
|
|
|
cocos2d::ScriptEngineManager::getInstance()->getScriptEngine()->sendEvent(&event);
|
2013-05-24 11:42:27 +08:00
|
|
|
}
|
|
|
|
|
2013-01-05 10:56:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
2012-08-15 14:33:56 +08:00
|
|
|
|
|
|
|
NS_CC_EXT_BEGIN
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
EditBoxImpl* __createSystemEditBox(EditBox* pEditBox)
|
2012-08-17 14:29:07 +08:00
|
|
|
{
|
2013-06-20 14:15:53 +08:00
|
|
|
return new EditBoxImplIOS(pEditBox);
|
2012-08-17 14:29:07 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
EditBoxImplIOS::EditBoxImplIOS(EditBox* pEditText)
|
|
|
|
: EditBoxImpl(pEditText)
|
2013-06-15 14:03:30 +08:00
|
|
|
, _label(NULL)
|
|
|
|
, _labelPlaceHolder(NULL)
|
|
|
|
, _systemControl(NULL)
|
2013-07-12 14:11:55 +08:00
|
|
|
, _anchorPoint(Point(0.5f, 0.5f))
|
2013-06-15 14:03:30 +08:00
|
|
|
, _maxTextLength(-1)
|
2012-08-15 14:33:56 +08:00
|
|
|
{
|
2013-06-21 08:46:22 +08:00
|
|
|
_inRetinaMode = [[CCEAGLView sharedEGLView] contentScaleFactor] == 2.0f ? true : false;
|
2012-08-15 14:33:56 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
EditBoxImplIOS::~EditBoxImplIOS()
|
2012-08-15 14:33:56 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
[_systemControl release];
|
2012-08-15 14:33:56 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
void EditBoxImplIOS::doAnimationWhenKeyboardMove(float duration, float distance)
|
2012-08-15 14:33:56 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
if ([_systemControl isEditState] || distance < 0.0f)
|
2012-08-15 14:33:56 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
[_systemControl doAnimationWhenKeyboardMoveWithDuration:duration distance:distance];
|
2012-08-15 14:33:56 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
bool EditBoxImplIOS::initWithSize(const Size& size)
|
2012-08-15 14:33:56 +08:00
|
|
|
{
|
|
|
|
do
|
|
|
|
{
|
2013-07-12 13:11:21 +08:00
|
|
|
EGLViewProtocol* eglView = EGLView::getInstance();
|
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());
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_inRetinaMode)
|
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-06-21 08:46:22 +08:00
|
|
|
_systemControl = [[CCEditBoxImplIOS_objc alloc] initWithFrame:rect editBox:this];
|
2013-06-15 14:03:30 +08:00
|
|
|
if (!_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-06-20 14:15:53 +08:00
|
|
|
void EditBoxImplIOS::initInactiveLabels(const Size& size)
|
2013-03-12 17:23:12 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
const char* pDefaultFontName = [[_systemControl.textField.font fontName] UTF8String];
|
2013-03-12 17:23:12 +08:00
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
_label = LabelTTF::create("", "", 0.0f);
|
2013-07-12 14:11:55 +08:00
|
|
|
_label->setAnchorPoint(Point(0, 0.5f));
|
2013-07-08 18:11:32 +08:00
|
|
|
_label->setColor(Color3B::WHITE);
|
2013-06-15 14:03:30 +08:00
|
|
|
_label->setVisible(false);
|
|
|
|
_editBox->addChild(_label, kLabelZOrder);
|
2013-03-12 17:23:12 +08:00
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
_labelPlaceHolder = LabelTTF::create("", "", 0.0f);
|
2013-03-12 17:23:12 +08:00
|
|
|
// align the text vertically center
|
2013-07-12 14:11:55 +08:00
|
|
|
_labelPlaceHolder->setAnchorPoint(Point(0, 0.5f));
|
2013-07-08 18:11:32 +08:00
|
|
|
_labelPlaceHolder->setColor(Color3B::GRAY);
|
2013-06-15 14:03:30 +08:00
|
|
|
_editBox->addChild(_labelPlaceHolder, 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-06-20 14:15:53 +08:00
|
|
|
void EditBoxImplIOS::placeInactiveLabels() {
|
2013-07-12 14:11:55 +08:00
|
|
|
_label->setPosition(Point(CC_EDIT_BOX_PADDING, _contentSize.height / 2.0f));
|
|
|
|
_labelPlaceHolder->setPosition(Point(CC_EDIT_BOX_PADDING, _contentSize.height / 2.0f));
|
2013-04-02 21:34:33 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
void EditBoxImplIOS::setInactiveText(const char* pText)
|
2013-03-12 17:23:12 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
if(_systemControl.textField.secureTextEntry == YES)
|
2013-03-12 17:23:12 +08:00
|
|
|
{
|
|
|
|
std::string passwordString;
|
|
|
|
for(int i = 0; i < strlen(pText); ++i)
|
|
|
|
passwordString.append("\u25CF");
|
2013-06-15 14:03:30 +08:00
|
|
|
_label->setString(passwordString.c_str());
|
2013-03-12 17:23:12 +08:00
|
|
|
}
|
|
|
|
else
|
2013-06-15 14:03:30 +08:00
|
|
|
_label->setString(getText());
|
2013-03-12 17:23:12 +08:00
|
|
|
|
|
|
|
// Clip the text width to fit to the text box
|
2013-06-15 14:03:30 +08:00
|
|
|
float fMaxWidth = _editBox->getContentSize().width - CC_EDIT_BOX_PADDING * 2;
|
2013-06-20 14:15:53 +08:00
|
|
|
Rect clippingRect = _label->getTextureRect();
|
2013-03-12 17:23:12 +08:00
|
|
|
if(clippingRect.size.width > fMaxWidth) {
|
|
|
|
clippingRect.size.width = fMaxWidth;
|
2013-06-15 14:03:30 +08:00
|
|
|
_label->setTextureRect(clippingRect);
|
2013-03-12 17:23:12 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
void EditBoxImplIOS::setFont(const char* pFontName, int fontSize)
|
2013-02-20 21:41:08 +08:00
|
|
|
{
|
2013-03-18 23:14:20 +08:00
|
|
|
bool isValidFontName = true;
|
|
|
|
if(pFontName == NULL || strlen(pFontName) == 0) {
|
|
|
|
isValidFontName = false;
|
|
|
|
}
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
float retinaFactor = _inRetinaMode ? 2.0f : 1.0f;
|
2013-02-20 21:41:08 +08:00
|
|
|
NSString * fntName = [NSString stringWithUTF8String:pFontName];
|
2013-07-12 13:11:21 +08:00
|
|
|
float scaleFactor = EGLView::getInstance()->getScaleX();
|
2013-03-18 23:14:20 +08:00
|
|
|
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-06-15 14:03:30 +08:00
|
|
|
[_systemControl.textField setFont:textFont];
|
2013-03-18 23:14:20 +08:00
|
|
|
}
|
2013-03-12 17:23:12 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_label->setFontName(pFontName);
|
|
|
|
_label->setFontSize(fontSize);
|
|
|
|
_labelPlaceHolder->setFontName(pFontName);
|
|
|
|
_labelPlaceHolder->setFontSize(fontSize);
|
2013-02-20 21:41:08 +08:00
|
|
|
}
|
|
|
|
|
2013-07-05 16:49:22 +08:00
|
|
|
void EditBoxImplIOS::setFontColor(const Color3B& color)
|
2012-08-15 14:33:56 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_systemControl.textField.textColor = [UIColor colorWithRed:color.r / 255.0f green:color.g / 255.0f blue:color.b / 255.0f alpha:1.0f];
|
|
|
|
_label->setColor(color);
|
2012-08-15 14:33:56 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
void EditBoxImplIOS::setPlaceholderFont(const char* pFontName, int fontSize)
|
2013-02-25 14:05:38 +08:00
|
|
|
{
|
|
|
|
// TODO need to be implemented.
|
|
|
|
}
|
|
|
|
|
2013-07-05 16:49:22 +08:00
|
|
|
void EditBoxImplIOS::setPlaceholderFontColor(const Color3B& color)
|
2012-08-15 14:33:56 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_labelPlaceHolder->setColor(color);
|
2012-08-15 14:33:56 +08:00
|
|
|
}
|
|
|
|
|
2013-07-26 17:45:01 +08:00
|
|
|
void EditBoxImplIOS::setInputMode(EditBox::InputMode inputMode)
|
2012-08-15 14:33:56 +08:00
|
|
|
{
|
|
|
|
switch (inputMode)
|
|
|
|
{
|
2013-07-26 17:45:01 +08:00
|
|
|
case EditBox::InputMode::EMAIL_ADDRESS:
|
2013-06-15 14:03:30 +08:00
|
|
|
_systemControl.textField.keyboardType = UIKeyboardTypeEmailAddress;
|
2012-08-15 14:33:56 +08:00
|
|
|
break;
|
2013-07-26 17:45:01 +08:00
|
|
|
case EditBox::InputMode::NUMERIC:
|
2013-06-15 14:03:30 +08:00
|
|
|
_systemControl.textField.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
|
2012-08-15 14:33:56 +08:00
|
|
|
break;
|
2013-07-26 17:45:01 +08:00
|
|
|
case EditBox::InputMode::PHONE_NUMBER:
|
2013-06-15 14:03:30 +08:00
|
|
|
_systemControl.textField.keyboardType = UIKeyboardTypePhonePad;
|
2012-08-15 14:33:56 +08:00
|
|
|
break;
|
2013-07-26 17:45:01 +08:00
|
|
|
case EditBox::InputMode::URL:
|
2013-06-15 14:03:30 +08:00
|
|
|
_systemControl.textField.keyboardType = UIKeyboardTypeURL;
|
2012-08-15 14:33:56 +08:00
|
|
|
break;
|
2013-07-26 17:45:01 +08:00
|
|
|
case EditBox::InputMode::DECIMAL:
|
2013-06-15 14:03:30 +08:00
|
|
|
_systemControl.textField.keyboardType = UIKeyboardTypeDecimalPad;
|
2012-08-15 14:33:56 +08:00
|
|
|
break;
|
2013-07-26 17:45:01 +08:00
|
|
|
case EditBox::InputMode::SINGLE_LINE:
|
2013-06-15 14:03:30 +08:00
|
|
|
_systemControl.textField.keyboardType = UIKeyboardTypeDefault;
|
2012-08-15 14:33:56 +08:00
|
|
|
break;
|
|
|
|
default:
|
2013-06-15 14:03:30 +08:00
|
|
|
_systemControl.textField.keyboardType = UIKeyboardTypeDefault;
|
2012-08-15 14:33:56 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
void EditBoxImplIOS::setMaxLength(int maxLength)
|
2012-08-15 14:33:56 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_maxTextLength = maxLength;
|
2012-08-15 14:33:56 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
int EditBoxImplIOS::getMaxLength()
|
2012-08-15 14:33:56 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return _maxTextLength;
|
2012-08-15 14:33:56 +08:00
|
|
|
}
|
|
|
|
|
2013-07-26 17:45:01 +08:00
|
|
|
void EditBoxImplIOS::setInputFlag(EditBox::InputFlag inputFlag)
|
2012-08-15 14:33:56 +08:00
|
|
|
{
|
|
|
|
switch (inputFlag)
|
|
|
|
{
|
2013-07-26 17:45:01 +08:00
|
|
|
case EditBox::InputFlag::PASSWORD:
|
2013-06-15 14:03:30 +08:00
|
|
|
_systemControl.textField.secureTextEntry = YES;
|
2012-08-15 14:33:56 +08:00
|
|
|
break;
|
2013-07-26 17:45:01 +08:00
|
|
|
case EditBox::InputFlag::INITIAL_CAPS_WORD:
|
2013-06-15 14:03:30 +08:00
|
|
|
_systemControl.textField.autocapitalizationType = UITextAutocapitalizationTypeWords;
|
2012-08-15 14:33:56 +08:00
|
|
|
break;
|
2013-07-26 17:45:01 +08:00
|
|
|
case EditBox::InputFlag::INITIAL_CAPS_SENTENCE:
|
2013-06-15 14:03:30 +08:00
|
|
|
_systemControl.textField.autocapitalizationType = UITextAutocapitalizationTypeSentences;
|
2012-08-15 14:33:56 +08:00
|
|
|
break;
|
2013-07-26 17:45:01 +08:00
|
|
|
case EditBox::InputFlag::INTIAL_CAPS_ALL_CHARACTERS:
|
2013-06-15 14:03:30 +08:00
|
|
|
_systemControl.textField.autocapitalizationType = UITextAutocapitalizationTypeAllCharacters;
|
2012-08-15 14:33:56 +08:00
|
|
|
break;
|
2013-07-26 17:45:01 +08:00
|
|
|
case EditBox::InputFlag::SENSITIVE:
|
2013-06-15 14:03:30 +08:00
|
|
|
_systemControl.textField.autocorrectionType = UITextAutocorrectionTypeNo;
|
2013-03-20 03:00:35 +08:00
|
|
|
break;
|
2012-08-15 14:33:56 +08:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-26 17:45:01 +08:00
|
|
|
void EditBoxImplIOS::setReturnType(EditBox::KeyboardReturnType returnType)
|
2012-08-15 14:33:56 +08:00
|
|
|
{
|
|
|
|
switch (returnType) {
|
2013-07-26 17:45:01 +08:00
|
|
|
case EditBox::KeyboardReturnType::DEFAULT:
|
2013-06-15 14:03:30 +08:00
|
|
|
_systemControl.textField.returnKeyType = UIReturnKeyDefault;
|
2012-08-15 14:33:56 +08:00
|
|
|
break;
|
2013-07-26 17:45:01 +08:00
|
|
|
case EditBox::KeyboardReturnType::DONE:
|
2013-06-15 14:03:30 +08:00
|
|
|
_systemControl.textField.returnKeyType = UIReturnKeyDone;
|
2012-08-15 14:33:56 +08:00
|
|
|
break;
|
2013-07-26 17:45:01 +08:00
|
|
|
case EditBox::KeyboardReturnType::SEND:
|
2013-06-15 14:03:30 +08:00
|
|
|
_systemControl.textField.returnKeyType = UIReturnKeySend;
|
2012-08-15 14:33:56 +08:00
|
|
|
break;
|
2013-07-26 17:45:01 +08:00
|
|
|
case EditBox::KeyboardReturnType::SEARCH:
|
2013-06-15 14:03:30 +08:00
|
|
|
_systemControl.textField.returnKeyType = UIReturnKeySearch;
|
2012-08-15 14:33:56 +08:00
|
|
|
break;
|
2013-07-26 17:45:01 +08:00
|
|
|
case EditBox::KeyboardReturnType::GO:
|
2013-06-15 14:03:30 +08:00
|
|
|
_systemControl.textField.returnKeyType = UIReturnKeyGo;
|
2012-08-15 14:33:56 +08:00
|
|
|
break;
|
|
|
|
default:
|
2013-06-15 14:03:30 +08:00
|
|
|
_systemControl.textField.returnKeyType = UIReturnKeyDefault;
|
2012-08-15 14:33:56 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
bool EditBoxImplIOS::isEditing()
|
2012-08-17 14:29:07 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return [_systemControl isEditState] ? true : false;
|
2012-08-17 14:29:07 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
void EditBoxImplIOS::setText(const char* pText)
|
2012-08-15 14:33:56 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_systemControl.textField.text = [NSString stringWithUTF8String:pText];
|
|
|
|
if(_systemControl.textField.hidden == YES) {
|
2013-03-12 17:23:12 +08:00
|
|
|
setInactiveText(pText);
|
|
|
|
if(strlen(pText) == 0)
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_label->setVisible(false);
|
|
|
|
_labelPlaceHolder->setVisible(true);
|
2013-03-12 17:23:12 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_label->setVisible(true);
|
|
|
|
_labelPlaceHolder->setVisible(false);
|
2013-03-12 17:23:12 +08:00
|
|
|
}
|
|
|
|
}
|
2012-08-15 14:33:56 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
const char* EditBoxImplIOS::getText(void)
|
2012-08-15 14:33:56 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return [_systemControl.textField.text UTF8String];
|
2012-08-15 14:33:56 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
void EditBoxImplIOS::setPlaceHolder(const char* pText)
|
2012-08-15 14:33:56 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_systemControl.textField.placeholder = [NSString stringWithUTF8String:pText];
|
|
|
|
_labelPlaceHolder->setString(pText);
|
2012-08-15 14:33:56 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
static CGPoint convertDesignCoordToScreenCoord(const Point& designCoord, bool bInRetinaMode)
|
2012-08-15 14:33:56 +08:00
|
|
|
{
|
2013-07-12 13:11:21 +08:00
|
|
|
EGLViewProtocol* eglView = EGLView::getInstance();
|
2013-06-21 08:46:22 +08:00
|
|
|
float viewH = (float)[[CCEAGLView sharedEGLView] getHeight];
|
2012-10-15 13:01:36 +08:00
|
|
|
|
2013-07-12 14:11:55 +08:00
|
|
|
Point visiblePos = Point(designCoord.x * eglView->getScaleX(), designCoord.y * eglView->getScaleY());
|
2013-07-11 16:38:58 +08:00
|
|
|
Point screenGLPos = 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;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
void EditBoxImplIOS::setPosition(const Point& pos)
|
2012-08-15 14:33:56 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_position = pos;
|
2013-03-12 17:23:12 +08:00
|
|
|
adjustTextFieldPosition();
|
2012-08-15 14:33:56 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
void EditBoxImplIOS::setVisible(bool visible)
|
2013-03-03 18:58:51 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
// _systemControl.textField.hidden = !visible;
|
2013-03-03 18:58:51 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
void EditBoxImplIOS::setContentSize(const Size& size)
|
2012-08-15 14:33:56 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_contentSize = 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();
|
2013-07-12 13:11:21 +08:00
|
|
|
EGLViewProtocol* eglView = EGLView::getInstance();
|
2013-04-02 21:34:33 +08:00
|
|
|
CGSize controlSize = CGSizeMake(size.width * eglView->getScaleX(),size.height * eglView->getScaleY());
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_inRetinaMode)
|
2013-04-02 21:34:33 +08:00
|
|
|
{
|
|
|
|
controlSize.width /= 2.0f;
|
|
|
|
controlSize.height /= 2.0f;
|
|
|
|
}
|
2013-06-15 14:03:30 +08:00
|
|
|
[_systemControl setContentSize:controlSize];
|
2012-08-15 14:33:56 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
void EditBoxImplIOS::setAnchorPoint(const Point& anchorPoint)
|
2013-02-20 21:41:08 +08:00
|
|
|
{
|
|
|
|
CCLOG("[Edit text] anchor point = (%f, %f)", anchorPoint.x, anchorPoint.y);
|
2013-06-15 14:03:30 +08:00
|
|
|
_anchorPoint = anchorPoint;
|
|
|
|
setPosition(_position);
|
2013-02-20 21:41:08 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
void EditBoxImplIOS::visit(void)
|
2012-08-15 14:33:56 +08:00
|
|
|
{
|
2013-03-12 17:23:12 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
void EditBoxImplIOS::onEnter(void)
|
2013-03-12 17:23:12 +08:00
|
|
|
{
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
void EditBoxImplIOS::adjustTextFieldPosition()
|
2013-03-12 17:23:12 +08:00
|
|
|
{
|
2013-06-20 14:15:53 +08:00
|
|
|
Size contentSize = _editBox->getContentSize();
|
2013-07-12 14:30:26 +08:00
|
|
|
Rect rect = Rect(0, 0, contentSize.width, contentSize.height);
|
2013-06-20 14:15:53 +08:00
|
|
|
rect = RectApplyAffineTransform(rect, _editBox->nodeToWorldTransform());
|
2013-03-12 17:23:12 +08:00
|
|
|
|
2013-07-12 14:11:55 +08:00
|
|
|
Point designCoord = Point(rect.origin.x, rect.origin.y + rect.size.height);
|
2013-06-15 14:03:30 +08:00
|
|
|
[_systemControl setPosition:convertDesignCoordToScreenCoord(designCoord, _inRetinaMode)];
|
2012-08-15 14:33:56 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
void EditBoxImplIOS::openKeyboard()
|
2012-08-15 14:33:56 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_label->setVisible(false);
|
|
|
|
_labelPlaceHolder->setVisible(false);
|
2013-03-12 17:23:12 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_systemControl.textField.hidden = NO;
|
|
|
|
[_systemControl openKeyboard];
|
2012-08-15 14:33:56 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
void EditBoxImplIOS::closeKeyboard()
|
2012-08-15 14:33:56 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
[_systemControl closeKeyboard];
|
2012-08-15 14:33:56 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
void EditBoxImplIOS::onEndEditing()
|
2013-03-12 17:23:12 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_systemControl.textField.hidden = YES;
|
2013-03-12 17:23:12 +08:00
|
|
|
if(strlen(getText()) == 0)
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_label->setVisible(false);
|
|
|
|
_labelPlaceHolder->setVisible(true);
|
2013-03-12 17:23:12 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_label->setVisible(true);
|
|
|
|
_labelPlaceHolder->setVisible(false);
|
2013-03-12 17:23:12 +08:00
|
|
|
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) */
|
|
|
|
|
|
|
|
|