Add ability to type with the virtual keyboard

This commit is contained in:
simon.kagstrom 2009-01-24 08:36:47 +00:00
parent a063dfe18d
commit 2ee93bab27
4 changed files with 102 additions and 54 deletions

View File

@ -196,7 +196,9 @@ public:
char save_game_name[256]; char save_game_name[256];
void select_disc(Prefs *np); void select_disc(Prefs *np);
void run_fake_key_sequence(Prefs *np); void select_fake_key_sequence(Prefs *np);
void start_fake_key_sequence(const char *str);
void run_fake_key_sequence();
char * bind_one_key(Prefs *np, int which); char * bind_one_key(Prefs *np, int which);
void bind_keys(Prefs *np); void bind_keys(Prefs *np);
void other_options(Prefs *np); void other_options(Prefs *np);

View File

@ -355,7 +355,35 @@ void C64::other_options(Prefs *np)
menu_fini(&display_menu); menu_fini(&display_menu);
} }
void C64::run_fake_key_sequence(Prefs *np) /* From dreamcast port but heavily modified */
void C64::run_fake_key_sequence()
{
int kc = this->virtual_keyboard->char_to_keycode(this->fake_key_str[this->fake_key_index]);
TheDisplay->FakeKeyPress(kc, TheCIA1->KeyMatrix, TheCIA1->RevMatrix);
this->fake_key_keytime --;
if (this->fake_key_keytime == 0)
{
this->fake_key_keytime = 4;
this->fake_key_index ++;
if (this->fake_key_str[this->fake_key_index] == '\0')
{
this->fake_key_sequence = false;
this->fake_key_index = 0;
this->fake_key_keytime = 5;
}
}
}
void C64::start_fake_key_sequence(const char *str)
{
this->fake_key_str = str;
this->fake_key_sequence = true;
}
void C64::select_fake_key_sequence(Prefs *np)
{ {
static const char *fake_key_sequences[] = { static const char *fake_key_sequences[] = {
"\nLOAD \"*\",8,1\nRUN\n", "\nLOAD \"*\",8,1\nRUN\n",
@ -368,6 +396,7 @@ void C64::run_fake_key_sequence(Prefs *np)
"LOAD \"?\",8", "LOAD \"?\",8",
"LIST", "LIST",
"10 PRINT \"HELLO WORLD\" and 20 GOTO 10", "10 PRINT \"HELLO WORLD\" and 20 GOTO 10",
"Type with virtual keyboard",
NULL}; NULL};
menu_t fake_key_menu; menu_t fake_key_menu;
int opt; int opt;
@ -380,8 +409,15 @@ void C64::run_fake_key_sequence(Prefs *np)
if (opt < 0) if (opt < 0)
return; return;
this->fake_key_str = fake_key_sequences[opt]; if (opt == 4)
this->fake_key_sequence = true; {
const char *seq = this->virtual_keyboard->get_string();
if (seq != NULL)
this->start_fake_key_sequence(seq);
}
else
this->start_fake_key_sequence(fake_key_sequences[opt]);
} }
void C64::save_load_state(Prefs *np) void C64::save_load_state(Prefs *np)
@ -489,27 +525,8 @@ void C64::VBlank(bool draw_frame)
if (TheDisplay->quit_requested) if (TheDisplay->quit_requested)
quit_thyself = true; quit_thyself = true;
/* From dreamcast port */
if (this->fake_key_sequence) if (this->fake_key_sequence)
{ this->run_fake_key_sequence();
int kc = this->virtual_keyboard->char_to_keycode(this->fake_key_str[this->fake_key_index]);
TheDisplay->FakeKeyPress(kc, TheCIA1->KeyMatrix, TheCIA1->RevMatrix);
this->fake_key_keytime --;
if (this->fake_key_keytime == 0)
{
this->fake_key_keytime = 4;
this->fake_key_index ++;
if (this->fake_key_str[this->fake_key_index] == '\0')
{
this->fake_key_sequence = false;
this->fake_key_index = 0;
this->fake_key_keytime = 5;
}
}
}
#ifndef GEKKO #ifndef GEKKO
// Joystick keyboard emulation // Joystick keyboard emulation
if (TheDisplay->NumLock()) if (TheDisplay->NumLock())
@ -563,7 +580,7 @@ void C64::VBlank(bool draw_frame)
switch(opt) switch(opt)
{ {
case 0: /* Load disc/tape */ case 0: /* Load disc/tape */
this->run_fake_key_sequence(&np); this->select_fake_key_sequence(&np);
break; break;
case 1: /* Insert disc/tape */ case 1: /* Insert disc/tape */
this->select_disc(&np); this->select_disc(&np);

View File

@ -15,11 +15,12 @@
#include "menu.h" #include "menu.h"
#include "VirtualKeyboard.h" #include "VirtualKeyboard.h"
typedef struct typedef struct virtkey
{ {
const char *name; const char *name;
int kc; int kc;
bool is_shift; bool is_shift;
bool is_done;
} virtkey_t; } virtkey_t;
/* /*
@ -38,11 +39,13 @@ typedef struct
#define MATRIX(a,b) (((a) << 3) | (b)) #define MATRIX(a,b) (((a) << 3) | (b))
#define K(name, a,b) \ #define K(name, a,b) \
{ name, MATRIX(a,b), false } { name, MATRIX(a,b), false, false }
#define S(name, a,b) \ #define S(name, a,b) \
{ name, MATRIX(a,b), true } { name, MATRIX(a,b), true, false }
#define N(name) \ #define N(name) \
{ name, -1, false } { name, -1, false, false }
#define D(name) \
{ name, -1, false, true }
#define KEY_COLS 15 #define KEY_COLS 15
#define KEY_ROWS 5 #define KEY_ROWS 5
@ -52,7 +55,7 @@ static virtkey_t keys[KEY_COLS * KEY_ROWS] = {
K("Cr", 7,2), K("Q", 7,6), K("W", 1,1), K("E", 1,6), K("R", 2,1), K("T", 2,6), K("Y", 3,1), K("U", 3,6), K("I", 4,1), K("O", 4,6), K("P", 5,1), K("@", 5,6), K("*", 6,1), K("Au", 6,6),K("Rstr",4,0), K("Cr", 7,2), K("Q", 7,6), K("W", 1,1), K("E", 1,6), K("R", 2,1), K("T", 2,6), K("Y", 3,1), K("U", 3,6), K("I", 4,1), K("O", 4,6), K("P", 5,1), K("@", 5,6), K("*", 6,1), K("Au", 6,6),K("Rstr",4,0),
K("R/Stp", 7,7), K(NULL,0,0), K("A", 1,2), K("S", 1,5), K("D", 2,2), K("F", 2,5), K("G", 3,2), K("H", 3,5), K("J", 4,2), K("K", 4,5), K("L", 5,2), K(":", 5,5), K(";", 6,2), K("=", 6,5), K("Ret", 0,1), K("R/Stp", 7,7), K(NULL,0,0), K("A", 1,2), K("S", 1,5), K("D", 2,2), K("F", 2,5), K("G", 3,2), K("H", 3,5), K("J", 4,2), K("K", 4,5), K("L", 5,2), K(":", 5,5), K(";", 6,2), K("=", 6,5), K("Ret", 0,1),
K("C=", 7,5), S("Shft",1,7),K(NULL,0,0),K("Z", 1,4), K("X", 2,7), K("C", 2,4), K("V", 3,7), K("B", 3,4), K("N", 4,7), K("M", 4,4), K(",", 5,7), K(".", 5,4), K("/", 6,7), K("Dwn",0,7),K("Rgt", 0,2), K("C=", 7,5), S("Shft",1,7),K(NULL,0,0),K("Z", 1,4), K("X", 2,7), K("C", 2,4), K("V", 3,7), K("B", 3,4), K("N", 4,7), K("M", 4,4), K(",", 5,7), K(".", 5,4), K("/", 6,7), K("Dwn",0,7),K("Rgt", 0,2),
N("None"), K(NULL,0,0), K(NULL,0,0), K("space", 7,4),K(0, 0,0),K(NULL,0,0), K("f1", 0,4),K("f3", 0,5),K("f5", 0,6),K("f7", 0,3),K(NULL,0,0), K(NULL,0,0), K(NULL,0,0), K(NULL,0,0), K("Del", 0,0), N("None"), K(NULL,0,0), K(NULL,0,0), K("space", 7,4),K(0, 0,0),K(NULL,0,0), K("f1", 0,4),K("f3", 0,5),K("f5", 0,6),K("f7", 0,3),K("Del",0,0),K(NULL,0,0), K(NULL,0,0), K(NULL,0,0), D("DONE"),
}; };
static const char *shifted_names[KEY_COLS * KEY_ROWS] = { static const char *shifted_names[KEY_COLS * KEY_ROWS] = {
@ -60,7 +63,7 @@ static const char *shifted_names[KEY_COLS * KEY_ROWS] = {
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "[", "]", NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "[", "]", NULL, NULL,
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "<", ">", "?", "Up", "Lft", NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "<", ">", "?", "Up", "Lft",
NULL, NULL, NULL, NULL, NULL, NULL, "f2", "f4", "f6", "f8", NULL, NULL, NULL, NULL, "Ins", NULL, NULL, NULL, NULL, NULL, NULL, "f2", "f4", "f6", "f8", "Ins", NULL, NULL, NULL, NULL,
}; };
VirtualKeyboard::VirtualKeyboard(SDL_Surface *screen, TTF_Font *font) VirtualKeyboard::VirtualKeyboard(SDL_Surface *screen, TTF_Font *font)
@ -196,7 +199,6 @@ int VirtualKeyboard::char_to_keycode(char c)
} }
} }
printf("Vobb! c=%d\n", c);
return -1; return -1;
} }
@ -206,10 +208,8 @@ const char VirtualKeyboard::get_char(int kc)
return this->keycode_to_string(kc)[0]; return this->keycode_to_string(kc)[0];
} }
int VirtualKeyboard::get_key_internal() struct virtkey *VirtualKeyboard::get_key_internal()
{ {
int kc = -1;
while(1) while(1)
{ {
uint32_t k; uint32_t k;
@ -228,30 +228,34 @@ int VirtualKeyboard::get_key_internal()
else if (k & KEY_RIGHT) else if (k & KEY_RIGHT)
this->select_next(1, 0); this->select_next(1, 0);
else if (k & KEY_ESCAPE) else if (k & KEY_ESCAPE)
return -2; return NULL;
else if (k & KEY_SELECT) else if (k & KEY_SELECT)
{ {
virtkey_t key = keys[ this->sel_y * KEY_COLS + this->sel_x ]; virtkey_t *key = &keys[ this->sel_y * KEY_COLS + this->sel_x ];
kc = key.kc; if (key->is_shift == true)
if (this->shift_on)
kc |= 0x80;
if (key.is_shift == true)
this->toggle_shift(); this->toggle_shift();
else else
return kc; return key;
} }
} }
return kc; return NULL;
} }
int VirtualKeyboard::get_key() int VirtualKeyboard::get_key()
{ {
virtkey_t *key;
SDL_FillRect(this->screen, 0, SDL_MapRGB(screen->format, 0x00, 0x80, 0x80)); SDL_FillRect(this->screen, 0, SDL_MapRGB(screen->format, 0x00, 0x80, 0x80));
return this->get_key_internal(); key = this->get_key_internal();
if (key == NULL)
return -2;
if (key->is_shift)
return key->kc | 0x80;
return key->kc;
} }
const char *VirtualKeyboard::get_string() const char *VirtualKeyboard::get_string()
@ -263,18 +267,41 @@ const char *VirtualKeyboard::get_string()
while (true) while (true)
{ {
int kc = this->get_key_internal(); virtkey_t *key = this->get_key_internal();
char c;
/* Abort or None */ /* Abort */
if (kc == -2 || kc == -1) if (key == NULL)
return NULL; return NULL;
/* Return */
if (kc == MATRIX(0, 1))
return this->buf;
this->buf[cnt] = this->get_char(kc); /* Done */
if (key->is_done)
return this->buf;
/* Skip None */
if (key->kc == -1)
continue;
/* Special-case for delete */
if (strcmp(key->name, "Del") == 0)
{
if (cnt < 1)
continue;
this->buf[cnt - 1] = ' ';
cnt -= 2;
}
else
{
c = this->get_char( this->shift_on ? key->kc | 0x80 : key->kc );
if (strcmp(key->name, "space") == 0)
c = ' ';
else if (strcmp(key->name, "Ret") == 0)
c = '\n';
this->buf[cnt] = c;
}
cnt++; cnt++;
if (cnt >= sizeof(this->buf)) if (cnt >= sizeof(this->buf) - 1)
return this->buf; return this->buf;
/* SDL_Flip is done in get_key_internal() */ /* SDL_Flip is done in get_key_internal() */

View File

@ -12,6 +12,8 @@
#include <SDL.h> #include <SDL.h>
#include <SDL_ttf.h> #include <SDL_ttf.h>
struct virtkey;
class VirtualKeyboard class VirtualKeyboard
{ {
public: public:
@ -23,7 +25,7 @@ public:
private: private:
const char get_char(int kc); const char get_char(int kc);
int get_key_internal(); struct virtkey *get_key_internal();
void draw(); void draw();
void select_next(int dx, int dy); void select_next(int dx, int dy);
void toggle_shift(); void toggle_shift();