axmol/cocos/ui/UIEditBox/UIEditBoxImpl-android.cpp

445 lines
13 KiB
C++
Raw Normal View History

2014-08-19 10:28:24 +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.
****************************************************************************/
2014-09-10 07:50:02 +08:00
#include "UIEditBoxImpl-android.h"
2014-08-19 10:28:24 +08:00
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
#include "UIEditBox.h"
#include <jni.h>
2014-08-19 10:28:24 +08:00
#include "jni/Java_org_cocos2dx_lib_Cocos2dxBitmap.h"
#include "jni/Java_org_cocos2dx_lib_Cocos2dxHelper.h"
2014-09-10 07:50:02 +08:00
#include "2d/CCLabel.h"
#include "base/ccUTF8.h"
#include "math/Vec2.h"
#include "ui/UIHelper.h"
#include "base/ccUTF8.h"
2014-08-19 10:28:24 +08:00
NS_CC_BEGIN
2014-08-19 10:28:24 +08:00
namespace ui {
#define LOGD(...) __android_log_print(ANDROID_LOG_ERROR,"",__VA_ARGS__)
static void editBoxEditingDidBegin(int index);
static void editBoxEditingDidChanged(int index, const std::string& text);
static void editBoxEditingDidEnd(int index, const std::string& text);
extern "C"{
JNIEXPORT void JNICALL Java_org_cocos2dx_lib_Cocos2dxEditBoxHelper_editBoxEditingDidBegin(JNIEnv *env, jclass, jint index) {
editBoxEditingDidBegin(index);
}
JNIEXPORT void JNICALL Java_org_cocos2dx_lib_Cocos2dxEditBoxHelper_editBoxEditingChanged(JNIEnv *env, jclass, jint index, jstring text) {
std::string textString = StringUtils::getStringUTFCharsJNI(env,text);
editBoxEditingDidChanged(index, textString);
}
JNIEXPORT void JNICALL Java_org_cocos2dx_lib_Cocos2dxEditBoxHelper_editBoxEditingDidEnd(JNIEnv *env, jclass, jint index, jstring text) {
std::string textString = StringUtils::getStringUTFCharsJNI(env,text);
editBoxEditingDidEnd(index, textString);
}
}
static std::unordered_map<int, EditBoxImplAndroid*> s_allEditBoxes;
EditBoxImpl* __createSystemEditBox(EditBox* editBox)
{
return new EditBoxImplAndroid(editBox);
}
void EditBoxImplAndroid::editBoxEditingDidBegin()
{
// LOGD("textFieldShouldBeginEditing...");
cocos2d::ui::EditBoxDelegate *pDelegate = _editBox->getDelegate();
if (pDelegate != nullptr)
{
pDelegate->editBoxEditingDidBegin(_editBox);
}
#if CC_ENABLE_SCRIPT_BINDING
if (NULL != _editBox && 0 != _editBox->getScriptEditBoxHandler())
{
cocos2d::CommonScriptData data(_editBox->getScriptEditBoxHandler(), "began", _editBox);
cocos2d::ScriptEvent event(cocos2d::kCommonEvent, (void *)&data);
cocos2d::ScriptEngineManager::getInstance()->getScriptEngine()->sendEvent(&event);
}
#endif
}
void EditBoxImplAndroid::editBoxEditingDidEnd(const std::string& text)
{
// LOGD("textFieldShouldEndEditing...");
_text = text;
this->refreshInactiveText();
cocos2d::ui::EditBoxDelegate *pDelegate = _editBox->getDelegate();
if (pDelegate != nullptr)
{
pDelegate->editBoxEditingDidEnd(_editBox);
pDelegate->editBoxReturn(_editBox);
}
#if CC_ENABLE_SCRIPT_BINDING
if (_editBox != nullptr && 0 != _editBox->getScriptEditBoxHandler())
{
cocos2d::CommonScriptData data(_editBox->getScriptEditBoxHandler(), "ended", _editBox);
cocos2d::ScriptEvent event(cocos2d::kCommonEvent, (void *)&data);
cocos2d::ScriptEngineManager::getInstance()->getScriptEngine()->sendEvent(&event);
memset(data.eventName, 0, sizeof(data.eventName));
strncpy(data.eventName, "return", sizeof(data.eventName));
event.data = (void *)&data;
cocos2d::ScriptEngineManager::getInstance()->getScriptEngine()->sendEvent(&event);
}
#endif
if (_editBox != nullptr)
{
this->onEndEditing(text);
}
}
void EditBoxImplAndroid::editBoxEditingChanged(const std::string& text)
2014-08-19 10:28:24 +08:00
{
// LOGD("editBoxTextChanged...");
cocos2d::ui::EditBoxDelegate *pDelegate = _editBox->getDelegate();
_text = text;
if (pDelegate != nullptr)
{
pDelegate->editBoxTextChanged(_editBox, text);
}
#if CC_ENABLE_SCRIPT_BINDING
if (NULL != _editBox && 0 != _editBox->getScriptEditBoxHandler())
{
cocos2d::CommonScriptData data(_editBox->getScriptEditBoxHandler(), "changed", _editBox);
cocos2d::ScriptEvent event(cocos2d::kCommonEvent, (void *)&data);
cocos2d::ScriptEngineManager::getInstance()->getScriptEngine()->sendEvent(&event);
}
#endif
2014-08-19 10:28:24 +08:00
}
EditBoxImplAndroid::EditBoxImplAndroid(EditBox* pEditText)
: EditBoxImpl(pEditText)
, _label(nullptr)
, _labelPlaceHolder(nullptr)
, _editBoxInputMode(EditBox::InputMode::SINGLE_LINE)
, _editBoxInputFlag(EditBox::InputFlag::INTIAL_CAPS_ALL_CHARACTERS)
, _keyboardReturnType(EditBox::KeyboardReturnType::DEFAULT)
, _colText(Color3B::WHITE)
, _colPlaceHolder(Color3B::GRAY)
, _maxLength(-1)
, _editBoxIndex(-1)
2014-08-19 10:28:24 +08:00
{
}
EditBoxImplAndroid::~EditBoxImplAndroid()
{
s_allEditBoxes.erase(_editBoxIndex);
removeEditBoxJNI(_editBoxIndex);
2014-08-19 10:28:24 +08:00
}
void EditBoxImplAndroid::doAnimationWhenKeyboardMove(float duration, float distance)
{ // don't need to be implemented on android platform.
}
static const int CC_EDIT_BOX_PADDING = 5;
bool EditBoxImplAndroid::initWithSize(const Size& size)
{
auto rect = Rect(0,0, size.width, size.height);
_editBoxIndex = addEditBoxJNI(rect.origin.x, rect.origin.y, rect.size.width, rect.size.height);
s_allEditBoxes[_editBoxIndex] = this;
2014-08-19 10:28:24 +08:00
_label = Label::create();
_label->setSystemFontSize(size.height-12);
// align the text vertically center
_label->setAnchorPoint(Vec2(0, 0.5f));
_label->setPosition(Vec2(CC_EDIT_BOX_PADDING, size.height / 2.0f));
_label->setTextColor(_colText);
2014-08-19 10:28:24 +08:00
_editBox->addChild(_label);
_labelPlaceHolder = Label::create();
_labelPlaceHolder->setSystemFontSize(size.height-12);
// align the text vertically center
_labelPlaceHolder->setAnchorPoint(Vec2(0, 0.5f));
_labelPlaceHolder->setPosition(CC_EDIT_BOX_PADDING, size.height / 2.0f);
2014-08-19 10:28:24 +08:00
_labelPlaceHolder->setVisible(false);
_labelPlaceHolder->setTextColor(_colPlaceHolder);
2014-08-19 10:28:24 +08:00
_editBox->addChild(_labelPlaceHolder);
_editSize = size;
return true;
}
void EditBoxImplAndroid::setFont(const char* pFontName, int fontSize)
{
if(_label != NULL)
{
if(strlen(pFontName) > 0)
{
_label->setSystemFontName(pFontName);
}
if(fontSize > 0)
{
_label->setSystemFontSize(fontSize);
}
}
2014-08-19 10:28:24 +08:00
if(_labelPlaceHolder != NULL)
{
if(strlen(pFontName) > 0)
{
_labelPlaceHolder->setSystemFontName(pFontName);
}
if(fontSize > 0)
{
_labelPlaceHolder->setSystemFontSize(fontSize);
}
}
setFontEditBoxJNI(_editBoxIndex, pFontName, fontSize);
2014-08-19 10:28:24 +08:00
}
2015-03-02 19:31:59 +08:00
void EditBoxImplAndroid::setFontColor(const Color4B& color)
2014-08-19 10:28:24 +08:00
{
_colText = color;
2015-03-02 19:31:59 +08:00
_label->setTextColor(color);
setFontColorEditBoxJNI(_editBoxIndex, color.r, color.g, color.b, color.a);
2014-08-19 10:28:24 +08:00
}
void EditBoxImplAndroid::setPlaceholderFont(const char* pFontName, int fontSize)
{
if(_labelPlaceHolder != NULL)
{
if(strlen(pFontName) > 0)
{
_labelPlaceHolder->setSystemFontName(pFontName);
}
if(fontSize > 0)
{
_labelPlaceHolder->setSystemFontSize(fontSize);
}
}
CCLOG("Wraning! You can't change Andriod Hint fontName and fontSize");
2014-08-19 10:28:24 +08:00
}
2015-03-02 19:31:59 +08:00
void EditBoxImplAndroid::setPlaceholderFontColor(const Color4B& color)
2014-08-19 10:28:24 +08:00
{
_colPlaceHolder = color;
2015-03-02 19:31:59 +08:00
_labelPlaceHolder->setTextColor(color);
setPlaceHolderTextColorEditBoxJNI(_editBoxIndex, color.r, color.g, color.b, color.a);
2014-08-19 10:28:24 +08:00
}
void EditBoxImplAndroid::setInputMode(EditBox::InputMode inputMode)
{
_editBoxInputMode = inputMode;
setInputModeEditBoxJNI(_editBoxIndex, static_cast<int>(inputMode));
2014-08-19 10:28:24 +08:00
}
void EditBoxImplAndroid::setMaxLength(int maxLength)
{
_maxLength = maxLength;
setMaxLengthJNI(_editBoxIndex, _maxLength);
2014-08-19 10:28:24 +08:00
}
int EditBoxImplAndroid::getMaxLength()
{
return _maxLength;
}
void EditBoxImplAndroid::setInputFlag(EditBox::InputFlag inputFlag)
{
_editBoxInputFlag = inputFlag;
setInputFlagEditBoxJNI(_editBoxIndex, static_cast<int>(inputFlag));
2014-08-19 10:28:24 +08:00
}
void EditBoxImplAndroid::setReturnType(EditBox::KeyboardReturnType returnType)
{
_keyboardReturnType = returnType;
setReturnTypeEditBoxJNI(_editBoxIndex, static_cast<int>(returnType));
2014-08-19 10:28:24 +08:00
}
bool EditBoxImplAndroid::isEditing()
{
return false;
}
void EditBoxImplAndroid::setInactiveText(const char* pText)
2014-08-19 10:28:24 +08:00
{
if(EditBox::InputFlag::PASSWORD == _editBoxInputFlag)
2014-08-19 10:28:24 +08:00
{
std::string passwordString;
for(int i = 0; i < strlen(pText); ++i)
passwordString.append("\u25CF");
_label->setString(passwordString.c_str());
}
else
{
_label->setString(pText);
2014-08-19 10:28:24 +08:00
}
// Clip the text width to fit to the text box
float fMaxWidth = _editBox->getContentSize().width - CC_EDIT_BOX_PADDING * 2;
Size labelSize = _label->getContentSize();
if(labelSize.width > fMaxWidth) {
_label->setDimensions(fMaxWidth,labelSize.height);
}
}
void EditBoxImplAndroid::refreshInactiveText()
{
setInactiveText(_text.c_str());
if(_text.size() == 0)
{
_label->setVisible(false);
_labelPlaceHolder->setVisible(true);
}
else
{
_label->setVisible(true);
_labelPlaceHolder->setVisible(false);
}
}
void EditBoxImplAndroid::setText(const char* pText)
{
setTextEditBoxJNI(_editBoxIndex, pText);
_text = pText;
refreshInactiveText();
2014-08-19 10:28:24 +08:00
}
const char* EditBoxImplAndroid::getText(void)
{
return _text.c_str();
}
void EditBoxImplAndroid::setPlaceHolder(const char* pText)
{
if (pText != NULL)
{
_placeHolder = pText;
if (_placeHolder.length() > 0 && _text.length() == 0)
{
_labelPlaceHolder->setVisible(true);
}
_labelPlaceHolder->setString(_placeHolder.c_str());
setPlaceHolderTextEditBoxJNI(_editBoxIndex, pText);
2014-08-19 10:28:24 +08:00
}
}
void EditBoxImplAndroid::setPosition(const Vec2& pos)
{ // don't need to be implemented on android platform.
}
void EditBoxImplAndroid::setVisible(bool visible)
{ // don't need to be implemented on android platform.
setVisibleEditBoxJNI(_editBoxIndex, visible);
2014-08-19 10:28:24 +08:00
}
void EditBoxImplAndroid::setContentSize(const Size& size)
{ // don't need to be implemented on android platform.
}
void EditBoxImplAndroid::setAnchorPoint(const Vec2& anchorPoint)
{ // don't need to be implemented on android platform.
}
void EditBoxImplAndroid::draw(Renderer *renderer, const Mat4 &parentTransform, uint32_t parentFlags)
2014-08-19 10:28:24 +08:00
{ // don't need to be implemented on android platform.
if(parentFlags)
{
auto rect = ui::Helper::convertBoundingBoxToScreen(_editBox);
setEditBoxViewRectJNI(_editBoxIndex, rect.origin.x, rect.origin.y, rect.size.width, rect.size.height);
}
2014-08-19 10:28:24 +08:00
}
void EditBoxImplAndroid::onEnter(void)
{ // don't need to be implemented on android platform.
}
void EditBoxImplAndroid::openKeyboard()
2014-08-19 10:28:24 +08:00
{
_label->setVisible(false);
_labelPlaceHolder->setVisible(false);
//it will also open up the soft keyboard
setVisibleEditBoxJNI(_editBoxIndex,true);
}
void EditBoxImplAndroid::closeKeyboard()
{
closeEditBoxKeyboardJNI(_editBoxIndex);
}
void EditBoxImplAndroid::onEndEditing(const std::string& text)
{
setVisibleEditBoxJNI(_editBoxIndex, false);
if(text.size() == 0)
2014-08-19 10:28:24 +08:00
{
_label->setVisible(false);
_labelPlaceHolder->setVisible(true);
2014-08-19 10:28:24 +08:00
}
else
{
_label->setVisible(true);
_labelPlaceHolder->setVisible(false);
setInactiveText(text.c_str());
2014-08-19 10:28:24 +08:00
}
}
void editBoxEditingDidBegin(int index)
2014-08-19 10:28:24 +08:00
{
auto it = s_allEditBoxes.find(index);
if (it != s_allEditBoxes.end())
2014-08-19 10:28:24 +08:00
{
s_allEditBoxes[index]->editBoxEditingDidBegin();
2014-08-19 10:28:24 +08:00
}
}
void editBoxEditingDidChanged(int index, const std::string& text)
2014-08-19 10:28:24 +08:00
{
auto it = s_allEditBoxes.find(index);
if (it != s_allEditBoxes.end())
{
s_allEditBoxes[index]->editBoxEditingChanged(text);
}
2014-08-19 10:28:24 +08:00
}
void editBoxEditingDidEnd(int index, const std::string& text)
{
auto it = s_allEditBoxes.find(index);
if (it != s_allEditBoxes.end())
{
s_allEditBoxes[index]->editBoxEditingDidEnd(text);
}
2014-08-19 10:28:24 +08:00
}
} //end of ui namespace
2014-08-19 10:28:24 +08:00
NS_CC_END
#endif /* #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) */