TouchesJni.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /****************************************************************************
  2. Copyright (c) 2010 cocos2d-x.org
  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 "base/CCDirector.h"
  21. #include "base/CCEventKeyboard.h"
  22. #include "base/CCEventDispatcher.h"
  23. #include "platform/android/CCGLViewImpl-android.h"
  24. #include <android/log.h>
  25. #include <jni.h>
  26. using namespace cocos2d;
  27. extern "C" {
  28. JNIEXPORT void JNICALL Java_org_cocos2dx_lib_Cocos2dxRenderer_nativeTouchesBegin(JNIEnv * env, jobject thiz, jint id, jfloat x, jfloat y) {
  29. intptr_t idlong = id;
  30. cocos2d::Director::getInstance()->getOpenGLView()->handleTouchesBegin(1, &idlong, &x, &y);
  31. }
  32. JNIEXPORT void JNICALL Java_org_cocos2dx_lib_Cocos2dxRenderer_nativeTouchesEnd(JNIEnv * env, jobject thiz, jint id, jfloat x, jfloat y) {
  33. intptr_t idlong = id;
  34. cocos2d::Director::getInstance()->getOpenGLView()->handleTouchesEnd(1, &idlong, &x, &y);
  35. }
  36. JNIEXPORT void JNICALL Java_org_cocos2dx_lib_Cocos2dxRenderer_nativeTouchesMove(JNIEnv * env, jobject thiz, jintArray ids, jfloatArray xs, jfloatArray ys) {
  37. int size = env->GetArrayLength(ids);
  38. jint id[size];
  39. jfloat x[size];
  40. jfloat y[size];
  41. env->GetIntArrayRegion(ids, 0, size, id);
  42. env->GetFloatArrayRegion(xs, 0, size, x);
  43. env->GetFloatArrayRegion(ys, 0, size, y);
  44. intptr_t idlong[size];
  45. for(int i = 0; i < size; i++)
  46. idlong[i] = id[i];
  47. cocos2d::Director::getInstance()->getOpenGLView()->handleTouchesMove(size, idlong, x, y);
  48. }
  49. JNIEXPORT void JNICALL Java_org_cocos2dx_lib_Cocos2dxRenderer_nativeTouchesCancel(JNIEnv * env, jobject thiz, jintArray ids, jfloatArray xs, jfloatArray ys) {
  50. int size = env->GetArrayLength(ids);
  51. jint id[size];
  52. jfloat x[size];
  53. jfloat y[size];
  54. env->GetIntArrayRegion(ids, 0, size, id);
  55. env->GetFloatArrayRegion(xs, 0, size, x);
  56. env->GetFloatArrayRegion(ys, 0, size, y);
  57. intptr_t idlong[size];
  58. for(int i = 0; i < size; i++)
  59. idlong[i] = id[i];
  60. cocos2d::Director::getInstance()->getOpenGLView()->handleTouchesCancel(size, idlong, x, y);
  61. }
  62. #define KEYCODE_BACK 0x04
  63. #define KEYCODE_MENU 0x52
  64. #define KEYCODE_DPAD_UP 0x13
  65. #define KEYCODE_DPAD_DOWN 0x14
  66. #define KEYCODE_DPAD_LEFT 0x15
  67. #define KEYCODE_DPAD_RIGHT 0x16
  68. #define KEYCODE_ENTER 0x42
  69. #define KEYCODE_PLAY 0x7e
  70. #define KEYCODE_DPAD_CENTER 0x17
  71. static std::unordered_map<int, cocos2d::EventKeyboard::KeyCode> g_keyCodeMap = {
  72. { KEYCODE_BACK , cocos2d::EventKeyboard::KeyCode::KEY_ESCAPE},
  73. { KEYCODE_MENU , cocos2d::EventKeyboard::KeyCode::KEY_MENU},
  74. { KEYCODE_DPAD_UP , cocos2d::EventKeyboard::KeyCode::KEY_DPAD_UP },
  75. { KEYCODE_DPAD_DOWN , cocos2d::EventKeyboard::KeyCode::KEY_DPAD_DOWN },
  76. { KEYCODE_DPAD_LEFT , cocos2d::EventKeyboard::KeyCode::KEY_DPAD_LEFT },
  77. { KEYCODE_DPAD_RIGHT , cocos2d::EventKeyboard::KeyCode::KEY_DPAD_RIGHT },
  78. { KEYCODE_ENTER , cocos2d::EventKeyboard::KeyCode::KEY_ENTER},
  79. { KEYCODE_PLAY , cocos2d::EventKeyboard::KeyCode::KEY_PLAY},
  80. { KEYCODE_DPAD_CENTER , cocos2d::EventKeyboard::KeyCode::KEY_DPAD_CENTER},
  81. };
  82. JNIEXPORT jboolean JNICALL Java_org_cocos2dx_lib_Cocos2dxRenderer_nativeKeyEvent(JNIEnv * env, jobject thiz, jint keyCode, jboolean isPressed) {
  83. Director* pDirector = Director::getInstance();
  84. auto iterKeyCode = g_keyCodeMap.find(keyCode);
  85. if (iterKeyCode == g_keyCodeMap.end()) {
  86. return JNI_FALSE;
  87. }
  88. cocos2d::EventKeyboard::KeyCode cocos2dKey = g_keyCodeMap.at(keyCode);
  89. cocos2d::EventKeyboard event(cocos2dKey, isPressed);
  90. cocos2d::Director::getInstance()->getEventDispatcher()->dispatchEvent(&event);
  91. return JNI_TRUE;
  92. }}