2014-08-19 10:28:24 +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.
|
|
|
|
****************************************************************************/
|
2014-09-10 07:50:02 +08:00
|
|
|
#include "UIEditBoxImpl-ios.h"
|
2014-08-19 10:28:24 +08:00
|
|
|
|
|
|
|
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
|
|
|
|
|
|
|
|
#define kLabelZOrder 9999
|
|
|
|
|
|
|
|
#include "UIEditBox.h"
|
2014-09-24 03:18:24 +08:00
|
|
|
#include "base/CCDirector.h"
|
|
|
|
#include "2d/CCLabel.h"
|
2014-09-10 07:50:02 +08:00
|
|
|
#import "platform/ios/CCEAGLView-ios.h"
|
2014-08-19 10:28:24 +08:00
|
|
|
|
2015-07-14 21:18:03 +08:00
|
|
|
#define getEditBoxImplIOS() ((cocos2d::ui::EditBoxImplIOS *)_editBox)
|
2014-08-19 10:28:24 +08:00
|
|
|
|
|
|
|
static const int CC_EDIT_BOX_PADDING = 5;
|
|
|
|
|
2015-07-14 21:18:03 +08:00
|
|
|
#pragma mark - Internal Classes
|
|
|
|
|
|
|
|
/** TODO: Missing doc - Why is this subclass necessary?
|
|
|
|
*/
|
|
|
|
@interface UICustomUITextField : UITextField
|
|
|
|
@end
|
|
|
|
|
2014-08-19 10:28:24 +08:00
|
|
|
@implementation UICustomUITextField
|
2015-07-14 21:18:03 +08:00
|
|
|
|
2014-08-19 10:28:24 +08:00
|
|
|
- (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);
|
|
|
|
}
|
2015-07-14 21:18:03 +08:00
|
|
|
|
|
|
|
- (CGRect)editingRectForBounds:(CGRect)bounds
|
|
|
|
{
|
2014-08-19 10:28:24 +08:00
|
|
|
return [self textRectForBounds:bounds];
|
|
|
|
}
|
2015-07-14 21:18:03 +08:00
|
|
|
|
2014-08-19 10:28:24 +08:00
|
|
|
@end
|
|
|
|
|
2015-07-14 21:18:03 +08:00
|
|
|
#pragma mark - UIEditBox private declerations
|
2014-08-19 10:28:24 +08:00
|
|
|
|
2015-07-14 21:18:03 +08:00
|
|
|
@interface UIEditBoxImplIOS_objc ()
|
2014-08-19 10:28:24 +08:00
|
|
|
|
2015-07-14 21:18:03 +08:00
|
|
|
@property (nonatomic, retain) UITextField *textField;
|
|
|
|
@property (nonatomic, assign) void *editBox;
|
2014-08-19 10:28:24 +08:00
|
|
|
|
2015-07-14 21:18:03 +08:00
|
|
|
@end
|
2014-08-19 10:28:24 +08:00
|
|
|
|
2015-07-14 21:18:03 +08:00
|
|
|
#pragma mark - UIEditBox iOS implementation
|
|
|
|
|
|
|
|
@implementation UIEditBoxImplIOS_objc
|
|
|
|
|
|
|
|
#pragma mark - Init & Dealloc
|
|
|
|
|
|
|
|
- (instancetype)initWithFrame:(CGRect)frameRect editBox:(void *)editBox
|
2014-08-19 10:28:24 +08:00
|
|
|
{
|
|
|
|
self = [super init];
|
|
|
|
|
|
|
|
if (self)
|
|
|
|
{
|
2015-07-14 21:18:03 +08:00
|
|
|
_editState = NO;
|
|
|
|
UITextField *textField = [[[UICustomUITextField alloc] initWithFrame: frameRect] autorelease];
|
|
|
|
|
|
|
|
textField.textColor = [UIColor whiteColor];
|
|
|
|
// TODO: need to delete hard code here.
|
|
|
|
textField.font = [UIFont systemFontOfSize:frameRect.size.height*2/3];
|
2015-07-14 21:34:11 +08:00
|
|
|
textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
|
2015-07-14 21:18:03 +08:00
|
|
|
textField.backgroundColor = [UIColor clearColor];
|
|
|
|
textField.borderStyle = UITextBorderStyleNone;
|
|
|
|
textField.delegate = self;
|
|
|
|
textField.hidden = true;
|
2015-07-14 21:34:11 +08:00
|
|
|
textField.returnKeyType = UIReturnKeyDefault;
|
2015-07-14 21:18:03 +08:00
|
|
|
|
|
|
|
[textField addTarget:self action:@selector(textChanged) forControlEvents:UIControlEventEditingChanged];
|
|
|
|
|
|
|
|
self.textField = textField;
|
2014-08-19 10:28:24 +08:00
|
|
|
self.editBox = editBox;
|
|
|
|
}
|
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2015-07-14 21:18:03 +08:00
|
|
|
- (void)dealloc
|
|
|
|
{
|
|
|
|
[_textField resignFirstResponder];
|
|
|
|
[_textField removeFromSuperview];
|
|
|
|
|
2015-07-17 21:25:45 +08:00
|
|
|
self.textField = nil;
|
2015-07-14 21:18:03 +08:00
|
|
|
|
|
|
|
[super dealloc];
|
|
|
|
}
|
|
|
|
|
|
|
|
#pragma mark - Public methods
|
|
|
|
|
|
|
|
- (void)doAnimationWhenKeyboardMoveWithDuration:(float)duration distance:(float)distance
|
2014-08-19 10:28:24 +08:00
|
|
|
{
|
|
|
|
auto view = cocos2d::Director::getInstance()->getOpenGLView();
|
2015-07-14 21:18:03 +08:00
|
|
|
CCEAGLView *eaglview = (CCEAGLView *)view->getEAGLView();
|
2014-08-19 10:28:24 +08:00
|
|
|
|
|
|
|
[eaglview doAnimationWhenKeyboardMoveWithDuration:duration distance:distance];
|
|
|
|
}
|
|
|
|
|
2015-07-14 21:18:03 +08:00
|
|
|
- (void)setPosition:(CGPoint)pos
|
2014-08-19 10:28:24 +08:00
|
|
|
{
|
2015-07-14 21:18:03 +08:00
|
|
|
// TODO: Handle anchor point?
|
|
|
|
CGRect frame = _textField.frame;
|
2014-08-19 10:28:24 +08:00
|
|
|
frame.origin = pos;
|
2015-07-14 21:18:03 +08:00
|
|
|
|
|
|
|
_textField.frame = frame;
|
2014-08-19 10:28:24 +08:00
|
|
|
}
|
|
|
|
|
2015-07-14 21:18:03 +08:00
|
|
|
- (void)setContentSize:(CGSize)size
|
2014-08-19 10:28:24 +08:00
|
|
|
{
|
2015-07-14 21:18:03 +08:00
|
|
|
CGRect frame = _textField.frame;
|
2014-08-19 10:28:24 +08:00
|
|
|
frame.size = size;
|
2015-07-14 21:18:03 +08:00
|
|
|
|
|
|
|
_textField.frame = frame;
|
2014-08-19 10:28:24 +08:00
|
|
|
}
|
|
|
|
|
2015-07-14 21:18:03 +08:00
|
|
|
- (void)openKeyboard
|
2014-08-19 10:28:24 +08:00
|
|
|
{
|
|
|
|
auto view = cocos2d::Director::getInstance()->getOpenGLView();
|
2015-07-14 21:18:03 +08:00
|
|
|
CCEAGLView *eaglview = (CCEAGLView *)view->getEAGLView();
|
2014-08-19 10:28:24 +08:00
|
|
|
|
2015-07-14 21:18:03 +08:00
|
|
|
[eaglview addSubview:_textField];
|
|
|
|
[_textField becomeFirstResponder];
|
2014-08-19 10:28:24 +08:00
|
|
|
}
|
|
|
|
|
2015-07-14 21:18:03 +08:00
|
|
|
- (void)closeKeyboard
|
2014-08-19 10:28:24 +08:00
|
|
|
{
|
2015-07-14 21:18:03 +08:00
|
|
|
[_textField resignFirstResponder];
|
|
|
|
[_textField removeFromSuperview];
|
2014-08-19 10:28:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL)textFieldShouldReturn:(UITextField *)sender
|
|
|
|
{
|
2015-07-14 21:18:03 +08:00
|
|
|
if (sender == _textField) {
|
2014-08-19 10:28:24 +08:00
|
|
|
[sender resignFirstResponder];
|
|
|
|
}
|
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
|
2015-07-14 21:18:03 +08:00
|
|
|
- (void)animationSelector
|
2014-08-19 10:28:24 +08:00
|
|
|
{
|
|
|
|
auto view = cocos2d::Director::getInstance()->getOpenGLView();
|
2015-07-14 21:18:03 +08:00
|
|
|
CCEAGLView *eaglview = (CCEAGLView *)view->getEAGLView();
|
2014-08-19 10:28:24 +08:00
|
|
|
|
|
|
|
[eaglview doAnimationWhenAnotherEditBeClicked];
|
|
|
|
}
|
|
|
|
|
2015-07-17 21:25:45 +08:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
|
2014-08-19 10:28:24 +08:00
|
|
|
- (BOOL)textFieldShouldBeginEditing:(UITextField *)sender // return NO to disallow editing.
|
|
|
|
{
|
|
|
|
CCLOG("textFieldShouldBeginEditing...");
|
2015-07-14 21:18:03 +08:00
|
|
|
_editState = YES;
|
2014-08-19 10:28:24 +08:00
|
|
|
|
|
|
|
auto view = cocos2d::Director::getInstance()->getOpenGLView();
|
|
|
|
CCEAGLView *eaglview = (CCEAGLView *) view->getEAGLView();
|
|
|
|
|
|
|
|
if ([eaglview isKeyboardShown])
|
|
|
|
{
|
|
|
|
[self performSelector:@selector(animationSelector) withObject:nil afterDelay:0.0f];
|
|
|
|
}
|
2015-07-14 21:18:03 +08:00
|
|
|
|
|
|
|
cocos2d::ui::EditBoxDelegate *pDelegate = getEditBoxImplIOS()->getDelegate();
|
|
|
|
|
2014-08-19 10:28:24 +08:00
|
|
|
if (pDelegate != NULL)
|
|
|
|
{
|
|
|
|
pDelegate->editBoxEditingDidBegin(getEditBoxImplIOS()->getEditBox());
|
|
|
|
}
|
|
|
|
|
|
|
|
#if CC_ENABLE_SCRIPT_BINDING
|
2015-07-14 21:18:03 +08:00
|
|
|
cocos2d::ui::EditBox *pEditBox= getEditBoxImplIOS()->getEditBox();
|
2014-08-19 10:28:24 +08:00
|
|
|
if (NULL != pEditBox && 0 != pEditBox->getScriptEditBoxHandler())
|
|
|
|
{
|
2015-07-14 21:18:03 +08:00
|
|
|
cocos2d::CommonScriptData data(pEditBox->getScriptEditBoxHandler(), "began", pEditBox);
|
|
|
|
cocos2d::ScriptEvent event(cocos2d::kCommonEvent, (void *)&data);
|
2014-08-19 10:28:24 +08:00
|
|
|
cocos2d::ScriptEngineManager::getInstance()->getScriptEngine()->sendEvent(&event);
|
|
|
|
}
|
|
|
|
#endif
|
2015-07-14 21:18:03 +08:00
|
|
|
|
2014-08-19 10:28:24 +08:00
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL)textFieldShouldEndEditing:(UITextField *)sender
|
|
|
|
{
|
|
|
|
CCLOG("textFieldShouldEndEditing...");
|
2015-07-14 21:18:03 +08:00
|
|
|
_editState = NO;
|
2014-08-19 10:28:24 +08:00
|
|
|
getEditBoxImplIOS()->refreshInactiveText();
|
|
|
|
|
2015-07-14 21:18:03 +08:00
|
|
|
cocos2d::ui::EditBoxDelegate *pDelegate = getEditBoxImplIOS()->getDelegate();
|
2014-08-19 10:28:24 +08:00
|
|
|
if (pDelegate != NULL)
|
|
|
|
{
|
|
|
|
pDelegate->editBoxEditingDidEnd(getEditBoxImplIOS()->getEditBox());
|
|
|
|
pDelegate->editBoxReturn(getEditBoxImplIOS()->getEditBox());
|
|
|
|
}
|
|
|
|
|
|
|
|
#if CC_ENABLE_SCRIPT_BINDING
|
2015-07-14 21:18:03 +08:00
|
|
|
cocos2d::ui::EditBox *pEditBox= getEditBoxImplIOS()->getEditBox();
|
|
|
|
if (pEditBox != nullptr && 0 != pEditBox->getScriptEditBoxHandler())
|
2014-08-19 10:28:24 +08:00
|
|
|
{
|
2015-07-14 21:18:03 +08:00
|
|
|
cocos2d::CommonScriptData data(pEditBox->getScriptEditBoxHandler(), "ended", pEditBox);
|
|
|
|
cocos2d::ScriptEvent event(cocos2d::kCommonEvent, (void *)&data);
|
2014-08-19 10:28:24 +08:00
|
|
|
cocos2d::ScriptEngineManager::getInstance()->getScriptEngine()->sendEvent(&event);
|
|
|
|
memset(data.eventName, 0, sizeof(data.eventName));
|
|
|
|
strncpy(data.eventName, "return", sizeof(data.eventName));
|
2015-07-14 21:18:03 +08:00
|
|
|
event.data = (void *)&data;
|
2014-08-19 10:28:24 +08:00
|
|
|
cocos2d::ScriptEngineManager::getInstance()->getScriptEngine()->sendEvent(&event);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2015-07-14 21:34:11 +08:00
|
|
|
if (_editBox != nil)
|
|
|
|
{
|
|
|
|
getEditBoxImplIOS()->onEndEditing();
|
|
|
|
}
|
2015-07-14 21:18:03 +08:00
|
|
|
|
2014-08-19 10:28:24 +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.
|
|
|
|
*/
|
2015-07-14 21:18:03 +08:00
|
|
|
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
|
2014-08-19 10:28:24 +08:00
|
|
|
{
|
|
|
|
if (getEditBoxImplIOS()->getMaxLength() < 0)
|
|
|
|
{
|
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
|
2015-07-14 21:18:03 +08:00
|
|
|
NSUInteger oldLength = textField.text.length;
|
|
|
|
NSUInteger replacementLength = string.length;
|
2014-08-19 10:28:24 +08:00
|
|
|
NSUInteger rangeLength = range.length;
|
|
|
|
|
|
|
|
NSUInteger newLength = oldLength - rangeLength + replacementLength;
|
|
|
|
|
|
|
|
return newLength <= getEditBoxImplIOS()->getMaxLength();
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
|
|
|
namespace ui {
|
|
|
|
|
|
|
|
EditBoxImpl* __createSystemEditBox(EditBox* pEditBox)
|
|
|
|
{
|
|
|
|
return new EditBoxImplIOS(pEditBox);
|
|
|
|
}
|
|
|
|
|
|
|
|
EditBoxImplIOS::EditBoxImplIOS(EditBox* pEditText)
|
|
|
|
: EditBoxImpl(pEditText)
|
|
|
|
, _label(nullptr)
|
|
|
|
, _labelPlaceHolder(nullptr)
|
|
|
|
, _anchorPoint(Vec2(0.5f, 0.5f))
|
|
|
|
, _systemControl(nullptr)
|
|
|
|
, _maxTextLength(-1)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
EditBoxImplIOS::~EditBoxImplIOS()
|
|
|
|
{
|
|
|
|
[_systemControl release];
|
|
|
|
}
|
|
|
|
|
|
|
|
void EditBoxImplIOS::doAnimationWhenKeyboardMove(float duration, float distance)
|
|
|
|
{
|
|
|
|
if ([_systemControl isEditState] || distance < 0.0f)
|
|
|
|
{
|
|
|
|
[_systemControl doAnimationWhenKeyboardMoveWithDuration:duration distance:distance];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool EditBoxImplIOS::initWithSize(const Size& size)
|
|
|
|
{
|
|
|
|
do
|
|
|
|
{
|
|
|
|
auto glview = cocos2d::Director::getInstance()->getOpenGLView();
|
|
|
|
|
|
|
|
CGRect rect = CGRectMake(0, 0, size.width * glview->getScaleX(),size.height * glview->getScaleY());
|
|
|
|
|
2014-11-07 19:08:31 +08:00
|
|
|
CCEAGLView *eaglview = static_cast<CCEAGLView *>(glview->getEAGLView());
|
|
|
|
float factor = eaglview.contentScaleFactor;
|
|
|
|
rect.size.width /= factor;
|
|
|
|
rect.size.height /= factor;
|
2014-08-19 10:28:24 +08:00
|
|
|
|
|
|
|
_systemControl = [[UIEditBoxImplIOS_objc alloc] initWithFrame:rect editBox:this];
|
|
|
|
if (!_systemControl) break;
|
|
|
|
|
2015-07-14 21:34:11 +08:00
|
|
|
initInactiveLabels(size);
|
2014-08-19 10:28:24 +08:00
|
|
|
setContentSize(size);
|
2015-07-14 21:34:11 +08:00
|
|
|
|
2014-08-19 10:28:24 +08:00
|
|
|
return true;
|
|
|
|
}while (0);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void EditBoxImplIOS::initInactiveLabels(const Size& size)
|
|
|
|
{
|
2015-07-14 21:34:11 +08:00
|
|
|
const char* pDefaultFontName = [[_systemControl.textField.font fontName] UTF8String];
|
2014-08-19 10:28:24 +08:00
|
|
|
|
2015-07-14 21:34:11 +08:00
|
|
|
_label = Label::create();
|
2014-08-19 10:28:24 +08:00
|
|
|
_label->setAnchorPoint(Vec2(0, 0.5f));
|
|
|
|
_label->setColor(Color3B::WHITE);
|
|
|
|
_label->setVisible(false);
|
|
|
|
_editBox->addChild(_label, kLabelZOrder);
|
2015-07-14 21:34:11 +08:00
|
|
|
|
2014-08-19 10:28:24 +08:00
|
|
|
_labelPlaceHolder = Label::create();
|
2015-07-14 21:34:11 +08:00
|
|
|
// align the text vertically center
|
2014-08-19 10:28:24 +08:00
|
|
|
_labelPlaceHolder->setAnchorPoint(Vec2(0, 0.5f));
|
|
|
|
_labelPlaceHolder->setColor(Color3B::GRAY);
|
|
|
|
_editBox->addChild(_labelPlaceHolder, kLabelZOrder);
|
|
|
|
|
|
|
|
setFont(pDefaultFontName, size.height*2/3);
|
|
|
|
setPlaceholderFont(pDefaultFontName, size.height*2/3);
|
|
|
|
}
|
|
|
|
|
|
|
|
void EditBoxImplIOS::placeInactiveLabels()
|
|
|
|
{
|
2014-08-28 11:41:18 +08:00
|
|
|
_label->setPosition(CC_EDIT_BOX_PADDING, _contentSize.height / 2.0f);
|
|
|
|
_labelPlaceHolder->setPosition(CC_EDIT_BOX_PADDING, _contentSize.height / 2.0f);
|
2014-08-19 10:28:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void EditBoxImplIOS::setInactiveText(const char* pText)
|
|
|
|
{
|
|
|
|
if(_systemControl.textField.secureTextEntry == YES)
|
|
|
|
{
|
|
|
|
std::string passwordString;
|
|
|
|
for(int i = 0; i < strlen(pText); ++i)
|
|
|
|
passwordString.append("\u25CF");
|
|
|
|
_label->setString(passwordString.c_str());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
_label->setString(getText());
|
|
|
|
|
|
|
|
// Clip the text width to fit to the text box
|
|
|
|
float fMaxWidth = _editBox->getContentSize().width - CC_EDIT_BOX_PADDING * 2;
|
|
|
|
Size labelSize = _label->getContentSize();
|
|
|
|
if(labelSize.width > fMaxWidth) {
|
|
|
|
_label->setDimensions(fMaxWidth,labelSize.height);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void EditBoxImplIOS::setFont(const char* pFontName, int fontSize)
|
|
|
|
{
|
|
|
|
bool isValidFontName = true;
|
2015-07-14 21:34:11 +08:00
|
|
|
if(pFontName == NULL || strlen(pFontName) == 0) {
|
2014-08-19 10:28:24 +08:00
|
|
|
isValidFontName = false;
|
|
|
|
}
|
|
|
|
|
2014-11-07 19:08:31 +08:00
|
|
|
CCEAGLView *eaglview = static_cast<CCEAGLView *>(cocos2d::Director::getInstance()->getOpenGLView()->getEAGLView());
|
|
|
|
float retinaFactor = eaglview.contentScaleFactor;
|
2015-07-14 21:34:11 +08:00
|
|
|
NSString * fntName = [NSString stringWithUTF8String:pFontName];
|
2014-08-19 10:28:24 +08:00
|
|
|
|
|
|
|
auto glview = cocos2d::Director::getInstance()->getOpenGLView();
|
|
|
|
|
|
|
|
float scaleFactor = glview->getScaleX();
|
|
|
|
UIFont *textFont = nil;
|
|
|
|
if (isValidFontName) {
|
|
|
|
textFont = [UIFont fontWithName:fntName size:fontSize * scaleFactor / retinaFactor];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isValidFontName || textFont == nil){
|
|
|
|
textFont = [UIFont systemFontOfSize:fontSize * scaleFactor / retinaFactor];
|
|
|
|
}
|
|
|
|
|
2015-07-14 21:34:11 +08:00
|
|
|
if(textFont != nil) {
|
|
|
|
[_systemControl.textField setFont:textFont];
|
2014-08-19 10:28:24 +08:00
|
|
|
}
|
|
|
|
|
2015-07-14 21:34:11 +08:00
|
|
|
_label->setSystemFontName(pFontName);
|
|
|
|
_label->setSystemFontSize(fontSize);
|
2014-08-19 10:28:24 +08:00
|
|
|
}
|
|
|
|
|
2015-03-02 19:31:59 +08:00
|
|
|
void EditBoxImplIOS::setFontColor(const Color4B& color)
|
2014-08-19 10:28:24 +08:00
|
|
|
{
|
2015-03-02 19:31:59 +08:00
|
|
|
_systemControl.textField.textColor = [UIColor colorWithRed:color.r / 255.0f green:color.g / 255.0f blue:color.b / 255.0f alpha:color.a / 255.f];
|
2015-07-14 21:34:11 +08:00
|
|
|
_label->setTextColor(color);
|
2014-08-19 10:28:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void EditBoxImplIOS::setPlaceholderFont(const char* pFontName, int fontSize)
|
|
|
|
{
|
2015-07-14 21:34:11 +08:00
|
|
|
_labelPlaceHolder->setSystemFontName(pFontName);
|
|
|
|
_labelPlaceHolder->setSystemFontSize(fontSize);
|
2014-08-19 10:28:24 +08:00
|
|
|
}
|
2015-03-02 19:31:59 +08:00
|
|
|
|
|
|
|
void EditBoxImplIOS::setPlaceholderFontColor(const Color4B &color)
|
2014-08-19 10:28:24 +08:00
|
|
|
{
|
2015-03-02 19:31:59 +08:00
|
|
|
_labelPlaceHolder->setTextColor(color);
|
2014-08-19 10:28:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void EditBoxImplIOS::setInputMode(EditBox::InputMode inputMode)
|
|
|
|
{
|
|
|
|
switch (inputMode)
|
|
|
|
{
|
|
|
|
case EditBox::InputMode::EMAIL_ADDRESS:
|
|
|
|
_systemControl.textField.keyboardType = UIKeyboardTypeEmailAddress;
|
|
|
|
break;
|
|
|
|
case EditBox::InputMode::NUMERIC:
|
|
|
|
_systemControl.textField.keyboardType = UIKeyboardTypeDecimalPad;
|
|
|
|
break;
|
|
|
|
case EditBox::InputMode::PHONE_NUMBER:
|
|
|
|
_systemControl.textField.keyboardType = UIKeyboardTypePhonePad;
|
|
|
|
break;
|
|
|
|
case EditBox::InputMode::URL:
|
|
|
|
_systemControl.textField.keyboardType = UIKeyboardTypeURL;
|
|
|
|
break;
|
|
|
|
case EditBox::InputMode::DECIMAL:
|
|
|
|
_systemControl.textField.keyboardType = UIKeyboardTypeDecimalPad;
|
|
|
|
break;
|
|
|
|
case EditBox::InputMode::SINGLE_LINE:
|
|
|
|
_systemControl.textField.keyboardType = UIKeyboardTypeDefault;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
_systemControl.textField.keyboardType = UIKeyboardTypeDefault;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void EditBoxImplIOS::setMaxLength(int maxLength)
|
|
|
|
{
|
|
|
|
_maxTextLength = maxLength;
|
|
|
|
}
|
|
|
|
|
|
|
|
int EditBoxImplIOS::getMaxLength()
|
|
|
|
{
|
|
|
|
return _maxTextLength;
|
|
|
|
}
|
|
|
|
|
|
|
|
void EditBoxImplIOS::setInputFlag(EditBox::InputFlag inputFlag)
|
|
|
|
{
|
|
|
|
switch (inputFlag)
|
|
|
|
{
|
|
|
|
case EditBox::InputFlag::PASSWORD:
|
|
|
|
_systemControl.textField.secureTextEntry = YES;
|
|
|
|
break;
|
|
|
|
case EditBox::InputFlag::INITIAL_CAPS_WORD:
|
|
|
|
_systemControl.textField.autocapitalizationType = UITextAutocapitalizationTypeWords;
|
|
|
|
break;
|
|
|
|
case EditBox::InputFlag::INITIAL_CAPS_SENTENCE:
|
|
|
|
_systemControl.textField.autocapitalizationType = UITextAutocapitalizationTypeSentences;
|
|
|
|
break;
|
|
|
|
case EditBox::InputFlag::INTIAL_CAPS_ALL_CHARACTERS:
|
|
|
|
_systemControl.textField.autocapitalizationType = UITextAutocapitalizationTypeAllCharacters;
|
|
|
|
break;
|
|
|
|
case EditBox::InputFlag::SENSITIVE:
|
|
|
|
_systemControl.textField.autocorrectionType = UITextAutocorrectionTypeNo;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void EditBoxImplIOS::setReturnType(EditBox::KeyboardReturnType returnType)
|
|
|
|
{
|
|
|
|
switch (returnType) {
|
|
|
|
case EditBox::KeyboardReturnType::DEFAULT:
|
|
|
|
_systemControl.textField.returnKeyType = UIReturnKeyDefault;
|
|
|
|
break;
|
|
|
|
case EditBox::KeyboardReturnType::DONE:
|
|
|
|
_systemControl.textField.returnKeyType = UIReturnKeyDone;
|
|
|
|
break;
|
|
|
|
case EditBox::KeyboardReturnType::SEND:
|
|
|
|
_systemControl.textField.returnKeyType = UIReturnKeySend;
|
|
|
|
break;
|
|
|
|
case EditBox::KeyboardReturnType::SEARCH:
|
|
|
|
_systemControl.textField.returnKeyType = UIReturnKeySearch;
|
|
|
|
break;
|
|
|
|
case EditBox::KeyboardReturnType::GO:
|
|
|
|
_systemControl.textField.returnKeyType = UIReturnKeyGo;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
_systemControl.textField.returnKeyType = UIReturnKeyDefault;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool EditBoxImplIOS::isEditing()
|
|
|
|
{
|
|
|
|
return [_systemControl isEditState] ? true : false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void EditBoxImplIOS::refreshInactiveText()
|
|
|
|
{
|
|
|
|
const char* text = getText();
|
|
|
|
if(_systemControl.textField.hidden == YES)
|
|
|
|
{
|
2015-07-14 21:34:11 +08:00
|
|
|
setInactiveText(text);
|
|
|
|
if(strlen(text) == 0)
|
|
|
|
{
|
|
|
|
_label->setVisible(false);
|
|
|
|
_labelPlaceHolder->setVisible(true);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_label->setVisible(true);
|
|
|
|
_labelPlaceHolder->setVisible(false);
|
|
|
|
}
|
|
|
|
}
|
2014-08-19 10:28:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void EditBoxImplIOS::setText(const char* text)
|
|
|
|
{
|
|
|
|
NSString* nsText =[NSString stringWithUTF8String:text];
|
|
|
|
if ([nsText compare:_systemControl.textField.text] != NSOrderedSame)
|
|
|
|
{
|
|
|
|
_systemControl.textField.text = nsText;
|
|
|
|
}
|
|
|
|
|
|
|
|
refreshInactiveText();
|
|
|
|
}
|
|
|
|
|
|
|
|
NSString* removeSiriString(NSString* str)
|
|
|
|
{
|
|
|
|
NSString* siriString = @"\xef\xbf\xbc";
|
|
|
|
return [str stringByReplacingOccurrencesOfString:siriString withString:@""];
|
|
|
|
}
|
|
|
|
|
|
|
|
const char* EditBoxImplIOS::getText(void)
|
|
|
|
{
|
|
|
|
return [removeSiriString(_systemControl.textField.text) UTF8String];
|
|
|
|
}
|
|
|
|
|
|
|
|
void EditBoxImplIOS::setPlaceHolder(const char* pText)
|
|
|
|
{
|
|
|
|
_systemControl.textField.placeholder = [NSString stringWithUTF8String:pText];
|
2015-07-14 21:34:11 +08:00
|
|
|
_labelPlaceHolder->setString(pText);
|
2014-08-19 10:28:24 +08:00
|
|
|
}
|
|
|
|
|
2014-11-07 19:08:31 +08:00
|
|
|
static CGPoint convertDesignCoordToScreenCoord(const Vec2& designCoord)
|
2014-08-19 10:28:24 +08:00
|
|
|
{
|
|
|
|
auto glview = cocos2d::Director::getInstance()->getOpenGLView();
|
|
|
|
CCEAGLView *eaglview = (CCEAGLView *) glview->getEAGLView();
|
|
|
|
|
|
|
|
float viewH = (float)[eaglview getHeight];
|
|
|
|
|
|
|
|
Vec2 visiblePos = Vec2(designCoord.x * glview->getScaleX(), designCoord.y * glview->getScaleY());
|
|
|
|
Vec2 screenGLPos = visiblePos + glview->getViewPortRect().origin;
|
|
|
|
|
|
|
|
CGPoint screenPos = CGPointMake(screenGLPos.x, viewH - screenGLPos.y);
|
|
|
|
|
2014-11-07 19:08:31 +08:00
|
|
|
float factor = eaglview.contentScaleFactor;
|
|
|
|
screenPos.x = screenPos.x / factor;
|
|
|
|
screenPos.y = screenPos.y / factor;
|
|
|
|
|
2014-08-19 10:28:24 +08:00
|
|
|
CCLOGINFO("[EditBox] pos x = %f, y = %f", screenGLPos.x, screenGLPos.y);
|
|
|
|
return screenPos;
|
|
|
|
}
|
|
|
|
|
|
|
|
void EditBoxImplIOS::setPosition(const Vec2& pos)
|
|
|
|
{
|
2015-07-14 21:34:11 +08:00
|
|
|
_position = pos;
|
|
|
|
adjustTextFieldPosition();
|
2014-08-19 10:28:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void EditBoxImplIOS::setVisible(bool visible)
|
|
|
|
{
|
|
|
|
// _systemControl.textField.hidden = !visible;
|
|
|
|
}
|
|
|
|
|
|
|
|
void EditBoxImplIOS::setContentSize(const Size& size)
|
|
|
|
{
|
|
|
|
_contentSize = size;
|
|
|
|
CCLOG("[Edit text] content size = (%f, %f)", size.width, size.height);
|
|
|
|
placeInactiveLabels();
|
|
|
|
auto glview = cocos2d::Director::getInstance()->getOpenGLView();
|
|
|
|
CGSize controlSize = CGSizeMake(size.width * glview->getScaleX(),size.height * glview->getScaleY());
|
|
|
|
|
2014-11-07 19:08:31 +08:00
|
|
|
CCEAGLView *eaglview = static_cast<CCEAGLView *>(glview->getEAGLView());
|
|
|
|
float factor = eaglview.contentScaleFactor;
|
|
|
|
controlSize.width /= factor;
|
|
|
|
controlSize.height /= factor;
|
|
|
|
|
2014-08-19 10:28:24 +08:00
|
|
|
[_systemControl setContentSize:controlSize];
|
|
|
|
}
|
|
|
|
|
|
|
|
void EditBoxImplIOS::setAnchorPoint(const Vec2& anchorPoint)
|
|
|
|
{
|
|
|
|
CCLOG("[Edit text] anchor point = (%f, %f)", anchorPoint.x, anchorPoint.y);
|
2015-07-14 21:34:11 +08:00
|
|
|
_anchorPoint = anchorPoint;
|
|
|
|
setPosition(_position);
|
2014-08-19 10:28:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void EditBoxImplIOS::visit(void)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void EditBoxImplIOS::onEnter(void)
|
|
|
|
{
|
|
|
|
adjustTextFieldPosition();
|
|
|
|
const char* pText = getText();
|
|
|
|
if (pText) {
|
|
|
|
setInactiveText(pText);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void EditBoxImplIOS::updatePosition(float dt)
|
|
|
|
{
|
|
|
|
if (nullptr != _systemControl) {
|
|
|
|
this->adjustTextFieldPosition();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void EditBoxImplIOS::adjustTextFieldPosition()
|
|
|
|
{
|
2015-07-14 21:34:11 +08:00
|
|
|
Size contentSize = _editBox->getContentSize();
|
|
|
|
Rect rect = Rect(0, 0, contentSize.width, contentSize.height);
|
2014-08-19 10:28:24 +08:00
|
|
|
rect = RectApplyAffineTransform(rect, _editBox->nodeToWorldTransform());
|
2015-07-14 21:34:11 +08:00
|
|
|
|
|
|
|
Vec2 designCoord = Vec2(rect.origin.x, rect.origin.y + rect.size.height);
|
2014-11-07 19:08:31 +08:00
|
|
|
[_systemControl setPosition:convertDesignCoordToScreenCoord(designCoord)];
|
2014-08-19 10:28:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void EditBoxImplIOS::openKeyboard()
|
|
|
|
{
|
2015-07-14 21:34:11 +08:00
|
|
|
_label->setVisible(false);
|
|
|
|
_labelPlaceHolder->setVisible(false);
|
2014-08-19 10:28:24 +08:00
|
|
|
|
2015-07-14 21:34:11 +08:00
|
|
|
_systemControl.textField.hidden = NO;
|
2014-08-19 10:28:24 +08:00
|
|
|
[_systemControl openKeyboard];
|
|
|
|
}
|
|
|
|
|
|
|
|
void EditBoxImplIOS::closeKeyboard()
|
|
|
|
{
|
|
|
|
[_systemControl closeKeyboard];
|
|
|
|
}
|
|
|
|
|
|
|
|
void EditBoxImplIOS::onEndEditing()
|
|
|
|
{
|
2015-07-14 21:34:11 +08:00
|
|
|
_systemControl.textField.hidden = YES;
|
|
|
|
if(strlen(getText()) == 0)
|
|
|
|
{
|
|
|
|
_label->setVisible(false);
|
|
|
|
_labelPlaceHolder->setVisible(true);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_label->setVisible(true);
|
|
|
|
_labelPlaceHolder->setVisible(false);
|
|
|
|
setInactiveText(getText());
|
|
|
|
}
|
2014-08-19 10:28:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_CC_END
|
|
|
|
|
|
|
|
#endif /* #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS) */
|
|
|
|
|
|
|
|
|