From 3486e0d4307e9462591901e3b8ad7b2ee6e0b4eb Mon Sep 17 00:00:00 2001 From: SwissalpS Date: Sat, 4 Jul 2020 19:15:59 +0200 Subject: [PATCH 1/3] expose some window api --- src/api/api.c | 3 ++ src/api/window.c | 95 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 98 insertions(+) create mode 100644 src/api/window.c diff --git a/src/api/api.c b/src/api/api.c index 34067a9c..7b8bc967 100644 --- a/src/api/api.c +++ b/src/api/api.c @@ -3,11 +3,13 @@ int luaopen_system(lua_State *L); int luaopen_renderer(lua_State *L); +int luaopen_window(lua_State *L); static const luaL_Reg libs[] = { { "system", luaopen_system }, { "renderer", luaopen_renderer }, + { "window", luaopen_window }, { NULL, NULL } }; @@ -16,3 +18,4 @@ void api_load_libs(lua_State *L) { luaL_requiref(L, libs[i].name, libs[i].func, 1); } } + diff --git a/src/api/window.c b/src/api/window.c new file mode 100644 index 00000000..fb272d39 --- /dev/null +++ b/src/api/window.c @@ -0,0 +1,95 @@ +#include +#include +#include "api.h" +#ifdef _WIN32 + #include +#endif + +extern SDL_Window *window; + + +static const char *window_opts[] = { "normal", "maximized", "fullscreen", 0 }; + +static int f_get_window_mode(lua_State *L) { + unsigned flags = SDL_GetWindowFlags(window); + bool isFullscreen = flags & SDL_WINDOW_FULLSCREEN_DESKTOP; + bool isMaximized = flags & SDL_WINDOW_MAXIMIZED; + int index = 0; + if (isFullscreen) { + index = 2; + } else if (isMaximized) { + index = 1; + } + lua_pushstring(L, window_opts[index]); + return 1; +} + + +static int f_get_window_position(lua_State *L) { + int x, y; + SDL_GetWindowPosition(window, &x, &y); + lua_pushnumber(L, x); + lua_pushnumber(L, y); + return 2; +} + + +static int f_get_window_size(lua_State *L) { + int w = 0; + int h = 0; + SDL_GetWindowSize(window, &w, &h); + lua_pushnumber(L, w); + lua_pushnumber(L, h); + return 2; +} + +// does not seem to work on Fedora Gnome Wayland +// it returns true but visually nothing changes +// possibly bc the view paints solid? +static int f_set_window_opacity(lua_State *L) { + double n = luaL_checknumber(L, 1); + int r = SDL_SetWindowOpacity(window, n); + lua_pushboolean(L, r > -1); + return 1; +} + + +static int f_set_window_position(lua_State *L) { + int x = luaL_checknumber(L, 1); + int y = luaL_checknumber(L, 1); + SDL_SetWindowPosition(window, x, y); + return 0; +} + + +static int f_set_window_size(lua_State *L) { + int w = luaL_checknumber(L, 1); + int h = luaL_checknumber(L, 1); + if ((0 >= w) || (0 >= h)) { + lua_pushboolean(L, false); + lua_pushstring(L, "Width and height must be bigger than 0"); + return 2; + } + SDL_SetWindowSize(window, w, h); + lua_pushboolean(L, true); + lua_pushnil(L); + return 2; +} + + +static const luaL_Reg lib[] = { + { "get_mode", f_get_window_mode }, + { "get_position", f_get_window_position }, + { "get_size", f_get_window_size }, + { "set_opacity", f_set_window_opacity }, + { "set_position", f_set_window_position }, + { "set_size", f_set_window_size }, + { NULL, NULL } +}; + + +int luaopen_window(lua_State *L) { + luaL_newlib(L, lib); + return 1; +} + From 7d80cdcaa292101e83166d88bf38be63f5fec065 Mon Sep 17 00:00:00 2001 From: SwissalpS Date: Sat, 4 Jul 2020 19:54:53 +0200 Subject: [PATCH 2/3] add existing methods from system lib to window lib --- src/api/window.c | 67 +++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 61 insertions(+), 6 deletions(-) diff --git a/src/api/window.c b/src/api/window.c index fb272d39..b4e1998c 100644 --- a/src/api/window.c +++ b/src/api/window.c @@ -9,6 +9,7 @@ extern SDL_Window *window; 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) { unsigned flags = SDL_GetWindowFlags(window); @@ -43,6 +44,15 @@ static int f_get_window_size(lua_State *L) { 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 // it returns true but visually nothing changes // 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[] = { - { "get_mode", f_get_window_mode }, - { "get_position", f_get_window_position }, - { "get_size", f_get_window_size }, - { "set_opacity", f_set_window_opacity }, - { "set_position", f_set_window_position }, - { "set_size", f_set_window_size }, + { "get_mode", f_get_window_mode }, + { "get_position", f_get_window_position }, + { "get_size", f_get_window_size }, + { "set_mode", f_set_window_mode }, + { "set_opacity", f_set_window_opacity }, + { "set_position", f_set_window_position }, + { "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 } }; From 4dc0e73cbee3838518faa2a82c8c13828957d26b Mon Sep 17 00:00:00 2001 From: SwissalpS Date: Sat, 4 Jul 2020 19:56:04 +0200 Subject: [PATCH 3/3] remove not working opacity method --- src/api/window.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/api/window.c b/src/api/window.c index b4e1998c..3043fb30 100644 --- a/src/api/window.c +++ b/src/api/window.c @@ -56,12 +56,14 @@ static int f_set_window_mode(lua_State *L) { // does not seem to work on Fedora Gnome Wayland // it returns true but visually nothing changes // possibly bc the view paints solid? +/* static int f_set_window_opacity(lua_State *L) { double n = luaL_checknumber(L, 1); int r = SDL_SetWindowOpacity(window, n); lua_pushboolean(L, r > -1); return 1; } +*/ static int f_set_window_position(lua_State *L) { @@ -133,7 +135,7 @@ static const luaL_Reg lib[] = { { "get_position", f_get_window_position }, { "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_size", f_set_window_size }, { "set_title", f_set_window_title },