Skip to content

Commit

Permalink
Added Android custom cursor implementation
Browse files Browse the repository at this point in the history
This is commented out in SDLActivity.java, with the note #CURSORIMPLEENTATION because it requires API 24, which is higher than the minimum required SDK
  • Loading branch information
slouken committed Mar 16, 2018
1 parent 1cfbe66 commit e20d417
Show file tree
Hide file tree
Showing 5 changed files with 291 additions and 8 deletions.
101 changes: 100 additions & 1 deletion android-project/app/src/main/java/org/libsdl/app/SDLActivity.java
Expand Up @@ -3,6 +3,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.Hashtable;
import java.lang.reflect.Method;

import android.app.*;
Expand Down Expand Up @@ -37,6 +38,21 @@ public class SDLActivity extends Activity {

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
public enum NativeState {
INIT, RESUMED, PAUSED
Expand All @@ -61,6 +77,10 @@ public enum NativeState {
protected static boolean mScreenKeyboardShown;
protected static ViewGroup mLayout;
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
Expand Down Expand Up @@ -133,6 +153,10 @@ public static void initialize() {
mTextEdit = null;
mLayout = null;
mClipboardHandler = null;
//#CURSORIMPLEENTATION
//mCursors = new Hashtable<Integer, PointerIcon>();
//mLastCursorID = 0;
//mActiveCursor = null;
mSDLThread = null;
mExitCalledFromJava = false;
mBrokenLibraries = false;
Expand Down Expand Up @@ -1051,7 +1075,7 @@ public boolean onKey(DialogInterface d, int keyCode, KeyEvent event) {
public static boolean clipboardHasText() {
return mClipboardHandler.clipboardHasText();
}

/**
* This method is called by SDL using JNI.
*/
Expand All @@ -1065,6 +1089,73 @@ public static String clipboardGetText() {
public static void clipboardSetText(String 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;
}
*/
}

/**
Expand Down Expand Up @@ -1456,6 +1547,14 @@ public void onSensorChanged(SensorEvent event) {
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
Expand Down
43 changes: 42 additions & 1 deletion src/core/android/SDL_android.c
Expand Up @@ -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;
Expand Down Expand Up @@ -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 ||
Expand Down Expand Up @@ -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: */
5 changes: 5 additions & 0 deletions src/core/android/SDL_android.h
Expand Up @@ -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* */
Expand Down
95 changes: 95 additions & 0 deletions src/video/android/SDL_androidmouse.c
Expand Up @@ -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;
}

Expand Down

0 comments on commit e20d417

Please sign in to comment.