axmol/extensions/CocoStudio/GUI/UIWidgets/UITextField.cpp

587 lines
13 KiB
C++
Raw Normal View History

2013-09-13 22:20:20 +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 "UITextField.h"
NS_CC_EXT_BEGIN
UICCTextField::UICCTextField()
: m_bMaxLengthEnabled(false)
, m_nMaxLength(0)
, m_bPasswordEnabled(false)
, m_strPasswordStyleText("*")
, m_bAttachWithIME(false)
, m_bDetachWithIME(false)
, m_bInsertText(false)
, m_bDeleteBackward(false)
{
}
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);
return NULL;
}
void UICCTextField::onEnter()
{
CCTextFieldTTF::setDelegate(this);
}
2013-09-16 15:32:52 +08:00
bool UICCTextField::onTextFieldAttachWithIME(TextFieldTTF *pSender)
2013-09-13 22:20:20 +08:00
{
setAttachWithIME(true);
return false;
}
2013-09-16 15:32:52 +08:00
bool UICCTextField::onTextFieldInsertText(TextFieldTTF *pSender, const char *text, int nLen)
2013-09-13 22:20:20 +08:00
{
if (nLen == 1 && strcmp(text, "\n") == 0)
{
return false;
}
setInsertText(true);
if (m_bMaxLengthEnabled)
{
if (CCTextFieldTTF::getCharCount() >= m_nMaxLength)
{
return true;
}
}
return false;
}
2013-09-16 15:32:52 +08:00
bool UICCTextField::onTextFieldDeleteBackward(TextFieldTTF *pSender, const char *delText, int nLen)
2013-09-13 22:20:20 +08:00
{
setDeleteBackward(true);
return false;
}
2013-09-16 15:32:52 +08:00
bool UICCTextField::onTextFieldDetachWithIME(TextFieldTTF *pSender)
2013-09-13 22:20:20 +08:00
{
setDetachWithIME(true);
return false;
}
void UICCTextField::insertText(const char * text, int len)
{
std::string str_text = text;
int str_len = strlen(CCTextFieldTTF::getString());
if (strcmp(text, "\n") != 0)
{
if (m_bMaxLengthEnabled)
{
int multiple = 1;
char value = text[0];
if (value < 0 || value > 127)
{
multiple = 3;
}
if (str_len + len > m_nMaxLength * multiple)
{
str_text = str_text.substr(0, m_nMaxLength * multiple);
len = m_nMaxLength * multiple;
/*
int mod = str_len % 3;
int offset = (mod == 0) ? 0 : (3 - mod);
int amount = str_len + offset;
str_text = str_text.substr(0, m_nMaxLength - amount);
// CCLOG("str_test = %s", str_text.c_str());
*/
}
}
}
CCTextFieldTTF::insertText(str_text.c_str(), len);
// password
if (m_bPasswordEnabled)
{
if (CCTextFieldTTF::getCharCount() > 0)
{
setPasswordText(_inputText->c_str());
}
}
}
void UICCTextField::deleteBackward()
{
CCTextFieldTTF::deleteBackward();
if (CCTextFieldTTF::getCharCount() > 0)
{
// password
if (m_bPasswordEnabled)
{
setPasswordText(_inputText->c_str());
}
}
}
void UICCTextField::openIME()
{
CCTextFieldTTF::attachWithIME();
}
void UICCTextField::closeIME()
{
CCTextFieldTTF::detachWithIME();
}
void UICCTextField::setMaxLengthEnabled(bool enable)
{
m_bMaxLengthEnabled = enable;
}
bool UICCTextField::isMaxLengthEnabled()
{
return m_bMaxLengthEnabled;
}
void UICCTextField::setMaxLength(int length)
{
m_nMaxLength = length;
}
int UICCTextField::getMaxLength()
{
return m_nMaxLength;
}
int UICCTextField::getCharCount()
{
return CCTextFieldTTF::getCharCount();
}
void UICCTextField::setPasswordEnabled(bool enable)
{
m_bPasswordEnabled = enable;
}
bool UICCTextField::isPasswordEnabled()
{
return m_bPasswordEnabled;
}
void UICCTextField::setPasswordStyleText(const char* styleText)
{
if (strlen(styleText) > 1)
{
return;
}
char value = styleText[0];
if (value < 33 || value > 126)
{
return;
}
m_strPasswordStyleText = styleText;
}
void UICCTextField::setPasswordText(const char *text)
{
std::string tempStr;
for (size_t i = 0; i < strlen(text); ++i)
{
tempStr.append(m_strPasswordStyleText);
}
CCLabelTTF::setString(tempStr.c_str());
}
void UICCTextField::setAttachWithIME(bool attach)
{
m_bAttachWithIME = attach;
}
bool UICCTextField::getAttachWithIME()
{
return m_bAttachWithIME;
}
void UICCTextField::setDetachWithIME(bool detach)
{
m_bDetachWithIME = detach;
}
bool UICCTextField::getDetachWithIME()
{
return m_bDetachWithIME;
}
void UICCTextField::setInsertText(bool insert)
{
m_bInsertText = insert;
}
bool UICCTextField::getInsertText()
{
return m_bInsertText;
}
void UICCTextField::setDeleteBackward(bool deleteBackward)
{
m_bDeleteBackward = deleteBackward;
}
bool UICCTextField::getDeleteBackward()
{
return m_bDeleteBackward;
}
UITextField::UITextField():
2013-09-16 20:54:13 +08:00
_touchWidth(0.0f),
_touchHeight(0.0f),
_useTouchArea(false),
_attachWithIMEListener(NULL),
_detachWithIMEListener(NULL),
_insertTextListener(NULL),
_deleteBackwardListener(NULL),
_attachWithIMESelector(NULL),
_detachWithIMESelector(NULL),
_insertTextSelector(NULL),
_deleteBackwardSelector(NULL),
_textFieldRenderer(NULL)
2013-09-13 22:20:20 +08:00
{
}
UITextField::~UITextField()
{
}
UITextField* UITextField::create()
{
UITextField* widget = new UITextField();
if (widget && widget->init())
{
widget->autorelease();
return widget;
}
CC_SAFE_DELETE(widget);
return NULL;
}
bool UITextField::init()
{
if (UIWidget::init())
{
setUpdateEnabled(true);
return true;
}
return false;
}
void UITextField::initRenderer()
{
UIWidget::initRenderer();
2013-09-16 20:54:13 +08:00
_textFieldRenderer = UICCTextField::create("input words here", "Thonburi", 20);
_renderer->addChild(_textFieldRenderer);
2013-09-13 22:20:20 +08:00
}
2013-09-16 15:32:52 +08:00
void UITextField::setTouchSize(const Size &size)
2013-09-13 22:20:20 +08:00
{
2013-09-16 20:54:13 +08:00
_useTouchArea = true;
_touchWidth = size.width;
_touchHeight = size.height;
2013-09-13 22:20:20 +08:00
}
void UITextField::setText(const char* text)
{
if (!text)
{
return;
}
std::string strText(text);
2013-09-16 20:54:13 +08:00
_textFieldRenderer->setString(strText.c_str());
2013-09-13 22:20:20 +08:00
textfieldRendererScaleChangedWithSize();
}
void UITextField::setPlaceHolder(const char *value)
{
2013-09-16 20:54:13 +08:00
_textFieldRenderer->setPlaceHolder(value);
2013-09-13 22:20:20 +08:00
textfieldRendererScaleChangedWithSize();
}
void UITextField::setFontSize(int size)
{
2013-09-16 20:54:13 +08:00
_textFieldRenderer->setFontSize(size);
2013-09-13 22:20:20 +08:00
textfieldRendererScaleChangedWithSize();
}
void UITextField::setFontName(const char *name)
{
2013-09-16 20:54:13 +08:00
_textFieldRenderer->setFontName(name);
2013-09-13 22:20:20 +08:00
textfieldRendererScaleChangedWithSize();
}
void UITextField::didNotSelectSelf()
{
2013-09-16 20:54:13 +08:00
_textFieldRenderer->detachWithIME();
2013-09-13 22:20:20 +08:00
}
const char* UITextField::getStringValue()
{
2013-09-16 20:54:13 +08:00
return _textFieldRenderer->getString();
2013-09-13 22:20:20 +08:00
}
2013-09-16 15:32:52 +08:00
bool UITextField::onTouchBegan(const Point &touchPoint)
2013-09-13 22:20:20 +08:00
{
bool pass = UIWidget::onTouchBegan(touchPoint);
2013-09-16 20:54:13 +08:00
_textFieldRenderer->attachWithIME();
2013-09-13 22:20:20 +08:00
return pass;
}
void UITextField::setMaxLengthEnabled(bool enable)
{
2013-09-16 20:54:13 +08:00
_textFieldRenderer->setMaxLengthEnabled(enable);
2013-09-13 22:20:20 +08:00
}
bool UITextField::isMaxLengthEnabled()
{
2013-09-16 20:54:13 +08:00
return _textFieldRenderer->isMaxLengthEnabled();
2013-09-13 22:20:20 +08:00
}
void UITextField::setMaxLength(int length)
{
2013-09-16 20:54:13 +08:00
_textFieldRenderer->setMaxLength(length);
2013-09-13 22:20:20 +08:00
}
int UITextField::getMaxLength()
{
2013-09-16 20:54:13 +08:00
return _textFieldRenderer->getMaxLength();
2013-09-13 22:20:20 +08:00
}
void UITextField::setPasswordEnabled(bool enable)
{
2013-09-16 20:54:13 +08:00
_textFieldRenderer->setPasswordEnabled(enable);
2013-09-13 22:20:20 +08:00
}
bool UITextField::isPasswordEnabled()
{
2013-09-16 20:54:13 +08:00
return _textFieldRenderer->isPasswordEnabled();
2013-09-13 22:20:20 +08:00
}
void UITextField::setPasswordStyleText(const char *styleText)
{
2013-09-16 20:54:13 +08:00
_textFieldRenderer->setPasswordStyleText(styleText);
2013-09-13 22:20:20 +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()
{
2013-09-16 20:54:13 +08:00
return _textFieldRenderer->getAttachWithIME();
2013-09-13 22:20:20 +08:00
}
void UITextField::setAttachWithIME(bool attach)
{
2013-09-16 20:54:13 +08:00
_textFieldRenderer->setAttachWithIME(attach);
2013-09-13 22:20:20 +08:00
}
bool UITextField::getDetachWithIME()
{
2013-09-16 20:54:13 +08:00
return _textFieldRenderer->getDetachWithIME();
2013-09-13 22:20:20 +08:00
}
void UITextField::setDetachWithIME(bool detach)
{
2013-09-16 20:54:13 +08:00
_textFieldRenderer->setDetachWithIME(detach);
2013-09-13 22:20:20 +08:00
}
bool UITextField::getInsertText()
{
2013-09-16 20:54:13 +08:00
return _textFieldRenderer->getInsertText();
2013-09-13 22:20:20 +08:00
}
void UITextField::setInsertText(bool insertText)
{
2013-09-16 20:54:13 +08:00
_textFieldRenderer->setInsertText(insertText);
2013-09-13 22:20:20 +08:00
}
bool UITextField::getDeleteBackward()
{
2013-09-16 20:54:13 +08:00
return _textFieldRenderer->getDeleteBackward();
2013-09-13 22:20:20 +08:00
}
void UITextField::setDeleteBackward(bool deleteBackward)
{
2013-09-16 20:54:13 +08:00
_textFieldRenderer->setDeleteBackward(deleteBackward);
2013-09-13 22:20:20 +08:00
}
void UITextField::attachWithIMEEvent()
{
2013-09-16 20:54:13 +08:00
if (_attachWithIMEListener && _attachWithIMESelector)
2013-09-13 22:20:20 +08:00
{
2013-09-16 20:54:13 +08:00
(_attachWithIMEListener->*_attachWithIMESelector)(this);
2013-09-13 22:20:20 +08:00
}
}
void UITextField::detachWithIMEEvent()
{
2013-09-16 20:54:13 +08:00
if (_detachWithIMEListener && _detachWithIMESelector)
2013-09-13 22:20:20 +08:00
{
2013-09-16 20:54:13 +08:00
(_detachWithIMEListener->*_detachWithIMESelector)(this);
2013-09-13 22:20:20 +08:00
}
}
void UITextField::insertTextEvent()
{
2013-09-16 20:54:13 +08:00
if (_insertTextListener && _insertTextSelector)
2013-09-13 22:20:20 +08:00
{
2013-09-16 20:54:13 +08:00
(_insertTextListener->*_insertTextSelector)(this);
2013-09-13 22:20:20 +08:00
}
}
void UITextField::deleteBackwardEvent()
{
2013-09-16 20:54:13 +08:00
if (_deleteBackwardListener && _deleteBackwardSelector)
2013-09-13 22:20:20 +08:00
{
2013-09-16 20:54:13 +08:00
(_deleteBackwardListener->*_deleteBackwardSelector)(this);
2013-09-13 22:20:20 +08:00
}
}
2013-09-16 15:32:52 +08:00
void UITextField::addAttachWithIMEEvent(Object *target, SEL_TextFieldAttachWithIMEEvent selecor)
2013-09-13 22:20:20 +08:00
{
2013-09-16 20:54:13 +08:00
_attachWithIMEListener = target;
_attachWithIMESelector = selecor;
2013-09-13 22:20:20 +08:00
}
2013-09-16 15:32:52 +08:00
void UITextField::addDetachWithIMEEvent(Object *target, SEL_TextFieldDetachWithIMEEvent selecor)
2013-09-13 22:20:20 +08:00
{
2013-09-16 20:54:13 +08:00
_detachWithIMEListener = target;
_detachWithIMESelector = selecor;
2013-09-13 22:20:20 +08:00
}
2013-09-16 15:32:52 +08:00
void UITextField::addInsertTextEvent(Object *target, SEL_TextFieldInsertTextEvent selecor)
2013-09-13 22:20:20 +08:00
{
2013-09-16 20:54:13 +08:00
_insertTextListener = target;
_insertTextSelector = selecor;
2013-09-13 22:20:20 +08:00
}
2013-09-16 15:32:52 +08:00
void UITextField::addDeleteBackwardEvent(Object *target, SEL_TextFieldDeleteBackwardEvent selecor)
2013-09-13 22:20:20 +08:00
{
2013-09-16 20:54:13 +08:00
_deleteBackwardListener = target;
_deleteBackwardSelector = selecor;
2013-09-13 22:20:20 +08:00
}
2013-09-16 15:32:52 +08:00
void UITextField::setAnchorPoint(const Point &pt)
2013-09-13 22:20:20 +08:00
{
UIWidget::setAnchorPoint(pt);
2013-09-16 20:54:13 +08:00
_textFieldRenderer->setAnchorPoint(pt);
2013-09-13 22:20:20 +08:00
}
2013-09-16 15:32:52 +08:00
void UITextField::setColor(const Color3B &color)
2013-09-13 22:20:20 +08:00
{
UIWidget::setColor(color);
2013-09-16 20:54:13 +08:00
_textFieldRenderer->setColor(color);
2013-09-13 22:20:20 +08:00
}
void UITextField::setOpacity(int opacity)
{
UIWidget::setOpacity(opacity);
2013-09-16 20:54:13 +08:00
_textFieldRenderer->setOpacity(opacity);
2013-09-13 22:20:20 +08:00
}
void UITextField::onSizeChanged()
{
textfieldRendererScaleChangedWithSize();
}
void UITextField::textfieldRendererScaleChangedWithSize()
{
2013-09-16 15:32:52 +08:00
if (_ignoreSize)
2013-09-13 22:20:20 +08:00
{
2013-09-16 20:54:13 +08:00
_textFieldRenderer->setScale(1.0f);
2013-09-16 15:32:52 +08:00
_size = getContentSize();
2013-09-13 22:20:20 +08:00
}
else
{
2013-09-16 15:32:52 +08:00
Size textureSize = getContentSize();
2013-09-13 22:20:20 +08:00
if (textureSize.width <= 0.0f || textureSize.height <= 0.0f)
{
2013-09-16 20:54:13 +08:00
_textFieldRenderer->setScale(1.0f);
2013-09-13 22:20:20 +08:00
return;
}
2013-09-16 15:32:52 +08:00
float scaleX = _size.width / textureSize.width;
float scaleY = _size.height / textureSize.height;
2013-09-16 20:54:13 +08:00
_textFieldRenderer->setScaleX(scaleX);
_textFieldRenderer->setScaleY(scaleY);
2013-09-13 22:20:20 +08:00
}
}
2013-09-16 15:32:52 +08:00
const Size& UITextField::getContentSize() const
2013-09-13 22:20:20 +08:00
{
2013-09-16 20:54:13 +08:00
return _textFieldRenderer->getContentSize();
2013-09-13 22:20:20 +08:00
}
2013-09-16 15:32:52 +08:00
Node* UITextField::getVirtualRenderer()
2013-09-13 22:20:20 +08:00
{
2013-09-16 20:54:13 +08:00
return _textFieldRenderer;
2013-09-13 22:20:20 +08:00
}
NS_CC_EXT_END