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"
|
2015-08-07 17:27:15 +08:00
|
|
|
#include <jni.h>
|
2014-08-19 10:28:24 +08:00
|
|
|
#include "jni/Java_org_cocos2dx_lib_Cocos2dxHelper.h"
|
2014-09-10 07:50:02 +08:00
|
|
|
#include "2d/CCLabel.h"
|
|
|
|
#include "base/ccUTF8.h"
|
2015-08-07 17:27:15 +08:00
|
|
|
#include "math/Vec2.h"
|
|
|
|
#include "ui/UIHelper.h"
|
2015-09-02 11:04:50 +08:00
|
|
|
#include "base/CCDirector.h"
|
2014-08-19 10:28:24 +08:00
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
2015-08-07 17:27:15 +08:00
|
|
|
|
2014-08-19 10:28:24 +08:00
|
|
|
namespace ui {
|
|
|
|
|
2015-08-07 17:27:15 +08:00
|
|
|
#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);
|
|
|
|
}
|
|
|
|
|
2014-08-19 10:28:24 +08:00
|
|
|
|
|
|
|
EditBoxImplAndroid::EditBoxImplAndroid(EditBox* pEditText)
|
2015-08-31 18:36:27 +08:00
|
|
|
: EditBoxImplCommon(pEditText)
|
2015-08-07 17:27:15 +08:00
|
|
|
, _editBoxIndex(-1)
|
2014-08-19 10:28:24 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
EditBoxImplAndroid::~EditBoxImplAndroid()
|
|
|
|
{
|
2015-08-07 17:27:15 +08:00
|
|
|
s_allEditBoxes.erase(_editBoxIndex);
|
|
|
|
removeEditBoxJNI(_editBoxIndex);
|
2014-08-19 10:28:24 +08:00
|
|
|
}
|
|
|
|
|
2015-08-31 18:36:27 +08:00
|
|
|
void EditBoxImplAndroid::createNativeControl(const Rect& frame)
|
2014-08-19 10:28:24 +08:00
|
|
|
{
|
2015-09-02 11:04:50 +08:00
|
|
|
auto director = cocos2d::Director::getInstance();
|
|
|
|
auto glView = director->getOpenGLView();
|
|
|
|
auto frameSize = glView->getFrameSize();
|
|
|
|
|
|
|
|
auto winSize = director->getWinSize();
|
|
|
|
auto leftBottom = _editBox->convertToWorldSpace(Point::ZERO);
|
|
|
|
|
|
|
|
auto contentSize = frame.size;
|
|
|
|
auto rightTop = _editBox->convertToWorldSpace(Point(contentSize.width, contentSize.height));
|
|
|
|
|
|
|
|
auto uiLeft = frameSize.width / 2 + (leftBottom.x - winSize.width / 2 ) * glView->getScaleX();
|
|
|
|
auto uiTop = frameSize.height /2 - (rightTop.y - winSize.height / 2) * glView->getScaleY();
|
|
|
|
auto uiWidth = (rightTop.x - leftBottom.x) * glView->getScaleX();
|
|
|
|
auto uiHeight = (rightTop.y - leftBottom.y) * glView->getScaleY();
|
|
|
|
LOGD("scaleX = %f", glView->getScaleX());
|
|
|
|
_editBoxIndex = addEditBoxJNI(uiLeft, uiTop, uiWidth, uiHeight, glView->getScaleX());
|
2015-08-07 17:27:15 +08:00
|
|
|
s_allEditBoxes[_editBoxIndex] = this;
|
2014-08-19 10:28:24 +08:00
|
|
|
}
|
|
|
|
|
2015-08-31 18:36:27 +08:00
|
|
|
void EditBoxImplAndroid::setNativeFont(const char* pFontName, int fontSize)
|
2014-08-19 10:28:24 +08:00
|
|
|
{
|
2015-09-02 11:04:50 +08:00
|
|
|
auto director = cocos2d::Director::getInstance();
|
|
|
|
auto glView = director->getOpenGLView();
|
|
|
|
setFontEditBoxJNI(_editBoxIndex, pFontName, fontSize * glView->getScaleX());
|
2014-08-19 10:28:24 +08:00
|
|
|
}
|
|
|
|
|
2015-08-31 18:36:27 +08:00
|
|
|
void EditBoxImplAndroid::setNativeFontColor(const Color4B& color)
|
2014-08-19 10:28:24 +08:00
|
|
|
{
|
2015-08-07 17:27:15 +08:00
|
|
|
setFontColorEditBoxJNI(_editBoxIndex, color.r, color.g, color.b, color.a);
|
2014-08-19 10:28:24 +08:00
|
|
|
}
|
|
|
|
|
2015-08-31 18:36:27 +08:00
|
|
|
void EditBoxImplAndroid::setNativePlaceholderFont(const char* pFontName, int fontSize)
|
2014-08-19 10:28:24 +08:00
|
|
|
{
|
2015-08-31 18:36:27 +08:00
|
|
|
CCLOG("Wraning! You can't change Andriod Hint fontName and fontSize");
|
2014-08-19 10:28:24 +08:00
|
|
|
}
|
|
|
|
|
2015-08-31 18:36:27 +08:00
|
|
|
void EditBoxImplAndroid::setNativePlaceholderFontColor(const Color4B& color)
|
2014-08-19 10:28:24 +08:00
|
|
|
{
|
2015-08-07 17:27:15 +08:00
|
|
|
setPlaceHolderTextColorEditBoxJNI(_editBoxIndex, color.r, color.g, color.b, color.a);
|
2014-08-19 10:28:24 +08:00
|
|
|
}
|
|
|
|
|
2015-08-31 18:36:27 +08:00
|
|
|
void EditBoxImplAndroid::setNativeInputMode(EditBox::InputMode inputMode)
|
2014-08-19 10:28:24 +08:00
|
|
|
{
|
2015-08-07 17:27:15 +08:00
|
|
|
setInputModeEditBoxJNI(_editBoxIndex, static_cast<int>(inputMode));
|
2014-08-19 10:28:24 +08:00
|
|
|
}
|
|
|
|
|
2015-08-31 18:36:27 +08:00
|
|
|
void EditBoxImplAndroid::setNativeMaxLength(int maxLength)
|
2014-08-19 10:28:24 +08:00
|
|
|
{
|
2015-08-31 18:36:27 +08:00
|
|
|
setMaxLengthJNI(_editBoxIndex, maxLength);
|
2014-08-19 10:28:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-08-31 18:36:27 +08:00
|
|
|
void EditBoxImplAndroid::setNativeInputFlag(EditBox::InputFlag inputFlag)
|
2014-08-19 10:28:24 +08:00
|
|
|
{
|
2015-08-07 17:27:15 +08:00
|
|
|
setInputFlagEditBoxJNI(_editBoxIndex, static_cast<int>(inputFlag));
|
2014-08-19 10:28:24 +08:00
|
|
|
}
|
|
|
|
|
2015-08-31 18:36:27 +08:00
|
|
|
void EditBoxImplAndroid::setNativeReturnType(EditBox::KeyboardReturnType returnType)
|
2014-08-19 10:28:24 +08:00
|
|
|
{
|
2015-08-07 17:27:15 +08:00
|
|
|
setReturnTypeEditBoxJNI(_editBoxIndex, static_cast<int>(returnType));
|
2014-08-19 10:28:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool EditBoxImplAndroid::isEditing()
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-08-31 18:36:27 +08:00
|
|
|
void EditBoxImplAndroid::setNativeText(const char* pText)
|
2015-08-07 17:27:15 +08:00
|
|
|
{
|
|
|
|
setTextEditBoxJNI(_editBoxIndex, pText);
|
2014-08-19 10:28:24 +08:00
|
|
|
}
|
|
|
|
|
2015-08-31 18:36:27 +08:00
|
|
|
void EditBoxImplAndroid::setNativePlaceHolder(const char* pText)
|
2014-08-19 10:28:24 +08:00
|
|
|
{
|
2015-08-31 18:36:27 +08:00
|
|
|
setPlaceHolderTextEditBoxJNI(_editBoxIndex, pText);
|
2014-08-19 10:28:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-08-31 18:36:27 +08:00
|
|
|
void EditBoxImplAndroid::setNativeVisible(bool visible)
|
2014-08-19 10:28:24 +08:00
|
|
|
{ // don't need to be implemented on android platform.
|
2015-08-07 17:27:15 +08:00
|
|
|
setVisibleEditBoxJNI(_editBoxIndex, visible);
|
2014-08-19 10:28:24 +08:00
|
|
|
}
|
|
|
|
|
2015-08-31 18:36:27 +08:00
|
|
|
void EditBoxImplAndroid::updateNativeFrame(const Rect& rect)
|
|
|
|
{
|
2014-08-19 10:28:24 +08:00
|
|
|
|
2015-08-31 18:36:27 +08:00
|
|
|
setEditBoxViewRectJNI(_editBoxIndex, rect.origin.x, rect.origin.y, rect.size.width, rect.size.height);
|
2014-08-19 10:28:24 +08:00
|
|
|
}
|
|
|
|
|
2015-08-31 18:36:27 +08:00
|
|
|
void EditBoxImplAndroid::nativeOpenKeyboard()
|
2014-08-19 10:28:24 +08:00
|
|
|
{
|
2015-08-07 17:27:15 +08:00
|
|
|
//it will also open up the soft keyboard
|
|
|
|
setVisibleEditBoxJNI(_editBoxIndex,true);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-08-31 18:36:27 +08:00
|
|
|
void EditBoxImplAndroid::nativeCloseKeyboard()
|
2015-08-07 17:27:15 +08:00
|
|
|
{
|
|
|
|
closeEditBoxKeyboardJNI(_editBoxIndex);
|
|
|
|
}
|
|
|
|
|
|
|
|
void editBoxEditingDidBegin(int index)
|
2014-08-19 10:28:24 +08:00
|
|
|
{
|
2015-08-07 17:27:15 +08:00
|
|
|
auto it = s_allEditBoxes.find(index);
|
|
|
|
if (it != s_allEditBoxes.end())
|
2014-08-19 10:28:24 +08:00
|
|
|
{
|
2015-08-07 17:27:15 +08:00
|
|
|
s_allEditBoxes[index]->editBoxEditingDidBegin();
|
2014-08-19 10:28:24 +08:00
|
|
|
}
|
|
|
|
}
|
2015-08-07 17:27:15 +08:00
|
|
|
void editBoxEditingDidChanged(int index, const std::string& text)
|
2014-08-19 10:28:24 +08:00
|
|
|
{
|
2015-08-07 17:27:15 +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
|
|
|
}
|
|
|
|
|
2015-08-07 17:27:15 +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
|
|
|
}
|
2015-08-31 18:36:27 +08:00
|
|
|
|
|
|
|
const char* EditBoxImplAndroid::getNativeDefaultFontName()
|
|
|
|
{
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
2015-08-07 17:27:15 +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) */
|
|
|
|
|