axmol/cocos/gui/UITextField.cpp

601 lines
13 KiB
C++
Raw Normal View History

2013-09-20 22:23:13 +08:00
/****************************************************************************
Copyright (c) 2013 cocos2d-x.org
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 "gui/UITextField.h"
2013-09-20 22:23:13 +08:00
namespace gui {
2013-09-20 22:23:13 +08:00
UICCTextField::UICCTextField()
2013-11-06 16:04:06 +08:00
: _maxLengthEnabled(false)
, _maxLength(0)
, _passwordEnabled(false)
, _passwordStyleText("*")
, _attachWithIME(false)
, _detachWithIME(false)
, _insertText(false)
, _deleteBackward(false)
2013-09-20 22:23:13 +08:00
{
}
UICCTextField::~UICCTextField()
{
}
UICCTextField * UICCTextField::create(const char *placeholder, const char *fontName, float fontSize)
{
UICCTextField *pRet = new UICCTextField();
if(pRet && pRet->initWithString("", fontName, fontSize))
{
pRet->autorelease();
if (placeholder)
{
pRet->setPlaceHolder(placeholder);
}
return pRet;
}
CC_SAFE_DELETE(pRet);
2013-11-14 11:37:46 +08:00
return nullptr;
2013-09-20 22:23:13 +08:00
}
void UICCTextField::onEnter()
{
2013-11-06 16:04:06 +08:00
cocos2d::TextFieldTTF::setDelegate(this);
2013-09-20 22:23:13 +08:00
}
2013-11-06 16:04:06 +08:00
bool UICCTextField::onTextFieldAttachWithIME(cocos2d::TextFieldTTF *pSender)
2013-09-20 22:23:13 +08:00
{
setAttachWithIME(true);
return false;
}
2013-11-06 16:04:06 +08:00
bool UICCTextField::onTextFieldInsertText(cocos2d::TextFieldTTF *pSender, const char *text, int nLen)
2013-09-20 22:23:13 +08:00
{
if (nLen == 1 && strcmp(text, "\n") == 0)
{
return false;
}
setInsertText(true);
2013-11-06 16:04:06 +08:00
if (_maxLengthEnabled)
2013-09-20 22:23:13 +08:00
{
2013-11-06 16:04:06 +08:00
if (cocos2d::TextFieldTTF::getCharCount() >= _maxLength)
2013-09-20 22:23:13 +08:00
{
return true;
}
}
return false;
}
2013-11-06 16:04:06 +08:00
bool UICCTextField::onTextFieldDeleteBackward(cocos2d::TextFieldTTF *pSender, const char *delText, int nLen)
2013-09-20 22:23:13 +08:00
{
setDeleteBackward(true);
return false;
}
2013-11-06 16:04:06 +08:00
bool UICCTextField::onTextFieldDetachWithIME(cocos2d::TextFieldTTF *pSender)
2013-09-20 22:23:13 +08:00
{
setDetachWithIME(true);
return false;
}
void UICCTextField::insertText(const char * text, int len)
{
std::string str_text = text;
2013-12-06 16:32:06 +08:00
size_t str_len = cocos2d::TextFieldTTF::getString().size();
2013-09-20 22:23:13 +08:00
if (strcmp(text, "\n") != 0)
{
2013-11-06 16:04:06 +08:00
if (_maxLengthEnabled)
2013-09-20 22:23:13 +08:00
{
int multiple = 1;
char value = text[0];
if (value < 0 || value > 127)
{
multiple = 3;
}
2013-11-06 16:04:06 +08:00
if (str_len + len > _maxLength * multiple)
2013-09-20 22:23:13 +08:00
{
2013-11-06 16:04:06 +08:00
str_text = str_text.substr(0, _maxLength * multiple);
len = _maxLength * multiple;
2013-09-20 22:23:13 +08:00
/*
int mod = str_len % 3;
int offset = (mod == 0) ? 0 : (3 - mod);
int amount = str_len + offset;
2013-11-06 16:04:06 +08:00
str_text = str_text.substr(0, _maxLength - amount);
2013-09-20 22:23:13 +08:00
// CCLOG("str_test = %s", str_text.c_str());
*/
}
}
}
2013-11-06 16:04:06 +08:00
cocos2d::TextFieldTTF::insertText(str_text.c_str(), len);
2013-09-20 22:23:13 +08:00
// password
2013-11-06 16:04:06 +08:00
if (_passwordEnabled)
2013-09-20 22:23:13 +08:00
{
2013-11-06 16:04:06 +08:00
if (cocos2d::TextFieldTTF::getCharCount() > 0)
2013-09-20 22:23:13 +08:00
{
setPasswordText(_inputText.c_str());
2013-09-20 22:23:13 +08:00
}
}
}
void UICCTextField::deleteBackward()
{
2013-11-06 16:04:06 +08:00
cocos2d::TextFieldTTF::deleteBackward();
2013-09-20 22:23:13 +08:00
2013-11-06 16:04:06 +08:00
if (cocos2d::TextFieldTTF::getCharCount() > 0)
2013-09-20 22:23:13 +08:00
{
// password
2013-11-06 16:04:06 +08:00
if (_passwordEnabled)
2013-09-20 22:23:13 +08:00
{
setPasswordText(_inputText.c_str());
2013-09-20 22:23:13 +08:00
}
}
}
void UICCTextField::openIME()
{
2013-11-06 16:04:06 +08:00
cocos2d::TextFieldTTF::attachWithIME();
2013-09-20 22:23:13 +08:00
}
void UICCTextField::closeIME()
{
2013-11-06 16:04:06 +08:00
cocos2d::TextFieldTTF::detachWithIME();
2013-09-20 22:23:13 +08:00
}
void UICCTextField::setMaxLengthEnabled(bool enable)
{
2013-11-06 16:04:06 +08:00
_maxLengthEnabled = enable;
2013-09-20 22:23:13 +08:00
}
bool UICCTextField::isMaxLengthEnabled()
{
2013-11-06 16:04:06 +08:00
return _maxLengthEnabled;
2013-09-20 22:23:13 +08:00
}
void UICCTextField::setMaxLength(int length)
{
2013-11-06 16:04:06 +08:00
_maxLength = length;
2013-09-20 22:23:13 +08:00
}
int UICCTextField::getMaxLength()
{
2013-11-06 16:04:06 +08:00
return _maxLength;
2013-09-20 22:23:13 +08:00
}
int UICCTextField::getCharCount()
{
2013-11-06 16:04:06 +08:00
return cocos2d::TextFieldTTF::getCharCount();
2013-09-20 22:23:13 +08:00
}
void UICCTextField::setPasswordEnabled(bool enable)
{
2013-11-06 16:04:06 +08:00
_passwordEnabled = enable;
2013-09-20 22:23:13 +08:00
}
bool UICCTextField::isPasswordEnabled()
{
2013-11-06 16:04:06 +08:00
return _passwordEnabled;
2013-09-20 22:23:13 +08:00
}
void UICCTextField::setPasswordStyleText(const char* styleText)
{
if (strlen(styleText) > 1)
{
return;
}
char value = styleText[0];
if (value < 33 || value > 126)
{
return;
}
2013-11-06 16:04:06 +08:00
_passwordStyleText = styleText;
2013-09-20 22:23:13 +08:00
}
void UICCTextField::setPasswordText(const char *text)
{
std::string tempStr;
for (size_t i = 0; i < strlen(text); ++i)
{
2013-11-06 16:04:06 +08:00
tempStr.append(_passwordStyleText);
2013-09-20 22:23:13 +08:00
}
2013-11-06 16:04:06 +08:00
cocos2d::LabelTTF::setString(tempStr.c_str());
2013-09-20 22:23:13 +08:00
}
void UICCTextField::setAttachWithIME(bool attach)
{
2013-11-06 16:04:06 +08:00
_attachWithIME = attach;
2013-09-20 22:23:13 +08:00
}
bool UICCTextField::getAttachWithIME()
{
2013-11-06 16:04:06 +08:00
return _attachWithIME;
2013-09-20 22:23:13 +08:00
}
void UICCTextField::setDetachWithIME(bool detach)
{
2013-11-06 16:04:06 +08:00
_detachWithIME = detach;
2013-09-20 22:23:13 +08:00
}
bool UICCTextField::getDetachWithIME()
{
2013-11-06 16:04:06 +08:00
return _detachWithIME;
2013-09-20 22:23:13 +08:00
}
void UICCTextField::setInsertText(bool insert)
{
2013-11-06 16:04:06 +08:00
_insertText = insert;
2013-09-20 22:23:13 +08:00
}
bool UICCTextField::getInsertText()
{
2013-11-06 16:04:06 +08:00
return _insertText;
2013-09-20 22:23:13 +08:00
}
void UICCTextField::setDeleteBackward(bool deleteBackward)
{
2013-11-06 16:04:06 +08:00
_deleteBackward = deleteBackward;
2013-09-20 22:23:13 +08:00
}
bool UICCTextField::getDeleteBackward()
{
2013-11-06 16:04:06 +08:00
return _deleteBackward;
2013-09-20 22:23:13 +08:00
}
UITextField::UITextField():
2013-11-14 11:37:46 +08:00
_textFieldRenderer(nullptr),
2013-09-20 22:23:13 +08:00
_touchWidth(0.0f),
_touchHeight(0.0f),
_useTouchArea(false),
2013-11-14 11:37:46 +08:00
_textFieldEventListener(nullptr),
_textFieldEventSelector(nullptr),
2013-11-06 16:04:06 +08:00
_passwordStyleText("")
2013-09-20 22:23:13 +08:00
{
}
UITextField::~UITextField()
{
2013-11-14 11:37:46 +08:00
_textFieldEventListener = nullptr;
_textFieldEventSelector = nullptr;
2013-09-20 22:23:13 +08:00
}
UITextField* UITextField::create()
{
UITextField* widget = new UITextField();
if (widget && widget->init())
{
widget->autorelease();
return widget;
}
CC_SAFE_DELETE(widget);
2013-11-14 11:37:46 +08:00
return nullptr;
2013-09-20 22:23:13 +08:00
}
bool UITextField::init()
{
if (UIWidget::init())
{
setUpdateEnabled(true);
return true;
}
return false;
}
void UITextField::initRenderer()
{
UIWidget::initRenderer();
_textFieldRenderer = UICCTextField::create("input words here", "Thonburi", 20);
_renderer->addChild(_textFieldRenderer);
}
2013-11-06 16:04:06 +08:00
void UITextField::setTouchSize(const cocos2d::Size &size)
2013-09-20 22:23:13 +08:00
{
_useTouchArea = true;
_touchWidth = size.width;
_touchHeight = size.height;
}
void UITextField::setText(const std::string& text)
2013-09-20 22:23:13 +08:00
{
if (text.size()==0)
2013-09-20 22:23:13 +08:00
return;
_textFieldRenderer->setString(text);
2013-09-20 22:23:13 +08:00
textfieldRendererScaleChangedWithSize();
}
void UITextField::setPlaceHolder(const std::string& value)
2013-09-20 22:23:13 +08:00
{
_textFieldRenderer->setPlaceHolder(value);
textfieldRendererScaleChangedWithSize();
}
void UITextField::setFontSize(int size)
{
_textFieldRenderer->setFontSize(size);
textfieldRendererScaleChangedWithSize();
}
void UITextField::setFontName(const std::string& name)
2013-09-20 22:23:13 +08:00
{
_textFieldRenderer->setFontName(name);
textfieldRendererScaleChangedWithSize();
}
void UITextField::didNotSelectSelf()
{
_textFieldRenderer->detachWithIME();
}
const std::string& UITextField::getStringValue()
2013-09-20 22:23:13 +08:00
{
return _textFieldRenderer->getString();
}
2013-11-06 16:04:06 +08:00
bool UITextField::onTouchBegan(const cocos2d::Point &touchPoint)
2013-09-20 22:23:13 +08:00
{
bool pass = UIWidget::onTouchBegan(touchPoint);
_textFieldRenderer->attachWithIME();
return pass;
}
void UITextField::setMaxLengthEnabled(bool enable)
{
_textFieldRenderer->setMaxLengthEnabled(enable);
}
bool UITextField::isMaxLengthEnabled()
{
return _textFieldRenderer->isMaxLengthEnabled();
}
void UITextField::setMaxLength(int length)
{
_textFieldRenderer->setMaxLength(length);
}
int UITextField::getMaxLength()
{
return _textFieldRenderer->getMaxLength();
}
void UITextField::setPasswordEnabled(bool enable)
{
_textFieldRenderer->setPasswordEnabled(enable);
}
bool UITextField::isPasswordEnabled()
{
return _textFieldRenderer->isPasswordEnabled();
}
void UITextField::setPasswordStyleText(const char *styleText)
{
_textFieldRenderer->setPasswordStyleText(styleText);
2013-11-06 16:04:06 +08:00
_passwordStyleText = styleText;
2013-09-20 22:23:13 +08:00
}
void UITextField::update(float dt)
{
if (getAttachWithIME())
{
attachWithIMEEvent();
setAttachWithIME(false);
}
if (getDetachWithIME())
{
detachWithIMEEvent();
setDetachWithIME(false);
}
if (getInsertText())
{
insertTextEvent();
setInsertText(false);
textfieldRendererScaleChangedWithSize();
}
if (getDeleteBackward())
{
deleteBackwardEvent();
setDeleteBackward(false);
}
}
bool UITextField::getAttachWithIME()
{
return _textFieldRenderer->getAttachWithIME();
}
void UITextField::setAttachWithIME(bool attach)
{
_textFieldRenderer->setAttachWithIME(attach);
}
bool UITextField::getDetachWithIME()
{
return _textFieldRenderer->getDetachWithIME();
}
void UITextField::setDetachWithIME(bool detach)
{
_textFieldRenderer->setDetachWithIME(detach);
}
bool UITextField::getInsertText()
{
return _textFieldRenderer->getInsertText();
}
void UITextField::setInsertText(bool insertText)
{
_textFieldRenderer->setInsertText(insertText);
}
bool UITextField::getDeleteBackward()
{
return _textFieldRenderer->getDeleteBackward();
}
void UITextField::setDeleteBackward(bool deleteBackward)
{
_textFieldRenderer->setDeleteBackward(deleteBackward);
}
void UITextField::attachWithIMEEvent()
{
2013-11-08 18:43:06 +08:00
if (_textFieldEventListener && _textFieldEventSelector)
2013-09-20 22:23:13 +08:00
{
2013-11-08 18:43:06 +08:00
(_textFieldEventListener->*_textFieldEventSelector)(this, TEXTFIELD_EVENT_ATTACH_WITH_IME);
2013-09-20 22:23:13 +08:00
}
}
void UITextField::detachWithIMEEvent()
{
2013-11-08 18:43:06 +08:00
if (_textFieldEventListener && _textFieldEventSelector)
2013-09-20 22:23:13 +08:00
{
2013-11-08 18:43:06 +08:00
(_textFieldEventListener->*_textFieldEventSelector)(this, TEXTFIELD_EVENT_DETACH_WITH_IME);
2013-09-20 22:23:13 +08:00
}
}
void UITextField::insertTextEvent()
{
2013-11-08 18:43:06 +08:00
if (_textFieldEventListener && _textFieldEventSelector)
2013-09-20 22:23:13 +08:00
{
2013-11-08 18:43:06 +08:00
(_textFieldEventListener->*_textFieldEventSelector)(this, TEXTFIELD_EVENT_INSERT_TEXT);
2013-09-20 22:23:13 +08:00
}
}
void UITextField::deleteBackwardEvent()
{
2013-11-08 18:43:06 +08:00
if (_textFieldEventListener && _textFieldEventSelector)
2013-09-20 22:23:13 +08:00
{
2013-11-08 18:43:06 +08:00
(_textFieldEventListener->*_textFieldEventSelector)(this, TEXTFIELD_EVENT_DELETE_BACKWARD);
2013-09-20 22:23:13 +08:00
}
}
2013-11-08 18:43:06 +08:00
void UITextField::addEventListenerTextField(cocos2d::Object *target, SEL_TextFieldEvent selecor)
2013-09-20 22:23:13 +08:00
{
2013-11-08 18:43:06 +08:00
_textFieldEventListener = target;
_textFieldEventSelector = selecor;
2013-09-20 22:23:13 +08:00
}
2013-11-06 16:04:06 +08:00
void UITextField::setAnchorPoint(const cocos2d::Point &pt)
2013-09-20 22:23:13 +08:00
{
UIWidget::setAnchorPoint(pt);
_textFieldRenderer->setAnchorPoint(pt);
}
2013-11-06 16:04:06 +08:00
void UITextField::setColor(const cocos2d::Color3B &color)
2013-09-20 22:23:13 +08:00
{
UIWidget::setColor(color);
_textFieldRenderer->setColor(color);
}
void UITextField::setOpacity(int opacity)
{
UIWidget::setOpacity(opacity);
_textFieldRenderer->setOpacity(opacity);
}
void UITextField::onSizeChanged()
{
textfieldRendererScaleChangedWithSize();
}
void UITextField::textfieldRendererScaleChangedWithSize()
{
if (_ignoreSize)
{
_textFieldRenderer->setScale(1.0f);
_size = getContentSize();
}
else
{
2013-11-06 16:04:06 +08:00
cocos2d::Size textureSize = getContentSize();
2013-09-20 22:23:13 +08:00
if (textureSize.width <= 0.0f || textureSize.height <= 0.0f)
{
_textFieldRenderer->setScale(1.0f);
return;
}
float scaleX = _size.width / textureSize.width;
float scaleY = _size.height / textureSize.height;
_textFieldRenderer->setScaleX(scaleX);
_textFieldRenderer->setScaleY(scaleY);
}
}
2013-11-06 16:04:06 +08:00
const cocos2d::Size& UITextField::getContentSize() const
2013-09-20 22:23:13 +08:00
{
return _textFieldRenderer->getContentSize();
}
2013-11-06 16:04:06 +08:00
cocos2d::Node* UITextField::getVirtualRenderer()
2013-09-20 22:23:13 +08:00
{
return _textFieldRenderer;
}
const char* UITextField::getDescription() const
{
return "TextField";
}
2013-11-06 16:04:06 +08:00
void UITextField::attachWithIME()
{
_textFieldRenderer->attachWithIME();
}
UIWidget* UITextField::createCloneInstance()
{
return UITextField::create();
}
void UITextField::copySpecialProperties(UIWidget *widget)
{
UITextField* textField = dynamic_cast<UITextField*>(widget);
if (textField)
{
setText(textField->_textFieldRenderer->getString());
setPlaceHolder(textField->getStringValue());
setFontSize(textField->_textFieldRenderer->getFontSize());
2013-11-07 16:43:04 +08:00
setFontName(textField->_textFieldRenderer->getFontName().c_str());
2013-11-06 16:04:06 +08:00
setMaxLengthEnabled(textField->isMaxLengthEnabled());
setMaxLength(textField->getMaxLength());
setPasswordEnabled(textField->isPasswordEnabled());
setPasswordStyleText(textField->_passwordStyleText.c_str());
setAttachWithIME(textField->getAttachWithIME());
setDetachWithIME(textField->getDetachWithIME());
setInsertText(textField->getInsertText());
setDeleteBackward(textField->getDeleteBackward());
}
}
}