Added QWERTY on-screen keyboard input, using built-in Android virtual keyboard and EditText widget

This commit is contained in:
pelya
2010-11-04 19:01:23 +02:00
parent dc48633eed
commit 7f582a107b
13 changed files with 239 additions and 112 deletions

View File

@@ -1 +1 @@
scummvm
pachi

View File

@@ -46,7 +46,7 @@ enum {
SDL_ANDRIOD_SCREENKEYBOARD_BUTTON_3,
SDL_ANDRIOD_SCREENKEYBOARD_BUTTON_4,
SDL_ANDRIOD_SCREENKEYBOARD_BUTTON_5,
SDL_ANDRIOD_SCREENKEYBOARD_BUTTON_6,
SDL_ANDRIOD_SCREENKEYBOARD_BUTTON_6, /* Button to show screen keyboard */
SDL_ANDRIOD_SCREENKEYBOARD_BUTTON_MAX = SDL_ANDRIOD_SCREENKEYBOARD_BUTTON_6
};
@@ -69,6 +69,9 @@ extern DECLSPEC int SDLCALL SDL_ANDROID_GetScreenKeyboardShown();
extern DECLSPEC int SDLCALL SDL_ANDROID_GetScreenKeyboardSize();
/* Show Android on-screen keyboard, and pass entered text back to application when user closes it */
extern DECLSPEC int SDLCALL SDL_ANDROID_ToggleScreenKeyboardTextInput();
#ifdef __cplusplus
}
#endif

View File

@@ -143,6 +143,30 @@ JAVA_EXPORT_NAME(DemoGLSurfaceView_nativeKey) ( JNIEnv* env, jobject thiz, jint
SDL_SendKeyboardKey( action ? SDL_PRESSED : SDL_RELEASED, TranslateKey(key ,&keysym) );
}
JNIEXPORT void JNICALL
JAVA_EXPORT_NAME(DemoGLSurfaceView_nativeTextInput) ( JNIEnv* env, jobject thiz, jint ascii, jint unicode )
{
SDL_keysym keysym;
keysym.scancode = ascii;
keysym.sym = ascii;
keysym.mod = KMOD_NONE;
keysym.unicode = 0;
if ( SDL_TranslateUNICODE ) {
/* Populate the unicode field with the ASCII value */
keysym.unicode = unicode;
}
#if SDL_VERSION_ATLEAST(1,3,0)
char text[2];
text[0]=ascii;
text[1]=0;
SDL_SendKeyboardText(text);
#else
SDL_SendKeyboardKey( SDL_PRESSED, &keysym );
SDL_SendKeyboardKey( SDL_RELEASED, &keysym );
#endif
}
static void updateOrientation ( float accX, float accY, float accZ );
@@ -233,9 +257,9 @@ void ANDROID_InitOSKeymap()
// TODO: make this configurable
keymap[KEYCODE_MENU] = SDL_KEY(SDL_KEY_VAL(SDL_ANDROID_KEYCODE_4));
keymap[KEYCODE_SEARCH] = SDL_KEY(SDL_KEY_VAL(SDL_ANDROID_KEYCODE_1));
keymap[KEYCODE_CALL] = SDL_KEY(SDL_KEY_VAL(SDL_ANDROID_KEYCODE_1));
keymap[KEYCODE_DPAD_CENTER] = SDL_KEY(SDL_KEY_VAL(SDL_ANDROID_KEYCODE_1));
keymap[KEYCODE_SEARCH] = SDL_KEY(SDL_KEY_VAL(SDL_ANDROID_KEYCODE_9));
keymap[KEYCODE_CALL] = SDL_KEY(SDL_KEY_VAL(SDL_ANDROID_KEYCODE_10));
//keymap[KEYCODE_CALL] = SDL_KEY(RCTRL);
//keymap[KEYCODE_DPAD_CENTER] = SDL_KEY(LALT);
@@ -724,3 +748,5 @@ void SDL_SYS_JoystickQuit(void)
SDL_ANDROID_CurrentJoysticks[i] = NULL;
return;
}

View File

@@ -200,5 +200,11 @@ extern int SDL_ANDROID_isTouchscreenKeyboardUsed;
#ifndef SDL_ANDROID_KEYCODE_8
#define SDL_ANDROID_KEYCODE_8 DELETE
#endif
#ifndef SDL_ANDROID_KEYCODE_9
#define SDL_ANDROID_KEYCODE_9 SDL_ANDROID_KEYCODE_1
#endif
#ifndef SDL_ANDROID_KEYCODE_10
#define SDL_ANDROID_KEYCODE_10 SDL_ANDROID_KEYCODE_1
#endif
#endif

View File

@@ -55,7 +55,9 @@ static JNIEnv* JavaEnv = NULL;
static jclass JavaRendererClass = NULL;
static jobject JavaRenderer = NULL;
static jmethodID JavaSwapBuffers = NULL;
static jmethodID JavaShowScreenKeyboard = NULL;
static int glContextLost = 0;
static int showScreenKeyboardDeferred = 0;
static void appPutToBackgroundCallbackDefault(void)
{
@@ -85,6 +87,11 @@ int SDL_ANDROID_CallJavaSwapBuffers()
SDL_ANDROID_VideoContextRecreated();
appRestoredCallback();
}
if( showScreenKeyboardDeferred )
{
showScreenKeyboardDeferred = 0;
(*JavaEnv)->CallVoidMethod( JavaEnv, JavaRenderer, JavaShowScreenKeyboard );
}
return 1;
}
@@ -151,6 +158,11 @@ JAVA_EXPORT_NAME(DemoRenderer_nativeGlContextRecreated) ( JNIEnv* env, jobject
#endif
}
void SDL_ANDROID_CallJavaShowScreenKeyboard()
{
showScreenKeyboardDeferred = 1;
}
JNIEXPORT void JNICALL
JAVA_EXPORT_NAME(DemoRenderer_nativeInitJavaCallbacks) ( JNIEnv* env, jobject thiz )
{
@@ -159,6 +171,7 @@ JAVA_EXPORT_NAME(DemoRenderer_nativeInitJavaCallbacks) ( JNIEnv* env, jobject t
JavaRendererClass = (*JavaEnv)->GetObjectClass(JavaEnv, thiz);
JavaSwapBuffers = (*JavaEnv)->GetMethodID(JavaEnv, JavaRendererClass, "swapBuffers", "()I");
JavaShowScreenKeyboard = (*JavaEnv)->GetMethodID(JavaEnv, JavaRendererClass, "showScreenKeyboard", "()V");
ANDROID_InitOSKeymap();

View File

@@ -32,6 +32,7 @@ extern int SDL_ANDROID_sWindowHeight;
extern int SDL_ANDROID_sFakeWindowWidth; // SDL 1.2 only
extern int SDL_ANDROID_sFakeWindowHeight; // SDL 1.2 only
extern int SDL_ANDROID_CallJavaSwapBuffers();
extern void SDL_ANDROID_CallJavaShowScreenKeyboard();
extern int SDL_ANDROID_drawTouchscreenKeyboard();
extern void SDL_ANDROID_VideoContextLost();
extern void SDL_ANDROID_VideoContextRecreated();

View File

@@ -55,13 +55,12 @@ FONT_BTN1 = 4, FONT_BTN2 = 5, FONT_BTN3 = 6, FONT_BTN4 = 7
static GLshort fontGL[sizeof(font)/sizeof(font[0])][FONT_MAX_LINES_PER_CHAR * 4 + 1];
enum { FONT_CHAR_LINES_COUNT = FONT_MAX_LINES_PER_CHAR * 4 };
enum { MAX_BUTTONS = SDL_ANDRIOD_SCREENKEYBOARD_BUTTON_MAX, MAX_BUTTONS_AUTOFIRE = 2 } ; // Max amount of custom buttons
enum { MAX_BUTTONS = SDL_ANDRIOD_SCREENKEYBOARD_BUTTON_MAX, MAX_BUTTONS_AUTOFIRE = 2, BUTTON_TEXT_INPUT = MAX_BUTTONS - 1 } ; // Max amount of custom buttons
int SDL_ANDROID_isTouchscreenKeyboardUsed = 0;
static int touchscreenKeyboardTheme = 0;
static int touchscreenKeyboardShown = 1;
static int AutoFireButtonsNum = 0;
static int nbuttons = 4;
static int buttonsize = 1;
static SDL_Rect arrows, buttons[MAX_BUTTONS], buttonsAutoFireRect[MAX_BUTTONS_AUTOFIRE];
@@ -242,8 +241,10 @@ int SDL_ANDROID_drawTouchscreenKeyboard()
255, 255, SDL_GetKeyboardState(NULL)[SDL_KEY(DOWN)] ? 255 : 0, 128 );
// Draw buttons
for( i = 0; i < nbuttons; i++ )
for( i = 0; i < MAX_BUTTONS; i++ )
{
if( ! buttons[i].h || ! buttons[i].w )
continue;
drawCharWireframe( FONT_BTN1 + i, buttons[i].x + buttons[i].w / 2, buttons[i].y + buttons[i].h / 2, ( i < AutoFireButtonsNum ? ButtonAutoFireRot[i] * 0x10000 : 0 ),
( i < AutoFireButtonsNum && ButtonAutoFire[i] ) ? 0 : 255, 255, SDL_GetKeyboardState(NULL)[buttonKeysyms[i]] ? 255 : 0, 128 );
}
@@ -270,8 +271,10 @@ int SDL_ANDROID_drawTouchscreenKeyboard()
drawCharTex( &arrowImages[4], NULL, &arrows, 255, 255, 255, 128 / blendFactor );
}
for( i = 0; i < nbuttons; i++ )
for( i = 0; i < MAX_BUTTONS; i++ )
{
if( ! buttons[i].h || ! buttons[i].w )
continue;
if( i < AutoFireButtonsNum )
{
if( ButtonAutoFire[i] == 1 && SDL_GetTicks() - ButtonAutoFireDecay[i] > 1000 )
@@ -411,15 +414,20 @@ int SDL_ANDROID_processTouchscreenKeyboard(int x, int y, int action, int pointer
}
}
for( i = 0; i < nbuttons; i++ )
for( i = 0; i < MAX_BUTTONS; i++ )
{
if( ! buttons[i].h || ! buttons[i].w )
continue;
if( InsideRect( &buttons[i], x, y) )
{
processed = 1;
if( pointerInButtonRect[i] == -1 )
{
pointerInButtonRect[i] = pointerId;
SDL_SendKeyboardKey( SDL_PRESSED, GetKeysym(buttonKeysyms[i], &keysym) );
if( i == BUTTON_TEXT_INPUT )
SDL_ANDROID_ToggleScreenKeyboardTextInput();
else
SDL_SendKeyboardKey( SDL_PRESSED, GetKeysym(buttonKeysyms[i], &keysym) );
if( i < AutoFireButtonsNum )
{
ButtonAutoFire[i] = 0;
@@ -465,8 +473,10 @@ int SDL_ANDROID_processTouchscreenKeyboard(int x, int y, int action, int pointer
oldArrows = 0;
}
}
for( i = 0; i < nbuttons; i++ )
for( i = 0; i < MAX_BUTTONS; i++ )
{
if( ! buttons[i].h || ! buttons[i].w )
continue;
if( pointerInButtonRect[i] == pointerId )
{
processed = 1;
@@ -477,7 +487,8 @@ int SDL_ANDROID_processTouchscreenKeyboard(int x, int y, int action, int pointer
}
else
{
SDL_SendKeyboardKey( SDL_RELEASED, GetKeysym(buttonKeysyms[i] ,&keysym) );
if( i != BUTTON_TEXT_INPUT )
SDL_SendKeyboardKey( SDL_RELEASED, GetKeysym(buttonKeysyms[i] ,&keysym) );
}
if( i < AutoFireButtonsNum )
{
@@ -571,7 +582,8 @@ int SDL_ANDROID_processTouchscreenKeyboard(int x, int y, int action, int pointer
pointerInButtonRect[i] = -1;
if( !ButtonAutoFire[i] )
{
SDL_SendKeyboardKey( SDL_RELEASED, GetKeysym(buttonKeysyms[i] ,&keysym) );
if( i != BUTTON_TEXT_INPUT )
SDL_SendKeyboardKey( SDL_RELEASED, GetKeysym(buttonKeysyms[i] ,&keysym) );
}
else
{
@@ -625,15 +637,18 @@ int SDL_ANDROID_processTouchscreenKeyboard(int x, int y, int action, int pointer
}
}
}
for( i = AutoFireButtonsNum; i < nbuttons; i++ )
for( i = AutoFireButtonsNum; i < MAX_BUTTONS; i++ )
{
if( ! buttons[i].h || ! buttons[i].w )
continue;
if( pointerInButtonRect[i] == pointerId )
{
processed = 1;
if( ! InsideRect( &buttons[i], x, y ) )
{
pointerInButtonRect[i] = -1;
SDL_SendKeyboardKey( SDL_RELEASED, GetKeysym(buttonKeysyms[i] ,&keysym) );
if( i != BUTTON_TEXT_INPUT )
SDL_SendKeyboardKey( SDL_RELEASED, GetKeysym(buttonKeysyms[i] ,&keysym) );
}
}
}
@@ -643,14 +658,13 @@ int SDL_ANDROID_processTouchscreenKeyboard(int x, int y, int action, int pointer
};
JNIEXPORT void JNICALL
JAVA_EXPORT_NAME(Settings_nativeSetupScreenKeyboard) ( JNIEnv* env, jobject thiz, jint size, jint theme, jint _nbuttons, jint nbuttonsAutoFire )
JAVA_EXPORT_NAME(Settings_nativeSetupScreenKeyboard) ( JNIEnv* env, jobject thiz, jint size, jint theme, jint _nbuttons, jint nbuttonsAutoFire, jint showArrows, jint showTextInput )
{
int i, ii;
int nbuttons1row, nbuttons2row;
nbuttons = _nbuttons;
touchscreenKeyboardTheme = theme;
if( nbuttons > MAX_BUTTONS )
nbuttons = MAX_BUTTONS;
if( _nbuttons > MAX_BUTTONS )
_nbuttons = MAX_BUTTONS;
AutoFireButtonsNum = nbuttonsAutoFire;
if( AutoFireButtonsNum > MAX_BUTTONS_AUTOFIRE )
AutoFireButtonsNum = MAX_BUTTONS_AUTOFIRE;
@@ -672,7 +686,7 @@ JAVA_EXPORT_NAME(Settings_nativeSetupScreenKeyboard) ( JNIEnv* env, jobject thi
buttons[0].y = SDL_ANDROID_sWindowHeight - buttons[0].h;
// Row of secondary buttons to the upper-right
nbuttons1row = MIN(nbuttons, 4);
nbuttons1row = 4;
for( i = 1; i < nbuttons1row; i++ )
{
buttons[i].w = SDL_ANDROID_sWindowWidth / (nbuttons1row - 1) / (size + 2);
@@ -682,7 +696,7 @@ JAVA_EXPORT_NAME(Settings_nativeSetupScreenKeyboard) ( JNIEnv* env, jobject thi
}
// Row of secondary buttons to the upper-left above arrows
nbuttons2row = MIN(nbuttons, 7);
nbuttons2row = MAX_BUTTONS;
for( i = 4; i < nbuttons2row; i++ )
{
buttons[i].w = SDL_ANDROID_sWindowWidth / (nbuttons2row - 4) / (size + 2);
@@ -697,7 +711,7 @@ JAVA_EXPORT_NAME(Settings_nativeSetupScreenKeyboard) ( JNIEnv* env, jobject thi
prepareFontCharWireframe(FONT_UP, arrows.w / 2, arrows.h / 2);
prepareFontCharWireframe(FONT_DOWN, arrows.w / 2, arrows.h / 2);
for( i = 0; i < nbuttons; i++ )
for( i = 0; i < MAX_BUTTONS; i++ )
{
prepareFontCharWireframe(FONT_BTN1 + i, MIN(buttons[i].h, buttons[i].w), MIN(buttons[i].h, buttons[i].w));
}
@@ -735,6 +749,17 @@ JAVA_EXPORT_NAME(Settings_nativeSetupScreenKeyboard) ( JNIEnv* env, jobject thi
buttons[6].w = 30;
buttons[6].h = 30;
}
if( !showArrows )
{
arrows.w = 0;
arrows.h = 0;
}
for( i = 0; i < MAX_BUTTONS; i++ )
{
if( i >= _nbuttons && ( i == BUTTON_TEXT_INPUT && !showTextInput ) )
buttons[i].w = buttons[i].h = 0;
}
for( i = 0; i < sizeof(pointerInButtonRect)/sizeof(pointerInButtonRect[0]); i++ )
{
@@ -923,3 +948,9 @@ int SDL_ANDROID_GetScreenKeyboardSize()
{
return buttonsize;
};
int SDL_ANDROID_ToggleScreenKeyboardTextInput()
{
SDL_ANDROID_CallJavaShowScreenKeyboard();
return 1;
};