Skip to content

Commit

Permalink
Added Gui.drawComboBox.
Browse files Browse the repository at this point in the history
  • Loading branch information
Rinnegatamante committed Apr 3, 2021
1 parent 39897db commit ed89c26
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
18 changes: 18 additions & 0 deletions doc/luaGui.cpp
Expand Up @@ -431,4 +431,22 @@ class Gui {
*/
int[] drawIntSlider(string label, int val_min, int val_max, int val1,int val2, int val3, int val4);

/**
* Draw a combobox.
* \ingroup Gui
*
* @par Usage example:
* @code
* elems = {"Element 1", "Element 2", "Element 3", "Element 4"}
* combo_idx = Gui.drawComboBox("##combo", combo_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 drawComboBox(string label, int index, table elements);
}
25 changes: 25 additions & 0 deletions source/luaGui.cpp
Expand Up @@ -501,6 +501,30 @@ static int lua_tooltip(lua_State *L) {
return 0;
}

static int lua_combobox(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);
lua_rawgeti(L, 3, idx);
if (ImGui::BeginCombo(label, lua_tostring(L, -1))) {
uint32_t len = lua_objlen(L, 3);
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::EndCombo();
}
lua_pushinteger(L, idx);
return 1;
}

//Register our Gui Functions
static const luaL_Reg Gui_functions[] = {
{"init", lua_init},
Expand Down Expand Up @@ -529,6 +553,7 @@ static const luaL_Reg Gui_functions[] = {
{"drawMenuItem", lua_mitem},
{"drawTooltip", lua_tooltip},
{"setInputMode", lua_config},
{"drawComboBox", lua_combobox},
{0, 0}
};

Expand Down

0 comments on commit ed89c26

Please sign in to comment.