Skip to content

Commit

Permalink
Added some more functions to Gui module.
Browse files Browse the repository at this point in the history
  • Loading branch information
Rinnegatamante committed Apr 4, 2021
1 parent 4d95e55 commit 2321110
Show file tree
Hide file tree
Showing 2 changed files with 141 additions and 0 deletions.
68 changes: 68 additions & 0 deletions doc/luaGui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,44 @@ class Gui {
*/
int drawComboBox(string label, int index, table elements);

/**
* Draw a listbox.
* \ingroup Gui
*
* @par Usage example:
* @code
* elems = {"Element 1", "Element 2", "Element 3", "Element 4"}
* list_idx = Gui.drawListBox("##list", list_idx, elems)
* end
* @endcode
*
* @param label - The label to show.
* @param index - The currently selected element.
* @param elements - The elements to use for the combobox.
*
* @return The updated selected element.
*/
int drawListBox(string label, int index, table elements);

/**
* Draw a color picker.
* \ingroup Gui
*
* @par Usage example:
* @code
* color = Color.new(255, 127, 65)
* color = Gui.drawColorPicker("##color_picker", color)
* end
* @endcode
*
* @param label - The label to show.
* @param color - The currently picked color (See ::Color)
* @param alpha - If true, alpha value will be pickable, not otherwise. <b>(optional)</b>.
*
* @return The updated picked color.
*/
int drawColorPicker(string label, int color, bool alpha);

/**
* Draw a progressbar.
* \ingroup Gui
Expand Down Expand Up @@ -497,4 +535,34 @@ class Gui {
* @param y - Y coordinate in pixels.
*/
void setWidgetPos(number x, number y);

/**
* Set next widgets width.
* \ingroup Gui
*
* @par Usage example:
* @code
* Gui.setWidgetWidth(400)
* color = Color.new(255, 255, 255)
* color = Gui.drawColorPicker("##color_picker", color)
* end
* @endcode
*
* @param w - Width of the element in pixels.
*/
void setWidgetWidth(number w);

/**
* Reset next widgets width to original values.
* \ingroup Gui
*
* @par Usage example:
* @code
* Gui.resetWidgetWidth()
* color = Color.new(255, 255, 255)
* color = Gui.drawColorPicker("##color_picker", color)
* end
* @endcode
*/
void resetWidgetWidth();
}
73 changes: 73 additions & 0 deletions source/luaGui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -566,6 +566,75 @@ static int lua_progressbar(lua_State *L) {
return 0;
}

static int lua_colorpicker(lua_State *L) {
int argc = lua_gettop(L);
#ifndef SKIP_ERROR_HANDLING
if (argc != 2 && argc != 3) return luaL_error(L, "wrong number of arguments");
#endif
char *label = luaL_checkstring(L, 1);
int color = luaL_checkinteger(L, 2);
bool alpha = false;

float clr[4];
clr[0] = (float)(color & 0xFF) / 255.0f;
clr[1] = (float)((color >> 8) & 0xFF) / 255.0f;
clr[2] = (float)((color >> 16) & 0xFF) / 255.0f;
if (argc == 2) {
alpha = lua_toboolean(L, 3);
}

if (alpha) {
clr[3] = (float)((color >> 24) & 0xFF) / 255.0f;
ImGui::ColorPicker4(label, clr);
} else {
ImGui::ColorPicker3(label, clr);
}

lua_pushinteger(L, (int)(clr[0] * 255.0f) | ((int)(clr[1] * 255.0f) << 8) | ((int)(clr[2] * 255.0f) << 16) | ((int)(clr[3] * 255.0f) << 24));
return 1;
}

static int lua_widgetwidth(lua_State *L) {
int argc = lua_gettop(L);
#ifndef SKIP_ERROR_HANDLING
if (argc != 1) return luaL_error(L, "wrong number of arguments");
#endif
float w = luaL_checknumber(L, 1);
ImGui::PushItemWidth(w);
return 0;
}

static int lua_widgetwidthr(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::PopItemWidth();
return 0;
}

static int lua_listbox(lua_State *L) {
int argc = lua_gettop(L);
#ifndef SKIP_ERROR_HANDLING
if (argc != 3) return luaL_error(L, "wrong number of arguments");
#endif
char *label = luaL_checkstring(L, 1);
int idx = luaL_checkinteger(L, 2);
uint32_t len = lua_objlen(L, 3);
ImGui::ListBoxHeader(label, len);
for (int i = 1; i <= len; i++) {
bool is_selected = i == idx;
lua_rawgeti(L, 3, i);
if (ImGui::Selectable(lua_tostring(L, -1), is_selected))
idx = i;
if (is_selected)
ImGui::SetItemDefaultFocus();
}
ImGui::ListBoxFooter();
lua_pushinteger(L, idx);
return 1;
}

//Register our Gui Functions
static const luaL_Reg Gui_functions[] = {
{"init", lua_init},
Expand Down Expand Up @@ -598,6 +667,10 @@ static const luaL_Reg Gui_functions[] = {
{"setWidgetPos", lua_cursorpos},
{"getTextSize", lua_textsize},
{"drawProgressbar", lua_progressbar},
{"drawColorPicker", lua_colorpicker},
{"setWidgetWidth", lua_widgetwidth},
{"resetWidgetWidth", lua_widgetwidthr},
{"drawListBox", lua_listbox},
{0, 0}
};

Expand Down

0 comments on commit 2321110

Please sign in to comment.