Unused get_string added, handle -2

This commit is contained in:
simon.kagstrom 2009-01-23 20:49:00 +00:00
parent 1ee6fe673e
commit f3c87f4bc1
3 changed files with 58 additions and 8 deletions

View File

@ -303,7 +303,8 @@ void C64::bind_keys(Prefs *np)
bool shifted; bool shifted;
key = this->virtual_keyboard->get_key(); key = this->virtual_keyboard->get_key();
if (key >= 0) /* -2 means abort */
if (key != np->JoystickKeyBinding[opt] && key != -2)
{ {
this->prefs_changed = true; this->prefs_changed = true;
np->JoystickKeyBinding[opt] = key; np->JoystickKeyBinding[opt] = key;

View File

@ -70,6 +70,8 @@ VirtualKeyboard::VirtualKeyboard(SDL_Surface *screen, TTF_Font *font)
this->sel_x = 0; this->sel_x = 0;
this->sel_y = 0; this->sel_y = 0;
this->shift_on = false; this->shift_on = false;
memset(this->buf, 0, sizeof(this->buf));
} }
void VirtualKeyboard::draw() void VirtualKeyboard::draw()
@ -80,6 +82,11 @@ void VirtualKeyboard::draw()
int key_h = 36; int key_h = 36;
int border_x = (screen_w - (key_w * KEY_COLS)) / 2; int border_x = (screen_w - (key_w * KEY_COLS)) / 2;
int border_y = (screen_h - (key_h * KEY_ROWS)) / 2; int border_y = (screen_h - (key_h * KEY_ROWS)) / 2;
SDL_Rect bg_rect = {border_x, border_y,
key_w * KEY_COLS, key_h * KEY_ROWS};
SDL_FillRect(this->screen, &bg_rect,
SDL_MapRGB(screen->format, 0x00, 0x80, 0x80));
for (int y = 0; y < KEY_ROWS; y++ ) for (int y = 0; y < KEY_ROWS; y++ )
{ {
@ -164,7 +171,13 @@ const char *VirtualKeyboard::keycode_to_string(int kc)
return out; return out;
} }
int VirtualKeyboard::get_key() const char VirtualKeyboard::get_char(int kc)
{
/* NULL is never, ever returned */
return this->keycode_to_string(kc)[0];
}
int VirtualKeyboard::get_key_internal()
{ {
int kc = -1; int kc = -1;
@ -172,8 +185,6 @@ int VirtualKeyboard::get_key()
{ {
uint32_t k; uint32_t k;
SDL_FillRect(this->screen, 0, SDL_MapRGB(screen->format, 0x00, 0x80, 0x80));
this->draw(); this->draw();
SDL_Flip(this->screen); SDL_Flip(this->screen);
@ -188,7 +199,7 @@ int VirtualKeyboard::get_key()
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)
break; return -2;
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 ];
@ -200,11 +211,44 @@ int VirtualKeyboard::get_key()
if (key.is_shift == true) if (key.is_shift == true)
this->toggle_shift(); this->toggle_shift();
else else
break; return kc;
} }
} }
SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 0, 0, 0));
return kc; return kc;
} }
int VirtualKeyboard::get_key()
{
SDL_FillRect(this->screen, 0, SDL_MapRGB(screen->format, 0x00, 0x80, 0x80));
return this->get_key_internal();
}
const char *VirtualKeyboard::get_string()
{
int cnt = 0;
SDL_FillRect(this->screen, 0, SDL_MapRGB(screen->format, 0x00, 0x80, 0x80));
memset(this->buf, 0, sizeof(this->buf));
while (true)
{
int kc = this->get_key_internal();
/* Abort or None */
if (kc == -2 || kc == -1)
return NULL;
/* Return */
if (kc == MATRIX(0, 1))
return this->buf;
this->buf[cnt] = this->get_char(kc);
cnt++;
if (cnt >= sizeof(this->buf))
return this->buf;
}
/* Not reachable */
return NULL;
}

View File

@ -17,9 +17,12 @@ class VirtualKeyboard
public: public:
VirtualKeyboard(SDL_Surface *screen, TTF_Font *font); VirtualKeyboard(SDL_Surface *screen, TTF_Font *font);
int get_key(); int get_key();
const char *get_string();
const char *keycode_to_string(int kc); const char *keycode_to_string(int kc);
private: private:
const char get_char(int kc);
int 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();
@ -29,4 +32,6 @@ private:
int sel_x; int sel_x;
int sel_y; int sel_y;
bool shift_on; bool shift_on;
char buf[255];
}; };