Changed the keyboard handling to store "keyboard events", rather than just

characters. Hopefully this will make things work smoother on the Mac, but I
have no way of testing that.

svn-id: r10376
This commit is contained in:
Torbjörn Andersson 2003-09-23 15:59:52 +00:00
parent d0de9fff86
commit 57d99796ea
12 changed files with 77 additions and 58 deletions

View File

@ -243,7 +243,7 @@ public:
virtual void onMouseMove(int x, int y) {}
virtual void onMouseDown(int x, int y) {}
virtual void onMouseUp(int x, int y) {}
virtual void onKey(char key) {}
virtual void onKey(_keyboardEvent *ke) {}
virtual void onTick() {}
virtual void releaseMouse(int x, int y) {}
@ -376,14 +376,14 @@ int Sword2Dialog::run() {
int16 newMouseX = mousex;
int16 newMouseY = mousey + 40;
char key;
int32 keyboardStatus = ReadKey(&key);
_mouseEvent *me = MouseEvent();
_keyboardEvent ke;
int32 keyboardStatus = ReadKey(&ke);
if (keyboardStatus == RD_OK) {
if (key == 27)
if (ke.keycode == 27)
setResult(0);
else if (key == 13)
else if (ke.keycode == '\n' || ke.keycode == '\r')
setResult(1);
}
@ -416,8 +416,8 @@ int Sword2Dialog::run() {
}
}
if (keyboardStatus == RD_OK && key != 0)
_widgets[i]->onKey(key);
if (keyboardStatus == RD_OK)
_widgets[i]->onKey(&ke);
_widgets[i]->onTick();
}
@ -972,9 +972,13 @@ public:
}
}
virtual void onKey(char key) {
if (_editable && (key == 8 || (key >= ' ' && key <= 'z')))
_parent->onAction(this, key);
virtual void onKey(_keyboardEvent *ke) {
if (_editable) {
if (ke->keycode == 8)
_parent->onAction(this, 8);
else if (ke->ascii >= ' ' && ke->ascii <= 'z')
_parent->onAction(this, ke->ascii);
}
}
virtual void onTick() {
@ -1347,7 +1351,7 @@ void Restart_control(void) { //Tony4Apr97
return;
}
// Stop music instantly! (James22aug97)
// Stop music instantly! (James 22aug97)
Kill_music();
//in case we were dead - well we're not anymore!
@ -1410,13 +1414,14 @@ void Control_error(char* text) { //Tony13May97
// Wait for ESC or mouse click
while (1) {
_mouseEvent *me;
char c;
ServiceWindows();
if (KeyWaiting()) {
ReadKey(&c);
if (c == 27)
_keyboardEvent ke;
ReadKey(&ke);
if (ke.keycode == 27)
break;
}

View File

@ -463,9 +463,9 @@ int32 PlaySmacker(char *filename, _movieTextObject *text[], uint8 *musicOut) {
ServiceWindows();
char key;
_keyboardEvent ke;
if (ReadKey(&key) == RD_OK && key == 27) {
if (ReadKey(&ke) == RD_OK && ke.keycode == 27) {
g_sword2->_mixer->stopHandle(handle);
break;
}

View File

@ -1207,11 +1207,16 @@ typedef int BOOL;
// ---------------------
//
typedef struct
{
typedef struct {
uint16 buttons;
} _mouseEvent;
typedef struct {
uint16 ascii;
int keycode;
int modifiers;
} _keyboardEvent;
#if !defined(__GNUC__)
#pragma START_PACK_STRUCTS
#endif
@ -1369,7 +1374,7 @@ extern void ResetRenderEngine(void);
// Keyboard functions - from keyboard.c
//-----------------------------------------------------------------------------
extern BOOL KeyWaiting(void);
extern int32 ReadKey(char *key);
extern int32 ReadKey(_keyboardEvent *ke);
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------

View File

@ -66,11 +66,15 @@
uint8 keyBacklog = 0; // The number of key presses waiting to be processed.
uint8 keyPointer = 0; // Index of the next key to read from the buffer.
char keyBuffer[MAX_KEY_BUFFER]; // The keyboard buffer
_keyboardEvent keyBuffer[MAX_KEY_BUFFER]; // The keyboard buffer
void WriteKey(char key) {
void WriteKey(uint16 ascii, int keycode, int modifiers) {
if (keyBuffer && keyBacklog < MAX_KEY_BUFFER) {
keyBuffer[(keyPointer + keyBacklog) % MAX_KEY_BUFFER] = key;
_keyboardEvent *slot = &keyBuffer[(keyPointer + keyBacklog) % MAX_KEY_BUFFER];
slot->ascii = ascii;
slot->keycode = keycode;
slot->modifiers = modifiers;
keyBacklog++;
}
}
@ -82,16 +86,19 @@ BOOL KeyWaiting(void) {
return FALSE;
}
int32 ReadKey(char *key) {
int32 ReadKey(_keyboardEvent *ev) {
if (!keyBacklog)
return RDERR_NOKEYWAITING;
if (key == NULL)
if (ev == NULL)
return RDERR_INVALIDPOINTER;
*key = keyBuffer[keyPointer++];
ev->ascii = keyBuffer[keyPointer].ascii;
ev->keycode = keyBuffer[keyPointer].keycode;
ev->modifiers = keyBuffer[keyPointer].modifiers;
keyPointer++;
if (keyPointer == MAX_KEY_BUFFER)
keyPointer = 0;

View File

@ -40,6 +40,6 @@
#ifndef KEYBOARD_H
#define KEYBOARD_H
void WriteKey(char key); // Adds a keypress to the buffer
void WriteKey(uint16 ascii, int keycode, int modifier); // Adds a keypress to the buffer
#endif

View File

@ -155,7 +155,7 @@ void Sword2State::parseEvents() {
if (event.kbd.keycode == 'w')
GrabScreenShot();
}
WriteKey(event.kbd.ascii);
WriteKey(event.kbd.ascii, event.kbd.keycode, event.kbd.flags);
break;
case OSystem::EVENT_MOUSEMOVE:
mousex = event.mouse.x;

View File

@ -426,8 +426,6 @@ int32 FN_play_credits(int32 *params) {
debug(0, "Credits music length: ~%d ms", music_length);
while (g_sound->MusicTimeRemaining()) {
char key;
EraseBackBuffer();
// FIXME: Draw the credits text. The actual text
@ -436,7 +434,9 @@ int32 FN_play_credits(int32 *params) {
ServiceWindows();
if (ReadKey(&key) == RD_OK && key == 27)
_keyboardEvent ke;
if (ReadKey(&ke) == RD_OK && ke.keycode == 27)
break;
g_system->delay_msecs(30);

View File

@ -328,7 +328,7 @@ uint32 logic::Examine_run_list(void) { // Tony25Oct96
uint32 *game_object_list;
_standardHeader *file_header;
int scrolls = 0;
char c;
_keyboardEvent ke;
if (current_run_list) {
// open and lock in place
@ -353,8 +353,8 @@ uint32 logic::Examine_run_list(void) { // Tony25Oct96
} while(!KeyWaiting());
// kill the key we just pressed
ReadKey(&c);
if (c == 27)
ReadKey(&ke);
if (ke.keycode == 27)
break;
// clear the Press Esc message ready for the

View File

@ -34,7 +34,7 @@ void Console_mem_display(void) { // Tony13Aug96
int pass, found_end, k, j, free = 0;
_standardHeader *file_header;
int scrolls = 0;
char c;
_keyboardEvent ke;
char inf[][20] = {
{ "M_null " },
@ -98,8 +98,7 @@ void Console_mem_display(void) { // Tony13Aug96
Build_display();
if (scrolls==18) {
if (scrolls == 18) {
Temp_print_to_console("- Press ESC to stop or any other key to continue");
Build_display();
@ -107,8 +106,8 @@ void Console_mem_display(void) { // Tony13Aug96
ServiceWindows();
} while(!KeyWaiting());
ReadKey(&c); //kill the key we just pressed
if (c == 27) //ESC
ReadKey(&ke); //kill the key we just pressed
if (ke.keycode == 27) //ESC
break;
// clear the Press Esc message ready for the new line

View File

@ -956,7 +956,7 @@ void resMan::Kill_all_res(uint8 wantInfo) { //Tony29Nov96
uint32 nuked = 0;
_standardHeader *header;
int scrolls = 0;
char c;
_keyboardEvent ke;
j = base_mem_block;
@ -990,8 +990,8 @@ void resMan::Kill_all_res(uint8 wantInfo) { //Tony29Nov96
ServiceWindows();
} while(!KeyWaiting());
ReadKey(&c); //kill the key we just pressed
if (c == 27) //ESC
ReadKey(&ke); //kill the key we just pressed
if (ke.keycode == 27) //ESC
break;
// clear the Press Esc message ready for the new line
@ -1029,7 +1029,7 @@ void resMan::Kill_all_objects(uint8 wantInfo) { // James17jan97
uint32 nuked = 0;
_standardHeader *header;
int scrolls = 0;
char c;
_keyboardEvent ke;
j = base_mem_block;
@ -1063,8 +1063,8 @@ void resMan::Kill_all_objects(uint8 wantInfo) { // James17jan97
} while(!KeyWaiting());
ReadKey(&c); //kill the key we just pressed
if (c == 27) // ESC
ReadKey(&ke); //kill the key we just pressed
if (ke.keycode == 27) // ESC
break;
// clear the Press Esc message ready for the new line

View File

@ -176,7 +176,7 @@ uint32 Con_print_start_menu(void) { // Tony14Oct96
uint32 j;
int scrolls = 0;
char c;
_keyboardEvent ke;
if (!total_startups) {
Print_to_console("Sorry - no startup positions registered?");
@ -201,8 +201,8 @@ uint32 Con_print_start_menu(void) { // Tony14Oct96
} while(!KeyWaiting());
// kill the key we just pressed
ReadKey(&c);
if (c == 27)
ReadKey(&ke);
if (ke.keycode == 27)
break;
// clear the Press Esc message ready for the

View File

@ -258,8 +258,8 @@ int32 GameCycle(void) {
void Sword2State::go() {
OSystem::Property prop;
uint32 rv;
uint8 breakOut = 0;
char c;
uint8 breakOut = 0;
_keyboardEvent ke;
// Zdebug("[%s]", lpCmdLine);
@ -377,16 +377,19 @@ void Sword2State::go() {
#endif
if (KeyWaiting()) {
ReadKey(&c);
ReadKey(&ke);
char c = toupper(ke.ascii);
#ifdef _SWORD2_DEBUG
// ESC whether paused or not
if (c == 27) {
if (ke.keycode == 27) {
PauseAllSound(); // see sound.cpp
StartConsole(); // start the console
} else
#endif
if (gamePaused) { // if currently paused
if (toupper(c) == 'P') {
if (c == 'P') {
// 'P' while paused = unpause!
UnpauseGame();
}
@ -394,21 +397,21 @@ void Sword2State::go() {
// frame-skipping only allowed on
// debug version
else if (toupper(c) == ' ') {
else if (c == ' ') {
// SPACE bar while paused =
// step one frame!
stepOneCycle = 1;
UnpauseGame();
}
#endif
} else if (toupper(c) == 'P') {
} else if (c == 'P') {
// 'P' while not paused = pause!
PauseGame();
} else if (toupper(c) == 'C' && _gameId == GID_SWORD2) {
} else if (c == 'C' && _gameId == GID_SWORD2) {
FN_play_credits(NULL);
}
#ifdef _SWORD2_DEBUG
else if (toupper(c) == 'S') {
else if (c == 'S') {
// 'S' toggles speed up (by skipping
// display rendering)
renderSkip = 1 - renderSkip;