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:
Sam Lantinga 2018-03-15 18:22:48 -07:00
parent 1cfbe664f5
commit e20d4173bf
5 changed files with 291 additions and 8 deletions

View File

@ -3,6 +3,7 @@ package org.libsdl.app;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.util.Arrays; import java.util.Arrays;
import java.util.Hashtable;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import android.app.*; import android.app.*;
@ -37,6 +38,21 @@ public class SDLActivity extends Activity {
public static boolean mIsResumedCalled, mIsSurfaceReady, mHasFocus; public static boolean mIsResumedCalled, mIsSurfaceReady, mHasFocus;
// Cursor types
private static final int SDL_SYSTEM_CURSOR_NONE = -1;
private static final int SDL_SYSTEM_CURSOR_ARROW = 0;
private static final int SDL_SYSTEM_CURSOR_IBEAM = 1;
private static final int SDL_SYSTEM_CURSOR_WAIT = 2;
private static final int SDL_SYSTEM_CURSOR_CROSSHAIR = 3;
private static final int SDL_SYSTEM_CURSOR_WAITARROW = 4;
private static final int SDL_SYSTEM_CURSOR_SIZENWSE = 5;
private static final int SDL_SYSTEM_CURSOR_SIZENESW = 6;
private static final int SDL_SYSTEM_CURSOR_SIZEWE = 7;
private static final int SDL_SYSTEM_CURSOR_SIZENS = 8;
private static final int SDL_SYSTEM_CURSOR_SIZEALL = 9;
private static final int SDL_SYSTEM_CURSOR_NO = 10;
private static final int SDL_SYSTEM_CURSOR_HAND = 11;
// Handle the state of the native layer // Handle the state of the native layer
public enum NativeState { public enum NativeState {
INIT, RESUMED, PAUSED INIT, RESUMED, PAUSED
@ -61,6 +77,10 @@ public class SDLActivity extends Activity {
protected static boolean mScreenKeyboardShown; protected static boolean mScreenKeyboardShown;
protected static ViewGroup mLayout; protected static ViewGroup mLayout;
protected static SDLClipboardHandler mClipboardHandler; protected static SDLClipboardHandler mClipboardHandler;
//#CURSORIMPLEENTATION
//protected static Hashtable<Integer, PointerIcon> mCursors;
//protected static int mLastCursorID;
//protected static PointerIcon mActiveCursor;
// This is what SDL runs in. It invokes SDL_main(), eventually // This is what SDL runs in. It invokes SDL_main(), eventually
@ -133,6 +153,10 @@ public class SDLActivity extends Activity {
mTextEdit = null; mTextEdit = null;
mLayout = null; mLayout = null;
mClipboardHandler = null; mClipboardHandler = null;
//#CURSORIMPLEENTATION
//mCursors = new Hashtable<Integer, PointerIcon>();
//mLastCursorID = 0;
//mActiveCursor = null;
mSDLThread = null; mSDLThread = null;
mExitCalledFromJava = false; mExitCalledFromJava = false;
mBrokenLibraries = false; mBrokenLibraries = false;
@ -1051,7 +1075,7 @@ public class SDLActivity extends Activity {
public static boolean clipboardHasText() { public static boolean clipboardHasText() {
return mClipboardHandler.clipboardHasText(); return mClipboardHandler.clipboardHasText();
} }
/** /**
* This method is called by SDL using JNI. * This method is called by SDL using JNI.
*/ */
@ -1065,6 +1089,73 @@ public class SDLActivity extends Activity {
public static void clipboardSetText(String string) { public static void clipboardSetText(String string) {
mClipboardHandler.clipboardSetText(string); mClipboardHandler.clipboardSetText(string);
} }
/**
* This method is called by SDL using JNI.
*/
/**
* #CURSORIMPLEENTATION
* The cursor implementation requires API 24 or above
*
public static int createCustomCursor(int[] colors, int width, int height, int hotSpotX, int hotSpotY) {
Bitmap bitmap = Bitmap.createBitmap(colors, width, height, Bitmap.Config.ARGB_8888);
++mLastCursorID;
mCursors.put(mLastCursorID, PointerIcon.create(bitmap, hotSpotX, hotSpotY));
return mLastCursorID;
}
public static void setCustomCursor(int cursorID) {
mActiveCursor = mCursors.get(cursorID);
}
public static void setSystemCursor(int cursorID) {
switch (cursorID) {
case SDL_SYSTEM_CURSOR_NONE:
mActiveCursor = PointerIcon.getSystemIcon(SDL.getContext(), PointerIcon.TYPE_NULL);
break;
case SDL_SYSTEM_CURSOR_ARROW:
mActiveCursor = PointerIcon.getSystemIcon(SDL.getContext(), PointerIcon.TYPE_ARROW);
break;
case SDL_SYSTEM_CURSOR_IBEAM:
mActiveCursor = PointerIcon.getSystemIcon(SDL.getContext(), PointerIcon.TYPE_TEXT);
break;
case SDL_SYSTEM_CURSOR_WAIT:
mActiveCursor = PointerIcon.getSystemIcon(SDL.getContext(), PointerIcon.TYPE_WAIT);
break;
case SDL_SYSTEM_CURSOR_CROSSHAIR:
mActiveCursor = PointerIcon.getSystemIcon(SDL.getContext(), PointerIcon.TYPE_CROSSHAIR);
break;
case SDL_SYSTEM_CURSOR_WAITARROW:
mActiveCursor = PointerIcon.getSystemIcon(SDL.getContext(), PointerIcon.TYPE_WAIT);
break;
case SDL_SYSTEM_CURSOR_SIZENWSE:
mActiveCursor = PointerIcon.getSystemIcon(SDL.getContext(), PointerIcon.TYPE_TOP_LEFT_DIAGONAL_DOUBLE_ARROW);
break;
case SDL_SYSTEM_CURSOR_SIZENESW:
mActiveCursor = PointerIcon.getSystemIcon(SDL.getContext(), PointerIcon.TYPE_TOP_RIGHT_DIAGONAL_DOUBLE_ARROW);
break;
case SDL_SYSTEM_CURSOR_SIZEWE:
mActiveCursor = PointerIcon.getSystemIcon(SDL.getContext(), PointerIcon.TYPE_HORIZONTAL_DOUBLE_ARROW);
break;
case SDL_SYSTEM_CURSOR_SIZENS:
mActiveCursor = PointerIcon.getSystemIcon(SDL.getContext(), PointerIcon.TYPE_VERTICAL_DOUBLE_ARROW);
break;
case SDL_SYSTEM_CURSOR_SIZEALL:
mActiveCursor = PointerIcon.getSystemIcon(SDL.getContext(), PointerIcon.TYPE_GRAB);
break;
case SDL_SYSTEM_CURSOR_NO:
mActiveCursor = PointerIcon.getSystemIcon(SDL.getContext(), PointerIcon.TYPE_NO_DROP);
break;
case SDL_SYSTEM_CURSOR_HAND:
mActiveCursor = PointerIcon.getSystemIcon(SDL.getContext(), PointerIcon.TYPE_HAND);
break;
}
}
public static PointerIcon getCursor() {
return mActiveCursor;
}
*/
} }
/** /**
@ -1456,6 +1547,14 @@ class SDLSurface extends SurfaceView implements SurfaceHolder.Callback,
event.values[2] / SensorManager.GRAVITY_EARTH); event.values[2] / SensorManager.GRAVITY_EARTH);
} }
} }
/**
* #CURSORIMPLEENTATION
@Override
public PointerIcon onResolvePointerIcon(MotionEvent event, int pointerIndex) {
return SDLActivity.getCursor();
}
*/
} }
/* This is a fake invisible editor view that receives the input and defines the /* This is a fake invisible editor view that receives the input and defines the

View File

@ -223,6 +223,9 @@ static jmethodID midClipboardHasText;
static jmethodID midOpenAPKExpansionInputStream; static jmethodID midOpenAPKExpansionInputStream;
static jmethodID midGetManifestEnvironmentVariables; static jmethodID midGetManifestEnvironmentVariables;
static jmethodID midGetDisplayDPI; static jmethodID midGetDisplayDPI;
static jmethodID midCreateCustomCursor;
static jmethodID midSetCustomCursor;
static jmethodID midSetSystemCursor;
/* audio manager */ /* audio manager */
static jclass mAudioManagerClass; static jclass mAudioManagerClass;
@ -332,7 +335,11 @@ JNIEXPORT void JNICALL SDL_JAVA_INTERFACE(nativeSetupJNI)(JNIEnv* mEnv, jclass c
"getManifestEnvironmentVariables", "()Z"); "getManifestEnvironmentVariables", "()Z");
midGetDisplayDPI = (*mEnv)->GetStaticMethodID(mEnv, mActivityClass, "getDisplayDPI", "()Landroid/util/DisplayMetrics;"); 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 || if (!midGetNativeSurface ||
!midSetActivityTitle || !midSetWindowStyle || !midSetOrientation || !midGetContext || !midIsAndroidTV || !midInputGetInputDeviceIds || !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__ */ #endif /* __ANDROID__ */
/* vi: set ts=4 sw=4 expandtab: */ /* vi: set ts=4 sw=4 expandtab: */

View File

@ -102,6 +102,11 @@ JNIEXPORT void JNICALL SDL_Android_Init(JNIEnv* mEnv, jclass cls);
#include "SDL_messagebox.h" #include "SDL_messagebox.h"
int Android_JNI_ShowMessageBox(const SDL_MessageBoxData *messageboxdata, int *buttonid); 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++ */ /* Ends C function definitions when using C++ */
#ifdef __cplusplus #ifdef __cplusplus
/* *INDENT-OFF* */ /* *INDENT-OFF* */

View File

@ -42,12 +42,107 @@
#define BUTTON_BACK 8 #define BUTTON_BACK 8
#define BUTTON_FORWARD 16 #define BUTTON_FORWARD 16
typedef struct
{
int custom_cursor;
int system_cursor;
} SDL_AndroidCursorData;
/* Last known Android mouse button state (includes all buttons) */ /* Last known Android mouse button state (includes all buttons) */
static int last_state; 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 void
Android_InitMouse(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; last_state = 0;
} }

View File

@ -73,6 +73,24 @@ init_color_cursor(const char *file)
SDL_Cursor *cursor = NULL; SDL_Cursor *cursor = NULL;
SDL_Surface *surface = SDL_LoadBMP(file); SDL_Surface *surface = SDL_LoadBMP(file);
if (surface) { if (surface) {
if (surface->format->palette) {
SDL_SetColorKey(surface, 1, *(Uint8 *) surface->pixels);
} else {
switch (surface->format->BitsPerPixel) {
case 15:
SDL_SetColorKey(surface, 1, (*(Uint16 *)surface->pixels) & 0x00007FFF);
break;
case 16:
SDL_SetColorKey(surface, 1, *(Uint16 *)surface->pixels);
break;
case 24:
SDL_SetColorKey(surface, 1, (*(Uint32 *)surface->pixels) & 0x00FFFFFF);
break;
case 32:
SDL_SetColorKey(surface, 1, *(Uint32 *)surface->pixels);
break;
}
}
cursor = SDL_CreateColorCursor(surface, 0, 0); cursor = SDL_CreateColorCursor(surface, 0, 0);
SDL_FreeSurface(surface); SDL_FreeSurface(surface);
} }
@ -116,7 +134,9 @@ init_system_cursor(const char *image[])
static SDLTest_CommonState *state; static SDLTest_CommonState *state;
int done; int done;
SDL_Cursor *cursor = NULL; static SDL_Cursor *cursors[1+SDL_NUM_SYSTEM_CURSORS];
static int current_cursor;
static int show_cursor;
/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */ /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
static void static void
@ -134,6 +154,18 @@ loop()
/* Check for events */ /* Check for events */
while (SDL_PollEvent(&event)) { while (SDL_PollEvent(&event)) {
SDLTest_CommonEvent(state, &event, &done); SDLTest_CommonEvent(state, &event, &done);
if (event.type == SDL_MOUSEBUTTONDOWN) {
if (event.button.button == SDL_BUTTON_LEFT) {
++current_cursor;
if (current_cursor == SDL_arraysize(cursors)) {
current_cursor = 0;
}
SDL_SetCursor(cursors[current_cursor]);
} else {
show_cursor = !show_cursor;
SDL_ShowCursor(show_cursor);
}
}
} }
for (i = 0; i < state->num_windows; ++i) { for (i = 0; i < state->num_windows; ++i) {
@ -188,15 +220,22 @@ main(int argc, char *argv[])
} }
if (color_cursor) { if (color_cursor) {
cursor = init_color_cursor(color_cursor); cursors[0] = init_color_cursor(color_cursor);
} else { } else {
cursor = init_system_cursor(arrow); cursors[0] = init_system_cursor(arrow);
} }
if (!cursor) { if (!cursors[0]) {
SDL_Log("Error, couldn't create cursor\n"); SDL_Log("Error, couldn't create cursor\n");
quit(2); quit(2);
} }
SDL_SetCursor(cursor); for (i = 0; i < SDL_NUM_SYSTEM_CURSORS; ++i) {
cursors[1+i] = SDL_CreateSystemCursor((SDL_SystemCursor)i);
if (!cursors[1+i]) {
SDL_Log("Error, couldn't create system cursor %d\n", i);
quit(2);
}
}
SDL_SetCursor(cursors[0]);
/* Main render loop */ /* Main render loop */
done = 0; done = 0;
@ -208,9 +247,13 @@ main(int argc, char *argv[])
} }
#endif #endif
SDL_FreeCursor(cursor); for (i = 0; i < SDL_arraysize(cursors); ++i) {
SDL_FreeCursor(cursors[i]);
}
quit(0); quit(0);
/* keep the compiler happy ... */ /* keep the compiler happy ... */
return(0); return(0);
} }
/* vi: set ts=4 sw=4 expandtab: */