add existing methods from system lib to window lib

This commit is contained in:
SwissalpS 2020-07-04 19:54:53 +02:00
parent 3486e0d430
commit 7d80cdcaa2
1 changed files with 61 additions and 6 deletions

View File

@ -9,6 +9,7 @@ extern SDL_Window *window;
static const char *window_opts[] = { "normal", "maximized", "fullscreen", 0 }; static const char *window_opts[] = { "normal", "maximized", "fullscreen", 0 };
enum { WIN_NORMAL, WIN_MAXIMIZED, WIN_FULLSCREEN };
static int f_get_window_mode(lua_State *L) { static int f_get_window_mode(lua_State *L) {
unsigned flags = SDL_GetWindowFlags(window); unsigned flags = SDL_GetWindowFlags(window);
@ -43,6 +44,15 @@ static int f_get_window_size(lua_State *L) {
return 2; return 2;
} }
static int f_set_window_mode(lua_State *L) {
int n = luaL_checkoption(L, 1, "normal", window_opts);
SDL_SetWindowFullscreen(window,
n == WIN_FULLSCREEN ? SDL_WINDOW_FULLSCREEN_DESKTOP : 0);
if (n == WIN_NORMAL) { SDL_RestoreWindow(window); }
if (n == WIN_MAXIMIZED) { SDL_MaximizeWindow(window); }
return 0;
}
// does not seem to work on Fedora Gnome Wayland // does not seem to work on Fedora Gnome Wayland
// it returns true but visually nothing changes // it returns true but visually nothing changes
// possibly bc the view paints solid? // possibly bc the view paints solid?
@ -77,13 +87,58 @@ static int f_set_window_size(lua_State *L) {
} }
static int f_set_window_title(lua_State *L) {
const char *title = luaL_checkstring(L, 1);
SDL_SetWindowTitle(window, title);
return 0;
}
static int f_show_confirm_dialog(lua_State *L) {
const char *title = luaL_checkstring(L, 1);
const char *msg = luaL_checkstring(L, 2);
#if _WIN32
int id = MessageBox(0, msg, title, MB_YESNO | MB_ICONWARNING);
lua_pushboolean(L, id == IDYES);
#else
SDL_MessageBoxButtonData buttons[] = {
{ SDL_MESSAGEBOX_BUTTON_RETURNKEY_DEFAULT, 1, "Yes" },
{ SDL_MESSAGEBOX_BUTTON_ESCAPEKEY_DEFAULT, 0, "No" },
};
SDL_MessageBoxData data = {
.title = title,
.message = msg,
.numbuttons = 2,
.buttons = buttons,
};
int buttonid;
SDL_ShowMessageBox(&data, &buttonid);
lua_pushboolean(L, buttonid == 1);
#endif
return 1;
}
static int f_window_has_focus(lua_State *L) {
unsigned flags = SDL_GetWindowFlags(window);
lua_pushboolean(L, flags & SDL_WINDOW_INPUT_FOCUS);
return 1;
}
static const luaL_Reg lib[] = { static const luaL_Reg lib[] = {
{ "get_mode", f_get_window_mode }, { "get_mode", f_get_window_mode },
{ "get_position", f_get_window_position }, { "get_position", f_get_window_position },
{ "get_size", f_get_window_size }, { "get_size", f_get_window_size },
{ "set_mode", f_set_window_mode },
{ "set_opacity", f_set_window_opacity }, { "set_opacity", f_set_window_opacity },
{ "set_position", f_set_window_position }, { "set_position", f_set_window_position },
{ "set_size", f_set_window_size }, { "set_size", f_set_window_size },
{ "set_title", f_set_window_title },
{ "show_confirm_dialog", f_show_confirm_dialog },
{ "has_focus", f_window_has_focus },
{ NULL, NULL } { NULL, NULL }
}; };