Path: blob/master/platform/android/java_godot_view_wrapper.cpp
10277 views
/**************************************************************************/1/* java_godot_view_wrapper.cpp */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#include "java_godot_view_wrapper.h"3132#include "thread_jandroid.h"3334GodotJavaViewWrapper::GodotJavaViewWrapper(jobject godot_view) {35JNIEnv *env = get_jni_env();36ERR_FAIL_NULL(env);3738_godot_view = env->NewGlobalRef(godot_view);3940_cls = (jclass)env->NewGlobalRef(env->GetObjectClass(godot_view));4142_configure_pointer_icon = env->GetMethodID(_cls, "configurePointerIcon", "(ILjava/lang/String;FF)V");43_set_pointer_icon = env->GetMethodID(_cls, "setPointerIcon", "(I)V");4445int android_device_api_level = android_get_device_api_level();46if (android_device_api_level >= __ANDROID_API_O__) {47_request_pointer_capture = env->GetMethodID(_cls, "requestPointerCapture", "()V");48_release_pointer_capture = env->GetMethodID(_cls, "releasePointerCapture", "()V");49}5051_can_capture_pointer = env->GetMethodID(_cls, "canCapturePointer", "()Z");52}5354bool GodotJavaViewWrapper::can_update_pointer_icon() const {55return _configure_pointer_icon != nullptr && _set_pointer_icon != nullptr;56}5758bool GodotJavaViewWrapper::can_capture_pointer() const {59// We can capture the pointer if the other jni capture method ids are initialized,60// and GodotView#canCapturePointer() returns true.61if (_request_pointer_capture != nullptr && _release_pointer_capture != nullptr && _can_capture_pointer != nullptr) {62JNIEnv *env = get_jni_env();63ERR_FAIL_NULL_V(env, false);6465return env->CallBooleanMethod(_godot_view, _can_capture_pointer);66}6768return false;69}7071void GodotJavaViewWrapper::request_pointer_capture() {72if (_request_pointer_capture != nullptr) {73JNIEnv *env = get_jni_env();74ERR_FAIL_NULL(env);7576env->CallVoidMethod(_godot_view, _request_pointer_capture);77}78}7980void GodotJavaViewWrapper::release_pointer_capture() {81if (_release_pointer_capture != nullptr) {82JNIEnv *env = get_jni_env();83ERR_FAIL_NULL(env);8485env->CallVoidMethod(_godot_view, _release_pointer_capture);86}87}8889void GodotJavaViewWrapper::configure_pointer_icon(int pointer_type, const String &image_path, const Vector2 &p_hotspot) {90if (_configure_pointer_icon != nullptr) {91JNIEnv *env = get_jni_env();92ERR_FAIL_NULL(env);9394jstring jImagePath = env->NewStringUTF(image_path.utf8().get_data());95env->CallVoidMethod(_godot_view, _configure_pointer_icon, pointer_type, jImagePath, p_hotspot.x, p_hotspot.y);96env->DeleteLocalRef(jImagePath);97}98}99100void GodotJavaViewWrapper::set_pointer_icon(int pointer_type) {101if (_set_pointer_icon != nullptr) {102JNIEnv *env = get_jni_env();103ERR_FAIL_NULL(env);104105env->CallVoidMethod(_godot_view, _set_pointer_icon, pointer_type);106}107}108109GodotJavaViewWrapper::~GodotJavaViewWrapper() {110JNIEnv *env = get_jni_env();111ERR_FAIL_NULL(env);112113env->DeleteGlobalRef(_godot_view);114env->DeleteGlobalRef(_cls);115}116117118