| 1 | /**************************************************************************/ |
| 2 | /* java_godot_view_wrapper.cpp */ |
| 3 | /**************************************************************************/ |
| 4 | /* This file is part of: */ |
| 5 | /* GODOT ENGINE */ |
| 6 | /* https://godotengine.org */ |
| 7 | /**************************************************************************/ |
| 8 | /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ |
| 9 | /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ |
| 10 | /* */ |
| 11 | /* Permission is hereby granted, free of charge, to any person obtaining */ |
| 12 | /* a copy of this software and associated documentation files (the */ |
| 13 | /* "Software"), to deal in the Software without restriction, including */ |
| 14 | /* without limitation the rights to use, copy, modify, merge, publish, */ |
| 15 | /* distribute, sublicense, and/or sell copies of the Software, and to */ |
| 16 | /* permit persons to whom the Software is furnished to do so, subject to */ |
| 17 | /* the following conditions: */ |
| 18 | /* */ |
| 19 | /* The above copyright notice and this permission notice shall be */ |
| 20 | /* included in all copies or substantial portions of the Software. */ |
| 21 | /* */ |
| 22 | /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ |
| 23 | /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ |
| 24 | /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ |
| 25 | /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ |
| 26 | /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ |
| 27 | /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ |
| 28 | /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ |
| 29 | /**************************************************************************/ |
| 30 | |
| 31 | #include "java_godot_view_wrapper.h" |
| 32 | |
| 33 | #include "thread_jandroid.h" |
| 34 | |
| 35 | GodotJavaViewWrapper::GodotJavaViewWrapper(jobject godot_view) { |
| 36 | JNIEnv *env = get_jni_env(); |
| 37 | ERR_FAIL_NULL(env); |
| 38 | |
| 39 | _godot_view = env->NewGlobalRef(godot_view); |
| 40 | |
| 41 | _cls = (jclass)env->NewGlobalRef(env->GetObjectClass(godot_view)); |
| 42 | |
| 43 | _configure_pointer_icon = env->GetMethodID(_cls, "configurePointerIcon", "(ILjava/lang/String;FF)V"); |
| 44 | _set_pointer_icon = env->GetMethodID(_cls, "setPointerIcon", "(I)V"); |
| 45 | |
| 46 | int android_device_api_level = android_get_device_api_level(); |
| 47 | if (android_device_api_level >= __ANDROID_API_O__) { |
| 48 | _request_pointer_capture = env->GetMethodID(_cls, "requestPointerCapture", "()V"); |
| 49 | _release_pointer_capture = env->GetMethodID(_cls, "releasePointerCapture", "()V"); |
| 50 | } |
| 51 | |
| 52 | _can_capture_pointer = env->GetMethodID(_cls, "canCapturePointer", "()Z"); |
| 53 | } |
| 54 | |
| 55 | bool GodotJavaViewWrapper::can_update_pointer_icon() const { |
| 56 | return _configure_pointer_icon != nullptr && _set_pointer_icon != nullptr; |
| 57 | } |
| 58 | |
| 59 | bool GodotJavaViewWrapper::can_capture_pointer() const { |
| 60 | // We can capture the pointer if the other jni capture method ids are initialized, |
| 61 | // and GodotView#canCapturePointer() returns true. |
| 62 | if (_request_pointer_capture != nullptr && _release_pointer_capture != nullptr && _can_capture_pointer != nullptr) { |
| 63 | JNIEnv *env = get_jni_env(); |
| 64 | ERR_FAIL_NULL_V(env, false); |
| 65 | |
| 66 | return env->CallBooleanMethod(_godot_view, _can_capture_pointer); |
| 67 | } |
| 68 | |
| 69 | return false; |
| 70 | } |
| 71 | |
| 72 | void GodotJavaViewWrapper::request_pointer_capture() { |
| 73 | if (_request_pointer_capture != nullptr) { |
| 74 | JNIEnv *env = get_jni_env(); |
| 75 | ERR_FAIL_NULL(env); |
| 76 | |
| 77 | env->CallVoidMethod(_godot_view, _request_pointer_capture); |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | void GodotJavaViewWrapper::release_pointer_capture() { |
| 82 | if (_release_pointer_capture != nullptr) { |
| 83 | JNIEnv *env = get_jni_env(); |
| 84 | ERR_FAIL_NULL(env); |
| 85 | |
| 86 | env->CallVoidMethod(_godot_view, _release_pointer_capture); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | void GodotJavaViewWrapper::configure_pointer_icon(int pointer_type, const String &image_path, const Vector2 &p_hotspot) { |
| 91 | if (_configure_pointer_icon != nullptr) { |
| 92 | JNIEnv *env = get_jni_env(); |
| 93 | ERR_FAIL_NULL(env); |
| 94 | |
| 95 | jstring jImagePath = env->NewStringUTF(image_path.utf8().get_data()); |
| 96 | env->CallVoidMethod(_godot_view, _configure_pointer_icon, pointer_type, jImagePath, p_hotspot.x, p_hotspot.y); |
| 97 | env->DeleteLocalRef(jImagePath); |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | void GodotJavaViewWrapper::set_pointer_icon(int pointer_type) { |
| 102 | if (_set_pointer_icon != nullptr) { |
| 103 | JNIEnv *env = get_jni_env(); |
| 104 | ERR_FAIL_NULL(env); |
| 105 | |
| 106 | env->CallVoidMethod(_godot_view, _set_pointer_icon, pointer_type); |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | GodotJavaViewWrapper::~GodotJavaViewWrapper() { |
| 111 | JNIEnv *env = get_jni_env(); |
| 112 | ERR_FAIL_NULL(env); |
| 113 | |
| 114 | env->DeleteGlobalRef(_godot_view); |
| 115 | env->DeleteGlobalRef(_cls); |
| 116 | } |