diff --git a/Makefile b/Makefile index 4d3aaed..709057f 100644 --- a/Makefile +++ b/Makefile @@ -4,7 +4,7 @@ SOURCES := source/include/ftp source/include source source/include/audiodec INCLUDES := include LIBS = -lcurl -lssl -lcrypto -lvorbisfile -lvorbis -logg -lsndfile -lvita2d -lSceLibKernel_stub -lScePvf_stub \ - -lSceJpegEnc_stub -lSceAppMgr_stub -lSceCtrl_stub -lSceTouch_stub -lSceMotion_stub \ + -limgui_vita2d -lSceJpegEnc_stub -lSceAppMgr_stub -lSceCtrl_stub -lSceTouch_stub -lSceMotion_stub \ -lScePromoterUtil_stub -lm -lSceNet_stub -lSceNetCtl_stub -lSceAppUtil_stub -lScePgf_stub \ -ljpeg -lfreetype -lc -lScePower_stub -lSceCommonDialog_stub -lpng16 -lz -lSceCamera_stub \ -lspeexdsp -lmpg123 -lSceAudio_stub -lSceGxm_stub -lSceDisplay_stub -lSceShellSvc_stub \ diff --git a/doc/luaGui.cpp b/doc/luaGui.cpp new file mode 100644 index 0000000..be4b8dc --- /dev/null +++ b/doc/luaGui.cpp @@ -0,0 +1,436 @@ +/** + * \defgroup Gui + * Module that handles 2D GUI development through dear ImGui wrapper. + */ + +/** + * Themes constants to use with ::Gui.setTheme. + * \ingroup System + */ +enum GuiTheme{ + DARK_THEME, //!< Dark Theme. + LIGHT_THEME, //!< Light Theme. + CLASSIC_THEME //!< Classic Theme +}; + +/** + * Window flags to use with ::Gui.initWindow. + * \ingroup System + */ +enum WinFlags{ + FLAG_NONE, //!< No flags. + FLAG_NO_COLLAPSE, //!< The window can't be collapsed. + FLAG_NO_MOVE, //!< The window can't be moved. + FLAG_NO_RESIZE, //!< The window can't be resized. + FLAG_NO_SCROLLBAR, //!< The window has no vertical scrollbar. + FLAG_NO_TITLEBAR, //!< The window doesn't show a label. + FLAG_HORIZONTAL_SCROLLBAR //!< The window has an horizontal scrollbar. +}; + +/** + * Function mode to alter frequency of execution. + * \ingroup System + */ +enum ConfigMode{ + SET_ONCE, //!< The function is executed only once. + SET_ALWAYS //!< The function is executed at every frame. +}; + +class Gui { + + public: + + /** + * Init Gui sub-system. + * \ingroup Gui + * + * @par Usage example: + * @code + * Gui.init() + * @endcode + */ + void init(void); + + /** + * Terminate Gui sub-system. + * \ingroup Gui + * + * @par Usage example: + * @code + * Gui.term() + * @endcode + */ + void term(void); + + /** + * Init Gui drawing phase. + * \ingroup Gui + * + * @par Usage example: + * @code + * Gui.initBlend() + * @endcode + */ + void initBlend(void); + + /** + * Terminate Gui drawing phase. + * \ingroup Gui + * + * @par Usage example: + * @code + * Gui.termBlend() + * @endcode + */ + void termBlend(void); + + /** + * Change theme for the running Gui. + * \ingroup Gui + * + * @par Usage example: + * @code + * Gui.setTheme(DARK_THEME) + * end + * @endcode + * + * @param theme - The theme to set. + */ + void setTheme(GuiTheme theme); + + /** + * Change input mode for the running Gui. + * \ingroup Gui + * + * @par Usage example: + * @code + * Gui.setInputMode(true, false, true, true) + * end + * @endcode + * + * @param use_touch - Enable front touch usage for cursor movement. + * @param use_rear - Enable rearpad touch usage for cursor movement. + * @param use_buttons - Enable buttons usage for cursor movement. + * @param indirect_touch - If enabled, cursor is moved by dragging it. If disabled, cursor jumps to the touched location. + */ + void setInputMode(bool use_touch, bool use_rear, bool use_buttons, bool indirect_touch); + + /** + * Init screen menubar. + * \ingroup Gui + * + * @par Usage example: + * @code + * if Gui.initMainMenubar() then + * Gui.termMainMenubar() + * end + * @endcode + * + * @return true if the menubar is opened, false otherwise. + */ + bool initMainMenubar(void); + + /** + * Terminate screen menubar. + * \ingroup Gui + * + * @par Usage example: + * @code + * if Gui.initMainMenubar() then + * Gui.termMainMenubar() + * end + * @endcode + */ + void termMainMenubar(void); + + /** + * Init a menu. + * \ingroup Gui + * + * @par Usage example: + * @code + * if Gui.initMenu("My Menu") then + * Gui.termMenu() + * end + * @endcode + * + * @param label - The label to show. + * @param enabled - The menu status to set (optional). + * + * @return true if the menu is opened, false otherwise. + */ + bool initMenu(string label, bool enabled); + + /** + * Terminate a menu. + * \ingroup Gui + * + * @par Usage example: + * @code + * if Gui.initMenu() then + * Gui.termMenu() + * end + * @endcode + */ + void termMenu(void); + + /** + * Draw a menu item. + * \ingroup Gui + * + * @par Usage example: + * @code + * if Gui.drawMenuItem("Feature", is_enabled) then + * is_enabled = true + * end + * @endcode + * @param label - The label to show. + * @param selected - The item checked status (optional). + * @param enabled - The item status to set (optional). + * + * @return true if clicked, false otherwise. + */ + bool drawMenuItem(string label, bool selected, bool enabled); + + /** + * Draw a text. + * \ingroup Gui + * + * @par Usage example: + * @code + * Gui.drawText("Hello World", Color.new(255, 255, 0)) + * @endcode + * + * @param label - The label to show. + * @param color - A valid color (See ::Color) (optional). + */ + void drawText(string label, int color); + + /** + * Draw a greyed out text. + * \ingroup Gui + * + * @par Usage example: + * @code + * Gui.drawDisabledText("Hello World") + * @endcode + * + * @param label - The label to show. + */ + void drawDisabledText(string label); + + /** + * Draw a text with automatic newlines to fit the window. + * \ingroup Gui + * + * @par Usage example: + * @code + * Gui.drawWrappedText("Hello World") + * @endcode + * + * @param label - The label to show. + */ + void drawWrappedText(string label); + + /** + * Draw a button. + * \ingroup Gui + * + * @par Usage example: + * @code + * if Gui.drawButton("Exit") then + * System.exit() + * end + * @endcode + * + * @param label - The label to show. + * @param width - The width of the button (optional). + * @param height - The height of the button (optional). + * + * @return true if the button has been clicked, false otherwise. + */ + bool drawButton(string label, number width, number height); + + /** + * Draw a small button. + * \ingroup Gui + * + * @par Usage example: + * @code + * if Gui.drawSmallButton("Exit") then + * System.exit() + * end + * @endcode + * + * @param label - The label to show. + * + * @return true if the button has been clicked, false otherwise. + */ + bool drawSmallButton(string label); + + /** + * Draw a checkbox. + * \ingroup Gui + * + * @par Usage example: + * @code + * is_checked = Gui.drawCheckbox("Check", is_checked) + * @endcode + * + * @param label - The label to show. + * @param status - The initial checked status of the checkbox. + * + * @return true if the checkbox is checked, false otherwise. + */ + bool drawCheckbox(string label, bool status); + + /** + * Draw a radiobutton. + * \ingroup Gui + * + * @par Usage example: + * @code + * is_checked = Gui.drawRadiobutton("Check", is_checked) + * @endcode + * + * @param label - The label to show. + * @param status - The initial checked status of the radiobutton. + * + * @return true if the radiobutton is checked, false otherwise. + */ + bool drawRadioButton(string label, bool status); + + /** + * Make next element be drawn on current line. + * \ingroup Gui + * + * @par Usage example: + * @code + * Gui.resetLine() + * @endcode + */ + void resetLine(void); + + /** + * Init a window. + * \ingroup Gui + * + * @par Usage example: + * @code + * Gui.initWindow("Window", FLAG_NO_MOVE + FLAG_NO_RESIZE + FLAG_NO_TITLEBAR + FLAG_NO_COLLAPSE) + * @endcode + * + * @param label - The label to show. + * @param flags - The flags to use with the window. + */ + void initWindow(string label, WinFlags flags); + + /** + * Terminate a window. + * \ingroup Gui + * + * @par Usage example: + * @code + * Gui.termWindow() + * @endcode + */ + void termWindow(void); + + /** + * Set next window position. + * \ingroup Gui + * + * @par Usage example: + * @code + * Gui.setWindowPos(100, 100, SET_ALWAYS) + * @endcode + * + * @param x - X coordinate of the window position in pixels. + * @param y - Y coordinate of the window position in pixels. + * @param mode - A mode to use for the function. + */ + void setWindowPos(number x, number y, ConfigMode mode); + + /** + * Set next window size. + * \ingroup Gui + * + * @par Usage example: + * @code + * Gui.setWindowSize(100, 100, SET_ALWAYS) + * @endcode + * + * @param w -Width of the window in pixels. + * @param h - Height of the window in pixels. + * @param mode - A mode to use for the function. + */ + void setWindowSize(number w, number h, ConfigMode mode); + + /** + * Draw a separator. + * \ingroup Gui + * + * @par Usage example: + * @code + * Gui.drawSeparator() + * @endcode + */ + void drawSeparator(void); + + /** + * Draw a tooltip when cursor hover on the previous item. + * \ingroup Gui + * + * @par Usage example: + * @code + * Gui.drawText("Text") + * Gui.drawTooltip("This is a textbox") + * @endcode + * + * @param label - The label to show. + */ + void drawTooltip(string label); + + /** + * Draw a slider with variable amount of values. + * \ingroup Gui + * + * @par Usage example: + * @code + * slider_val = Gui.drawSlider("A slider", 0, 100, slider_val) + * @endcode + * + * @param label - The label to show. + * @param val_min - The minimum value settable through the slider. + * @param val_max - The maximum value settable through the slider. + * @param val1 - The first value to handle with the slider. + * @param val2 - The second value to handle with the slider (optional). + * @param val3 - The third value to handle with the slider (optional). + * @param val4 - The forth value to handle with the slider (optional). + * + * @return The updated values after the frame execution. + */ + number[] drawSlider(string label, number val_min, number val_max, number val1, number val2, number val3, number val4); + + /** + * Draw a slider with variable amount of integer values. + * \ingroup Gui + * + * @par Usage example: + * @code + * slider_val = Gui.drawIntSlider("A int slider", 0, 100, slider_val) + * @endcode + * + * @param label - The label to show. + * @param val_min - The minimum value settable through the slider. + * @param val_max - The maximum value settable through the slider. + * @param val1 - The first value to handle with the slider. + * @param val2 - The second value to handle with the slider (optional). + * @param val3 - The third value to handle with the slider (optional). + * @param val4 - The forth value to handle with the slider (optional). + * + * @return The updated values after the frame execution. + */ + int[] drawIntSlider(string label, int val_min, int val_max, int val1,int val2, int val3, int val4); + +} \ No newline at end of file diff --git a/samples/Gui/index.lua b/samples/Gui/index.lua new file mode 100644 index 0000000..a4184a5 --- /dev/null +++ b/samples/Gui/index.lua @@ -0,0 +1,82 @@ +Gui.init() +Gui.setInputMode(true, false, true, true) + +is_checked = false +theme = DARK_THEME + +radio_set = 1 + +slider_val = 52.4 +slider_int_val = 38 + +-- Main loop +while true do + + Graphics.initBlend() + Screen.clear() + Gui.initBlend() + + -- Main menubar + if Gui.initMainMenubar() then + if Gui.initMenu("Settings") then + if Gui.initMenu("Theme") then + if Gui.drawMenuItem("Dark", theme == DARK_THEME) then + theme = DARK_THEME + Gui.setTheme(theme) + end + if Gui.drawMenuItem("Light", theme == LIGHT_THEME) then + theme = LIGHT_THEME + Gui.setTheme(theme) + end + if Gui.drawMenuItem("Classic", theme == CLASSIC_THEME) then + theme = CLASSIC_THEME + Gui.setTheme(theme) + end + Gui.termMenu() + end + if Gui.drawMenuItem("Exit Sample") then + System.exit() + end + Gui.termMenu() + end + Gui.termMainMenubar() + end + + -- Main window + Gui.setWindowPos(0, 22, SET_ALWAYS) + Gui.setWindowSize(960, 544-22, SET_ALWAYS) + Gui.initWindow("#main_window#", FLAG_NO_MOVE + FLAG_NO_RESIZE + FLAG_NO_TITLEBAR + FLAG_NO_COLLAPSE) + Gui.drawText("Hello World ImGui") + Gui.drawText("Inside lpp-vita through Gui module!", Color.new(255,0,0)) + Gui.drawSeparator() + is_checked = Gui.drawCheckbox("Checkbox", is_checked) + Gui.drawSeparator() + if Gui.drawRadioButton("Radio 1", radio_set == 1) then + radio_set = 1 + end + if Gui.drawRadioButton("Radio 2", radio_set == 2) then + radio_set = 2 + end + if Gui.drawRadioButton("Radio 3", radio_set == 3) then + radio_set = 3 + end + Gui.drawSeparator() + slider_val = Gui.drawSlider("A slider", 0, 100, slider_val) + slider_int_val = Gui.drawIntSlider("A integer slider", 0, 100, slider_int_val) + Gui.drawSeparator() + if Gui.drawButton("Exit the sample") then + System.exit() + end + Gui.resetLine() + if Gui.drawButton("Reboot device") then + System.reboot() + end + Gui.termWindow() + + Gui.termBlend() + Graphics.termBlend() + + -- Flip screen + Screen.flip() + +end \ No newline at end of file diff --git a/source/include/luaplayer.h b/source/include/luaplayer.h index 1bad889..30a3045 100644 --- a/source/include/luaplayer.h +++ b/source/include/luaplayer.h @@ -62,6 +62,7 @@ void luaRender_init(lua_State *L); void luaMic_init(lua_State *L); void luaVideo_init(lua_State *L); void luaDatabase_init(lua_State *L); +void luaGui_init(lua_State *L); void bitlib_init(lua_State *L); // lua-compat diff --git a/source/luaGui.cpp b/source/luaGui.cpp new file mode 100644 index 0000000..dfc7d61 --- /dev/null +++ b/source/luaGui.cpp @@ -0,0 +1,551 @@ +/*---------------------------------------------------------------------------------------------------------------------# +#----------------------------------------------------------------------------------------------------------------------# +#------ This File is Part Of : ---------------------------------------------------------------------------------------# +#------- _ ------------------- ______ _ --------------------------------------------------------------------------# +#------ | | ------------------- (_____ \| | --------------------------------------------------------------------------# +#------ | | --- _ _ ____ _____) )| | ____ _ _ ____ ____ ----------------------------------------------# +#------ | | --- | | | | / _ | | ____/| | / _ || | | | / _ ) / ___) ----------------------------------------------# +#------ | |_____| |_| |( ( | | | | | |( ( | || |_| |( (/ / | | --------------------------------------------------# +#------ |_______)\____| \_||_| |_| |_| \_||_| \__ | \____)|_| --------------------------------------------------# +#------------------------------------------------- (____/ -------------------------------------------------------------# +#------------------------ ______ _ ------------------------------------------------------------------------------# +#------------------------ (_____ \ | | ------------------------------------------------------------------------------# +#------------------------ _____) )| | _ _ ___ -----------------------------------------------------------------# +#------------------------ | ____/ | || | | | /___) -----------------------------------------------------------------# +#------------------------ | | | || |_| ||___ | -----------------------------------------------------------------# +#------------------------ |_| |_| \____|(___/ -----------------------------------------------------------------# +#----------------------------------------------------------------------------------------------------------------------# +#----------------------------------------------------------------------------------------------------------------------# +#- Licensed under the GPL License -------------------------------------------------------------------------------------# +#----------------------------------------------------------------------------------------------------------------------# +#- Copyright (c) Nanni --------------------------------------------------------------------------# +#- Copyright (c) Rinnegatamante ------------------------------------------------------------# +#----------------------------------------------------------------------------------------------------------------------# +#----------------------------------------------------------------------------------------------------------------------# +#- Credits : ----------------------------------------------------------------------------------------------------------# +#----------------------------------------------------------------------------------------------------------------------# +#- All the devs involved in Rejuvenate and vita-toolchain -------------------------------------------------------------# +#- xerpi for drawing libs and for FTP server code ---------------------------------------------------------------------# +#----------------------------------------------------------------------------------------------------------------------*/ + +#include +#include +#include +extern "C"{ +#include +} +#include +#include "include/luaplayer.h" +#define stringify(str) #str +#define VariableRegister(lua, value) do { lua_pushinteger(lua, value); lua_setglobal (lua, stringify(value)); } while(0) + +enum { + DARK_THEME, + LIGHT_THEME, + CLASSIC_THEME +}; + +enum { + SET_ONCE, + SET_ALWAYS +}; + +enum { + FLAG_NONE = 0, + FLAG_NO_TITLEBAR = 1 << 0, + FLAG_NO_RESIZE = 1 << 1, + FLAG_NO_MOVE = 1 << 2, + FLAG_NO_SCROLLBAR = 1 << 3, + FLAG_NO_COLLAPSE = 1 << 5, + FLAG_HORIZONTAL_SCROLLBAR = 1 << 11 +}; + +static int lua_init(lua_State *L) { + int argc = lua_gettop(L); +#ifndef SKIP_ERROR_HANDLING + if (argc != 0) return luaL_error(L, "wrong number of arguments"); +#endif + ImGui::CreateContext(); + ImGuiIO& io = ImGui::GetIO(); (void)io; + ImGui_ImplVita2D_Init(); + ImGui::StyleColorsDark(); + ImGui_ImplVita2D_TouchUsage(true); + ImGui_ImplVita2D_UseIndirectFrontTouch(true); + ImGui_ImplVita2D_UseRearTouch(false); + ImGui_ImplVita2D_GamepadUsage(true); + + return 0; +} + +static int lua_config(lua_State *L) { + int argc = lua_gettop(L); +#ifndef SKIP_ERROR_HANDLING + if (argc != 4) return luaL_error(L, "wrong number of arguments"); +#endif + bool use_touch = lua_toboolean(L, 1); + bool use_rearpad = lua_toboolean(L, 2); + bool use_buttons = lua_toboolean(L, 3); + bool use_indirect_touch = lua_toboolean(L, 4); + ImGui_ImplVita2D_TouchUsage(use_touch); + ImGui_ImplVita2D_UseIndirectFrontTouch(use_indirect_touch); + ImGui_ImplVita2D_UseRearTouch(use_rearpad); + ImGui_ImplVita2D_GamepadUsage(use_buttons); + return 0; +} + +static int lua_term(lua_State *L) { + int argc = lua_gettop(L); +#ifndef SKIP_ERROR_HANDLING + if (argc != 0) return luaL_error(L, "wrong number of arguments"); +#endif + ImGui_ImplVita2D_Shutdown(); + ImGui::DestroyContext(); + + return 0; +} + +static int lua_initblend(lua_State *L) { + int argc = lua_gettop(L); +#ifndef SKIP_ERROR_HANDLING + if (argc != 0) return luaL_error(L, "wrong number of arguments"); +#endif + ImGui_ImplVita2D_NewFrame(); + + return 0; +} + +static int lua_termblend(lua_State *L) { + int argc = lua_gettop(L); +#ifndef SKIP_ERROR_HANDLING + if (argc != 0) return luaL_error(L, "wrong number of arguments"); +#endif + ImGui::Render(); + ImGui_ImplVita2D_RenderDrawData(ImGui::GetDrawData()); + + return 0; +} + +static int lua_settheme(lua_State *L) { + int argc = lua_gettop(L); +#ifndef SKIP_ERROR_HANDLING + if (argc != 1) return luaL_error(L, "wrong number of arguments"); +#endif + int idx = luaL_checkinteger(L, 1); + switch (idx) { + case DARK_THEME: + ImGui::StyleColorsDark(); + break; + case LIGHT_THEME: + ImGui::StyleColorsLight(); + break; + default: + ImGui::StyleColorsClassic(); + break; + } + + return 0; +} + +static int lua_smmenubar(lua_State *L) { + int argc = lua_gettop(L); +#ifndef SKIP_ERROR_HANDLING + if (argc != 0) return luaL_error(L, "wrong number of arguments"); +#endif + lua_pushboolean(L, ImGui::BeginMainMenuBar()); + + return 1; +} + +static int lua_emmenubar(lua_State *L) { + int argc = lua_gettop(L); +#ifndef SKIP_ERROR_HANDLING + if (argc != 0) return luaL_error(L, "wrong number of arguments"); +#endif + ImGui::EndMainMenuBar(); + + return 0; +} + +static int lua_smenu(lua_State *L) { + int argc = lua_gettop(L); +#ifndef SKIP_ERROR_HANDLING + if (argc != 1 && argc != 2) return luaL_error(L, "wrong number of arguments"); +#endif + char *label = luaL_checkstring(L, 1); + bool enabled = true; + if (argc == 2) { + enabled = lua_toboolean(L, 2); + } + lua_pushboolean(L, ImGui::BeginMenu(label, enabled)); + + return 1; +} + +static int lua_emenu(lua_State *L) { + int argc = lua_gettop(L); +#ifndef SKIP_ERROR_HANDLING + if (argc != 0) return luaL_error(L, "wrong number of arguments"); +#endif + ImGui::EndMenu(); + + return 0; +} + +static int lua_text(lua_State *L) { + int argc = lua_gettop(L); +#ifndef SKIP_ERROR_HANDLING + if (argc != 1 && argc != 2) return luaL_error(L, "wrong number of arguments"); +#endif + char *label = luaL_checkstring(L, 1); + if (argc == 1) { + ImGui::Text(label); + } else { + int color = luaL_checkinteger(L, 2); + ImGui::TextColored(ImVec4( + (float)(color & 0xFF) / 255.0f, + (float)((color >> 8) & 0xFF) / 255.0f, + (float)((color >> 16) & 0xFF) / 255.0f, + (float)((color >> 24) & 0xFF) / 255.0f), + label); + } + + return 0; +} + +static int lua_distext(lua_State *L) { + int argc = lua_gettop(L); +#ifndef SKIP_ERROR_HANDLING + if (argc != 1) return luaL_error(L, "wrong number of arguments"); +#endif + char *label = luaL_checkstring(L, 1); + ImGui::TextDisabled(label); + + return 0; +} + +static int lua_wraptext(lua_State *L) { + int argc = lua_gettop(L); +#ifndef SKIP_ERROR_HANDLING + if (argc != 1) return luaL_error(L, "wrong number of arguments"); +#endif + char *label = luaL_checkstring(L, 1); + ImGui::TextWrapped(label); + + return 0; +} + +static int lua_button(lua_State *L) { + int argc = lua_gettop(L); +#ifndef SKIP_ERROR_HANDLING + if (argc != 1 && argc != 3) return luaL_error(L, "wrong number of arguments"); +#endif + char *label = luaL_checkstring(L, 1); + float sizex = 0; + float sizey = 0; + if (argc == 3) { + sizex = luaL_checknumber(L, 2); + sizey = luaL_checknumber(L, 3); + } + lua_pushboolean(L, ImGui::Button(label, ImVec2(sizex, sizey))); + + return 1; +} + +static int lua_sbutton(lua_State *L) { + int argc = lua_gettop(L); +#ifndef SKIP_ERROR_HANDLING + if (argc != 1) return luaL_error(L, "wrong number of arguments"); +#endif + char *label = luaL_checkstring(L, 1); + lua_pushboolean(L, ImGui::SmallButton(label)); + + return 1; +} + +static int lua_checkbox(lua_State *L) { + int argc = lua_gettop(L); +#ifndef SKIP_ERROR_HANDLING + if (argc != 2) return luaL_error(L, "wrong number of arguments"); +#endif + char *label = luaL_checkstring(L, 1); + bool status = lua_toboolean(L, 2); + ImGui::Checkbox(label, &status); + lua_pushboolean(L, status); + + return 1; +} + +static int lua_radiobutton(lua_State *L) { + int argc = lua_gettop(L); +#ifndef SKIP_ERROR_HANDLING + if (argc != 2) return luaL_error(L, "wrong number of arguments"); +#endif + char *label = luaL_checkstring(L, 1); + bool status = lua_toboolean(L, 2); + lua_pushboolean(L, ImGui::RadioButton(label, status)); + + return 1; +} + +static int lua_sameline(lua_State *L) { + int argc = lua_gettop(L); +#ifndef SKIP_ERROR_HANDLING + if (argc != 0) return luaL_error(L, "wrong number of arguments"); +#endif + ImGui::SameLine(); + + return 0; +} + +static int lua_swindow(lua_State *L) { + int argc = lua_gettop(L); +#ifndef SKIP_ERROR_HANDLING + if (argc != 1 && argc != 2) return luaL_error(L, "wrong number of arguments"); +#endif + char *label = luaL_checkstring(L, 1); + + ImGuiWindowFlags flags = FLAG_NONE; + if (argc == 2) { + flags = luaL_checkinteger(L, 2); + } + + ImGui::Begin(label, nullptr, flags); + + return 0; +} + +static int lua_ewindow(lua_State *L) { + int argc = lua_gettop(L); +#ifndef SKIP_ERROR_HANDLING + if (argc != 0) return luaL_error(L, "wrong number of arguments"); +#endif + ImGui::End(); + + return 0; +} + +static int lua_winpos(lua_State *L) { + int argc = lua_gettop(L); +#ifndef SKIP_ERROR_HANDLING + if (argc != 3) return luaL_error(L, "wrong number of arguments"); +#endif + float x = luaL_checknumber(L, 1); + float y = luaL_checknumber(L, 2); + int mode = luaL_checkinteger(L, 3); + ImGuiCond flags; + switch (mode) { + case SET_ONCE: + flags = ImGuiCond_Once; + break; + default: + flags = ImGuiCond_Always; + break; + } + ImGui::SetNextWindowPos(ImVec2(x, y), flags); + + return 0; +} + +static int lua_winsize(lua_State *L) { + int argc = lua_gettop(L); +#ifndef SKIP_ERROR_HANDLING + if (argc != 3) return luaL_error(L, "wrong number of arguments"); +#endif + float x = luaL_checknumber(L, 1); + float y = luaL_checknumber(L, 2); + int mode = luaL_checkinteger(L, 3); + ImGuiCond flags; + switch (mode) { + case SET_ONCE: + flags = ImGuiCond_Once; + break; + default: + flags = ImGuiCond_Always; + break; + } + ImGui::SetNextWindowSize(ImVec2(x, y), flags); + + return 0; +} + +static int lua_separator(lua_State *L) { + int argc = lua_gettop(L); +#ifndef SKIP_ERROR_HANDLING + if (argc != 0) return luaL_error(L, "wrong number of arguments"); +#endif + ImGui::Separator(); + + return 0; +} + +static int lua_slider(lua_State *L) { + int argc = lua_gettop(L); +#ifndef SKIP_ERROR_HANDLING + if (argc < 4 && argc > 7) return luaL_error(L, "wrong number of arguments"); +#endif + char *label = luaL_checkstring(L, 1); + float v_min = luaL_checknumber(L, 2); + float v_max = luaL_checknumber(L, 3); + + float vals[4]; + vals[0] = luaL_checknumber(L, 4); + + switch (argc) { + case 4: + ImGui::SliderFloat(label, vals, v_min, v_max); + lua_pushnumber(L, vals[0]); + break; + case 5: + vals[1] = luaL_checknumber(L, 5); + ImGui::SliderFloat2(label, vals, v_min, v_max); + lua_pushnumber(L, vals[0]); + lua_pushnumber(L, vals[1]); + break; + case 6: + vals[1] = luaL_checknumber(L, 5); + vals[2] = luaL_checknumber(L, 6); + ImGui::SliderFloat3(label, vals, v_min, v_max); + lua_pushnumber(L, vals[0]); + lua_pushnumber(L, vals[1]); + lua_pushnumber(L, vals[2]); + break; + + default: + vals[1] = luaL_checknumber(L, 5); + vals[2] = luaL_checknumber(L, 6); + vals[3] = luaL_checknumber(L, 7); + ImGui::SliderFloat4(label, vals, v_min, v_max); + lua_pushnumber(L, vals[0]); + lua_pushnumber(L, vals[1]); + lua_pushnumber(L, vals[2]); + lua_pushnumber(L, vals[3]); + break; + } + + return argc - 3; +} + +static int lua_islider(lua_State *L) { + int argc = lua_gettop(L); +#ifndef SKIP_ERROR_HANDLING + if (argc < 4 && argc > 7) return luaL_error(L, "wrong number of arguments"); +#endif + char *label = luaL_checkstring(L, 1); + int v_min = luaL_checkinteger(L, 2); + int v_max = luaL_checkinteger(L, 3); + + int vals[4]; + vals[0] = luaL_checkinteger(L, 4); + + switch (argc) { + case 4: + ImGui::SliderInt(label, vals, v_min, v_max); + lua_pushinteger(L, vals[0]); + break; + case 5: + vals[1] = luaL_checknumber(L, 5); + ImGui::SliderInt2(label, vals, v_min, v_max); + lua_pushinteger(L, vals[0]); + lua_pushinteger(L, vals[1]); + break; + case 6: + vals[1] = luaL_checknumber(L, 5); + vals[2] = luaL_checknumber(L, 6); + ImGui::SliderInt3(label, vals, v_min, v_max); + lua_pushinteger(L, vals[0]); + lua_pushinteger(L, vals[1]); + lua_pushinteger(L, vals[2]); + break; + + default: + vals[1] = luaL_checkinteger(L, 5); + vals[2] = luaL_checkinteger(L, 6); + vals[3] = luaL_checkinteger(L, 7); + ImGui::SliderInt4(label, vals, v_min, v_max); + lua_pushinteger(L, vals[0]); + lua_pushinteger(L, vals[1]); + lua_pushinteger(L, vals[2]); + lua_pushinteger(L, vals[3]); + break; + } + + return argc - 3; +} + +static int lua_mitem(lua_State *L) { + int argc = lua_gettop(L); +#ifndef SKIP_ERROR_HANDLING + if (argc != 1 && argc != 2 && argc != 3) return luaL_error(L, "wrong number of arguments"); +#endif + char *label = luaL_checkstring(L, 1); + bool selected = false; + bool enabled = true; + if (argc > 1) { + selected = lua_toboolean(L, 2); + if (argc == 3) enabled = lua_toboolean(L, 3); + } + lua_pushboolean(L, ImGui::MenuItem(label, nullptr, selected, enabled)); + + return 1; +} + +static int lua_tooltip(lua_State *L) { + int argc = lua_gettop(L); +#ifndef SKIP_ERROR_HANDLING + if (argc != 1) return luaL_error(L, "wrong number of arguments"); +#endif + char *label = luaL_checkstring(L, 1); + if (ImGui::IsItemHovered()) + ImGui::SetTooltip(label); + + return 0; +} + +//Register our Gui Functions +static const luaL_Reg Gui_functions[] = { + {"init", lua_init}, + {"initBlend", lua_initblend}, + {"termBlend", lua_termblend}, + {"setTheme", lua_settheme}, + {"initMainMenubar", lua_smmenubar}, + {"termMainMenubar", lua_emmenubar}, + {"initMenu", lua_smenu}, + {"termMenu", lua_emenu}, + {"drawText", lua_text}, + {"drawDisabledText", lua_distext}, + {"drawWrappedText", lua_wraptext}, + {"drawButton", lua_button}, + {"drawSmallButton", lua_sbutton}, + {"drawCheckbox", lua_checkbox}, + {"drawRadioButton", lua_radiobutton}, + {"resetLine", lua_sameline}, + {"initWindow", lua_swindow}, + {"termWindow", lua_ewindow}, + {"setWindowPos", lua_winpos}, + {"setWindowSize", lua_winsize}, + {"drawSeparator", lua_separator}, + {"drawSlider", lua_slider}, + {"drawIntSlider", lua_islider}, + {"drawMenuItem", lua_mitem}, + {"drawTooltip", lua_tooltip}, + {"setInputMode", lua_config}, + {0, 0} +}; + +void luaGui_init(lua_State *L) { + lua_newtable(L); + luaL_setfuncs(L, Gui_functions, 0); + lua_setglobal(L, "Gui"); + VariableRegister(L,DARK_THEME); + VariableRegister(L,LIGHT_THEME); + VariableRegister(L,CLASSIC_THEME); + VariableRegister(L,SET_ONCE); + VariableRegister(L,SET_ALWAYS); + VariableRegister(L,FLAG_NONE); + VariableRegister(L,FLAG_NO_COLLAPSE); + VariableRegister(L,FLAG_NO_MOVE); + VariableRegister(L,FLAG_NO_RESIZE); + VariableRegister(L,FLAG_NO_SCROLLBAR); + VariableRegister(L,FLAG_NO_TITLEBAR); + VariableRegister(L,FLAG_HORIZONTAL_SCROLLBAR); +} diff --git a/source/luaPlayer.cpp b/source/luaPlayer.cpp index e4b7b15..1594103 100644 --- a/source/luaPlayer.cpp +++ b/source/luaPlayer.cpp @@ -77,6 +77,7 @@ const char *runScript(const char* script, bool isStringBuffer) luaVideo_init(L); luaCamera_init(L); luaDatabase_init(L); + luaGui_init(L); int s = 0; const char *errMsg = NULL;