Menu support for hotkeys and 'text entry' box, savegame support for the name from the text entry

This commit is contained in:
Erich Edgar Hoover 2005-12-26 19:26:46 +00:00
parent 6f145cbe13
commit 848b96039b
3 changed files with 59 additions and 27 deletions

View file

@ -32,6 +32,12 @@
#include <SDL_timer.h>
#include <assert.h>
// CHAR_KEY tests to see whether a keycode is for
// a "character" handler or a "button" handler
#define CHAR_KEY(k) (k >= SDLK_a && k <= SDLK_z) || (k >= SDLK_0 && k <= SDLK_9) || k == SDLK_SPACE
// numupper provides conversion between number keys and their "upper case"
const char numupper[] = {')', '!', '@', '#', '$', '%', '^', '&', '*', '('};
Engine *g_engine = NULL;
extern Imuse *g_imuse;
@ -97,21 +103,60 @@ Engine::Engine() :
printLineDefaults.justify = 2;
}
void Engine::handleButton(int operation, int key) {
void Engine::handleButton(int operation, int key, int keyModifier) {
lua_Object handler, system_table, userPaintHandler;
// If we're not supposed to handle the key then don't
if (!_controlsEnabled[key])
return;
lua_beginblock();
lua_Object handler = getEventHandler("buttonHandler");
if (handler != LUA_NOOBJECT) {
lua_pushnumber(key);
if (operation == SDL_KEYDOWN)
lua_pushnumber(1);
else
system_table = lua_getglobal("system");
userPaintHandler = getTableValue(system_table, "userPaintHandler");
if (userPaintHandler != LUA_NOOBJECT && CHAR_KEY(key)) {
handler = getTableFunction(userPaintHandler, "characterHandler");
// Ignore SDL_KEYUP so there are not duplicate keystrokes, but
// don't pass on to the normal buttonHandler since it doesn't
// recognize character codes
if (handler != LUA_NOOBJECT && operation == SDL_KEYDOWN) {
char keychar[2];
lua_beginblock();
lua_pushobject(userPaintHandler);
if (keyModifier & KMOD_SHIFT)
if (isalpha(key))
keychar[0] = toupper(key);
else
keychar[0] = numupper[key - SDLK_0];
else
keychar[0] = key;
keychar[1] = '\0';
lua_pushstring(keychar);
lua_pushnil();
lua_pushnil();
lua_callfunction(handler);
lua_callfunction(handler);
lua_endblock();
}
} else {
// Only allow the "Q" safe-exit when in-game, otherwise
// it interferes with menu operation
if (key == SDLK_q) {
lua_beginblock();
lua_Object handler = getEventHandler("exitHandler");
if (handler != LUA_NOOBJECT)
lua_callfunction(handler);
lua_endblock();
} else {
handler = getEventHandler("buttonHandler");
if (handler != LUA_NOOBJECT) {
lua_pushnumber(key);
if (operation == SDL_KEYDOWN)
lua_pushnumber(1);
else
lua_pushnil();
lua_pushnil();
lua_callfunction(handler);
}
}
}
lua_endblock();
}
@ -355,7 +400,7 @@ void Engine::mainLoop() {
while (SDL_PollEvent(&event)) {
// Handle any button operations
if (event.type == SDL_KEYDOWN || event.type == SDL_KEYUP)
handleButton(event.type, event.key.keysym.sym);
handleButton(event.type, event.key.keysym.sym, event.key.keysym.mod);
// Check for "Hard" quit"
if (event.type == SDL_QUIT)
return;
@ -371,13 +416,6 @@ void Engine::mainLoop() {
(event.key.keysym.mod & KMOD_ALT)) {
g_driver->toggleFullscreenMode();
}
if (event.key.keysym.sym == SDLK_q) {
lua_beginblock();
lua_Object handler = getEventHandler("exitHandler");
if (handler != LUA_NOOBJECT)
lua_callfunction(handler);
lua_endblock();
}
}
}

View file

@ -213,7 +213,7 @@ public:
private:
void handleButton(int operation, int key);
void handleButton(int operation, int key, int keyModifier);
Scene *_currScene;
int _mode, _previousMode;

12
lua.cpp
View file

@ -2883,9 +2883,6 @@ static void SubmitSaveGameData() {
if (lua_isnil(table2))
break;
str = lua_getstring(table2);
// Leave out an apparently bogus option
if (!strcmp(str, "000000000000000000000000000000000000000000000000000000000000"))
continue;
int len = strlen(str) + 1;
savedState->writeBlock(&len, sizeof(int));
savedState->writeBlock(str, len);
@ -2912,12 +2909,6 @@ static void GetSaveGameData() {
int strSize;
int count = 0;
// Get the name of the saved game
lua_pushobject(result);
lua_pushnumber(count++);
lua_pushstring(filename); // TODO: Use an actual stored name
lua_settable();
for (;;) {
if (dataSize <= 0)
break;
@ -3053,6 +3044,9 @@ static void Display() {
if (g_engine->getFlipEnable()) {
g_driver->flipBuffer();
}
// refreshDrawMode ensures that the blinking cursor
// for the "text entry" renders correctly
g_engine->refreshDrawMode();
}
static void EngineDisplay() {