EditBox-android.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /****************************************************************************
  2. Copyright (c) 2018 Xiamen Yaji Software Co., Ltd.
  3. http://www.cocos2d-x.org
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in
  11. all copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  18. THE SOFTWARE.
  19. ****************************************************************************/
  20. #include "EditBox.h"
  21. #include "platform/android/jni/JniHelper.h"
  22. #include "cocos/scripting/js-bindings/jswrapper/SeApi.h"
  23. #include "cocos/scripting/js-bindings/manual/jsb_global.h"
  24. #ifndef JCLS_EDITBOX
  25. #define JCLS_EDITBOX "org/cocos2dx/lib/Cocos2dxEditBox"
  26. #endif
  27. #ifndef ORG_EDITBOX_CLASS_NAME
  28. #define ORG_EDITBOX_CLASS_NAME org_cocos2dx_lib_Cocos2dxEditBox
  29. #endif
  30. #define JNI_EDITBOX(FUNC) JNI_METHOD1(ORG_EDITBOX_CLASS_NAME,FUNC)
  31. NS_CC_BEGIN
  32. void EditBox::show(const cocos2d::EditBox::ShowInfo& showInfo)
  33. {
  34. JniHelper::callStaticVoidMethod(JCLS_EDITBOX,
  35. "showNative",
  36. showInfo.defaultValue,
  37. showInfo.maxLength,
  38. showInfo.isMultiline,
  39. showInfo.confirmHold,
  40. showInfo.confirmType,
  41. showInfo.inputType);
  42. }
  43. void EditBox::hide()
  44. {
  45. JniHelper::callStaticVoidMethod(JCLS_EDITBOX, "hideNative");
  46. }
  47. void EditBox::updateRect(int x, int y, int width, int height)
  48. {
  49. // not supported ...
  50. }
  51. NS_CC_END
  52. namespace
  53. {
  54. se::Value textInputCallback;
  55. void getTextInputCallback()
  56. {
  57. if (! textInputCallback.isUndefined())
  58. return;
  59. auto global = se::ScriptEngine::getInstance()->getGlobalObject();
  60. se::Value jsbVal;
  61. if (global->getProperty("jsb", &jsbVal) && jsbVal.isObject())
  62. {
  63. jsbVal.toObject()->getProperty("onTextInput", &textInputCallback);
  64. // free globle se::Value before ScriptEngine clean up
  65. se::ScriptEngine::getInstance()->addBeforeCleanupHook([](){
  66. textInputCallback.setUndefined();
  67. });
  68. }
  69. }
  70. void callJSFunc(const std::string& type, const jstring& text)
  71. {
  72. getTextInputCallback();
  73. se::AutoHandleScope scope;
  74. se::ValueArray args;
  75. args.push_back(se::Value(type));
  76. args.push_back(se::Value(cocos2d::JniHelper::jstring2string(text)));
  77. textInputCallback.toObject()->call(args, nullptr);
  78. }
  79. }
  80. extern "C"
  81. {
  82. JNIEXPORT void JNICALL JNI_EDITBOX(onKeyboardInputNative)(JNIEnv* env, jclass, jstring text)
  83. {
  84. callJSFunc("input", text);
  85. }
  86. JNIEXPORT void JNICALL JNI_EDITBOX(onKeyboardCompleteNative)(JNIEnv* env, jclass, jstring text)
  87. {
  88. callJSFunc("complete", text);
  89. }
  90. JNIEXPORT void JNICALL JNI_EDITBOX(onKeyboardConfirmNative)(JNIEnv* env, jclass, jstring text)
  91. {
  92. callJSFunc("confirm", text);
  93. }
  94. }