2019-11-23 20:27:39 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2010-2012 cocos2d-x.org
|
|
|
|
Copyright (c) 2012 James Chen
|
|
|
|
Copyright (c) 2013-2015 zilongshanren
|
|
|
|
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
2021-07-15 23:03:43 +08:00
|
|
|
Copyright (c) 2021 Bytedance Inc.
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2022-10-01 16:24:52 +08:00
|
|
|
https://axmolengine.github.io/
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
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:
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
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 "ui/UIEditBox/UIEditBoxImpl-android.h"
|
|
|
|
|
2022-07-16 10:43:05 +08:00
|
|
|
#if (AX_TARGET_PLATFORM == AX_PLATFORM_ANDROID)
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
# include "ui/UIEditBox/UIEditBox.h"
|
|
|
|
# include <jni.h>
|
|
|
|
# include "platform/android/jni/JniHelper.h"
|
|
|
|
# include "2d/CCLabel.h"
|
|
|
|
# include "base/ccUTF8.h"
|
|
|
|
# include "math/Vec2.h"
|
|
|
|
# include "ui/UIHelper.h"
|
|
|
|
# include "base/CCDirector.h"
|
|
|
|
# include "platform/CCFileUtils.h"
|
|
|
|
# include "yasio/cxx17/string_view.hpp"
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
NS_AX_BEGIN
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2022-10-01 16:24:52 +08:00
|
|
|
static const char* editBoxClassName = "org.axmol.lib.EditBoxHelper";
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
namespace ui
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
# define LOGD(...) __android_log_print(ANDROID_LOG_ERROR, "", __VA_ARGS__)
|
2019-11-23 20:27:39 +08:00
|
|
|
static void editBoxEditingDidBegin(int index);
|
2021-12-31 12:12:40 +08:00
|
|
|
static void editBoxEditingDidChanged(int index, std::string_view text);
|
|
|
|
static void editBoxEditingDidEnd(int index, std::string_view text, int action);
|
2021-12-25 10:04:45 +08:00
|
|
|
extern "C" {
|
2022-10-01 16:24:52 +08:00
|
|
|
JNIEXPORT void JNICALL Java_org_axmol_lib_EditBoxHelper_editBoxEditingDidBegin(JNIEnv*, jclass, jint index)
|
2021-12-25 10:04:45 +08:00
|
|
|
{
|
|
|
|
editBoxEditingDidBegin(index);
|
|
|
|
}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2022-10-01 16:24:52 +08:00
|
|
|
JNIEXPORT void JNICALL Java_org_axmol_lib_EditBoxHelper_editBoxEditingChanged(JNIEnv* env,
|
2021-12-25 10:04:45 +08:00
|
|
|
jclass,
|
|
|
|
jint index,
|
|
|
|
jstring text)
|
|
|
|
{
|
|
|
|
std::string textString = StringUtils::getStringUTFCharsJNI(env, text);
|
|
|
|
editBoxEditingDidChanged(index, textString);
|
|
|
|
}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2022-10-01 16:24:52 +08:00
|
|
|
JNIEXPORT void JNICALL Java_org_axmol_lib_EditBoxHelper_editBoxEditingDidEnd(JNIEnv* env,
|
2021-12-25 10:04:45 +08:00
|
|
|
jclass,
|
|
|
|
jint index,
|
|
|
|
jstring text,
|
|
|
|
jint action)
|
|
|
|
{
|
|
|
|
std::string textString = StringUtils::getStringUTFCharsJNI(env, text);
|
|
|
|
editBoxEditingDidEnd(index, textString, action);
|
|
|
|
}
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static std::unordered_map<int, EditBoxImplAndroid*> s_allEditBoxes;
|
|
|
|
|
|
|
|
EditBoxImpl* __createSystemEditBox(EditBox* editBox)
|
|
|
|
{
|
|
|
|
return new EditBoxImplAndroid(editBox);
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
EditBoxImplAndroid::EditBoxImplAndroid(EditBox* pEditText) : EditBoxImplCommon(pEditText), _editBoxIndex(-1) {}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
EditBoxImplAndroid::~EditBoxImplAndroid()
|
|
|
|
{
|
|
|
|
s_allEditBoxes.erase(_editBoxIndex);
|
2021-12-29 16:08:20 +08:00
|
|
|
JniHelper::callStaticVoidMethod(editBoxClassName, "removeEditBox", _editBoxIndex);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void EditBoxImplAndroid::createNativeControl(const Rect& frame)
|
|
|
|
{
|
2022-08-08 18:02:17 +08:00
|
|
|
auto director = ax::Director::getInstance();
|
2021-12-25 10:04:45 +08:00
|
|
|
auto glView = director->getOpenGLView();
|
2019-11-23 20:27:39 +08:00
|
|
|
auto frameSize = glView->getFrameSize();
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
auto winSize = director->getWinSize();
|
2019-11-23 20:27:39 +08:00
|
|
|
auto leftBottom = _editBox->convertToWorldSpace(Point::ZERO);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
auto contentSize = frame.size;
|
2021-12-25 10:04:45 +08:00
|
|
|
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();
|
2019-11-23 20:27:39 +08:00
|
|
|
auto uiHeight = (rightTop.y - leftBottom.y) * glView->getScaleY();
|
|
|
|
LOGD("scaleX = %f", glView->getScaleX());
|
2021-12-29 16:08:20 +08:00
|
|
|
_editBoxIndex = JniHelper::callStaticIntMethod(editBoxClassName, "createEditBox", (int)uiLeft, (int)uiTop,
|
2021-12-25 10:04:45 +08:00
|
|
|
(int)uiWidth, (int)uiHeight, (float)glView->getScaleX());
|
2019-11-23 20:27:39 +08:00
|
|
|
s_allEditBoxes[_editBoxIndex] = this;
|
|
|
|
}
|
|
|
|
|
|
|
|
void EditBoxImplAndroid::setNativeFont(const char* pFontName, int fontSize)
|
|
|
|
{
|
2022-08-08 18:02:17 +08:00
|
|
|
auto director = ax::Director::getInstance();
|
2021-12-25 10:04:45 +08:00
|
|
|
auto glView = director->getOpenGLView();
|
2022-08-08 18:02:17 +08:00
|
|
|
auto isFontFileExists = ax::FileUtils::getInstance()->isFileExist(pFontName);
|
2019-11-23 20:27:39 +08:00
|
|
|
std::string realFontPath = pFontName;
|
2021-12-25 10:04:45 +08:00
|
|
|
if (isFontFileExists)
|
|
|
|
{
|
2022-08-08 18:02:17 +08:00
|
|
|
realFontPath = ax::FileUtils::getInstance()->fullPathForFilename(pFontName);
|
2021-12-25 10:04:45 +08:00
|
|
|
using namespace cxx17; // for cxx17::string_view literal
|
2021-07-15 23:03:43 +08:00
|
|
|
if (cxx20::starts_with(cxx17::string_view{realFontPath}, "assets/"_sv))
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
realFontPath = realFontPath.substr(sizeof("assets/") - 1); // Chop out the 'assets/' portion of the path.
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
}
|
2021-12-29 16:08:20 +08:00
|
|
|
JniHelper::callStaticVoidMethod(editBoxClassName, "setFont", _editBoxIndex, realFontPath,
|
2019-11-23 20:27:39 +08:00
|
|
|
(float)fontSize * glView->getScaleX());
|
|
|
|
}
|
|
|
|
|
|
|
|
void EditBoxImplAndroid::setNativeFontColor(const Color4B& color)
|
|
|
|
{
|
2021-12-29 16:08:20 +08:00
|
|
|
JniHelper::callStaticVoidMethod(editBoxClassName, "setFontColor", _editBoxIndex, (int)color.r, (int)color.g,
|
2021-12-25 10:04:45 +08:00
|
|
|
(int)color.b, (int)color.a);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void EditBoxImplAndroid::setNativePlaceholderFont(const char* pFontName, int fontSize)
|
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AXLOG("Warning! You can't change Android Hint fontName and fontSize");
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void EditBoxImplAndroid::setNativePlaceholderFontColor(const Color4B& color)
|
|
|
|
{
|
2021-12-29 16:08:20 +08:00
|
|
|
JniHelper::callStaticVoidMethod(editBoxClassName, "setPlaceHolderTextColor", _editBoxIndex, (int)color.r,
|
2021-12-25 10:04:45 +08:00
|
|
|
(int)color.g, (int)color.b, (int)color.a);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void EditBoxImplAndroid::setNativeInputMode(EditBox::InputMode inputMode)
|
|
|
|
{
|
2021-12-29 16:08:20 +08:00
|
|
|
JniHelper::callStaticVoidMethod(editBoxClassName, "setInputMode", _editBoxIndex, static_cast<int>(inputMode));
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void EditBoxImplAndroid::setNativeMaxLength(int maxLength)
|
|
|
|
{
|
2021-12-29 16:08:20 +08:00
|
|
|
JniHelper::callStaticVoidMethod(editBoxClassName, "setMaxLength", _editBoxIndex, maxLength);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void EditBoxImplAndroid::setNativeInputFlag(EditBox::InputFlag inputFlag)
|
|
|
|
{
|
2021-12-29 16:08:20 +08:00
|
|
|
JniHelper::callStaticVoidMethod(editBoxClassName, "setInputFlag", _editBoxIndex, static_cast<int>(inputFlag));
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void EditBoxImplAndroid::setNativeReturnType(EditBox::KeyboardReturnType returnType)
|
|
|
|
{
|
2021-12-29 16:08:20 +08:00
|
|
|
JniHelper::callStaticVoidMethod(editBoxClassName, "setReturnType", _editBoxIndex, static_cast<int>(returnType));
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2022-08-08 18:02:17 +08:00
|
|
|
void EditBoxImplAndroid::setNativeTextHorizontalAlignment(ax::TextHAlignment alignment)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-29 16:08:20 +08:00
|
|
|
JniHelper::callStaticVoidMethod(editBoxClassName, "setTextHorizontalAlignment", _editBoxIndex,
|
2021-12-25 10:04:45 +08:00
|
|
|
static_cast<int>(alignment));
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool EditBoxImplAndroid::isEditing()
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void EditBoxImplAndroid::setNativeText(const char* pText)
|
|
|
|
{
|
2021-12-29 16:08:20 +08:00
|
|
|
JniHelper::callStaticVoidMethod(editBoxClassName, "setText", _editBoxIndex, pText);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void EditBoxImplAndroid::setNativePlaceHolder(const char* pText)
|
|
|
|
{
|
2021-12-29 16:08:20 +08:00
|
|
|
JniHelper::callStaticVoidMethod(editBoxClassName, "setPlaceHolderText", _editBoxIndex, pText);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void EditBoxImplAndroid::setNativeVisible(bool visible)
|
2021-12-25 10:04:45 +08:00
|
|
|
{ // don't need to be implemented on android platform.
|
2021-12-29 16:08:20 +08:00
|
|
|
JniHelper::callStaticVoidMethod(editBoxClassName, "setVisible", _editBoxIndex, visible);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void EditBoxImplAndroid::updateNativeFrame(const Rect& rect)
|
|
|
|
{
|
2021-12-29 16:08:20 +08:00
|
|
|
JniHelper::callStaticVoidMethod(editBoxClassName, "setEditBoxViewRect", _editBoxIndex, (int)rect.origin.x,
|
2021-12-25 10:04:45 +08:00
|
|
|
(int)rect.origin.y, (int)rect.size.width, (int)rect.size.height);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void EditBoxImplAndroid::nativeOpenKeyboard()
|
|
|
|
{
|
2021-12-29 16:08:20 +08:00
|
|
|
JniHelper::callStaticVoidMethod(editBoxClassName, "openKeyboard", _editBoxIndex);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void EditBoxImplAndroid::nativeCloseKeyboard()
|
|
|
|
{
|
2021-12-29 16:08:20 +08:00
|
|
|
JniHelper::callStaticVoidMethod(editBoxClassName, "closeKeyboard", _editBoxIndex);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void editBoxEditingDidBegin(int index)
|
|
|
|
{
|
|
|
|
auto it = s_allEditBoxes.find(index);
|
|
|
|
if (it != s_allEditBoxes.end())
|
|
|
|
{
|
|
|
|
s_allEditBoxes[index]->editBoxEditingDidBegin();
|
|
|
|
}
|
|
|
|
}
|
2021-12-31 12:12:40 +08:00
|
|
|
void editBoxEditingDidChanged(int index, std::string_view text)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
auto it = s_allEditBoxes.find(index);
|
|
|
|
if (it != s_allEditBoxes.end())
|
|
|
|
{
|
|
|
|
s_allEditBoxes[index]->editBoxEditingChanged(text);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
void editBoxEditingDidEnd(int index, std::string_view text, int action)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
auto it = s_allEditBoxes.find(index);
|
|
|
|
if (it != s_allEditBoxes.end())
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
s_allEditBoxes[index]->editBoxEditingDidEnd(
|
2022-08-08 18:02:17 +08:00
|
|
|
text, static_cast<ax::ui::EditBoxDelegate::EditBoxEndAction>(action));
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const char* EditBoxImplAndroid::getNativeDefaultFontName()
|
|
|
|
{
|
|
|
|
return "sans-serif";
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
} // namespace ui
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
NS_AX_END
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2022-07-16 10:43:05 +08:00
|
|
|
#endif /* #if (AX_TARGET_PLATFORM == AX_PLATFORM_ANDROID) */
|