Lua: upgrade gui.popup, add input.popup.

This commit is contained in:
gocha 2009-10-12 22:30:06 +00:00
parent 643f8f7cff
commit e462f55688
2 changed files with 70 additions and 37 deletions

View File

@ -2583,48 +2583,69 @@ static int gui_register(lua_State *L) {
} }
// string gui.popup(string message, [string type = "ok"]) static int doPopup(lua_State *L, const char* deftype, const char* deficon) {
// const char *str = luaL_checkstring(L, 1);
// Popup dialog! const char* type = lua_type(L,2) == LUA_TSTRING ? lua_tostring(L,2) : deftype;
int gui_popup(lua_State *L) { const char* icon = lua_type(L,3) == LUA_TSTRING ? lua_tostring(L,3) : deficon;
const char *message = luaL_checkstring(L, 1);
const char *type = luaL_optstring(L, 2, "ok"); int itype = -1, iters = 0;
while(itype == -1 && iters++ < 2)
{
if(!stricmp(type, "ok")) itype = 0;
else if(!stricmp(type, "yesno")) itype = 1;
else if(!stricmp(type, "yesnocancel")) itype = 2;
else if(!stricmp(type, "okcancel")) itype = 3;
else if(!stricmp(type, "abortretryignore")) itype = 4;
else type = deftype;
}
assert(itype >= 0 && itype <= 4);
if(!(itype >= 0 && itype <= 4)) itype = 0;
int iicon = -1; iters = 0;
while(iicon == -1 && iters++ < 2)
{
if(!stricmp(icon, "message") || !stricmp(icon, "notice")) iicon = 0;
else if(!stricmp(icon, "question")) iicon = 1;
else if(!stricmp(icon, "warning")) iicon = 2;
else if(!stricmp(icon, "error")) iicon = 3;
else icon = deficon;
}
assert(iicon >= 0 && iicon <= 3);
if(!(iicon >= 0 && iicon <= 3)) iicon = 0;
static const char * const titles [] = {"Notice", "Question", "Warning", "Error"};
const char* answer = "ok";
#ifdef WIN32 #ifdef WIN32
int t; static const int etypes [] = {MB_OK, MB_YESNO, MB_YESNOCANCEL, MB_OKCANCEL, MB_ABORTRETRYIGNORE};
if (strcmp(type, "ok") == 0) static const int eicons [] = {MB_ICONINFORMATION, MB_ICONQUESTION, MB_ICONWARNING, MB_ICONERROR};
t = MB_OK; //StopSound(); //mbg merge 7/27/08
else if (strcmp(type, "yesno") == 0) int ianswer = MessageBox(hAppWnd, str, titles[iicon], etypes[itype] | eicons[iicon]);
t = MB_YESNO; switch(ianswer)
else if (strcmp(type, "yesnocancel") == 0) {
t = MB_YESNOCANCEL; case IDOK: answer = "ok"; break;
else case IDCANCEL: answer = "cancel"; break;
return luaL_error(L, "invalid popup type \"%s\"", type); case IDABORT: answer = "abort"; break;
case IDRETRY: answer = "retry"; break;
//StopSound(); //mbg merge 7/27/08 case IDIGNORE: answer = "ignore"; break;
int result = MessageBox(hAppWnd, message, "Lua Script Pop-up", t); case IDYES: answer = "yes"; break;
case IDNO: answer = "no"; break;
lua_settop(L,1);
if (t != MB_OK) {
if (result == IDYES)
lua_pushstring(L, "yes");
else if (result == IDNO)
lua_pushstring(L, "no");
else if (result == IDCANCEL)
lua_pushstring(L, "cancel");
else
luaL_error(L, "win32 unrecognized return value %d", result);
return 1;
} }
// else, we don't care. lua_pushstring(L, answer);
return 0; return 1;
#else #else
char *t; char *t;
#ifdef __linux #ifdef __linux
// The Linux backend has a "FromPause" variable.
// If set to 1, assume some known external event has screwed with the flow of time.
// Since this pauses the emulator waiting for a response, we set it to 1.
extern int FromPause;
FromPause = 1;
int pid; // appease compiler int pid; // appease compiler
// Before doing any work, verify the correctness of the parameters. // Before doing any work, verify the correctness of the parameters.
@ -2680,7 +2701,7 @@ int gui_popup(lua_State *L) {
// I'm gonna be dead in a matter of microseconds anyways, so wasted memory doesn't matter to me. // I'm gonna be dead in a matter of microseconds anyways, so wasted memory doesn't matter to me.
// Go ahead and abuse strdup. // Go ahead and abuse strdup.
char * parameters[] = {"xmessage", "-buttons", t, strdup(message), NULL}; char * parameters[] = {"xmessage", "-buttons", t, strdup(str), NULL};
execvp("xmessage", parameters); execvp("xmessage", parameters);
@ -2739,9 +2760,9 @@ use_console:
else else
return luaL_error(L, "invalid popup type \"%s\"", type); return luaL_error(L, "invalid popup type \"%s\"", type);
fprintf(stderr, "Lua Message: %s\n", message); fprintf(stderr, "Lua Message: %s\n", str);
while (TRUE) { while (true) {
char buffer[64]; char buffer[64];
// We don't want parameters // We don't want parameters
@ -2782,6 +2803,17 @@ use_console:
} }
// string gui.popup(string message, string type = "ok", string icon = "message")
// string input.popup(string message, string type = "yesno", string icon = "question")
static int gui_popup(lua_State *L)
{
return doPopup(L, "ok", "message");
}
static int input_popup(lua_State *L)
{
return doPopup(L, "yesno", "question");
}
// the following bit operations are ported from LuaBitOp 1.0.1, // the following bit operations are ported from LuaBitOp 1.0.1,
// because it can handle the sign bit (bit 31) correctly. // because it can handle the sign bit (bit 31) correctly.
@ -3170,6 +3202,7 @@ static const struct luaL_reg zapperlib[] = {
static const struct luaL_reg inputlib[] = { static const struct luaL_reg inputlib[] = {
{"get", input_get}, {"get", input_get},
{"popup", input_popup},
// alternative names // alternative names
{"read", input_get}, {"read", input_get},
{NULL,NULL} {NULL,NULL}

Binary file not shown.