axmol/extensions/GUI/CCEditBox/CCEditBoxImplTizen.cpp

345 lines
9.3 KiB
C++
Raw Normal View History

2013-05-05 12:42:01 +08:00
/****************************************************************************
Copyright (c) 2010-2012 cocos2d-x.org
Copyright (c) 2012 James Chen
Copyright (c) 2013 Lee, Jae-Hong
2013-05-05 12:42:01 +08:00
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 "CCEditBoxImplTizen.h"
#if (CC_TARGET_PLATFORM == CC_PLATFORM_TIZEN)
#include "CCEditBox.h"
#include "platform/tizen/CCOspForm.h"
2013-05-05 12:42:01 +08:00
using namespace Tizen::Ui::Controls;
2013-05-05 12:42:01 +08:00
NS_CC_EXT_BEGIN
EditBoxImpl* __createSystemEditBox(EditBox* pEditBox)
2013-05-05 12:42:01 +08:00
{
return new EditBoxImplTizen(pEditBox);
2013-05-05 12:42:01 +08:00
}
EditBoxImplTizen::EditBoxImplTizen(EditBox* pEditText)
: EditBoxImpl(pEditText)
, _label(NULL)
, _labelPlaceHolder(NULL)
, _editBoxInputMode(kEditBoxInputModeSingleLine)
, _editBoxInputFlag(kEditBoxInputFlagInitialCapsAllCharacters)
, _keyboardReturnType(kKeyboardReturnTypeDefault)
, _colText(Color3B::WHITE)
, _colPlaceHolder(Color3B::GRAY)
, _maxLength(-1)
2013-05-05 12:42:01 +08:00
{
}
EditBoxImplTizen::~EditBoxImplTizen()
2013-05-05 12:42:01 +08:00
{
}
void EditBoxImplTizen::doAnimationWhenKeyboardMove(float duration, float distance)
2013-05-05 12:42:01 +08:00
{
}
static const int CC_EDIT_BOX_PADDING = 5;
bool EditBoxImplTizen::initWithSize(const Size& size)
2013-05-05 12:42:01 +08:00
{
int fontSize = (int)size.height-12;
_label = LabelTTF::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 = LabelTTF::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;
2013-05-05 12:42:01 +08:00
return true;
}
void EditBoxImplTizen::setFont(const char* pFontName, int fontSize)
2013-05-05 12:42:01 +08:00
{
if(_label != NULL) {
_label->setFontName(pFontName);
_label->setFontSize(fontSize);
}
if(_labelPlaceHolder != NULL) {
_labelPlaceHolder->setFontName(pFontName);
_labelPlaceHolder->setFontSize(fontSize);
}
2013-05-05 12:42:01 +08:00
}
void EditBoxImplTizen::setFontColor(const Color3B& color)
2013-05-05 12:42:01 +08:00
{
_colText = color;
_label->setColor(color);
2013-05-05 12:42:01 +08:00
}
void EditBoxImplTizen::setPlaceholderFont(const char* pFontName, int fontSize)
2013-05-05 12:42:01 +08:00
{
if(_labelPlaceHolder != NULL) {
_labelPlaceHolder->setFontName(pFontName);
_labelPlaceHolder->setFontSize(fontSize);
}
2013-05-05 12:42:01 +08:00
}
void EditBoxImplTizen::setPlaceholderFontColor(const Color3B& color)
2013-05-05 12:42:01 +08:00
{
_colPlaceHolder = color;
_labelPlaceHolder->setColor(color);
2013-05-05 12:42:01 +08:00
}
void EditBoxImplTizen::setInputMode(EditBoxInputMode inputMode)
2013-05-05 12:42:01 +08:00
{
_editBoxInputMode = inputMode;
2013-05-05 12:42:01 +08:00
}
void EditBoxImplTizen::setMaxLength(int maxLength)
2013-05-05 12:42:01 +08:00
{
_maxLength = maxLength;
2013-05-05 12:42:01 +08:00
}
int EditBoxImplTizen::getMaxLength()
2013-05-05 12:42:01 +08:00
{
return _maxLength;
2013-05-05 12:42:01 +08:00
}
void EditBoxImplTizen::setInputFlag(EditBoxInputFlag inputFlag)
2013-05-05 12:42:01 +08:00
{
_editBoxInputFlag = inputFlag;
2013-05-05 12:42:01 +08:00
}
void EditBoxImplTizen::setReturnType(KeyboardReturnType returnType)
2013-05-05 12:42:01 +08:00
{
_keyboardReturnType = returnType;
2013-05-05 12:42:01 +08:00
}
bool EditBoxImplTizen::isEditing()
2013-05-05 12:42:01 +08:00
{
return false;
}
void EditBoxImplTizen::setText(const char* pText)
2013-05-05 12:42:01 +08:00
{
if (pText != NULL)
{
_text = pText;
if (_text.length() > 0)
2013-05-05 12:42:01 +08:00
{
_labelPlaceHolder->setVisible(false);
2013-05-05 12:42:01 +08:00
std::string strToShow;
if (kEditBoxInputFlagPassword == _editBoxInputFlag)
2013-05-05 12:42:01 +08:00
{
long length = cc_utf8_strlen(_text.c_str(), -1);
2013-05-05 12:42:01 +08:00
for (long i = 0; i < length; i++)
{
strToShow.append("*");
2013-05-05 12:42:01 +08:00
}
}
else
{
strToShow = _text;
2013-05-05 12:42:01 +08:00
}
_label->setString(strToShow.c_str());
2013-05-05 12:42:01 +08:00
// Clip the text width to fit to the text box
float fMaxWidth = _editSize.width - CC_EDIT_BOX_PADDING * 2;
Rect clippingRect = _label->getTextureRect();
if(clippingRect.size.width > fMaxWidth) {
clippingRect.size.width = fMaxWidth;
_label->setTextureRect(clippingRect);
}
2013-05-05 12:42:01 +08:00
}
else
{
_labelPlaceHolder->setVisible(true);
_label->setString("");
2013-05-05 12:42:01 +08:00
}
2013-05-05 12:42:01 +08:00
}
}
const char* EditBoxImplTizen::getText(void)
2013-05-05 12:42:01 +08:00
{
return _text.c_str();
2013-05-05 12:42:01 +08:00
}
void EditBoxImplTizen::setPlaceHolder(const char* pText)
2013-05-05 12:42:01 +08:00
{
if (pText != NULL)
{
_placeHolder = pText;
if (_placeHolder.length() > 0 && _text.length() == 0)
2013-05-05 12:42:01 +08:00
{
_labelPlaceHolder->setVisible(true);
2013-05-05 12:42:01 +08:00
}
_labelPlaceHolder->setString(_placeHolder.c_str());
2013-05-05 12:42:01 +08:00
}
}
void EditBoxImplTizen::setPosition(const Point& pos)
2013-05-05 12:42:01 +08:00
{
}
void EditBoxImplTizen::setVisible(bool visible)
2013-05-05 12:42:01 +08:00
{
}
void EditBoxImplTizen::setContentSize(const Size& size)
2013-05-05 12:42:01 +08:00
{
}
void EditBoxImplTizen::setAnchorPoint(const Point& anchorPoint)
2013-05-05 12:42:01 +08:00
{
}
void EditBoxImplTizen::visit(void)
2013-05-05 12:42:01 +08:00
{
}
void EditBoxImplTizen::onEnter(void)
2013-05-05 12:42:01 +08:00
{
}
static void editBoxCallbackFunc(const char* pText, void* ctx)
{
EditBoxImplTizen* thiz = (EditBoxImplTizen*)ctx;
2013-05-05 12:42:01 +08:00
thiz->setText(pText);
2013-05-05 12:42:01 +08:00
if (thiz->getDelegate() != NULL)
{
thiz->getDelegate()->editBoxTextChanged(thiz->getEditBox(), thiz->getText());
thiz->getDelegate()->editBoxEditingDidEnd(thiz->getEditBox());
thiz->getDelegate()->editBoxReturn(thiz->getEditBox());
2013-05-05 12:42:01 +08:00
}
2013-05-24 11:42:27 +08:00
EditBox* pEditBox = thiz->getEditBox();
2013-05-24 11:42:27 +08:00
if (NULL != pEditBox && 0 != pEditBox->getScriptEditBoxHandler())
{
CommonScriptData data(pEditBox->getScriptEditBoxHandler(), "changed",pEditBox);
ScriptEvent event(kCommonEvent,(void*)&data);
ScriptEngineManager::sharedManager()->getScriptEngine()->sendEvent(&event);
memset(data.eventName,0,64*sizeof(char));
strncpy(data.eventName,"ended",64);
event.data = (void*)&data;
ScriptEngineManager::sharedManager()->getScriptEngine()->sendEvent(&event);
memset(data.eventName,0,64*sizeof(char));
strncpy(data.eventName,"return",64);
event.data = (void*)&data;
ScriptEngineManager::sharedManager()->getScriptEngine()->sendEvent(&event);
2013-05-24 11:42:27 +08:00
}
2013-05-05 12:42:01 +08:00
}
void EditBoxImplTizen::openKeyboard()
2013-05-05 12:42:01 +08:00
{
if (_delegate != NULL)
2013-05-05 12:42:01 +08:00
{
_delegate->editBoxEditingDidBegin(_editBox);
2013-05-05 12:42:01 +08:00
}
EditBox* pEditBox = this->getEditBox();
2013-05-24 11:42:27 +08:00
if (NULL != pEditBox && 0 != pEditBox->getScriptEditBoxHandler())
{
CommonScriptData data(pEditBox->getScriptEditBoxHandler(), "began",pEditBox);
ScriptEvent event(kCommonEvent,(void*)&data);
ScriptEngineManager::sharedManager()->getScriptEngine()->sendEvent(&event);
2013-05-24 11:42:27 +08:00
}
KeypadStyle keypadStyle = KEYPAD_STYLE_NORMAL;
KeypadInputModeCategory keypadCategory = KEYPAD_MODE_ALPHA;
bool bSingleLineEnabled = false;
switch (_editBoxInputMode)
{
case kEditBoxInputModeAny:
keypadStyle = KEYPAD_STYLE_NORMAL;
break;
case kEditBoxInputModeEmailAddr:
keypadStyle = KEYPAD_STYLE_EMAIL;
break;
case kEditBoxInputModeNumeric:
case kEditBoxInputModeDecimal:
keypadStyle = KEYPAD_STYLE_NUMBER;
keypadCategory = KEYPAD_MODE_NUMERIC;
break;
case kEditBoxInputModePhoneNumber:
keypadStyle = KEYPAD_STYLE_PHONE_NUMBER;
break;
case kEditBoxInputModeUrl:
keypadStyle = KEYPAD_STYLE_URL;
break;
case kEditBoxInputModeSingleLine:
bSingleLineEnabled = true;
break;
default:
keypadStyle = KEYPAD_STYLE_NORMAL;
break;
}
bool bTextPrediction = true;
switch (_editBoxInputFlag)
{
case kEditBoxInputFlagPassword:
keypadStyle = KEYPAD_STYLE_PASSWORD;
break;
case kEditBoxInputFlagSensitive:
bTextPrediction = false;
break;
default:
break;
}
((OspForm *)OspApplication::GetInstance()->getOspForm())->ShowKeypad(
_text.c_str(),
keypadStyle,
keypadCategory,
bSingleLineEnabled,
bTextPrediction,
_maxLength,
editBoxCallbackFunc,
(void*)this);
2013-05-05 12:42:01 +08:00
}
void EditBoxImplTizen::closeKeyboard()
2013-05-05 12:42:01 +08:00
{
((OspForm *)OspApplication::GetInstance()->getOspForm())->CloseKeypad();
2013-05-05 12:42:01 +08:00
}
NS_CC_EXT_END
#endif /* #if (CC_TARGET_PLATFORM == CC_PLATFORM_TIZEN) */