UIEditBoxImpl-android.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. /****************************************************************************
  2. Copyright (c) 2010-2012 cocos2d-x.org
  3. Copyright (c) 2012 James Chen
  4. Copyright (c) 2013-2015 zilongshanren
  5. Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
  6. http://www.cocos2d-x.org
  7. Permission is hereby granted, free of charge, to any person obtaining a copy
  8. of this software and associated documentation files (the "Software"), to deal
  9. in the Software without restriction, including without limitation the rights
  10. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. copies of the Software, and to permit persons to whom the Software is
  12. furnished to do so, subject to the following conditions:
  13. The above copyright notice and this permission notice shall be included in
  14. all copies or substantial portions of the Software.
  15. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. THE SOFTWARE.
  22. ****************************************************************************/
  23. #include "ui/UIEditBox/UIEditBoxImpl-android.h"
  24. #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
  25. #include "ui/UIEditBox/UIEditBox.h"
  26. #include <jni.h>
  27. #include "platform/android/jni/JniHelper.h"
  28. #include "2d/CCLabel.h"
  29. #include "base/ccUTF8.h"
  30. #include "math/Vec2.h"
  31. #include "ui/UIHelper.h"
  32. #include "base/CCDirector.h"
  33. #include "platform/CCFileUtils.h"
  34. NS_CC_BEGIN
  35. static const std::string editBoxClassName = "org/cocos2dx/lib/Cocos2dxEditBoxHelper";
  36. namespace ui {
  37. #define LOGD(...) __android_log_print(ANDROID_LOG_ERROR,"",__VA_ARGS__)
  38. static void editBoxEditingDidBegin(int index);
  39. static void editBoxEditingDidChanged(int index, const std::string& text);
  40. static void editBoxEditingDidEnd(int index, const std::string& text, int action);
  41. extern "C"{
  42. JNIEXPORT void JNICALL Java_org_cocos2dx_lib_Cocos2dxEditBoxHelper_editBoxEditingDidBegin(JNIEnv *env, jclass, jint index) {
  43. editBoxEditingDidBegin(index);
  44. }
  45. JNIEXPORT void JNICALL Java_org_cocos2dx_lib_Cocos2dxEditBoxHelper_editBoxEditingChanged(JNIEnv *env, jclass, jint index, jstring text) {
  46. std::string textString = StringUtils::getStringUTFCharsJNI(env,text);
  47. editBoxEditingDidChanged(index, textString);
  48. }
  49. JNIEXPORT void JNICALL Java_org_cocos2dx_lib_Cocos2dxEditBoxHelper_editBoxEditingDidEnd(JNIEnv *env, jclass, jint index, jstring text, jint action) {
  50. std::string textString = StringUtils::getStringUTFCharsJNI(env,text);
  51. editBoxEditingDidEnd(index, textString, action);
  52. }
  53. }
  54. static std::unordered_map<int, EditBoxImplAndroid*> s_allEditBoxes;
  55. EditBoxImpl* __createSystemEditBox(EditBox* editBox)
  56. {
  57. return new EditBoxImplAndroid(editBox);
  58. }
  59. EditBoxImplAndroid::EditBoxImplAndroid(EditBox* pEditText)
  60. : EditBoxImplCommon(pEditText)
  61. , _editBoxIndex(-1)
  62. {
  63. }
  64. EditBoxImplAndroid::~EditBoxImplAndroid()
  65. {
  66. s_allEditBoxes.erase(_editBoxIndex);
  67. JniHelper::callStaticVoidMethod(editBoxClassName, "removeEditBox", _editBoxIndex);
  68. }
  69. void EditBoxImplAndroid::createNativeControl(const Rect& frame)
  70. {
  71. auto director = cocos2d::Director::getInstance();
  72. auto glView = director->getOpenGLView();
  73. auto frameSize = glView->getFrameSize();
  74. auto winSize = director->getWinSize();
  75. auto leftBottom = _editBox->convertToWorldSpace(Point::ZERO);
  76. auto contentSize = frame.size;
  77. auto rightTop = _editBox->convertToWorldSpace(Point(contentSize.width, contentSize.height));
  78. auto uiLeft = frameSize.width / 2 + (leftBottom.x - winSize.width / 2 ) * glView->getScaleX();
  79. auto uiTop = frameSize.height /2 - (rightTop.y - winSize.height / 2) * glView->getScaleY();
  80. auto uiWidth = (rightTop.x - leftBottom.x) * glView->getScaleX();
  81. auto uiHeight = (rightTop.y - leftBottom.y) * glView->getScaleY();
  82. LOGD("scaleX = %f", glView->getScaleX());
  83. _editBoxIndex = JniHelper::callStaticIntMethod(editBoxClassName, "createEditBox",
  84. (int)uiLeft, (int)uiTop, (int)uiWidth, (int)uiHeight,
  85. (float)glView->getScaleX());
  86. s_allEditBoxes[_editBoxIndex] = this;
  87. }
  88. void EditBoxImplAndroid::setNativeFont(const char* pFontName, int fontSize)
  89. {
  90. auto director = cocos2d::Director::getInstance();
  91. auto glView = director->getOpenGLView();
  92. auto isFontFileExists = cocos2d::FileUtils::getInstance()->isFileExist(pFontName);
  93. std::string realFontPath = pFontName;
  94. if(isFontFileExists) {
  95. realFontPath = cocos2d::FileUtils::getInstance()->fullPathForFilename(pFontName);
  96. if (realFontPath.find("assets/") == 0)
  97. {
  98. realFontPath = realFontPath.substr(strlen("assets/")); // Chop out the 'assets/' portion of the path.
  99. }
  100. }
  101. JniHelper::callStaticVoidMethod(editBoxClassName, "setFont",
  102. _editBoxIndex, realFontPath,
  103. (float)fontSize * glView->getScaleX());
  104. }
  105. void EditBoxImplAndroid::setNativeFontColor(const Color4B& color)
  106. {
  107. JniHelper::callStaticVoidMethod(editBoxClassName, "setFontColor", _editBoxIndex,
  108. (int)color.r, (int)color.g, (int)color.b, (int)color.a);
  109. }
  110. void EditBoxImplAndroid::setNativePlaceholderFont(const char* pFontName, int fontSize)
  111. {
  112. CCLOG("Warning! You can't change Android Hint fontName and fontSize");
  113. }
  114. void EditBoxImplAndroid::setNativePlaceholderFontColor(const Color4B& color)
  115. {
  116. JniHelper::callStaticVoidMethod(editBoxClassName, "setPlaceHolderTextColor", _editBoxIndex,
  117. (int)color.r, (int)color.g, (int)color.b, (int)color.a);
  118. }
  119. void EditBoxImplAndroid::setNativeInputMode(EditBox::InputMode inputMode)
  120. {
  121. JniHelper::callStaticVoidMethod(editBoxClassName, "setInputMode",
  122. _editBoxIndex, static_cast<int>(inputMode));
  123. }
  124. void EditBoxImplAndroid::setNativeMaxLength(int maxLength)
  125. {
  126. JniHelper::callStaticVoidMethod(editBoxClassName, "setMaxLength", _editBoxIndex, maxLength);
  127. }
  128. void EditBoxImplAndroid::setNativeInputFlag(EditBox::InputFlag inputFlag)
  129. {
  130. JniHelper::callStaticVoidMethod(editBoxClassName, "setInputFlag",
  131. _editBoxIndex, static_cast<int>(inputFlag));
  132. }
  133. void EditBoxImplAndroid::setNativeReturnType(EditBox::KeyboardReturnType returnType)
  134. {
  135. JniHelper::callStaticVoidMethod(editBoxClassName, "setReturnType",
  136. _editBoxIndex, static_cast<int>(returnType));
  137. }
  138. void EditBoxImplAndroid::setNativeTextHorizontalAlignment(cocos2d::TextHAlignment alignment)
  139. {
  140. JniHelper::callStaticVoidMethod(editBoxClassName, "setTextHorizontalAlignment",
  141. _editBoxIndex, static_cast<int>(alignment));
  142. }
  143. bool EditBoxImplAndroid::isEditing()
  144. {
  145. return false;
  146. }
  147. void EditBoxImplAndroid::setNativeText(const char* pText)
  148. {
  149. JniHelper::callStaticVoidMethod(editBoxClassName, "setText", _editBoxIndex, pText);
  150. }
  151. void EditBoxImplAndroid::setNativePlaceHolder(const char* pText)
  152. {
  153. JniHelper::callStaticVoidMethod(editBoxClassName, "setPlaceHolderText", _editBoxIndex, pText);
  154. }
  155. void EditBoxImplAndroid::setNativeVisible(bool visible)
  156. { // don't need to be implemented on android platform.
  157. JniHelper::callStaticVoidMethod(editBoxClassName, "setVisible", _editBoxIndex, visible);
  158. }
  159. void EditBoxImplAndroid::updateNativeFrame(const Rect& rect)
  160. {
  161. JniHelper::callStaticVoidMethod(editBoxClassName, "setEditBoxViewRect", _editBoxIndex,
  162. (int)rect.origin.x, (int)rect.origin.y,
  163. (int)rect.size.width, (int)rect.size.height);
  164. }
  165. void EditBoxImplAndroid::nativeOpenKeyboard()
  166. {
  167. JniHelper::callStaticVoidMethod(editBoxClassName, "openKeyboard", _editBoxIndex);
  168. }
  169. void EditBoxImplAndroid::nativeCloseKeyboard()
  170. {
  171. JniHelper::callStaticVoidMethod(editBoxClassName, "closeKeyboard", _editBoxIndex);
  172. }
  173. void editBoxEditingDidBegin(int index)
  174. {
  175. auto it = s_allEditBoxes.find(index);
  176. if (it != s_allEditBoxes.end())
  177. {
  178. s_allEditBoxes[index]->editBoxEditingDidBegin();
  179. }
  180. }
  181. void editBoxEditingDidChanged(int index, const std::string& text)
  182. {
  183. auto it = s_allEditBoxes.find(index);
  184. if (it != s_allEditBoxes.end())
  185. {
  186. s_allEditBoxes[index]->editBoxEditingChanged(text);
  187. }
  188. }
  189. void editBoxEditingDidEnd(int index, const std::string& text, int action)
  190. {
  191. auto it = s_allEditBoxes.find(index);
  192. if (it != s_allEditBoxes.end())
  193. {
  194. s_allEditBoxes[index]->editBoxEditingDidEnd(text, static_cast<cocos2d::ui::EditBoxDelegate::EditBoxEndAction>(action));
  195. }
  196. }
  197. const char* EditBoxImplAndroid::getNativeDefaultFontName()
  198. {
  199. return "sans-serif";
  200. }
  201. } //end of ui namespace
  202. NS_CC_END
  203. #endif /* #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) */