axmol/cocos/ui/UITextField.cpp

868 lines
20 KiB
C++
Raw Normal View History

2014-03-11 17:13:54 +08:00
/****************************************************************************
Copyright (c) 2013-2014 Chukong Technologies Inc.
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 "ui/UITextField.h"
NS_CC_BEGIN
namespace ui {
static int _calcCharCount(const char * pszText)
{
int n = 0;
char ch = 0;
while ((ch = *pszText))
{
CC_BREAK_IF(! ch);
if (0x80 != (0xC0 & ch))
{
++n;
}
++pszText;
}
return n;
}
UICCTextField::UICCTextField()
: _maxLengthEnabled(false)
, _maxLength(0)
, _passwordEnabled(false)
, _passwordStyleText("*")
, _attachWithIME(false)
, _detachWithIME(false)
, _insertText(false)
, _deleteBackward(false)
{
}
UICCTextField::~UICCTextField()
{
}
UICCTextField * UICCTextField::create(const std::string& placeholder, const std::string& fontName, float fontSize)
2014-03-11 17:13:54 +08:00
{
UICCTextField *pRet = new UICCTextField();
if(pRet && pRet->initWithPlaceHolder("", fontName, fontSize))
2014-03-11 17:13:54 +08:00
{
pRet->autorelease();
if (!placeholder.empty())
2014-03-11 17:13:54 +08:00
{
pRet->setPlaceHolder(placeholder);
}
return pRet;
}
CC_SAFE_DELETE(pRet);
return nullptr;
}
void UICCTextField::onEnter()
{
TextFieldTTF::setDelegate(this);
}
bool UICCTextField::onTextFieldAttachWithIME(TextFieldTTF *pSender)
{
setAttachWithIME(true);
return false;
}
2014-03-20 10:58:04 +08:00
bool UICCTextField::onTextFieldInsertText(TextFieldTTF *pSender, const char *text, size_t nLen)
2014-03-11 17:13:54 +08:00
{
if (nLen == 1 && strcmp(text, "\n") == 0)
{
return false;
}
setInsertText(true);
if (_maxLengthEnabled)
{
if (TextFieldTTF::getCharCount() >= _maxLength)
{
return true;
}
}
return false;
}
2014-03-20 16:24:55 +08:00
bool UICCTextField::onTextFieldDeleteBackward(TextFieldTTF *pSender, const char *delText, size_t nLen)
2014-03-11 17:13:54 +08:00
{
setDeleteBackward(true);
return false;
}
bool UICCTextField::onTextFieldDetachWithIME(TextFieldTTF *pSender)
{
setDetachWithIME(true);
return false;
}
void UICCTextField::insertText(const char* text, size_t len)
2014-03-11 17:13:54 +08:00
{
std::string input_text = text;
if (strcmp(text, "\n") != 0)
{
if (_maxLengthEnabled)
{
int text_count = _calcCharCount(getString().c_str());
if (text_count >= _maxLength)
{
// password
if (_passwordEnabled)
{
setPasswordText(getString().c_str());
}
return;
}
#if ((CC_TARGET_PLATFORM == CC_PLATFORM_IOS) || (CC_TARGET_PLATFORM == CC_PLATFORM_MAC) || (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32))
int input_count = _calcCharCount(text);
int total = total = text_count + input_count;
if (total > _maxLength)
{
int end = 0;
int length = _maxLength - text_count;
for (int i = 0; i < length; ++i)
{
char value = text[i];
if (value >= 0 && value <= 127) // ascii
{
end++;
}
else
{
end += 3;
}
}
input_text = input_text.substr(0, end);
len = end;
}
#elif (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
int input_count = _calcCharCount(text);
2014-03-28 18:54:06 +08:00
int total = text_count + input_count;
if (total > _maxLength)
2014-03-11 17:13:54 +08:00
{
int ascii = 0;
int unicode = 0;
int end = 0;
int count = 0;
2014-03-28 18:54:06 +08:00
for (int i = 0; i < total * 3; ++i)
2014-03-11 17:13:54 +08:00
{
char value = text[i];
if (value >= 0 && value <= 127) // ascii
{
ascii++;
count++;
}
else
{
unicode++;
if (unicode % 3 == 0)
{
count++;
}
}
if (count == _maxLength)
{
break;
}
}
end = ascii + unicode;
input_text = input_text.substr(0, end);
len = end;
}
#endif
}
}
TextFieldTTF::insertText(input_text.c_str(), len);
// password
if (_passwordEnabled)
{
if (TextFieldTTF::getCharCount() > 0)
{
setPasswordText(getString().c_str());
}
}
}
void UICCTextField::deleteBackward()
{
TextFieldTTF::deleteBackward();
if (TextFieldTTF::getCharCount() > 0)
{
// password
if (_passwordEnabled)
{
setPasswordText(_inputText.c_str());
}
}
}
void UICCTextField::openIME()
{
TextFieldTTF::attachWithIME();
}
void UICCTextField::closeIME()
{
TextFieldTTF::detachWithIME();
}
void UICCTextField::setMaxLengthEnabled(bool enable)
{
_maxLengthEnabled = enable;
}
2014-05-27 11:03:38 +08:00
bool UICCTextField::isMaxLengthEnabled()const
2014-03-11 17:13:54 +08:00
{
return _maxLengthEnabled;
}
void UICCTextField::setMaxLength(int length)
{
_maxLength = length;
}
2014-05-27 11:03:38 +08:00
int UICCTextField::getMaxLength()const
2014-03-11 17:13:54 +08:00
{
return _maxLength;
}
2014-05-27 11:03:38 +08:00
int UICCTextField::getCharCount()const
2014-03-11 17:13:54 +08:00
{
return TextFieldTTF::getCharCount();
}
void UICCTextField::setPasswordEnabled(bool enable)
{
_passwordEnabled = enable;
}
2014-05-27 11:03:38 +08:00
bool UICCTextField::isPasswordEnabled()const
2014-03-11 17:13:54 +08:00
{
return _passwordEnabled;
}
void UICCTextField::setPasswordStyleText(const std::string& styleText)
2014-03-11 17:13:54 +08:00
{
if (styleText.length() > 1)
2014-03-11 17:13:54 +08:00
{
return;
}
char value = styleText[0];
if (value < 33 || value > 126)
{
return;
}
_passwordStyleText = styleText;
}
void UICCTextField::setPasswordText(const std::string& text)
2014-03-11 17:13:54 +08:00
{
2014-03-28 18:54:06 +08:00
std::string tempStr = "";
int text_count = _calcCharCount(text.c_str());
2014-03-28 18:54:06 +08:00
int max = text_count;
if (_maxLengthEnabled)
{
if (text_count > _maxLength)
{
max = _maxLength;
}
}
for (int i = 0; i < max; ++i)
2014-03-11 17:13:54 +08:00
{
tempStr.append(_passwordStyleText);
}
2014-03-28 18:54:06 +08:00
Label::setString(tempStr);
2014-03-11 17:13:54 +08:00
}
void UICCTextField::setAttachWithIME(bool attach)
{
_attachWithIME = attach;
}
2014-05-27 11:03:38 +08:00
bool UICCTextField::getAttachWithIME()const
2014-03-11 17:13:54 +08:00
{
return _attachWithIME;
}
void UICCTextField::setDetachWithIME(bool detach)
{
_detachWithIME = detach;
}
2014-05-27 11:03:38 +08:00
bool UICCTextField::getDetachWithIME()const
2014-03-11 17:13:54 +08:00
{
return _detachWithIME;
}
void UICCTextField::setInsertText(bool insert)
{
_insertText = insert;
}
2014-05-27 11:03:38 +08:00
bool UICCTextField::getInsertText()const
2014-03-11 17:13:54 +08:00
{
return _insertText;
}
void UICCTextField::setDeleteBackward(bool deleteBackward)
{
_deleteBackward = deleteBackward;
}
2014-05-27 11:03:38 +08:00
bool UICCTextField::getDeleteBackward()const
2014-03-11 17:13:54 +08:00
{
return _deleteBackward;
}
static const int TEXTFIELD_RENDERER_Z = (-1);
IMPLEMENT_CLASS_GUI_INFO(TextField)
TextField::TextField():
_textFieldRenderer(nullptr),
_touchWidth(0.0f),
_touchHeight(0.0f),
_useTouchArea(false),
_textFieldEventListener(nullptr),
_textFieldEventSelector(nullptr),
2014-05-12 11:29:22 +08:00
_eventCallback(nullptr),
2014-04-17 14:08:25 +08:00
_passwordStyleText(""),
_textFieldRendererAdaptDirty(true)
2014-03-11 17:13:54 +08:00
{
}
TextField::~TextField()
{
_textFieldEventListener = nullptr;
_textFieldEventSelector = nullptr;
}
TextField* TextField::create()
{
TextField* widget = new TextField();
if (widget && widget->init())
{
widget->autorelease();
return widget;
}
CC_SAFE_DELETE(widget);
return nullptr;
}
2014-04-02 18:35:08 +08:00
TextField* TextField::create(const std::string &placeholder, const std::string &fontName, int fontSize)
{
TextField* widget = new TextField();
if (widget && widget->init())
{
widget->setPlaceHolder(placeholder);
widget->setFontName(fontName);
widget->setFontSize(fontSize);
widget->autorelease();
return widget;
}
CC_SAFE_DELETE(widget);
return nullptr;
}
2014-03-11 17:13:54 +08:00
bool TextField::init()
{
if (Widget::init())
{
setTouchEnabled(true);
return true;
}
return false;
}
void TextField::onEnter()
{
#if CC_ENABLE_SCRIPT_BINDING
if (_scriptType == kScriptTypeJavascript)
{
if (ScriptEngineManager::sendNodeEventToJSExtended(this, kNodeOnEnter))
return;
}
#endif
2014-03-11 17:13:54 +08:00
Widget::onEnter();
scheduleUpdate();
}
void TextField::initRenderer()
{
_textFieldRenderer = UICCTextField::create("input words here", "Thonburi", 20);
2014-03-24 15:25:44 +08:00
addProtectedChild(_textFieldRenderer, TEXTFIELD_RENDERER_Z, -1);
2014-03-11 17:13:54 +08:00
}
void TextField::setTouchSize(const Size &size)
{
_touchWidth = size.width;
_touchHeight = size.height;
}
void TextField::setTouchAreaEnabled(bool enable)
{
_useTouchArea = enable;
}
Squashed commit of the following: commit a9572b8913f3a38b59adbd7b4017ab9848a6b2b5 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed May 14 10:03:44 2014 -0700 math renames `Vector2` -> `Vec2` `Vector3` -> `Vec3` `Vector4` -> `Vec4` `Matrix` -> `Mat4` commit 4e107f4bd854c26bfceb52b063d6bd9cea02d6a3 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 09:24:28 2014 -0700 raw version of rename Vector3 commit 1d115573ebe96a5fc815fa44fbe6417ea7dba841 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 09:07:14 2014 -0700 rename Vector2 after merge commit ab2ed58c129dbc30a4c0970ed94568c5d271657b Merge: 1978d2d 86fb75a Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 09:05:30 2014 -0700 Merge branch 'v3' into v3_renameMathClassName Conflicts: tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UIButtonTest/UIButtonTest_Editor.cpp tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UICheckBoxTest/UICheckBoxTest_Editor.cpp tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UISliderTest/UISliderTest_Editor.cpp tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UITextFieldTest/UITextFieldTest.cpp tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UITextFieldTest/UITextFieldTest_Editor.cpp commit 1978d2d174877172ccddc083020a1bbf43ad3b39 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 08:51:45 2014 -0700 rename vector2 in tests/cpp-empty-test folder commit d4e0ff13dcce62724d2fece656543f26aa28e467 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:58:23 2014 -0700 rename vector2 in tests/cpp-tests cpp files commit be50ca2ec75e0fd32a6fcdaa15fe1ebb4cafe79f Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:52:57 2014 -0700 rename vector2 in tests/cpp-tests head files commit 6daef564400d4e28c4ce20859a68e0f583fed125 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:49:48 2014 -0700 rename vector2 in extension folder commit 8f3f0f65ceea92c9e7a0d87ab54e62220c5572e2 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:47:22 2014 -0700 rename vector2 in cocos/2d cpp files commit e1f3105aae06d595661a3030f519f7cc13aefbed Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:44:39 2014 -0700 rename vector2 in cocos/2d head files commit 6708d890bfe486109120c3cd4b9fe5c078b7108f Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:40:59 2014 -0700 rename vector2 in cocos/base folder commit d3978fa5447c31ea2f3ece5469b7e746dfba4248 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:40:43 2014 -0700 rename vector2 in cocos/deprecated folder commit 4bff45139363d6b9706edbbcf9f322d48b4fd019 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:40:26 2014 -0700 rename vector2 in cocos/editor-support folder commit 353d244c995f8b5d14f635c52aed8bc5e5fc1a6f Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:36:48 2014 -0700 rename vector2 in cocos/ui folder commit 758b8f4d513084b9922d7242e9b8f2c7f316de6c Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:32:39 2014 -0700 rename vector2 in cocos/renderer folder commit 0bd2710dd8714cecb993880bc37affd9ecb05c27 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:32:15 2014 -0700 rename vector2 in cocos/physics folder commit b7f0581c4587348bdbc1478d5374c2325735f21d Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:25:01 2014 -0700 rename vector2 in cocos/math folder commit a8631a8e1a4e2740807ccd9be9d70de6ecaad7dd Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:16:55 2014 -0700 rename Vector2 to Vec2 deprecate typedef Vector2
2014-05-15 01:07:09 +08:00
bool TextField::hitTest(const Vec2 &pt)
2014-03-11 17:13:54 +08:00
{
if (_useTouchArea)
{
Squashed commit of the following: commit a9572b8913f3a38b59adbd7b4017ab9848a6b2b5 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed May 14 10:03:44 2014 -0700 math renames `Vector2` -> `Vec2` `Vector3` -> `Vec3` `Vector4` -> `Vec4` `Matrix` -> `Mat4` commit 4e107f4bd854c26bfceb52b063d6bd9cea02d6a3 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 09:24:28 2014 -0700 raw version of rename Vector3 commit 1d115573ebe96a5fc815fa44fbe6417ea7dba841 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 09:07:14 2014 -0700 rename Vector2 after merge commit ab2ed58c129dbc30a4c0970ed94568c5d271657b Merge: 1978d2d 86fb75a Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 09:05:30 2014 -0700 Merge branch 'v3' into v3_renameMathClassName Conflicts: tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UIButtonTest/UIButtonTest_Editor.cpp tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UICheckBoxTest/UICheckBoxTest_Editor.cpp tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UISliderTest/UISliderTest_Editor.cpp tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UITextFieldTest/UITextFieldTest.cpp tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UITextFieldTest/UITextFieldTest_Editor.cpp commit 1978d2d174877172ccddc083020a1bbf43ad3b39 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 08:51:45 2014 -0700 rename vector2 in tests/cpp-empty-test folder commit d4e0ff13dcce62724d2fece656543f26aa28e467 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:58:23 2014 -0700 rename vector2 in tests/cpp-tests cpp files commit be50ca2ec75e0fd32a6fcdaa15fe1ebb4cafe79f Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:52:57 2014 -0700 rename vector2 in tests/cpp-tests head files commit 6daef564400d4e28c4ce20859a68e0f583fed125 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:49:48 2014 -0700 rename vector2 in extension folder commit 8f3f0f65ceea92c9e7a0d87ab54e62220c5572e2 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:47:22 2014 -0700 rename vector2 in cocos/2d cpp files commit e1f3105aae06d595661a3030f519f7cc13aefbed Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:44:39 2014 -0700 rename vector2 in cocos/2d head files commit 6708d890bfe486109120c3cd4b9fe5c078b7108f Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:40:59 2014 -0700 rename vector2 in cocos/base folder commit d3978fa5447c31ea2f3ece5469b7e746dfba4248 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:40:43 2014 -0700 rename vector2 in cocos/deprecated folder commit 4bff45139363d6b9706edbbcf9f322d48b4fd019 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:40:26 2014 -0700 rename vector2 in cocos/editor-support folder commit 353d244c995f8b5d14f635c52aed8bc5e5fc1a6f Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:36:48 2014 -0700 rename vector2 in cocos/ui folder commit 758b8f4d513084b9922d7242e9b8f2c7f316de6c Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:32:39 2014 -0700 rename vector2 in cocos/renderer folder commit 0bd2710dd8714cecb993880bc37affd9ecb05c27 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:32:15 2014 -0700 rename vector2 in cocos/physics folder commit b7f0581c4587348bdbc1478d5374c2325735f21d Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:25:01 2014 -0700 rename vector2 in cocos/math folder commit a8631a8e1a4e2740807ccd9be9d70de6ecaad7dd Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:16:55 2014 -0700 rename Vector2 to Vec2 deprecate typedef Vector2
2014-05-15 01:07:09 +08:00
Vec2 nsp = convertToNodeSpace(pt);
2014-03-11 17:13:54 +08:00
Rect bb = Rect(-_touchWidth * _anchorPoint.x, -_touchHeight * _anchorPoint.y, _touchWidth, _touchHeight);
if (nsp.x >= bb.origin.x && nsp.x <= bb.origin.x + bb.size.width && nsp.y >= bb.origin.y && nsp.y <= bb.origin.y + bb.size.height)
{
return true;
}
}
else
{
return Widget::hitTest(pt);
}
return false;
}
2014-05-27 11:03:38 +08:00
Size TextField::getTouchSize()const
2014-03-11 17:13:54 +08:00
{
return Size(_touchWidth, _touchHeight);
}
void TextField::setText(const std::string& text)
{
std::string strText(text);
2014-03-28 18:54:06 +08:00
2014-03-11 17:13:54 +08:00
if (isMaxLengthEnabled())
{
2014-03-28 18:54:06 +08:00
int max = _textFieldRenderer->getMaxLength();
int text_count = _calcCharCount(text.c_str());
int total = text_count + _calcCharCount(getStringValue().c_str());
if (total > max)
{
int ascii = 0;
int unicode = 0;
int end = 0;
int count = 0;
for (int i = 0; i < total * 3; ++i)
{
char value = text[i];
if (value >= 0 && value <= 127) // ascii
{
ascii++;
count++;
}
else
{
unicode++;
if (unicode % 3 == 0)
{
count++;
}
}
if (count == max)
{
break;
}
}
end = ascii + unicode;
strText = strText.substr(0, end);
}
2014-03-11 17:13:54 +08:00
}
2014-03-28 18:54:06 +08:00
2014-03-11 17:13:54 +08:00
const char* content = strText.c_str();
if (isPasswordEnabled())
{
_textFieldRenderer->setPasswordText(content);
_textFieldRenderer->setString("");
2014-03-28 18:54:06 +08:00
_textFieldRenderer->insertText(content, strlen(content));
2014-03-11 17:13:54 +08:00
}
else
{
_textFieldRenderer->setString(content);
}
2014-04-17 14:08:25 +08:00
_textFieldRendererAdaptDirty = true;
updateContentSizeWithTextureSize(_textFieldRenderer->getContentSize());
2014-03-11 17:13:54 +08:00
}
void TextField::setPlaceHolder(const std::string& value)
{
_textFieldRenderer->setPlaceHolder(value);
2014-04-17 14:08:25 +08:00
_textFieldRendererAdaptDirty = true;
updateContentSizeWithTextureSize(_textFieldRenderer->getContentSize());
2014-03-11 17:13:54 +08:00
}
2014-05-27 11:03:38 +08:00
const std::string& TextField::getPlaceHolder()const
2014-03-11 17:13:54 +08:00
{
return _textFieldRenderer->getPlaceHolder();
}
void TextField::setFontSize(int size)
{
_textFieldRenderer->setSystemFontSize(size);
2014-04-17 14:08:25 +08:00
_textFieldRendererAdaptDirty = true;
updateContentSizeWithTextureSize(_textFieldRenderer->getContentSize());
2014-03-11 17:13:54 +08:00
}
2014-05-27 11:03:38 +08:00
int TextField::getFontSize()const
2014-03-11 17:13:54 +08:00
{
return _textFieldRenderer->getSystemFontSize();
2014-03-11 17:13:54 +08:00
}
void TextField::setFontName(const std::string& name)
{
_textFieldRenderer->setSystemFontName(name);
2014-04-17 14:08:25 +08:00
_textFieldRendererAdaptDirty = true;
updateContentSizeWithTextureSize(_textFieldRenderer->getContentSize());
2014-03-11 17:13:54 +08:00
}
2014-05-27 11:03:38 +08:00
const std::string& TextField::getFontName()const
2014-03-11 17:13:54 +08:00
{
return _textFieldRenderer->getSystemFontName();
2014-03-11 17:13:54 +08:00
}
void TextField::didNotSelectSelf()
{
_textFieldRenderer->detachWithIME();
}
2014-05-27 11:03:38 +08:00
const std::string& TextField::getStringValue()const
2014-03-11 17:13:54 +08:00
{
return _textFieldRenderer->getString();
}
int TextField::getStringLength() const {
return _textFieldRenderer->getStringLength();
}
2014-03-11 17:13:54 +08:00
bool TextField::onTouchBegan(Touch *touch, Event *unusedEvent)
{
bool pass = Widget::onTouchBegan(touch, unusedEvent);
if (_hitted)
{
_textFieldRenderer->attachWithIME();
2014-06-10 14:53:27 +08:00
} else {
this->didNotSelectSelf();
2014-03-11 17:13:54 +08:00
}
return pass;
}
void TextField::setMaxLengthEnabled(bool enable)
{
_textFieldRenderer->setMaxLengthEnabled(enable);
}
2014-05-27 11:03:38 +08:00
bool TextField::isMaxLengthEnabled()const
2014-03-11 17:13:54 +08:00
{
return _textFieldRenderer->isMaxLengthEnabled();
}
void TextField::setMaxLength(int length)
{
_textFieldRenderer->setMaxLength(length);
setText(getStringValue());
}
2014-05-27 11:03:38 +08:00
int TextField::getMaxLength()const
2014-03-11 17:13:54 +08:00
{
return _textFieldRenderer->getMaxLength();
}
void TextField::setPasswordEnabled(bool enable)
{
_textFieldRenderer->setPasswordEnabled(enable);
}
2014-05-27 11:03:38 +08:00
bool TextField::isPasswordEnabled()const
2014-03-11 17:13:54 +08:00
{
return _textFieldRenderer->isPasswordEnabled();
}
void TextField::setPasswordStyleText(const char *styleText)
{
2014-07-01 10:57:12 +08:00
_textFieldRenderer->setPasswordStyleText(styleText);
_passwordStyleText = styleText;
2014-03-11 17:13:54 +08:00
2014-07-01 10:57:12 +08:00
setText(getStringValue());
2014-03-11 17:13:54 +08:00
}
2014-05-27 11:03:38 +08:00
const char* TextField::getPasswordStyleText()const
2014-03-11 17:13:54 +08:00
{
return _passwordStyleText.c_str();
}
void TextField::update(float dt)
{
if (getAttachWithIME())
{
attachWithIMEEvent();
setAttachWithIME(false);
}
if (getDetachWithIME())
{
detachWithIMEEvent();
setDetachWithIME(false);
}
if (getInsertText())
{
insertTextEvent();
setInsertText(false);
2014-04-17 14:08:25 +08:00
_textFieldRendererAdaptDirty = true;
updateContentSizeWithTextureSize(_textFieldRenderer->getContentSize());
2014-03-11 17:13:54 +08:00
}
if (getDeleteBackward())
{
deleteBackwardEvent();
setDeleteBackward(false);
2014-04-17 14:08:25 +08:00
_textFieldRendererAdaptDirty = true;
updateContentSizeWithTextureSize(_textFieldRenderer->getContentSize());
2014-03-11 17:13:54 +08:00
}
}
2014-05-27 11:03:38 +08:00
bool TextField::getAttachWithIME()const
2014-03-11 17:13:54 +08:00
{
return _textFieldRenderer->getAttachWithIME();
}
void TextField::setAttachWithIME(bool attach)
{
_textFieldRenderer->setAttachWithIME(attach);
}
2014-05-27 11:03:38 +08:00
bool TextField::getDetachWithIME()const
2014-03-11 17:13:54 +08:00
{
return _textFieldRenderer->getDetachWithIME();
}
void TextField::setDetachWithIME(bool detach)
{
_textFieldRenderer->setDetachWithIME(detach);
}
2014-05-27 11:03:38 +08:00
bool TextField::getInsertText()const
2014-03-11 17:13:54 +08:00
{
return _textFieldRenderer->getInsertText();
}
void TextField::setInsertText(bool insertText)
{
_textFieldRenderer->setInsertText(insertText);
}
2014-05-27 11:03:38 +08:00
bool TextField::getDeleteBackward()const
2014-03-11 17:13:54 +08:00
{
return _textFieldRenderer->getDeleteBackward();
}
void TextField::setDeleteBackward(bool deleteBackward)
{
_textFieldRenderer->setDeleteBackward(deleteBackward);
}
void TextField::attachWithIMEEvent()
{
if (_textFieldEventListener && _textFieldEventSelector)
{
(_textFieldEventListener->*_textFieldEventSelector)(this, TEXTFIELD_EVENT_ATTACH_WITH_IME);
}
2014-05-12 11:29:22 +08:00
if (_eventCallback) {
_eventCallback(this, EventType::ATTACH_WITH_IME);
}
2014-03-11 17:13:54 +08:00
}
void TextField::detachWithIMEEvent()
{
if (_textFieldEventListener && _textFieldEventSelector)
{
(_textFieldEventListener->*_textFieldEventSelector)(this, TEXTFIELD_EVENT_DETACH_WITH_IME);
}
2014-05-12 11:29:22 +08:00
if (_eventCallback) {
_eventCallback(this, EventType::DETACH_WITH_IME);
}
2014-03-11 17:13:54 +08:00
}
void TextField::insertTextEvent()
{
if (_textFieldEventListener && _textFieldEventSelector)
{
(_textFieldEventListener->*_textFieldEventSelector)(this, TEXTFIELD_EVENT_INSERT_TEXT);
}
2014-05-12 11:29:22 +08:00
if (_eventCallback) {
_eventCallback(this, EventType::INSERT_TEXT);
}
2014-03-11 17:13:54 +08:00
}
void TextField::deleteBackwardEvent()
{
if (_textFieldEventListener && _textFieldEventSelector)
{
(_textFieldEventListener->*_textFieldEventSelector)(this, TEXTFIELD_EVENT_DELETE_BACKWARD);
}
2014-05-12 11:29:22 +08:00
if (_eventCallback) {
_eventCallback(this, EventType::DELETE_BACKWARD);
}
2014-03-11 17:13:54 +08:00
}
void TextField::addEventListenerTextField(Ref *target, SEL_TextFieldEvent selecor)
{
_textFieldEventListener = target;
_textFieldEventSelector = selecor;
}
2014-05-12 11:29:22 +08:00
void TextField::addEventListener(const ccTextFieldCallback& callback)
2014-05-12 11:29:22 +08:00
{
_eventCallback = callback;
}
2014-03-11 17:13:54 +08:00
void TextField::onSizeChanged()
{
Widget::onSizeChanged();
2014-04-17 14:08:25 +08:00
_textFieldRendererAdaptDirty = true;
}
void TextField::adaptRenderers()
{
if (_textFieldRendererAdaptDirty)
{
textfieldRendererScaleChangedWithSize();
_textFieldRendererAdaptDirty = false;
}
2014-03-11 17:13:54 +08:00
}
void TextField::textfieldRendererScaleChangedWithSize()
{
if (_ignoreSize)
{
_textFieldRenderer->setDimensions(0,0);
2014-03-11 17:13:54 +08:00
_textFieldRenderer->setScale(1.0f);
}
else
{
_textFieldRenderer->setDimensions(_contentSize.width, _contentSize.height);
2014-03-11 17:13:54 +08:00
Size textureSize = getContentSize();
if (textureSize.width <= 0.0f || textureSize.height <= 0.0f)
{
_textFieldRenderer->setScale(1.0f);
return;
}
float scaleX = _contentSize.width / textureSize.width;
float scaleY = _contentSize.height / textureSize.height;
2014-03-11 17:13:54 +08:00
_textFieldRenderer->setScaleX(scaleX);
_textFieldRenderer->setScaleY(scaleY);
}
2014-04-17 14:08:25 +08:00
_textFieldRenderer->setPosition(_contentSize.width / 2.0f, _contentSize.height / 2.0f);
2014-03-11 17:13:54 +08:00
}
2014-04-17 14:08:25 +08:00
const Size& TextField::getVirtualRendererSize() const
2014-03-11 17:13:54 +08:00
{
return _textFieldRenderer->getContentSize();
}
Node* TextField::getVirtualRenderer()
{
return _textFieldRenderer;
}
std::string TextField::getDescription() const
{
return "TextField";
}
void TextField::attachWithIME()
{
_textFieldRenderer->attachWithIME();
}
Widget* TextField::createCloneInstance()
{
return TextField::create();
}
void TextField::copySpecialProperties(Widget *widget)
{
TextField* textField = dynamic_cast<TextField*>(widget);
if (textField)
{
setText(textField->_textFieldRenderer->getString());
setPlaceHolder(textField->getStringValue());
setFontSize(textField->_textFieldRenderer->getSystemFontSize());
setFontName(textField->_textFieldRenderer->getSystemFontName());
2014-03-11 17:13:54 +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());
2014-05-23 15:03:46 +08:00
_eventCallback = textField->_eventCallback;
_textFieldEventListener = textField->_textFieldEventListener;
_textFieldEventSelector = textField->_textFieldEventSelector;
2014-03-11 17:13:54 +08:00
}
}
void TextField::setTextAreaSize(const Size &size)
{
_textFieldRenderer->setDimensions(size.width,size.height);
2014-03-11 17:13:54 +08:00
}
void TextField::setTextHorizontalAlignment(TextHAlignment alignment)
{
_textFieldRenderer->setHorizontalAlignment(alignment);
}
void TextField::setTextVerticalAlignment(TextVAlignment alignment)
{
_textFieldRenderer->setVerticalAlignment(alignment);
}
}
NS_CC_END