2012-08-22 09:47:04 +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 "CCEditBoxImplAndroid.h"
|
2013-01-05 11:10:24 +08:00
|
|
|
|
|
|
|
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
|
|
|
|
|
2012-08-22 09:47:04 +08:00
|
|
|
#include "CCEditBox.h"
|
2012-09-21 17:01:19 +08:00
|
|
|
#include "jni/Java_org_cocos2dx_lib_Cocos2dxBitmap.h"
|
|
|
|
#include "jni/Java_org_cocos2dx_lib_Cocos2dxHelper.h"
|
2012-08-22 09:47:04 +08:00
|
|
|
|
2012-08-23 09:42:28 +08:00
|
|
|
|
2012-08-22 09:47:04 +08:00
|
|
|
NS_CC_EXT_BEGIN
|
|
|
|
|
|
|
|
CCEditBoxImpl* __createSystemEditBox(CCEditBox* pEditBox)
|
|
|
|
{
|
|
|
|
return new CCEditBoxImplAndroid(pEditBox);
|
|
|
|
}
|
|
|
|
|
|
|
|
CCEditBoxImplAndroid::CCEditBoxImplAndroid(CCEditBox* pEditText)
|
|
|
|
: CCEditBoxImpl(pEditText)
|
|
|
|
, m_pLabel(NULL)
|
2012-08-23 09:42:28 +08:00
|
|
|
, m_pLabelPlaceHolder(NULL)
|
2012-08-22 09:47:04 +08:00
|
|
|
, m_eEditBoxInputMode(kEditBoxInputModeSingleLine)
|
|
|
|
, m_eEditBoxInputFlag(kEditBoxInputFlagInitialCapsAllCharacters)
|
|
|
|
, m_eKeyboardReturnType(kKeyboardReturnTypeDefault)
|
|
|
|
, m_colText(ccWHITE)
|
|
|
|
, m_colPlaceHolder(ccGRAY)
|
|
|
|
, m_nMaxLength(-1)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
CCEditBoxImplAndroid::~CCEditBoxImplAndroid()
|
|
|
|
{
|
2013-03-12 17:23:12 +08:00
|
|
|
|
2012-08-22 09:47:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CCEditBoxImplAndroid::doAnimationWhenKeyboardMove(float duration, float distance)
|
|
|
|
{ // don't need to be implemented on android platform.
|
2013-03-12 17:23:12 +08:00
|
|
|
|
2012-08-22 09:47:04 +08:00
|
|
|
}
|
|
|
|
|
2013-03-12 17:23:12 +08:00
|
|
|
static const int CC_EDIT_BOX_PADDING = 5;
|
|
|
|
|
2012-08-22 09:47:04 +08:00
|
|
|
bool CCEditBoxImplAndroid::initWithSize(const CCSize& size)
|
|
|
|
{
|
2012-08-23 09:42:28 +08:00
|
|
|
int fontSize = getFontSizeAccordingHeightJni(size.height-12);
|
|
|
|
m_pLabel = CCLabelTTF::create("", "", size.height-12);
|
2013-02-20 21:41:08 +08:00
|
|
|
// align the text vertically center
|
|
|
|
m_pLabel->setAnchorPoint(ccp(0, 0.5f));
|
2013-03-12 17:23:12 +08:00
|
|
|
m_pLabel->setPosition(ccp(CC_EDIT_BOX_PADDING, size.height / 2.0f));
|
2012-08-23 09:42:28 +08:00
|
|
|
m_pLabel->setColor(m_colText);
|
2012-08-22 09:47:04 +08:00
|
|
|
m_pEditBox->addChild(m_pLabel);
|
2013-03-12 17:23:12 +08:00
|
|
|
|
2012-08-23 09:42:28 +08:00
|
|
|
m_pLabelPlaceHolder = CCLabelTTF::create("", "", size.height-12);
|
2013-02-20 21:41:08 +08:00
|
|
|
// align the text vertically center
|
|
|
|
m_pLabelPlaceHolder->setAnchorPoint(ccp(0, 0.5f));
|
2013-03-12 17:23:12 +08:00
|
|
|
m_pLabelPlaceHolder->setPosition(ccp(CC_EDIT_BOX_PADDING, size.height / 2.0f));
|
2012-08-23 09:42:28 +08:00
|
|
|
m_pLabelPlaceHolder->setVisible(false);
|
|
|
|
m_pLabelPlaceHolder->setColor(m_colPlaceHolder);
|
|
|
|
m_pEditBox->addChild(m_pLabelPlaceHolder);
|
|
|
|
|
|
|
|
m_EditSize = size;
|
2012-08-22 09:47:04 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-02-20 21:41:08 +08:00
|
|
|
void CCEditBoxImplAndroid::setFont(const char* pFontName, int fontSize)
|
|
|
|
{
|
|
|
|
if(m_pLabel != NULL) {
|
|
|
|
m_pLabel->setFontName(pFontName);
|
|
|
|
m_pLabel->setFontSize(fontSize);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(m_pLabelPlaceHolder != NULL) {
|
|
|
|
m_pLabelPlaceHolder->setFontName(pFontName);
|
|
|
|
m_pLabelPlaceHolder->setFontSize(fontSize);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-22 09:47:04 +08:00
|
|
|
void CCEditBoxImplAndroid::setFontColor(const ccColor3B& color)
|
|
|
|
{
|
|
|
|
m_colText = color;
|
|
|
|
m_pLabel->setColor(color);
|
|
|
|
}
|
|
|
|
|
2013-02-25 14:05:38 +08:00
|
|
|
void CCEditBoxImplAndroid::setPlaceholderFont(const char* pFontName, int fontSize)
|
|
|
|
{
|
|
|
|
if(m_pLabelPlaceHolder != NULL) {
|
|
|
|
m_pLabelPlaceHolder->setFontName(pFontName);
|
|
|
|
m_pLabelPlaceHolder->setFontSize(fontSize);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-22 09:47:04 +08:00
|
|
|
void CCEditBoxImplAndroid::setPlaceholderFontColor(const ccColor3B& color)
|
|
|
|
{
|
|
|
|
m_colPlaceHolder = color;
|
2012-08-23 09:42:28 +08:00
|
|
|
m_pLabelPlaceHolder->setColor(color);
|
2012-08-22 09:47:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CCEditBoxImplAndroid::setInputMode(EditBoxInputMode inputMode)
|
|
|
|
{
|
|
|
|
m_eEditBoxInputMode = inputMode;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCEditBoxImplAndroid::setMaxLength(int maxLength)
|
|
|
|
{
|
|
|
|
m_nMaxLength = maxLength;
|
|
|
|
}
|
|
|
|
|
|
|
|
int CCEditBoxImplAndroid::getMaxLength()
|
|
|
|
{
|
|
|
|
return m_nMaxLength;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCEditBoxImplAndroid::setInputFlag(EditBoxInputFlag inputFlag)
|
|
|
|
{
|
|
|
|
m_eEditBoxInputFlag = inputFlag;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCEditBoxImplAndroid::setReturnType(KeyboardReturnType returnType)
|
|
|
|
{
|
|
|
|
m_eKeyboardReturnType = returnType;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CCEditBoxImplAndroid::isEditing()
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCEditBoxImplAndroid::setText(const char* pText)
|
|
|
|
{
|
|
|
|
if (pText != NULL)
|
|
|
|
{
|
|
|
|
m_strText = pText;
|
2013-03-12 17:23:12 +08:00
|
|
|
|
2012-08-23 09:42:28 +08:00
|
|
|
if (m_strText.length() > 0)
|
|
|
|
{
|
|
|
|
m_pLabelPlaceHolder->setVisible(false);
|
2013-03-12 17:23:12 +08:00
|
|
|
|
2012-08-23 09:42:28 +08:00
|
|
|
std::string strToShow;
|
2013-03-12 17:23:12 +08:00
|
|
|
|
2012-08-23 09:42:28 +08:00
|
|
|
if (kEditBoxInputFlagPassword == m_eEditBoxInputFlag)
|
|
|
|
{
|
|
|
|
long length = cc_utf8_strlen(m_strText.c_str(), -1);
|
|
|
|
for (long i = 0; i < length; i++)
|
|
|
|
{
|
2013-03-12 17:23:12 +08:00
|
|
|
strToShow.append("\u25CF");
|
2012-08-23 09:42:28 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
strToShow = m_strText;
|
|
|
|
}
|
|
|
|
|
2013-03-12 17:23:12 +08:00
|
|
|
m_pLabel->setString(strToShow.c_str());
|
|
|
|
|
|
|
|
// Clip the text width to fit to the text box
|
|
|
|
float fMaxWidth = m_EditSize.width - CC_EDIT_BOX_PADDING * 2;
|
|
|
|
CCRect clippingRect = m_pLabel->getTextureRect();
|
|
|
|
if(clippingRect.size.width > fMaxWidth) {
|
|
|
|
clippingRect.size.width = fMaxWidth;
|
|
|
|
m_pLabel->setTextureRect(clippingRect);
|
|
|
|
}
|
|
|
|
|
2012-08-23 09:42:28 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_pLabelPlaceHolder->setVisible(true);
|
|
|
|
m_pLabel->setString("");
|
|
|
|
}
|
2013-03-12 17:23:12 +08:00
|
|
|
|
2012-08-22 09:47:04 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const char* CCEditBoxImplAndroid::getText(void)
|
|
|
|
{
|
|
|
|
return m_strText.c_str();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCEditBoxImplAndroid::setPlaceHolder(const char* pText)
|
|
|
|
{
|
|
|
|
if (pText != NULL)
|
|
|
|
{
|
|
|
|
m_strPlaceHolder = pText;
|
2012-08-23 09:42:28 +08:00
|
|
|
if (m_strPlaceHolder.length() > 0 && m_strText.length() == 0)
|
|
|
|
{
|
|
|
|
m_pLabelPlaceHolder->setVisible(true);
|
|
|
|
}
|
2013-03-12 17:23:12 +08:00
|
|
|
|
2012-08-23 09:42:28 +08:00
|
|
|
m_pLabelPlaceHolder->setString(m_strPlaceHolder.c_str());
|
2012-08-22 09:47:04 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCEditBoxImplAndroid::setPosition(const CCPoint& pos)
|
|
|
|
{ // don't need to be implemented on android platform.
|
2013-03-12 17:23:12 +08:00
|
|
|
|
2012-08-22 09:47:04 +08:00
|
|
|
}
|
|
|
|
|
2013-03-03 20:08:50 +08:00
|
|
|
void CCEditBoxImplAndroid::setVisible(bool visible)
|
|
|
|
{ // don't need to be implemented on android platform.
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2012-08-22 09:47:04 +08:00
|
|
|
void CCEditBoxImplAndroid::setContentSize(const CCSize& size)
|
|
|
|
{ // don't need to be implemented on android platform.
|
2013-03-12 17:23:12 +08:00
|
|
|
|
2012-08-22 09:47:04 +08:00
|
|
|
}
|
|
|
|
|
2013-02-20 21:41:08 +08:00
|
|
|
void CCEditBoxImplAndroid::setAnchorPoint(const CCPoint& anchorPoint)
|
|
|
|
{ // don't need to be implemented on android platform.
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2012-08-22 09:47:04 +08:00
|
|
|
void CCEditBoxImplAndroid::visit(void)
|
|
|
|
{ // don't need to be implemented on android platform.
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-03-12 17:23:12 +08:00
|
|
|
void CCEditBoxImplAndroid::onEnter(void)
|
|
|
|
{ // don't need to be implemented on android platform.
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2012-08-22 09:47:04 +08:00
|
|
|
static void editBoxCallbackFunc(const char* pText, void* ctx)
|
|
|
|
{
|
|
|
|
CCEditBoxImplAndroid* thiz = (CCEditBoxImplAndroid*)ctx;
|
|
|
|
thiz->setText(pText);
|
2013-03-12 17:23:12 +08:00
|
|
|
|
2012-08-22 09:47:04 +08:00
|
|
|
if (thiz->getDelegate() != NULL)
|
|
|
|
{
|
|
|
|
thiz->getDelegate()->editBoxTextChanged(thiz->getCCEditBox(), thiz->getText());
|
|
|
|
thiz->getDelegate()->editBoxEditingDidEnd(thiz->getCCEditBox());
|
|
|
|
thiz->getDelegate()->editBoxReturn(thiz->getCCEditBox());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCEditBoxImplAndroid::openKeyboard()
|
|
|
|
{
|
|
|
|
if (m_pDelegate != NULL)
|
|
|
|
{
|
|
|
|
m_pDelegate->editBoxEditingDidBegin(m_pEditBox);
|
|
|
|
}
|
2013-03-12 17:23:12 +08:00
|
|
|
|
2012-09-21 17:01:19 +08:00
|
|
|
showEditTextDialogJNI( m_strPlaceHolder.c_str(),
|
2013-03-12 17:23:12 +08:00
|
|
|
m_strText.c_str(),
|
|
|
|
m_eEditBoxInputMode,
|
|
|
|
m_eEditBoxInputFlag,
|
|
|
|
m_eKeyboardReturnType,
|
|
|
|
m_nMaxLength,
|
|
|
|
editBoxCallbackFunc,
|
|
|
|
(void*)this );
|
|
|
|
|
2012-08-22 09:47:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CCEditBoxImplAndroid::closeKeyboard()
|
|
|
|
{
|
2013-03-12 17:23:12 +08:00
|
|
|
|
2012-08-22 09:47:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_CC_EXT_END
|
|
|
|
|
2013-01-05 11:10:24 +08:00
|
|
|
#endif /* #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) */
|
|
|
|
|