mirror of https://github.com/axmolengine/axmol.git
302 lines
8.5 KiB
C++
302 lines
8.5 KiB
C++
/****************************************************************************
|
|
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"
|
|
|
|
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
|
|
|
|
#include "CCEditBox.h"
|
|
#include "jni/Java_org_cocos2dx_lib_Cocos2dxBitmap.h"
|
|
#include "jni/Java_org_cocos2dx_lib_Cocos2dxHelper.h"
|
|
|
|
|
|
NS_CC_EXT_BEGIN
|
|
|
|
CCEditBoxImpl* __createSystemEditBox(CCEditBox* pEditBox)
|
|
{
|
|
return new CCEditBoxImplAndroid(pEditBox);
|
|
}
|
|
|
|
CCEditBoxImplAndroid::CCEditBoxImplAndroid(CCEditBox* pEditText)
|
|
: CCEditBoxImpl(pEditText)
|
|
, _label(NULL)
|
|
, _labelPlaceHolder(NULL)
|
|
, _editBoxInputMode(kEditBoxInputModeSingleLine)
|
|
, _editBoxInputFlag(kEditBoxInputFlagInitialCapsAllCharacters)
|
|
, _keyboardReturnType(kKeyboardReturnTypeDefault)
|
|
, _colText(ccWHITE)
|
|
, _colPlaceHolder(ccGRAY)
|
|
, _maxLength(-1)
|
|
{
|
|
|
|
}
|
|
|
|
CCEditBoxImplAndroid::~CCEditBoxImplAndroid()
|
|
{
|
|
|
|
}
|
|
|
|
void CCEditBoxImplAndroid::doAnimationWhenKeyboardMove(float duration, float distance)
|
|
{ // don't need to be implemented on android platform.
|
|
|
|
}
|
|
|
|
static const int CC_EDIT_BOX_PADDING = 5;
|
|
|
|
bool CCEditBoxImplAndroid::initWithSize(const CCSize& size)
|
|
{
|
|
int fontSize = getFontSizeAccordingHeightJni(size.height-12);
|
|
_label = CCLabelTTF::create("", "", size.height-12);
|
|
// align the text vertically center
|
|
_label->setAnchorPoint(ccp(0, 0.5f));
|
|
_label->setPosition(ccp(CC_EDIT_BOX_PADDING, size.height / 2.0f));
|
|
_label->setColor(_colText);
|
|
_editBox->addChild(_label);
|
|
|
|
_labelPlaceHolder = CCLabelTTF::create("", "", size.height-12);
|
|
// align the text vertically center
|
|
_labelPlaceHolder->setAnchorPoint(ccp(0, 0.5f));
|
|
_labelPlaceHolder->setPosition(ccp(CC_EDIT_BOX_PADDING, size.height / 2.0f));
|
|
_labelPlaceHolder->setVisible(false);
|
|
_labelPlaceHolder->setColor(_colPlaceHolder);
|
|
_editBox->addChild(_labelPlaceHolder);
|
|
|
|
_editSize = size;
|
|
return true;
|
|
}
|
|
|
|
void CCEditBoxImplAndroid::setFont(const char* pFontName, int fontSize)
|
|
{
|
|
if(_label != NULL) {
|
|
_label->setFontName(pFontName);
|
|
_label->setFontSize(fontSize);
|
|
}
|
|
|
|
if(_labelPlaceHolder != NULL) {
|
|
_labelPlaceHolder->setFontName(pFontName);
|
|
_labelPlaceHolder->setFontSize(fontSize);
|
|
}
|
|
}
|
|
|
|
void CCEditBoxImplAndroid::setFontColor(const ccColor3B& color)
|
|
{
|
|
_colText = color;
|
|
_label->setColor(color);
|
|
}
|
|
|
|
void CCEditBoxImplAndroid::setPlaceholderFont(const char* pFontName, int fontSize)
|
|
{
|
|
if(_labelPlaceHolder != NULL) {
|
|
_labelPlaceHolder->setFontName(pFontName);
|
|
_labelPlaceHolder->setFontSize(fontSize);
|
|
}
|
|
}
|
|
|
|
void CCEditBoxImplAndroid::setPlaceholderFontColor(const ccColor3B& color)
|
|
{
|
|
_colPlaceHolder = color;
|
|
_labelPlaceHolder->setColor(color);
|
|
}
|
|
|
|
void CCEditBoxImplAndroid::setInputMode(EditBoxInputMode inputMode)
|
|
{
|
|
_editBoxInputMode = inputMode;
|
|
}
|
|
|
|
void CCEditBoxImplAndroid::setMaxLength(int maxLength)
|
|
{
|
|
_maxLength = maxLength;
|
|
}
|
|
|
|
int CCEditBoxImplAndroid::getMaxLength()
|
|
{
|
|
return _maxLength;
|
|
}
|
|
|
|
void CCEditBoxImplAndroid::setInputFlag(EditBoxInputFlag inputFlag)
|
|
{
|
|
_editBoxInputFlag = inputFlag;
|
|
}
|
|
|
|
void CCEditBoxImplAndroid::setReturnType(KeyboardReturnType returnType)
|
|
{
|
|
_keyboardReturnType = returnType;
|
|
}
|
|
|
|
bool CCEditBoxImplAndroid::isEditing()
|
|
{
|
|
return false;
|
|
}
|
|
|
|
void CCEditBoxImplAndroid::setText(const char* pText)
|
|
{
|
|
if (pText != NULL)
|
|
{
|
|
_text = pText;
|
|
|
|
if (_text.length() > 0)
|
|
{
|
|
_labelPlaceHolder->setVisible(false);
|
|
|
|
std::string strToShow;
|
|
|
|
if (kEditBoxInputFlagPassword == _editBoxInputFlag)
|
|
{
|
|
long length = cc_utf8_strlen(_text.c_str(), -1);
|
|
for (long i = 0; i < length; i++)
|
|
{
|
|
strToShow.append("\u25CF");
|
|
}
|
|
}
|
|
else
|
|
{
|
|
strToShow = _text;
|
|
}
|
|
|
|
_label->setString(strToShow.c_str());
|
|
|
|
// Clip the text width to fit to the text box
|
|
float fMaxWidth = _editSize.width - CC_EDIT_BOX_PADDING * 2;
|
|
CCRect clippingRect = _label->getTextureRect();
|
|
if(clippingRect.size.width > fMaxWidth) {
|
|
clippingRect.size.width = fMaxWidth;
|
|
_label->setTextureRect(clippingRect);
|
|
}
|
|
|
|
}
|
|
else
|
|
{
|
|
_labelPlaceHolder->setVisible(true);
|
|
_label->setString("");
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
const char* CCEditBoxImplAndroid::getText(void)
|
|
{
|
|
return _text.c_str();
|
|
}
|
|
|
|
void CCEditBoxImplAndroid::setPlaceHolder(const char* pText)
|
|
{
|
|
if (pText != NULL)
|
|
{
|
|
_placeHolder = pText;
|
|
if (_placeHolder.length() > 0 && _text.length() == 0)
|
|
{
|
|
_labelPlaceHolder->setVisible(true);
|
|
}
|
|
|
|
_labelPlaceHolder->setString(_placeHolder.c_str());
|
|
}
|
|
}
|
|
|
|
void CCEditBoxImplAndroid::setPosition(const CCPoint& pos)
|
|
{ // don't need to be implemented on android platform.
|
|
|
|
}
|
|
|
|
void CCEditBoxImplAndroid::setVisible(bool visible)
|
|
{ // don't need to be implemented on android platform.
|
|
|
|
}
|
|
|
|
void CCEditBoxImplAndroid::setContentSize(const CCSize& size)
|
|
{ // don't need to be implemented on android platform.
|
|
|
|
}
|
|
|
|
void CCEditBoxImplAndroid::setAnchorPoint(const CCPoint& anchorPoint)
|
|
{ // don't need to be implemented on android platform.
|
|
|
|
}
|
|
|
|
void CCEditBoxImplAndroid::visit(void)
|
|
{ // don't need to be implemented on android platform.
|
|
|
|
}
|
|
|
|
void CCEditBoxImplAndroid::onEnter(void)
|
|
{ // don't need to be implemented on android platform.
|
|
|
|
}
|
|
|
|
static void editBoxCallbackFunc(const char* pText, void* ctx)
|
|
{
|
|
CCEditBoxImplAndroid* thiz = (CCEditBoxImplAndroid*)ctx;
|
|
thiz->setText(pText);
|
|
|
|
if (thiz->getDelegate() != NULL)
|
|
{
|
|
thiz->getDelegate()->editBoxTextChanged(thiz->getCCEditBox(), thiz->getText());
|
|
thiz->getDelegate()->editBoxEditingDidEnd(thiz->getCCEditBox());
|
|
thiz->getDelegate()->editBoxReturn(thiz->getCCEditBox());
|
|
}
|
|
|
|
CCEditBox* pEditBox = thiz->getCCEditBox();
|
|
if (NULL != pEditBox && 0 != pEditBox->getScriptEditBoxHandler())
|
|
{
|
|
cocos2d::CCScriptEngineProtocol* pEngine = cocos2d::CCScriptEngineManager::sharedManager()->getScriptEngine();
|
|
pEngine->executeEvent(pEditBox->getScriptEditBoxHandler(), "changed",pEditBox);
|
|
pEngine->executeEvent(pEditBox->getScriptEditBoxHandler(), "ended",pEditBox);
|
|
pEngine->executeEvent(pEditBox->getScriptEditBoxHandler(), "return",pEditBox);
|
|
}
|
|
}
|
|
|
|
void CCEditBoxImplAndroid::openKeyboard()
|
|
{
|
|
if (_delegate != NULL)
|
|
{
|
|
_delegate->editBoxEditingDidBegin(_editBox);
|
|
}
|
|
CCEditBox* pEditBox = this->getCCEditBox();
|
|
if (NULL != pEditBox && 0 != pEditBox->getScriptEditBoxHandler())
|
|
{
|
|
cocos2d::CCScriptEngineProtocol* pEngine = cocos2d::CCScriptEngineManager::sharedManager()->getScriptEngine();
|
|
pEngine->executeEvent(pEditBox->getScriptEditBoxHandler(), "began",pEditBox);
|
|
}
|
|
|
|
showEditTextDialogJNI( _placeHolder.c_str(),
|
|
_text.c_str(),
|
|
_editBoxInputMode,
|
|
_editBoxInputFlag,
|
|
_keyboardReturnType,
|
|
_maxLength,
|
|
editBoxCallbackFunc,
|
|
(void*)this );
|
|
|
|
}
|
|
|
|
void CCEditBoxImplAndroid::closeKeyboard()
|
|
{
|
|
|
|
}
|
|
|
|
NS_CC_EXT_END
|
|
|
|
#endif /* #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) */
|
|
|