mirror of
https://github.com/encounter/SDL.git
synced 2025-12-10 05:57:44 +00:00
Added Android custom cursor implementation
This is commented out in SDLActivity.java, with the note #CURSORIMPLEENTATION because it requires API 24, which is higher than the minimum required SDK
This commit is contained in:
@@ -223,6 +223,9 @@ static jmethodID midClipboardHasText;
|
||||
static jmethodID midOpenAPKExpansionInputStream;
|
||||
static jmethodID midGetManifestEnvironmentVariables;
|
||||
static jmethodID midGetDisplayDPI;
|
||||
static jmethodID midCreateCustomCursor;
|
||||
static jmethodID midSetCustomCursor;
|
||||
static jmethodID midSetSystemCursor;
|
||||
|
||||
/* audio manager */
|
||||
static jclass mAudioManagerClass;
|
||||
@@ -332,7 +335,11 @@ JNIEXPORT void JNICALL SDL_JAVA_INTERFACE(nativeSetupJNI)(JNIEnv* mEnv, jclass c
|
||||
"getManifestEnvironmentVariables", "()Z");
|
||||
|
||||
midGetDisplayDPI = (*mEnv)->GetStaticMethodID(mEnv, mActivityClass, "getDisplayDPI", "()Landroid/util/DisplayMetrics;");
|
||||
midGetDisplayDPI = (*mEnv)->GetStaticMethodID(mEnv, mActivityClass, "getDisplayDPI", "()Landroid/util/DisplayMetrics;");
|
||||
|
||||
/* Custom cursor implementation is only available on API 24 and above */
|
||||
midCreateCustomCursor = (*mEnv)->GetStaticMethodID(mEnv, mActivityClass, "createCustomCursor", "([IIIII)I");
|
||||
midSetCustomCursor = (*mEnv)->GetStaticMethodID(mEnv, mActivityClass, "setCustomCursor", "(I)V");
|
||||
midSetSystemCursor = (*mEnv)->GetStaticMethodID(mEnv, mActivityClass, "setSystemCursor", "(I)V");
|
||||
|
||||
if (!midGetNativeSurface ||
|
||||
!midSetActivityTitle || !midSetWindowStyle || !midSetOrientation || !midGetContext || !midIsAndroidTV || !midInputGetInputDeviceIds ||
|
||||
@@ -2167,6 +2174,40 @@ void Android_JNI_GetManifestEnvironmentVariables(void)
|
||||
}
|
||||
}
|
||||
|
||||
int Android_JNI_CreateCustomCursor(SDL_Surface *surface, int hot_x, int hot_y)
|
||||
{
|
||||
JNIEnv *mEnv = Android_JNI_GetEnv();
|
||||
int custom_cursor = 0;
|
||||
if (midCreateCustomCursor) {
|
||||
jintArray pixels;
|
||||
pixels = (*mEnv)->NewIntArray(mEnv, surface->w * surface->h);
|
||||
if (!pixels) {
|
||||
return 0;
|
||||
}
|
||||
(*mEnv)->SetIntArrayRegion(mEnv, pixels, 0, surface->w * surface->h, (int *)surface->pixels);
|
||||
custom_cursor = (*mEnv)->CallStaticIntMethod(mEnv, mActivityClass, midCreateCustomCursor, pixels, surface->w, surface->h, hot_x, hot_y);
|
||||
(*mEnv)->DeleteLocalRef(mEnv, pixels);
|
||||
}
|
||||
return custom_cursor;
|
||||
}
|
||||
|
||||
|
||||
void Android_JNI_SetCustomCursor(int cursorID)
|
||||
{
|
||||
JNIEnv *mEnv = Android_JNI_GetEnv();
|
||||
if (midSetCustomCursor) {
|
||||
(*mEnv)->CallStaticVoidMethod(mEnv, mActivityClass, midSetCustomCursor, cursorID);
|
||||
}
|
||||
}
|
||||
|
||||
void Android_JNI_SetSystemCursor(int cursorID)
|
||||
{
|
||||
JNIEnv *mEnv = Android_JNI_GetEnv();
|
||||
if (midSetSystemCursor) {
|
||||
(*mEnv)->CallStaticVoidMethod(mEnv, mActivityClass, midSetSystemCursor, cursorID);
|
||||
}
|
||||
}
|
||||
|
||||
#endif /* __ANDROID__ */
|
||||
|
||||
/* vi: set ts=4 sw=4 expandtab: */
|
||||
|
||||
@@ -102,6 +102,11 @@ JNIEXPORT void JNICALL SDL_Android_Init(JNIEnv* mEnv, jclass cls);
|
||||
#include "SDL_messagebox.h"
|
||||
int Android_JNI_ShowMessageBox(const SDL_MessageBoxData *messageboxdata, int *buttonid);
|
||||
|
||||
/* Cursor support */
|
||||
int Android_JNI_CreateCustomCursor(SDL_Surface *surface, int hot_x, int hot_y);
|
||||
void Android_JNI_SetCustomCursor(int cursorID);
|
||||
void Android_JNI_SetSystemCursor(int cursorID);
|
||||
|
||||
/* Ends C function definitions when using C++ */
|
||||
#ifdef __cplusplus
|
||||
/* *INDENT-OFF* */
|
||||
|
||||
@@ -42,12 +42,107 @@
|
||||
#define BUTTON_BACK 8
|
||||
#define BUTTON_FORWARD 16
|
||||
|
||||
typedef struct
|
||||
{
|
||||
int custom_cursor;
|
||||
int system_cursor;
|
||||
|
||||
} SDL_AndroidCursorData;
|
||||
|
||||
/* Last known Android mouse button state (includes all buttons) */
|
||||
static int last_state;
|
||||
|
||||
|
||||
static SDL_Cursor *
|
||||
Android_WrapCursor(int custom_cursor, int system_cursor)
|
||||
{
|
||||
SDL_Cursor *cursor;
|
||||
|
||||
cursor = SDL_calloc(1, sizeof(*cursor));
|
||||
if (cursor) {
|
||||
SDL_AndroidCursorData *data = (SDL_AndroidCursorData*)SDL_calloc(1, sizeof(*data));
|
||||
if (data) {
|
||||
data->custom_cursor = custom_cursor;
|
||||
data->system_cursor = system_cursor;
|
||||
cursor->driverdata = data;
|
||||
} else {
|
||||
SDL_free(cursor);
|
||||
cursor = NULL;
|
||||
SDL_OutOfMemory();
|
||||
}
|
||||
} else {
|
||||
SDL_OutOfMemory();
|
||||
}
|
||||
|
||||
return cursor;
|
||||
}
|
||||
|
||||
static SDL_Cursor *
|
||||
Android_CreateDefaultCursor()
|
||||
{
|
||||
return Android_WrapCursor(0, SDL_SYSTEM_CURSOR_ARROW);
|
||||
}
|
||||
|
||||
static SDL_Cursor *
|
||||
Android_CreateCursor(SDL_Surface * surface, int hot_x, int hot_y)
|
||||
{
|
||||
int custom_cursor;
|
||||
SDL_Surface *converted;
|
||||
|
||||
converted = SDL_ConvertSurfaceFormat(surface, SDL_PIXELFORMAT_ARGB8888, 0);
|
||||
if (!converted) {
|
||||
return NULL;
|
||||
}
|
||||
custom_cursor = Android_JNI_CreateCustomCursor(converted, hot_x, hot_y);
|
||||
SDL_FreeSurface(converted);
|
||||
if (!custom_cursor) {
|
||||
SDL_Unsupported();
|
||||
return NULL;
|
||||
}
|
||||
return Android_WrapCursor(custom_cursor, 0);
|
||||
}
|
||||
|
||||
static SDL_Cursor *
|
||||
Android_CreateSystemCursor(SDL_SystemCursor id)
|
||||
{
|
||||
return Android_WrapCursor(0, id);
|
||||
}
|
||||
|
||||
static void
|
||||
Android_FreeCursor(SDL_Cursor * cursor)
|
||||
{
|
||||
SDL_free(cursor->driverdata);
|
||||
SDL_free(cursor);
|
||||
}
|
||||
|
||||
static int
|
||||
Android_ShowCursor(SDL_Cursor * cursor)
|
||||
{
|
||||
if (cursor) {
|
||||
SDL_AndroidCursorData *data = (SDL_AndroidCursorData*)cursor->driverdata;
|
||||
if (data->custom_cursor) {
|
||||
Android_JNI_SetCustomCursor(data->custom_cursor);
|
||||
} else {
|
||||
Android_JNI_SetSystemCursor(data->system_cursor);
|
||||
}
|
||||
} else {
|
||||
Android_JNI_SetSystemCursor(-1);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
void
|
||||
Android_InitMouse(void)
|
||||
{
|
||||
SDL_Mouse *mouse = SDL_GetMouse();
|
||||
|
||||
mouse->CreateCursor = Android_CreateCursor;
|
||||
mouse->CreateSystemCursor = Android_CreateSystemCursor;
|
||||
mouse->ShowCursor = Android_ShowCursor;
|
||||
mouse->FreeCursor = Android_FreeCursor;
|
||||
|
||||
SDL_SetDefaultCursor(Android_CreateDefaultCursor());
|
||||
|
||||
last_state = 0;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user